BufferedSoundStream.cpp 3.6 KB

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