BufferedSoundStream.cpp 3.5 KB

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