From fd8e8f3809e6cafa4528dcc32759aff8955a1d7f Mon Sep 17 00:00:00 2001 From: Barinade Date: Thu, 15 Jul 2021 22:41:01 -0500 Subject: [PATCH] update volume according to changes live instead of requiring new sounds courtesy of: https://github.com/stepmania/stepmania/commit/996b69cb01bf562d8ea2908ce368819f7691c6a8 https://github.com/stepmania/stepmania/commit/9074fc09f12cf7cde3b734e6ae2cb43ce3a3e2f1 --- src/Etterna/Singletons/GameSoundManager.cpp | 3 --- src/RageUtil/Sound/RageSound.cpp | 6 ++--- src/RageUtil/Sound/RageSound.h | 2 +- src/RageUtil/Sound/RageSoundManager.cpp | 5 ++-- src/RageUtil/Sound/RageSoundManager.h | 2 -- .../Sound/RageSoundReader_PostBuffering.cpp | 25 ++++++++++++++++--- .../Sound/RageSoundReader_PostBuffering.h | 2 ++ 7 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/Etterna/Singletons/GameSoundManager.cpp b/src/Etterna/Singletons/GameSoundManager.cpp index 4948230027..03a1f2ddf3 100644 --- a/src/Etterna/Singletons/GameSoundManager.cpp +++ b/src/Etterna/Singletons/GameSoundManager.cpp @@ -937,9 +937,6 @@ class LunaGameSoundManager : public Luna CLAMP(fVol, 0.0f, 1.0f); pRet->Set(fVol); SOUNDMAN->SetMixVolume(); - p->DimMusic( - FArg(1), - 0.01f); // lazy hack to update volume without changing songs - mina return 0; } static int PlayOnce(T* p, lua_State* L) diff --git a/src/RageUtil/Sound/RageSound.cpp b/src/RageUtil/Sound/RageSound.cpp index 16f10ea82b..d23c909b66 100644 --- a/src/RageUtil/Sound/RageSound.cpp +++ b/src/RageUtil/Sound/RageSound.cpp @@ -370,8 +370,8 @@ RageSound::ExecutePlayBackCallback(Lua* L) auto r = fftBuffer[i].real; auto im = fftBuffer[i].imag; lua_pushnumber(L, - (r * r + im * im) / (0.01f + SOUNDMAN->GetMixVolume()) / - (0.01f + SOUNDMAN->GetMixVolume()) / 15.f); + (r * r + im * im) / (0.01f + RageSoundReader_PostBuffering::GetMasterVolume()) / + (0.01f + RageSoundReader_PostBuffering::GetMasterVolume()) / 15.f); lua_rawseti(L, -2, i + 1); } PushSelf(L); @@ -697,7 +697,7 @@ RageSound::ApplyParams() m_pSource->SetProperty("FadeSeconds", m_Param.m_fFadeOutSeconds); m_pSource->SetProperty("AccurateSync", m_Param.m_bAccurateSync); - auto fVolume = m_Param.m_Volume * SOUNDMAN->GetMixVolume(); + auto fVolume = m_Param.m_Volume; if (!m_Param.m_bIsCriticalSound) fVolume *= m_Param.m_fAttractVolume; m_pSource->SetProperty("Volume", fVolume); diff --git a/src/RageUtil/Sound/RageSound.h b/src/RageUtil/Sound/RageSound.h index 36ec5d514c..8268bfb839 100644 --- a/src/RageUtil/Sound/RageSound.h +++ b/src/RageUtil/Sound/RageSound.h @@ -51,7 +51,7 @@ struct RageSoundParams // Number of seconds to spend fading out. float m_fFadeOutSeconds{ 0 }; - float m_Volume{ 1.0F }; // multiplies with SOUNDMAN->GetMixVolume() + float m_Volume{ 1.0F }; float m_fAttractVolume{ 1.0F }; // multiplies with m_Volume /* Number of samples input and output when changing speed. diff --git a/src/RageUtil/Sound/RageSoundManager.cpp b/src/RageUtil/Sound/RageSoundManager.cpp index 6974c9ef02..73643e662a 100644 --- a/src/RageUtil/Sound/RageSoundManager.cpp +++ b/src/RageUtil/Sound/RageSoundManager.cpp @@ -16,6 +16,7 @@ #include "RageSound.h" #include "RageSoundManager.h" #include "RageSoundReader_Preload.h" +#include "RageSoundReader_PostBuffering.h" #include "RageUtil/Misc/RageTimer.h" #include "RageUtil/Utils/RageUtil.h" #include "arch/Sound/RageSoundDriver.h" @@ -208,9 +209,7 @@ static Preference g_fSoundVolume("SoundVolume", 1.0f); void RageSoundManager::SetMixVolume() { - g_SoundManMutex.Lock(); /* lock for access to m_fMixVolume */ - m_fMixVolume = std::clamp(g_fSoundVolume.Get(), 0.0f, 1.0f); - g_SoundManMutex.Unlock(); /* finished with m_fMixVolume */ + RageSoundReader_PostBuffering::SetMasterVolume(g_fSoundVolume.Get()); } void diff --git a/src/RageUtil/Sound/RageSoundManager.h b/src/RageUtil/Sound/RageSoundManager.h index 1276cd51f8..d2359b97b8 100644 --- a/src/RageUtil/Sound/RageSoundManager.h +++ b/src/RageUtil/Sound/RageSoundManager.h @@ -29,7 +29,6 @@ class RageSoundManager void Init(); - float GetMixVolume() const { return m_fMixVolume; } void SetMixVolume(); float GetVolumeOfNonCriticalSounds() const { @@ -57,7 +56,6 @@ class RageSoundManager RageSoundDriver* m_pDriver; /* Prefs: */ - float m_fMixVolume{ 1.0f }; float m_fVolumeOfNonCriticalSounds{ 1.0f }; // Swallow up warnings. If they must be used, define them. RageSoundManager& operator=(const RageSoundManager& rhs); diff --git a/src/RageUtil/Sound/RageSoundReader_PostBuffering.cpp b/src/RageUtil/Sound/RageSoundReader_PostBuffering.cpp index b0a1c506f3..0d34de2637 100644 --- a/src/RageUtil/Sound/RageSoundReader_PostBuffering.cpp +++ b/src/RageUtil/Sound/RageSoundReader_PostBuffering.cpp @@ -1,5 +1,6 @@ #include "Etterna/Globals/global.h" #include "RageSoundReader_PostBuffering.h" +#include "RageUtil/Misc/RageThreads.h" #include "RageSoundUtil.h" /* @@ -8,6 +9,9 @@ * changed with low latency. */ +RageMutex g_Mutex("PostBuffering"); +static float g_fMasterVolume = 1.0f; + RageSoundReader_PostBuffering::RageSoundReader_PostBuffering( RageSoundReader* pSource) : RageSoundReader_Filter(pSource) @@ -15,6 +19,17 @@ RageSoundReader_PostBuffering::RageSoundReader_PostBuffering( m_fVolume = 1.0f; } +void +RageSoundReader_PostBuffering::SetMasterVolume(float fVolume) { + LockMut(g_Mutex); + g_fMasterVolume = fVolume; +} + +float +RageSoundReader_PostBuffering::GetMasterVolume() { + return g_fMasterVolume; +} + int RageSoundReader_PostBuffering::Read(float* pBuf, int iFrames) { @@ -22,9 +37,13 @@ RageSoundReader_PostBuffering::Read(float* pBuf, int iFrames) if (iFrames < 0) return iFrames; - if (m_fVolume != 1.0f) - RageSoundUtil::Attenuate( - pBuf, iFrames * this->GetNumChannels(), m_fVolume); + g_Mutex.Lock(); + float fVolume = m_fVolume * g_fMasterVolume * g_fMasterVolume; + CLAMP(fVolume, 0, 1); + g_Mutex.Unlock(); + + if(fVolume != 1.0F) + RageSoundUtil::Attenuate(pBuf, iFrames * this->GetNumChannels(), fVolume); return iFrames; } diff --git a/src/RageUtil/Sound/RageSoundReader_PostBuffering.h b/src/RageUtil/Sound/RageSoundReader_PostBuffering.h index f9febd8e8f..f9971720a3 100644 --- a/src/RageUtil/Sound/RageSoundReader_PostBuffering.h +++ b/src/RageUtil/Sound/RageSoundReader_PostBuffering.h @@ -15,6 +15,8 @@ class RageSoundReader_PostBuffering : public RageSoundReader_Filter } int Read(float* pBuf, int iFrames) override; bool SetProperty(const std::string& sProperty, float fValue) override; + static void SetMasterVolume(float fVolume); + static float GetMasterVolume(); private: float m_fVolume;