Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate sound buffer pool from sound manager #2971

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/openmw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ add_openmw_dir (mwscript
add_openmw_dir (mwsound
soundmanagerimp openal_output ffmpeg_decoder sound sound_buffer sound_decoder sound_output
loudness movieaudiofactory alext efx efx-presets regionsoundselector watersoundupdater volumesettings
sound_buffer
)

add_openmw_dir (mwworld
Expand Down
7 changes: 7 additions & 0 deletions apps/openmw/mwsound/sound.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

namespace MWSound
{
// Extra play flags, not intended for caller use
enum PlayModeEx
{
Play_2D = 0,
Play_3D = 1 << 31,
};

// For testing individual PlayMode flags
inline int operator&(int a, PlayMode b) { return a & static_cast<int>(b); }
inline int operator&(PlayMode a, PlayMode b) { return static_cast<int>(a) & static_cast<int>(b); }
Expand Down
165 changes: 165 additions & 0 deletions apps/openmw/mwsound/sound_buffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include "sound_buffer.hpp"

#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwworld/esmstore.hpp"

#include <components/debug/debuglog.hpp>
#include <components/settings/settings.hpp>
#include <components/vfs/manager.hpp>

#include <algorithm>
#include <cmath>

namespace MWSound
{
namespace
{
struct AudioParams
{
float mAudioDefaultMinDistance;
float mAudioDefaultMaxDistance;
float mAudioMinDistanceMult;
float mAudioMaxDistanceMult;
};

AudioParams makeAudioParams(const MWBase::World& world)
{
const auto& settings = world.getStore().get<ESM::GameSetting>();
AudioParams params;
params.mAudioDefaultMinDistance = settings.find("fAudioDefaultMinDistance")->mValue.getFloat();
params.mAudioDefaultMaxDistance = settings.find("fAudioDefaultMaxDistance")->mValue.getFloat();
params.mAudioMinDistanceMult = settings.find("fAudioMinDistanceMult")->mValue.getFloat();
params.mAudioMaxDistanceMult = settings.find("fAudioMaxDistanceMult")->mValue.getFloat();
return params;
}
}

SoundBufferPool::SoundBufferPool(const VFS::Manager& vfs, Sound_Output& output) :
mVfs(&vfs),
mOutput(&output),
mBufferCacheMin(std::max(Settings::Manager::getInt("buffer cache min", "Sound"), 1)),
mBufferCacheMax(std::max(Settings::Manager::getInt("buffer cache max", "Sound"), 1))
{
mBufferCacheMax *= 1024 * 1024;
mBufferCacheMin = std::min(mBufferCacheMin * 1024 * 1024, mBufferCacheMax);
}

SoundBufferPool::~SoundBufferPool()
{
assert(mUsedBuffers.empty());
clear();
}

Sound_Buffer* SoundBufferPool::lookup(const std::string& soundId) const
{
const auto it = mBufferNameMap.find(soundId);
if (it != mBufferNameMap.end())
{
Sound_Buffer* sfx = it->second.get();
if (sfx->getHandle() != nullptr)
return sfx;
}
return nullptr;
}

Sound_Buffer* SoundBufferPool::load(const std::string& soundId)
{
if (mBufferNameMap.empty())
{
for (const ESM::Sound& sound : MWBase::Environment::get().getWorld()->getStore().get<ESM::Sound>())
insertSound(Misc::StringUtils::lowerCase(sound.mId), sound);
}

Sound_Buffer* sfx;
const auto it = mBufferNameMap.find(soundId);
if (it != mBufferNameMap.end())
sfx = it->second.get();
else
{
const ESM::Sound *sound = MWBase::Environment::get().getWorld()->getStore().get<ESM::Sound>().search(soundId);
if (sound == nullptr)
return {};
sfx = insertSound(soundId, *sound);
}

if (sfx->getHandle() == nullptr)
{
Sound_Handle handle;
size_t size;
std::tie(handle, size) = mOutput->loadSound(sfx->getResourceName());
if (handle == nullptr)
return {};

sfx->setHandle(handle);

mBufferCacheSize += size;
if (mBufferCacheSize > mBufferCacheMax)
{
unloadUnused();
if (!mUnusedBuffers.empty() && mBufferCacheSize > mBufferCacheMax)
Log(Debug::Warning) << "No unused sound buffers to free, using " << mBufferCacheSize << " bytes!";
}
sfx->setPool(*this, mUnusedBuffers.insert(mUnusedBuffers.begin(), sfx));
}

return sfx;
}

void SoundBufferPool::clear()
{
for (const auto unused : mUnusedBuffers)
{
mBufferCacheSize -= mOutput->unloadSound(unused->getHandle());
unused->setHandle(nullptr);
}
mUnusedBuffers.clear();
}

Sound_Buffer* SoundBufferPool::insertSound(const std::string& soundId, const ESM::Sound& sound)
{
static const AudioParams audioParams = makeAudioParams(*MWBase::Environment::get().getWorld());

float volume = static_cast<float>(std::pow(10.0, (sound.mData.mVolume / 255.0 * 3348.0 - 3348.0) / 2000.0));
float min = sound.mData.mMinRange;
float max = sound.mData.mMaxRange;
if (min == 0 && max == 0)
{
min = audioParams.mAudioDefaultMinDistance;
max = audioParams.mAudioDefaultMaxDistance;
}

min *= audioParams.mAudioMinDistanceMult;
max *= audioParams.mAudioMaxDistanceMult;
min = std::max(min, 1.0f);
max = std::max(min, max);

auto sfx = mSoundBuffers.get();
sfx->init([&] {
SoundBufferParams params;
params.mResourceName = "Sound/" + sound.mSound;
mVfs->normalizeFilename(params.mResourceName);
params.mVolume = volume;
params.mMinDist = min;
params.mMaxDist = max;
return params;
} ());

Sound_Buffer* result = sfx.get();
mBufferNameMap.emplace(soundId, std::move(sfx));
return result;
}

void SoundBufferPool::unloadUnused()
{
while (!mUnusedBuffers.empty() && mBufferCacheSize > mBufferCacheMin)
{
Sound_Buffer* const unused = mUnusedBuffers.back();

mBufferCacheSize -= mOutput->unloadSound(unused->getHandle());
unused->setHandle(nullptr);

mUnusedBuffers.pop_back();
}
}
}
176 changes: 167 additions & 9 deletions apps/openmw/mwsound/sound_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,185 @@
#define GAME_SOUND_SOUND_BUFFER_H

#include <string>
#include <memory>
#include <list>
#include <unordered_map>

#include "sound_output.hpp"

#include <components/misc/objectpool.hpp>

namespace ESM
{
struct Sound;
}

namespace VFS
{
class Manager;
}

namespace MWSound
{
class Sound_Buffer
struct SoundBufferParams
{
public:
std::string mResourceName;
float mVolume = 0;
float mMinDist = 0;
float mMaxDist = 0;
};

float mVolume;
float mMinDist, mMaxDist;
class SoundBufferPool;
class SoundBufferSharedRef;
class Sound_Buffer;

Sound_Handle mHandle;
using SoundBufferIterator = std::list<Sound_Buffer*>::const_iterator;

class Sound_Buffer
{
public:
void init(SoundBufferParams params)
{
mParams = std::move(params);
mHandle = nullptr;
mUses = 0;
}

size_t mUses;
const std::string& getResourceName() const noexcept { return mParams.mResourceName; }

Sound_Buffer(std::string resname, float volume, float mindist, float maxdist)
: mResourceName(resname), mVolume(volume), mMinDist(mindist), mMaxDist(maxdist), mHandle(0), mUses(0)
{ }
Sound_Handle getHandle() const noexcept { return mHandle; }

void setHandle(Sound_Handle value) noexcept { mHandle = value; }

float getVolume() const noexcept { return mParams.mVolume; }

float getMinDist() const noexcept { return mParams.mMinDist; }

float getMaxDist() const noexcept { return mParams.mMaxDist; }

void setPool(SoundBufferPool& pool, SoundBufferIterator value) noexcept
{
mPool = &pool;
mIterator = value;
}

inline SoundBufferSharedRef use() noexcept;

private:
SoundBufferParams mParams;
Sound_Handle mHandle = nullptr;
std::size_t mUses = 0;
SoundBufferPool* mPool = nullptr;
SoundBufferIterator mIterator;

friend class SoundBufferSharedRef;
};

class SoundBufferPool
{
public:
SoundBufferPool(const VFS::Manager& vfs, Sound_Output& output);

~SoundBufferPool();

Sound_Buffer* lookup(const std::string& soundId) const;

Sound_Buffer* load(const std::string& soundId);

void acquire(SoundBufferIterator it) noexcept
{
mUsedBuffers.splice(mUsedBuffers.end(), mUnusedBuffers, it);
}

void release(SoundBufferIterator it) noexcept
{
mUnusedBuffers.splice(mUnusedBuffers.end(), mUsedBuffers, it);
}

void clear();

private:
const VFS::Manager* const mVfs;
Sound_Output* mOutput;
Misc::ObjectPool<Sound_Buffer> mSoundBuffers;
std::unordered_map<std::string, Misc::ObjectPtr<Sound_Buffer>> mBufferNameMap;
std::size_t mBufferCacheMin;
std::size_t mBufferCacheMax;
std::size_t mBufferCacheSize = 0;
std::list<Sound_Buffer*> mUnusedBuffers;
std::list<Sound_Buffer*> mUsedBuffers;

inline Sound_Buffer* insertSound(const std::string& soundId, const ESM::Sound& sound);

inline void unloadUnused();
};

class SoundBufferSharedRef
{
public:
explicit SoundBufferSharedRef(Sound_Buffer& ref) noexcept
: mRef(&ref)
{
if (mRef->mUses++ == 0)
mRef->mPool->acquire(mRef->mIterator);
}

SoundBufferSharedRef(const SoundBufferSharedRef& other) noexcept
: mRef(other.mRef)
{
++mRef->mUses;
}

SoundBufferSharedRef(SoundBufferSharedRef&& other) noexcept
: mRef(other.mRef)
{
other.mRef = nullptr;
}

~SoundBufferSharedRef() noexcept
{
if (mRef != nullptr && --mRef->mUses == 0)
mRef->mPool->release(mRef->mIterator);
}

void reset() noexcept
{
if (mRef != nullptr)
{
mRef->mPool->release(mRef->mIterator);
mRef = nullptr;
}
}

SoundBufferSharedRef& operator=(const SoundBufferSharedRef& other) noexcept
{
SoundBufferSharedRef copy(other);
std::swap(mRef, copy.mRef);
return *this;
}

SoundBufferSharedRef& operator=(SoundBufferSharedRef&& other) noexcept
{
SoundBufferSharedRef moved(std::move(other));
std::swap(mRef, moved.mRef);
return *this;
}

friend inline bool operator==(SoundBufferSharedRef lhs, Sound_Buffer* rhs) noexcept
{
return lhs.mRef == rhs;
}

friend inline bool operator!=(SoundBufferSharedRef lhs, Sound_Buffer* rhs) noexcept
{
return lhs.mRef != rhs;
}

private:
Sound_Buffer* mRef;
};

inline SoundBufferSharedRef Sound_Buffer::use() noexcept { return SoundBufferSharedRef(*this); }
}

#endif /* GAME_SOUND_SOUND_BUFFER_H */
Loading