diff options
-rw-r--r-- | sword2/controls.cpp | 2 | ||||
-rw-r--r-- | sword2/driver/d_sound.cpp | 81 | ||||
-rw-r--r-- | sword2/function.cpp | 2 | ||||
-rw-r--r-- | sword2/resman.cpp | 4 | ||||
-rw-r--r-- | sword2/sound.cpp | 10 | ||||
-rw-r--r-- | sword2/sound.h | 14 |
6 files changed, 70 insertions, 43 deletions
diff --git a/sword2/controls.cpp b/sword2/controls.cpp index 5d2936b006..08444ed4d9 100644 --- a/sword2/controls.cpp +++ b/sword2/controls.cpp @@ -1586,7 +1586,7 @@ void Gui::restartControl(void) { // Restart the game. To do this, we must... // Stop music instantly! - _vm->_sound->stopMusic(); + _vm->_sound->stopMusic(true); // In case we were dead - well we're not anymore! Logic::_scriptVars[DEAD] = 0; diff --git a/sword2/driver/d_sound.cpp b/sword2/driver/d_sound.cpp index 1b6177807d..7279db7538 100644 --- a/sword2/driver/d_sound.cpp +++ b/sword2/driver/d_sound.cpp @@ -41,8 +41,6 @@ namespace Sword2 { static AudioStream *makeCLUStream(File *fp, int size); -static File fpMus; - static AudioStream *getAudioStream(File *fp, const char *base, int cd, uint32 id, uint32 *numSamples) { struct { const char *ext; @@ -228,10 +226,17 @@ AudioStream *makeCLUStream(File *file, int size) { // The length of a fade-in/out, in milliseconds. #define FADE_LENGTH 3000 -MusicInputStream::MusicInputStream(int cd, uint32 musicId, bool looping) - : _cd(cd), _musicId(musicId), _bufferEnd(_buffer + BUFFER_SIZE), - _remove(false), _looping(looping), _fading(0) { - _decoder = getAudioStream(&fpMus, "music", _cd, _musicId, &_numSamples); +MusicInputStream::MusicInputStream(int cd, File *fp, uint32 musicId, bool looping) { + _cd = cd; + _file = fp; + _musicId = musicId; + _looping = looping; + + _bufferEnd = _buffer + BUFFER_SIZE; + _remove = false; + _fading = 0; + + _decoder = getAudioStream(fp, "music", _cd, _musicId, &_numSamples); if (_decoder) { _samplesLeft = _numSamples; _fadeSamples = (getRate() * FADE_LENGTH) / 1000; @@ -349,7 +354,7 @@ void MusicInputStream::refill() { if (!_samplesLeft) { if (_looping) { delete _decoder; - _decoder = getAudioStream(&fpMus, "music", _cd, _musicId, &_numSamples); + _decoder = getAudioStream(_file, "music", _cd, _musicId, &_numSamples); _samplesLeft = _numSamples; } else _remove = true; @@ -429,15 +434,36 @@ int Sound::readBuffer(int16 *buffer, const int numSamples) { } } - if (!_music[0] && !_music[1] && fpMus.isOpen()) - fpMus.close(); + bool inUse[MAXMUS]; + + for (i = 0; i < MAXMUS; i++) + inUse[i] = false; + + for (i = 0; i < MAXMUS; i++) { + if (_music[i]) { + if (_music[i]->whichCd() == 1) + inUse[0] = true; + else + inUse[1] = true; + } + } + + for (i = 0; i < MAXMUS; i++) { + if (!inUse[i] && _musicFile[i].isOpen()) + _musicFile[i].close(); + } return numSamples; } -bool Sound::isStereo() const { return false; } -bool Sound::endOfData() const { return !fpMus.isOpen(); } -int Sound::getRate() const { return 22050; } +bool Sound::endOfData() const { + for (int i = 0; i < MAXMUS; i++) { + if (_musicFile[i].isOpen()) + return false; + } + + return true; +} // ---------------------------------------------------------------------------- // MUSIC @@ -468,14 +494,20 @@ void Sound::unpauseMusic() { * Fades out and stops the music. */ -void Sound::stopMusic() { +void Sound::stopMusic(bool immediately) { Common::StackLock lock(_mutex); _loopingMusicId = 0; - for (int i = 0; i < MAXMUS; i++) - if (_music[i]) - _music[i]->fadeDown(); + for (int i = 0; i < MAXMUS; i++) { + if (_music[i]) { + if (immediately) { + delete _music[i]; + _music[i] = NULL; + } else + _music[i]->fadeDown(); + } + } } /** @@ -489,19 +521,6 @@ int32 Sound::streamCompMusic(uint32 musicId, bool loop) { int cd = _vm->_resman->whichCd(); - // HACK: We only have one music file handle, so if any music from the - // "wrong" CD is playing, kill it immediately. - - for (int i = 0; i < MAXMUS; i++) { - if (_music[i] && _music[i]->whichCd() != cd) { - delete _music[i]; - _music[i] = NULL; - - if (fpMus.isOpen()) - fpMus.close(); - } - } - if (loop) _loopingMusicId = musicId; else @@ -560,7 +579,9 @@ int32 Sound::streamCompMusic(uint32 musicId, bool loop) { if (secondary != -1) _music[secondary]->fadeDown(); - _music[primary] = new MusicInputStream(cd, musicId, loop); + File *fp = (cd == 1) ? &_musicFile[0] : &_musicFile[1]; + + _music[primary] = new MusicInputStream(cd, fp, musicId, loop); if (!_music[primary]->isReady()) { delete _music[primary]; diff --git a/sword2/function.cpp b/sword2/function.cpp index 9aa800bb8b..748de8bec4 100644 --- a/sword2/function.cpp +++ b/sword2/function.cpp @@ -2095,7 +2095,7 @@ int32 Logic::fnPlayMusic(int32 *params) { int32 Logic::fnStopMusic(int32 *params) { // params: none - _vm->_sound->stopMusic(); + _vm->_sound->stopMusic(false); return IR_CONT; } diff --git a/sword2/resman.cpp b/sword2/resman.cpp index ad1292ea38..099e218721 100644 --- a/sword2/resman.cpp +++ b/sword2/resman.cpp @@ -863,11 +863,11 @@ void ResourceManager::killAllObjects(bool wantInfo) { void ResourceManager::getCd(int cd) { byte *textRes; - // stop any music from playing - so the system no longer needs the + // Stop any music from playing - so the system no longer needs the // current CD - otherwise when we take out the CD, Windows will // complain! - _vm->_logic->fnStopMusic(NULL); + _vm->_sound->stopMusic(true); textRes = openResource(2283); _vm->displayMsg(_vm->fetchTextLine(textRes, 5 + cd) + 2, 0); diff --git a/sword2/sound.cpp b/sword2/sound.cpp index a9e21373c5..90e5f8a395 100644 --- a/sword2/sound.cpp +++ b/sword2/sound.cpp @@ -71,13 +71,15 @@ Sound::~Sound() { _vm->_mixer->setupPremix(0); clearFxQueue(); - stopMusic(); + stopMusic(true); stopSpeech(); - for (int i = 0; i < MAXMUS; i++) - delete _music[i]; - free(_mixBuffer); + + for (int i = 0; i < MAXMUS; i++) { + if (_musicFile[i].isOpen()) + _musicFile[i].close(); + } } /** diff --git a/sword2/sound.h b/sword2/sound.h index f38c106d8d..0d5c2371b5 100644 --- a/sword2/sound.h +++ b/sword2/sound.h @@ -31,6 +31,7 @@ #ifndef SOUND_H #define SOUND_H +#include "common/file.h" #include "sound/audiostream.h" #include "sound/mixer.h" @@ -95,6 +96,7 @@ public: class MusicInputStream : public AudioStream { private: int _cd; + File *_file; uint32 _musicId; AudioStream *_decoder; int16 _buffer[BUFFER_SIZE]; @@ -117,7 +119,7 @@ private: } public: - MusicInputStream(int cd, uint32 musicId, bool looping); + MusicInputStream(int cd, File *fp, uint32 musicId, bool looping); ~MusicInputStream(); int readBuffer(int16 *buffer, const int numSamples); @@ -172,8 +174,10 @@ private: int32 _loopingMusicId; PlayingSoundHandle _soundHandleSpeech; - + MusicInputStream *_music[MAXMUS]; + File _musicFile[MAXMUS]; + int16 *_mixBuffer; int _mixBufferLen; @@ -184,9 +188,9 @@ public: // AudioStream API int readBuffer(int16 *buffer, const int numSamples); - bool isStereo() const; + bool isStereo() const { return false; } bool endOfData() const; - int getRate() const; + int getRate() const { return 22050; } // End of AudioStream API @@ -233,7 +237,7 @@ public: int32 stopSpeech(); int32 streamCompMusic(uint32 musicId, bool loop); - void stopMusic(); + void stopMusic(bool immediately); int32 musicTimeRemaining(); }; |