Each track chunk, MTrk, holds the events of one track in time order. The
chunk has the usual 8-byte head, the type MTrk and a 32-bit big-endian
length, and then events until the end of the chunk. There is no count of events: the
last one is always End of
track.
Events
Every event has the same two parts:
<delta time> <event>
The delta time is the number of ticks since the previous event in the same track. The event is one of three kinds, told apart by its first byte, the status byte:
| First byte | Kind | Layout |
|---|---|---|
80 to EF | Channel message | status byte, then one or two data bytes |
F0, F7 | System exclusive event | status byte, a length, then the bytes |
FF | Meta event | FF, a type byte, a length, then the data |
00 to 7F | A data byte | the event repeats the last status byte: see running status |
The other status bytes, F1 to F6 and F8 to
FE, are system common and real-time messages. They are not events in a
file and can only be sent inside an F7
escape. FF in a file is always a meta event, never System Reset.
Delta time
The delta time is a count of ticks, written as a variable-length quantity. It is 0 for an event at the same moment as the one before. How long a tick lasts is set by the header’s division and, for tick-per-beat timing, by the tempo: see Timing.
The delta time of the first event is counted from the start of the track. Adding the delta times up gives each event’s time in ticks from the start.
Variable-length quantities
Delta times, and the lengths of meta and system exclusive events, are written seven
bits to a byte, the most significant group first. Every byte but the last has its top
bit set. A quantity is at most four bytes long, so the largest value is
0FFFFFFF, 268,435,455.
| Value | Hex | Bytes |
|---|---|---|
| 0 | 00000000 | 00 |
| 127 | 0000007F | 7F |
| 128 | 00000080 | 81 00 |
| 192 | 000000C0 | 81 40 |
| 8,192 | 00002000 | C0 00 |
| 16,383 | 00003FFF | FF 7F |
| 16,384 | 00004000 | 81 80 00 |
| 2,097,151 | 001FFFFF | FF FF 7F |
| 268,435,455 | 0FFFFFFF | FF FF FF 7F |
To read one:
- Start with a value of 0.
- Read a byte. Multiply the value by 128 and add the byte’s low seven bits
(
byte & 7F). - If the byte’s top bit is set (the byte is
80or more), go back to step 2. Otherwise the value is complete.
To write one, take the value seven bits at a time from the bottom, then write the groups from the top down, setting the top bit on every byte but the last. The explorer turns down a quantity that runs on for more than four bytes.
Running status
When a channel message has the same status byte as the channel message before it,
the status byte may be left out. The event is then just the delta time and the data
bytes. A reader can tell, because the first byte after the delta time is below
80, and data bytes always are.
System exclusive and meta events cancel running status, so the first channel message after one must have its own status byte. A data byte with no earlier status to repeat is an error. One of the files examined for this site uses running status straight after a meta event; the explorer reads it with the status from before the meta event and says so.
An example from a real file, with the explorer’s reading:
| Bytes | Meaning |
|---|---|
00 FF 21 01 00 | Delta 0, meta event 21 (MIDI port): port 0 |
81 40 B0 00 01 | Delta 192, control change, channel 1: bank select = 1 |
01 20 00 | Delta 1, running status B0: controller 32 (bank select LSB) = 0 |
01 C0 51 | Delta 1, program change, channel 1: 51 = 81, program 82, Lead 2 (sawtooth) |
01 B0 07 6D | Delta 1, control change, channel 1: channel volume = 109 |
00 0A 50 | Delta 0, running status B0: pan = 80 |
End of track
The meta event FF 2F 00 must be the last event of every track. It gives
the track an exact length: its delta time can place the end after the last note. Bytes
after it, inside the chunk, are not part of the track; several of the files examined
have some, and the explorer shows them separately.
A track whose last event runs past the end of its chunk is damaged. One file examined had a first track 2 bytes shorter than its End of track event; the explorer shows such a file in its lenient reading.
Timing
With ticks per quarter note in the header, the length of a tick depends on the tempo, set by the Set tempo meta event in microseconds per quarter note:
seconds = ticks × tempo ÷ 1,000,000 ÷ ticks per quarter note
Until the first Set tempo event the tempo is 500,000 µs a quarter note, which is 120 beats a minute. The time signature is taken as 4/4 until the first Time signature event; that default is widely stated, but was not found in the text of the specification read for this page.
A tempo change applies from its tick onwards, so the time of an event is the sum, over each stretch between tempo events, of the ticks in that stretch at that stretch’s tempo. In types 0 and 1 every track runs on one clock, from the tempo events of the whole file (in type 1 they belong in the first track). In type 2 each pattern has its own.
Worked: at 192 ticks a quarter note and a tempo of 857,143 µs (70 beats a minute), 192 ticks last 192 × 857,143 ÷ 1,000,000 ÷ 192 = 0.857 seconds.
With SMPTE timing, a tick is a fixed fraction of a second, 1 ÷ (frames a second × ticks per frame), and tempo events do not change it.