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

MIDIFile:

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:

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>

The number of track chunks contained in this MIDI file.
This is a 16-bit binary number, MSB first.

<division>

This defines the default unit of delta-time for this MIDI file.
This is a 16-bit binary value, MSB first.

This may be in either of two formats, depending on the value of MS bit:

Bit:

15

14 ... 8

7 ... 0

<division>

0

ticks per quarter note

1

-frames/second

ticks / frame

bit 15 = 0:

bits 0-14

number of delta-time units
in each a quarter-note.

bit 15 = 1:

bits 0-7

number of delta-time units per SMTPE frame

bits 8-14

form a negative number,
representing the number of SMTPE frames per second.
Valid values correspond to those in the
MTC Quarter Frame message.

Where is is desirable to include messages other than
MIDI Channel messages in a MIDI file (System Exclusive messages in particular),
the <sysex_event>
can be used.

Sysex events come in two flavors:

Sysex Events

F0<length><sysex_data>

F0 Sysex Event

This results in a F0 being sent (Start-Of-Exclusive),
followed by <sysex_data>

<length>

is a variable length quantity, specifying the length of
<sysex_data>

<sysex_data>

F0<sysex_data>is sent as a MIDI message.

F7<length><any_data>

F7 Sysex Event (or 'escape')

This results in exactly <any_data> being sent,
without anything else being added.

<length>

is a variable length quantity, specifying the length of
<any_data>

<any_data>

<any_data>is sent as a MIDI message.

In both cases, the End-Of-Exclusive message, F7, is not sent
automatically. It must be specified explicitly within <sysex_data>
or <any_data>.

Most System Exclusive messages are quite simple, and are sent
as a single packet of bytes, starting with F0 and ending with
F7. These are easily accomodated using the F0 form of
the <sysex_event>

However, some System Exclusive messages are used
to control device parameters in real-time, and what is syntactically
a single System Exclusive message may consist
of a series of small parts which must be sent with appropriate delays.
Hence it is necessary to be able to break up a single message into
a number of events, with appropriate delta-times.
This can be accomodated by using the F7 form of the
<sysex_event>.

In fact, the F7 Sysex Event can be used to included any
data into the MIDI stream, such as MIDI System Real-Time
messages (though this is generally not desirable).

Meta Events are used for things like
track-names, lyrics and cue-points, which don't
result in MIDI messages being sent,
but are still useful components of a MIDI file.

Meta Events have the general form:

FF<type><length><data>

where:

<type>

is a single byte, specifying the type of meta-event.
The possible range is 00-7F.
Not all values in this range are defined, but programs must be able
to cope with (ie ignore) unexpected values by examining the length and
skipping over the data portion.

<length>

is the number of bytes of <data> following.
This is a variable length quantity
0 is a valid <length>

A description of the instrument(s) used on this track.
This can also be used to describe instruments on a
particular MIDI Channel within a track, by preceding
this event with the meta-event MIDI Channel Prefix.
(or specifying the channel(s) within the text).

FF 05<len><text>

Lyric

Lyrics for the song.
Normally, each syllable will have it's own lyric-event,
which occurs at the time the lyric is to be sung.

Time signature of the form:
nn/2^ddeg: 6/8 would be specified using nn=6, dd=3

The parameter cc is the number of MIDI Clocks
per metronome tick.

Normally, there are 24 MIDI Clocks per quarter note.
However, some software allows this to be set by the user.
The parameter bb defines this in terms of the
number of 1/32 notes which make up the usual 24 MIDI Clocks
(the 'standard' quarter note).

nn

Time signature, numerator

dd

Time signature, denominator expressed as a
power of 2.
eg a denominator of 4 is expressed as dd=2

cc

MIDI Clocks per metronome tick

bb

Number of 1/32 notes per 24 MIDI clocks (8 is standard)

FF 59 02sf mi

Key Signature

Key Signature, expressed as the number of
sharps or flats, and a major/minor flag.