diff options
author | athrxx | 2019-04-01 00:55:17 +0200 |
---|---|---|
committer | athrxx | 2019-04-13 18:55:02 +0200 |
commit | 0ea6650837cce3007f0820acd17518f9809fd4a7 (patch) | |
tree | 507986e06df0d230cc2d1ee64b47efb87546b4e7 /engines/kyra/sound | |
parent | 972f4e201ade5660723f6348480437e2a02fa6de (diff) | |
download | scummvm-rg350-0ea6650837cce3007f0820acd17518f9809fd4a7.tar.gz scummvm-rg350-0ea6650837cce3007f0820acd17518f9809fd4a7.tar.bz2 scummvm-rg350-0ea6650837cce3007f0820acd17518f9809fd4a7.zip |
KYRA: (EOB2/Amiga) - implement proper sound file loading
(also fix some sound related bugs)
Diffstat (limited to 'engines/kyra/sound')
-rw-r--r-- | engines/kyra/sound/drivers/audiomaster2.cpp | 111 | ||||
-rw-r--r-- | engines/kyra/sound/sound.h | 5 | ||||
-rw-r--r-- | engines/kyra/sound/sound_amiga_eob.cpp | 74 | ||||
-rw-r--r-- | engines/kyra/sound/sound_intern.h | 9 |
4 files changed, 105 insertions, 94 deletions
diff --git a/engines/kyra/sound/drivers/audiomaster2.cpp b/engines/kyra/sound/drivers/audiomaster2.cpp index dbc6f30424..7db98879ad 100644 --- a/engines/kyra/sound/drivers/audiomaster2.cpp +++ b/engines/kyra/sound/drivers/audiomaster2.cpp @@ -292,12 +292,13 @@ private: class AudioMaster2ResourceManager { public: - AudioMaster2ResourceManager(AudioMaster2Internal *driver, Common::Mutex *mutex); + AudioMaster2ResourceManager(AudioMaster2Internal *driver, Common::Mutex &mutex); ~AudioMaster2ResourceManager(); void loadResourceFile(Common::SeekableReadStream *data); void initResource(SoundResource *resource); + void deinitResource(SoundResource *resource); void releaseResource(const Common::String &resName); void stopChain(); @@ -314,12 +315,12 @@ private: void linkToChain(SoundResource *resource, SoundResource::Mode mode); SoundResource *_chainPlaying; - SoundResource *_chainInactive; + SoundResource *_chainStorage; uint16 _masterVolume[3]; AudioMaster2Internal *_driver; - Common::Mutex *_mutex; + Common::Mutex &_mutex; }; class AudioMaster2IFFLoader : public Common::IFFParser { @@ -456,11 +457,16 @@ void SoundResource::loadName(Common::ReadStream *stream, uint32 size) { void SoundResource::open() { _refCnt++; + debugC(8, kDebugLevelSound, "SoundResource::open(): '%s', type '%s', new refCount: '%d'", _name.c_str(), (_type == 1) ? "SMUS" : (_type == 2 ? "INST" : "8SVX"), _refCnt); } void SoundResource::close() { - if (--_refCnt <= 0) + _refCnt--; + debugC(8, kDebugLevelSound, "SoundResource::close(): '%s', type '%s', new refCount: '%d' %s", _name.c_str(), (_type == 1) ? "SMUS" : (_type == 2 ? "INST" : "8SVX"), _refCnt, _refCnt <= 0 ? "--> RELEASED" : ""); + if (_refCnt == 0) { + _res->deinitResource(this); release(); + } } const Common::String &SoundResource::getName() const { @@ -656,6 +662,7 @@ void SoundResourceINST::loadSamples(Common::ReadStream *stream, uint32 size) { } else { // This will come up quite often in EOB II. But never with intruments that are actually used. No need to bother the user with a warning here. debugC(9, kDebugLevelSound, "SoundResourceINST::loadInstrument(): Samples resource '%s' not found for '%s'.", data, _name.c_str()); + _samplesResource = 0; } delete[] data; @@ -760,11 +767,7 @@ void SoundResourceSMUS::prepare() { _playFlags = 0; for (Common::Array<Track*>::iterator trk = _tracks.begin(); trk != _tracks.end(); ++trk) { (*trk)->_dataCur = (*trk)->_dataStart; - - for (Common::Array<SoundResource*>::iterator instr = _instruments.begin(); instr != _instruments.end(); ++instr) { - (*trk)->setInstrument(*instr); - break; - } + (*trk)->setInstrument(*_instruments.begin()); if (!(*trk)->_instrument) error("SoundResourceSMUS::prepare():: Unable to assign default instrument to track (resource files loaded in the wrong order?)"); @@ -864,7 +867,7 @@ const uint16 SoundResourceSMUS::_durationTable[64] = { 0x8700, 0x4380, 0x21c0, 0x10e0, 0x0870, 0x0438, 0x021c, 0x010e }; -AudioMaster2ResourceManager::AudioMaster2ResourceManager(AudioMaster2Internal *driver, Common::Mutex *mutex) : _driver(driver), _mutex(mutex), _chainPlaying(0), _chainInactive(0) { +AudioMaster2ResourceManager::AudioMaster2ResourceManager(AudioMaster2Internal *driver, Common::Mutex &mutex) : _driver(driver), _mutex(mutex), _chainPlaying(0), _chainStorage(0) { memset(_masterVolume, 0, sizeof(_masterVolume)); } @@ -898,6 +901,36 @@ void AudioMaster2ResourceManager::initResource(SoundResource *resource) { linkToChain(res, SoundResource::kIdle); } +void AudioMaster2ResourceManager::deinitResource(SoundResource *resource) { + Common::StackLock lock(_mutex); + + SoundResource *prev = 0; + for (SoundResource *cur = _chainPlaying; cur; cur = cur->_next) { + if (cur == resource) { + if (prev) + prev->_next = cur->_next; + else + _chainPlaying = cur->_next; + cur->_next = 0; + return; + } + prev = cur; + } + + prev = 0; + for (SoundResource *cur = _chainStorage; cur; cur = cur->_next) { + if (cur == resource) { + if (prev) + prev->_next = cur->_next; + else + _chainStorage = cur->_next; + cur->_next = 0; + return; + } + prev = cur; + } +} + void AudioMaster2ResourceManager::releaseResource(const Common::String &resName) { stopChain(); @@ -910,7 +943,7 @@ void AudioMaster2ResourceManager::releaseResource(const Common::String &resName) } void AudioMaster2ResourceManager::stopChain() { - Common::StackLock lock(*_mutex); + Common::StackLock lock(_mutex); SoundResource *cur = _chainPlaying; while (cur) { @@ -922,21 +955,19 @@ void AudioMaster2ResourceManager::stopChain() { } void AudioMaster2ResourceManager::flush() { - stopChain(); + Common::StackLock lock(_mutex); - Common::StackLock lock(*_mutex); + stopChain(); - for (SoundResource *res = _chainPlaying; _chainPlaying; res = _chainPlaying) { - _chainPlaying = res->_next; - res->_next = 0; - res->setPlayStatus(false); + while (_chainPlaying) { + SoundResource *res = _chainPlaying; + deinitResource(res); res->close(); } - for (SoundResource *res = _chainInactive; _chainInactive; res = _chainInactive) { - _chainInactive = res->_next; - res->_next = 0; - res->setPlayStatus(false); + while (_chainStorage) { + SoundResource *res = _chainStorage; + deinitResource(res); res->close(); } } @@ -961,7 +992,7 @@ SoundResource *AudioMaster2ResourceManager::getResource(const Common::String &re void AudioMaster2ResourceManager::setMasterVolume(int type, int volume) { assert(type == 1 || type == 2 || type == 4); - Common::StackLock lock(*_mutex); + Common::StackLock lock(_mutex); _masterVolume[type >> 1] = volume & 0xFFFF; @@ -970,7 +1001,7 @@ void AudioMaster2ResourceManager::setMasterVolume(int type, int volume) { res->setMasterVolume(volume); } - for (SoundResource *res = _chainInactive; res; res = res->_next) { + for (SoundResource *res = _chainStorage; res; res = res->_next) { if (res->getType() == type) res->setMasterVolume(volume); } @@ -988,13 +1019,13 @@ void AudioMaster2ResourceManager::interrupt(AudioMaster2IOManager *io) { cur = cur->_next; } else if (prev) { prev->_next = cur->_next; - cur->_next = _chainInactive; - _chainInactive = cur; + cur->_next = _chainStorage; + _chainStorage = cur; cur = prev->_next; } else { _chainPlaying = cur->_next; - cur->_next = _chainInactive; - _chainInactive = cur; + cur->_next = _chainStorage; + _chainStorage = cur; cur = _chainPlaying; } } @@ -1012,11 +1043,11 @@ SoundResource *AudioMaster2ResourceManager::retrieveFromChain(const Common::Stri const char *srchStr = resName.c_str(); uint32 srchDepth = strlen(srchStr); + Common::StackLock lock(_mutex); + SoundResource *cur = _chainPlaying; SoundResource *prev = 0; - Common::StackLock lock(*_mutex); - while (cur) { if (!scumm_strnicmp(cur->getName().c_str(), srchStr, srchDepth)) { if (prev) @@ -1030,7 +1061,7 @@ SoundResource *AudioMaster2ResourceManager::retrieveFromChain(const Common::Stri cur = cur->_next; } - cur = _chainInactive; + cur = _chainStorage; prev = 0; while (cur) { @@ -1038,7 +1069,7 @@ SoundResource *AudioMaster2ResourceManager::retrieveFromChain(const Common::Stri if (prev) prev->_next = cur->_next; else - _chainInactive = cur->_next; + _chainStorage = cur->_next; cur->_next = 0; return cur; } @@ -1051,13 +1082,13 @@ SoundResource *AudioMaster2ResourceManager::retrieveFromChain(const Common::Stri void AudioMaster2ResourceManager::linkToChain(SoundResource *resource, SoundResource::Mode mode) { + Common::StackLock lock(_mutex); + if (resource->getType() == 1) { stopChain(); resource->prepare(); } - Common::StackLock lock(*_mutex); - if (mode == SoundResource::kRestart) { resource->setPlayStatus(true); resource->_next = _chainPlaying; @@ -1067,8 +1098,8 @@ void AudioMaster2ResourceManager::linkToChain(SoundResource *resource, SoundReso _driver->sync(resource); } else { - resource->_next = _chainInactive; - _chainInactive = resource; + resource->_next = _chainStorage; + _chainStorage = resource; } } @@ -1203,13 +1234,13 @@ bool AudioMaster2Internal::init() { return true; _io = new AudioMaster2IOManager(); - _res = new AudioMaster2ResourceManager(this, &_mutex); + _res = new AudioMaster2ResourceManager(this, _mutex); + startPaula(); + _mixer->playStream(Audio::Mixer::kPlainSoundType, &_soundHandle, this, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO, true); - startPaula(); - _ready = true; return true; @@ -1290,9 +1321,9 @@ void AudioMaster2Internal::sync(SoundResource *res) { if (res->getType() != 1) return; - SoundResourceSMUS *smus = static_cast<SoundResourceSMUS*>(res); - Common::StackLock lock(_mutex); + + SoundResourceSMUS *smus = static_cast<SoundResourceSMUS*>(res); _io->_tempo = smus->getTempo(); smus->setSync(_io->_sync); } diff --git a/engines/kyra/sound/sound.h b/engines/kyra/sound/sound.h index 871263cf65..6c45d8b59b 100644 --- a/engines/kyra/sound/sound.h +++ b/engines/kyra/sound/sound.h @@ -161,6 +161,11 @@ public: virtual void loadSoundFile(Common::String file) = 0; /** + * Unload a specifc sound file that has been loaded before. + */ + virtual void unloadSoundFile(Common::String file) {} + + /** * Load a sound file for playing sound * effects from. */ diff --git a/engines/kyra/sound/sound_amiga_eob.cpp b/engines/kyra/sound/sound_amiga_eob.cpp index 444c85995c..f572c11c43 100644 --- a/engines/kyra/sound/sound_amiga_eob.cpp +++ b/engines/kyra/sound/sound_amiga_eob.cpp @@ -30,7 +30,7 @@ namespace Kyra { SoundAmiga_EoB::SoundAmiga_EoB(KyraEngine_v1 *vm, Audio::Mixer *mixer) : Sound(vm, mixer), - _vm(vm), _driver(0), _currentResourceSet(-1), _currentFile(-1), _levelSoundList1(0), _levelSoundList2(0), _ready(false) { + _vm(vm), _driver(0), _currentResourceSet(-1), _ready(false) { _fileBuffer = new uint8[64000]; memset(_resInfo, 0, sizeof(_resInfo)); } @@ -51,11 +51,8 @@ bool SoundAmiga_EoB::init() { if (!_driver->init()) return false; - int temp = 0; - _levelSoundList1 = _vm->staticres()->loadStrings(kEoBBaseLevelSounds1, temp); - _levelSoundList2 = _vm->staticres()->loadStrings(kEoBBaseLevelSounds2, temp); - _ready = true; + return true; } @@ -78,29 +75,12 @@ void SoundAmiga_EoB::selectAudioResourceSet(int set) { _currentResourceSet = set; } -void SoundAmiga_EoB::loadSoundFile(uint file) { - if (_vm->gameFlags().platform != Common::kPlatformAmiga || _currentResourceSet != kMusicIngame || !_ready) - return; - - unloadLevelSounds(); - - for (int i = 0; i < 2; ++i) { - if (_levelSoundList1[file * 2 + i][0]) - loadSoundFile(Common::String::format("%s.CPS", _levelSoundList1[file * 2 + i])); - if (_levelSoundList2[file * 2 + i][0]) - loadSoundFile(Common::String::format("%s.CPS", _levelSoundList2[file * 2 + i])); - } - - loadSoundFile(Common::String::format("LEVELSAM%d.CPS", file)); - - _currentFile = file; -} - void SoundAmiga_EoB::loadSoundFile(Common::String file) { if (!_ready) return; Common::SeekableReadStream *in = _vm->resource()->createReadStream(file); + debugC(6, kDebugLevelSound, "SoundAmiga_EoB::loadSoundFile(): Attempting to load sound file '%s'...%s", file.c_str(), in ? "SUCCESS" : "FILE NOT FOUND"); if (!in) return; @@ -139,6 +119,13 @@ void SoundAmiga_EoB::loadSoundFile(Common::String file) { delete[] buf; } +void SoundAmiga_EoB::unloadSoundFile(Common::String file) { + if (!_ready) + return; + debugC(5, kDebugLevelSound, "SoundAmiga_EoB::unloadSoundFile(): Attempting to free resource '%s'...%s", file.c_str(), _driver->stopSound(file) ? "SUCCESS" : "FAILURE"); + _driver->flushResource(file); +} + void SoundAmiga_EoB::playTrack(uint8 track) { if (!_musicEnabled || !_ready) return; @@ -191,21 +178,32 @@ void SoundAmiga_EoB::playSoundEffect(uint8 track, uint8 volume) { if (!_resInfo[_currentResourceSet]->soundList || track >= 120 || !_sfxEnabled) return; + if (_vm->game() == GI_EOB2 && track == 2) { + beginFadeOut(60); + return; + } + Common::String newSound = _resInfo[_currentResourceSet]->soundList[track]; + const char *suffix = (_vm->game() == GI_EOB1) ? "1.SAM" : ((track > 51 && track < 68) ? ".SMUS" : ".SAM"); if (!newSound.empty()) { if (volume == 255) { - if (_driver->startSound(newSound + "1.SAM")) { - _lastSound = newSound + "1.SAM"; + if (_driver->startSound(newSound + suffix)) { + _lastSound = newSound + suffix; return; } else { volume = 1; } } - if (volume > 0 && volume < 5) { - newSound = Common::String::format("%s%d", newSound.c_str(), volume); - _driver->startSound(newSound); + if (volume > 0 && volume < 5) + newSound = Common::String::format("%s%d", newSound.c_str(), volume); + + if (!_driver->startSound(newSound)) { + // WORKAROUND for wrongly named resources. This applies to at least 'BLADE' in the EOB II dungeons (instead of 'BLADE1'). + newSound = _resInfo[_currentResourceSet]->soundList[track]; + if (_driver->startSound(newSound)) + debugC(5, kDebugLevelSound, "SoundAmiga_EoB::playSoundEffect(): Triggered workaround for wrongly named resource: '%s'", newSound.c_str()); } _lastSound = newSound; @@ -216,6 +214,7 @@ void SoundAmiga_EoB::beginFadeOut(int delay) { _driver->fadeOut(delay); while (_driver->isFading() && !_vm->shouldQuit()) _vm->delay(5); + haltTrack(); } void SoundAmiga_EoB::updateVolumeSettings() { @@ -234,23 +233,4 @@ int SoundAmiga_EoB::checkTrigger() { return _driver->getPlayDuration(); } -void SoundAmiga_EoB::unloadLevelSounds() { - if (_currentFile != -1) { - _driver->flushResource(Common::String::format("L%dM1A1", _currentFile)); - _driver->flushResource(Common::String::format("L%dM2A1", _currentFile)); - - for (int i = 1; i < 5; ++i) { - _driver->flushResource(Common::String::format("L%dM1M%d", _currentFile, i)); - _driver->flushResource(Common::String::format("L%dM2M%d", _currentFile, i)); - } - - for (int i = 0; i < 2; ++i) { - if (_levelSoundList1[_currentFile * 2 + i][0]) - _driver->flushResource(_levelSoundList1[_currentFile * 2 + i]); - if (_levelSoundList2[_currentFile * 2 + i][0]) - _driver->flushResource(_levelSoundList2[_currentFile * 2 + i]); - } - } -} - } // End of namespace Kyra diff --git a/engines/kyra/sound/sound_intern.h b/engines/kyra/sound/sound_intern.h index fbf19d742e..57afb26ab5 100644 --- a/engines/kyra/sound/sound_intern.h +++ b/engines/kyra/sound/sound_intern.h @@ -417,8 +417,9 @@ public: void initAudioResourceInfo(int set, void *info); void selectAudioResourceSet(int set); bool hasSoundFile(uint file) const { return false; } - void loadSoundFile(uint file); + void loadSoundFile(uint) {} void loadSoundFile(Common::String file); + void unloadSoundFile(Common::String file); void playTrack(uint8 track); void haltTrack(); void playSoundEffect(uint8 track, uint8 volume = 0xFF); @@ -428,8 +429,6 @@ public: int checkTrigger(); private: - void unloadLevelSounds(); - uint8 *_fileBuffer; KyraEngine_v1 *_vm; @@ -438,10 +437,6 @@ private: Common::String _lastSound; int _currentResourceSet; - int _currentFile; - - const char *const *_levelSoundList1; - const char *const *_levelSoundList2; bool _ready; }; |