From b7152c2f277b08b12f53de28bd4c6bfc98f10c49 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 9 Jun 2013 19:01:33 -0700 Subject: [PATCH] Correct envelope scaling in sas mixing. Sound effects seem a bit loud, but this matches what JPCSP does and I've added comments based on my understanding of it. It makes sense. This makes all the sounds that never get muted when the envelope hits 0 go away. Hurray. Thanks to help from @i1x. --- Core/HW/SasAudio.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Core/HW/SasAudio.cpp b/Core/HW/SasAudio.cpp index 5cfa467412..349da53749 100644 --- a/Core/HW/SasAudio.cpp +++ b/Core/HW/SasAudio.cpp @@ -414,12 +414,14 @@ void SasInstance::Mix(u32 outAddr, u32 inAddr, int leftVol, int rightVol) { int sample = resampleBuffer[sampleFrac / PSP_SAS_PITCH_BASE + 2]; sampleFrac += voice.pitch; - // Reduce envelope to 15 bits, rounding down. + // 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(); - envelopeValue = ((envelopeValue >> 15) + 1) >> 1; + envelopeValue = (envelopeValue + (1 << 14)) >> 15; // We just scale by the envelope before we scale by volumes. - sample = sample * (envelopeValue + 0x4000) >> 15; + // Again, we round up by adding (1 << 14) first (*after* multiplying.) + sample = ((sample * envelopeValue) + (1 << 14)) >> 15; // We mix into this 32-bit temp buffer and clip in a second loop // Ideally, the shift right should be there too but for now I'm concerned about