summaryrefslogtreecommitdiff
path: root/libs/fluidsynth/src/fluid_midi.c
diff options
context:
space:
mode:
Diffstat (limited to 'libs/fluidsynth/src/fluid_midi.c')
-rw-r--r--libs/fluidsynth/src/fluid_midi.c114
1 files changed, 50 insertions, 64 deletions
diff --git a/libs/fluidsynth/src/fluid_midi.c b/libs/fluidsynth/src/fluid_midi.c
index 5b2efe0b9b..c92fb2fefa 100644
--- a/libs/fluidsynth/src/fluid_midi.c
+++ b/libs/fluidsynth/src/fluid_midi.c
@@ -48,7 +48,7 @@ static fluid_midi_event_t *fluid_track_next_event(fluid_track_t *track);
static int fluid_track_get_duration(fluid_track_t *track);
static int fluid_track_reset(fluid_track_t *track);
-static int fluid_track_send_events(fluid_track_t *track,
+static void fluid_track_send_events(fluid_track_t *track,
fluid_synth_t *synth,
fluid_player_t *player,
unsigned int ticks);
@@ -1553,13 +1553,12 @@ fluid_track_reset(fluid_track_t *track)
/*
* fluid_track_send_events
*/
-int
+void
fluid_track_send_events(fluid_track_t *track,
fluid_synth_t *synth,
fluid_player_t *player,
unsigned int ticks)
{
- int status = FLUID_OK;
fluid_midi_event_t *event;
int seeking = player->seek_ticks >= 0;
@@ -1580,7 +1579,7 @@ fluid_track_send_events(fluid_track_t *track,
if(event == NULL)
{
- return status;
+ return;
}
/* printf("track=%02d\tticks=%05u\ttrack=%05u\tdtime=%05u\tnext=%05u\n", */
@@ -1592,7 +1591,7 @@ fluid_track_send_events(fluid_track_t *track,
if(track->ticks + event->dtime > ticks)
{
- return status;
+ return;
}
track->ticks += event->dtime;
@@ -1620,8 +1619,6 @@ fluid_track_send_events(fluid_track_t *track,
fluid_track_next_event(track);
}
-
- return status;
}
/******************************************************
@@ -1679,6 +1676,26 @@ new_fluid_player(fluid_synth_t *synth)
fluid_player_set_playback_callback(player, fluid_synth_handle_midi_event, synth);
player->use_system_timer = fluid_settings_str_equal(synth->settings,
"player.timing-source", "system");
+ if(player->use_system_timer)
+ {
+ player->system_timer = new_fluid_timer((int) player->deltatime,
+ fluid_player_callback, player, TRUE, FALSE, TRUE);
+
+ if(player->system_timer == NULL)
+ {
+ goto err;
+ }
+ }
+ else
+ {
+ player->sample_timer = new_fluid_sample_timer(player->synth,
+ fluid_player_callback, player);
+
+ if(player->sample_timer == NULL)
+ {
+ goto err;
+ }
+ }
fluid_settings_getint(synth->settings, "player.reset-synth", &i);
fluid_player_handle_reset_synth(player, NULL, i);
@@ -1687,11 +1704,16 @@ new_fluid_player(fluid_synth_t *synth)
fluid_player_handle_reset_synth, player);
return player;
+
+err:
+ delete_fluid_player(player);
+ return NULL;
}
/**
* Delete a MIDI player instance.
* @param player MIDI player instance
+ * @warning Do not call while the \p synth renders audio, i.e. an audio driver is running or any other synthesizer thread calls fluid_synth_process() or fluid_synth_nwrite_float() or fluid_synth_write_*() !
*/
void
delete_fluid_player(fluid_player_t *player)
@@ -1704,6 +1726,9 @@ delete_fluid_player(fluid_player_t *player)
fluid_player_stop(player);
fluid_player_reset(player);
+ delete_fluid_timer(player->system_timer);
+ delete_fluid_sample_timer(player->synth, player->sample_timer);
+
while(player->playlist != NULL)
{
q = player->playlist->next;
@@ -2031,6 +2056,11 @@ fluid_player_callback(void *data, unsigned int msec)
loadnextfile = player->currentfile == NULL ? 1 : 0;
+ if(player->status == FLUID_PLAYER_DONE)
+ {
+ fluid_synth_all_notes_off(synth, -1);
+ return 1;
+ }
do
{
if(loadnextfile)
@@ -2059,12 +2089,7 @@ fluid_player_callback(void *data, unsigned int msec)
if(!fluid_track_eot(player->track[i]))
{
status = FLUID_PLAYER_PLAYING;
-
- if(fluid_track_send_events(player->track[i], synth, player,
- player->cur_ticks) != FLUID_OK)
- {
- /* */
- }
+ fluid_track_send_events(player->track[i], synth, player, player->cur_ticks);
}
}
@@ -2099,63 +2124,33 @@ fluid_player_callback(void *data, unsigned int msec)
int
fluid_player_play(fluid_player_t *player)
{
- if(player->status == FLUID_PLAYER_PLAYING)
+ if(player->status == FLUID_PLAYER_PLAYING ||
+ player->playlist == NULL)
{
return FLUID_OK;
}
- if(player->playlist == NULL)
+ if(!player->use_system_timer)
{
- return FLUID_OK;
+ fluid_sample_timer_reset(player->synth, player->sample_timer);
}
player->status = FLUID_PLAYER_PLAYING;
- if(player->use_system_timer)
- {
- player->system_timer = new_fluid_timer((int) player->deltatime,
- fluid_player_callback, (void *) player, TRUE, FALSE, TRUE);
-
- if(player->system_timer == NULL)
- {
- return FLUID_FAILED;
- }
- }
- else
- {
- player->sample_timer = new_fluid_sample_timer(player->synth,
- fluid_player_callback, (void *) player);
-
- if(player->sample_timer == NULL)
- {
- return FLUID_FAILED;
- }
- }
-
return FLUID_OK;
}
-
/**
- * Stops a MIDI player.
+ * Pauses the MIDI playback.
+ *
+ * It will not rewind to the beginning of the file, use fluid_player_seek() for this purpose.
* @param player MIDI player instance
* @return Always returns #FLUID_OK
*/
int
fluid_player_stop(fluid_player_t *player)
{
- if(player->system_timer != NULL)
- {
- delete_fluid_timer(player->system_timer);
- }
-
- if(player->sample_timer != NULL)
- {
- delete_fluid_sample_timer(player->synth, player->sample_timer);
- }
-
player->status = FLUID_PLAYER_DONE;
- player->sample_timer = NULL;
- player->system_timer = NULL;
+ fluid_player_seek(player, fluid_player_get_current_tick(player));
return FLUID_OK;
}
@@ -2241,26 +2236,17 @@ int fluid_player_set_bpm(fluid_player_t *player, int bpm)
}
/**
- * Wait for a MIDI player to terminate (when done playing).
+ * Wait for a MIDI player until the playback has been stopped.
* @param player MIDI player instance
- * @return #FLUID_OK on success, #FLUID_FAILED otherwise
+ * @return Always #FLUID_OK
*/
int
fluid_player_join(fluid_player_t *player)
{
- if(player->system_timer)
- {
- return fluid_timer_join(player->system_timer);
- }
- else if(player->sample_timer)
+ while(player->status != FLUID_PLAYER_DONE)
{
- /* Busy-wait loop, since there's no thread to wait for... */
- while(player->status != FLUID_PLAYER_DONE)
- {
- fluid_msleep(10);
- }
+ fluid_msleep(10);
}
-
return FLUID_OK;
}