43 #ifndef _audiowavdecoder_cpp
44 #define _audiowavdecoder_cpp
46 #ifdef ENABLE_WAV_ENCODE
48 #include "Audio/wavdecoder.h"
67 const char* RIFFTAG =
"RIFF";
68 const char* WAVETAG =
"WAVE";
69 const char* FORMATTAG =
"fmt ";
70 const char* DATATAG =
"data";
79 this->WavStream->SetStreamPosition(0);
80 this->WavStream->Read(Ident,4);
82 if( strncmp(Ident,RIFFTAG,4) == 0 )
84 this->WavStream->Read(&Temp32,4);
88 this->WavStream->Read(Ident,4);
90 if( strncmp(Ident,WAVETAG,4) == 0 )
93 StartOffset = this->WavStream->GetStreamPosition();
96 this->WavStream->Read(Ident,4);
97 }
while( ( strncmp(Ident,FORMATTAG,4) != 0 ) && ( this->WavStream->GetStreamPosition() < this->WavStream->GetSize() ) );
99 if( this->WavStream->GetStreamPosition() < ( this->WavStream->GetSize() - 16 ) )
102 this->WavStream->Read(&Temp32,4);
106 this->WavStream->Read(&Temp16,2);
107 this->Channels = Temp16;
109 if( this->Channels == 1 || this->Channels == 2 )
111 this->WavStream->Read(&Temp32,4);
112 this->SampleRate = Temp32;
113 this->WavStream->Read(&Temp32,4);
114 this->ByteRate = Temp32;
115 this->WavStream->Read(&Temp16,2);
116 this->BlockAlign = Temp16;
117 this->WavStream->Read(&Temp16,2);
118 this->BitsPerSample = Temp16;
121 if( this->BitsPerSample == 8 || this->BitsPerSample == 16 )
124 this->WavStream->SetStreamPosition(StartOffset);
127 this->WavStream->Read(Ident,4);
128 }
while( ( strncmp(Ident,DATATAG,4) != 0 ) && ( this->WavStream->GetStreamPosition() < this->WavStream->GetSize() ) );
131 if( this->WavStream->GetStreamPosition() < this->WavStream->GetSize() )
134 this->WavStream->Read(&Temp32,4);
135 this->DataSize = Temp32;
136 this->DataOffset = this->WavStream->GetStreamPosition();
148 WavDecoder::~WavDecoder()
155 bool WavDecoder::IsValid()
165 bool WavDecoder::IsSeekingSupported()
172 if(this->Channels == 1 && this->BitsPerSample == 8)
173 return Audio::BC_8Bit_Mono;
174 else if(this->Channels == 1 && this->BitsPerSample == 16)
175 return Audio::BC_16Bit_Mono;
176 else if(this->Channels == 2 && this->BitsPerSample == 8)
177 return Audio::BC_8Bit_Stereo;
178 else if(this->Channels == 2 && this->BitsPerSample == 16)
179 return Audio::BC_16Bit_Stereo;
181 return Audio::BC_8Bit_Mono;
184 UInt32 WavDecoder::GetFrequency()
const
186 return this->SampleRate;
191 return this->WavStream;
194 bool WavDecoder::SetPosition(
Int32 Position,
bool Relative)
196 Int32 CurrPos = this->WavStream->GetStreamPosition();
197 Int32 StartPos = this->DataOffset;
198 Int32 EndPos = this->DataOffset + this->DataSize;
201 if( CurrPos + Position < StartPos ) Position = StartPos;
202 else if( CurrPos + Position > EndPos ) Position = EndPos;
204 if( Position < StartPos ) Position = StartPos;
205 else if( Position > EndPos ) Position = EndPos;
208 this->WavStream->SetStreamPosition(Position);
212 bool WavDecoder::Seek(
const Real Seconds,
bool Relative)
214 Int32 SeekInBytes = ( Seconds *
static_cast<Real>(this->SampleRate) * static_cast<Real>(this->Channels) * (
static_cast<Real>(this->BitsPerSample) / 8.0) );
215 return this->SetPosition(SeekInBytes,Relative);
218 UInt32 WavDecoder::ReadAudioData(
void* Output,
UInt32 Amount)
220 Int32 CurrPos = this->WavStream->GetStreamPosition();
221 Int32 StartPos = this->DataOffset;
222 Int32 EndPos = this->DataOffset + this->DataSize;
223 Int32 ReadClamped = Amount;
225 if( CurrPos > EndPos )
228 if( CurrPos < StartPos )
230 this->WavStream->SetStreamPosition(StartPos);
231 CurrPos = this->WavStream->GetStreamPosition();
234 if( CurrPos + ReadClamped > EndPos )
235 ReadClamped = EndPos - CurrPos;
237 if( ReadClamped < 0 )
240 return this->WavStream->Read(Output,ReadClamped);
246 Real WavDecoder::GetTotalTime()
const
248 Real Second = (
static_cast<Real>(this->SampleRate) * static_cast<Real>(this->Channels) * (
static_cast<Real>(this->BitsPerSample) / 8.0) );
249 return static_cast<Real>(this->WavStream->GetSize()) / Second;
252 Real WavDecoder::GetCurrentTime()
const
254 Real Second = (
static_cast<Real>(this->SampleRate) * static_cast<Real>(this->Channels) * (
static_cast<Real>(this->BitsPerSample) / 8.0) );
255 return static_cast<Real>(this->WavStream->GetStreamPosition()) / Second;
258 UInt32 WavDecoder::GetTotalSize()
const
260 return this->WavStream->GetSize();
263 UInt32 WavDecoder::GetCompressedSize()
const
265 return this->WavStream->GetSize();
268 UInt32 WavDecoder::GetCurrentPosition()
const
270 return this->WavStream->GetStreamPosition();
273 UInt32 WavDecoder::GetCurrentCompressedPosition()
const
275 return this->WavStream->GetStreamPosition();
280 #endif //ENABLE_WAV_ENCODE