diff --git a/src/sound/SESound.cpp b/src/sound/SESound.cpp index 0b62cec..faabde2 100644 --- a/src/sound/SESound.cpp +++ b/src/sound/SESound.cpp @@ -9,6 +9,7 @@ #define LOG_WRITE(result, ...) \ SESound::Log_Write(__LINE__, __FILE__, result, __VA_ARGS__); +STORM_LIST(SoundCacheNode) SESound::s_CacheList; SCritSect SESound::s_CritSect3; int32_t SESound::s_Initialized; SCritSect SESound::s_InternalCritSect; @@ -86,7 +87,7 @@ FMOD_RESULT DoneLoadingCallback(FMOD_SOUND* fmodSound, FMOD_RESULT callbackResul // Mark cache sound node as loaded if (lookup->m_internal->m_useCache) { - static_cast(lookup->m_internal)->m_cacheNode->loaded = 1; + static_cast(lookup->m_internal)->m_cacheNode->m_loaded = 1; } SESound::s_LoadingCritSect.Leave(); @@ -390,13 +391,44 @@ int32_t SESound::LoadDiskSound(FMOD::System* fmodSystem, const char* filename, F } } + // Generate name + char fmodName[300]; SStrPrintf(fmodName, sizeof(fmodName), "%-24d%s", internal->m_uniqueID, filename); - // TODO + // Generate cache key + + char cacheKey[400]; + auto loopStr = fmodMode & FMOD_LOOP_NORMAL ? "_LOOP_" : "_NOLOOP_"; + auto positionStr = fmodMode & FMOD_2D ? "_2D_" : "_3D_"; + SStrPrintf(cacheKey, sizeof(cacheKey), "%s%s%s", filename, positionStr, loopStr); + + auto cacheHashval = SStrHash(cacheKey); + + // Load from cache if (useCache) { - // TODO + for (auto cacheNode = SESound::s_CacheList.Head(); cacheNode; cacheNode = SESound::s_CacheList.Link(cacheNode)->Next()) { + if (cacheNode->m_hashval == cacheHashval) { + // Cache hit + + internal->m_fmodSound = cacheNode->m_fmodSound; + internal->m_useCache = 1; + internal->m_cacheNode = cacheNode; + + // TODO + + if (cacheNode->m_loaded) { + internal->m_nonblockingReady = 1; + } + + // TODO + + SESound::s_LoadingCritSect.Leave(); + + return 1; + } + } } // Validate file exists @@ -415,8 +447,6 @@ int32_t SESound::LoadDiskSound(FMOD::System* fmodSystem, const char* filename, F return 0; } - int32_t nonblockingReady = 0; - fmodMode |= FMOD_VIRTUAL_PLAYFROMSTART | FMOD_IGNORETAGS | FMOD_NONBLOCKING; uint32_t maxCacheSize = 1048576; @@ -462,14 +492,25 @@ int32_t SESound::LoadDiskSound(FMOD::System* fmodSystem, const char* filename, F return 0; } + // Create cache node + if (useCache) { + auto cacheNode = STORM_NEW(SoundCacheNode); + + cacheNode->m_fmodSound = internal->m_fmodSound; + cacheNode->m_hashval = cacheHashval; + SStrCopy(cacheNode->m_filename, filename, sizeof(cacheNode->m_filename)); + // TODO + + internal->m_useCache = 1; + internal->m_cacheNode = cacheNode; } // TODO - // Used to permit instant playback of cached sounds - internal->m_nonblockingReady = nonblockingReady; + // No cache hit (yet) + internal->m_nonblockingReady = 0; s_LoadingCritSect.Leave(); diff --git a/src/sound/SESound.hpp b/src/sound/SESound.hpp index b0d742f..b2b5f57 100644 --- a/src/sound/SESound.hpp +++ b/src/sound/SESound.hpp @@ -14,6 +14,7 @@ struct SOUND_INTERNAL_LOOKUP : TSHashObject class SESound { public: // Public static variables + static STORM_LIST(SoundCacheNode) s_CacheList; static SCritSect s_CritSect3; static int32_t s_Initialized; static SCritSect s_InternalCritSect; diff --git a/src/sound/SESoundInternal.cpp b/src/sound/SESoundInternal.cpp index 81fd3c7..ae14fc0 100644 --- a/src/sound/SESoundInternal.cpp +++ b/src/sound/SESoundInternal.cpp @@ -4,6 +4,10 @@ #define LOG_WRITE(result, ...) \ SESound::Log_Write(__LINE__, __FILE__, result, __VA_ARGS__); +SoundCacheNode::SoundCacheNode() { + SESound::s_CacheList.LinkToTail(this); +} + SESoundInternal::SESoundInternal() { // TODO this->m_uniqueID = SESound::s_UniqueID++; diff --git a/src/sound/SESoundInternal.hpp b/src/sound/SESoundInternal.hpp index e3304c1..255aa5d 100644 --- a/src/sound/SESoundInternal.hpp +++ b/src/sound/SESoundInternal.hpp @@ -8,14 +8,18 @@ class SESound; class SFile; -struct SoundCacheNode : TSLinkedNode { - // Member variables - FMOD::Sound* sound; - int32_t loaded; - char filename[128]; - uint32_t hashval; - // TODO dword94 - // TODO dword98 +class SoundCacheNode : public TSLinkedNode { + public: + // Member variables + FMOD::Sound* m_fmodSound = nullptr; + int32_t m_loaded = 0; + char m_filename[128]; + uint32_t m_hashval = 0; + // TODO dword94 + // TODO dword98 + + // Member functions + SoundCacheNode(); }; class SESoundInternal : public TSLinkedNode {