ForOhFor Error
ForOhFor Error

Reputation: 35

Processing midi notes in real time

Basically, I want to get and display the data of a midi note (the track, the note, and the octave) as a sequencer plays it, in real time.

I'd like to be able to add this to the following code:

Sequencer sequencer = MidiSystem.getSequencer();
sequencer.setSequence(MidiSystem.getSequence(song));
sequencer.open();
sequencer.start();
while(true) {
    if(sequencer.isRunning()) {
        try {
            Thread.sleep(1000); // Check every second
        } catch(InterruptedException ignore) {
            break;
        }
    } else {
        break;
    }
}

But I have no idea how to do it.

Upvotes: 1

Views: 1597

Answers (1)

aymeric
aymeric

Reputation: 3895

Add a ControllerEventListener to the Sequencer. It will save you the active wait and will provide all the information you need to display.

ControllerEventListener controllerEventListener = new ControllerEventListener() {
   public void controlChange(ShortMessage event) {
      // TODO convert the event into a readable/desired output
      System.out.println(event);
   }
};

Sequencer sequencer = MidiSystem.getSequencer();
int[] controllersOfInterest = { 1, 2, 4 };
sequencer.addControllerEventListener(controllerEventListener, controllersOfInterest); 

Have a look at this page as well.

Upvotes: 2

Related Questions