Audio Engine volume bug & small update

Fixed a bug where the volume change would be ignored if the sound was fading
Added SoundEngine::GetMusicVolume
Removed volume factor
Removed unused SetListenerVelocity
This commit is contained in:
Amun
2025-10-05 22:08:37 +03:00
parent de6817c338
commit 01f4417d80
5 changed files with 19 additions and 21 deletions

View File

@@ -143,13 +143,14 @@ void MaSoundInstance::Fade(float toVolume, float secDurationFromMinMax)
toVolume = std::clamp<float>(toVolume, 0.0f, 1.0f);
m_FadeTargetVolume = toVolume;
float rate = 1.0f / 61.0f / secDurationFromMinMax;
float rate = 1.0f / CS_CLIENT_FPS / secDurationFromMinMax;
m_FadeRatePerFrame = GetVolume() > toVolume ? -rate : rate;
}
void MaSoundInstance::StopFading()
{
m_FadeRatePerFrame = 0.0f;
m_FadeTargetVolume = 0.0f;
}
bool MaSoundInstance::IsFading() const
@@ -157,17 +158,17 @@ bool MaSoundInstance::IsFading() const
return m_FadeRatePerFrame != 0.0f;
}
void MaSoundInstance::Update(float volumeFactor) // volume factor is the user's volume
void MaSoundInstance::Update()
{
if (m_FadeRatePerFrame != 0.0f)
{
float targetVolume = std::clamp<float>(m_FadeTargetVolume * volumeFactor, 0.0f, 1.0f);
float volume = std::clamp<float>(GetVolume() + (m_FadeRatePerFrame * volumeFactor), 0.0f, 1.0f);
float targetVolume = std::clamp<float>(m_FadeTargetVolume, 0.0f, 1.0f);
float volume = std::clamp<float>(GetVolume() + m_FadeRatePerFrame, 0.0f, 1.0f);
if ((m_FadeRatePerFrame > 0.0f && volume >= targetVolume) || (m_FadeRatePerFrame < 0.0f && volume <= targetVolume))
{
volume = m_FadeTargetVolume * volumeFactor;
volume = m_FadeTargetVolume;
m_FadeRatePerFrame = 0.0f;
if (volume <= 0.0f)
if (m_FadeTargetVolume <= 0.0f)
ma_sound_stop(&m_Sound);
}
ma_sound_set_volume(&m_Sound, volume);

View File

@@ -42,7 +42,7 @@ public:
bool IsFading() const;
void Update(float volumeFactor = 1.0f);
void Update();
private:
std::string m_Identity;

View File

@@ -154,11 +154,13 @@ void SoundEngine::FadeOutAllMusic()
void SoundEngine::SetMusicVolume(float volume)
{
m_MusicVolume = std::clamp<float>(volume, 0.0f, 1.0f);
for (auto& music : m_Music)
{
if (music.IsInitialized() && !music.IsFading()) // fading music will update itself
music.SetVolume(m_MusicVolume);
}
m_Music[m_CurrentMusicIndex].StopFading();
m_Music[m_CurrentMusicIndex].SetVolume(m_MusicVolume);
}
float SoundEngine::GetMusicVolume() const
{
return m_MusicVolume;
}
void SoundEngine::SaveVolume(bool isMinimized)
@@ -203,15 +205,10 @@ void SoundEngine::SetListenerOrientation(float forwardX, float forwardY, float f
ma_engine_listener_set_world_up(&m_Engine, 0, upX, -upY, upZ);
}
void SoundEngine::SetListenerVelocity(float x, float y, float z)
{
ma_engine_listener_set_velocity(&m_Engine, 0, x, y, z);
}
void SoundEngine::Update()
{
for (auto& music : m_Music)
music.Update(m_MusicVolume);
music.Update();
if (m_MasterVolumeFadeRatePerFrame)
{

View File

@@ -56,6 +56,8 @@ public:
void SetMusicVolume(float volume);
float GetMusicVolume() const;
void SaveVolume(bool isMinimized);
void RestoreVolume();
@@ -67,8 +69,6 @@ public:
void SetListenerOrientation(float forwardX, float forwardY, float forwardZ,
float upX, float upY, float upZ);
void SetListenerVelocity(float x, float y, float z);
void Update();

View File

@@ -36,7 +36,7 @@ PyObject* sndFadeInMusic(PyObject* poSelf, PyObject* poArgs)
if (!PyTuple_GetString(poArgs, 0, &szFileName))
return Py_BuildException();
SoundEngine::Instance().FadeInMusic(szFileName);
SoundEngine::Instance().FadeInMusic(szFileName, SoundEngine::Instance().GetMusicVolume());
return Py_BuildNone();
}