Audio: Fix overflows that wrap instead of clamping, and swapped underrun padding

- StereoResampler: the interpolation multiplied a 17-bit difference by a
  16-bit fraction in 32 bits, which wraps when adjacent samples are more
  than half the range apart. Same with the volume multiply on a loud mix.
- StereoResampler: the padding after an underrun took left from the right
  sample and right from the left.
- SAS reverb: the four comb products could sum past 32 bits, and the
  return multiply overflowed once the output passed 16 bits.
- SAS: a fast decay can leave the envelope height negative for a sample,
  which mixed that sample inverted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-11-09 17:29:39 +01:00
1 parent aa7e5fa5dd
commit 3c41ace5c8
3 files changed
+17 -9

No files matched your search

+3 -1
View File
@@ -622,7 +622,9 @@ void SasInstance::MixVoice(SasVoice &voice) {
// The maximum envelope height (PSP_SAS_ENVELOPE_HEIGHT_MAX) is (1 << 30) - 1.
// Reduce it to 14 bits, by shifting off 15. Round up by adding (1 << 14) first.
int envelopeValue = voice.envelope.GetHeight();
// A fast decay can leave the height below zero for a sample, as the switch to sustain
// doesn't reset it. Don't let that invert the waveform into a full-scale click.
int envelopeValue = std::max(voice.envelope.GetHeight(), 0);
voice.envelope.Step();
envelopeValue = (envelopeValue + (1 << 14)) >> 15;
+7 -4
View File
@@ -15,6 +15,7 @@
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <algorithm>
#include <cstdint>
#include <cstring>
@@ -325,8 +326,9 @@ void SasReverb::ProcessReverb(int16_t *output, const int16_t *input, size_t inpu
b[d.mLDIFF] = clamp_s16(Lin + (b[d.dRDIFF] * d.vWALL >> 15) - (b[d.mLDIFF - 1]*d.vIIR >> 15) + b[d.mLDIFF - 1]); // R - to - L
b[d.mRDIFF] = clamp_s16(Rin + (b[d.dLDIFF] * d.vWALL >> 15) - (b[d.mRDIFF - 1]*d.vIIR >> 15) + b[d.mRDIFF - 1]); // L - to - R
// ___Early Echo(Comb Filter, with input from buffer)__________________________
int32_t Lout = ((d.vCOMB1*b[d.mLCOMB1] + d.vCOMB2*b[d.mLCOMB2] + d.vCOMB3*b[d.mLCOMB3] + d.vCOMB4*b[d.mLCOMB4]) >> 15);
int32_t Rout = ((d.vCOMB1*b[d.mRCOMB1] + d.vCOMB2*b[d.mRCOMB2] + d.vCOMB3*b[d.mRCOMB3] + d.vCOMB4*b[d.mRCOMB4]) >> 15);
// Four full-scale products can sum past 32 bits.
int32_t Lout = (int32_t)(((int64_t)d.vCOMB1*b[d.mLCOMB1] + (int64_t)d.vCOMB2*b[d.mLCOMB2] + (int64_t)d.vCOMB3*b[d.mLCOMB3] + (int64_t)d.vCOMB4*b[d.mLCOMB4]) >> 15);
int32_t Rout = (int32_t)(((int64_t)d.vCOMB1*b[d.mRCOMB1] + (int64_t)d.vCOMB2*b[d.mRCOMB2] + (int64_t)d.vCOMB3*b[d.mRCOMB3] + (int64_t)d.vCOMB4*b[d.mRCOMB4]) >> 15);
// ___Late Reverb APF1(All Pass Filter 1, with input from COMB)________________
b[d.mLAPF1] = clamp_s16(Lout - (d.vAPF1*b[(d.mLAPF1 - d.dAPF1)] >> 15));
Lout = b[(d.mLAPF1 - d.dAPF1)] + (b[d.mLAPF1] * d.vAPF1 >> 15);
@@ -338,8 +340,9 @@ void SasReverb::ProcessReverb(int16_t *output, const int16_t *input, size_t inpu
b[d.mRAPF2] = clamp_s16(Rout - (d.vAPF2*b[(d.mRAPF2 - d.dAPF2)] >> 15));
Rout = b[(d.mRAPF2 - d.dAPF2)] + (b[d.mRAPF2] * d.vAPF2 >> 15);
// ___Output to Mixer(Output volume multiplied with input from APF2)___________
output[i * 4 + 0] = clamp_s16((Lout * volLeft) >> 15);
output[i * 4 + 1] = clamp_s16((Rout * volRight) >> 15);
// Lout reaches about 17 bits and the volume 18, so this needs 64 bits.
output[i * 4 + 0] = (int16_t)std::clamp(((int64_t)Lout * volLeft) >> 15, (int64_t)-32768, (int64_t)32767);
output[i * 4 + 1] = (int16_t)std::clamp(((int64_t)Rout * volRight) >> 15, (int64_t)-32768, (int64_t)32767);
output[i * 4 + 2] = 0;
output[i * 4 + 3] = 0;
+7 -4
View File
@@ -99,7 +99,8 @@ inline void ClampBufferToS16(s16 *out, const s32 *in, size_t size, int factor) {
// so CrossSIMD won't be very useful.
// LLVM autovec does an okay job with this on ARM64, it turns out.
for (size_t i = 0; i < size; i++) {
out[i] = clamp_s16((in[i] * factor) >> 12);
// A loud mix times a 12-bit factor can pass 32 bits.
out[i] = (s16)std::clamp(((s64)in[i] * factor) >> 12, (s64)-32768, (s64)32767);
}
} else {
#ifdef _M_SSE
@@ -150,7 +151,8 @@ void StereoResampler::Clear() {
}
inline int16_t MixSingleSample(int16_t s1, int16_t s2, uint16_t frac) {
int32_t value = s1 + (((s2 - s1) * frac) >> 16);
// The difference is up to 17 bits and frac 16, so the product needs 64 bits.
int32_t value = s1 + (int32_t)(((int64_t)(s2 - s1) * frac) >> 16);
if (value < -32767)
return -32767;
else if (value > 32767)
@@ -237,8 +239,9 @@ void StereoResampler::Mix(s16 *samples, unsigned int numSamples, bool consider_f
// Padding with the last value to reduce clicking
short s[2];
s[0] = clamp_s16(buffer_[(indexR - 1) & INDEX_MASK]);
s[1] = clamp_s16(buffer_[(indexR - 2) & INDEX_MASK]);
// The last frame consumed starts at indexR - 2, left first.
s[0] = buffer_[(indexR - 2) & INDEX_MASK];
s[1] = buffer_[(indexR - 1) & INDEX_MASK];
for (; currentSample < numSamples * 2; currentSample += 2) {
samples[currentSample] = s[0];
samples[currentSample + 1] = s[1];