OggVorbisSoundStream.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Audio/OggVorbisSoundStream.h"
  24. #include "../Audio/Sound.h"
  25. #include <STB/stb_vorbis.h>
  26. #include "../DebugNew.h"
  27. namespace Atomic
  28. {
  29. OggVorbisSoundStream::OggVorbisSoundStream(const Sound* sound)
  30. {
  31. assert(sound && sound->IsCompressed());
  32. SetFormat(sound->GetIntFrequency(), sound->IsSixteenBit(), sound->IsStereo());
  33. // If the sound is looped, the stream will automatically rewind at end
  34. SetStopAtEnd(!sound->IsLooped());
  35. // Initialize decoder
  36. data_ = sound->GetData();
  37. dataSize_ = sound->GetDataSize();
  38. int error;
  39. decoder_ = stb_vorbis_open_memory((unsigned char*)data_.Get(), dataSize_, &error, 0);
  40. }
  41. OggVorbisSoundStream::~OggVorbisSoundStream()
  42. {
  43. // Close decoder
  44. if (decoder_)
  45. {
  46. stb_vorbis* vorbis = static_cast<stb_vorbis*>(decoder_);
  47. stb_vorbis_close(vorbis);
  48. decoder_ = 0;
  49. }
  50. }
  51. bool OggVorbisSoundStream::Seek(unsigned sample_number)
  52. {
  53. if (!decoder_)
  54. return false;
  55. stb_vorbis* vorbis = static_cast<stb_vorbis*>(decoder_);
  56. return stb_vorbis_seek(vorbis, sample_number) == 1;
  57. }
  58. unsigned OggVorbisSoundStream::GetData(signed char* dest, unsigned numBytes)
  59. {
  60. if (!decoder_)
  61. return 0;
  62. stb_vorbis* vorbis = static_cast<stb_vorbis*>(decoder_);
  63. unsigned channels = stereo_ ? 2 : 1;
  64. unsigned outSamples = (unsigned)stb_vorbis_get_samples_short_interleaved(vorbis, channels, (short*)dest, numBytes >> 1);
  65. unsigned outBytes = (outSamples * channels) << 1;
  66. // Rewind and retry if is looping and produced less output than should have
  67. if (outBytes < numBytes && !stopAtEnd_)
  68. {
  69. numBytes -= outBytes;
  70. stb_vorbis_seek_start(vorbis);
  71. outSamples =
  72. (unsigned)stb_vorbis_get_samples_short_interleaved(vorbis, channels, (short*)(dest + outBytes), numBytes >> 1);
  73. outBytes += (outSamples * channels) << 1;
  74. }
  75. return outBytes;
  76. }
  77. }