MIDI Bus Library Interaction Quirk

Working on a sketch that uses MIDIbus to read keystrokes which are mapped to draw/hide objects in an array. The original algorithm used a boolean value to determine whether or not to redraw, unfortunately that method caused the on/off notes to sometime be confused as to who was played last. For example, if I played a C Major chord, C, E, G, but released the keys, E, G, C, the behavior triggered by the ons of G & C would not return to the default off(keyup/key released state).

The code below activates the intended behavior using QWERTY keyboard strokes, but the MIDI business does not. I casted the expression inside the board array just in case integer math wasn’t actually occurring. midiValue is set to 21, to avoid NullPointerExceptions. In an earlier version there are println statements at the beginning of noteOn and NoteOff, inside the if statement and one last one at the conclusion of the if statement before closing the method. Those all print to the console as expected. It’s accessing the array of objects where things seem to break down.

Any ideas would be appreciated!

void noteOn(Note note) {
  if (note.pitch <= 108 && note.pitch >= 21) {
    midiPlayed = note.pitch;
    board[int(midiPlayed - midiValue)].display(true);
  }
}

void noteOff(Note note) { 
  if (note.pitch <= 108 && note.pitch >= 21) {
    midiPlayed = note.pitch;
    board[int(midiPlayed - midiValue)].display(false);
  }
}

void keyPressed() {
  if ((key == 'c') || (key =='C')) {
    midiPlayed = 48;
    board[midiPlayed - midiValue].display(true);
  }
}
void keyReleased() {
  if ((key == 'c') || (key =='C')) {
    midiPlayed = 48;
    board[midiPlayed - midiValue].display(false);
  }
}
1 Like

How do they break down? What’s the error or misbehavior?

The noteOn/noteOff routines are able to output to the console window, but did not seem to be accessing the display method of the board[]. I ended up implementing a boolean[] in the noteOn/noteOff part and then the draw loop calls the display method in the board[].

void draw(){
  for (int b = 0; b < 88; b++){
    changer = notesOn[b];
    board[b].display(changer);
  }
}
void noteOn(Note note) {
  if (note.pitch <= 108 && note.pitch >= 21) {
    midiPlayed = note.pitch;
    notesOn[midiPlayed - midiValue] = true;
  }
}

That sounds like the right approach :slight_smile: I think the midi library runs in its own thread, which can not issue opengl calls (cannot draw to the screen).

If I remember right, what I’ve done is to have a queue where I store messages I receive via midi, and then go through that queue in draw to draw things.

Funny, because the midibus examples only println() stuff to the console instead of drawing things. But I guess all of us want to draw things, not just print text :slight_smile: Maybe I’ll do a pull request with a graphical example…

1 Like