Unexpected NOTE OFF's When Reading MIDI File

When I open a simple midi file containing only four NOTE ON’s and no NOTE OFFs, the getNumEvents() function returns more events than I see using a hex editor (17). Using the getEventPointer() function to read events from the file then returns pointers to four unexpected NOTE OFF’s as well as the expected four NOTE ONs. The hex editor clearly shows only the four NOTE ONs and no NOTE OFFs. Is JUCE adding NOTE OFF’s for some reason? The file contains the following midi messages when using a hex editor:

MThd, MTrk, Set Tempo Command, 8 NOTE ONs, End Of Track

On further examination, I found the NOTE OFF’s are being “added” just before a NOTE ON is to be played if there is no NOTE OFF for that same note number. In other words, it looks like JUCE is enforcing monophonic playback of the midi file.

Try adding a breakpoint where your midi file is loaded, and step into the JUCE code that actually parses and loads a midi file. that’ll tell you if JUCE is adding NoteOFFs for note on’s without matching NoteOff’s.

Thank you for your response.

I haven’t exactly proven NOTE OFF’s are being added but I did as you suggested and found that the readFrom() function does more than just read the midi file. As expected, it calls readNextTrack() but that function includes the following lines at the end:

   // use a sort that puts all the note-offs before note-ons that have the same time
   MidiFileHelpers::Sorter sorter;
   result.list.sort (sorter, true);
   addTrack (result);

This looks like the MIDI file events returned by readFrom() are not necessarily exactly as they exist in the MIDI file. I suspect the NOTE OFF’s are being added somewhere in this code section.

I’d really like to hear from a JUCE developer if this is really whats going on and if there is a way to use the readFrom() function to read the MIDI file without modifying or adding MIDI events.

The updateMatchedPairs will add the missing note offs:

Thank you Fabian.

Is there a way to use the JUCE midi file functions to read a midi without adding NOTE OFF’s? I’m working on an application that creates and plays back midi files and I would like these midi files to be played back exactly as they were created.

OK I’ve added an option to disable adding missing note-offs. It’s on the develop branch with commit b856e38.

1 Like


Thank you very much.

1 Like