Introduction
A Standard MIDI File stores music as performance instructions, not sound: which note to play, when, how hard and on which instrument. A synthesiser or sound bank turns the instructions into audio, so the same file sounds different on different devices. The format was adopted by the MIDI Manufacturers Association, now the MIDI Association, in 1988, and is still at version 1.0. It is the usual way to move a song between sequencers, notation programs and keyboards.
There are three types of file. Type 0 holds everything in one track. Type 1 holds several tracks that play together, the first of them carrying the tempo. Type 2 holds independent patterns, each a track of its own; it is rarely used. The type is only the first field of the header and the rest of the layout is the same, so all three are one format. Later additions to the standard, such as new meta events, only add to it, and readers skip what they do not know.
Files use the extension .mid, and sometimes .midi or
.smf. A MIDI karaoke file
(.kar) is a Standard MIDI File with the words in its text events.
A .mid file dropped on any page of this site opens in the explorer,
which shows its chunks and every event, with the notes as a piano roll, the lyrics and
a list of the events with their times.
Structure
A Standard MIDI File is a series of chunks, each a four-letter type and a 32-bit length. The header chunk comes first, then one track chunk for each track. All numbers are big-endian.
0MThd header chunk: type, number of tracks and timing division14 bytes14MTrk track chunk, one per trackrepeated+0MTrk chunk type and length8 bytes+8Events: a delta time, then a channel message, a system exclusive event or a meta eventrepeated…End of trackFF 2F 003 bytes
Each event says how many ticks after the one before it happens. How long a tick lasts comes from the header’s division and the tempo events; the timing section shows how to turn ticks into seconds. Program numbers and percussion keys are named by General MIDI.
A complete file
This 41-byte type 0 file, built for this page, plays middle C for one beat at 120 beats a minute. The header is marked, then the track chunk follows: a tempo event, a note on, a note off 96 ticks later and the end of the track.
00000000 4D 54 68 64 00 00 00 06 00 00 00 01 00 60 4D 54 MThd.........`MT
00000010 72 6B 00 00 00 13 00 FF 51 03 07 A1 20 00 90 3C rk......Q... ..<
00000020 64 60 80 3C 40 00 FF 2F 00 d`.<@../.
| Bytes | Meaning |
|---|---|
4D 54 68 64 00 00 00 06 | Header chunk MThd, 6 bytes long |
00 00 00 01 00 60 | Type 0, 1 track, 96 ticks a quarter note |
4D 54 72 6B 00 00 00 13 | Track chunk MTrk, 19 bytes long |
00 FF 51 03 07 A1 20 | At 0 ticks, set tempo: 500,000 µs a quarter note, 120 BPM |
00 90 3C 64 | At 0 ticks, note on, channel 1, key 60 (middle C), velocity 100 |
60 80 3C 40 | 96 ticks later, note off, channel 1, key 60, release velocity 64 |
00 FF 2F 00 | At the same time, end of track |
Telling it apart
The file starts with the four bytes MThd (4D 54 68 64). The
explorer’s parser for this format checks that, then that the header chunk is at least 6
bytes long and the type is 0, 1 or 2, and then that every event in every track can be
read: a delta time of at most four bytes, a status byte for every event, no system
common or real-time status bytes outside an F7 escape, and meta and
system exclusive lengths that fit in the track. A file that fails is turned down.
A file that starts with RIFF and has RMID at offset 8 is a
RIFF MIDI file, a separate format, even
though it carries a Standard MIDI File inside.
Variants
- Standard MIDI File (MThd) – a header chunk and track chunks of timed events, types 0, 1 and 2
- RIFF MIDI file (RMID) – a Standard MIDI File inside a RIFF container, with optional text and a sound bank
Links
Software
- MuseScore – free notation program that imports and exports MIDI files as scores (Windows, macOS, Linux)
- REAPER – low-cost digital audio workstation that imports and exports MIDI (Windows, macOS, Linux)
- Logic Pro – Apple’s professional music production app (macOS)
- GarageBand – Apple’s free music app, which opens MIDI files as tracks (macOS, iOS)
- FL Studio – Image-Line’s music production software (Windows, macOS)
- Ableton Live – music production software that imports and exports MIDI clips (Windows, macOS)
- Cubase – Steinberg’s music production software (Windows, macOS)
- TiMidity++ – software synthesiser that plays MIDI files (Windows, macOS, Linux)
Developers
- Mido – library that reads and writes MIDI files (Python)
- midifile – library for reading and writing Standard MIDI Files (C++)
- pretty_midi – reads and writes MIDI files, with note and tempo helpers (Python)
- @tonejs/midi – reads and writes MIDI files as JSON (JavaScript)
- FluidSynth – synthesiser library that plays MIDI files with SoundFonts (C)
Reference
- Standard MIDI Files Specification – the MIDI Association’s page for RP-001, with the document for members
- General MIDI Level 1 – the MIDI Association’s page on General MIDI 1