AudioCommon/WASAPI: Do volume adjustment only when really needed

This skips a potentially costly loop if volume is 100% or 0%,
as for former there is no need for volume adjustment,
while latter can be solved by specifying a AUDCLNT_BUFFERFLAGS_SILENT flag
This commit is contained in:
Silent 2019-08-17 11:44:51 +02:00
parent 991b3ba8c2
commit c373890505
No known key found for this signature in database
GPG Key ID: AE53149BB0C45AF1

View File

@ -361,14 +361,23 @@ void WASAPIStream::SoundLoop()
WaitForSingleObject(m_need_data_event.get(), 1000);
m_audio_renderer->GetBuffer(m_frames_in_buffer, &data);
GetMixer()->Mix(reinterpret_cast<s16*>(data), m_frames_in_buffer);
float volume = SConfig::GetInstance().m_IsMuted ? 0 : SConfig::GetInstance().m_Volume / 100.;
s16* audio_data = reinterpret_cast<s16*>(data);
GetMixer()->Mix(audio_data, m_frames_in_buffer);
for (u32 i = 0; i < m_frames_in_buffer * 2; i++)
reinterpret_cast<s16*>(data)[i] = static_cast<s16>(reinterpret_cast<s16*>(data)[i] * volume);
const SConfig& config = SConfig::GetInstance();
const bool is_muted = config.m_IsMuted || config.m_Volume == 0;
const bool need_volume_adjustment = config.m_Volume != 100 && !is_muted;
m_audio_renderer->ReleaseBuffer(m_frames_in_buffer, 0);
if (need_volume_adjustment)
{
const float volume = config.m_Volume / 100.0f;
for (u32 i = 0; i < m_frames_in_buffer * 2; i++)
*audio_data++ *= volume;
}
m_audio_renderer->ReleaseBuffer(m_frames_in_buffer, is_muted ? AUDCLNT_BUFFERFLAGS_SILENT : 0);
}
}