Permalink
Browse files

midiproc: fix music playback of any kind

Unregister songs when they are stopped and wait for an acknowldgement.

Rationale: When SDL_Mixer uses an external library (e.g. fluidsynth)
for music playback, this library may keep the temporary music lump file
open. This means that it cannot be overridden by the next attempt to
register another song. As a consequence, the same song is played over
and over again.

Instead, unregister a song as soon as it is stopped in midiproc and
let the main process wait until the unregistering is acknowledged.
Then it is the roght time to override the music lump file and play
a new song.

Thanks a lot to @Zodomaniac for his willingness and patience to
tackle this issue!
  • Loading branch information...
fabiangreffrath committed Dec 5, 2017
1 parent 67ec69a commit a8fd660d4046efe5d57db64ac0815799ac28c393
Showing with 30 additions and 5 deletions.
  1. +14 −0 midiproc/main.c
  2. +1 −0 midiproc/proto.h
  3. +13 −3 src/i_midipipe.c
  4. +2 −2 src/i_sdlmusic.c
View
@@ -99,6 +99,7 @@ static void UnregisterSong()
}
Mix_FreeMusic(music);
music = NULL;
}
//
@@ -155,6 +156,7 @@ static void PlaySong(int loops)
static void StopSong()
{
Mix_HaltMusic();
UnregisterSong();
}
//=============================================================================
@@ -220,8 +222,20 @@ boolean MidiPipe_PlaySong(buffer_reader_t *reader)
boolean MidiPipe_StopSong()
{
CHAR buffer[2];
DWORD bytes_written;
StopSong();
if (!WriteInt16(buffer, sizeof(buffer),
MIDIPIPE_PACKET_TYPE_STOP_SONG_ACK))
{
return false;
}
WriteFile(midi_process_out, buffer, sizeof(buffer),
&bytes_written, NULL);
return true;
}
View
@@ -24,6 +24,7 @@ typedef enum {
MIDIPIPE_PACKET_TYPE_SET_VOLUME,
MIDIPIPE_PACKET_TYPE_PLAY_SONG,
MIDIPIPE_PACKET_TYPE_STOP_SONG,
MIDIPIPE_PACKET_TYPE_STOP_SONG_ACK,
MIDIPIPE_PACKET_TYPE_SHUTDOWN
} net_midipipe_packet_type_t;
View
@@ -236,7 +236,6 @@ boolean I_MidiPipe_RegisterSong(char *filename)
NET_WriteString(packet, filename);
ok = WritePipe(packet);
NET_FreePacket(packet);
free(filename);
midi_server_registered = false;
@@ -328,14 +327,25 @@ void I_MidiPipe_StopSong()
ok = WritePipe(packet);
NET_FreePacket(packet);
midi_server_registered = false;
if (!ok)
{
DEBUGOUT("I_MidiPipe_StopSong failed");
return;
}
packet = NET_NewPacket(2);
NET_WriteInt16(packet, MIDIPIPE_PACKET_TYPE_STOP_SONG_ACK);
ok = ExpectPipe(packet);
NET_FreePacket(packet);
if (!ok)
{
DEBUGOUT("I_MidiPipe_StopSong ack failed");
return;
}
midi_server_registered = false;
DEBUGOUT("I_MidiPipe_StopSong succeeded");
}
View
@@ -1261,10 +1261,10 @@ static void *I_SDL_RegisterSong(void *data, int len)
{
remove(filename);
}
free(filename);
}
free(filename);
return music;
}

0 comments on commit a8fd660

Please sign in to comment.