Skip to content

Commit

Permalink
update volume according to changes live instead of requiring new sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
poco0317 committed Jul 16, 2021
1 parent 817c913 commit fd8e8f3
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 15 deletions.
3 changes: 0 additions & 3 deletions src/Etterna/Singletons/GameSoundManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -937,9 +937,6 @@ class LunaGameSoundManager : public Luna<GameSoundManager>
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)
Expand Down
6 changes: 3 additions & 3 deletions src/RageUtil/Sound/RageSound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/RageUtil/Sound/RageSound.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions src/RageUtil/Sound/RageSoundManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -208,9 +209,7 @@ static Preference<float> 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
Expand Down
2 changes: 0 additions & 2 deletions src/RageUtil/Sound/RageSoundManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class RageSoundManager

void Init();

float GetMixVolume() const { return m_fMixVolume; }
void SetMixVolume();
float GetVolumeOfNonCriticalSounds() const
{
Expand Down Expand Up @@ -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);
Expand Down
25 changes: 22 additions & 3 deletions src/RageUtil/Sound/RageSoundReader_PostBuffering.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Etterna/Globals/global.h"
#include "RageSoundReader_PostBuffering.h"
#include "RageUtil/Misc/RageThreads.h"
#include "RageSoundUtil.h"

/*
Expand All @@ -8,23 +9,41 @@
* changed with low latency.
*/

RageMutex g_Mutex("PostBuffering");
static float g_fMasterVolume = 1.0f;

RageSoundReader_PostBuffering::RageSoundReader_PostBuffering(
RageSoundReader* pSource)
: RageSoundReader_Filter(pSource)
{
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)
{
iFrames = m_pSource->Read(pBuf, 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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/RageUtil/Sound/RageSoundReader_PostBuffering.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit fd8e8f3

Please sign in to comment.