Each time sequencer plays event, do something

36 views Asked by At

We are trying to write a low-level guitar Hero clone for a semester project.

I have created the track, sequence and sequencer for a song. Now I want the sequencer to notify another class every time its processes/plays an event of type NOTE_ON in its sequence. The other class would then set the timing for the user to hit a certain key on their keyboard.

I do not know if what I am trying here is possible. This is my current code, it uses a sequence that has been filled by a track. The track has several MidiEvents, so far the only statuses are NOTE_ON and NOTE_OFF.

public void startSequencer() throws MidiUnavailableException, InterruptedException, InvalidMidiDataException {

    sequencer.open();
    Thread.sleep(2000);


    // sequence includes a single track
    sequencer.setSequence(sequence);

    //Start sequencer, music plays
    sequencer.start();

    //TODO Sync sequencer with UserInputHandler, via Sequencer.Syncmode?

    //Check every 100 ms if the sequencer is still running, if not, close it.
    while (sequencer.isRunning()) {
        Thread.sleep(100);
    }
    sequencer.close();
}
1

There are 1 answers

0
jjazzboss On

To be notified when a Sequencer plays a Note ON/OFF, you need a custom Receiver and connect it to the Sequencer.

Receiver myReceiver = new Receiver()
{
    public void send(MidiMessage message, long timeStamp)
    {
        // If it's a NOTE_ON/NOTE_OFF then do something, preferably on a different thread if it's a long task
    }
    ...
};

...
sequencer.getTransmitter().setReceiver(myReceiver);