Standard Midi FIle Format ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ Standard Midi Files consist of a sequence of "chunks". The first chunk is call the Header Chunk; and all subsequent chunks are called Track Chunks. The Header Chunk ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ header chunk = "MThd" // ch4(length) // ch2(format) // ch2(n) // ch2(division) length = 6 (for the header chunk) format = 0, 1, or 2, for single track (MIDI0), multiple track (MIDI1), or multiple song file formats, respectively n = number tracks that follow division = midi ticks per musical beat (usually a quarter note) Example: top of Romeo file for Bach Trio Sonate 1: Mvt 1 file 0 &dA4d54 6864&d@ &dI0000 0006&d@ &dK0001&d@ &dK0006&d@ &dK00f0&d@ 4d54 726b 0000 "MThd" ch4(6) 1 6 240 The Track Chunk ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ A track chunk consists of a literal identified string, a length indicator specifying the number of bytes which follow in this track, and actual event data in the track. track chunk = "MTrk" // ch4(length) // event1 // event2 // ... Track Event ÄÄÄÄÄÄÄÄÄÄÄ A track event consists of a delta time since the last event, and one or three types of events; Midi-event, Meta-event, and Sysex-event event = v_time // (Midi-event | Meta-event | Sysex-event) Midi event ÄÄÄÄÄÄÄÄÄÄ This is any MIDI channel message such as note-on or note-off. Running status is used in the same manner as it is used between MIDI devices. For note on and note off events, the Midi-event is a 3 byte string Byte 1: high half = command low half = channel 8 = note off 0 - 15 9 = note on Byte 2: pitch, a number between 1 and 127 60 = middle C Byte 3: velocity, a number between 1 and 127 0 = note off For program change events, the Midi-event is a 2 byte string Byte 1: high half = command low half = channel 12 = program change 0 - 15 Byte 2: instrument number, 0 to 127 representing instruments 1 to 128 For other Midi-events ? 10 = 11 = 13 = 14 = 15 = Meta event ÄÄÄÄÄÄÄÄÄÄ Meta events are non-MIDI data of various sorts consisting of a fixed prefix type indicator, a length field, and actual event data Meta-event = chr(ff) // chr(type) // v_lng // data selected values of type: type event 0x00 sequence number 0x01 text event 0x02 Copyright notice 0x03 sequence or track name 0x04 instrument name 0x05 lyric text 0x06 marker text 0x07 cue point 0x20 MIDI channel prefix assignment 0x2f End of track 0x51 tempo setting (microseconds per MIDI quarter-note, or 1/24th microseconds per MIDI clock) 0x54 SMPTE offset 0x58 time signature (4 bytes, n, d, c, b n = numerator of time sig; d = power of 2 of denominator of time sig c = MIDI clocks per metronome click d = 32nd notes in a MIDI quarter note 0x59 key signature (2 bytes, k and m k = key (-7 to +7) m = mode (0 = major, 1 = minor) 0x7f sequencer specific event Variable Length Values Two parameters in this discription (so far) have been given in the variable length value format: v_time and v_lng. This format works as follows: Take the number to be expressed. Espress it as a series of bits. Group the bits in groups of &dAseven&d@, with the lower order group having the low order seven bits, etc. The number of groups will be the number of bytes in the format. Each byte except the last will have the first (high order) bit set. Thus the number 12390293 would be expressed as follows: 12390293 = 0xbd0f95 = 1011 1101 0000 1111 1001 0101 = 101 1110100 0011111 0010101 = 1000 0101 1111 0100 1001 1111 0001 0101 = 0x85 0xf4 0x9f 0x15 (four bytes) In the MIDI0 format, there can only be one track. Therefore, this track must contain all necessary Meta-events, such as temp, SMPTE, time sig. as well as any legal or naming information (text). These data will then be followed by the musical data in the order it occurs. Note that MIDI0 is not restricted to one channel. The various channels can be assigned to the various instrumental sounds via the "program change" command (0xc#) at the beginning of the music. This leaves us the option of creating two types of MIDI0 formats; namely MIDI0 collapsed to one channel and MIDI0 sent to multiple channels. The MIDI1 format consists of multiple tracks. By convention, the first Track chunk should contain, amoung other things the tempo settings, the SMPTE value and the time signature(s). Some examples from Romeo files follow, with expanations. Example: Romeo file for Bach Trio Sonate 1: Mvt 1 file Header chunk: &dA4d54 6864&d@ &dI0000 0006&d@ &dK0001&d@ &dK0006&d@ &dK00f0&d@ "MThd" ch4(6) 1 6 240 1 = MIDI1 format 6 = six track chunks in this file 240 = 240 units (divisions) per quarter note Track chunk1: &dA4d54 726b&d@ &dI0000 001c&d@ "MTrk" ch4(28) &dL00&d@ &dGff5804&d@ ff58 = time signature 04 = length 4 t &dK0402&d@ 4 / 4 time 2 &dK1808&d@ 24 MIDI ckocks per metronome; 2 8 32nd notes per quarter. &dL00&d@ &dGff5103&d@ ff51 = tempo setting 03 = length 4 t &dK0e4e1c&d@ = 937,500 u-secs per q. (q = 64.00) 3 &dL00&d@ &dGff5405&d@ ff54 = SMPTE value 05 = length 4 t &dK40 0000 0000&d@ = start track after 64 hours! 5 &dL00&d@ &dGff2f00&d@ ff2f = end of track 00 = length 4 t ÄÄÄÄ 28 Track chunk2: &dA4d54 726b&d@ &dI0000 0016&d@ "MTrk" ch4(22) &dL00&d@ &dGff030e&d@ ff03 = sequence or track name 0e = len 4 t &dK2843 2920 2620 2850 2920 3139 3932&d@ 14 "(C) & (P) 1992" &dL7f&d@ &dGff2f00&d@ ff2f = end of track 00 = length 4 t ÄÄÄÄ 22 Track chunk3: &dA4d54 726b&d@ &dI0000 0019&d@ "MTrk" ch4(25) &dL00&d@ &dGff0311&d@ ff03 = sequence or track name 11 = len 4 t &dK526f 6d65 6f20 4d75 7369 6320 496e 746c 2e&d@ 17 "Romeo Music Intl." &dL00&d@ &dGff2f00&d@ ff2f = end of track 00 = length 4 t ÄÄÄÄ 25 Track chunk4: &dA4d54 726b&d@ &dI0000 149e&d@ "MTrk" ch4(5278) &dL00&d@ &dGff0306&d@ ff03 = sequence or track name 06 = len 4 t &dK5061 7274 2031&d@ 6 "Part 1" &dL00&d@ &dGc013&d@ program change: 3 t channel 0 = instrument 20 &dL8f00&d@ &dG90465a&d@ note on: channel 0 5 t = 1920 pitch = Bf4 (8 quarters) velocity = 90 &dL76&d@ &dG80465a&d@ note off: channel 0 4 t = 118 pitch = Bf4 velocity = 90 &dL02&d@ &dG904a5a&d@ note on: channel 0 4 t = 2 (+118 = eighth pitch = D5 note later) velocity = 90 etc. &dL816d&d@ &dG804b5a&d@ note off: channel 0 (last note) t = 237 pitch = Ef5 (quarter note) velocity = 90 &dL00&d@ &dGff2f00&d@ ff2f = end of track 00 = length 4 Track chunk5: &dA4d54 726b&d@ &dI0000 1503&d@ "MTrk" ch4(5379) &dL00&d@ &dGff0306&d@ ff03 = sequence or track name 06 = len 4 t &dK5061 7274 2032&d@ 6 "Part 2" &dL00&d@ &dGc113&d@ program change: 3 t channel 1 = instrument 20 &dL00&d@ &dG913f5a&d@ note on: channel 1 4 t pitch = Ef4 velocity = 90 &dL76&d@ &dG813f5a&d@ note off: channel 1 4 t = 118 pitch = Ef4 velocity = 90 &dL02&d@ &dG91435a&d@ note on: channel 1 4 t = 2 (+118 = eighth pitch = G4 note later) velocity = 90 etc. &dL816d&d@ &dG814&dC3&dG5a&d@ note off: channel 1 (last note) t = 237 pitch = G4 (quarter note) velocity = 90 &dL00&d@ &dGff2f00&d@ ff2f = end of track 00 = length 4 Track chunk6: &dA4d54 726b&d@ &dI0000 0aa4&d@ "MTrk" ch4(2724) &dL00&d@ &dGff0306&d@ ff03 = sequence or track name 06 = len 4 t &dK5061 7274 2033&d@ 6 "Part 3" &dL00&d@ &dGc213&d@ program change: 3 t channel 2 = instrument 20 &dL00&d@ &dG92335a&d@ note on: channel 2 4 t pitch = Ef3 velocity = 90 &dL816d&d@ &dG82335a&d@ note off: channel 2 4 t = 237 pitch = Ef3 (quarter note) velocity = 90 &dL3f&d@ &dG92375a&d@ note on: channel 2 4 t = 63 pitch = G3 (16th rest) velocity = 90 etc. &dL835a&d@ &dG82275a&d@ note off: channel 2 (last note) t = 474 pitch = Ef2 (half note) velocity = 90 &dL00&d@ &dGff2f00&d@ ff2f = end of track 00 = length 4 End of File