43 #ifndef _audiowavdecoder_cpp
44 #define _audiowavdecoder_cpp
46 #ifdef ENABLE_WAV_ENCODE
48 #include "Audio/wavdecoder.h"
69 this->WavStream->seekg(0,std::ios_base::end);
70 this->WavStreamSize = this->WavStream->tellg();
72 this->ReadWavMetaData(Stream);
75 WavDecoder::~WavDecoder()
80 const char* RIFFTAG =
"RIFF";
81 const char* WAVETAG =
"WAVE";
82 const char* FORMATTAG =
"fmt ";
83 const char* DATATAG =
"data";
92 this->WavStream->seekg(0);
93 this->WavStream->read(Ident,4);
95 if( strncmp(Ident,RIFFTAG,4) == 0 ) {
96 this->WavStream->read((
char*)&Temp32,4);
99 this->WavStream->read(Ident,4);
101 if( strncmp(Ident,WAVETAG,4) == 0 ) {
103 StartOffset = this->WavStream->tellg();
106 this->WavStream->read(Ident,4);
107 }
while( ( strncmp(Ident,FORMATTAG,4) != 0 ) && ( this->WavStream->tellg() < this->WavStreamSize ) );
109 if( this->WavStream->tellg() < ( this->WavStreamSize - 16 ) ) {
111 this->WavStream->read((
char*)&Temp32,4);
114 this->WavStream->read((
char*)&Temp16,2);
115 this->Channels = Temp16;
117 if( this->Channels == 1 || this->Channels == 2 ) {
118 this->WavStream->read((
char*)&Temp32,4);
119 this->SampleRate = Temp32;
120 this->WavStream->read((
char*)&Temp32,4);
121 this->ByteRate = Temp32;
122 this->WavStream->read((
char*)&Temp16,2);
123 this->BlockAlign = Temp16;
124 this->WavStream->read((
char*)&Temp16,2);
125 this->BitsPerSample = Temp16;
128 if( this->BitsPerSample == 8 || this->BitsPerSample == 16 ) {
130 this->WavStream->seekg(StartOffset);
133 this->WavStream->read(Ident,4);
134 }
while( ( strncmp(Ident,DATATAG,4) != 0 ) && ( this->WavStream->tellg() < this->WavStreamSize ) );
137 if( this->WavStream->tellg() < this->WavStreamSize ) {
139 this->WavStream->read((
char*)&Temp32,4);
140 this->DataSize = Temp32;
141 this->DataOffset = this->WavStream->tellg();
153 void WavDecoder::ClearStreamErrors()
155 if( this->WavStream->eof() ) {
156 this->WavStream->clear( this->WavStream->rdstate() ^ ( std::ios::eofbit | std::ios::failbit ) );
163 Boole WavDecoder::IsValid()
173 Boole WavDecoder::IsSeekingSupported()
180 if(this->Channels == 1 && this->BitsPerSample == 8)
181 return Audio::BC_8Bit_Mono;
182 else if(this->Channels == 1 && this->BitsPerSample == 16)
183 return Audio::BC_16Bit_Mono;
184 else if(this->Channels == 2 && this->BitsPerSample == 8)
185 return Audio::BC_8Bit_Stereo;
186 else if(this->Channels == 2 && this->BitsPerSample == 16)
187 return Audio::BC_16Bit_Stereo;
189 return Audio::BC_8Bit_Mono;
192 UInt32 WavDecoder::GetFrequency()
const
194 return this->SampleRate;
199 return this->WavStream;
202 Boole WavDecoder::IsEndOfStream()
const
204 return ( this->WavStream->eof() || this->WavStream->tellg() >= this->WavStreamSize );
209 Int32 CurrPos = this->WavStream->tellg();
210 Int32 StartPos = this->DataOffset;
211 Int32 EndPos = this->DataOffset + this->DataSize;
214 if( CurrPos + Position < StartPos ) Position = StartPos;
215 else if( CurrPos + Position > EndPos ) Position = EndPos;
217 if( Position < StartPos ) Position = StartPos;
218 else if( Position > EndPos ) Position = EndPos;
221 this->WavStream->seekg(Position);
227 Int32 SeekInBytes = ( Seconds *
static_cast<Real>(this->SampleRate) * static_cast<Real>(this->Channels) * (
static_cast<Real>(this->BitsPerSample) / 8.0) );
228 return this->SetPosition(SeekInBytes,Relative);
231 UInt32 WavDecoder::ReadAudioData(
void* Output,
UInt32 Amount)
233 Int32 CurrPos = this->WavStream->tellg();
234 Int32 StartPos = this->DataOffset;
235 Int32 EndPos = this->DataOffset + this->DataSize;
236 Int32 ReadClamped = Amount;
238 if( CurrPos > EndPos ) {
242 if( CurrPos < StartPos ) {
243 this->WavStream->seekg(StartPos);
244 CurrPos = this->WavStream->tellg();
247 if( CurrPos + ReadClamped > EndPos )
248 ReadClamped = EndPos - CurrPos;
250 if( ReadClamped < 0 )
253 this->WavStream->read(static_cast<char*>(Output),ReadClamped);
254 return this->WavStream->gcount();
260 Real WavDecoder::GetTotalTime()
const
262 Real Second = (
static_cast<Real>(this->SampleRate) * static_cast<Real>(this->Channels) * (
static_cast<Real>(this->BitsPerSample) / 8.0) );
263 return static_cast<Real>(this->WavStreamSize) / Second;
266 Real WavDecoder::GetCurrentTime()
const
268 Real Second = (
static_cast<Real>(this->SampleRate) * static_cast<Real>(this->Channels) * (
static_cast<Real>(this->BitsPerSample) / 8.0) );
269 return static_cast<Real>(this->WavStream->tellg()) / Second;
272 UInt32 WavDecoder::GetTotalSize()
const
274 return this->WavStreamSize;
277 UInt32 WavDecoder::GetCompressedSize()
const
279 return this->WavStreamSize;
282 UInt32 WavDecoder::GetCurrentPosition()
const
284 return this->WavStream->tellg();
287 UInt32 WavDecoder::GetCurrentCompressedPosition()
const
289 return this->WavStream->tellg();
294 #endif //ENABLE_WAV_ENCODE
int32_t Int32
An 32-bit integer.
bool Boole
Generally acts a single bit, true or false.
float Real
A Datatype used to represent a real floating point number.
uint32_t UInt32
An 32-bit unsigned integer.
int16_t Int16
An 16-bit integer.
Encoding
The encoding to use when reading or writing an audio buffer.
The bulk of the engine components go in this namspace.
CountedPtr< DataStream > DataStreamPtr
This is a convenience type for a data stream in a counted pointer.
BitConfig
Used to describe the different bit configurations supported by this audio system. ...