From fd8931c87eb254dce947451619e7b4911fcb1ddf Mon Sep 17 00:00:00 2001 From: Megamouse Date: Mon, 6 Jun 2022 18:22:43 +0200 Subject: [PATCH] cellAudio: simplify sound mixer There was no need to treat the first mix differently if all it did was init the buffer anyway. Let's just always memset to zero. --- rpcs3/Emu/Cell/Modules/cellAudio.cpp | 173 +++++++-------------------- 1 file changed, 44 insertions(+), 129 deletions(-) diff --git a/rpcs3/Emu/Cell/Modules/cellAudio.cpp b/rpcs3/Emu/Cell/Modules/cellAudio.cpp index 04684b98d6..ac6d379ff1 100644 --- a/rpcs3/Emu/Cell/Modules/cellAudio.cpp +++ b/rpcs3/Emu/Cell/Modules/cellAudio.cpp @@ -892,10 +892,11 @@ void cell_audio_thread::mix(float *out_buffer, s32 offset) constexpr u32 channels = static_cast(downmix); constexpr u32 out_buffer_sz = channels * AUDIO_BUFFER_SAMPLES; - bool first_mix = true; - const float master_volume = g_cfg.audio.volume / 100.0f; + // Reset out_buffer + std::memset(out_buffer, 0, out_buffer_sz * sizeof(float)); + // mixing for (auto& port : ports) { @@ -929,138 +930,58 @@ void cell_audio_thread::mix(float *out_buffer, s32 offset) if (port.num_channels == 2) { - if (first_mix) + for (u32 out = 0, in = 0; out < out_buffer_sz; out += channels, in += 2) { - for (u32 out = 0, in = 0; out < out_buffer_sz; out += channels, in += 2) - { - step_volume(port); + step_volume(port); - const float left = buf[in + 0] * m; - const float right = buf[in + 1] * m; + const float left = buf[in + 0] * m; + const float right = buf[in + 1] * m; - out_buffer[out + 0] = left; - out_buffer[out + 1] = right; - - if constexpr (downmix != AudioChannelCnt::STEREO) - { - out_buffer[out + 2] = 0.0f; - out_buffer[out + 3] = 0.0f; - out_buffer[out + 4] = 0.0f; - out_buffer[out + 5] = 0.0f; - - if constexpr (downmix != AudioChannelCnt::SURROUND_5_1) - { - out_buffer[out + 6] = 0.0f; - out_buffer[out + 7] = 0.0f; - } - } - } - first_mix = false; - } - else - { - for (u32 out = 0, in = 0; out < out_buffer_sz; out += channels, in += 2) - { - step_volume(port); - - const float left = buf[in + 0] * m; - const float right = buf[in + 1] * m; - - out_buffer[out + 0] += left; - out_buffer[out + 1] += right; - } + out_buffer[out + 0] += left; + out_buffer[out + 1] += right; } } else if (port.num_channels == 8) { - if (first_mix) + for (u32 out = 0, in = 0; out < out_buffer_sz; out += channels, in += 8) { - for (u32 out = 0, in = 0; out < out_buffer_sz; out += channels, in += 8) + step_volume(port); + + const float left = buf[in + 0] * m; + const float right = buf[in + 1] * m; + const float center = buf[in + 2] * m; + const float low_freq = buf[in + 3] * m; + const float side_left = buf[in + 4] * m; + const float side_right = buf[in + 5] * m; + const float rear_left = buf[in + 6] * m; + const float rear_right = buf[in + 7] * m; + + if constexpr (downmix == AudioChannelCnt::STEREO) { - step_volume(port); - - const float left = buf[in + 0] * m; - const float right = buf[in + 1] * m; - const float center = buf[in + 2] * m; - [[maybe_unused]] - const float low_freq = buf[in + 3] * m; - const float side_left = buf[in + 4] * m; - const float side_right = buf[in + 5] * m; - const float rear_left = buf[in + 6] * m; - const float rear_right = buf[in + 7] * m; - - if constexpr (downmix == AudioChannelCnt::STEREO) - { - // Don't mix in the lfe as per dolby specification and based on documentation - const float mid = center * 0.5f; - out_buffer[out + 0] = left * minus_3db + mid + side_left * 0.5f + rear_left * 0.5f; - out_buffer[out + 1] = right * minus_3db + mid + side_right * 0.5f + rear_right * 0.5f; - } - else if constexpr (downmix == AudioChannelCnt::SURROUND_5_1) - { - out_buffer[out + 0] = left; - out_buffer[out + 1] = right; - out_buffer[out + 2] = center; - out_buffer[out + 3] = low_freq; - out_buffer[out + 4] = side_left + rear_left; - out_buffer[out + 5] = side_right + rear_right; - } - else - { - out_buffer[out + 0] = left; - out_buffer[out + 1] = right; - out_buffer[out + 2] = center; - out_buffer[out + 3] = low_freq; - out_buffer[out + 4] = rear_left; - out_buffer[out + 5] = rear_right; - out_buffer[out + 6] = side_left; - out_buffer[out + 7] = side_right; - } + // Don't mix in the lfe as per dolby specification and based on documentation + const float mid = center * 0.5f; + out_buffer[out + 0] += left * minus_3db + mid + side_left * 0.5f + rear_left * 0.5f; + out_buffer[out + 1] += right * minus_3db + mid + side_right * 0.5f + rear_right * 0.5f; } - first_mix = false; - } - else - { - for (u32 out = 0, in = 0; out < out_buffer_sz; out += channels, in += 8) + else if constexpr (downmix == AudioChannelCnt::SURROUND_5_1) { - step_volume(port); - - const float left = buf[in + 0] * m; - const float right = buf[in + 1] * m; - const float center = buf[in + 2] * m; - const float low_freq = buf[in + 3] * m; - const float side_left = buf[in + 4] * m; - const float side_right = buf[in + 5] * m; - const float rear_left = buf[in + 6] * m; - const float rear_right = buf[in + 7] * m; - - if constexpr (downmix == AudioChannelCnt::STEREO) - { - // Don't mix in the lfe as per dolby specification and based on documentation - const float mid = center * 0.5f; - out_buffer[out + 0] += left * minus_3db + mid + side_left * 0.5f + rear_left * 0.5f; - out_buffer[out + 1] += right * minus_3db + mid + side_right * 0.5f + rear_right * 0.5f; - } - else if constexpr (downmix == AudioChannelCnt::SURROUND_5_1) - { - out_buffer[out + 0] += left; - out_buffer[out + 1] += right; - out_buffer[out + 2] += center; - out_buffer[out + 3] += low_freq; - out_buffer[out + 4] += side_left + rear_left; - out_buffer[out + 5] += side_right + rear_right; - } - else - { - out_buffer[out + 0] += left; - out_buffer[out + 1] += right; - out_buffer[out + 2] += center; - out_buffer[out + 3] += low_freq; - out_buffer[out + 4] += rear_left; - out_buffer[out + 5] += rear_right; - out_buffer[out + 6] += side_left; - out_buffer[out + 7] += side_right; - } + out_buffer[out + 0] += left; + out_buffer[out + 1] += right; + out_buffer[out + 2] += center; + out_buffer[out + 3] += low_freq; + out_buffer[out + 4] += side_left + rear_left; + out_buffer[out + 5] += side_right + rear_right; + } + else + { + out_buffer[out + 0] += left; + out_buffer[out + 1] += right; + out_buffer[out + 2] += center; + out_buffer[out + 3] += low_freq; + out_buffer[out + 4] += rear_left; + out_buffer[out + 5] += rear_right; + out_buffer[out + 6] += side_left; + out_buffer[out + 7] += side_right; } } } @@ -1069,12 +990,6 @@ void cell_audio_thread::mix(float *out_buffer, s32 offset) fmt::throw_exception("Unknown channel count (port=%u, channel=%d)", port.number, port.num_channels); } } - - // Nothing was mixed, memset out_buffer to 0 - if (first_mix) - { - std::memset(out_buffer, 0, out_buffer_sz * sizeof(float)); - } } void cell_audio_thread::finish_port_volume_stepping()