From 7820bd5aed9d19fe09b910d1e6cc7ae1aa010631 Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Sun, 6 Dec 2015 21:56:28 -0500 Subject: ACCESS: Prevent multiple copies of the same sound being queued --- engines/access/sound.cpp | 31 ++++++++++++++++++++++--------- engines/access/sound.h | 13 +++++++++++-- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/engines/access/sound.cpp b/engines/access/sound.cpp index 5fb971188a..38f544de4e 100644 --- a/engines/access/sound.cpp +++ b/engines/access/sound.cpp @@ -51,11 +51,20 @@ void SoundManager::clearSounds() { _mixer->stopHandle(_effectsHandle); while (_queue.size()) { - delete _queue[0]; + delete _queue[0]._stream; _queue.remove_at(0); } } +bool SoundManager::isSoundQueued(int soundId) const { + for (uint idx = 0; idx < _queue.size(); ++idx) { + if (_queue[idx]._soundId == soundId) + return true; + } + + return false; +} + void SoundManager::loadSoundTable(int idx, int fileNum, int subfile, int priority) { debugC(1, kDebugSound, "loadSoundTable(%d, %d, %d)", idx, fileNum, subfile); @@ -77,12 +86,15 @@ Resource *SoundManager::loadSound(int fileNum, int subfile) { void SoundManager::playSound(int soundIndex, bool loop) { debugC(1, kDebugSound, "playSound(%d, %d)", soundIndex, loop); + if (isSoundQueued(soundIndex)) + // Prevent duplicate copies of a sound from being queued + return; int priority = _soundTable[soundIndex]._priority; - playSound(_soundTable[soundIndex]._res, priority, loop); + playSound(_soundTable[soundIndex]._res, priority, loop, soundIndex); } -void SoundManager::playSound(Resource *res, int priority, bool loop) { +void SoundManager::playSound(Resource *res, int priority, bool loop, int soundIndex) { debugC(1, kDebugSound, "playSound"); byte *resourceData = res->data(); @@ -139,14 +151,15 @@ void SoundManager::playSound(Resource *res, int priority, bool loop) { error("Unknown format"); if (loop) { - _queue.push_back(new Audio::LoopingAudioStream(audioStream, 0, DisposeAfterUse::NO)); + _queue.push_back(QueuedSound(new Audio::LoopingAudioStream(audioStream, 0, + DisposeAfterUse::NO), soundIndex)); } else { - _queue.push_back(audioStream); + _queue.push_back(QueuedSound(audioStream, soundIndex)); } if (!_mixer->isSoundHandleActive(_effectsHandle)) _mixer->playStream(Audio::Mixer::kSFXSoundType, &_effectsHandle, - _queue[0], -1, _mixer->kMaxChannelVolume, 0, + _queue[0]._stream, -1, _mixer->kMaxChannelVolume, 0, DisposeAfterUse::NO); } @@ -156,12 +169,12 @@ void SoundManager::checkSoundQueue() { if (_queue.empty() || _mixer->isSoundHandleActive(_effectsHandle)) return; - delete _queue[0]; + delete _queue[0]._stream; _queue.remove_at(0); - if (_queue.size() && _queue[0]) + if (_queue.size() && _queue[0]._stream) _mixer->playStream(Audio::Mixer::kSFXSoundType, &_effectsHandle, - _queue[0], -1, _mixer->kMaxChannelVolume, 0, + _queue[0]._stream, -1, _mixer->kMaxChannelVolume, 0, DisposeAfterUse::NO); } diff --git a/engines/access/sound.h b/engines/access/sound.h index e11a6b9730..b372e566d2 100644 --- a/engines/access/sound.h +++ b/engines/access/sound.h @@ -45,15 +45,24 @@ struct SoundEntry { }; class SoundManager { + struct QueuedSound { + Audio::AudioStream *_stream; + int _soundId; + + QueuedSound() : _stream(nullptr), _soundId(-1) {} + QueuedSound(Audio::AudioStream *stream, int soundId) : _stream(stream), _soundId(soundId) {} + }; private: AccessEngine *_vm; Audio::Mixer *_mixer; Audio::SoundHandle _effectsHandle; - Common::Array _queue; + Common::Array _queue; void clearSounds(); - void playSound(Resource *res, int priority, bool loop); + void playSound(Resource *res, int priority, bool loop, int soundIndex = -1); + + bool isSoundQueued(int soundId) const; public: Common::Array _soundTable; bool _playingSound; -- cgit v1.2.3