A Standard MIDI File is built from chunks. The first is the header chunk
MThd, which says what kind of file this is, how many tracks follow and how
time is counted. The track chunks come
after it. All numbers are big-endian, most significant byte first.
00000000 4D 54 68 64 00 00 00 06 00 01 00 12 00 C0 4D 54 MThd..........MT
00000010 72 6B rk
Chunks
Every chunk has the same 8-byte head. There is no padding between chunks: the next chunk starts straight after the last byte of the one before.
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 4 | ASCII | type | MThd for the header, MTrk for a track. Any other type is a chunk to skip. |
| 4 | 4 | u32 | length | The number of data bytes that follow, not counting these 8. |
| 8 | length | data | The chunk’s contents. |
The file is one MThd chunk followed by the MTrk chunks, as
many as the header says.
The header chunk
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 4 | ASCII | type | MThd (4D 54 68 64). |
| 4 | 4 | u32 | length | 6. Readers must accept a larger value and skip the bytes after the three fields. |
| 8 | 2 | u16 | format | The type of file, 0, 1 or 2. See Format. |
| 10 | 2 | u16 | ntrks | The number of track chunks. See Tracks. |
| 12 | 2 | u16 | division | How delta times are counted. See Division. |
In the example above, 00 00 00 06 is the length, 00 01 is
type 1, 00 12 is 18 tracks and 00 C0 is 192 ticks a quarter
note.
Format
| Value | Type | Meaning |
|---|---|---|
| 0 | Single track | One track chunk holds everything: every channel, the tempo and the time signature. |
| 1 | Several tracks | Tracks that play at the same time. The first track should hold the tempo map: the tempo and time signature events. All tracks share it. |
| 2 | Independent patterns | Each track is a pattern of its own, with its own tempo events. A sequence number meta event can identify each one. |
Type 0 and type 1 files are the usual ones. Type 2 files are rare: none was among the files examined for this site. The explorer times a type 2 file’s patterns one after another, each starting at 120 beats a minute with its own tempo events.
Tracks
ntrks is the number of MTrk chunks in the file. It is always
1 in a type 0 file. Some files have a count that does not match: in one of the files
examined it was one less than the tracks present. The explorer reads every
MTrk chunk it finds and reports the difference.
Division
The top bit of division chooses one of two ways of counting time.
| Bit 15 | Bits 14 to 8 | Bits 7 to 0 | Meaning |
|---|---|---|---|
| 0 | ticks per quarter note, 1 to 32,767 | Delta times are fractions of a beat; how long a beat lasts depends on the tempo. | |
| 1 | minus the SMPTE frame rate | ticks per frame | Delta times are fixed fractions of a second. See SMPTE timing. |
Ticks per quarter note are often called PPQ, pulses per quarter note. Common values are 96, 120, 192, 384 and 480; 48 and 240 also occur.
SMPTE timing
When bit 15 is set, the high byte is a negative number in two’s complement, minus the frame rate, and the low byte is the number of ticks in each frame.
| High byte | Value | Frame rate |
|---|---|---|
E8 | −24 | 24 frames a second |
E7 | −25 | 25 frames a second |
E3 | −29 | 30 drop-frame, 29.97 frames a second |
E2 | −30 | 30 frames a second |
For example, E7 28 is 25 frames of 40 ticks: 1,000 ticks a second, so
each tick is a millisecond. With SMPTE timing, tempo events do not change how long a
tick lasts. No file with SMPTE timing was among the files examined.
Unknown chunks
A reader must skip a chunk whose type it does not know, whole, by its length. This lets later versions add chunk types without breaking older readers. The explorer shows such chunks with their bytes and carries on.
Damaged files also occur. In the files examined for this site there were bytes left
between chunks by an editor that rewrote chunks in place, and a few stray bytes after
the last chunk. When the bytes after a chunk do not start another chunk, the explorer
looks for the next MTrk and carries on from there; zero bytes at the end
of the file are shown as padding.