diff --git a/src/audio/AL/ALAudio.cpp b/src/audio/AL/ALAudio.cpp index c1798537..01d750a3 100644 --- a/src/audio/AL/ALAudio.cpp +++ b/src/audio/AL/ALAudio.cpp @@ -103,14 +103,25 @@ void ALStream::update(double delta) { std::cout << "unqueue " << buffer << std::endl; } + uint preloaded = 0; if (!unusedBuffers.empty()) { uint buffer = unusedBuffers.front(); if (preloadBuffer(buffer, loop)) { + preloaded++; unusedBuffers.pop(); std::cout << "queue " << buffer << std::endl; AL_CHECK(alSourceQueueBuffers(source, 1, &buffer)); } } + if (speaker->isStopped() && !speaker->isStoppedManually()) { + std::cout << "preloaded " << preloaded << std::endl; + if (preloaded) { + speaker->play(); + std::cout << "speaker restored" << std::endl; + } else { + speaker->stop(); + } + } } void ALStream::setTime(duration_t time) { @@ -160,6 +171,7 @@ void ALSpeaker::setLoop(bool loop) { } void ALSpeaker::play() { + stoppedManually = false; AL_CHECK(alSourcePlay(source)); } @@ -168,9 +180,16 @@ void ALSpeaker::pause() { } void ALSpeaker::stop() { - AL_CHECK(alSourceStop(source)); - al->freeSource(source); - source = 0; + stoppedManually = true; + if (source) { + AL_CHECK(alSourceStop(source)); + al->freeSource(source); + source = 0; + } +} + +bool ALSpeaker::isStoppedManually() const { + return stoppedManually; } duration_t ALSpeaker::getTime() const { diff --git a/src/audio/AL/ALAudio.h b/src/audio/AL/ALAudio.h index f8d02c2c..e2afd03b 100644 --- a/src/audio/AL/ALAudio.h +++ b/src/audio/AL/ALAudio.h @@ -73,6 +73,7 @@ namespace audio { class ALSpeaker : public Speaker { ALAudio* al; int priority; + bool stoppedManually = false; public: uint source; @@ -93,6 +94,7 @@ namespace audio { void play() override; void pause() override; void stop() override; + bool isStoppedManually() const override; duration_t getTime() const override; void setTime(duration_t time) override; diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index bf10547d..738ff3f5 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -294,7 +294,8 @@ void audio::update(double delta) { } for (auto it = speakers.begin(); it != speakers.end();) { - if (it->second->isStopped()) { + if (it->second->isStoppedManually()) { + streams.erase(it->first); it = speakers.erase(it); } else { it++; diff --git a/src/audio/audio.h b/src/audio/audio.h index 11fc49bc..1bcb3512 100644 --- a/src/audio/audio.h +++ b/src/audio/audio.h @@ -205,6 +205,9 @@ namespace audio { /// @brief Stop and destroy speaker virtual void stop() = 0; + + /// @brief Check if the speaker has stopped by calling stop() + virtual bool isStoppedManually() const = 0; /// @brief Get current time position of playing audio /// @return time position in seconds