Merge pull request #27 from amun3808/new-audio-system
Audio Engine volume bug & small update
This commit is contained in:
@@ -140,16 +140,18 @@ void MaSoundInstance::Config3D(bool toggle, float minDist, float maxDist)
|
||||
|
||||
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;
|
||||
m_FadeRatePerFrame = GetVolume() > toVolume ? -rate : rate;
|
||||
m_FadeTargetVolume = std::clamp<float>(toVolume, 0.0f, 1.0f);
|
||||
if (m_FadeTargetVolume != GetVolume())
|
||||
{
|
||||
const float rate = 1.0f / CS_CLIENT_FPS / secDurationFromMinMax;
|
||||
m_FadeRatePerFrame = GetVolume() > m_FadeTargetVolume ? -rate : rate;
|
||||
}
|
||||
}
|
||||
|
||||
void MaSoundInstance::StopFading()
|
||||
{
|
||||
m_FadeRatePerFrame = 0.0f;
|
||||
m_FadeTargetVolume = 0.0f;
|
||||
}
|
||||
|
||||
bool MaSoundInstance::IsFading() const
|
||||
@@ -157,17 +159,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);
|
||||
|
||||
@@ -42,7 +42,7 @@ public:
|
||||
|
||||
bool IsFading() const;
|
||||
|
||||
void Update(float volumeFactor = 1.0f);
|
||||
void Update();
|
||||
|
||||
private:
|
||||
std::string m_Identity;
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user