Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race condition around fluid_player_get_total_ticks #648

Closed
sykhro opened this issue May 9, 2020 · 4 comments
Closed

Race condition around fluid_player_get_total_ticks #648

sykhro opened this issue May 9, 2020 · 4 comments
Labels

Comments

@sykhro
Copy link
Contributor

sykhro commented May 9, 2020

FluidSynth version

2.1.2

Describe the bug

Calling fluid_player_get_total_ticks shortly after fluid_player_add_mem makes the function return 0 ticks. Sometimes the returned values can even be incorrect (lower than the actual maximum number of ticks).

Expected behavior

fluid_player_get_total_ticks returns the actual total number of ticks.

Steps to reproduce

  1. Load a file from memory using fluid_player_add_mem
  2. Call fluid_player_get_total_ticks

Additional context

I'm implementing seek controls for a MIDI player and I figured querying fluid_player_get_total_ticks would be simpler than computing it from the file. It can take up to ~10 calls to get the function to return a meaningful value.
Probably not relevant information but the synth is using a custom sfloader to load SF2 files from memory.

@sykhro sykhro added the bug label May 9, 2020
@derselbst
Copy link
Member

fluid_player_add_mem enqueues a file. fluid_player_get_total_ticks() however, returns the ticks of the file which is currently played back. There could be any number of files enqueued in the player for which the playback hasn't started yet.

You could call fluid_player_get_total_ticks() from synth context by registering you own callback function via fluid_player_set_playback_callback().

@sykhro
Copy link
Contributor Author

sykhro commented May 10, 2020

Let me be a bit more precise on the calling sequence:

  1. The player is stopped
  2. File is added to queue with fluid_player_add_mem()
  3. fluid_player_play() is called
  4. Player status is queried with fluid_player_get_status() and signaled to the GUI (at this point, FLUID_PLAYER_PLAYING)
  5. Since the synth is playing, seek controls get enabled. This is where fluid_player_get_total_ticks() is called

So, to my understanding, the player can be in the playing state when no file has yet been loaded?

@derselbst
Copy link
Member

derselbst commented May 10, 2020

Point 3 is redundant. fluid_player_get_status only tells you what you already know, since you've called play() before.

Point 4 is not clear to me. "Synth is playing", how, when, where? Are you using fluidsynth's audio driver or are you rendering from the synth directly yourself?

So, to my understanding, the player can be in the playing state when no file has yet been loaded?

Yes. But the answer to that question is not really enlightening, I think. The point is, fluid_player_get_total_ticks will (should) give you the total ticks

  • after the first time the synth has called back the player, and
  • if at that time a file was already enqueued.

If you call fluid_player_get_total_ticks from outside the synth context, you won't know when this happens. And that's why, try fluid_player_get_total_ticks from within fluid_player_set_playback_callback.

@sykhro
Copy link
Contributor Author

sykhro commented May 10, 2020

"Synth is playing", how, when, where? Are you using fluidsynth's audio driver or are you rendering from the synth directly yourself?

I'm using one of the fluidsynth drivers, but I didn't know that the player could be in a playing state before it has actually loaded

If you call fluid_player_get_total_ticks from outside the synth context, you won't know when this happens.

I see, I didn't think this would be the case because the only parameter is the player (I basically assumed that if the status was FLUID_PLAYER_PLAYING then a file was surely loaded). I will be using fluid_player_set_playback_callback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants