I’m trying to read a midi file in C++ and I’m stuck with an error which seems to be an event not defined in the standard midi spec (retrieved from many sites including this: http://www.sonicspot.com/guide/midifiles.html)
I opened the midi in a HEX editor and pinpointed the error. It happens before the very first “note on” event. The file was generated using Sibelius 6 software and I was wondering that if I post the file section here someone would help me come around this.
This is the beginning of the 3rd track:
4D 54 72 6B (MTrk magic number)
00 00 1F F5 (track size in bytes - 8181)
00 FF 03 04 (4 bytes follows) (track sequence name)
00 FF 04 0B (11 bytes follows) (instrument name)
00 C0 34 (program change event)
00 B0 79 00 (controller event)
00 5B <- (what the heck is that?!)
It can’t be delta ticks since the first value is 00 (no MSB set to 1), it’s not a recognizable event either. 10 bytes later I recognize a “note on” event
It goes:
00 5B 30 00 40 00 00 07 64 00 0A 10 00 90 3E 47 <- note on
I have no idea what those 12 bytes before “note on” represent and therefore cannot read the file. I know that the very first note is quite long maybe that’s the reason. I also noticed I couldn’t find a event to represent “bar” pauses. Maybe that’s the code for a bar + length? Should I read those as ticks?
One complexity of MIDI files is Running Status. If there’s sequence of messages of the same type and channel (eg all controllers or all notes) then MIDI can save a number of bytes by omitting the status byte. If this didn’t use running status then the bytes you would see are:
Because all the controller messages are contiguous and are for the same channel, the status byte can be omitted. As soon as there’s a change of message type, the status byte has to be added again.
If you’re trying to make sense of MIDI files then I would recommend using a separate tool such as Python-MIDI or GNMidi as a sanity checker whenever there’s a MIDI event you can’t make sense of. These can show it as text so you can mimic what it’s doing.
EDIT: Another gotcha to be aware of is that any MIDI messages that take a length or duration parameter (eg the time in PPQN between events in a MIDI file, or the length of sysex messages or meta events) uses a variable length, so don’t assume all the length fields are always a fixed length.
Disclaimer: I wrote the MIDI export code in Sibelius 6…