BsOAAudioClip.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsOAAudioClip.h"
  4. #include "BsOggVorbisEncoder.h"
  5. #include "BsOggVorbisDecoder.h"
  6. #include "FileSystem/BsDataStream.h"
  7. #include "BsOAAudio.h"
  8. #include "AL/al.h"
  9. namespace bs
  10. {
  11. OAAudioClip::OAAudioClip(const SPtr<DataStream>& samples, UINT32 streamSize, UINT32 numSamples, const AUDIO_CLIP_DESC& desc)
  12. :AudioClip(samples, streamSize, numSamples, desc), mNeedsDecompression(false), mBufferId((UINT32)-1), mSourceStreamSize(0)
  13. { }
  14. OAAudioClip::~OAAudioClip()
  15. {
  16. if (mBufferId != (UINT32)-1)
  17. alDeleteBuffers(1, &mBufferId);
  18. }
  19. void OAAudioClip::initialize()
  20. {
  21. {
  22. Lock lock(mMutex); // Needs to be called even if stream data is null, to ensure memory fence is added so the
  23. // other thread sees properly initialized AudioClip members
  24. AudioDataInfo info;
  25. info.bitDepth = mDesc.bitDepth;
  26. info.numChannels = mDesc.numChannels;
  27. info.numSamples = mNumSamples;
  28. info.sampleRate = mDesc.frequency;
  29. // If we need to keep source data, read everything into memory and keep a copy
  30. if (mKeepSourceData)
  31. {
  32. mStreamData->seek(mStreamOffset);
  33. UINT8* sampleBuffer = (UINT8*)bs_alloc(mStreamSize);
  34. mStreamData->read(sampleBuffer, mStreamSize);
  35. mSourceStreamData = bs_shared_ptr_new<MemoryDataStream>(sampleBuffer, mStreamSize);
  36. mSourceStreamSize = mStreamSize;
  37. }
  38. // Load decompressed data into a sound buffer
  39. bool loadDecompressed =
  40. mDesc.readMode == AudioReadMode::LoadDecompressed ||
  41. (mDesc.readMode == AudioReadMode::LoadCompressed && mDesc.format == AudioFormat::PCM);
  42. if(loadDecompressed)
  43. {
  44. // Read all data into memory
  45. SPtr<DataStream> stream;
  46. UINT32 offset = 0;
  47. if (mSourceStreamData != nullptr) // If it's already loaded in memory, use it directly
  48. stream = mSourceStreamData;
  49. else
  50. {
  51. stream = mStreamData;
  52. offset = mStreamOffset;
  53. }
  54. UINT32 bufferSize = info.numSamples * (info.bitDepth / 8);
  55. UINT8* sampleBuffer = (UINT8*)bs_stack_alloc(bufferSize);
  56. // Decompress from Ogg
  57. if (mDesc.format == AudioFormat::VORBIS)
  58. {
  59. OggVorbisDecoder reader;
  60. if (reader.open(stream, info, offset))
  61. reader.read(sampleBuffer, info.numSamples);
  62. else
  63. LOGERR("Failed decompressing AudioClip stream.");
  64. }
  65. // Load directly
  66. else
  67. {
  68. stream->seek(offset);
  69. stream->read(sampleBuffer, bufferSize);
  70. }
  71. alGenBuffers(1, &mBufferId);
  72. gOAAudio()._writeToOpenALBuffer(mBufferId, sampleBuffer, info);
  73. mStreamData = nullptr;
  74. mStreamOffset = 0;
  75. mStreamSize = 0;
  76. bs_stack_free(sampleBuffer);
  77. }
  78. // Load compressed data for streaming from memory
  79. else if(mDesc.readMode == AudioReadMode::LoadCompressed)
  80. {
  81. // If reading from file, make a copy of data in memory, otherwise just take ownership of the existing buffer
  82. if (mStreamData->isFile())
  83. {
  84. if (mSourceStreamData != nullptr) // If it's already loaded in memory, use it directly
  85. mStreamData = mSourceStreamData;
  86. else
  87. {
  88. UINT8* data = (UINT8*)bs_alloc(mStreamSize);
  89. mStreamData->seek(mStreamOffset);
  90. mStreamData->read(data, mStreamSize);
  91. mStreamData = bs_shared_ptr_new<MemoryDataStream>(data, mStreamSize);
  92. }
  93. mStreamOffset = 0;
  94. }
  95. }
  96. // Keep original stream for streaming from file
  97. else
  98. {
  99. // Do nothing
  100. }
  101. if (mDesc.format == AudioFormat::VORBIS && mDesc.readMode != AudioReadMode::LoadDecompressed)
  102. {
  103. mNeedsDecompression = true;
  104. if (mStreamData != nullptr)
  105. {
  106. if (!mVorbisReader.open(mStreamData, info, mStreamOffset))
  107. LOGERR("Failed decompressing AudioClip stream.");
  108. }
  109. }
  110. }
  111. AudioClip::initialize();
  112. }
  113. void OAAudioClip::getSamples(UINT8* samples, UINT32 offset, UINT32 count) const
  114. {
  115. Lock lock(mMutex);
  116. // Try to read from normal stream, and if that fails read from in-memory stream if it exists
  117. if (mStreamData != nullptr)
  118. {
  119. if (mNeedsDecompression)
  120. {
  121. mVorbisReader.seek(offset);
  122. mVorbisReader.read(samples, count);
  123. }
  124. else
  125. {
  126. UINT32 bytesPerSample = mDesc.bitDepth / 8;
  127. UINT32 size = count * bytesPerSample;
  128. UINT32 streamOffset = mStreamOffset + offset * bytesPerSample;
  129. mStreamData->seek(streamOffset);
  130. mStreamData->read(samples, size);
  131. }
  132. return;
  133. }
  134. if (mSourceStreamData != nullptr)
  135. {
  136. assert(!mNeedsDecompression); // Normal stream must exist if decompressing
  137. UINT32 bytesPerSample = mDesc.bitDepth / 8;
  138. UINT32 size = count * bytesPerSample;
  139. UINT32 streamOffset = offset * bytesPerSample;
  140. mSourceStreamData->seek(streamOffset);
  141. mSourceStreamData->read(samples, size);
  142. return;
  143. }
  144. LOGWRN("Attempting to read samples while sample data is not available.");
  145. }
  146. SPtr<DataStream> OAAudioClip::getSourceStream(UINT32& size)
  147. {
  148. Lock lock(mMutex);
  149. size = mSourceStreamSize;
  150. mSourceStreamData->seek(0);
  151. return mSourceStreamData;
  152. }
  153. }