From 1a7fc846cf2b62c9a91615b207ba2c8e6e60111a Mon Sep 17 00:00:00 2001 From: Nipun Garg Date: Thu, 25 Jul 2019 02:21:35 +0530 Subject: HDB: Add Sound Cache --- engines/hdb/sound.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++----- engines/hdb/sound.h | 29 +++++++++++++++++++- 2 files changed, 95 insertions(+), 7 deletions(-) (limited to 'engines/hdb') diff --git a/engines/hdb/sound.cpp b/engines/hdb/sound.cpp index 878f0074d7..b10c5d573a 100644 --- a/engines/hdb/sound.cpp +++ b/engines/hdb/sound.cpp @@ -1392,6 +1392,10 @@ const SoundLookUp soundList[] = { {LAST_SOUND, NULL, NULL} }; +Sound::Sound() { + _sfxVolume = 255; +} + void Sound::test() { #ifdef USE_MAD Common::SeekableReadStream *soundStream = g_hdb->_fileMan->findFirstData("M00_AIRLOCK_01_MP3", TYPE_BINARY); @@ -1402,7 +1406,27 @@ void Sound::test() { } bool Sound::init() { - warning("STUB: Sound::init()"); + warning("STUB: Initialize songs"); + + // + // init sound caching system + // + int index = 0; + while (soundList[index].idx != LAST_SOUND) { + int index2 = soundList[index].idx; + _soundCache[index2].loaded = false; + _soundCache[index2].name = soundList[index].name; + _soundCache[index2].luaName = soundList[index].luaName; + debug(9, "sName: %s, sLuaName: %s", soundList[index].name, soundList[index].luaName); + index++; + if (index > kMaxSounds) + error("Reached MAX_SOUNDS in Sound::Init() !"); + } + _numSounds = index; + + // voices are on by default + warning("STUB: Initialize voices"); + return true; } @@ -1463,13 +1487,46 @@ void Sound::stopMusic() { } int Sound::registerSound(const char *name) { - debug(9, "STUB: Register Sound"); - return 0; + int index = 0; + + while (_soundCache[index].name) { + index++; + if (index == kMaxSounds) + return -1; + } + + _soundCache[index].name = name; + _soundCache[index].loaded = 0; // just to be sure! + + return index; } bool Sound::freeSound(int index) { - debug(9, "STUB: Free Sound"); - return true; + if (_soundCache[index].loaded == 1) { + warning("STUB: Free the audio stream in cache"); + _soundCache[index].loaded = 0; + return true; + } + return false; +} + +const char *Sound::getSNDLuaName(int index) { + if (index >= kMaxSounds || !_soundCache[index].luaName) + return nullptr; + + return _soundCache[index].luaName; +} + +int Sound::getSNDIndex(const char *name) { + int i = 0; + + while (soundList[i].idx != LAST_SOUND) { + if (!scumm_stricmp(soundList[i].luaName, name)) + return i; + i++; + } + + return 0; } SoundType Sound::whatSongIsPlaying() { @@ -1478,7 +1535,11 @@ SoundType Sound::whatSongIsPlaying() { } void Sound::markSoundCacheFreeable() { - warning("STUB: Sound::markSoundCacheFreeable() "); + int i; + for (i = 0; i < kMaxSounds; i++) { + if (_soundCache[i].loaded == 1) + _soundCache[i].loaded = -1; + } } } // End of Namespace diff --git a/engines/hdb/sound.h b/engines/hdb/sound.h index a5a23fdd69..0dbb1cfc30 100644 --- a/engines/hdb/sound.h +++ b/engines/hdb/sound.h @@ -51,7 +51,7 @@ namespace HDB { enum { kMaxSNDChannels = 32, - kSNDFrequency = 22050, + kMaxSounds = 5000, kLaserChannel = kMaxSNDChannels - 1, kMusicChannel = kMaxSNDChannels - 2, kVoiceChannel = kMaxSNDChannels - 5 @@ -1408,9 +1408,24 @@ struct SoundLookUp { const char *luaName; // name for Lua code to use }; +struct SoundCache { + int loaded; // -1 = freeable; in memory, 0 = not cached, 1 = cached + int32 size; // size of sound + const char *name; // filename / MSD name + const char *luaName; // name used by Lua for i.d. + //void *data; // actual file data + //FSOUND_SAMPLE *sample; // used to play sound in FMOD + + SoundCache() : loaded(0), size(0), name(nullptr), luaName(nullptr) { + + } +}; + class Sound { public: + Sound(); + void test(); // FIXME. Remove bool init(); @@ -1450,10 +1465,22 @@ public: bool stopChannel(int channel); int registerSound(const char *name); bool freeSound(int index); + const char *getSNDLuaName(int index); + int getSNDIndex(const char *name); + int getNumSounds() { + return _numSounds; + } SoundType whatSongIsPlaying(); void markSoundCacheFreeable(); + + // Sound Caching System Variables + + SoundCache _soundCache[kMaxSounds]; + int _numSounds; + int _sfxVolume; + }; } // End of Namespace -- cgit v1.2.3