Possible stuck notes in MidiKeyboardState?


#1

I was just having a little look in MidiKeyboardState. This seems to keep a simple flag for each ‘on’ note, rather than a counter.

It looks like if two overlapping MIDI notes of identical MIDI note number are played, this may result in the second note off not responding. The sequence would be:

  • First note on arrives, noteOnInternal() sets the note’s flag and calls handleNoteOn() on the listeners
  • Second note on arrives (with same MIDI note number) - noteOnInternal() sets the note’s flag (which should already be set from the first note) and calls handleNoteOn() on the listeners
  • First note off arrives - noteOffInternal() sees that the note’s flag is set, clears the flag and calls handleNoteOff() on the listeners
  • Second note off arrives - noteOffInternal() sees that the note’s flag is not set and fails to call handleNoteOff() on the listeners

Admittedly this is from static code inspection rather than empirical evidence and this situation is impossible to get into with most MIDI controllers, but it could occur when merging MIDI streams or with multi-touch interfaces.

A listener could choose to ignore the second note on, but most good synths should allow multiple instances of the same MIDI note to be played.


#2

The MIDI spec allows more than one note simultaneously playing on the same pitch. A single note on event must turn off only a single note on. If two note on events are sent, then two note offs must be sent. This is standard MIDI protocol. If a synthesizer turns off both notes when it receives just one note off event, that is a bug.

The MIDI spec does not comment on which note on should be turned off by a note off event of the same pitch, but common practice has been to turn off the oldest note.

This is discussed in the Appendix, section A-4 “Assignment of Note On/Off commands.”

You can easily do this in Digital Performer by cutting one long note and create two new ones. It is used to simulate legato AFAIK.

MIDI spec is not freely available unfortunately.

If you google “Assignment of Note On/Off commands”, you will find results though


#3

Yeah this is a legitimate issue for me.