foxygit / doom Log in
commit a4ff8b18d8c02b566ec1e203f983fc111408a752
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Mon Mar 28 21:36:00 2011 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Mon Mar 28 21:36:00 2011 +0000

    Fix OPL MIDI playback when using an empty .mus / .mid file (thanks
    Alexandre Xavier).

    Subversion-branch: /trunk/chocolate-doom
    Subversion-revision: 2313
---
 NEWS             | 2 ++
 src/i_oplmusic.c | 9 +++++++--
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index b76917f2..c0163f8c 100644
--- a/NEWS
+++ b/NEWS
@@ -41,6 +41,8 @@
        Xavier).
      * Weapon cycling from the shotgun to the chaingun in Doom 1 now
        works properly (thanks Alexandre Xavier).
+     * MIDI playback that locked up when using an empty MUS / MIDI
+       file (thanks Alexandre Xavier).

     libtextscreen:
      * It is now possible to type a '+' in input boxes (thanks
diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c
index e8d65d6d..97ba0783 100644
--- a/src/i_oplmusic.c
+++ b/src/i_oplmusic.c
@@ -1080,7 +1080,7 @@ static void ScheduleTrack(opl_track_data_t *track);

 // Restart a song from the beginning.

-static void RestartSong(void)
+static void RestartSong(void *unused)
 {
     unsigned int i;

@@ -1118,10 +1118,15 @@ static void TrackTimerCallback(void *arg)
         --running_tracks;

         // When all tracks have finished, restart the song.
+        // Don't restart the song immediately, but wait for 5ms
+        // before triggering a restart.  Otherwise it is possible
+        // to construct an empty MIDI file that causes the game
+        // to lock up in an infinite loop. (5ms should be short
+        // enough not to be noticeable by the listener).

         if (running_tracks <= 0 && song_looping)
         {
-            RestartSong();
+            OPL_SetCallback(5, RestartSong, NULL);
         }

         return;