foxygit / doom Log in
commit 1659e586b4ded9f6bfaaf6ee246e4888725c7945
Author:     Alex Mayfield <alexmax2742@gmail.com>
AuthorDate: Mon Feb 20 20:28:08 2017 -0500
Commit:     Alex Mayfield <alexmax2742@gmail.com>
CommitDate: Mon Feb 20 20:28:08 2017 -0500

    Do not start MIDI server when Timidity is in use

    Also, some general code cleanup concering using_midiproc (now
    midi_server_registered), do all manipulation of the global inside
    i_midipipe.c and only test it from outside.
---
 src/i_midipipe.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 src/i_midipipe.h |  3 +++
 src/i_sdlmusic.c | 24 ++++++------------------
 3 files changed, 60 insertions(+), 23 deletions(-)

diff --git a/src/i_midipipe.c b/src/i_midipipe.c
index 6390d93e..20f59428 100644
--- a/src/i_midipipe.c
+++ b/src/i_midipipe.c
@@ -24,6 +24,7 @@
 #include "i_midipipe.h"

 #include "config.h"
+#include "i_sound.h"
 #include "i_timer.h"
 #include "m_misc.h"
 #include "net_packet.h"
@@ -34,6 +35,19 @@
 #define DEBUGOUT(s)
 #endif

+//=============================================================================
+//
+// Public Data
+//
+
+// True if the midi proces was initialized at least once and has not been
+// explicitly shut down.  This remains true if the server is momentarily
+// unreachable.
+boolean midi_server_initialized;
+
+// True if the current track is being handled via the MIDI server.
+boolean midi_server_registered;
+
 //=============================================================================
 //
 // Data
@@ -46,13 +60,34 @@ static HANDLE  midi_process_in_writer;
 static HANDLE  midi_process_out_reader; // Output stream for midi process.
 static HANDLE  midi_process_out_writer;

-static boolean server_init = false; // if true, server was started
-
 //=============================================================================
 //
 // Private functions
 //

+//
+// UsingNativeMidi
+//
+// Enumerate all music decoders and return true if NATIVEMIDI is one of them.
+//
+// If this is the case, using the MIDI server is probably necessary.  If not,
+// we're likely using Timidity and thus don't need to start the server.
+//
+static boolean UsingNativeMidi()
+{
+    int decoders = Mix_GetNumMusicDecoders();
+
+    for (int i = 0;i < decoders;i++)
+    {
+        if (strcmp(Mix_GetMusicDecoder(i), "NATIVEMIDI") == 0)
+        {
+            return true;
+        }
+    }
+
+    return false;
+}
+
 //
 // WritePipe
 //
@@ -171,6 +206,8 @@ boolean I_MidiPipe_RegisterSong(const char *filename)
         return false;
     }

+    midi_server_registered = true;
+
     DEBUGOUT("I_MidiPipe_RegisterSong succeeded");
     return true;
 }
@@ -240,6 +277,8 @@ void I_MidiPipe_StopSong()
     ok = WritePipe(packet);
     NET_FreePacket(packet);

+    midi_server_registered = false;
+
     if (!ok)
     {
         DEBUGOUT("I_MidiPipe_StopSong failed");
@@ -264,7 +303,7 @@ void I_MidiPipe_ShutdownServer()
     ok = WritePipe(packet);
     NET_FreePacket(packet);

-    server_init = false;
+    midi_server_initialized = false;

     if (!ok)
     {
@@ -288,7 +327,14 @@ void I_MidiPipe_ShutdownServer()
 boolean I_MidiPipe_InitServer()
 {
     struct stat sbuf;
-    char filename[MAX_PATH+1];
+    char filename[MAX_PATH + 1];
+
+    if (!UsingNativeMidi() || strlen(snd_musiccmd) > 0)
+    {
+        // If we're not using native MIDI, or if we're playing music through
+        // an exteranl program, we don't need to start the server.
+        return false;
+    }

     memset(filename, 0, sizeof(filename));
     size_t filename_len = GetModuleFileName(NULL, filename, MAX_PATH);
@@ -361,7 +407,7 @@ boolean I_MidiPipe_InitServer()
     if (ok)
     {
         DEBUGOUT("midiproc started");
-        server_init = true;
+        midi_server_initialized = true;
     }
     else
     {
diff --git a/src/i_midipipe.h b/src/i_midipipe.h
index 9bb3db86..f53852be 100644
--- a/src/i_midipipe.h
+++ b/src/i_midipipe.h
@@ -25,6 +25,9 @@

 #include "doomtype.h"

+extern boolean midi_server_initialized;
+extern boolean midi_server_registered;
+
 boolean I_MidiPipe_RegisterSong(const char *filename);
 void I_MidiPipe_SetVolume(int vol);
 void I_MidiPipe_PlaySong(int loops);
diff --git a/src/i_sdlmusic.c b/src/i_sdlmusic.c
index 5c5d6492..3f25a631 100644
--- a/src/i_sdlmusic.c
+++ b/src/i_sdlmusic.c
@@ -133,9 +133,6 @@ static Mix_Music *current_track_music = NULL;
 // If true, the currently playing track is being played on loop.
 static boolean current_track_loop;

-// If true, the current track is being handled via midiproc.
-static boolean using_midiproc;
-
 // Given a time string (for LOOP_START/LOOP_END), parse it and return
 // the time (in # samples since start of track) it represents.
 static unsigned int ParseVorbisTime(unsigned int samplerate_hz, char *value)
@@ -1033,7 +1030,7 @@ static void I_SDL_PlaySong(void *handle, boolean looping)
         return;
     }

-    if (handle == NULL && !using_midiproc)
+    if (handle == NULL && !midi_server_registered)
     {
         return;
     }
@@ -1061,7 +1058,7 @@ static void I_SDL_PlaySong(void *handle, boolean looping)
     }

 #if defined(_WIN32)
-    if (using_midiproc)
+    if (midi_server_registered)
     {
         I_MidiPipe_PlaySong(loops);
     }
@@ -1106,10 +1103,9 @@ static void I_SDL_StopSong(void)
     }

 #if defined(_WIN32)
-    if (using_midiproc)
+    if (midi_server_registered)
     {
         I_MidiPipe_StopSong();
-        using_midiproc = false;
     }
     else
     {
@@ -1201,9 +1197,6 @@ static void *I_SDL_RegisterSong(void *data, int len)
         }
         else
         {
-            // [AM] Substitute music never uses midiproc.
-            using_midiproc = false;
-
             // Read loop point metadata from the file so that we know where
             // to loop the music.
             playing_substitute = true;
@@ -1234,15 +1227,11 @@ static void *I_SDL_RegisterSong(void *data, int len)

 #if defined(_WIN32)
     // [AM] If we do not have an external music command defined, play
-    //      music with midiproc.exe.
-    if (strlen(snd_musiccmd) == 0)
+    //      music with the MIDI server.
+    if (midi_server_initialized)
     {
         music = NULL;
-        if (I_MidiPipe_RegisterSong(filename))
-        {
-            using_midiproc = true;
-        }
-        else
+        if (!I_MidiPipe_RegisterSong(filename))
         {
             fprintf(stderr, "Error loading midi: %s\n",
                 "Could not communicate with midiproc.");
@@ -1250,7 +1239,6 @@ static void *I_SDL_RegisterSong(void *data, int len)
     }
     else
     {
-        using_midiproc = false;
         music = Mix_LoadMUS(filename);
         if (music == NULL)
         {