shithub: choc

Download patch

ref: a5b01f3b6a0c3ec22d9b23b60923e9d2d73a54b5
parent: 5e7b3e17170671f488191f3ad60eba916cdafa05
author: Michael Day <[email protected]>
date: Sun Jun 26 18:03:31 EDT 2022

Check for functioning FluidSynth before using

Check that FluidSynth shows up in SDL Mixer's list of valid decoders.
This ensures that FluidSynth is actually available and the soundfont
file is good.

--- a/src/i_sdlmusic.c
+++ b/src/i_sdlmusic.c
@@ -186,6 +186,7 @@
 static boolean I_SDL_InitMusic(void)
 {
     boolean fluidsynth_sf_is_set = false;
+    boolean fluidsynth_requested;
 
     // If SDL_mixer is not initialized, we have to initialize it
     // and have the responsibility to shut it down later on.
@@ -215,6 +216,28 @@
         }
     }
 
+    // When using FluidSynth, proceed to set the soundfont path via
+    // Mix_SetSoundFonts if necessary. We need to do this before calling
+    // Mix_Init() in order for FluidSynth to be registered as a valid decoder
+    // in the Mix_GetMusicDecoder() list.
+
+    fluidsynth_requested =
+        ((strlen(fluidsynth_sf_path) > 0) && (strlen(timidity_cfg_path) == 0));
+
+    if (fluidsynth_requested)
+    {
+        if (M_FileExists(fluidsynth_sf_path))
+        {
+            Mix_SetSoundFonts(fluidsynth_sf_path);
+        }
+        else
+        {
+            fprintf(stderr,
+                    "I_SDL_InitMusic: Can't find FluidSynth soundfont.\n");
+            fluidsynth_requested = false;
+        }
+    }
+
     // Initialize SDL_Mixer for MIDI music playback
     Mix_Init(MIX_INIT_MID);
 
@@ -223,24 +246,32 @@
 
     RemoveTimidityConfig();
 
-    // When using FluidSynth, proceed to set the soundfont path via
-    // Mix_SetSoundFonts if necessary.
-
-    if (strlen(fluidsynth_sf_path) > 0 && strlen(timidity_cfg_path) == 0)
+    // Confirm that FluidSynth is actually available.
+    if (fluidsynth_requested)
     {
-        if (M_FileExists(fluidsynth_sf_path))
+        int total;
+
+        total = Mix_GetNumMusicDecoders();
+
+        // If FluidSynth is present and has a valid soundfont, it will be in
+        // the list of available music decoders.
+        for (int i = 0; i < total; ++i)
         {
-            fluidsynth_sf_is_set = true;
+            if (!strcmp(Mix_GetMusicDecoder(i), "FLUIDSYNTH"))
+            {
+                fluidsynth_sf_is_set = true;
+                break;
+            }
         }
+
+        if (fluidsynth_sf_is_set)
+        {
+            printf("I_SDL_InitMusic: Using FluidSynth.\n");
+        }
         else
         {
-            fprintf(stderr, "Can't find Fluidsynth soundfont.\n");
+            fprintf(stderr, "I_SDL_InitMusic: FluidSynth unavailable.\n");
         }
-    }
-
-    if (fluidsynth_sf_is_set)
-    {
-        Mix_SetSoundFonts(fluidsynth_sf_path);
     }
 
     // If snd_musiccmd is set, we need to call Mix_SetMusicCMD to