diff --git a/rwengine/src/audio/SoundBuffer.hpp b/rwengine/src/audio/SoundBuffer.hpp index ce7d4bb6..e33ced70 100644 --- a/rwengine/src/audio/SoundBuffer.hpp +++ b/rwengine/src/audio/SoundBuffer.hpp @@ -20,8 +20,8 @@ struct SoundBuffer { bool isStopped() const; virtual void play(); - void pause(); - void stop(); + virtual void pause(); + virtual void stop(); void setPosition(const glm::vec3& position); void setLooping(bool looping); diff --git a/rwengine/src/audio/SoundBufferStreamed.cpp b/rwengine/src/audio/SoundBufferStreamed.cpp index bc771d46..b7e7f707 100644 --- a/rwengine/src/audio/SoundBufferStreamed.cpp +++ b/rwengine/src/audio/SoundBufferStreamed.cpp @@ -18,16 +18,18 @@ SoundBufferStreamed::SoundBufferStreamed() { } SoundBufferStreamed::~SoundBufferStreamed() { - alCheck(alSourceUnqueueBuffers(source, kNrBuffersStreaming, buffers.data())); + alCheck( + alSourceUnqueueBuffers(source, kNrBuffersStreaming, buffers.data())); alCheck(alDeleteBuffers(kNrBuffersStreaming, buffers.data())); } bool SoundBufferStreamed::bufferData(SoundSource &soundSource) { + std::lock_guard lock(soundSource.mutex); + /* Rewind the source position and clear the buffer queue */ alCheck(alSourceRewind(source)); alCheck(alSourcei(source, AL_BUFFER, 0)); - std::lock_guard lock(soundSource.mutex); /* Fill the buffer queue */ for (auto i = 0u; i < buffers.size() && streamedData * kSizeOfChunk < soundSource.data.size(); @@ -52,24 +54,31 @@ bool SoundBufferStreamed::bufferData(SoundSource &soundSource) { } void SoundBufferStreamed::play() { - alSourcePlay(source); - - running = true; + { + std::lock_guard lock(soundSource->mutex); + alSourcePlay(source); + running = true; + } loadingThread = std::async(std::launch::async, &SoundBufferStreamed::updateBuffers, this); } void SoundBufferStreamed::updateBuffers() { - while (running) { + while (true) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + std::lock_guard lock(soundSource->mutex); + if (!running) { + return; + } + ALint processed, state; /* Get relevant source info */ alCheck(alGetSourcei(source, AL_SOURCE_STATE, &state)); alCheck(alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed)); - std::lock_guard lock(soundSource->mutex); bool bufferedData = false; /* Unqueue and handle each processed buffer */ @@ -110,3 +119,14 @@ void SoundBufferStreamed::updateBuffers() { } } } + +void SoundBufferStreamed::pause() { + std::lock_guard lock(soundSource->mutex); + running = false; + alCheck(alSourcePause(source)); +} +void SoundBufferStreamed::stop() { + std::lock_guard lock(soundSource->mutex); + running = false; + alCheck(alSourceStop(source)); +} diff --git a/rwengine/src/audio/SoundBufferStreamed.hpp b/rwengine/src/audio/SoundBufferStreamed.hpp index ffb40695..dddbb5bf 100644 --- a/rwengine/src/audio/SoundBufferStreamed.hpp +++ b/rwengine/src/audio/SoundBufferStreamed.hpp @@ -14,7 +14,9 @@ struct SoundBufferStreamed : public SoundBuffer { ~SoundBufferStreamed() override; bool bufferData(SoundSource& soundSource) final; - virtual void play() final; + void play() final; + void pause() final; + void stop() final; private: SoundSource* soundSource = nullptr;