Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading MIDI file (C): 0x00 appearing after the end of a var-length value

I'm trying to write some functions to read in a MIDI file. I've been referring to a number of sites that give the specification, but mostly this one: http://www.sonicspot.com/guide/midifiles.html (archive)

I have been testing it on a MIDI file for the Super Mario Bros theme that I downloaded, and I'm getting some unexpected data. It may be that the file is wrongly formatted, but I think it's more likely that I'm doing something wrong. Here is the data (from a hex editor) that I'm having trouble with, and what I think I know about it:

4D 54 72 6B 00 00 00 19 00 FF 51 03 05 7B 71 00 FF 58 
|---------| |---------| || || || |------| || ||
    MTrk    Chunk size  || || ||  Tempo   || ||
             (25 bytes) || || ||(ms per   || ||
                        \/ || ||1/4 note) || ||
                VLen value || ||          \/ ||
          (Event at time 0)|| ||  VLen value ||
                           \/ || (event at   ||
                 Beginning of ||  time 113)  ||
                 meta-event   ||             ||
                              \/             \/
                 Meta-event type:           ????
                      set tempo

As you can see, if everything before the 0x00 is correct, then what is it doing there? The VLen value before has a binary value of 01110001 and so isn't expecting another part of the VLen value, and therefore, AFAIK, should be an event type. However there is no event type associated with 0x0. Can anyone see where I'm going wrong?

like image 645
benwad Avatar asked Dec 06 '12 12:12

benwad


1 Answers

I found the problem: meta-event codes (in my case 0x51) are proceeded by a chunk size just like normal events. The reason I thought otherwise is because, for the set tempo meta-event, the tempo data size is always 3. Therefore 51 03 05 7B 71 is actually the set tempo event code (51), the size of the tempo data (03) then the actual tempo (05 7B 71), and then the 00 is just another variable-length value telling me that the next event is at time 0.

I hope this helps someone. I also found a better piece of documentation for the MIDI format which made this clearer: http://www.omega-art.com/midi/mfiles.html (archive)

like image 76
benwad Avatar answered Sep 23 '22 06:09

benwad