SDL_mixer: timidity: Cleaned out FIXME about sample buffer sizes.

From d82a52ac4dff4a0a4e0380b76a9e8f53aa8cf5da Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Thu, 20 Aug 2026 15:30:40 -0400
Subject: [PATCH] timidity: Cleaned out FIXME about sample buffer sizes.

(Thanks to Ozkan Sezer for the actual fix, which I've basically migrated here
from SDL_sound.)

Fixes #893.
---
 src/decoder_timidity.c  | 13 +++++++------
 src/timidity/timidity.c | 12 ++++++------
 src/timidity/timidity.h |  2 +-
 3 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/src/decoder_timidity.c b/src/decoder_timidity.c
index 4abb71e0..d528ad00 100644
--- a/src/decoder_timidity.c
+++ b/src/decoder_timidity.c
@@ -27,6 +27,8 @@
 
 #include "timidity/timidity.h"
 
+#define SAMPLES_PER_DECODE 256
+
 // Config file should contain any other directory that needs
 //  to be added to the search path. The library adds the path
 //  of the config file to its search path, too.
@@ -38,7 +40,6 @@ static const char *timidity_cfgs[] = { "/etc/timidity.cfg", "/etc/timidity/timid
 
 typedef struct TIMIDITY_TrackData
 {
-    Sint32 samples[4096 * 2];   // !!! FIXME: there's a hardcoded thing about buffer_size in our copy of timidity that needs to be fixed; it's hardcoded to this at the moment.
     MidiSong *song;
     int freq;
 } TIMIDITY_TrackData;
@@ -92,7 +93,7 @@ static bool SDLCALL TIMIDITY_init_audio(SDL_IOStream *io, SDL_AudioSpec *spec, S
     // Use the device's current sample rate, already set in spec->freq
 
     Sint64 song_length_in_frames = -1;
-    MidiSong *song = Timidity_LoadSong(io, spec);
+    MidiSong *song = Timidity_LoadSong(io, spec, SAMPLES_PER_DECODE);
     if (!song) {
         return false;
     }
@@ -117,7 +118,7 @@ static bool SDLCALL TIMIDITY_init_track(void *audio_userdata, SDL_IOStream *io,
         return false;
     }
 
-    tdata->song = Timidity_LoadSong(io, spec);
+    tdata->song = Timidity_LoadSong(io, spec, SAMPLES_PER_DECODE);
     if (!tdata->song) {
         SDL_free(tdata);
         return SDL_SetError("Timidity_LoadSong failed");
@@ -135,13 +136,13 @@ static bool SDLCALL TIMIDITY_init_track(void *audio_userdata, SDL_IOStream *io,
 static bool SDLCALL TIMIDITY_decode(void *track_userdata, SDL_AudioStream *stream)
 {
     TIMIDITY_TrackData *tdata = (TIMIDITY_TrackData *) track_userdata;
-    //Sint32 samples[256];  // !!! FIXME: there's a hardcoded thing about buffer_size in our copy of timidity that needs to be fixed; it's hardcoded at the moment, so we use tdata->samples.
-    const int amount = Timidity_PlaySome(tdata->song, tdata->samples, sizeof (tdata->samples));
+    Sint32 samples[SAMPLES_PER_DECODE * 2/*channels*/];
+    const int amount = Timidity_PlaySome(tdata->song, samples, sizeof (samples));
     if (amount <= 0) {
         return false;  // EOF or error, we're done either way.
     }
 
-    SDL_PutAudioStreamData(stream, tdata->samples, amount);
+    SDL_PutAudioStreamData(stream, samples, amount);
     return true;
 }
 
diff --git a/src/timidity/timidity.c b/src/timidity/timidity.c
index 892b5ff3..dc60d812 100644
--- a/src/timidity/timidity.c
+++ b/src/timidity/timidity.c
@@ -582,7 +582,7 @@ int Timidity_SetSoundfont(const char *file)
   return 0;
 }
 
-static void do_song_load(SDL_IOStream *io, const SDL_AudioSpec *audio, MidiSong **out)
+static void do_song_load(SDL_IOStream *io, const SDL_AudioSpec *audio, MidiSong **out, int samples)
 {
   MidiSong *song;
   int i;
@@ -660,10 +660,10 @@ static void do_song_load(SDL_IOStream *io, const SDL_AudioSpec *audio, MidiSong
     goto fail;
   }
 
-  song->buffer_size = 4096/*audio->samples*/;
-  song->resample_buffer = SDL_malloc(4096/*audio->samples*/ * sizeof(sample_t));
+  song->buffer_size = samples;
+  song->resample_buffer = SDL_malloc(samples * sizeof(sample_t));
   if (!song->resample_buffer) goto fail;
-  song->common_buffer = SDL_malloc(4096/*audio->samples*/ * 2 * sizeof(Sint32));
+  song->common_buffer = SDL_malloc(samples * 2 * sizeof(Sint32));
   if (!song->common_buffer) goto fail;
 
   song->control_ratio = audio->freq / CONTROLS_PER_SECOND;
@@ -702,10 +702,10 @@ fail: Timidity_FreeSong(song);
   }
 }
 
-MidiSong *Timidity_LoadSong(SDL_IOStream *io, const SDL_AudioSpec *audio)
+MidiSong *Timidity_LoadSong(SDL_IOStream *io, const SDL_AudioSpec *audio, int samples)
 {
   MidiSong *song;
-  do_song_load(io, audio, &song);
+  do_song_load(io, audio, &song, samples);
   return song;
 }
 
diff --git a/src/timidity/timidity.h b/src/timidity/timidity.h
index 1f416952..4ca371ba 100644
--- a/src/timidity/timidity.h
+++ b/src/timidity/timidity.h
@@ -164,7 +164,7 @@ extern int Timidity_Init_NoConfig(void);
 extern int Timidity_SetSoundfont(const char *sf2_file);
 extern void Timidity_SetVolume(MidiSong *song, int volume);
 extern int Timidity_PlaySome(MidiSong *song, void *stream, Sint32 len);
-extern MidiSong *Timidity_LoadSong(SDL_IOStream *io, const SDL_AudioSpec *audio);
+extern MidiSong *Timidity_LoadSong(SDL_IOStream *io, const SDL_AudioSpec *audio, int samples);
 extern void Timidity_Start(MidiSong *song);
 extern void Timidity_Seek(MidiSong *song, Uint32 ms);
 extern Uint32 Timidity_GetSongLength(MidiSong *song); /* returns millseconds */