home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / MIDISTUF.ZIP / mid_form.txt < prev    next >
Text File  |  1995-01-01  |  12KB  |  254 lines

  1.  
  2.                            Standard MIDI File Format
  3.                                 Dustin Caldwell
  4.  
  5.       The standard MIDI file format is a very strange beast. When viewed as a
  6. whole, it can be quite overwhelming. Of course, no matter how you look at it,
  7. describing a piece of music in enough detail to be able to reproduce it
  8. accurately is no small task. So, while complicated, the structure of the midi
  9. file format is fairly intuitive when understood. 
  10.       I must insert a disclaimer here that I am by no means an expert with
  11. midi nor midi files. I recently obtained a Gravis UltraSound board for my PC,
  12. and upon hearing a few midi files (.MID) thought, "Gee, I'd like to be able to
  13. make my own .MID files." Well, many aggravating hours later, I discovered that
  14. this was no trivial task. But, I couldn't let a stupid file format stop me.
  15. (besides, I once told my wife that computers aren't really that hard to use,
  16. and I'd hate to be a hypocrite) So if any errors are found in this
  17. information, please let me know and I will fix it. Also, this document's scope
  18. does not extend to EVERY type of midi command and EVERY possible file
  19. configuration. It is a basic guide that should enable the reader (with a
  20. moderate investment in time) to generate a quality midi file.
  21.  
  22. 1. Overview
  23.  
  24.       A midi (.MID) file contains basically 2 things, Header chunks and Track
  25. chunks. Section 2 explains the header chunks, and Section 3 explains the track
  26. chunks. A midi file contains ONE header chunk describing the file format,
  27. etc., and any number of track chunks. A track may be thought of in the same
  28. way as a track on a multi-track tape deck. You may assign one track to each
  29. voice, each staff, each instrument or whatever you want. 
  30.  
  31. 2. Header Chunk
  32.  
  33.       The header chunk appears at the beginning of the file, and describes the
  34. file in three ways. The header chunk always looks like:
  35.  
  36. 4D 54 68 64 00 00 00 06 ff ff nn nn dd dd
  37.  
  38. The ascii equivalent of the first 4 bytes is MThd. After MThd comes the 4-byte
  39. size of the header. This will always be 00 00 00 06, because the actual header
  40. information will always be 6 bytes. 
  41.  
  42. ff ff is the file format. There are 3 formats:
  43.  
  44. 0 - single-track 
  45. 1 - multiple tracks, synchronous
  46. 2 - multiple tracks, asynchronous
  47.  
  48. Single track is fairly self-explanatory - one track only. Synchronous multiple
  49. tracks means that the tracks will all be vertically synchronous, or in other
  50. words, they all start at the same time, and so can represent different parts
  51. in one song. Asynchronous multiple tracks do not necessarily start at the same
  52. time, and can be completely asynchronous. 
  53.  
  54. nn nn is the number of tracks in the midi file.
  55.  
  56. dd dd is the number of delta-time ticks per quarter note. (More about this
  57. later)
  58.  
  59.  
  60. 3. Track Chunks
  61.  
  62. The remainder of the file after the header chunk consists of track chunks.
  63. Each track has one header and may contain as many midi commands as you like.
  64. The header for a track is very similar to the one for the file:
  65.  
  66. 4D 54 72 6B xx xx xx xx
  67.  
  68. As with the header, the first 4 bytes has an ascii equivalent. This one is
  69. MTrk. The 4 bytes after MTrk give the length of the track (not including the
  70. track header) in bytes. 
  71.       Following the header are midi events. These events are identical to the
  72. actual data sent and received by MIDI ports on a synth with one addition. A
  73. midi event is preceded by a delta-time. A delta time is the number of ticks
  74. after which the midi event is to be executed. The number of ticks per quarter
  75. note was defined previously in the file header chunk. This delta-time is a
  76. variable-length encoded value. This format, while confusing, allows large
  77. numbers to use as many bytes as they need, without requiring small numbers to
  78. waste bytes by filling with zeros. The number is converted into 7-bit bytes,
  79. and the most-significant bit of each byte is 1 except for the last byte of the
  80. number, which has a msb of 0. This allows the number to be read one byte at a
  81. time, and when you see a msb of 0, you know that it was the last (least
  82. significant) byte of the number. According to the MIDI spec, the entire delta-
  83. time should be at most 4 bytes long. 
  84.       Following the delta-time is a midi event. Each midi event (except a
  85. running midi event) has a command byte which will always have a msb of 1 (the
  86. value will be >= 128). A list of most of these commands is in appendix A. Each
  87. command has different parameters and lengths, but the data that follows the
  88. command will have a msb of 0 (less than 128). The exception to this is a meta-
  89. event, which may contain data with a msb of 1. However, meta-events require a
  90. length parameter which alleviates confusion. 
  91.       One subtlety which can cause confusion is running mode. This is where
  92. the actual midi command is omitted, and the last midi command issued is
  93. assumed. This means that the midi event will consist of a delta-time and the
  94. parameters that would go to the command if it were included. 
  95.  
  96. 4. Conclusion
  97.  
  98.       If this explanation has only served to confuse the issue more, the
  99. appendices contain examples which may help clarify the issue. Also, 2
  100. utilities and a graphic file should have been included with this document: 
  101.  
  102. DEC.EXE - This utility converts a binary file (like .MID) to a tab-delimited
  103. text file containing the decimal equivalents of each byte.
  104.  
  105. REC.EXE - This utility converts a tab-delimited text file of decimal values
  106. into a binary file in which each byte corresponds to one of the decimal
  107. values.
  108.  
  109. MIDINOTE.PS - This is the postscript form of a page showing note numbers with
  110. a keyboard and with the standard grand staff.
  111.                                                                           Appendix A
  112.  
  113. 1. MIDI Event Commands
  114.  
  115. Each command byte has 2 parts. The left nybble (4 bits) contains the actual
  116. command, and the right nybble contains the midi channel number on which the
  117. command will be executed. There are 16 midi channels, and 8 midi commands (the
  118. command nybble must have a msb of 1).
  119. In the following table, x indicates the midi channel number. Note that all
  120. data bytes will be <128 (msb set to 0).
  121.  
  122. Hex      Binary       Data          Description
  123. 8x       1000xxxx     nn vv         Note off (key is released)
  124.                                     nn=note number
  125.                                     vv=velocity
  126.  
  127. 9x       1001xxxx     nn vv         Note on (key is pressed)
  128.                                     nn=note number
  129.                                     vv=velocity
  130.  
  131. Ax       1010xxxx     nn vv         Key after-touch
  132.                                     nn=note number
  133.                                     vv=velocity
  134.  
  135. Bx       1011xxxx     cc vv         Control Change
  136.                                     cc=controller number
  137.                                     vv=new value
  138.  
  139. Cx       1100xxxx     pp            Program (patch) change
  140.                                     pp=new program number
  141.  
  142. Dx       1101xxxx     cc            Channel after-touch
  143.                                     cc=channel number
  144.  
  145. Ex       1110xxxx     bb tt         Pitch wheel change (2000H is normal or no
  146.                                     change)
  147.                                     bb=bottom (least sig) 7 bits of value
  148.                                     tt=top (most sig) 7 bits of value
  149. The following table lists meta-events which have no midi channel number. They
  150. are of the format:
  151.  
  152. FF xx nn dd
  153.  
  154. All meta-events start with FF followed by the command (xx), the length, or
  155. number of bytes that will contain data (nn), and the actual data (dd).
  156.  
  157. Hex      Binary       Data          Description
  158. 00       00000000     nn ssss       Sets the track's sequence number.
  159.                                     nn=02 (length of 2-byte sequence number)
  160.                                     ssss=sequence number
  161.  
  162. 01       00000001     nn tt ..      Text event- any text you want.
  163.                                     nn=length in bytes of text
  164.                                     tt=text characters
  165.  
  166. 02       00000010     nn tt ..      Same as text event, but used for
  167.                                     copyright info.
  168.                                     nn tt=same as text event
  169.  
  170. 03       00000011     nn tt ..      Sequence or Track name
  171.                                     nn tt=same as text event
  172.  
  173. 04       00000100     nn tt ..      Track instrument name
  174.                                     nn tt=same as text event
  175.  
  176. 05       00000101     nn tt ..      Lyric
  177.                                     nn tt=same as text event
  178.  
  179. 06       00000110     nn tt ..      Marker
  180.                                     nn tt=same as text event
  181.  
  182. 07       00000111     nn tt ..      Cue point
  183.                                     nn tt=same as text event
  184.  
  185. 2F       00101111     00            This event must come at the end of each
  186.                                     track
  187.  
  188. 51       01010001     03 tttttt     Set tempo
  189.                                     tttttt=microseconds/quarter note
  190.  
  191. 58       01011000     04 nn dd ccbb Time Signature
  192.                                     nn=numerator of time sig.
  193.                                     dd=denominator of time sig. 2=quarter
  194.                                     3=eighth, etc.
  195.                                     cc=number of ticks in metronome click
  196.                                     bb=number of 32nd notes to the quarter
  197.                                     note
  198.  
  199. 59       01011001     02 sf mi      Key signature
  200.                                     sf=sharps/flats (-7=7 flats, 0=key of C,
  201.                                     7=7 sharps)
  202.                                     mi=major/minor (0=major, 1=minor)
  203.  
  204. 7F       01111111     xx dd ..      Sequencer specific information
  205.                                     xx=number of bytes to be sent
  206.                                     dd=data
  207. The following table lists system messages which control the entire system.
  208. These have no midi channel number. (these will generally only apply to
  209. controlling a midi keyboard, etc.)
  210.  
  211. Hex      Binary       Data          Description
  212. F8       11111000                   Timing clock used when synchronization is
  213.                                     required.
  214.  
  215. FA       11111010                   Start current sequence
  216.  
  217. FB       11111011                   Continue a stopped sequence where left
  218.                                     off
  219.  
  220. FC       11111100                   Stop a sequence
  221.  
  222.  
  223. The following table lists the numbers corresponding to notes for use in note 
  224. on and note off commands.
  225.  
  226.  
  227. Octave||                     Note Numbers
  228.    #  ||
  229.       || C   | C#  | D   | D#  | E   | F   | F#  | G   | G#  | A   | A#  | B
  230. -----------------------------------------------------------------------------
  231.    0  ||   0 |   1 |   2 |   3 |   4 |   5 |   6 |   7 |   8 |   9 |  10 | 11
  232.    1  ||  12 |  13 |  14 |  15 |  16 |  17 |  18 |  19 |  20 |  21 |  22 | 23
  233.    2  ||  24 |  25 |  26 |  27 |  28 |  29 |  30 |  31 |  32 |  33 |  34 | 35
  234.    3  ||  36 |  37 |  38 |  39 |  40 |  41 |  42 |  43 |  44 |  45 |  46 | 47
  235.    4  ||  48 |  49 |  50 |  51 |  52 |  53 |  54 |  55 |  56 |  57 |  58 | 59
  236.    5  ||  60 |  61 |  62 |  63 |  64 |  65 |  66 |  67 |  68 |  69 |  70 | 71
  237.    6  ||  72 |  73 |  74 |  75 |  76 |  77 |  78 |  79 |  80 |  81 |  82 | 83
  238.    7  ||  84 |  85 |  86 |  87 |  88 |  89 |  90 |  91 |  92 |  93 |  94 | 95
  239.    8  ||  96 |  97 |  98 |  99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107
  240.    9  || 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119
  241.   10  || 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 |
  242.  
  243.  
  244.                                 BIBLIOGRAPHY
  245.  
  246.   "MIDI Systems and Control" Francis Rumsey  1990 Focal Press
  247.  
  248.   "MIDI and Sound Book for the Atari ST" Bernd Enders and Wolfgang Klemme
  249.           1989 M&T Publishing, Inc.
  250.  
  251.   MIDI file specs and general MIDI specs were also obtained by sending e-mail
  252.          to LISTSERV@AUVM.AMERICAN.EDU with the phrase GET MIDISPEC PACKAGE
  253.          in the message.
  254.