Spinning Topp Logo BlackTopp Studios
inc
rawdecoder.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 // Copyright (c) 2008-2010 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones
41 // This file is part of the "cAudio Engine"
42 // For conditions of distribution and use, see copyright notice in cAudio-ZLIBLicense.txt
43 #ifndef _audiorawdecoder_cpp
44 #define _audiorawdecoder_cpp
45 
46 #ifdef ENABLE_RAW_ENCODE
47 
48 #include "Audio/rawdecoder.h"
49 
50 namespace Mezzanine
51 {
52  namespace Audio
53  {
54  RawDecoder::RawDecoder(Resource::DataStreamPtr Stream, const UInt32 Freq, const Audio::BitConfig Config) :
55  RawStream(Stream),
56  RawStreamSize(0),
57  RawStreamPos(0),
58  Frequency(Freq),
59  BitConfiguration(Config)
60  {
61  this->RawStream->seekg(0,std::ios_base::end);
62  this->RawStreamSize = this->RawStream->tellg();
63  this->RawStream->seekg(0);
64  }
65 
66  RawDecoder::~RawDecoder()
67  { }
68 
69  void RawDecoder::ClearStreamErrors()
70  {
71  if( this->RawStream->eof() ) {
72  this->RawStream->clear( this->RawStream->rdstate() ^ ( std::ios::eofbit | std::ios::failbit ) );
73  }
74  }
75 
76  ///////////////////////////////////////////////////////////////////////////////
77  // Utility
78 
79  Boole RawDecoder::IsValid()
80  {
81  return ( RawStream );
82  }
83 
84  Audio::Encoding RawDecoder::GetEncoding() const
85  {
86  return Audio::Enc_RAW;
87  }
88 
89  Boole RawDecoder::IsSeekingSupported()
90  {
91  return true;
92  }
93 
94  Audio::BitConfig RawDecoder::GetBitConfiguration() const
95  {
96  return this->BitConfiguration;
97  }
98 
99  UInt32 RawDecoder::GetFrequency() const
100  {
101  return this->Frequency;
102  }
103 
104  Resource::DataStreamPtr RawDecoder::GetStream() const
105  {
106  return this->RawStream;
107  }
108 
109  Boole RawDecoder::IsEndOfStream() const
110  {
111  return ( this->RawStream->eof() || this->RawStream->tellg() >= this->RawStreamSize );
112  }
113 
114  Boole RawDecoder::SetPosition(Int32 Position, const Boole Relative)
115  {
116  this->RawStream->seekg(Position,( Relative ? std::ios_base::cur : std::ios_base::beg ));
117  return true;
118  }
119 
120  Boole RawDecoder::Seek(const Real Seconds, const Boole Relative)
121  {
122  Int32 Pos = static_cast<Int32>( Seconds * static_cast<Real>(this->Frequency) * static_cast<Real>( this->GetSampleSize() ) );
123  return this->SetPosition(Pos,Relative);
124  }
125 
126  UInt32 RawDecoder::ReadAudioData(void* Output, UInt32 Amount)
127  {
128  this->RawStream->read(static_cast<char*>(Output),Amount);
129  return this->RawStream->gcount();
130  }
131 
132  ///////////////////////////////////////////////////////////////////////////////
133  // Stream Stats
134 
135  Real RawDecoder::GetTotalTime() const
136  { return static_cast<Real>( this->RawStreamSize ) / ( static_cast<Real>(this->Frequency) * static_cast<Real>( this->GetSampleSize() ) ); }
137 
138  Real RawDecoder::GetCurrentTime() const
139  { return static_cast<Real>( this->RawStream->tellg() ) / ( static_cast<Real>(this->Frequency) * static_cast<Real>( this->GetSampleSize() ) ); }
140 
141  UInt32 RawDecoder::GetTotalSize() const
142  { return this->RawStreamSize; }
143 
144  UInt32 RawDecoder::GetCompressedSize() const
145  { return this->RawStreamSize; }
146 
147  UInt32 RawDecoder::GetCurrentPosition() const
148  { return this->RawStream->tellg(); }
149 
150  UInt32 RawDecoder::GetCurrentCompressedPosition() const
151  { return this->RawStream->tellg(); }
152  }//Audio
153 }//Mezzanine
154 
155 #endif //ENABLE_RAW_ENCODE
156 
157 #endif
int32_t Int32
An 32-bit integer.
Definition: datatypes.h:124
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
Encoding
The encoding to use when reading or writing an audio buffer.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
Unknown or error condition.
CountedPtr< DataStream > DataStreamPtr
This is a convenience type for a data stream in a counted pointer.
Definition: datastream.h:383
BitConfig
Used to describe the different bit configurations supported by this audio system. ...