MIDI File Format ================ By Nathan Laredo, last revision: 25 August 1996 NOTE: THIS IS NOT THE OFFICIAL MIDI FILE FORMAT SPECIFICATION. THESE ARE PERSONAL NOTES I WROTE WHEN I FIRST STARTED TO WRITE MY OWN MIDI FILE READING ROUTINES. THIS INFORMATION IS PROVIDED IN THE HOPE THAT IT MAY SAVE SOMEONE ELSE THE AMOUNT OF RESEARCH THAT WENT INTO IT. I MAKE NO GUARANTEES OR WARRANTIES PERTAINING TO USEFULNESS OR ACCURACY. ----------------------------------------------------------------------- (MThd = 0x4d546864) (32-bit big endian length = 6) (16-bit big endian format) (16-bit big endian tracks) (16-bit big-endian division) format is either 0 - one track, 1 - many tracks, one sequence or 2 - many tracks with one sequence per track. format 1 files should have all tempo changes in the first track (MTrk = 0x4d54726b) (32-bit big endian length of track) (variable-length encoded ticks since previous event) (Event data -- see below) ticks and event data are repeated for length bytes. MTrk block is repeated for as many tracks as indicated in header. ------------------------------------------------------------------------ Variable length quantities are a series of 7 bit values encoded from msb to lsb, with the lsb bit 7 clear, all prior have bit 7 set. ------------------------------------------------------------------------ Event data is either midi data (with running status) that is to be sent to the midi device, a sysex (two types), or a meta-event ------------------------------------------------------------------------- A sysex event contains information to be sent directly to the midi synth. It consists of (0xf0) (variable-length encoded length) (data after 0xf0) or (0xf7) (variable-length encoded length) (all data to be sent) Sysex messages may or may not be terminated with 0xf7 (EOX), EOX should not be added automagically when messages are output to a midi synth, as there may be a following 0xf7-type sysex after a delay that may be required by the synth. The 0xf7-type sysex may contain data > 0x7f so programs shouldn't stop sending when a byte value is > 0x7f. ------------------------------------------------------------------------- A meta event contaions information such as text, tempo, and key signature. It consists of (0xff) (type) (variable length encoded deta length) (data) Text is not usually null-terminated. You must depend on the length when reading it. meta event types range from 0x00 to 0x7f Type Contents ==== ======== 0x00: (16-bit big endian sequence number) 0x01: (any text) 0x02: (Copyright Message text) 0x03: (Sequence/Track Name text) 0x04: (Instrument Name text) 0x05: (Lyric text) 0x06: (Marker text) 0x07: (Cue-point text) 0x2f: End of Track -- no data for this type 0x51: (24-bit big endian tempo) -- microseconds per midi quarter note 0x54: (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start 0x58: (numerator) (denominator) (clocks per metronome click) (32nd notes notated per midi quarter note) -- Time Signature denominator is a power of two, ie 0x03 = 2^3 = 8 0x59: (sharps/flats) (major/minor flag) -- Key Signature if sharp key, first byte represents number of sharps if flat key, first byte represents negative number of flats the major/minor flag is 0 if minor, 1 if major. 0x7f: (Sequencer Specific data) -- I've never used this type, I ignore it. I have never seen it used, and If someone would let me know why it exists, I would appreciate it. ----------------------------------------------------------------------------- Many thanks to the authors of the many free midi references available on ftp.ucsd.edu that I used in creating this file format specification which I use for my Playmidi package for Linux. If you find any errors in this document, please email me. Nathan Laredo -- laredo@gnu.ai.mit.edu -- (C)1995, 1996 Nathan Laredo This document may be freely distributed in its original form.