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)
|
void MaSoundInstance::Fade(float toVolume, float secDurationFromMinMax)
|
||||||
{
|
{
|
||||||
toVolume = std::clamp<float>(toVolume, 0.0f, 1.0f);
|
m_FadeTargetVolume = std::clamp<float>(toVolume, 0.0f, 1.0f);
|
||||||
m_FadeTargetVolume = toVolume;
|
if (m_FadeTargetVolume != GetVolume())
|
||||||
|
{
|
||||||
float rate = 1.0f / 61.0f / secDurationFromMinMax;
|
const float rate = 1.0f / CS_CLIENT_FPS / secDurationFromMinMax;
|
||||||
m_FadeRatePerFrame = GetVolume() > toVolume ? -rate : rate;
|
m_FadeRatePerFrame = GetVolume() > m_FadeTargetVolume ? -rate : rate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MaSoundInstance::StopFading()
|
void MaSoundInstance::StopFading()
|
||||||
{
|
{
|
||||||
m_FadeRatePerFrame = 0.0f;
|
m_FadeRatePerFrame = 0.0f;
|
||||||
|
m_FadeTargetVolume = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MaSoundInstance::IsFading() const
|
bool MaSoundInstance::IsFading() const
|
||||||
@@ -157,17 +159,17 @@ bool MaSoundInstance::IsFading() const
|
|||||||
return m_FadeRatePerFrame != 0.0f;
|
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)
|
if (m_FadeRatePerFrame != 0.0f)
|
||||||
{
|
{
|
||||||
float targetVolume = std::clamp<float>(m_FadeTargetVolume * volumeFactor, 0.0f, 1.0f);
|
float targetVolume = std::clamp<float>(m_FadeTargetVolume, 0.0f, 1.0f);
|
||||||
float volume = std::clamp<float>(GetVolume() + (m_FadeRatePerFrame * volumeFactor), 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))
|
if ((m_FadeRatePerFrame > 0.0f && volume >= targetVolume) || (m_FadeRatePerFrame < 0.0f && volume <= targetVolume))
|
||||||
{
|
{
|
||||||
volume = m_FadeTargetVolume * volumeFactor;
|
volume = m_FadeTargetVolume;
|
||||||
m_FadeRatePerFrame = 0.0f;
|
m_FadeRatePerFrame = 0.0f;
|
||||||
if (volume <= 0.0f)
|
if (m_FadeTargetVolume <= 0.0f)
|
||||||
ma_sound_stop(&m_Sound);
|
ma_sound_stop(&m_Sound);
|
||||||
}
|
}
|
||||||
ma_sound_set_volume(&m_Sound, volume);
|
ma_sound_set_volume(&m_Sound, volume);
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ public:
|
|||||||
|
|
||||||
bool IsFading() const;
|
bool IsFading() const;
|
||||||
|
|
||||||
void Update(float volumeFactor = 1.0f);
|
void Update();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_Identity;
|
std::string m_Identity;
|
||||||
|
|||||||
@@ -154,11 +154,13 @@ void SoundEngine::FadeOutAllMusic()
|
|||||||
void SoundEngine::SetMusicVolume(float volume)
|
void SoundEngine::SetMusicVolume(float volume)
|
||||||
{
|
{
|
||||||
m_MusicVolume = std::clamp<float>(volume, 0.0f, 1.0f);
|
m_MusicVolume = std::clamp<float>(volume, 0.0f, 1.0f);
|
||||||
for (auto& music : m_Music)
|
m_Music[m_CurrentMusicIndex].StopFading();
|
||||||
{
|
m_Music[m_CurrentMusicIndex].SetVolume(m_MusicVolume);
|
||||||
if (music.IsInitialized() && !music.IsFading()) // fading music will update itself
|
}
|
||||||
music.SetVolume(m_MusicVolume);
|
|
||||||
}
|
float SoundEngine::GetMusicVolume() const
|
||||||
|
{
|
||||||
|
return m_MusicVolume;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SoundEngine::SaveVolume(bool isMinimized)
|
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);
|
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()
|
void SoundEngine::Update()
|
||||||
{
|
{
|
||||||
for (auto& music : m_Music)
|
for (auto& music : m_Music)
|
||||||
music.Update(m_MusicVolume);
|
music.Update();
|
||||||
|
|
||||||
if (m_MasterVolumeFadeRatePerFrame)
|
if (m_MasterVolumeFadeRatePerFrame)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -56,6 +56,8 @@ public:
|
|||||||
|
|
||||||
void SetMusicVolume(float volume);
|
void SetMusicVolume(float volume);
|
||||||
|
|
||||||
|
float GetMusicVolume() const;
|
||||||
|
|
||||||
void SaveVolume(bool isMinimized);
|
void SaveVolume(bool isMinimized);
|
||||||
|
|
||||||
void RestoreVolume();
|
void RestoreVolume();
|
||||||
@@ -67,8 +69,6 @@ public:
|
|||||||
void SetListenerOrientation(float forwardX, float forwardY, float forwardZ,
|
void SetListenerOrientation(float forwardX, float forwardY, float forwardZ,
|
||||||
float upX, float upY, float upZ);
|
float upX, float upY, float upZ);
|
||||||
|
|
||||||
void SetListenerVelocity(float x, float y, float z);
|
|
||||||
|
|
||||||
void Update();
|
void Update();
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ PyObject* sndFadeInMusic(PyObject* poSelf, PyObject* poArgs)
|
|||||||
if (!PyTuple_GetString(poArgs, 0, &szFileName))
|
if (!PyTuple_GetString(poArgs, 0, &szFileName))
|
||||||
return Py_BuildException();
|
return Py_BuildException();
|
||||||
|
|
||||||
SoundEngine::Instance().FadeInMusic(szFileName);
|
SoundEngine::Instance().FadeInMusic(szFileName, SoundEngine::Instance().GetMusicVolume());
|
||||||
return Py_BuildNone();
|
return Py_BuildNone();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user