r/JavaFX • • Dec 21 '23

Help Faster response time on button presses?

I'm trying to make a little piano demo on JavaFX, but the even handlers are too slow, and there's a significant delay between the button presses and the notes playing. How could I make it more responsive?

Edit: Okay, it's not just a button thing. :(

2 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/BWC_semaJ Dec 21 '23

I'll be honest. I don't fully understand your first paragraph. I am not familiar with javax.sound.midi and MIDI files in general.

Mouse press -> goes to event dispatcher -> tells listeners who are listening for event -> inside the listener you play the sound not on the Application Thread

If you play the sound directly on the Application Thread, as in your Application Thread is handling making the noise, your GUI will "freeze", then once the sound is played will return to updating itself with relevant information...

I don't know if you mean this? You say there is no delay for audio to play but then say there is a delay for your handler which doesn't make much sense because handler would be the one to tell audio to play?

If you could provide a small example of what you currently have that would be extremely helpful. Best if you could provide a git repository that we could fork then clone, make things much easier to help you.

I use AudioClips for all my button sounds and they are immediately played instantly after each button press. I can press buttons multiple times and the same audio plays repeatedly. AudioClip does all the dirty work underneath to not worry about having it freeze the GUI. With library you are using maybe it is so low level that you have to worry what Thread the sound is playing on, but honestly I would be a bit surprised if that was the case.

Just glancing at the library, I would have to spend sometime learning what's going on in order to get good idea what's happening, so at the moment I'm just not.

https://openjfx.io/javadoc/21/javafx.media/javafx/scene/media/AudioClip.html

2

u/xyloPhoton Dec 21 '23

Yes, I'm sorry, the problem is with my midi synthesizer. I'm specifically trying to get .sf2 soundbanks to work with low latency. It's not a JavaFX issue.

I was sure it was a problem with event handling. I'm sorry. I don't think you can help with this; you said you don't know the library.

I'm thankful for the suggestion, but AudioClip can't load sf2 soundbanks.