| 1 |
MIDI File Format |
| 2 |
================ |
| 3 |
|
| 4 |
By Nathan Laredo, last revision: 25 August 1996 |
| 5 |
|
| 6 |
NOTE: THIS IS NOT THE OFFICIAL MIDI FILE FORMAT SPECIFICATION. THESE |
| 7 |
ARE PERSONAL NOTES I WROTE WHEN I FIRST STARTED TO WRITE MY OWN MIDI |
| 8 |
FILE READING ROUTINES. THIS INFORMATION IS PROVIDED IN THE HOPE THAT |
| 9 |
IT MAY SAVE SOMEONE ELSE THE AMOUNT OF RESEARCH THAT WENT INTO IT. I |
| 10 |
MAKE NO GUARANTEES OR WARRANTIES PERTAINING TO USEFULNESS OR ACCURACY. |
| 11 |
|
| 12 |
----------------------------------------------------------------------- |
| 13 |
(MThd = 0x4d546864) |
| 14 |
(32-bit big endian length = 6) (16-bit big endian format) |
| 15 |
(16-bit big endian tracks) (16-bit big-endian division) |
| 16 |
|
| 17 |
format is either 0 - one track, 1 - many tracks, one sequence |
| 18 |
or 2 - many tracks with one sequence per track. |
| 19 |
format 1 files should have all tempo changes in the first track |
| 20 |
|
| 21 |
(MTrk = 0x4d54726b) |
| 22 |
(32-bit big endian length of track) |
| 23 |
|
| 24 |
(variable-length encoded ticks since previous event) |
| 25 |
(Event data -- see below) |
| 26 |
|
| 27 |
ticks and event data are repeated for length bytes. |
| 28 |
|
| 29 |
MTrk block is repeated for as many tracks as indicated in header. |
| 30 |
------------------------------------------------------------------------ |
| 31 |
Variable length quantities are a series of 7 bit values encoded from |
| 32 |
msb to lsb, with the lsb bit 7 clear, all prior have bit 7 set. |
| 33 |
------------------------------------------------------------------------ |
| 34 |
Event data is either midi data (with running status) that is to be |
| 35 |
sent to the midi device, a sysex (two types), or a meta-event |
| 36 |
------------------------------------------------------------------------- |
| 37 |
A sysex event contains information to be sent directly to the midi synth. |
| 38 |
It consists of (0xf0) (variable-length encoded length) (data after 0xf0) |
| 39 |
or (0xf7) (variable-length encoded length) (all data to be sent) |
| 40 |
Sysex messages may or may not be terminated with 0xf7 (EOX), EOX should |
| 41 |
not be added automagically when messages are output to a midi synth, as |
| 42 |
there may be a following 0xf7-type sysex after a delay that may be |
| 43 |
required by the synth. The 0xf7-type sysex may contain data > 0x7f so |
| 44 |
programs shouldn't stop sending when a byte value is > 0x7f. |
| 45 |
------------------------------------------------------------------------- |
| 46 |
A meta event contaions information such as text, tempo, and key signature. |
| 47 |
It consists of (0xff) (type) (variable length encoded deta length) (data) |
| 48 |
Text is not usually null-terminated. You must depend on the length |
| 49 |
when reading it. |
| 50 |
|
| 51 |
meta event types range from 0x00 to 0x7f |
| 52 |
Type Contents |
| 53 |
==== ======== |
| 54 |
0x00: (16-bit big endian sequence number) |
| 55 |
0x01: (any text) |
| 56 |
0x02: (Copyright Message text) |
| 57 |
0x03: (Sequence/Track Name text) |
| 58 |
0x04: (Instrument Name text) |
| 59 |
0x05: (Lyric text) |
| 60 |
0x06: (Marker text) |
| 61 |
0x07: (Cue-point text) |
| 62 |
0x2f: End of Track -- no data for this type |
| 63 |
0x51: (24-bit big endian tempo) -- microseconds per midi quarter note |
| 64 |
0x54: (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start |
| 65 |
0x58: (numerator) (denominator) (clocks per metronome click) |
| 66 |
(32nd notes notated per midi quarter note) -- Time Signature |
| 67 |
denominator is a power of two, ie 0x03 = 2^3 = 8 |
| 68 |
0x59: (sharps/flats) (major/minor flag) -- Key Signature |
| 69 |
if sharp key, first byte represents number of sharps |
| 70 |
if flat key, first byte represents negative number of flats |
| 71 |
the major/minor flag is 0 if minor, 1 if major. |
| 72 |
0x7f: (Sequencer Specific data) -- I've never used this type, I ignore it. |
| 73 |
I have never seen it used, and If someone would let me know why it |
| 74 |
exists, I would appreciate it. |
| 75 |
----------------------------------------------------------------------------- |
| 76 |
Many thanks to the authors of the many free midi references available on |
| 77 |
ftp.ucsd.edu that I used in creating this file format specification which |
| 78 |
I use for my Playmidi package for Linux. |
| 79 |
|
| 80 |
If you find any errors in this document, please email me. |
| 81 |
|
| 82 |
Nathan Laredo -- [email protected] |
| 83 |
|
| 84 |
-- |
| 85 |
(C)1995, 1996 Nathan Laredo |
| 86 |
This document may be freely distributed in its original form. |