The MIDI File Format
MIDI File Structure
Chunks
MIDI files are structured into chunks.
Each chunk consists of:
type length data
4 bytes 4 bytes length bytes
• A 4-byte chunk type (ascii)
• A 4-byte length (32 bits, msb first)
• length bytes of data
There are two types of chunks:
Header Chunks
which have a chunk type of "MThd"
Track Chunks
which have a chunk type of "MTrk"
A MIDI file consists of a single header chunk followed by one or more track chunks.
Since the length-field is mandatory in the structure of chunks, it is possible to accomodate chunks other than "MThd" or "MTrk" in a MIDI file, by skipping over their contents. The MIDI
specification requires that software be able to handle unexpected chunk-types by ignoring the entire chunk.
<---Chunk--->
type length Data
MIDI
File
:
MThd
6 <format> <tracks> <division>
MTrk
<length> <delta_time> <event> ...
:
MTrk
<length> <delta_time> <event> ...
Value Representations
Within a MIDI file, there is a variety of information in addition to the basic MIDI data, such as delta-times and meta-events.
Where such information contains numeric values, these are generally represented in one of two formats:
• binary
• variable length quantity
Binary
Binary values are stored:
• 8-bits per byte
• MSB first (Most Significant Byte first)
(unless otherwise specified)
Variable Length Quantities
The variable-length quantity provides a convenient means of representing arbitrarily large integers, without creating needlessly large fixed-width integers.
A variable-length quantity is a represented as a series of 7-bit values, from most-significant to least-significant. where the last byte of the series bit 7 (the most significant bit) set to 0, and the preceding
bytes have bit 7 set to 1.
Examples:
Value Variable Length representation
Decimal Hex Binary Binary Hex
- abcd
aaaabbbbccccdddd 100000aa 1aabbbbc 0cccdddd
-
0
:
127
00
:
7F
0000 0000
:
0111 1111
0000 0000
:
0111 1111
00
:
7F
128
:
16383
80
:
3FFF
00000000 10000000
:
00111111 11111111
10000001 00000000
:
11111111 01111111
81 00
:
FF 7F
1000 03E8
11 1110 1000 10000111 01101000
87 68
100000 0F4240
1111 0100 0010 0100 0000 10111101 10000100 01000000
BD 84 40
As you can see from the above examples, small values (0-127) can be represented by a single byte, while larger values are also accomodated.
The largest value allowed within a MIDI file is 0FFFFFFF. This limit is set to allow variable-length quantities to be manipulated as 32-bit integers.
Header Chunks
The data part of a header chunk contains three 16-bit fields. These fields specify the format, number of tracks, and timing for the MIDI file.
The length of the header chunk is 6-bytes. However, software which reads MIDI files is required to honour the length field, even if it is greater than expected. Any unexpected data must be ignored.
Header Chunk
Chunk Type length Data
4 bytes
(ascii)
4 bytes
(32-bit binary)
<-- length (= 6 bytes) -->
16-bit 16-bit 16-bit
MThd
<length> <format> <tracks> <division>
<length>
length in bytes of the chunk data part.
This is a 32-bit binary number, MSB first.
This will be exactly 6 (bytes) for any MIDI file created under the MIDI!1.0 specification. Nevertheless, any MIDI file reader should be able to cope with larger header-chunks, to allow for future
expansion.
<format>
The MIDI file format.
This is a 16-bit binary number, MSB first.
The only valid formats are 0, 1 and 2.
<tracks>