BufferedSoundStream.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // Copyright (c) 2008-2020 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/BufferedSoundStream.h"
  24. #include "../DebugNew.h"
  25. namespace Urho3D
  26. {
  27. BufferedSoundStream::BufferedSoundStream() :
  28. position_(0)
  29. {
  30. }
  31. BufferedSoundStream::~BufferedSoundStream() = default;
  32. unsigned BufferedSoundStream::GetData(signed char* dest, unsigned numBytes)
  33. {
  34. MutexLock lock(bufferMutex_);
  35. unsigned outBytes = 0;
  36. while (numBytes && buffers_.Size())
  37. {
  38. // Copy as much from the front buffer as possible, then discard it and move to the next
  39. List<Pair<SharedArrayPtr<signed char>, unsigned> >::Iterator front = buffers_.Begin();
  40. unsigned copySize = front->second_ - position_;
  41. if (copySize > numBytes)
  42. copySize = numBytes;
  43. memcpy(dest, front->first_.Get() + position_, copySize);
  44. position_ += copySize;
  45. if (position_ >= front->second_)
  46. {
  47. buffers_.PopFront();
  48. position_ = 0;
  49. }
  50. dest += copySize;
  51. outBytes += copySize;
  52. numBytes -= copySize;
  53. }
  54. return outBytes;
  55. }
  56. void BufferedSoundStream::AddData(void* data, unsigned numBytes)
  57. {
  58. if (data && numBytes)
  59. {
  60. MutexLock lock(bufferMutex_);
  61. SharedArrayPtr<signed char> newBuffer(new signed char[numBytes]);
  62. memcpy(newBuffer.Get(), data, numBytes);
  63. buffers_.Push(MakePair(newBuffer, numBytes));
  64. }
  65. }
  66. void BufferedSoundStream::AddData(const SharedArrayPtr<signed char>& data, unsigned numBytes)
  67. {
  68. if (data && numBytes)
  69. {
  70. MutexLock lock(bufferMutex_);
  71. buffers_.Push(MakePair(data, numBytes));
  72. }
  73. }
  74. void BufferedSoundStream::AddData(const SharedArrayPtr<signed short>& data, unsigned numBytes)
  75. {
  76. if (data && numBytes)
  77. {
  78. MutexLock lock(bufferMutex_);
  79. buffers_.Push(MakePair(ReinterpretCast<signed char>(data), numBytes));
  80. }
  81. }
  82. void BufferedSoundStream::Clear()
  83. {
  84. MutexLock lock(bufferMutex_);
  85. buffers_.Clear();
  86. position_ = 0;
  87. }
  88. unsigned BufferedSoundStream::GetBufferNumBytes() const
  89. {
  90. MutexLock lock(bufferMutex_);
  91. unsigned ret = 0;
  92. for (List<Pair<SharedArrayPtr<signed char>, unsigned> >::ConstIterator i = buffers_.Begin(); i != buffers_.End(); ++i)
  93. ret += i->second_;
  94. // Subtract amount of sound data played from the front buffer
  95. ret -= position_;
  96. return ret;
  97. }
  98. float BufferedSoundStream::GetBufferLength() const
  99. {
  100. return (float)GetBufferNumBytes() / (GetFrequency() * (float)GetSampleSize());
  101. }
  102. }