[This is a copy of my answer to a question on Stack Exchange's Music Practice and Theory forum.]
Overlapping notes cause ambiguity in a MIDI stream because a MIDI note off message isn't explicitly linked back to a note on message. The illustration below demonstrates the issue. Two notes of the same pitch, 1 and 2, overlap in two different ways, but the exact same MIDI messages are received in both cases. In the first case, note 2 starts after note 1 and extends beyond it. In the second case, note 2 occurs entirely within note 1.
1 2 3 4 time
+---+---+---+---
On On Off Off MIDI stream
11111111 1st case: note 2 starts after note 1
22222222
111111111111 2nd case: note 2 occurs within note 1
2222
The stream of MIDI messages doesn't contain enough information to allow a receiver to distinguish between these two cases. The sender knew which case it was, but the receiver doesn't know, and can't know, because data was lost. This ambiguity could have been avoided if a MIDI note message explicitly specified its duration, instead of expecting the receiver to infer the duration from a subsequent note off, but that ship has sailed.
A MIDI instrument can handle overlapping notes in two possible ways. The most common way is this:
- The first note on message starts the note.
- The second note on message ends the first note and starts a new one.
- The first note off message ends the note.
- The second note off message is discarded as spurious.
This method truncates one of the two instances of the note. It's easy to implement, but it's inaccurate and often causes user complaints. Here's a diagram of it:
1 2 3 4 time
+---+---+---+---
On On Off Off MIDI stream
1111
2222 note is (unjustly) truncated at time 3
The right way is instance counting. The instrument maintains a count for each note, which indicates how many instances of that note currently exist.
- The first note on message starts the note (count: 0 to 1).
- The second note on message starts a second instance of that same note (count: 1 to 2).
- The first note off message ends one of the two instances of the note (count: 2 to 1).
- The second note off message ends the other instance of the note (count: 1 to 0).
How does the instrument decide which instance of the note to end at step 3? It has no basis on which to decide, but it doesn't matter, because both instances are presumed to be identical, so either will do. Here's an updated diagram:
1 2 3 4 time
+---+---+---+---
On On Off Off MIDI stream
111111111111 proper overlap handling
2222
Instrument manufacturers often don't bother with instance counting because it's extra work, and also because stuck notes could occur if the counts somehow get scrambled. Another potential downside is flanging, because during the period of overlap, two sound generators are producing the same sound. Nonetheless instance counting is definitely more correct than truncation.
Instance counting still doesn't allow the receiver to distinguish between the two cases I mentioned at the beginning. You might think it doesn't matter, and for an instrument, you'd be right. But for a music editing software, it does matter, because such software is expected to display the notes correctly, including their durations. In that case, again, the MIDI stream lost crucial information, so there's no proper solution, and the software has to arbitrarily pick one of the two cases. FWIW my MIDI sequencer does distinguish between these cases; see the Note Overlap manual page.