I've been working with MIDI files generated from MIDI keyboard playing and finding that some of them are seriously flawed, in that the number of note_on messages and note_off messages does not always match up, and there is no way to reconstruct the input from the MIDI output.
When I count note_offs, I am accounting for both 0 velocity note_ons and explicit note_off messages. Sometimes I find more note_ons than note_offs, sometimes vice versa, and worst of all sometimes even if they match in number there isn't a proper correspondence.

Has anyone else encountered this problem (which might be keyboard specific?) And if so, did you figure out what might be a/the cause of it?

  • It's quite easy to get those anomalies in MIDI output from a notation program, where you can write notes which overlap (which may be perfectly valid notation for a human to read), create a tie after a note but not actually tie it to another note, etc, etc. One reason for getting this from keyboard playing is that you have a cheap and nasty MIDI keyboard or computer interface that is simply losing data. If the keyboard or interface were made in China and you bought them on Ebay, that is not usually good news.
    – user19146
    Oct 17 '16 at 21:35

MIDI allows two ways to say a note is off. One is saying the note is off, another is doing a note on event with velocity 0. Either is valid and it is possible your keyboard does both.

If that is not the issue, it's possible however unlikely the envelop used on that patch of the keyboard makes it so notes over a certain length decay out of existence which works for that keyboard, but obviously may not work for others.

A good way to check if the file is parseable is import it into a notation software like Finale, MuseScore, or Sibelius. If they have issues then there's really not much you can do besides correct it yourself.

  • interestingly a lot of the music notation softwares will deal with this without throwing errors. They seem to have some ways of sanitizing or parsing information in the MIDI. But if the file is ill-formed then the various softwares will produce different results. Oct 17 '16 at 19:05
  • @user3499756 I know in finale it will fail to import a MIDI if certain things are invalid (mostly bad timing events).
    – Dom
    Oct 17 '16 at 19:09

MIDI is horribly inconsistent in many regards. No, it doesn't specify that every note_on must be matched with a note_off – if it isn't, the instrument is supposed to just keep the note ringing as long as it feels like: generally until either another note is sent with the same number (and a special case of that, the velocity-0 note, is actually often used as equivalent to a note-off event!) or until its polyphony voices are used up and it needs to reassign one.

The other way around, note_off can also be sent “just in case”, e.g. a MIDI-panic button does that. (However, some virtual instruments may actually emit a sound like piano dampers on note-off...)

To sanitise a given file, I'd suggest you first normalise either all velo-0 notes to note_off. Then scan the note events on each note number, and for every note_on that's followed immediately by another note_on (or by EOF) you insert a note_off right before the following event. Sequential note_off events can be removed.

  • thanks this is interesting food for thought. I wonder how reliable this sanitization method is.... You mention that a note_on doesn't need an effective note_off partner for practical purposes (for the instrument's purposes the note can just decay in volume. That accounts for note_ons > note_offs... what about the other direction? How can an effective note_off come into existence if the note was never played? Oct 17 '16 at 18:50
  • As I said note_off is often used as a mere precaution. In live setups, MIDI is notorious for leaving notes ringing forever and you can't find out which instrument, which channel and which note number is the culprit... Oct 17 '16 at 19:24

lots of drum machines just send noteon events for the sequencer to record. or a trailing noteoff that may come after a 2nd+ note on.

and midi sequencers are not all perfect.

and unpaired events are completely allowed.

so you really just can't expect em to all match up.

there's also the track versus channel thing. maybe 3 tracks have the notes for a given device/channel. the ons and offs may all balance but not necessarily within one of those 3 tracks.

midi is messy. it ain't easy dealin' with it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.