sfxSndStream.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/sfxSndStream.h"
  23. #include "core/stream/fileStream.h"
  24. #include "core/stream/stream.h"
  25. #include "console/console.h"
  26. #define MAX_BUFFER 4096
  27. bool SFXSndStream::_readHeader()
  28. {
  29. mCurPos = 0;
  30. mBytesRead = 0;
  31. dMemset(&sfinfo, 0, sizeof(SF_INFO));
  32. vio.get_filelen = sndFileLen;
  33. vio.seek = sndSeek;
  34. vio.read = sndRead;
  35. vio.write = sndWrite;
  36. vio.tell = sndTell;
  37. vio_data.length = 0;
  38. vio_data.offset = 0;
  39. vio_data.data = mStream;
  40. if ((sndFile = sf_open_virtual(&vio, SFM_READ, &sfinfo, &vio_data)) == NULL)
  41. {
  42. Con::printf("SFXSndStream - _readHeader failed: %s", sf_strerror(sndFile));
  43. return false;
  44. }
  45. S32 bitsPerSample = 0;
  46. switch ((sfinfo.format & SF_FORMAT_SUBMASK))
  47. {
  48. case SF_FORMAT_PCM_S8:
  49. case SF_FORMAT_PCM_U8:
  50. bitsPerSample = 8;
  51. break;
  52. case SF_FORMAT_PCM_16:
  53. bitsPerSample = 16;
  54. break;
  55. case SF_FORMAT_VORBIS:
  56. case SF_FORMAT_PCM_24:
  57. case SF_FORMAT_PCM_32:
  58. case SF_FORMAT_FLOAT:
  59. bitsPerSample = 16;
  60. sf_command(sndFile, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE);
  61. break;
  62. default:
  63. // missed, set it to 16 anyway.
  64. bitsPerSample = 16;
  65. break;
  66. }
  67. mFormat.set(sfinfo.channels, bitsPerSample * sfinfo.channels, sfinfo.samplerate);
  68. mSamples = sfinfo.frames;
  69. return true;
  70. }
  71. void SFXSndStream::_close()
  72. {
  73. if (!sndFile)
  74. return;
  75. sf_close(sndFile);
  76. }
  77. SFXSndStream* SFXSndStream::create(Stream* stream)
  78. {
  79. SFXSndStream* sfxStream = new SFXSndStream();
  80. if (sfxStream->open(stream, true))
  81. return sfxStream;
  82. delete sfxStream;
  83. return nullptr;
  84. }
  85. void SFXSndStream::reset()
  86. {
  87. vio_data.offset = 0;
  88. }
  89. U32 SFXSndStream::read(U8* buffer, U32 length)
  90. {
  91. if (!sndFile)
  92. {
  93. Con::errorf("SFXSndStream - read: Called on uninitialized stream.");
  94. return 0;
  95. }
  96. U32 framesToRead = length / mFormat.getBytesPerSample();
  97. U32 framesRead = 0;
  98. switch (sfinfo.format & SF_FORMAT_SUBMASK)
  99. {
  100. case SF_FORMAT_PCM_S8:
  101. case SF_FORMAT_PCM_U8:
  102. framesRead = sf_readf_int(sndFile, (int*)buffer, framesToRead);
  103. break;
  104. case SF_FORMAT_PCM_16:
  105. case SF_FORMAT_VORBIS:
  106. case SF_FORMAT_PCM_24:
  107. case SF_FORMAT_PCM_32:
  108. case SF_FORMAT_FLOAT:
  109. framesRead = sf_readf_short(sndFile, (short*)buffer, framesToRead);
  110. break;
  111. default:
  112. Con::errorf("SFXSndStream - read: Unsupported format.");
  113. return 0;
  114. }
  115. if (framesRead != framesToRead)
  116. {
  117. Con::errorf("SFXSndStream - read: %s", sf_strerror(sndFile));
  118. }
  119. // (convert to frames) - number of frames available < MAX_BUFFER? reset
  120. if (((getPosition() / mFormat.getBytesPerSample()) - sfinfo.frames) < MAX_BUFFER)
  121. {
  122. // reset stream
  123. setPosition(0);
  124. }
  125. return framesRead * mFormat.getBytesPerSample();
  126. }
  127. bool SFXSndStream::isEOS() const
  128. {
  129. return (Parent::isEOS() || (mStream && vio_data.length == vio_data.offset));
  130. }
  131. U32 SFXSndStream::getPosition() const
  132. {
  133. return vio_data.offset;
  134. }
  135. void SFXSndStream::setPosition(U32 offset)
  136. {
  137. sf_seek(sndFile, offset / mFormat.getBytesPerSample(), SEEK_SET);
  138. }
  139. sf_count_t SFXSndStream::sndSeek(sf_count_t offset, int whence, void* user_data)
  140. {
  141. VIO_DATA* vf = (VIO_DATA*)user_data;
  142. Stream* stream = reinterpret_cast<Stream*>(vf->data);
  143. switch (whence)
  144. {
  145. case SEEK_SET:
  146. vf->offset = offset;
  147. break;
  148. case SEEK_CUR:
  149. vf->offset = vf->offset + offset;
  150. break;
  151. case SEEK_END:
  152. vf->offset = vf->length - offset;
  153. break;
  154. default:
  155. break;
  156. };
  157. return stream->setPosition(vf->offset) ? 0 : -1;
  158. }
  159. sf_count_t SFXSndStream::sndRead(void* ptr, sf_count_t count, void* user_data)
  160. {
  161. VIO_DATA* vf = (VIO_DATA*)user_data;
  162. Stream* stream = reinterpret_cast<Stream*>(vf->data);
  163. if (vf->offset + count > vf->length)
  164. count = vf->length - vf->offset;
  165. stream->read((U32)(count), ptr);
  166. vf->offset += count;
  167. return count;
  168. }
  169. sf_count_t SFXSndStream::sndWrite(const void* ptr, sf_count_t count, void* user_data)
  170. {
  171. return sf_count_t();
  172. }
  173. sf_count_t SFXSndStream::sndTell(void* user_data)
  174. {
  175. VIO_DATA* vf = (VIO_DATA*)user_data;
  176. return vf->offset;
  177. }
  178. sf_count_t SFXSndStream::sndFileLen(void* user_data)
  179. {
  180. VIO_DATA* vf = (VIO_DATA*)user_data;
  181. Stream* stream = reinterpret_cast<Stream*>(vf->data);
  182. vf->length = stream->getStreamSize();
  183. return vf->length;
  184. }