MUSC 108. Introduction to Music Technology - Fall 2013

04 Standard MIDI Files (SMF)

[Overview] [Syllabus]

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:

  1. all values are in hex
  2. PPQ times are stored in a variable length data format called Delta Times.

SMF Structure

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.

SMF structure

The remainder of this document explains the SMF structure in more detail

SMF file Example

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.

04 Class Example Score

Unable to play MP3

Hex Editors

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.

HexEdit display

Track Markers

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.

Tracks in Hex Viewq

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.

Excel color coded SMF contents   

Legend

Legend

MThd - The Header chunk

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
Number of Bytes
Value
Notes
Marker
4
MThd
All SMF's begin with MThd.
Length of data
4
6
The length is always 6 for the Header Chunk.
Type of SMF
2
0 or 1 or 2
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
2
nn
The number of individual tracks in this SMF.
PPQ
2
nn
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
4

MThd

MThd

Length of data
4
MThd_length

All header chunks use 6 bytes of data

Type of SMF
2
MThd data 1

1 = Type 1 SMF

Number of Tracks
2
MThd data 2

3 = 3 tracks in this file

PPQ
2
MThd data 3

1E0 hex = 480 dec

MTrk - MIDI Track Chunks

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
Value
Number of Bytes
Notes
Marker
MTrk
4
Every track begins with MTrk.
Length of data
nn
4
The number of bytes to follow

<Delta Time> <Track Event>

varies
varies

The rest of the track consists of any number
of <Delta Time> <Track Event> pairs.

Delta Time

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.

Track Event

A Track Event follows every Delta Time. There are three types of track events:

  1. MIDI Event - NON, NOF messages, patch changes, volume, pan, pitch bends, etc. This is the data you've been using in MIDIDisplay.
  2. Meta Events - Special codes for things like: tempo, key signature, title, track name, copyright, lyrics, comments, and the required End Of Track event. You'll be using some of these meta events in your MIDI project.
  3. Sysex Event - System Exclusive messages. Sysex messages can be used to dump internal settings of the synthesizer to the computer for storage and later retrieval. We will not be covering Sysex events in this class.

Track 1 - The Tempo track or Conductor track

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
1-4
Variable length PPQ data
 

<meta event>= FF + <meta type> + <variable length data > + <data bytes>

Marker
FF
2
All Meta Events start with $FF
Meta Type
nn
2

Several types of Meta Events are defined below.

Variable Length Data
nn
1-4 bytes
Represents the length of the following data in
as few bytes as possible.
Data Bytes
nn
varies
Specific to the meta event type

This is the track 1 data.

Track 1 The Tempo Track
Hex
Decimal
Legend
Conductor track in hex Conductor track in decimal Legend Track 1

Here's a detailed explanation of the tempo track.

04ClassExample Track 1
Subsection Bytes Delta Time Data Hex Notes
Marker
4
not used MTrk in hex MTrk, the start of a track chunk
Length of data
4
not used Length in hex length in bytes = 19(hex) or 25 (decimal)
 
Delta time Time signature meta event color $58 is Meta Event for Time Signature
 
Delta time Key Signature meta event in color $59 Meta Event for Key Signature
    Delta time Set Tempo meta event colored $51 Meta Event for Set Tempo
    Delta time of 1 in color End of track meta event colored $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.

  1. The first byte is FF.
  2. The second byte indicates the type of meta event.
  3. The third byte indicates the number of bytes to follow. (It could be a variable length number and use more than one byte.)
  4. The remaining data is specific to the meta event

You'll be using meta events for the time signature, key signature and tempo in your project.

Time Signature Meta Event

 
Byte 1
Type
Length
Data
Data Interpretation
Time signature meta event color
FF
58
4
4 2 18 8

4 = numerator of Time Signature

2 = denominator of Time Signature expressed as a power of 2.
2 to the power 2 = 4.

18 or (24 decimal) MIDI clocks per metronome click

8 = 8 thirty second notes per quarter note

Key Signature Meta Event

 
Byte 1
Type
Length
Data
Data Interpretation
Key Signature meta event in color
FF
59
2
2 0

2 = two sharps in the key signature

0 = major key

Tempo Meta Event

 
Byte 1
Type
Length
Data Bytes
Data Interpretation
Set Tempo meta event colored
FF 51
51
3
0F 42 40

tt tt tt = time in microseconds per quarter note
converted to three hex digits

microseconds = 1,000,000 * 60 / tempo

tempo = 1,000,000*60 / microseconds

0F 42 40 (hex) = 1,000,000 ( decimal)
tempo = 60000000/1000000 = 60 beats per minute

A complete list of meta events can be found in the SMF readings on Reference page.

MIDI Tracks 2-17

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
Value
Number of Bytes
Notes
Marker
MTrk
4
Every track begins with MTrk.
Length of data
nn
4
The number of bytes to follow
MIDI Events
varies
varies

Any number of MIDI events

End of Track Marker
FF 2F 00
varies
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
Value
Number of Bytes
Notes
Delta Time
varies
varies 1-4
PPQ values
 
MIDI Message = <Status> <data 1> <data 2>
Status
$80 - $FF
1
status byte
Data 1
0-127
1
data1 byte
Data 2
0-127
1
data2 byte

Track 2 - The first MIDI data track

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
Track 2 hex
Track 2 decimal
Legend track 2

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
4
not used
4D 54 72 6B
MTrk
Length of data
4
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

Track 3

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
Track 3 hex Track3 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
Format

Notes
Marker
4
not used
4D 54 72 6B
MTrk
Length of data
4
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.

Delta Time Code in C For Programmers

Delta time C code

[Overview] [Syllabus]

Revised John Ellinger, January - September 2013