The Standard MIDI File (SMF) specification was first published in 1990. The specification describes the low level format that all MIDI files follow. As a result of this standard, users can create, play, save, and share MIDI files across every major computing platform including, Windows, Mac, Linux, and Unix. Standard MIDI Files normally end with a ".mid" file extension. Complete SMF documentation can be found on the Reference page.
The MIDI messages in a SMF are very similar to MIDIDisplay data with two differences:
A Standard MIDI File is built in "chunks." Each chunk is a self contained section of data within the larger file. There are two chunk types, the header chunk and the track chunk. Every SMF begins with one header chunk that can be followed by up to 17 track chunks. The header chunk has the same format in all MIDI files and is exactly 14 bytes long. The first four bytes of the header chunk are MThd (MIDI Track header). It's these four bytes that identify the file as a SMF on all computers. The next four bytes indicate the length of data to follow and is always 6 in the header chunk. The six bytes of data following the length provide three important pieces of information: the SMF type, the number of tracks in the file, and the PPQ value. Up to 17 track chunks can follow the header chunk. The first four bytes of every track are MTrk (MIDI Track). The next four bytes indicate the length of the data to follow. Track 1 is is called the Tempo Track and contains meta events that provide information about the key signature, time signature, and tempo. Tracks 2-17 are the instrument tracks and contain MIDI data very similar to MIDIDisplay. Every track ends with the End of Track meta event. An overview of the SMF structure looks like this.
The remainder of this document explains the SMF structure in more detail
The MIDI file example I used in class was created using the Sibelius 7 notation software. The remainder of this document uses the same notes but was created in a previous version of Sibelius. There are slight differences in how Sibelius 7 saves a MIDI file compared to this earlier version. The MIDI file used for the following examples is called "04ClassExample60bpm.mid" and can be found in the course common folder.
You cannot view SMF data in an ordinary text editor because the data is stored as binary data. You need a special editor called a hex editor that allows you to view the contents of any file as hex bytes. If you converted every hex byte to its 8 bit binary representation, you'd get the actual ones and zeros the computer uses. The Reference Page has links to free hex editors on Mac and Windows. The following picture shows what the class example SMF looks like when viewed in HexEdit, a hex editor for the Macintosh. The HexEdit window is divided into three panes: the left pane is a running total of byte indexes into the file, the middle section is the hexadecimal display (16 bytes per line), and the right section is the ASCII (text) display. In the text display pane hex digits that correspond to alphanumeric characters are shown as text, otherwise as a period. You can see one MThd and three MTrk markers in the text area.
When the first four bytes of a file are 4D 54 68 64 (MThd in text), the computer knows it's a MIDI file. In the following picture you can see MThd at the beginning of the file, followed by three occurrences of MTrk. Each track chunk is given a different color. The MTrk track markers are the first four bytes of each track chunk.
The four bytes following the MThd or MTrk marker indicate the length in bytes of the remaining data in the track. Here's the same file with the the rest of the data color coded. Data for NON-NOF messages is shown in yellow, MIDI Controller messages in light green, and Patch Changes in pink. They're the same as MIDIDisplay messages except all numbers are in hex.
Legend |
The first four bytes of every MIDI file are always MThd. The header chunk has the same format in all MIDI files and is exactly 14 bytes long. The first four bytes of the header chunk are MThd (MIDI Track header). The next four bytes indicate the length of data to follow and is always 6 in the header chunk. The six bytes of data following the length provide three important pieces of information: the SMF type, the number of tracks in the file, and the PPQ value.
Header Chunk | |||
Subsection | Notes | ||
Marker | All SMF's begin with MThd. | ||
Length of data | The length is always 6 for the Header Chunk. | ||
Type of SMF | There are three types: 0, 1, and 2. We'll be using type 1 where each instrument is stored in a separate track. |
||
Number of Tracks | The number of individual tracks in this SMF. | ||
PPQ | The PPQ (parts per quarter note) value used to calculate the time differences. |
Here's the header chunk in our example.
04ClassExample header chunk | |||
Subsection | Bytes | Data | Notes |
Marker | MThd |
||
Length of data | All header chunks use 6 bytes of data |
||
Type of SMF | 1 = Type 1 SMF |
||
Number of Tracks | 3 = 3 tracks in this file |
||
PPQ | 1E0 hex = 480 dec |
Up to 17 Track Chunks follow the Header Chunk. The structure of a Track Chunk is shown below. The first four bytes of every track chunk are MTrk. The next four bytes indicate the length of data to follow. A variable number of track events follow, each track event preceded by a delta time. The track ends with an End of Track meta event.
Track Chunk
| |||
Subsection | Notes | ||
Marker | Every track begins with MTrk. | ||
Length of data | The number of bytes to follow | ||
<Delta Time> <Track Event> |
The rest of the track consists of any number |
Delta times are PPQ times converted to a special format to minimize file size. The designers of the SMF realized that most of the time the PPQ values from one note to the next was a small number. However they still had to account for the possibility of rests across multiple measures. They used a method to compress small time values into one byte and larger time values into two, three, or four bytes, whichever was smaller. They called these variable length PPQ times, delta times. Delta times are easy for the computer to calculate, but difficult for humans. The delta time conversion code is listed here if you're interested. You will not have to calculate any delta times in your project. MIDIDisplay will convert PPQ times to delta times for you.
A Track Event follows every Delta Time. There are three types of track events:
You may have noticed the example score contains only two instruments but the Header chunk indicated there were three tracks. Track 1 is a special non instrument track called the tempo track. It contains meta events that provide information about the song. Things like the time signature, key signature, song name, copyright, lyrics, and most importantly tempo changes. Several types of meta events are defined in the SMF specification. Every Meta Event is preceded by a delta time and begins with FF, followed by the type of meta event, followed by a variable length one to four byte item that indicates the length of data to follow.
Meta Event = Delta Time + <meta event> |
|||
Delta Time |
Number of Bytes |
Notes |
|
Delta Time | Variable length PPQ data | ||
<meta event>= FF + <meta type> + <variable length data > + <data bytes> |
|||
Marker | All Meta Events start with $FF | ||
Meta Type | Several types of Meta Events are defined below. |
||
Variable Length Data | Represents the length of the following data in as few bytes as possible. |
||
Data Bytes | Specific to the meta event type |
This is the track 1 data.
Track 1 The Tempo Track |
||
Hex |
Decimal |
Legend |
Here's a detailed explanation of the tempo track.
04ClassExample Track 1 | ||||
Subsection | Bytes | Delta Time | Data Hex | Notes |
Marker | not used | MTrk, the start of a track chunk | ||
Length of data | not used | length in bytes = 19(hex) or 25 (decimal) | ||
$58 is Meta Event for Time Signature | ||||
$59 Meta Event for Key Signature | ||||
$51 Meta Event for Set Tempo | ||||
$2F Meta Event for End or Track - Required |
The first three bytes of every meta event have the same meaning. The bytes following the third byte are interpreted differently for each meta event.
You'll be using meta events for the time signature, key signature and tempo in your project.
Byte 1 |
Type |
Length |
Data |
Data Interpretation | |
FF
|
58 |
4 |
4 2 18 8 |
4 = numerator of Time Signature 2 = denominator of Time Signature expressed as a power
of 2. 18 or (24 decimal) MIDI clocks per metronome click 8 = 8 thirty second notes per quarter note |
Byte 1 |
Type |
Length |
Data |
Data Interpretation | |
FF |
59 |
2 |
2 0 |
2 = two sharps in the key signature 0 = major key |
Byte 1 |
Type |
Length |
Data Bytes |
Data Interpretation | |
FF 51 |
51 |
3 |
0F 42 40 |
tt tt tt = time in microseconds per quarter note microseconds = 1,000,000 * 60 / tempo tempo = 1,000,000*60 / microseconds 0F 42 40 (hex) = 1,000,000 ( decimal) |
A complete list of meta events can be found in the SMF readings on Reference page.
Up to 16 tracks can follow the tempo track, one for each MIDI channel. These are the instrument tracks that contain the same MIDI messages you've been using in MIDIDisplay. Generally one track is used for each instrument. There are exceptions. For example, a piano part might use two tracks, one for the right hand and one for the left hand, or a guitar part might use six tracks, one for each string. All instrument tracks follow this format.
Tracks 2-17 |
|||
Subsection | Notes | ||
Marker | Every track begins with MTrk. | ||
Length of data | The number of bytes to follow | ||
MIDI Events | Any number of MIDI events |
||
End of Track Marker | Required - End of Track |
A MIDI Event consists of a delta time plus a MIDI message. You already know the MIDI messages for NOF, NON, Controllers, and Patch Changes.
MIDI Event = Delta Time + MIDI
Message |
|||
Delta Time | Notes | ||
Delta Time | PPQ values | ||
MIDI Message = <Status> <data
1> <data 2>
|
|||
Status | status byte | ||
Data 1 | data1 byte | ||
Data 2 | data2 byte |
Track 2 is the flute track and uses some strange looking PPQ delta times like 81 70. When these are converted back into normal PPQ's using a value of 480 they look familiar. These are the PPQ values of the delta times used in this track.
Delta Time Hex | Converted to PPQ Decimal | Explanation |
00 | 0 | Time of zero duration |
01 | 1 | 1/480 of a quarter note |
81 70 | 240 | An eighth note |
87 40 | 960 | A half note |
This is all of the data in the track 2 chunk.
Hex |
MIDI Display Format |
Legend |
Here's a more detailed explanation of track 2. Everything should be familiar except the PPQ delta times and the new controller messages for Sustain pedal, Effects level, and Track Name.
04ClassExample Track 2 | |||||
Subsection | Bytes | Delta Time Hex | Data Hex | MD Format |
Notes |
Marker | not used |
4D 54 72 6B |
MTrk | ||
Length of data | not used |
0000 00 71 |
length in bytes = 71(hex) or 113 (decimal) | ||
00 |
C0 49 |
C0 73 |
Patch change 73 = Flute | ||
00 |
B0 79 00 |
B0 121 00 |
Reset all controllers |
||
00 |
B0 40 00 |
B0 64 00 |
Sustain pedal off |
||
00 |
B0 5B 30 |
B0 91 48 |
Effects level (Reverb) |
||
00 |
B0 0A 40 |
B0 10 64 |
Stereo Pan | ||
00 |
B0 07 64 |
B0 7 100 |
Volume | ||
00 |
FF 03 01 20 |
Track Name meta event <20 = space> | |||
0 |
90 3E 48 |
90 62 72 |
Delta Time -NON | ||
81 70 |
80 3E 0 |
80 62 0 |
Delta time -NOF | ||
0 |
90 40 4C |
90 64 76 |
etc. | ||
81 70 |
80 40 0 |
80 64 0 |
|||
0 |
90 42 4E |
90 66 78 |
|||
81 70 |
80 42 0 |
80 66 0 |
|||
0 |
90 43 56 |
90 67 86 |
|||
81 70 |
80 43 0 |
80 67 0 |
|||
0 |
90 45 56 |
90 69 86 |
|||
81 70 |
80 45 0 |
80 69 0 |
|||
0 |
90 47 54 |
90 71 84 |
|||
81 70 |
80 47 0 |
80 71 0 |
|||
0 |
90 49 52 |
90 73 82 |
|||
81 70 |
80 49 0 |
80 73 0 |
|||
0 |
90 45 41 |
90 69 65 |
|||
81 70 |
80 45 0 |
80 69 0 |
|||
0 |
90 4A 50 |
90 74 80 |
|||
8740 |
80 4A 0 |
80 74 0 |
|||
01 |
FF 2F 00 |
REQUIRED - End of Track meta event |
This is the data for track 3, the bassoon part. You should recognize the MIDIDisplay messages, except for the PPQ times shown as Delta Times. Track 3 uses these PPQ values:
Delta Time Hex | PPQ Decimal | Notes |
00 | 0 | Time duration of zero |
01 | 1 | 1/480 of a quarter note |
8360 | 480 | A quarter note if PPQ = 480 |
8740 | 960 | A half note if PPQ = 480 |
This is all of the data in the track 3 chunk.
Hex |
Decimal |
Legend |
Here's a more detailed explanation of track 3. Everything should be familiar except the PPQ delta times and the new controller messages.
04ClassExample Track 3 | |||||
Subsection | Bytes | Delta Time Hex | Data Hex | MIDIDisplay |
Notes |
Marker | not used |
4D 54 72 6B |
MTrk | ||
Length of data | not used |
00 00 00 41 |
length in bytes = 41(hex) or 65 (decimal) | ||
00 |
B1 79 00 |
B0 121 00 |
Reset all controllers |
||
00 |
B1 40 00 |
B0 64 00 |
Sustain pedal off |
||
00 |
B1 5B 30 |
B0 91 48 |
Effects level (Reverb) |
||
00 |
B1 0A 40 |
B0 10 64 |
Stereo Pan | ||
00 |
B1 07 64 |
B0 7 100 |
Volume | ||
00 |
C1 46 |
C1 70 |
Patch change 70 = Bassoon | ||
00 |
FF 03 01 20 |
Track Name meta event <20 = space> | |||
0 |
91 36 45 |
91 54 69 |
Delta time -NON | ||
8360 |
81 36 0 |
81 54 0 |
Delta time -NOF | ||
0 |
91 32 4A |
91 50 74 |
etc. | ||
8360 |
81 32 0 |
81 50 0 |
|||
0 |
91 31 4A |
91 49 74 |
|||
8360 |
81 31 0 |
81 49 0 |
|||
0 |
91 2D 45 |
91 45 69 |
|||
8360 |
81 2D 0 |
81 45 0 |
|||
0 |
91 32 4D |
91 50 77 |
|||
8740 |
81 32 0 |
81 50 0 |
|||
1 |
FF 2F 00 |
REQUIRED - End of Track meta event |
Additional information about Standard MIDI Files can be found on the Reference page for Unit 4.