| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- // Copyright (c) 2008-2023 the Urho3D project
- // License: MIT
- #include "../Precompiled.h"
- #include "../Audio/BufferedSoundStream.h"
- #include "../DebugNew.h"
- namespace Urho3D
- {
- BufferedSoundStream::BufferedSoundStream() :
- position_(0)
- {
- }
- BufferedSoundStream::~BufferedSoundStream() = default;
- unsigned BufferedSoundStream::GetData(signed char* dest, unsigned numBytes)
- {
- MutexLock lock(bufferMutex_);
- unsigned outBytes = 0;
- while (numBytes && buffers_.Size())
- {
- // Copy as much from the front buffer as possible, then discard it and move to the next
- List<Pair<SharedArrayPtr<signed char>, unsigned>>::Iterator front = buffers_.Begin();
- unsigned copySize = front->second_ - position_;
- if (copySize > numBytes)
- copySize = numBytes;
- memcpy(dest, front->first_.Get() + position_, copySize);
- position_ += copySize;
- if (position_ >= front->second_)
- {
- buffers_.PopFront();
- position_ = 0;
- }
- dest += copySize;
- outBytes += copySize;
- numBytes -= copySize;
- }
- return outBytes;
- }
- void BufferedSoundStream::AddData(void* data, unsigned numBytes)
- {
- if (data && numBytes)
- {
- MutexLock lock(bufferMutex_);
- SharedArrayPtr<signed char> newBuffer(new signed char[numBytes]);
- memcpy(newBuffer.Get(), data, numBytes);
- buffers_.Push(MakePair(newBuffer, numBytes));
- }
- }
- void BufferedSoundStream::AddData(const SharedArrayPtr<signed char>& data, unsigned numBytes)
- {
- if (data && numBytes)
- {
- MutexLock lock(bufferMutex_);
- buffers_.Push(MakePair(data, numBytes));
- }
- }
- void BufferedSoundStream::AddData(const SharedArrayPtr<signed short>& data, unsigned numBytes)
- {
- if (data && numBytes)
- {
- MutexLock lock(bufferMutex_);
- buffers_.Push(MakePair(ReinterpretCast<signed char>(data), numBytes));
- }
- }
- void BufferedSoundStream::Clear()
- {
- MutexLock lock(bufferMutex_);
- buffers_.Clear();
- position_ = 0;
- }
- unsigned BufferedSoundStream::GetBufferNumBytes() const
- {
- MutexLock lock(bufferMutex_);
- unsigned ret = 0;
- for (List<Pair<SharedArrayPtr<signed char>, unsigned>>::ConstIterator i = buffers_.Begin(); i != buffers_.End(); ++i)
- ret += i->second_;
- // Subtract amount of sound data played from the front buffer
- ret -= position_;
- return ret;
- }
- float BufferedSoundStream::GetBufferLength() const
- {
- return (float)GetBufferNumBytes() / (GetFrequency() * (float)GetSampleSize());
- }
- }
|