Derek Thompson
Derek Thompson
Sep 28, 2009
0 Comments | 3866 Views | 1 Hits


File Format:
Standard MIDI files provide a common file format used by most musical software and hardware devices to store song information. MIDI files contain one or more MIDI streams, with time information for each event. MIDI files make it possible to exchange information such as:
  1. Musical notes,
  2. Program changes,
  3. Expression control and channel information between musical instruments, sequencers, computers, lighting controllers and mixers.
MIDI files generally consist of serially listed channel and system messages which include at least program status messages and matching note on and note off messages. Song, sequence, and track structures, tempo and time signature information, are all supported. Track names and other descriptive information may be stored with the MIDI data. This format supports multiple tracks and multiple sequences so that if the user of a program which supports multiple tracks intends to move a file to another one, this format can allow that to happen.
Data Formats:
All data values are stored in Big-Endian (most significant byte first) format. Also, many values are stored in a variable-length format which may use one or more bytes per value. Variable-length values use the lower 7 bits of a byte for data and the top bit to signal a following data byte. If the top bit is set to 1, then another value byte follows.
A variable-length value may use a maximum of 4 bytes. This means the maximum value that can be represented is 0x0FFFFFFF (represented as 0xFF, 0xFF, 0xFF, 0x7F).
File Structure:
MIDI files are organized into data chunks. Each chunk is prefixed with an 8 byte header: 4 byte ID string used to identify the type of chunk followed by a 4 byte size which defines the chunk's length as number of bytes following this chunk's header.
Header Chunk
The header chunk contains information about the entire song including MIDI format type, number of tracks and timing division. There is only one header chunk per standard MIDI file and it always comes first. Before describing each element of the header chunk, here is a chart to help give an overview of the chunk's organization.
chunk ID
"MThd" (0x4D546864)
chunk size
6 (0x00000006)
format type
0 - 2
number of tracks
1 - 65,535
time division
see following text
MIDI Header Chunk Format
Here's the syntax of the complete chunk:
<chunk type> <length> <format> <ntrks> <division>
Chunk ID and Size:
The chunk ID is always "MThd" (0x4D546864) and the size is always 6 because the header chunk always contains the same 3 word values.
Format Type:
The first word describes the MIDI format type. It can be a value of 0, 1 or 2 and describes what how the following track information is to be interpreted.
Number of Tracks:
The second word simply defines the number of track chunks that follow this header chunk. A type 0 MIDI file may only contain a value of 1, because they can only contain one track. Type 1 and 2 MIDI files may contain up to 65,536 (0xFFFF) tracks.
Time Division
It contains the time division used to decode the track event delta times into "real" time. This value is represents either ticks per beat or frames per second. If the top bit of the word (bit mask 0x8000) is 0, the following 15 bits describe the time division in ticks per beat. Otherwise the following 15 bits (bit mask 0x7FFF) describe the time division in frames per second. Ticks per beat translate to the number of clock ticks or track delta positions (described in the Track Chunk section) in every quarter note of music. Common values range from 48 to 960, although newer sequencers go far beyond this range to ease working with MIDI and digital audio together. So a time division example of 0x9978 could be broken down into it's three parts: the top bit is one, so it is in SMPTE frames per second format, the following 7 bits have a value of 25 (0x19) and the bottom byte has a value of 120 (0x78). This means the example plays at 24 frames per second SMPTE time and has 120 ticks per frame.
Track Chunk
Track chunks contain all of the information for an individual track including, track name and music events. Here is an overview of a track chunk's organization.
chunk ID
"MTrk" (0x4D54726B)
chunk size
see following text
track event data (see following text)
MIDI Track Chunk Format
Chunk ID and Size
The chunk ID is always "MTrk" (0x4D54726B) and the size varies depending on the number of bytes used for all of the events contained in the track.
Track Event Data
The track event data contains a stream of MIDI events that define information about the sequence and how it is played.
MIDI Events
Track events are used to describe all of the musical content of a MIDI file, from tempo changes to sequence and track titles to individual music events. Each event includes a delta time, event type and usually some event type specific data.
The event delta time is defined by a variable-length value. It determines when an event should be played relative to the track's last event.
Types of Events
There are three types of events: MIDI Control Events, System Exclusive Events and Meta Events.
MIDI Channel Events

Musical control information such as playing a note or adjusting a MIDI channel's modulation value are defined by MIDI Channel Events.
Note Off Event
The Note Off Event is used to signal when a MIDI key is released.
Note On Event
The Note On Event is used to signal when a MIDI key is pressed.
Note Aftertouch Event
The Note Aftertouch Event is used to indicate a pressure change on one of the currently pressed MIDI keys.
Controller Event
The Controller Event signals the change in a MIDI channels state. There are 128 controllers which define different attributes of the channel including volume, pan, modulation, effects, and more.
Program Change Event
The Program Change Event is used to change which program (instrument/patch) should be played on the MIDI channel.
Pitch Bend Event
The Pitch Bend Event is similar to a controller event, except that it is a unique MIDI Channel Event that has two bytes to describe it's value. The pitch value is defined by both parameters of the MIDI Channel Event by joining them in the format of yyyyyyyxxxxxxx where the y characters represent the last 7 bits of the second parameter and the x characters represent the last 7 bits of the first parameter. The combining of both parameters enables high accuracy values (0 - 16383).
Meta Events
Events that are not to be sent or received over a MIDI port are called Meta Events. These events are defined by an event type value of 0xFF and have a variable size of parameter data which is defined after the event type.
Meta Event
255 (0xFF)
type specific
Meta Event Values
There are currently fifteen defined Meta Events. They are:
1. Sequence Number
2. Text Event
3. Copyright Notice
4. Sequence/Track Name
5. Instrument Name
6. Lyrics
7. Marker
8. Cue Point
9. MIDI Channel Prefix
10. End Of Track
11. Set Tempo
12. SMPTE Offset
13. Time Signature
14. Key Signature
15. Sequencer Specific
System Exclusive Events:
Also known as SysEx Events, these MIDI events are used to control MIDI hardware or software that require special data bytes that will follow their manufacturer's specifications. Every SysEx event includes an ID that specifies which manufacturer's product is to be the intended receiver (see the MIDI Manufacturers List). All other products will ignore the event. There are three types of SysEx messages which are used to send data in a single event, across multiple events or authorize the transmission of specific MIDI messages.
  1. Normal SysEx Events
  2. Divided SysEx Events
  3. Authorization SysEx Events 
C. Different types of Sound/Music file structure
Some common audio file formats are as follows:
1.   Windows Media (.wma)
2.   WAV Audio (.wav)
3.   VOC sound (.voc)
4.   Real audio (.rm)
5.   QCP Audio (.qcp)
6.   Ogg-Vorbis (.ogg)
7.   MPEG Audio stream (.mpeg)
8.   MP3 Audio (.mp3)
9.   mLaw Sound (.au)
10. Beatnik (rmf)
11. Amiga MED sound (.med)
12. AIFF Sound (.aiff)
Author's note: Midi File Format
Keywords: Midi File Format

Please Signup to comment on this article