diff options
-rw-r--r-- | engines/sludge/builtin.cpp | 28 | ||||
-rw-r--r-- | engines/sludge/loadsave.cpp | 4 | ||||
-rw-r--r-- | engines/sludge/main_loop.cpp | 12 | ||||
-rw-r--r-- | engines/sludge/newfatal.cpp | 2 | ||||
-rw-r--r-- | engines/sludge/people.cpp | 4 | ||||
-rw-r--r-- | engines/sludge/sludge.cpp | 4 | ||||
-rw-r--r-- | engines/sludge/sludge.h | 3 | ||||
-rw-r--r-- | engines/sludge/sludger.cpp | 4 | ||||
-rw-r--r-- | engines/sludge/sound.cpp | 324 | ||||
-rw-r--r-- | engines/sludge/sound.h | 109 | ||||
-rw-r--r-- | engines/sludge/talk.cpp | 4 |
11 files changed, 270 insertions, 228 deletions
diff --git a/engines/sludge/builtin.cpp b/engines/sludge/builtin.cpp index d88ecb31db..4ae01ab78f 100644 --- a/engines/sludge/builtin.cpp +++ b/engines/sludge/builtin.cpp @@ -1063,7 +1063,7 @@ builtIn(startMusic) { if (!getValueType(fileNumber, SVT_FILE, fun->stack->thisVar)) return BR_ERROR; trimStack(fun->stack); - if (!playMOD(fileNumber, musChan, fromTrack)) + if (!g_sludge->_soundMan->playMOD(fileNumber, musChan, fromTrack)) return BR_CONTINUE; //BR_ERROR; return BR_CONTINUE; } @@ -1074,7 +1074,7 @@ builtIn(stopMusic) { if (!getValueType(v, SVT_INT, fun->stack->thisVar)) return BR_ERROR; trimStack(fun->stack); - stopMOD(v); + g_sludge->_soundMan->stopMOD(v); return BR_CONTINUE; } @@ -1087,7 +1087,7 @@ builtIn(setMusicVolume) { if (!getValueType(musChan, SVT_INT, fun->stack->thisVar)) return BR_ERROR; trimStack(fun->stack); - setMusicVolume(musChan, v); + g_sludge->_soundMan->setMusicVolume(musChan, v); return BR_CONTINUE; } @@ -1097,7 +1097,7 @@ builtIn(setDefaultMusicVolume) { if (!getValueType(v, SVT_INT, fun->stack->thisVar)) return BR_ERROR; trimStack(fun->stack); - setDefaultMusicVolume(v); + g_sludge->_soundMan->setDefaultMusicVolume(v); return BR_CONTINUE; } @@ -1107,7 +1107,7 @@ builtIn(playSound) { if (!getValueType(fileNumber, SVT_FILE, fun->stack->thisVar)) return BR_ERROR; trimStack(fun->stack); - if (!startSound(fileNumber, false)) + if (!g_sludge->_soundMan->startSound(fileNumber, false)) return BR_CONTINUE; // Was BR_ERROR return BR_CONTINUE; } @@ -1123,7 +1123,7 @@ builtIn(loopSound) { if (!getValueType(fileNumber, SVT_FILE, fun->stack->thisVar)) return BR_ERROR; trimStack(fun->stack); - if (!startSound(fileNumber, true)) + if (!g_sludge->_soundMan->startSound(fileNumber, true)) return BR_CONTINUE; // Was BR_ERROR return BR_CONTINUE; } else { @@ -1165,7 +1165,7 @@ builtIn(loopSound) { s->next = s; } old->vol = -1; - playSoundList(old); + g_sludge->_soundMan->playSoundList(old); return BR_CONTINUE; } } @@ -1176,7 +1176,7 @@ builtIn(stopSound) { if (!getValueType(v, SVT_FILE, fun->stack->thisVar)) return BR_ERROR; trimStack(fun->stack); - huntKillSound(v); + g_sludge->_soundMan->huntKillSound(v); return BR_CONTINUE; } @@ -1186,7 +1186,7 @@ builtIn(setDefaultSoundVolume) { if (!getValueType(v, SVT_INT, fun->stack->thisVar)) return BR_ERROR; trimStack(fun->stack); - setDefaultSoundVolume(v); + g_sludge->_soundMan->setDefaultSoundVolume(v); return BR_CONTINUE; } @@ -1199,7 +1199,7 @@ builtIn(setSoundVolume) { if (!getValueType(musChan, SVT_FILE, fun->stack->thisVar)) return BR_ERROR; trimStack(fun->stack); - setSoundVolume(musChan, v); + g_sludge->_soundMan->setSoundVolume(musChan, v); return BR_CONTINUE; } @@ -1215,7 +1215,7 @@ builtIn(setSoundLoopPoints) { if (!getValueType(musChan, SVT_FILE, fun->stack->thisVar)) return BR_ERROR; trimStack(fun->stack); - setSoundLoop(musChan, theStart, theEnd); + g_sludge->_soundMan->setSoundLoop(musChan, theStart, theEnd); return BR_CONTINUE; } @@ -2153,7 +2153,7 @@ builtIn(cacheSound) { if (!getValueType(fileNumber, SVT_FILE, fun->stack->thisVar)) return BR_ERROR; trimStack(fun->stack); - if (cacheSound(fileNumber) == -1) + if (g_sludge->_soundMan->cacheSound(fileNumber) == -1) return BR_ERROR; return BR_CONTINUE; } @@ -2245,7 +2245,7 @@ builtIn(getSoundCache) { fun->reg.varData.theStack->first = NULL; fun->reg.varData.theStack->last = NULL; fun->reg.varData.theStack->timesUsed = 1; - if (!getSoundCacheStack(fun->reg.varData.theStack)) + if (!g_sludge->_soundMan->getSoundCacheStack(fun->reg.varData.theStack)) return BR_ERROR; return BR_CONTINUE; } @@ -2312,7 +2312,7 @@ builtIn(freeSound) { if (!getValueType(v, SVT_FILE, fun->stack->thisVar)) return BR_ERROR; trimStack(fun->stack); - huntKillFreeSound(v); + g_sludge->_soundMan->huntKillFreeSound(v); return BR_CONTINUE; } diff --git a/engines/sludge/loadsave.cpp b/engines/sludge/loadsave.cpp index 8eb59fc3f8..da3037e94f 100644 --- a/engines/sludge/loadsave.cpp +++ b/engines/sludge/loadsave.cpp @@ -432,7 +432,7 @@ bool saveGame(const Common::String &fname) { fp->writeByte(fadeMode); saveSpeech(speech, fp); saveStatusBars(fp); - saveSounds(fp); + g_sludge->_soundMan->saveSounds(fp); fp->writeUint16BE(saveEncoding); @@ -604,7 +604,7 @@ bool loadGame(const Common::String &fname) { fadeMode = fp->readByte(); loadSpeech(speech, fp); loadStatusBars(fp); - loadSounds(fp); + g_sludge->_soundMan->loadSounds(fp); saveEncoding = fp->readUint16BE(); diff --git a/engines/sludge/main_loop.cpp b/engines/sludge/main_loop.cpp index a4851d76b9..3a42d6d478 100644 --- a/engines/sludge/main_loop.cpp +++ b/engines/sludge/main_loop.cpp @@ -45,12 +45,6 @@ namespace Sludge { -#ifndef MAX_PATH -#define MAX_PATH 1024 // maximum size of a path name -#endif - -HWND hMainWindow = NULL; - extern VariableStack *noStack; int dialogValue = 0; @@ -77,7 +71,7 @@ int main_loop(const char *filename) { resetRandW(); if (!ConfMan.hasKey("mute") || !ConfMan.getBool("mute")) { - initSoundStuff(hMainWindow); + g_sludge->_soundMan->initSoundStuff(); } startNewFunctionNum(0, 0, NULL, noStack); @@ -91,11 +85,11 @@ int main_loop(const char *filename) { runSludge(); } sludgeDisplay(); - handleSoundLists(); + g_sludge->_soundMan->handleSoundLists(); g_sludge->_timer.waitFrame(); } - killSoundStuff(); + g_sludge->_soundMan->killSoundStuff(); return (0); } diff --git a/engines/sludge/newfatal.cpp b/engines/sludge/newfatal.cpp index acbcc1450d..b86ddb2d1f 100644 --- a/engines/sludge/newfatal.cpp +++ b/engines/sludge/newfatal.cpp @@ -58,7 +58,7 @@ void registerWindowForFatal() { } int inFatal(const Common::String &str) { - killSoundStuff(); + g_sludge->_soundMan->killSoundStuff(); error("%s", str.c_str()); return true; } diff --git a/engines/sludge/people.cpp b/engines/sludge/people.cpp index 1021829955..1edec124bb 100644 --- a/engines/sludge/people.cpp +++ b/engines/sludge/people.cpp @@ -457,7 +457,7 @@ void drawPeople() { thisPerson->frameNum = 0; thisPerson->frameTick = myAnim->frames[0].howMany; if (myAnim->frames[thisPerson->frameNum].noise > 0) { - startSound(myAnim->frames[thisPerson->frameNum].noise, false); + g_sludge->_soundMan->startSound(myAnim->frames[thisPerson->frameNum].noise, false); thisPerson->frameNum++; thisPerson->frameNum %= thisPerson->myAnim->numFrames; thisPerson->frameTick = thisPerson->myAnim->frames[thisPerson->frameNum].howMany; @@ -495,7 +495,7 @@ void drawPeople() { thisPerson->frameTick = thisPerson->myAnim->frames[thisPerson->frameNum].howMany; if (thisPerson->show && myAnim && myAnim->frames) { if (myAnim->frames[thisPerson->frameNum].noise > 0) { - startSound(myAnim->frames[thisPerson->frameNum].noise, false); + g_sludge->_soundMan->startSound(myAnim->frames[thisPerson->frameNum].noise, false); thisPerson->frameNum++; thisPerson->frameNum %= thisPerson->myAnim->numFrames; thisPerson->frameTick = thisPerson->myAnim->frames[thisPerson->frameNum].howMany; diff --git a/engines/sludge/sludge.cpp b/engines/sludge/sludge.cpp index fef2ebf77d..4766356b06 100644 --- a/engines/sludge/sludge.cpp +++ b/engines/sludge/sludge.cpp @@ -28,6 +28,7 @@ #include "sludge/event.h" #include "sludge/graphics.h" #include "sludge/sludge.h" +#include "sludge/sound.h" #include "sludge/main_loop.h" namespace Sludge { @@ -69,6 +70,7 @@ SludgeEngine::SludgeEngine(OSystem *syst, const SludgeGameDescription *gameDesc) _objMan = new ObjectManager(this); _gfxMan = new GraphicsManager(this); _evtMan = new EventManager(this); + _soundMan = new SoundManager(); } SludgeEngine::~SludgeEngine() { @@ -91,6 +93,8 @@ SludgeEngine::~SludgeEngine() { _pixelFormat = nullptr; // Dispose managers + delete _soundMan; + _soundMan = nullptr; delete _evtMan; _evtMan = nullptr; delete _gfxMan; diff --git a/engines/sludge/sludge.h b/engines/sludge/sludge.h index 74d5c079f2..6fb4689481 100644 --- a/engines/sludge/sludge.h +++ b/engines/sludge/sludge.h @@ -40,6 +40,8 @@ extern SludgeEngine *g_sludge; class EventManager; class GraphicsManager; +class SoundManager; + class SludgeConsole; struct SludgeGameDescription; @@ -76,6 +78,7 @@ public: ObjectManager *_objMan; GraphicsManager *_gfxMan; EventManager *_evtMan; + SoundManager *_soundMan; SludgeEngine(OSystem *syst, const SludgeGameDescription *gameDesc); virtual ~SludgeEngine(); diff --git a/engines/sludge/sludger.cpp b/engines/sludge/sludger.cpp index b890c78893..ffcecc9865 100644 --- a/engines/sludge/sludger.cpp +++ b/engines/sludge/sludger.cpp @@ -882,8 +882,8 @@ bool runSludge() { if (!thisFunction->freezerLevel) { if (thisFunction->timeLeft) { if (thisFunction->timeLeft < 0) { - if (!stillPlayingSound( - findInSoundCache(speech->lastFile))) { + if (!g_sludge->_soundMan->stillPlayingSound( + g_sludge->_soundMan->findInSoundCache(speech->lastFile))) { thisFunction->timeLeft = 0; } } else if (!--(thisFunction->timeLeft)) { diff --git a/engines/sludge/sound.cpp b/engines/sludge/sound.cpp index 5d6b426bc9..f25b48fb53 100644 --- a/engines/sludge/sound.cpp +++ b/engines/sludge/sound.cpp @@ -26,7 +26,6 @@ #include "common/memstream.h" #include "audio/audiostream.h" -#include "audio/mixer.h" #include "audio/decoders/wave.h" #include "audio/decoders/vorbis.h" #include "audio/mods/protracker.h" @@ -38,67 +37,73 @@ #include "sludge/fileset.h" #include "sludge/sludge.h" -#define MAX_SAMPLES 8 -#define MAX_MODS 3 -#define NUM_BUFS 3 - namespace Sludge { -bool soundOK = false; -bool SilenceIKillYou = false; -bool isHandlingSoundList = false; -// there's possibility that several sound list played at the same time -typedef Common::List<SoundList*> SoundListHandles; -SoundListHandles soundListHandles; - -struct soundThing { - Audio::SoundHandle handle; - int fileLoaded, vol; //Used for sounds only. (sound saving/loading) - bool looping; //Used for sounds only. (sound saving/loading) - bool inSoundList; -}; - -soundThing soundCache[MAX_SAMPLES]; -#if 0 -soundThing modCache[MAX_MODS]; -#endif +const int SoundManager::MAX_SAMPLES = 8; +const int SoundManager::MAX_MODS = 3; -int defVol = 128; -int defSoundVol = 255; -const float modLoudness = 0.95f; +SoundManager::SoundManager() { + // there's possibility that several sound list played at the same time + _soundListHandles.clear(); -/* - * Set up, tear down: - */ + _soundOK = false; + _silenceIKillYou = false; + _isHandlingSoundList = false; + + _soundCache = nullptr; + _soundCache = new SoundThing[MAX_SAMPLES]; + #if 0 + _modCache = nullptr; + _modCache = new SoundThing[MAX_MODS]; + #endif + + _defVol = 128; + _defSoundVol = 255; + _modLoudness = 0.95f; -bool initSoundStuff(HWND hwnd) { + _emptySoundSlot = 0; +} + +SoundManager::~SoundManager() { + delete []_soundCache; + _soundCache = nullptr; + + #if 0 + delete []_modCache; + _modCache = nullptr; + #endif + + killSoundStuff(); +} + +bool SoundManager::initSoundStuff() { for (int a = 0; a < MAX_SAMPLES; a ++) { - soundCache[a].fileLoaded = -1; - soundCache[a].looping = false; - soundCache[a].inSoundList = false; + _soundCache[a].fileLoaded = -1; + _soundCache[a].looping = false; + _soundCache[a].inSoundList = false; } #if 0 for (int a = 0; a < MAX_MODS; a ++) { - modCache[a].stream = NULL; - modCache[a].playing = false; + _modCache[a].stream = NULL; + _modCache[a].playing = false; } #endif - return soundOK = true; + return _soundOK = true; } -void killSoundStuff() { - if (!soundOK) +void SoundManager::killSoundStuff() { + if (!_soundOK) return; - SilenceIKillYou = true; + _silenceIKillYou = true; for (int i = 0; i < MAX_SAMPLES; i ++) { - if (g_sludge->_mixer->isSoundHandleActive(soundCache[i].handle)) { - g_sludge->_mixer->stopHandle(soundCache[i].handle); + if (g_sludge->_mixer->isSoundHandleActive(_soundCache[i].handle)) { + g_sludge->_mixer->stopHandle(_soundCache[i].handle); } } #if 0 for (int i = 0; i < MAX_MODS; i ++) { - if (modCache[i].playing) { + if (_modCache[i].playing) { if (! alureStopSource(modCache[i].playingOnSource, AL_TRUE)) { debugOut("Failed to stop source: %s\n", @@ -107,7 +112,7 @@ void killSoundStuff() { } - if (modCache[i].stream != NULL) { + if (_modCache[i].stream != NULL) { if (! alureDestroyStream(modCache[i].stream, 0, NULL)) { debugOut("Failed to destroy stream: %s\n", @@ -117,63 +122,59 @@ void killSoundStuff() { } } #endif - SilenceIKillYou = false; + _silenceIKillYou = false; } /* * Some setters: */ -void setMusicVolume(int a, int v) { - if (!soundOK) +void SoundManager::setMusicVolume(int a, int v) { + if (!_soundOK) return; #if 0 - if (modCache[a].playing) { - alSourcef(modCache[a].playingOnSource, AL_GAIN, (float) modLoudness * v / 256); + if (_modCache[a].playing) { + alSourcef(modCache[a].playingOnSource, AL_GAIN, (float) _modLoudness * v / 256); } #endif } -void setDefaultMusicVolume(int v) { - defVol = v; +void SoundManager::setDefaultMusicVolume(int v) { + _defVol = v; } -void setSoundVolume(int a, int v) { - if (!soundOK) +void SoundManager::setSoundVolume(int a, int v) { + if (!_soundOK) return; int ch = findInSoundCache(a); if (ch != -1) { - if (g_sludge->_mixer->isSoundHandleActive(soundCache[ch].handle)) { - soundCache[ch].vol = v; - g_sludge->_mixer->setChannelVolume(soundCache[ch].handle, v); + if (g_sludge->_mixer->isSoundHandleActive(_soundCache[ch].handle)) { + _soundCache[ch].vol = v; + g_sludge->_mixer->setChannelVolume(_soundCache[ch].handle, v); } } } -void setDefaultSoundVolume(int v) { - defSoundVol = v; +void SoundManager::setDefaultSoundVolume(int v) { + _defSoundVol = v; } -void setSoundLoop(int a, int s, int e) { +void SoundManager::setSoundLoop(int a, int s, int e) { //#pragma unused (a,s,e) } -/* - * Stopping things: - */ - -int findInSoundCache(int a) { +int SoundManager::findInSoundCache(int a) { int i; for (i = 0; i < MAX_SAMPLES; i++) { - if (soundCache[i].fileLoaded == a) { + if (_soundCache[i].fileLoaded == a) { return i; } } return -1; } -void stopMOD(int i) { - if (!soundOK) +void SoundManager::stopMOD(int i) { + if (!_soundOK) return; #if 0 alGetError(); @@ -185,41 +186,41 @@ void stopMOD(int i) { #endif } -void huntKillSound(int filenum) { - if (!soundOK) +void SoundManager::huntKillSound(int filenum) { + if (!_soundOK) return; int gotSlot = findInSoundCache(filenum); if (gotSlot == -1) return; - SilenceIKillYou = true; + _silenceIKillYou = true; - if (g_sludge->_mixer->isSoundHandleActive(soundCache[gotSlot].handle)) { - g_sludge->_mixer->stopHandle(soundCache[gotSlot].handle); + if (g_sludge->_mixer->isSoundHandleActive(_soundCache[gotSlot].handle)) { + g_sludge->_mixer->stopHandle(_soundCache[gotSlot].handle); } - SilenceIKillYou = false; + _silenceIKillYou = false; } -void freeSound(int a) { - if (!soundOK) +void SoundManager::freeSound(int a) { + if (!_soundOK) return; - SilenceIKillYou = true; + _silenceIKillYou = true; - if (g_sludge->_mixer->isSoundHandleActive(soundCache[a].handle)) { - g_sludge->_mixer->stopHandle(soundCache[a].handle); - if (soundCache[a].inSoundList) + if (g_sludge->_mixer->isSoundHandleActive(_soundCache[a].handle)) { + g_sludge->_mixer->stopHandle(_soundCache[a].handle); + if (_soundCache[a].inSoundList) handleSoundLists(); } - soundCache[a].fileLoaded = -1; + _soundCache[a].fileLoaded = -1; - SilenceIKillYou = false; + _silenceIKillYou = false; } -void huntKillFreeSound(int filenum) { - if (!soundOK) +void SoundManager::huntKillFreeSound(int filenum) { + if (!_soundOK) return; int gotSlot = findInSoundCache(filenum); if (gotSlot == -1) @@ -230,7 +231,7 @@ void huntKillFreeSound(int filenum) { /* * Loading and playing: */ -bool playMOD(int f, int a, int fromTrack) { +bool SoundManager::playMOD(int f, int a, int fromTrack) { #if 0 // load sound setResourceForFatal(f); @@ -253,9 +254,8 @@ bool playMOD(int f, int a, int fromTrack) { Audio::SoundHandle soundHandle; g_sludge->_mixer->playStream(Audio::Mixer::kSFXSoundType, &soundHandle, stream, -1, Audio::Mixer::kMaxChannelVolume); -#endif -#if 0 - if (!soundOK) + + if (!_soundOK) return true; stopMOD(a); @@ -271,11 +271,11 @@ bool playMOD(int f, int a, int fromTrack) { memImage = (byte *) loadEntireFileToMemory(bigDataFile, length); if (! memImage) return fatal(ERROR_MUSIC_MEMORY_LOW); - modCache[a].stream = alureCreateStreamFromMemory(memImage, length, 19200, 0, NULL); + _modCache[a].stream = alureCreateStreamFromMemory(memImage, length, 19200, 0, NULL); delete memImage; - if (modCache[a].stream != NULL) { + if (_modCache[a].stream != NULL) { setMusicVolume(a, defVol); if (! alureSetStreamOrder(modCache[a].stream, fromTrack)) { @@ -291,28 +291,28 @@ bool playMOD(int f, int a, int fromTrack) { alureGetErrorString()); warning(ERROR_MUSIC_ODDNESS); - soundCache[a].stream = NULL; - soundCache[a].playing = false; - soundCache[a].playingOnSource = 0; + _soundCache[a].stream = NULL; + _soundCache[a].playing = false; + _soundCache[a].playingOnSource = 0; } setResourceForFatal(-1); #endif return true; } -bool stillPlayingSound(int ch) { - if (soundOK) +bool SoundManager::stillPlayingSound(int ch) { + if (_soundOK) if (ch != -1) - if (soundCache[ch].fileLoaded != -1) - if (g_sludge->_mixer->isSoundHandleActive(soundCache[ch].handle)) + if (_soundCache[ch].fileLoaded != -1) + if (g_sludge->_mixer->isSoundHandleActive(_soundCache[ch].handle)) return true; return false; } -bool forceRemoveSound() { +bool SoundManager::forceRemoveSound() { for (int a = 0; a < MAX_SAMPLES; a++) { - if (soundCache[a].fileLoaded != -1 && !stillPlayingSound(a)) { + if (_soundCache[a].fileLoaded != -1 && !stillPlayingSound(a)) { // soundWarning ("Deleting silent sound", a); freeSound(a); return 1; @@ -320,7 +320,7 @@ bool forceRemoveSound() { } for (int a = 0; a < MAX_SAMPLES; a++) { - if (soundCache[a].fileLoaded != -1) { + if (_soundCache[a].fileLoaded != -1) { // soundWarning ("Deleting playing sound", a); freeSound(a); return 1; @@ -330,46 +330,44 @@ bool forceRemoveSound() { return 0; } -int emptySoundSlot = 0; - -int findEmptySoundSlot() { +int SoundManager::findEmptySoundSlot() { for (int t = 0; t < MAX_SAMPLES; t++) { - emptySoundSlot++; - emptySoundSlot %= MAX_SAMPLES; - if (!g_sludge->_mixer->isSoundHandleActive(soundCache[emptySoundSlot].handle) && !soundCache[emptySoundSlot].inSoundList) - return emptySoundSlot; + _emptySoundSlot++; + _emptySoundSlot %= MAX_SAMPLES; + if (!g_sludge->_mixer->isSoundHandleActive(_soundCache[_emptySoundSlot].handle) && !_soundCache[_emptySoundSlot].inSoundList) + return _emptySoundSlot; } // Argh! They're all playing! Let's trash the oldest that's not looping... for (int t = 0; t < MAX_SAMPLES; t++) { - emptySoundSlot++; - emptySoundSlot %= MAX_SAMPLES; - if (!soundCache[emptySoundSlot].looping && !soundCache[emptySoundSlot].inSoundList) - return emptySoundSlot; + _emptySoundSlot++; + _emptySoundSlot %= MAX_SAMPLES; + if (!_soundCache[_emptySoundSlot].looping && !_soundCache[_emptySoundSlot].inSoundList) + return _emptySoundSlot; } // Holy crap, they're all looping! What's this twat playing at? - emptySoundSlot++; - emptySoundSlot %= MAX_SAMPLES; - return emptySoundSlot; + _emptySoundSlot++; + _emptySoundSlot %= MAX_SAMPLES; + return _emptySoundSlot; } -int cacheSound(int f) { +int SoundManager::cacheSound(int f) { return 0; // don't load source in advance } -int makeSoundAudioStream(int f, Audio::AudioStream *&audiostream, bool loopy) { - if (!soundOK) +int SoundManager::makeSoundAudioStream(int f, Audio::AudioStream *&audiostream, bool loopy) { + if (!_soundOK) return -1; int a = findInSoundCache(f); if (a != -1) { // if this sound has been loaded before // still playing - if (g_sludge->_mixer->isSoundHandleActive(soundCache[a].handle)) { - g_sludge->_mixer->stopHandle(soundCache[a].handle); // stop it - if (soundCache[a].inSoundList) { + if (g_sludge->_mixer->isSoundHandleActive(_soundCache[a].handle)) { + g_sludge->_mixer->stopHandle(_soundCache[a].handle); // stop it + if (_soundCache[a].inSoundList) { handleSoundLists(); } } @@ -399,21 +397,21 @@ int makeSoundAudioStream(int f, Audio::AudioStream *&audiostream, bool loopy) { if (stream) { audiostream = Audio::makeLoopingAudioStream(stream, loopy ? 0 : 1); - soundCache[a].fileLoaded = f; + _soundCache[a].fileLoaded = f; setResourceForFatal(-1); } else { audiostream = nullptr; warning(ERROR_SOUND_ODDNESS); - soundCache[a].fileLoaded = -1; - soundCache[a].looping = false; + _soundCache[a].fileLoaded = -1; + _soundCache[a].looping = false; return -1; } return a; } -bool startSound(int f, bool loopy) { - if (soundOK) { +bool SoundManager::startSound(int f, bool loopy) { + if (_soundOK) { // Load sound Audio::AudioStream *stream = nullptr; int a = makeSoundAudioStream(f, stream, loopy); @@ -423,49 +421,49 @@ bool startSound(int f, bool loopy) { } // play sound - soundCache[a].looping = loopy; - soundCache[a].vol = defSoundVol; - g_sludge->_mixer->playStream(Audio::Mixer::kSFXSoundType, &soundCache[a].handle, stream, -1, soundCache[a].vol); + _soundCache[a].looping = loopy; + _soundCache[a].vol = _defSoundVol; + g_sludge->_mixer->playStream(Audio::Mixer::kSFXSoundType, &_soundCache[a].handle, stream, -1, _soundCache[a].vol); } return true; } -void saveSounds(Common::WriteStream *stream) { - if (soundOK) { +void SoundManager::saveSounds(Common::WriteStream *stream) { + if (_soundOK) { for (int i = 0; i < MAX_SAMPLES; i++) { - if (soundCache[i].looping) { + if (_soundCache[i].looping) { stream->writeByte(1); - stream->writeUint16BE(soundCache[i].fileLoaded); - stream->writeUint16BE(soundCache[i].vol); + stream->writeUint16BE(_soundCache[i].fileLoaded); + stream->writeUint16BE(_soundCache[i].vol); } } } stream->writeByte(0); - stream->writeUint16BE(defSoundVol); - stream->writeUint16BE(defVol); + stream->writeUint16BE(_defSoundVol); + stream->writeUint16BE(_defVol); } -void loadSounds(Common::SeekableReadStream *stream) { +void SoundManager::loadSounds(Common::SeekableReadStream *stream) { for (int i = 0; i < MAX_SAMPLES; i++) freeSound(i); while (stream->readByte()) { int fileLoaded = stream->readUint16BE(); - defSoundVol = stream->readUint16BE(); + _defSoundVol = stream->readUint16BE(); startSound(fileLoaded, 1); } - defSoundVol = stream->readUint16BE(); - defVol = stream->readUint16BE(); + _defSoundVol = stream->readUint16BE(); + _defVol = stream->readUint16BE(); } -bool getSoundCacheStack(StackHandler *sH) { +bool SoundManager::getSoundCacheStack(StackHandler *sH) { Variable newFileHandle; newFileHandle.varType = SVT_NULL; for (int a = 0; a < MAX_SAMPLES; a++) { - if (soundCache[a].fileLoaded != -1) { - setVariable(newFileHandle, SVT_FILE, soundCache[a].fileLoaded); + if (_soundCache[a].fileLoaded != -1) { + setVariable(newFileHandle, SVT_FILE, _soundCache[a].fileLoaded); if (!addVarToStackQuick(newFileHandle, sH->first)) return false; if (sH->last == NULL) @@ -475,7 +473,7 @@ bool getSoundCacheStack(StackHandler *sH) { return true; } -bool deleteSoundFromList(SoundList*&s) { +bool SoundManager::deleteSoundFromList(SoundList*&s) { // Don't delete a playing sound. if (s->cacheIndex) return false; @@ -500,33 +498,33 @@ bool deleteSoundFromList(SoundList*&s) { return (s != NULL); } -void handleSoundLists() { - if (isHandlingSoundList) +void SoundManager::handleSoundLists() { + if (_isHandlingSoundList) return; - isHandlingSoundList = true; - for (SoundListHandles::iterator it = soundListHandles.begin(); it != soundListHandles.end(); ++it) { + _isHandlingSoundList = true; + for (SoundListHandles::iterator it = _soundListHandles.begin(); it != _soundListHandles.end(); ++it) { SoundList*s = (*it); int a = s->cacheIndex; bool remove = false; - if (!g_sludge->_mixer->isSoundHandleActive(soundCache[a].handle)) { // reach the end of stream + if (!g_sludge->_mixer->isSoundHandleActive(_soundCache[a].handle)) { // reach the end of stream s->cacheIndex = false; - soundCache[a].inSoundList = false; - if (SilenceIKillYou) { + _soundCache[a].inSoundList = false; + if (_silenceIKillYou) { while (deleteSoundFromList(s)) ; remove = (s == NULL); // s not null if still playing } else { if (s->next) { if (s->next == s) { // loop the same sound - int v = defSoundVol; - defSoundVol = soundCache[a].vol; + int v = _defSoundVol; + _defSoundVol = _soundCache[a].vol; startSound(s->sound, true); - defSoundVol = v; + _defSoundVol = v; while (deleteSoundFromList(s)) ; remove = (s == NULL); // s not null if still playing } else { // repush the next sound list - s->next->vol = soundCache[a].vol; + s->next->vol = _soundCache[a].vol; playSoundList(s->next); remove = true; // remove this one } @@ -539,15 +537,15 @@ void handleSoundLists() { } } if (remove) { - it = soundListHandles.reverse_erase(it); + it = _soundListHandles.reverse_erase(it); } } - isHandlingSoundList = false; + _isHandlingSoundList = false; } // loop a list of sound -void playSoundList(SoundList*s) { - if (soundOK) { +void SoundManager::playSoundList(SoundList*s) { + if (_soundOK) { // Load sound Audio::AudioStream *stream; int a = makeSoundAudioStream(s->sound, stream, false); @@ -557,17 +555,17 @@ void playSoundList(SoundList*s) { } // Play sound - soundCache[a].looping = false; + _soundCache[a].looping = false; if (s->vol < 0) - soundCache[a].vol = defSoundVol; + _soundCache[a].vol = _defSoundVol; else - soundCache[a].vol = s->vol; + _soundCache[a].vol = s->vol; s-> cacheIndex = a; - g_sludge->_mixer->playStream(Audio::Mixer::kSFXSoundType, &soundCache[a].handle, stream, -1, soundCache[a].vol); - soundCache[a].inSoundList = true; + g_sludge->_mixer->playStream(Audio::Mixer::kSFXSoundType, &_soundCache[a].handle, stream, -1, _soundCache[a].vol); + _soundCache[a].inSoundList = true; // push sound list - soundListHandles.push_back(s); + _soundListHandles.push_back(s); } } @@ -642,7 +640,7 @@ int initMovieSound(int f, ALenum format, int audioChannels, ALuint samplerate, } #endif -uint getSoundSource(int index) { +uint SoundManager::getSoundSource(int index) { return 0; /*soundCache[index].playingOnSource;*/ //TODO:false value } diff --git a/engines/sludge/sound.h b/engines/sludge/sound.h index 8e9038dcbb..cdd76b33cc 100644 --- a/engines/sludge/sound.h +++ b/engines/sludge/sound.h @@ -23,10 +23,10 @@ #ifndef SLUDGE_SOUND_H #define SLUDGE_SOUND_H -#define HWND void * - #include "common/file.h" +#include "audio/mixer.h" + #include "sludge/variable.h" namespace Sludge { @@ -39,37 +39,80 @@ struct SoundList{ int cacheIndex; int vol; }; -bool deleteSoundFromList(SoundList*&s); -void playSoundList(SoundList*s); -void handleSoundLists(); // to produce the same effects as end of stream call back functions - -// GENERAL... -bool initSoundStuff(HWND); -void killSoundStuff(); - -// MUSIC... -bool playMOD(int, int, int); -void stopMOD(int); -void setMusicVolume(int a, int v); -void setDefaultMusicVolume(int v); - -// SAMPLES... -int cacheSound(int f); -bool startSound(int, bool = false); -void huntKillSound(int a); -void huntKillFreeSound(int filenum); -void setSoundVolume(int a, int v); -void setDefaultSoundVolume(int v); -void setSoundLoop(int a, int s, int e); -bool stillPlayingSound(int ch); -bool getSoundCacheStack(StackHandler *sH); -int findInSoundCache(int a); - -void debugSounds(); -void loadSounds(Common::SeekableReadStream *stream); -void saveSounds(Common::WriteStream *stream); - -uint getSoundSource(int index); + +class SoundManager { +public: + SoundManager(); + virtual ~SoundManager(); + + // Sound list + void playSoundList(SoundList*s); + void handleSoundLists(); // to produce the same effects as end of stream call back functions + + // GENERAL... + bool initSoundStuff(); + void killSoundStuff(); + + // MUSIC... + bool playMOD(int, int, int); + void stopMOD(int); + void setMusicVolume(int a, int v); + void setDefaultMusicVolume(int v); + + // SAMPLES... + int cacheSound(int f); + bool startSound(int, bool = false); + void huntKillSound(int a); + void huntKillFreeSound(int filenum); + void setSoundVolume(int a, int v); + void setDefaultSoundVolume(int v); + void setSoundLoop(int a, int s, int e); + bool stillPlayingSound(int ch); + bool getSoundCacheStack(StackHandler *sH); + int findInSoundCache(int a); + + // Load & save + void loadSounds(Common::SeekableReadStream *stream); + void saveSounds(Common::WriteStream *stream); + + uint getSoundSource(int index); + +private: + const static int MAX_SAMPLES; + const static int MAX_MODS; + + struct SoundThing { + Audio::SoundHandle handle; + int fileLoaded, vol; //Used for sounds only. (sound saving/loading) + bool looping; //Used for sounds only. (sound saving/loading) + bool inSoundList; + }; + typedef Common::List<SoundList *> SoundListHandles; + + // there's possibility that several sound list played at the same time + SoundListHandles _soundListHandles; + + bool _soundOK; + bool _silenceIKillYou; + bool _isHandlingSoundList; + + SoundThing *_soundCache; + #if 0 + SoundThing *_modCache; + #endif + + int _defVol; + int _defSoundVol; + float _modLoudness; + + int _emptySoundSlot; + + void freeSound(int a); + bool forceRemoveSound(); + bool deleteSoundFromList(SoundList*&s); + int findEmptySoundSlot(); + int makeSoundAudioStream(int f, Audio::AudioStream *&audiostream, bool loopy); +}; } // End of namespace Sludge diff --git a/engines/sludge/talk.cpp b/engines/sludge/talk.cpp index 4e8cd3fb61..23ebc5f461 100644 --- a/engines/sludge/talk.cpp +++ b/engines/sludge/talk.cpp @@ -54,7 +54,7 @@ void initSpeech() { void killAllSpeech() { if (speech->lastFile != -1) { - huntKillSound(speech->lastFile); + g_sludge->_soundMan->huntKillSound(speech->lastFile); speech->lastFile = -1; } @@ -114,7 +114,7 @@ int wrapSpeechXY(const Common::String &theText, int x, int y, int wrap, int samp speechTime = 1; if (sampleFile != -1) { if (speechMode >= 1) { - if (startSound(sampleFile, false)) { + if (g_sludge->_soundMan->startSound(sampleFile, false)) { speechTime = -10; speech->lastFile = sampleFile; if (speechMode == 2) return -10; |