stream now restores speaker after long freezes

This commit is contained in:
MihailRis 2024-03-03 14:49:47 +03:00
parent 011bdd8397
commit cd657a1234
4 changed files with 29 additions and 4 deletions

View File

@ -103,14 +103,25 @@ void ALStream::update(double delta) {
std::cout << "unqueue " << buffer << std::endl; std::cout << "unqueue " << buffer << std::endl;
} }
uint preloaded = 0;
if (!unusedBuffers.empty()) { if (!unusedBuffers.empty()) {
uint buffer = unusedBuffers.front(); uint buffer = unusedBuffers.front();
if (preloadBuffer(buffer, loop)) { if (preloadBuffer(buffer, loop)) {
preloaded++;
unusedBuffers.pop(); unusedBuffers.pop();
std::cout << "queue " << buffer << std::endl; std::cout << "queue " << buffer << std::endl;
AL_CHECK(alSourceQueueBuffers(source, 1, &buffer)); 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) { void ALStream::setTime(duration_t time) {
@ -160,6 +171,7 @@ void ALSpeaker::setLoop(bool loop) {
} }
void ALSpeaker::play() { void ALSpeaker::play() {
stoppedManually = false;
AL_CHECK(alSourcePlay(source)); AL_CHECK(alSourcePlay(source));
} }
@ -168,10 +180,17 @@ void ALSpeaker::pause() {
} }
void ALSpeaker::stop() { void ALSpeaker::stop() {
stoppedManually = true;
if (source) {
AL_CHECK(alSourceStop(source)); AL_CHECK(alSourceStop(source));
al->freeSource(source); al->freeSource(source);
source = 0; source = 0;
} }
}
bool ALSpeaker::isStoppedManually() const {
return stoppedManually;
}
duration_t ALSpeaker::getTime() const { duration_t ALSpeaker::getTime() const {
return static_cast<duration_t>(AL::getSourcef(source, AL_SEC_OFFSET)); return static_cast<duration_t>(AL::getSourcef(source, AL_SEC_OFFSET));

View File

@ -73,6 +73,7 @@ namespace audio {
class ALSpeaker : public Speaker { class ALSpeaker : public Speaker {
ALAudio* al; ALAudio* al;
int priority; int priority;
bool stoppedManually = false;
public: public:
uint source; uint source;
@ -93,6 +94,7 @@ namespace audio {
void play() override; void play() override;
void pause() override; void pause() override;
void stop() override; void stop() override;
bool isStoppedManually() const override;
duration_t getTime() const override; duration_t getTime() const override;
void setTime(duration_t time) override; void setTime(duration_t time) override;

View File

@ -294,7 +294,8 @@ void audio::update(double delta) {
} }
for (auto it = speakers.begin(); it != speakers.end();) { for (auto it = speakers.begin(); it != speakers.end();) {
if (it->second->isStopped()) { if (it->second->isStoppedManually()) {
streams.erase(it->first);
it = speakers.erase(it); it = speakers.erase(it);
} else { } else {
it++; it++;

View File

@ -206,6 +206,9 @@ namespace audio {
/// @brief Stop and destroy speaker /// @brief Stop and destroy speaker
virtual void stop() = 0; 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 /// @brief Get current time position of playing audio
/// @return time position in seconds /// @return time position in seconds
virtual duration_t getTime() const = 0; virtual duration_t getTime() const = 0;