shithub: choc

Download patch

ref: 1659e586b4ded9f6bfaaf6ee246e4888725c7945
parent: f7baca64df6c2e2f5cb4a7c579c5fc881cf10a98
author: Alex Mayfield <[email protected]>
date: Mon Feb 20 15:28:08 EST 2017

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.

--- 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"
@@ -36,6 +37,19 @@
 
 //=============================================================================
 //
+// 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,8 +60,6 @@
 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
@@ -54,6 +66,29 @@
 //
 
 //
+// 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
 //
 // Writes packet data to the subprocess' standard in.
@@ -171,6 +206,8 @@
         return false;
     }
 
+    midi_server_registered = true;
+
     DEBUGOUT("I_MidiPipe_RegisterSong succeeded");
     return true;
 }
@@ -240,6 +277,8 @@
     ok = WritePipe(packet);
     NET_FreePacket(packet);
 
+    midi_server_registered = false;
+
     if (!ok)
     {
         DEBUGOUT("I_MidiPipe_StopSong failed");
@@ -264,7 +303,7 @@
     ok = WritePipe(packet);
     NET_FreePacket(packet);
 
-    server_init = false;
+    midi_server_initialized = false;
 
     if (!ok)
     {
@@ -288,8 +327,15 @@
 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 @@
     if (ok)
     {
         DEBUGOUT("midiproc started");
-        server_init = true;
+        midi_server_initialized = true;
     }
     else
     {
--- 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);
--- a/src/i_sdlmusic.c
+++ b/src/i_sdlmusic.c
@@ -133,9 +133,6 @@
 // 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 @@
         return;
     }
 
-    if (handle == NULL && !using_midiproc)
+    if (handle == NULL && !midi_server_registered)
     {
         return;
     }
@@ -1061,7 +1058,7 @@
     }
 
 #if defined(_WIN32)
-    if (using_midiproc)
+    if (midi_server_registered)
     {
         I_MidiPipe_PlaySong(loops);
     }
@@ -1106,10 +1103,9 @@
     }
 
 #if defined(_WIN32)
-    if (using_midiproc)
+    if (midi_server_registered)
     {
         I_MidiPipe_StopSong();
-        using_midiproc = false;
     }
     else
     {
@@ -1201,9 +1197,6 @@
         }
         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,16 +1227,12 @@
 
 #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))
+        if (!I_MidiPipe_RegisterSong(filename))
         {
-            using_midiproc = true;
-        }
-        else
-        {
             fprintf(stderr, "Error loading midi: %s\n",
                 "Could not communicate with midiproc.");
         }
@@ -1250,7 +1239,6 @@
     }
     else
     {
-        using_midiproc = false;
         music = Mix_LoadMUS(filename);
         if (music == NULL)
         {