sfxWavStream.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. {
  86. }
  87. SFXWavStream::SFXWavStream( const SFXWavStream& cloneFrom )
  88. : Parent( cloneFrom ),
  89. mDataStart( cloneFrom.mDataStart )
  90. {
  91. }
  92. SFXWavStream::~SFXWavStream()
  93. {
  94. // We must call close from our own destructor
  95. // and not the base class... as it causes a
  96. // pure virtual runtime assertion.
  97. close();
  98. }
  99. void SFXWavStream::_close()
  100. {
  101. mDataStart = -1;
  102. }
  103. bool SFXWavStream::_readHeader()
  104. {
  105. // We read the wav chunks to gather than header info
  106. // and find the start and end position of the data chunk.
  107. mDataStart = -1;
  108. WAVFileHdr fileHdr;
  109. mStream->read( 4, &fileHdr.id[0] );
  110. mStream->read( &fileHdr.size );
  111. mStream->read( 4, &fileHdr.type[0] );
  112. fileHdr.size=((fileHdr.size+1)&~1)-4;
  113. WAVChunkHdr chunkHdr;
  114. mStream->read( 4, &chunkHdr.id[0] );
  115. mStream->read( &chunkHdr.size );
  116. // Unread chunk data rounded up to nearest WORD.
  117. S32 chunkRemaining = chunkHdr.size + ( chunkHdr.size & 1 );
  118. WAVFmtHdr fmtHdr;
  119. WAVFmtExHdr fmtExHdr;
  120. WAVSmplHdr smplHdr;
  121. dMemset(&fmtHdr, 0, sizeof(fmtHdr));
  122. while ((fileHdr.size!=0) && (mStream->getStatus() != Stream::EOS))
  123. {
  124. // WAV format header chunk.
  125. if ( !dStrncmp( (const char*)chunkHdr.id, "fmt ", 4 ) )
  126. {
  127. mStream->read(&fmtHdr.format);
  128. mStream->read(&fmtHdr.channels);
  129. mStream->read(&fmtHdr.samplesPerSec);
  130. mStream->read(&fmtHdr.bytesPerSec);
  131. mStream->read(&fmtHdr.blockAlign);
  132. mStream->read(&fmtHdr.bitsPerSample);
  133. if ( fmtHdr.format == 0x0001 )
  134. {
  135. mFormat.set( fmtHdr.channels, fmtHdr.bitsPerSample * fmtHdr.channels, fmtHdr.samplesPerSec );
  136. chunkRemaining -= sizeof( WAVFmtHdr );
  137. }
  138. else
  139. {
  140. mStream->read(sizeof(WAVFmtExHdr), &fmtExHdr);
  141. chunkRemaining -= sizeof(WAVFmtExHdr);
  142. }
  143. }
  144. // WAV data chunk
  145. else if (!dStrncmp((const char*)chunkHdr.id,"data",4))
  146. {
  147. // TODO: Handle these other formats in a more graceful manner!
  148. if (fmtHdr.format==0x0001)
  149. {
  150. mDataStart = mStream->getPosition();
  151. mStream->setPosition( mDataStart + chunkHdr.size );
  152. chunkRemaining -= chunkHdr.size;
  153. mSamples = chunkHdr.size / mFormat.getBytesPerSample();
  154. }
  155. else if (fmtHdr.format==0x0011)
  156. {
  157. //IMA ADPCM
  158. }
  159. else if (fmtHdr.format==0x0055)
  160. {
  161. //MP3 WAVE
  162. }
  163. }
  164. // WAV sample header
  165. else if (!dStrncmp((const char*)chunkHdr.id,"smpl",4))
  166. {
  167. // this struct read is NOT endian safe but it is ok because
  168. // we are only testing the loops field against ZERO
  169. mStream->read(sizeof(WAVSmplHdr), &smplHdr);
  170. // This has never been hooked up and its usefulness is
  171. // dubious. Do we really want the audio file overriding
  172. // the SFXDescription setting?
  173. //mLooping = ( smplHdr.loops ? true : false );
  174. chunkRemaining -= sizeof(WAVSmplHdr);
  175. }
  176. // either we have unread chunk data or we found an unknown chunk type
  177. // loop and read up to 1K bytes at a time until we have
  178. // read to the end of this chunk
  179. AssertFatal(chunkRemaining >= 0, "AudioBuffer::readWAV: remaining chunk data should never be less than zero.");
  180. if ( chunkRemaining > 0 )
  181. {
  182. U32 pos = mStream->getPosition();
  183. mStream->setPosition( pos + chunkRemaining );
  184. chunkRemaining = 0;
  185. }
  186. fileHdr.size-=(((chunkHdr.size+1)&~1)+8);
  187. // read next chunk header...
  188. mStream->read(4, &chunkHdr.id[0]);
  189. mStream->read(&chunkHdr.size);
  190. // unread chunk data rounded up to nearest WORD
  191. chunkRemaining = chunkHdr.size + (chunkHdr.size&1);
  192. }
  193. return ( mDataStart != -1 );
  194. }
  195. void SFXWavStream::reset()
  196. {
  197. AssertFatal( mStream, "SFXWavStream::reset() - Stream is null!" );
  198. AssertFatal( mDataStart != -1, "SFXWavStream::seek() - Data start offset is invalid!" );
  199. mStream->setPosition( mDataStart );
  200. }
  201. U32 SFXWavStream::getPosition() const
  202. {
  203. AssertFatal( mStream, "SFXWavStream::getPosition() - Stream is null!" );
  204. return ( mStream->getPosition() - mDataStart );
  205. }
  206. void SFXWavStream::setPosition( U32 offset )
  207. {
  208. AssertFatal( mStream, "SFXWavStream::setPosition() - Stream is null!" );
  209. offset -= offset % mFormat.getBytesPerSample();
  210. const U32 dataLength = mSamples * mFormat.getBytesPerSample();
  211. if( offset > dataLength )
  212. offset = dataLength;
  213. AssertFatal( mDataStart != -1, "SFXWavStream::getPosition() - Data start offset is invalid!" );
  214. U32 byte = mDataStart + offset;
  215. mStream->setPosition( byte );
  216. }
  217. U32 SFXWavStream::read( U8 *buffer, U32 bytes )
  218. {
  219. AssertFatal( mStream, "SFXWavStream::seek() - Stream is null!" );
  220. // Read in even sample chunks.
  221. bytes -= bytes % mFormat.getBytesPerSample();
  222. // Read the data and determine how much we've read.
  223. // FileStreams apparently report positions past
  224. // the actual stream length, so manually cap the
  225. // numbers here.
  226. const U32 oldPosition = mStream->getPosition();
  227. mStream->read( bytes, buffer );
  228. U32 newPosition = mStream->getPosition();
  229. const U32 maxPosition = getDataLength() + mDataStart;
  230. if( newPosition > maxPosition )
  231. newPosition = maxPosition;
  232. const U32 numBytesRead = newPosition - oldPosition;
  233. // TODO: Is it *just* 16 bit samples that needs to
  234. // be flipped? What about 32 bit samples?
  235. #ifdef TORQUE_BIG_ENDIAN
  236. // We need to endian-flip 16-bit data.
  237. if ( getFormat().getBytesPerChannel() == 2 )
  238. {
  239. U16 *ds = (U16*)buffer;
  240. U16 *de = (U16*)(buffer+bytes);
  241. while (ds<de)
  242. {
  243. *ds = convertLEndianToHost(*ds);
  244. ds++;
  245. }
  246. }
  247. #endif
  248. return numBytesRead;
  249. }