1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-10 21:40:15 +00:00

Use a separate lookup for sound files

This commit is contained in:
Andrei Kortunov 2023-08-19 13:46:20 +04:00
parent e5d5cbcdd1
commit 921375a06b
3 changed files with 47 additions and 11 deletions

View File

@ -64,8 +64,14 @@ namespace MWSound
Sound_Buffer* SoundBufferPool::lookup(std::string_view fileName) const Sound_Buffer* SoundBufferPool::lookup(std::string_view fileName) const
{ {
auto soundId = ESM::RefId::stringRefId(fileName); const auto it = mBufferFileNameMap.find(std::string(fileName));
return lookup(soundId); if (it != mBufferFileNameMap.end())
{
Sound_Buffer* sfx = it->second;
if (sfx->getHandle() != nullptr)
return sfx;
}
return nullptr;
} }
Sound_Buffer* SoundBufferPool::loadSfx(Sound_Buffer* sfx) Sound_Buffer* SoundBufferPool::loadSfx(Sound_Buffer* sfx)
@ -116,11 +122,9 @@ namespace MWSound
Sound_Buffer* SoundBufferPool::load(std::string_view fileName) Sound_Buffer* SoundBufferPool::load(std::string_view fileName)
{ {
auto soundId = ESM::RefId::stringRefId(fileName);
Sound_Buffer* sfx; Sound_Buffer* sfx;
const auto it = mBufferNameMap.find(soundId); const auto it = mBufferFileNameMap.find(std::string(fileName));
if (it != mBufferNameMap.end()) if (it != mBufferFileNameMap.end())
sfx = it->second; sfx = it->second;
else else
{ {
@ -138,6 +142,9 @@ namespace MWSound
mOutput->unloadSound(sfx.mHandle); mOutput->unloadSound(sfx.mHandle);
sfx.mHandle = nullptr; sfx.mHandle = nullptr;
} }
mBufferFileNameMap.clear();
mBufferNameMap.clear();
mUnusedBuffers.clear(); mUnusedBuffers.clear();
} }
@ -155,7 +162,7 @@ namespace MWSound
Sound_Buffer& sfx = mSoundBuffers.emplace_back(fileName, volume, min, max); Sound_Buffer& sfx = mSoundBuffers.emplace_back(fileName, volume, min, max);
mBufferNameMap.emplace(ESM::RefId::stringRefId(fileName), &sfx); mBufferFileNameMap.emplace(fileName, &sfx);
return &sfx; return &sfx;
} }

View File

@ -104,6 +104,7 @@ namespace MWSound
Sound_Output* mOutput; Sound_Output* mOutput;
std::deque<Sound_Buffer> mSoundBuffers; std::deque<Sound_Buffer> mSoundBuffers;
std::unordered_map<ESM::RefId, Sound_Buffer*> mBufferNameMap; std::unordered_map<ESM::RefId, Sound_Buffer*> mBufferNameMap;
std::unordered_map<std::string, Sound_Buffer*> mBufferFileNameMap;
std::size_t mBufferCacheMax; std::size_t mBufferCacheMax;
std::size_t mBufferCacheMin; std::size_t mBufferCacheMin;
std::size_t mBufferCacheSize = 0; std::size_t mBufferCacheSize = 0;

View File

@ -534,6 +534,9 @@ namespace MWSound
return nullptr; return nullptr;
std::string normalizedName = VFS::Path::normalizeFilename(fileName); std::string normalizedName = VFS::Path::normalizeFilename(fileName);
if (!mVFS->exists(normalizedName))
return nullptr;
Sound_Buffer* sfx = mSoundBuffers.load(normalizedName); Sound_Buffer* sfx = mSoundBuffers.load(normalizedName);
if (!sfx) if (!sfx)
return nullptr; return nullptr;
@ -630,6 +633,9 @@ namespace MWSound
// Look up the sound // Look up the sound
std::string normalizedName = VFS::Path::normalizeFilename(fileName); std::string normalizedName = VFS::Path::normalizeFilename(fileName);
if (!mVFS->exists(normalizedName))
return nullptr;
Sound_Buffer* sfx = mSoundBuffers.load(normalizedName); Sound_Buffer* sfx = mSoundBuffers.load(normalizedName);
if (!sfx) if (!sfx)
return nullptr; return nullptr;
@ -705,9 +711,15 @@ namespace MWSound
void SoundManager::stopSound3D(const MWWorld::ConstPtr& ptr, std::string_view fileName) void SoundManager::stopSound3D(const MWWorld::ConstPtr& ptr, std::string_view fileName)
{ {
if (!mOutput->isInitialized())
return;
std::string normalizedName = VFS::Path::normalizeFilename(fileName); std::string normalizedName = VFS::Path::normalizeFilename(fileName);
auto soundId = ESM::RefId::stringRefId(normalizedName); Sound_Buffer* sfx = mSoundBuffers.lookup(normalizedName);
stopSound3D(ptr, soundId); if (!sfx)
return;
stopSound(sfx, ptr);
} }
void SoundManager::stopSound3D(const MWWorld::ConstPtr& ptr) void SoundManager::stopSound3D(const MWWorld::ConstPtr& ptr)
@ -769,8 +781,21 @@ namespace MWSound
bool SoundManager::getSoundPlaying(const MWWorld::ConstPtr& ptr, std::string_view fileName) const bool SoundManager::getSoundPlaying(const MWWorld::ConstPtr& ptr, std::string_view fileName) const
{ {
std::string normalizedName = VFS::Path::normalizeFilename(fileName); std::string normalizedName = VFS::Path::normalizeFilename(fileName);
auto soundId = ESM::RefId::stringRefId(normalizedName);
return getSoundPlaying(ptr, soundId); SoundMap::const_iterator snditer = mActiveSounds.find(ptr.mRef);
if (snditer != mActiveSounds.end())
{
Sound_Buffer* sfx = mSoundBuffers.lookup(normalizedName);
if (!sfx)
return false;
return std::find_if(snditer->second.mList.cbegin(), snditer->second.mList.cend(),
[this, sfx](const SoundBufferRefPair& snd) -> bool {
return snd.second == sfx && mOutput->isSoundPlaying(snd.first.get());
})
!= snditer->second.mList.cend();
}
return false;
} }
bool SoundManager::getSoundPlaying(const MWWorld::ConstPtr& ptr, const ESM::RefId& soundId) const bool SoundManager::getSoundPlaying(const MWWorld::ConstPtr& ptr, const ESM::RefId& soundId) const
@ -779,6 +804,9 @@ namespace MWSound
if (snditer != mActiveSounds.end()) if (snditer != mActiveSounds.end())
{ {
Sound_Buffer* sfx = mSoundBuffers.lookup(soundId); Sound_Buffer* sfx = mSoundBuffers.lookup(soundId);
if (!sfx)
return false;
return std::find_if(snditer->second.mList.cbegin(), snditer->second.mList.cend(), return std::find_if(snditer->second.mList.cbegin(), snditer->second.mList.cend(),
[this, sfx](const SoundBufferRefPair& snd) -> bool { [this, sfx](const SoundBufferRefPair& snd) -> bool {
return snd.second == sfx && mOutput->isSoundPlaying(snd.first.get()); return snd.second == sfx && mOutput->isSoundPlaying(snd.first.get());