sdl2-compat: audiocvt: Fix len_ratio for channel conversion

From cd0f1435772a615403099e2e46625abbd8ac0478 Mon Sep 17 00:00:00 2001
From: Cameron Gutman <[EMAIL REDACTED]>
Date: Sun, 20 Apr 2025 20:18:44 -0500
Subject: [PATCH] audiocvt: Fix len_ratio for channel conversion

Fixes #452
---
 src/sdl2_compat.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index a010378..05625e3 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -11555,7 +11555,12 @@ SDL_BuildAudioCVT(SDL_AudioCVT *cvt,
         }
 
         if (src_channels < dst_channels) {
-            cvt->len_mult = ((cvt->len_mult * dst_channels) + (src_channels - 1)) / src_channels;
+            const double mult = ((double)dst_channels / (double)src_channels);
+            cvt->len_mult *= (int)SDL_ceil(mult);
+            cvt->len_ratio *= mult;
+        } else {
+            const double divisor = ((double)src_channels / (double)dst_channels);
+            cvt->len_ratio /= divisor;
         }
 
         if (src_rate < dst_rate) {