Standard MIDI File Format

Byte order: Big-endian
Offset   Length   Contents
  0      4 bytes  "MThd"
  4      4 bytes  Header data size (6)
  8      2 bytes  Format
                    0: 	The file contains a single multi-channel track
                    1:  The file contains one or more simultaneous
                        tracks (or MIDI outputs) of a sequence
                    2:  The file contains one or more sequentially
                        independent single-track patterns
  10     2 bytes  Number of tracks (n)
  12     2 bytes  Divisions
[ // Tracks
         4 bytes  "MTrk"
         4 bytes  Track data size (t)
        (t)bytes  Track data
]{n}
Track data:
<track-data>  ::= <MTrk-event>+
<MTrk-event>  ::= <Delta-time> <Event>
<Event>       ::= <MIDI-event> | <SysEx> | <Meta-event>
<MIDI-event>  ::= <status-byte> <data-byte>*
<status-byte> ::= [\x80-\xFF]
<data-byte>   ::= [\x00-\x7F]
<Meta-event>  ::= 0xFF <Meta-type> <Length> <Meta-data>
<SysEx>       ::= (0xF0|0xF7) <Length> <SysEx-message>
<Delta-time>  ::= [\x80-\xFF]*[\x00-\x7F]     # Variable-length quantity
<Length>      ::= [\x80-\xFF]*[\x00-\x7F]     # Variable-length quantity

Appendix: