MIDI files begin with a header chunk.
The header chunk of a MIDI file is a piece of information at the beginning of a MIDI file that is composed of the following.
|Description||Length in bytes||Starts at byte||Value|
|chunk ID||4||0x00||The character string "MThd"|
|format type||2||0x08||0, 1, or 2|
|number of tracks||2||0x0A||1 - 65,535|
|time division||2||0x0C||Various as described below|
In other words, the header chunk is a piece of information and shows that this is a header chunk that contains 6 bytes of data and these bytes represent three pieces of information as follows: 1) the format of the MIDI song; 2) the number of tracks in the MIDI song; and 3) the timing resolution of the MIDI song. The MIDI header chunk describes the whole MIDI song.
The "chunk ID" for the header chunk is always the text "MThd", which signifies that this is a header chunk.
The "size" of the header chunk is always six for the six bytes that follow the size (for the format type, number of tracks, and time division). In principle, it is not necessary to use four bytes for the header chunk size, but this is consistent with the four bytes that track chunks use to describe their size.
The "number of tracks" is the number of track chunks in the MIDI file.
The "format type" can have values of 0, 1, or 2. A value of 0 means that the MIDI file contains one track chunk with all relevant MIDI events, including meta events that contain information about the MIDI song as well as musical MIDI events that actually do something. A value of 1 means that the MIDI file contains two or more track chunks. In such a case by convention the first chunk will contain meta events with the song information and the second and other chunks will contain the musical MIDI events. A value of 2 also means multiple track chunks, but in this case each track represents a separate MIDI sequence, which may or may not be played simultaneously, and which may contain its own meta and musical events.
The "time division" of the MIDI song is an indication of how MIDI ticks should be translated into time. There are two possible translations of these two bytes. If the top bit of the 16 bits is 0, then the time division is in "ticks per beat" (or “pulses per quarter note”). If the top bit is 1, then the time division is in "frames per second".
When the top bit of the time division bytes is 0, the time division is in ticks per beat. The remaining 15 bits are the number of MIDI ticks per bit. Suppose that these 15 bits compute to the number 60. Then the time division is 60 ticks per beat and the length of one tick is
1 tick = microseconds per beat / 60
The variable "microseconds per beat" can be specified by a MIDI event (a tempo meta event). If it is not specified then it is 500,000 microseconds by default, which is equivalent to 120 beats per minute. Then, in this example, the length of one tick would be
1 tick = 500,000 / 60 = 8333.33 microseconds
When the top bit of the time division bytes is 1, the remaining 15 bits have to be broken in two pieces. The top remaining 7 bits (the rest of the top byte) represent the number of frames per second and could be 24, 25, 29.97 (represented by 29), or 30. The low byte (the low 8 bits) describes the number of ticks per frame. Thus, if the frames per second are 24 and there are 100 ticks per frame, one tick is equal to
1 tick = 1,000,000 / (24 * 100) = 694.44 microseconds
Thus, when the time division top bit is 1, the length of a tick is strictly defined by the two time division bytes. The first byte is the frames per second and the second byte is the number of ticks per frame, which is enough to specify the tick length exactly. This is not so in the first case above, when the top bit of the time division bytes is 0. The time division in this case defines the ticks per beat, but nothing in the time division specifies the number of beats per second. A separate MIDI event can specify the number of beats per second (or the length of a beat), or you can leave it up to the MIDI device to set the tempo (120 beats per minute by default, as mentioned above).