sfxWavStream.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "sfx/media/sfxWavStream.h"
  23. #include "core/stream/stream.h"
  24. #include "core/strings/stringFunctions.h"
  25. /// WAV File-header
  26. struct WAVFileHdr
  27. {
  28. U8 id[4];
  29. U32 size;
  30. U8 type[4];
  31. };
  32. //// WAV Fmt-header
  33. struct WAVFmtHdr
  34. {
  35. U16 format;
  36. U16 channels;
  37. U32 samplesPerSec;
  38. U32 bytesPerSec;
  39. U16 blockAlign;
  40. U16 bitsPerSample;
  41. };
  42. /// WAV FmtEx-header
  43. struct WAVFmtExHdr
  44. {
  45. U16 size;
  46. U16 samplesPerBlock;
  47. };
  48. /// WAV Smpl-header
  49. struct WAVSmplHdr
  50. {
  51. U32 manufacturer;
  52. U32 product;
  53. U32 samplePeriod;
  54. U32 note;
  55. U32 fineTune;
  56. U32 SMPTEFormat;
  57. U32 SMPTEOffest;
  58. U32 loops;
  59. U32 samplerData;
  60. struct
  61. {
  62. U32 identifier;
  63. U32 type;
  64. U32 start;
  65. U32 end;
  66. U32 fraction;
  67. U32 count;
  68. } loop[1];
  69. };
  70. /// WAV Chunk-header
  71. struct WAVChunkHdr
  72. {
  73. U8 id[4];
  74. U32 size;
  75. };
  76. SFXWavStream* SFXWavStream::create( Stream *stream )
  77. {
  78. SFXWavStream *sfxStream = new SFXWavStream();
  79. if ( sfxStream->open( stream, true ) )
  80. return sfxStream;
  81. delete sfxStream;
  82. return NULL;
  83. }
  84. SFXWavStream::SFXWavStream()
  85. :mDataStart(-1)
  86. {
  87. }
  88. SFXWavStream::SFXWavStream( const SFXWavStream& cloneFrom )
  89. : Parent( cloneFrom ),
  90. mDataStart( cloneFrom.mDataStart )
  91. {
  92. }
  93. SFXWavStream::~SFXWavStream()
  94. {
  95. // We must call close from our own destructor
  96. // and not the base class... as it causes a
  97. // pure virtual runtime assertion.
  98. close();
  99. }
  100. void SFXWavStream::_close()
  101. {
  102. mDataStart = -1;
  103. }
  104. bool SFXWavStream::_readHeader()
  105. {
  106. // We read the wav chunks to gather than header info
  107. // and find the start and end position of the data chunk.
  108. mDataStart = -1;
  109. WAVFileHdr fileHdr;
  110. mStream->read( 4, &fileHdr.id[0] );
  111. mStream->read( &fileHdr.size );
  112. mStream->read( 4, &fileHdr.type[0] );
  113. fileHdr.size=((fileHdr.size+1)&~1)-4;
  114. WAVChunkHdr chunkHdr;
  115. mStream->read( 4, &chunkHdr.id[0] );
  116. mStream->read( &chunkHdr.size );
  117. // Unread chunk data rounded up to nearest WORD.
  118. S32 chunkRemaining = chunkHdr.size + ( chunkHdr.size & 1 );
  119. WAVFmtHdr fmtHdr;
  120. WAVFmtExHdr fmtExHdr;
  121. WAVSmplHdr smplHdr;
  122. dMemset(&fmtHdr, 0, sizeof(fmtHdr));
  123. while ((fileHdr.size!=0) && (mStream->getStatus() != Stream::EOS))
  124. {
  125. // WAV format header chunk.
  126. if ( !dStrncmp( (const char*)chunkHdr.id, "fmt ", 4 ) )
  127. {
  128. mStream->read(&fmtHdr.format);
  129. mStream->read(&fmtHdr.channels);
  130. mStream->read(&fmtHdr.samplesPerSec);
  131. mStream->read(&fmtHdr.bytesPerSec);
  132. mStream->read(&fmtHdr.blockAlign);
  133. mStream->read(&fmtHdr.bitsPerSample);
  134. if ( fmtHdr.format == 0x0001 )
  135. {
  136. mFormat.set( fmtHdr.channels, fmtHdr.bitsPerSample * fmtHdr.channels, fmtHdr.samplesPerSec );
  137. chunkRemaining -= sizeof( WAVFmtHdr );
  138. }
  139. else
  140. {
  141. mStream->read(sizeof(WAVFmtExHdr), &fmtExHdr);
  142. chunkRemaining -= sizeof(WAVFmtExHdr);
  143. }
  144. }
  145. // WAV data chunk
  146. else if (!dStrncmp((const char*)chunkHdr.id,"data",4))
  147. {
  148. // TODO: Handle these other formats in a more graceful manner!
  149. if (fmtHdr.format==0x0001)
  150. {
  151. mDataStart = mStream->getPosition();
  152. mStream->setPosition( mDataStart + chunkHdr.size );
  153. chunkRemaining -= chunkHdr.size;
  154. mSamples = chunkHdr.size / mFormat.getBytesPerSample();
  155. }
  156. else if (fmtHdr.format==0x0011)
  157. {
  158. //IMA ADPCM
  159. }
  160. else if (fmtHdr.format==0x0055)
  161. {
  162. //MP3 WAVE
  163. }
  164. }
  165. // WAV sample header
  166. else if (!dStrncmp((const char*)chunkHdr.id,"smpl",4))
  167. {
  168. // this struct read is NOT endian safe but it is ok because
  169. // we are only testing the loops field against ZERO
  170. mStream->read(sizeof(WAVSmplHdr), &smplHdr);
  171. // This has never been hooked up and its usefulness is
  172. // dubious. Do we really want the audio file overriding
  173. // the SFXDescription setting?
  174. //mLooping = ( smplHdr.loops ? true : false );
  175. chunkRemaining -= sizeof(WAVSmplHdr);
  176. }
  177. // either we have unread chunk data or we found an unknown chunk type
  178. // loop and read up to 1K bytes at a time until we have
  179. // read to the end of this chunk
  180. AssertFatal(chunkRemaining >= 0, "AudioBuffer::readWAV: remaining chunk data should never be less than zero.");
  181. if ( chunkRemaining > 0 )
  182. {
  183. U32 pos = mStream->getPosition();
  184. mStream->setPosition( pos + chunkRemaining );
  185. chunkRemaining = 0;
  186. }
  187. fileHdr.size-=(((chunkHdr.size+1)&~1)+8);
  188. // read next chunk header...
  189. mStream->read(4, &chunkHdr.id[0]);
  190. mStream->read(&chunkHdr.size);
  191. // unread chunk data rounded up to nearest WORD
  192. chunkRemaining = chunkHdr.size + (chunkHdr.size&1);
  193. }
  194. return ( mDataStart != -1 );
  195. }
  196. void SFXWavStream::reset()
  197. {
  198. AssertFatal( mStream, "SFXWavStream::reset() - Stream is null!" );
  199. AssertFatal( mDataStart != -1, "SFXWavStream::seek() - Data start offset is invalid!" );
  200. mStream->setPosition( mDataStart );
  201. }
  202. U32 SFXWavStream::getPosition() const
  203. {
  204. AssertFatal( mStream, "SFXWavStream::getPosition() - Stream is null!" );
  205. return ( mStream->getPosition() - mDataStart );
  206. }
  207. void SFXWavStream::setPosition( U32 offset )
  208. {
  209. AssertFatal( mStream, "SFXWavStream::setPosition() - Stream is null!" );
  210. offset -= offset % mFormat.getBytesPerSample();
  211. const U32 dataLength = mSamples * mFormat.getBytesPerSample();
  212. if( offset > dataLength )
  213. offset = dataLength;
  214. AssertFatal( mDataStart != -1, "SFXWavStream::getPosition() - Data start offset is invalid!" );
  215. U32 byte = mDataStart + offset;
  216. mStream->setPosition( byte );
  217. }
  218. U32 SFXWavStream::read( U8 *buffer, U32 bytes )
  219. {
  220. AssertFatal( mStream, "SFXWavStream::seek() - Stream is null!" );
  221. // Read in even sample chunks.
  222. bytes -= bytes % mFormat.getBytesPerSample();
  223. // Read the data and determine how much we've read.
  224. // FileStreams apparently report positions past
  225. // the actual stream length, so manually cap the
  226. // numbers here.
  227. const U32 oldPosition = mStream->getPosition();
  228. mStream->read( bytes, buffer );
  229. U32 newPosition = mStream->getPosition();
  230. const U32 maxPosition = getDataLength() + mDataStart;
  231. if( newPosition > maxPosition )
  232. newPosition = maxPosition;
  233. const U32 numBytesRead = newPosition - oldPosition;
  234. // TODO: Is it *just* 16 bit samples that needs to
  235. // be flipped? What about 32 bit samples?
  236. #ifdef TORQUE_BIG_ENDIAN
  237. // We need to endian-flip 16-bit data.
  238. if ( getFormat().getBytesPerChannel() == 2 )
  239. {
  240. U16 *ds = (U16*)buffer;
  241. U16 *de = (U16*)(buffer+bytes);
  242. while (ds<de)
  243. {
  244. *ds = convertLEndianToHost(*ds);
  245. ds++;
  246. }
  247. }
  248. #endif
  249. return numBytesRead;
  250. }