sdl2-compat: Fixed len_mult calculation in SDL_BuildAudioCVT()

From 819d44b7377e81e5c23bacef90569d79660552b7 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 9 Jan 2024 09:19:03 -0800
Subject: [PATCH] Fixed len_mult calculation in SDL_BuildAudioCVT()

This gets much closer to fixing audio_resampleLoss in testautomation
---
 src/sdl2_compat.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index b452728..735cb58 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -7271,18 +7271,18 @@ SDL_BuildAudioCVT(SDL_AudioCVT *cvt,
             cvt->needed = 0;
         }
 
+        if (src_channels < dst_channels) {
+            cvt->len_mult = ((cvt->len_mult * dst_channels) + (src_channels - 1)) / src_channels;
+        }
+
         if (src_rate < dst_rate) {
-            const int mult = (dst_rate / src_rate);
-            cvt->len_mult *= mult;
+            const double mult = ((double)dst_rate / (double)src_rate);
+            cvt->len_mult *= (int)SDL_ceil(mult);
             cvt->len_ratio *= mult;
         } else {
-            const int divisor = (src_rate / dst_rate);
+            const double divisor = ((double)src_rate / (double)dst_rate);
             cvt->len_ratio /= divisor;
         }
-
-        if (src_channels < dst_channels) {
-            cvt->len_mult = ((cvt->len_mult * dst_channels) + (src_channels - 1)) / src_channels;
-        }
     }
 
     return cvt->needed;
@@ -7297,7 +7297,7 @@ SDL_ConvertAudio(SDL_AudioCVT *cvt)
     int dst_channels, dst_rate;
 
     int src_len, dst_len, real_dst_len;
-    int src_samplesize, dst_samplesize;
+    int src_samplesize;
 
     /* Sanity check target pointer */
     if (cvt == NULL) {
@@ -7328,14 +7328,9 @@ SDL_ConvertAudio(SDL_AudioCVT *cvt)
     }
 
     src_samplesize = (SDL_AUDIO_BITSIZE(src_format) / 8) * src_channels;
-    dst_samplesize = (SDL_AUDIO_BITSIZE(dst_format) / 8) * dst_channels;
 
     src_len = cvt->len & ~(src_samplesize - 1);
-    dst_len = dst_samplesize * (src_len / src_samplesize);
-    if (src_rate < dst_rate) {
-        const double mult = ((double)dst_rate) / ((double)src_rate);
-        dst_len *= (int) SDL3_ceil(mult);
-    }
+    dst_len = cvt->len * cvt->len_mult;
 
     /* Run the audio converter */
     if (SDL_AudioStreamPut(stream2, cvt->buf, src_len) < 0 ||
@@ -7343,9 +7338,6 @@ SDL_ConvertAudio(SDL_AudioCVT *cvt)
         goto failure;
     }
 
-    dst_len = SDL_min(dst_len, cvt->len * cvt->len_mult);
-    dst_len = dst_len & ~(dst_samplesize - 1);
-
     /* Get back in the same buffer */
     real_dst_len = SDL_AudioStreamGet(stream2, cvt->buf, dst_len);
     if (real_dst_len < 0) {