MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
wavdecoder.cpp
1 // © Copyright 2010 - 2014 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 _audiowavdecoder_cpp
44 #define _audiowavdecoder_cpp
45 
46 #ifdef ENABLE_WAV_ENCODE
47 
48 #include "Audio/wavdecoder.h"
49 
50 #include <cstring>
51 
52 namespace Mezzanine
53 {
54  namespace Audio
55  {
56  WavDecoder::WavDecoder(Resource::DataStreamPtr Stream)
57  : WavStream(Stream),
58  Valid(false),
59  Channels(0),
60  BlockAlign(0),
61  BitsPerSample(0),
62  SampleRate(0),
63  ByteRate(0),
64  DataSize(0),
65  DataOffset(0)
66  {
67  const char* RIFFTAG = "RIFF";
68  const char* WAVETAG = "WAVE";
69  const char* FORMATTAG = "fmt ";
70  const char* DATATAG = "data";
71 
72  char Ident[4];
73  Int32 Temp32 = 0;
74  Int16 Temp16 = 0;
75  //Int8 Temp8 = 0;
76  UInt32 StartOffset = 0;
77 
78  //Read the first 4 bytes
79  this->WavStream->SetStreamPosition(0);
80  this->WavStream->Read(Ident,4);
81  // ©heck to see if it is a valid RIFF file
82  if( strncmp(Ident,RIFFTAG,4) == 0 )
83  {
84  this->WavStream->Read(&Temp32,4);
85  // ©heck to see if the file is big enough to be valid (not completely accurate)
86  if( Temp32 >= 44 )
87  {
88  this->WavStream->Read(Ident,4);
89  // ©heck that it is a wave file
90  if( strncmp(Ident,WAVETAG,4) == 0 )
91  {
92  //Save our position
93  StartOffset = this->WavStream->GetStreamPosition();
94  //Scan for the first fmt chuck (not necessarily right after)
95  do{
96  this->WavStream->Read(Ident,4);
97  } while( ( strncmp(Ident,FORMATTAG,4) != 0 ) && ( this->WavStream->GetStreamPosition() < this->WavStream->GetSize() ) );
98  //Did we find it?
99  if( this->WavStream->GetStreamPosition() < ( this->WavStream->GetSize() - 16 ) )
100  {
101  //Yes, read it in
102  this->WavStream->Read(&Temp32,4);
103  if( Temp32 >= 16 )
104  {
105  // ©heck that it is in PCM format, we don't support compressed wavs
106  this->WavStream->Read(&Temp16,2);
107  this->Channels = Temp16;
108  //We only support mono or stereo wavs
109  if( this->Channels == 1 || this->Channels == 2 )
110  {
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;
119 
120  //We only support 8 bit or 16 bit wavs
121  if( this->BitsPerSample == 8 || this->BitsPerSample == 16 )
122  {
123  //Reset our pointer to start scanning for the data block
124  this->WavStream->SetStreamPosition(StartOffset);
125  //Scan for the first data chuck (not necessarily right after)
126  do{
127  this->WavStream->Read(Ident,4);
128  } while( ( strncmp(Ident,DATATAG,4) != 0 ) && ( this->WavStream->GetStreamPosition() < this->WavStream->GetSize() ) );
129 
130  //Did we find it?
131  if( this->WavStream->GetStreamPosition() < this->WavStream->GetSize() )
132  {
133  //Get size of data block
134  this->WavStream->Read(&Temp32,4);
135  this->DataSize = Temp32;
136  this->DataOffset = this->WavStream->GetStreamPosition();
137  this->Valid = true;
138  }
139  }
140  }
141  }
142  }
143  }
144  }
145  }
146  }
147 
148  WavDecoder::~WavDecoder()
149  {
150  }
151 
152  ///////////////////////////////////////////////////////////////////////////////
153  // Utility
154 
155  bool WavDecoder::IsValid()
156  {
157  return this->Valid;
158  }
159 
160  Audio::Encoding WavDecoder::GetEncoding() const
161  {
162  return Audio::Enc_WAV;
163  }
164 
165  bool WavDecoder::IsSeekingSupported()
166  {
167  return true;
168  }
169 
170  Audio::BitConfig WavDecoder::GetBitConfiguration() const
171  {
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;
180  else
181  return Audio::BC_8Bit_Mono;//fallback return
182  }
183 
184  UInt32 WavDecoder::GetFrequency() const
185  {
186  return this->SampleRate;
187  }
188 
189  Resource::DataStreamPtr WavDecoder::GetStream() const
190  {
191  return this->WavStream;
192  }
193 
194  bool WavDecoder::SetPosition(Int32 Position, bool Relative)
195  {
196  Int32 CurrPos = this->WavStream->GetStreamPosition();
197  Int32 StartPos = this->DataOffset;
198  Int32 EndPos = this->DataOffset + this->DataSize;
199 
200  if( Relative ) {
201  if( CurrPos + Position < StartPos ) Position = StartPos;
202  else if( CurrPos + Position > EndPos ) Position = EndPos;
203  }else{
204  if( Position < StartPos ) Position = StartPos;
205  else if( Position > EndPos ) Position = EndPos;
206  }
207 
208  this->WavStream->SetStreamPosition(Position);
209  return true;
210  }
211 
212  bool WavDecoder::Seek(const Real Seconds, bool Relative)
213  {
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);
216  }
217 
218  UInt32 WavDecoder::ReadAudioData(void* Output, UInt32 Amount)
219  {
220  Int32 CurrPos = this->WavStream->GetStreamPosition();
221  Int32 StartPos = this->DataOffset;
222  Int32 EndPos = this->DataOffset + this->DataSize;
223  Int32 ReadClamped = Amount;
224 
225  if( CurrPos > EndPos )
226  return 0;
227 
228  if( CurrPos < StartPos )
229  {
230  this->WavStream->SetStreamPosition(StartPos);
231  CurrPos = this->WavStream->GetStreamPosition();
232  }
233 
234  if( CurrPos + ReadClamped > EndPos )
235  ReadClamped = EndPos - CurrPos;
236 
237  if( ReadClamped < 0 )
238  return 0;
239 
240  return this->WavStream->Read(Output,ReadClamped);
241  }
242 
243  ///////////////////////////////////////////////////////////////////////////////
244  // Stream Stats
245 
246  Real WavDecoder::GetTotalTime() const
247  {
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;
250  }
251 
252  Real WavDecoder::GetCurrentTime() const
253  {
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;
256  }
257 
258  UInt32 WavDecoder::GetTotalSize() const
259  {
260  return this->WavStream->GetSize();
261  }
262 
263  UInt32 WavDecoder::GetCompressedSize() const
264  {
265  return this->WavStream->GetSize();
266  }
267 
268  UInt32 WavDecoder::GetCurrentPosition() const
269  {
270  return this->WavStream->GetStreamPosition();
271  }
272 
273  UInt32 WavDecoder::GetCurrentCompressedPosition() const
274  {
275  return this->WavStream->GetStreamPosition();
276  }
277  }//Audio
278 }//Mezzanine
279 
280 #endif //ENABLE_WAV_ENCODE
281 
282 #endif