From 675ffc1024281d2ab22cac381f448233e69bd86a Mon Sep 17 00:00:00 2001 From: MerryMage Date: Sat, 8 Sep 2018 22:21:49 +0100 Subject: [PATCH] dsp_interface: Move volume control to audio thread We also clamp the linear volume value to [0.0, 1.0]. Do nothing if linear volume is 1.0. --- src/audio_core/dsp_interface.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/audio_core/dsp_interface.cpp b/src/audio_core/dsp_interface.cpp index ce17414a94..aa4bb1fd1d 100644 --- a/src/audio_core/dsp_interface.cpp +++ b/src/audio_core/dsp_interface.cpp @@ -42,13 +42,6 @@ void DspInterface::OutputFrame(StereoFrame16& frame) { if (!sink) return; - // Implementation of the hardware volume slider with a dynamic range of 60 dB - double volume_scale_factor = std::exp(6.90775 * Settings::values.volume) * 0.001; - for (std::size_t i = 0; i < frame.size(); i++) { - frame[i][0] = static_cast(frame[i][0] * volume_scale_factor); - frame[i][1] = static_cast(frame[i][1] * volume_scale_factor); - } - fifo.Push(frame.data(), frame.size()); } @@ -75,6 +68,16 @@ void DspInterface::OutputCallback(s16* buffer, std::size_t num_frames) { for (std::size_t i = frames_written; i < num_frames; i++) { std::memcpy(buffer + 2 * i, &last_frame[0], 2 * sizeof(s16)); } + + // Implementation of the hardware volume slider with a dynamic range of 60 dB + const float linear_volume = std::clamp(Settings::values.volume, 0.0f, 1.0f); + if (linear_volume != 1.0) { + const float volume_scale_factor = std::exp(6.90775f * linear_volume) * 0.001f; + for (std::size_t i = 0; i < num_frames; i++) { + buffer[i * 2 + 0] = static_cast(buffer[i * 2 + 0] * volume_scale_factor); + buffer[i * 2 + 1] = static_cast(buffer[i * 2 + 1] * volume_scale_factor); + } + } } } // namespace AudioCore