diff --git a/src/drivers/libretro/libretro.c b/src/drivers/libretro/libretro.c index dc8d5b8..04a7d89 100644 --- a/src/drivers/libretro/libretro.c +++ b/src/drivers/libretro/libretro.c @@ -1067,9 +1067,12 @@ static void stereo_filter_apply_delay(int32_t *sound_buffer, size_t size) stereo_filter_delay.samples_size = tmp_buffer_size; } - for (i = 0; i < size; i++) - stereo_filter_delay.samples[i + - stereo_filter_delay.samples_pos] = sound_buffer[i]; + /* Copy current samples into the delay buffer's tail. The previous + * implementation walked the array element-by-element; memcpy is + * trivially equivalent for non-overlapping int32_t blocks and + * lets the compiler/libc dispatch SIMD where available. */ + memcpy(stereo_filter_delay.samples + stereo_filter_delay.samples_pos, + sound_buffer, size * sizeof(int32_t)); stereo_filter_delay.samples_pos += size; diff --git a/src/filter.c b/src/filter.c index 3e4f364..b2272c5 100644 --- a/src/filter.c +++ b/src/filter.c @@ -1,4 +1,3 @@ -#include #include "fceu-types.h" #include "sound.h" @@ -11,35 +10,34 @@ static uint32_t mrindex; static uint32_t mrratio; -void SexyFilter2(int32_t *in, int32_t count) { - #ifdef moo - static int64_t acc = 0; - double x, p; - int64_t c; +int64_t sexyfilter_acc1 = 0, sexyfilter_acc2 = 0; - x = 2 * M_PI * 6000 / FSettings.SndRate; - p = ((double)2 - cos(x)) - sqrt(pow((double)2 - cos(x), 2) - 1); +/* SexyFilter2's separate accumulator. Like sexyfilter_acc1/_acc2 it + * was a function-local static, which meant FCEUSND_Power couldn't + * reset it - cart B in a long-running process would inherit cart A's + * lowpass state. Lifted to file scope alongside the others so + * SexyFilter_Reset can zero it too. */ +int64_t sexyfilter2_acc = 0; - c = p * 0x100000; - #endif - static int64_t acc = 0; - - while (count--) { - int64_t dropcurrent; - dropcurrent = ((*in << 16) - acc) >> 3; - - acc += dropcurrent; - *in = acc >> 16; - in++; -#if 0 - acc=((int64_t)0x100000-c)* *in + ((c*acc)>>20); - *in=acc>>20; - in++; -#endif - } +void SexyFilter_Reset(void) +{ + sexyfilter_acc1 = 0; + sexyfilter_acc2 = 0; + sexyfilter2_acc = 0; + /* mrindex is reset in MakeFilters, which FCEUI_Sound calls on + * every cart load, so it doesn't need to be reset here. */ } -int64_t sexyfilter_acc1 = 0, sexyfilter_acc2 = 0; +void SexyFilter2(int32_t *in, int32_t count) { + while (count--) { + int64_t dropcurrent; + dropcurrent = ((*in << 16) - sexyfilter2_acc) >> 3; + + sexyfilter2_acc += dropcurrent; + *in = sexyfilter2_acc >> 16; + in++; + } +} void SexyFilter(int32_t *in, int32_t *out, int32_t count) { int32_t mul1, mul2, vmul; diff --git a/src/filter.h b/src/filter.h index e881280..17c6b12 100644 --- a/src/filter.h +++ b/src/filter.h @@ -6,6 +6,15 @@ void MakeFilters(int32_t rate); void SexyFilter(int32_t *in, int32_t *out, int32_t count); void SexyFilter2(int32_t *in, int32_t count); +/* Zero the IIR filter state. The accumulators are saved/loaded with + * the rest of the sound state (sound.c FAC1/FAC2 entries), but they + * are not reinitialised by FCEUSND_Power on cart load. Without this, + * starting cart B in a process that previously ran cart A inherits + * cart A's IIR state - audibly minor but breaks frame-determinism for + * the first samples of cart B. */ +void SexyFilter_Reset(void); + extern int64_t sexyfilter_acc1, sexyfilter_acc2; +extern int64_t sexyfilter2_acc; #endif diff --git a/src/sound.c b/src/sound.c index 2fb2981..81fb6e3 100644 --- a/src/sound.c +++ b/src/sound.c @@ -649,8 +649,12 @@ static void RDoSQLQ(void) { totalout = wlookup1[ ttable[0][RectDutyCount[0]] + ttable[1][RectDutyCount[1]] ]; if (!inie[0] && !inie[1]) { - for (V = start; V < end; V++) - Wave[V >> 4] += totalout; + /* Both squares silent: amp[x] was forced to 0 above (line + * "if (!inie[x]) amp[x] = 0"), which propagates through + * ttable[x] to make totalout = wlookup1[0] = 0. The + * previous code looped (end - start) iterations adding 0 + * to Wave[V>>4] - genuinely no-op for ~30000 NES cycles + * per frame. Skip the loop entirely. */ } else { for (V = start; V < end; V++) { /* int tmpamp=0; @@ -970,8 +974,23 @@ int FlushEmulateSound(void) { end = NeoFilterSound(WaveHi, WaveFinal, SOUNDTS, &left); + /* Slide the trailing `left` coefficient-history samples back + * to the start of the buffer for next frame's filter, then + * clear the area between left and SOUNDTS so next frame's + * channel accumulators start at zero. + * + * The previous code cleared all the way to sizeof(WaveHi), + * but only indices [left, SOUNDTS) were dirtied this frame - + * everything past SOUNDTS is still zero from the prior + * frame's clear (or from FCEUSND_Power on first frame). + * WaveHi is 40000 entries = 160 KB; SOUNDTS is bounded by + * NES cycles per frame (~30000), so this saves ~40 KB of + * memset per HQ frame. The (SOUNDTS > left) guard handles + * the degenerate case of a very short frame where SOUNDTS + * may not have advanced past the coefficient history. */ memmove(WaveHi, WaveHi + SOUNDTS - left, left * sizeof(uint32_t)); - memset(WaveHi + left, 0, sizeof(WaveHi) - left * sizeof(uint32_t)); + if ((uint32_t)SOUNDTS > (uint32_t)left) + memset(WaveHi + left, 0, (SOUNDTS - left) * sizeof(uint32_t)); if (GameExpSound.HiSync) GameExpSound.HiSync(left); for (x = 0; x < 5; x++) @@ -1069,6 +1088,13 @@ void FCEUSND_Power(void) { soundtsoffs = 0; IRQFrameMode = 0x1; /* Only initialized by power-on reset, not by soft reset. NRS: don't start with Frame IRQ enabled for greater compatibility. Any game that actually uses frame IRQ will explicitly enable it, anyway. */ LoadDMCPeriod(DMCFormat & 0xF); + + /* Reset post-mix filter accumulators. These are file-scope in + * filter.c and were not previously cleared on cart load, so a + * second cart loaded in the same process inherited the first + * cart's IIR state. Audibly minor on its own but breaks + * frame-determinism for the first samples of a new run. */ + SexyFilter_Reset(); } @@ -1096,12 +1122,23 @@ void SetSoundVariables(void) { DoSQ1 = RDoSQ1; DoSQ2 = RDoSQ2; } else { - DoNoise = DoTriangle = DoPCM = DoSQ1 = DoSQ2 = Dummyfunc; - DoSQ1 = RDoSQLQ; - DoSQ2 = RDoSQLQ; + /* In LQ mode the squares are mixed by RDoSQLQ in a single + * call, and the triangle/noise/PCM channels are mixed by + * RDoTriangleNoisePCMLQ in a single call - both functions + * advance their respective ChannelBC and process all the + * channels they handle in one pass. The previous code + * pointed DoTriangle/DoNoise/DoPCM all at + * RDoTriangleNoisePCMLQ, which meant the second and third + * call entered the function only to be rejected by the + * "if (end <= start) return;" guard at the top. Same for + * DoSQ1/DoSQ2 -> RDoSQLQ. Map only one entry each to the + * real worker and stub the others so we save the redundant + * call+early-return overhead. */ + DoSQ1 = RDoSQLQ; + DoSQ2 = Dummyfunc; DoTriangle = RDoTriangleNoisePCMLQ; - DoNoise = RDoTriangleNoisePCMLQ; - DoPCM = RDoTriangleNoisePCMLQ; + DoNoise = Dummyfunc; + DoPCM = Dummyfunc; } } else { DoNoise = DoTriangle = DoPCM = DoSQ1 = DoSQ2 = Dummyfunc; @@ -1221,6 +1258,7 @@ SFORMAT FCEUSND_STATEINFO[] = { { &wlcount[3], sizeof(wlcount[3]) | FCEUSTATE_RLSB, "WLC4" }, { &sexyfilter_acc1, sizeof(sexyfilter_acc1) | FCEUSTATE_RLSB, "FAC1" }, { &sexyfilter_acc2, sizeof(sexyfilter_acc2) | FCEUSTATE_RLSB, "FAC2" }, + { &sexyfilter2_acc, sizeof(sexyfilter2_acc) | FCEUSTATE_RLSB, "FAC3" }, { &lq_tcout, sizeof(lq_tcout) | FCEUSTATE_RLSB, "TCOU"}, /* 2018-12-14 - Wii and possibly other big-endian platforms are having