From 6ca7a7654b9e4cce9416f105502f64f2109b6332 Mon Sep 17 00:00:00 2001 From: Johannes Schultz Date: Sat, 27 Apr 2024 22:14:27 +0000 Subject: [PATCH] Merged revision(s) 20662 from trunk/OpenMPT: [Fix] Opal: With mix rates exceeding 131kHz, it was possible that the interpolated output overflowed. The new linear interpolation is based on OpenMPT's own mixer code, and may also be slightly faster because it divides by SampleRate only once (https://bugs.openmpt.org/view.php?id=1775). ........ git-svn-id: https://source.openmpt.org/svn/openmpt/branches/OpenMPT-1.31@20663 56274372-70c3-4bfc-bfc3-4c3a0b034d27 --- soundlib/opal.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/soundlib/opal.h b/soundlib/opal.h index 556433e9dcf..b10ad285d49 100644 --- a/soundlib/opal.h +++ b/soundlib/opal.h @@ -22,6 +22,7 @@ #include +#include "../common/mptBaseUtils.h" @@ -574,9 +575,9 @@ void Opal::Sample(int16_t *left, int16_t *right) { } // Mix with the partial accumulation - int32_t omblend = SampleRate - SampleAccum; - *left = static_cast((LastOutput[0] * omblend + CurrOutput[0] * SampleAccum) / SampleRate); - *right = static_cast((LastOutput[1] * omblend + CurrOutput[1] * SampleAccum) / SampleRate); + const int32_t fract = Util::muldivr(SampleAccum, 65536, SampleRate); + *left = static_cast(LastOutput[0] + ((fract * (CurrOutput[0] - LastOutput[0])) / 65536)); + *right = static_cast(LastOutput[1] + ((fract * (CurrOutput[1] - LastOutput[1])) / 65536)); SampleAccum += OPL3SampleRate; } @@ -606,14 +607,14 @@ void Opal::Output(int16_t &left, int16_t &right) { else if (leftmix > 0x7FFF) left = 0x7FFF; else - left = static_cast(leftmix); + left = static_cast(leftmix); if (rightmix < -0x8000) right = -0x8000; else if (rightmix > 0x7FFF) right = 0x7FFF; else - right = static_cast(rightmix); + right = static_cast(rightmix); Clock++;