foxygit / doom Log in
commit 241d056f6018b6c1700affdfae4e87ccf8a88be0
Author:     Fabian Greffrath <fabian@greffrath.com>
AuthorDate: Mon Dec 11 14:46:09 2017 +0100
Commit:     Fabian Greffrath <fabian@greffrath.com>
CommitDate: Mon Dec 11 14:46:09 2017 +0100

    midiproc: some further improvements

    Recent versions of SDL_Mixer allow for rendering MIDI songs using
    the fluidsynth backend and recent versions of fluidsynth allow for
    using soundfonts in SF3 format (which contain OGG compressed samples).

    The midiproc process currently assumes that it is possible to remove
    the temporary music file immediately after Mix_LoadMUS() has been
    called, which is true if the music is rendered using Windows's limited
    intrnal MIDI playback. However, when using an advanced backend like
    fluidsynth, this file maybe locked by this library. Furthermore,
    midiproc currently assumes that it is possible to override the
    temporary music file as soon as Mix_HaltMusic() has been called which
    is again not true if fluidsynth is used. In this case, this is only
    possible after Mix_FreeMusic() has been called (and returned).
    Additionally, we increase the time-out value after which we give up
    waiting for the midiproc process to send an acknowledgement to 1s to
    give it some time to load the fluidsynth library and a soundfont
    (although 1s will still not be enough for soundfonts in SF3 format
    which reportedly load up to the order of 10s!).
---
 midiproc/main.c  |  5 +++++
 src/i_midipipe.c |  2 +-
 src/i_sdlmusic.c | 16 ++++++++--------
 3 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/midiproc/main.c b/midiproc/main.c
index 473eb4de..19d2c047 100644
--- a/midiproc/main.c
+++ b/midiproc/main.c
@@ -99,6 +99,7 @@ static void UnregisterSong()
     }

     Mix_FreeMusic(music);
+    music = NULL;
 }

 //
@@ -121,6 +122,9 @@ static boolean RegisterSong(const char *filename)
     UnregisterSong();
     music = Mix_LoadMUS(filename);

+    // Remove the temporary MIDI file
+    remove(filename);
+
     if (music == NULL)
     {
         return false;
@@ -218,6 +222,7 @@ boolean MidiPipe_PlaySong(buffer_reader_t *reader)
 boolean MidiPipe_StopSong()
 {
     StopSong();
+    UnregisterSong();

     return true;
 }
diff --git a/src/i_midipipe.c b/src/i_midipipe.c
index a318e999..330377d9 100644
--- a/src/i_midipipe.c
+++ b/src/i_midipipe.c
@@ -58,7 +58,7 @@ boolean midi_server_registered = false;
 // Data
 //

-#define MIDIPIPE_MAX_WAIT 500 // Max amount of ms to wait for expected data.
+#define MIDIPIPE_MAX_WAIT 1000 // Max amount of ms to wait for expected data.

 static HANDLE  midi_process_in_reader;  // Input stream for midi process.
 static HANDLE  midi_process_in_writer;
diff --git a/src/i_sdlmusic.c b/src/i_sdlmusic.c
index 67c01b9d..f4b08452 100644
--- a/src/i_sdlmusic.c
+++ b/src/i_sdlmusic.c
@@ -1242,16 +1242,16 @@ static void *I_SDL_RegisterSong(void *data, int len)
             // Failed to load
             fprintf(stderr, "Error loading midi: %s\n", Mix_GetError());
         }
-    }

-    // Remove the temporary MIDI file; however, when using an external
-    // MIDI program we can't delete the file. Otherwise, the program
-    // won't find the file to play. This means we leave a mess on
-    // disk :(
+        // Remove the temporary MIDI file; however, when using an external
+        // MIDI program we can't delete the file. Otherwise, the program
+        // won't find the file to play. This means we leave a mess on
+        // disk :(

-    if (strlen(snd_musiccmd) == 0)
-    {
-        remove(filename);
+        if (strlen(snd_musiccmd) == 0)
+        {
+            remove(filename);
+        }
     }

     free(filename);