shithub: choc

Download patch

ref: 8089e905e3ce33bfbe8377d71bb6d75f5868edbd
parent: 06c73f7a43b0b569c63fb1153e9f8da25f3ddfb7
author: Simon Howard <fraggle@gmail.com>
date: Thu Oct 1 15:09:37 EDT 2009

Remove temporary MIDI file after loading MIDI data. Stop all playing OPL
voices when music is shut down.

Subversion-branch: /branches/opl-branch
Subversion-revision: 1704

--- a/src/i_oplmusic.c
+++ b/src/i_oplmusic.c
@@ -322,7 +322,7 @@
 // Track data for playing tracks:
 
 static opl_track_data_t *tracks;
-static unsigned int num_tracks;
+static unsigned int num_tracks = 0;
 static unsigned int running_tracks = 0;
 static boolean song_looping;
 
@@ -571,47 +571,6 @@
     }
 }
 
-// Shutdown music
-
-static void I_OPL_ShutdownMusic(void)
-{
-    if (music_initialized)
-    {
-        OPL_Shutdown();
-
-        // Release GENMIDI lump
-
-        W_ReleaseLumpName("GENMIDI");
-
-        music_initialized = false;
-    }
-}
-
-// Initialize music subsystem
-
-static boolean I_OPL_InitMusic(void)
-{
-    if (!OPL_Init(opl_io_port))
-    {
-        printf("Dude.  The Adlib isn't responding.\n");
-        return false;
-    }
-
-    // Load instruments from GENMIDI lump:
-
-    if (!LoadInstrumentTable())
-    {
-        OPL_Shutdown();
-        return false;
-    }
-
-    InitVoices();
-
-    music_initialized = true;
-
-    return true;
-}
-
 // Set music volume (0 - 127)
 
 static void I_OPL_SetMusicVolume(int volume)
@@ -1252,6 +1211,8 @@
         return;
     }
 
+    OPL_Lock();
+
     // Stop all playback.
 
     OPL_ClearCallbacks();
@@ -1275,6 +1236,11 @@
     }
 
     free(tracks);
+
+    tracks = NULL;
+    num_tracks = 0;
+
+    OPL_Unlock();
 }
 
 static void I_OPL_UnRegisterSong(void *handle)
@@ -1358,7 +1324,7 @@
 
     // remove file now
 
-//    remove(filename);
+    remove(filename);
 
     Z_Free(filename);
 
@@ -1366,6 +1332,7 @@
 }
 
 // Is the song playing?
+
 static boolean I_OPL_MusicIsPlaying(void)
 {
     if (!music_initialized)
@@ -1373,7 +1340,54 @@
         return false;
     }
 
-    return false;
+    return num_tracks > 0;
+}
+
+// Shutdown music
+
+static void I_OPL_ShutdownMusic(void)
+{
+    if (music_initialized)
+    {
+        // Stop currently-playing track, if there is one:
+
+        I_OPL_StopSong();
+
+        OPL_Shutdown();
+
+        // Release GENMIDI lump
+
+        W_ReleaseLumpName("GENMIDI");
+
+        music_initialized = false;
+    }
+}
+
+// Initialize music subsystem
+
+static boolean I_OPL_InitMusic(void)
+{
+    if (!OPL_Init(opl_io_port))
+    {
+        printf("Dude.  The Adlib isn't responding.\n");
+        return false;
+    }
+
+    // Load instruments from GENMIDI lump:
+
+    if (!LoadInstrumentTable())
+    {
+        OPL_Shutdown();
+        return false;
+    }
+
+    InitVoices();
+
+    tracks = NULL;
+    num_tracks = 0;
+    music_initialized = true;
+
+    return true;
 }
 
 static snddevice_t music_opl_devices[] =