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

208 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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?

0
On

I would suggest you reduce your

getTickPosition() every millisecond

which = 1000 x second - is too often You could reduce by a factor of 100 to 10 x Second This would reduce you CPU usage (to 1% of what it currently is?)

The MIDI timing clock messages are (at 120 bpm) only two beats / per second which is (at 24 ticks per quarter note) 1/50th of a second (20 mSecs) to capture sub beat intervals - is that what you need to do?

The better solution might be for the Sequencer to tell you when it has arrived but I don't know if a mechanism exists for this

I would be interested to know what fix works for you