Search code examples
javamidijavasoundjavax.sound.midi

How can I be notified when a certain tick is reached in a midi sequence?


I'm using javax.sound.midi.Sequencer to play a sequence and I need to jump to an other part of the sequence when a certain tick is reached. Imagine a loop that when we reach tick number "y" we jump back to tick number "x". But I'm not doing a loop so the built in looping methods won't work for me.

At the moment I'm using a swing timer that fires a getTickPosition() every millisecond until I reach my desired tick but this is giving me slightly inconsistent results and I have a feeling it is using a lot of CPU for a fairly simple task.

I wish there was an event listener that would notify me when a certain tick is processed by the sequencer but there isn't AFAIK. Any suggestions would be greatly appreciated.


Solution

  • I have only little ideas about midi, but was always fascinated by computer music, so I looked a bit around and found that you could maybe add a MetaMessage to one of the tracks at the desired tick and use a MetaEventListener to be notified when the message is processed?