How do I know which noteOff MidiMessage is for which noteOn MidiMessage?

I’m going through the MidiMessage documentation, and I can’t find a way to correlate note offs with which note they’re turning off.

For instance, how do I differentiate between these two sets of on’s and off’s:

(One starts1, starts2, stops2, stops1, and the other starts1, starts2, stops1, stops2).

The note numbers are the same, what could be different about them? If two different notes are played, only differentiating in velocity, how do I know which note ends when the midi noteOff message arrives?

I don’t, personally. How would you do that in a real MIDI device, anyway? My code would always turn off the most recent instance of a given MIDI on for that note number (D6 in this case). So the first instance is what would always happen, regardless of your intent. There is nothing in the MIDI note on/off messages that ties them together except having the same note number, as far as I know. Is there a real-world scenario where this would happen? If they were on different channels, I could see it, but the same notes, overlapping, on the same channel? Hmm…


Howard is right, the MIDI specification only gives you a note number and a channel for note events.
So you have no way to know what note off is linked to what note on when they are on the same channel.
It’s up to you to handle that the way you want when you build a synth. Some options:

  • kill the note as soon as you encounter a second note on event, just before restarting it (basically forbidding overlapping notes)
  • maintain a counter of note on, decrease it for each note off, and kill the note when the counter goes to 0 (merging overlapping notes)
  • actually change the sound of a note when encountering another note on, some kind of mid-note attack (handling overlapping notes)
  • or anything you can imagine really!
1 Like

When they designed MIDI 40 years back they thought of physical keyboards and not virtual instruments. How could they?

The solution is Midi Polyphonic Expression. An MPE controller will assign a channel for each note, which allows you to track each note precisely when it starts and when it ends, as well as using controllers like pitch wheel that used to be global per note. This limits you to 14 notes polyphony though (IIRC channel 1 and 16 are reserved for special purposes).

The other solution is MIDI 2.0 which is published but very early days. Could be worth exploring though.


Thanks, I look forward to when the hosts can do that. If there’s an evolving sound, then it could be useful to have two sounds overlap on the same note. Also, if there are two sustained notes of different velocities, then it would make a difference which ended first.

MPE seems to be limited to a couple different hardware controllers, and there seems to be no support in the DAW’s midi generation. MIDI 2.0 looks to be on the horizon, but not yet adopted.

MIDI 1.0 also supports real-time tuning messages, which provides for the same pitch on two different keys.

Using the last bit of the velocity message, 2 sounds could be played simultaneously. obviously this would force to edit some note in such a way that if the first velocity is even the other must be odd. Unless the host or controller is designed to automatically assign it upon detecting the overlapping note.