From facada5d04b0f6aa0ff1b23d2b6f916341b8fb02 Mon Sep 17 00:00:00 2001 From: Ziemas Date: Tue, 17 Oct 2023 04:44:35 +0200 Subject: [PATCH] SPU2: Simplify reverb resampling --- pcsx2/SPU2/Reverb.cpp | 49 +++++++++++++++++++------------------------ pcsx2/SPU2/defs.h | 6 +++--- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/pcsx2/SPU2/Reverb.cpp b/pcsx2/SPU2/Reverb.cpp index 2f007df04d..5718706711 100644 --- a/pcsx2/SPU2/Reverb.cpp +++ b/pcsx2/SPU2/Reverb.cpp @@ -99,47 +99,34 @@ static constexpr std::array filter_coefs = { s32 __forceinline V_Core::ReverbDownsample(bool right) { + int index = (RevbSampleBufPos - NUM_TAPS) & 63; s32 out = 0; - // Skipping the 0 coefs. - for (u32 i = 0; i < NUM_TAPS; i += 2) + for (int i = 0; i < NUM_TAPS; i++) { - out += RevbDownBuf[right][((RevbSampleBufPos - NUM_TAPS) + i) & 63] * filter_coefs[i]; + out += RevbDownBuf[right][index + i] * filter_coefs[i]; } - // We also skipped the middle so add that in. - out += RevbDownBuf[right][((RevbSampleBufPos - NUM_TAPS) + 19) & 63] * filter_coefs[19]; - out >>= 15; out = std::clamp(out, INT16_MIN, INT16_MAX); return out; } -StereoOut32 __forceinline V_Core::ReverbUpsample(bool phase) +StereoOut32 __forceinline V_Core::ReverbUpsample() { + int index = (RevbSampleBufPos - NUM_TAPS) & 63; s32 ls = 0, rs = 0; - if (phase) + for (int i = 0; i < NUM_TAPS; i++) { - ls += RevbUpBuf[0][(((RevbSampleBufPos - NUM_TAPS) >> 1) + 9) & 63] * filter_coefs[19]; - rs += RevbUpBuf[1][(((RevbSampleBufPos - NUM_TAPS) >> 1) + 9) & 63] * filter_coefs[19]; - } - else - { - for (u32 i = 0; i < (NUM_TAPS >> 1) + 1; i++) - { - ls += RevbUpBuf[0][(((RevbSampleBufPos - NUM_TAPS) >> 1) + i) & 63] * filter_coefs[i * 2]; - } - for (u32 i = 0; i < (NUM_TAPS >> 1) + 1; i++) - { - rs += RevbUpBuf[1][(((RevbSampleBufPos - NUM_TAPS) >> 1) + i) & 63] * filter_coefs[i * 2]; - } + ls += RevbUpBuf[0][index + i] * (filter_coefs[i] * 2); + rs += RevbUpBuf[1][index + i] * (filter_coefs[i] * 2); } - ls >>= 14; + ls >>= 15; ls = std::clamp(ls, INT16_MIN, INT16_MAX); - rs >>= 14; + rs >>= 15; rs = std::clamp(rs, INT16_MIN, INT16_MAX); return {ls, rs}; @@ -164,8 +151,10 @@ StereoOut32 V_Core::DoReverb(const StereoOut32& Input) return StereoOut32::Empty; } - RevbDownBuf[0][RevbSampleBufPos & 63] = Input.Left; - RevbDownBuf[1][RevbSampleBufPos & 63] = Input.Right; + RevbDownBuf[0][RevbSampleBufPos] = Input.Left; + RevbDownBuf[1][RevbSampleBufPos] = Input.Right; + RevbDownBuf[0][RevbSampleBufPos + 64] = Input.Left; + RevbDownBuf[1][RevbSampleBufPos + 64] = Input.Right; bool R = Cycles & 1; @@ -245,9 +234,13 @@ StereoOut32 V_Core::DoReverb(const StereoOut32& Input) _spu2mem[apf2_dst] = clamp_mix(apf2); } - RevbUpBuf[R][(RevbSampleBufPos >> 1) & 63] = clamp_mix(out); + RevbUpBuf[R][RevbSampleBufPos] = clamp_mix(out); + RevbUpBuf[!R][RevbSampleBufPos] = 0; - RevbSampleBufPos++; + RevbUpBuf[R][RevbSampleBufPos + 64] = clamp_mix(out); + RevbUpBuf[!R][RevbSampleBufPos + 64] = 0; - return ReverbUpsample(RevbSampleBufPos & 1); + RevbSampleBufPos = (RevbSampleBufPos + 1) & 63; + + return ReverbUpsample(); } diff --git a/pcsx2/SPU2/defs.h b/pcsx2/SPU2/defs.h index 0e06a70826..6733ad0ac0 100644 --- a/pcsx2/SPU2/defs.h +++ b/pcsx2/SPU2/defs.h @@ -422,8 +422,8 @@ struct V_Core V_Reverb Revb; // Reverb Registers - s32 RevbDownBuf[2][64]; // Downsample buffer for reverb, one for each channel - s32 RevbUpBuf[2][64]; // Upsample buffer for reverb, one for each channel + s32 RevbDownBuf[2][64 * 2]; // Downsample buffer for reverb, one for each channel + s32 RevbUpBuf[2][64 * 2]; // Upsample buffer for reverb, one for each channel u32 RevbSampleBufPos; u32 EffectsStartA; u32 EffectsEndA; @@ -487,7 +487,7 @@ struct V_Core s32 RevbGetIndexer(s32 offset); s32 ReverbDownsample(bool right); - StereoOut32 ReverbUpsample(bool phase); + StereoOut32 ReverbUpsample(); StereoOut32 ReadInput(); StereoOut32 ReadInput_HiFi();