Fix a race condition while fluid_player is seeking

Fixes #634
This commit is contained in:
derselbst 2020-04-19 12:08:48 +02:00
parent f15b8e5447
commit 69cfa781eb
2 changed files with 14 additions and 10 deletions

View File

@ -1558,11 +1558,12 @@ fluid_track_send_events(fluid_track_t *track,
unsigned int ticks)
{
fluid_midi_event_t *event;
int seeking = player->seek_ticks >= 0;
int seek_ticks = fluid_atomic_int_get(&player->seek_ticks);
int seeking = seek_ticks >= 0;
if(seeking)
{
ticks = player->seek_ticks; /* update target ticks */
ticks = seek_ticks; /* update target ticks */
if(track->ticks > ticks)
{
@ -1670,7 +1671,7 @@ new_fluid_player(fluid_synth_t *synth)
player->deltatime = 4.0;
player->cur_msec = 0;
player->cur_ticks = 0;
player->seek_ticks = -1;
fluid_atomic_int_set(&player->seek_ticks, -1);
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");
@ -2061,6 +2062,8 @@ fluid_player_callback(void *data, unsigned int msec)
}
do
{
int seek_ticks;
if(loadnextfile)
{
loadnextfile = 0;
@ -2077,7 +2080,8 @@ fluid_player_callback(void *data, unsigned int msec)
+ (int)((double)(player->cur_msec - player->start_msec)
/ player->deltatime + 0.5)); /* 0.5 to average overall error when casting */
if(player->seek_ticks >= 0)
seek_ticks = fluid_atomic_int_get(&player->seek_ticks);
if(seek_ticks >= 0)
{
fluid_synth_all_sounds_off(synth, -1); /* avoid hanging notes */
}
@ -2091,13 +2095,13 @@ fluid_player_callback(void *data, unsigned int msec)
}
}
if(player->seek_ticks >= 0)
if(seek_ticks >= 0)
{
player->start_ticks = player->seek_ticks; /* tick position of last tempo value (which is now) */
player->cur_ticks = player->seek_ticks;
player->start_ticks = seek_ticks; /* tick position of last tempo value (which is now) */
player->cur_ticks = seek_ticks;
player->begin_msec = msec; /* only used to calculate the duration of playing */
player->start_msec = msec; /* should be the (synth)-time of the last tempo change */
player->seek_ticks = -1; /* clear seek_ticks */
fluid_atomic_int_set(&player->seek_ticks, -1); /* clear seek_ticks */
}
if(status == FLUID_PLAYER_DONE)
@ -2181,7 +2185,7 @@ int fluid_player_seek(fluid_player_t *player, int ticks)
return FLUID_FAILED;
}
player->seek_ticks = ticks;
fluid_atomic_int_set(&player->seek_ticks, ticks);
return FLUID_OK;
}

View File

@ -286,7 +286,7 @@ struct _fluid_player_t
char send_program_change; /* should we ignore the program changes? */
char use_system_timer; /* if zero, use sample timers, otherwise use system clock timer */
char reset_synth_between_songs; /* 1 if system reset should be sent to the synth between songs. */
int seek_ticks; /* new position in tempo ticks (midi ticks) for seeking */
fluid_atomic_int_t seek_ticks; /* new position in tempo ticks (midi ticks) for seeking */
int start_ticks; /* the number of tempo ticks passed at the last tempo change */
int cur_ticks; /* the number of tempo ticks passed */
int begin_msec; /* the time (msec) of the beginning of the file */