home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 10: Diskmags / nf_archive_10.iso / MAGS / ST_USER / 1990 / USERDC90.MSA / TEXT_MIDI.TXT < prev    next >
Text File  |  1990-10-07  |  19KB  |  451 lines

  1.  
  2.       ---------------------------------------------------------
  3.       Over the last few years the Standard MIDI File format 
  4.       has undergone several revisions and, not surprisingly, 
  5.       this has led to some confusion amongst both programmers 
  6.       and MIDI File users. In this first part of a two-part 
  7.       article Paul Overaa examines the current standard in 
  8.       detail !
  9.       ---------------------------------------------------------
  10.  
  11. Standard MIDI Files (often called SMF files) are used primarily to transfer
  12. time-stamped MIDI data between different software packages. These files have
  13. been around for quite a time and those of you who are established 'MIDI-
  14. ites' will no doubt remember the many early MIDI programs (such as Opcode's 
  15. Sequencer 2.5, Intelligent Music's Jam Factory and Steinburg's Pro 24) which
  16. were quick to lend their support to the idea of such a common, portable, 
  17. file format. Today a great many software packages provide some level of 
  18. MIDI file support.
  19.  
  20. Unfortunately things have gone less than smoothly on the MIDI file 
  21. development front. To start with the file format initially proposed was 
  22. limited to just a single stream of MIDI data. This was felt by many to be 
  23. too restrictive but nowadays, the MIDI File standard, under the control of 
  24. the International MIDI Association, has grown to allow track information,
  25. multiple streams, sequencer specific messages, and a host of other new event 
  26. types. The penalty we've had to pay for these new benefits is that the 
  27. standard has changed significantly from that which was originally proposed.
  28. It is obvious from the questions we get asked about MIDI files that most 
  29. MIDI users and ST programmers are not aware of exactly how the MIDI File 
  30. specification has changed. In this two-part mini-series we'll attempt to 
  31. put this right... firstly by looking at the MIDI File standard itself, and 
  32. secondly by looking at some of the programming tricks needed to interpret 
  33. the contents of such files.
  34.  
  35. At the highest level MIDI Files consist of identifiable blocks of data 
  36. called 'chunks'. Each chunk consists of a 4 character identifier followed 
  37. by a 32 bit number which specifies the byte-length of the data held in the 
  38. chunk, ie all chunks adopt this type of arrangement...
  39.  
  40.  
  41.    Chunk  <chunk-identifier> <chunk-size> <actual chunk data>
  42.  
  43.          4 Bytes       4 Bytes   chunk-size bytes
  44.  
  45.  
  46. At the moment only two types of chunks are defined: Header chunks which 
  47. have a 'MThd' identifier, and track chunks which have a 'MTrk' identifier.
  48. It is however highly likely that new chunk types will come into existence 
  49. in years to come... so any programs which read SMF files have to assume 
  50. that they WILL, one day, come across chunks which they cannot interpret. 
  51. The idea then, if you are writing your own software, is to write programs 
  52. which look at the chunk identifiers and skip over any chunks that cannot 
  53. be recognised. It's easy to do and we'll look at some typical chunk reading
  54. code next month !
  55.  
  56. The idea of files consisting of identifiable chunks which may be used or 
  57. skipped over is similar to that used by Electronic Art's IFF format. 
  58. There are however a couple of important differences: Firstly, the SMF 
  59. arrangement does NOT support the idea of nested chunks - so the SMF 
  60. read/write routines are much simpler than the equivalent IFF routines.
  61. Secondly, SMF chunks are not padded to an even number of bytes like IFF 
  62. files. To be honest it would have been better for 68000 programmers if 
  63. they were because if the first chunk in memory started word-aligned then 
  64. all subsequent chunks would then also be word-aligned. Why would that 
  65. help ? It's because it leads to tidier code based on the adoption of
  66. structure based chunk access to all SMF chunks. As it is you can use some
  67. structure based code, but you do have to be careful to avoid inadvertently
  68. causing 68000 addressing errors. 
  69.  
  70.  
  71. Overall Chunk Arrangements
  72. --------------------------
  73.  
  74. At the moment the two chunk types (Header and Track chunks) can be arranged
  75. in three ways, and this leads to three types of MIDI files: 
  76.  
  77. Format 0 type files contain a header chunk followed by a single track 
  78. chunk - It's the simplest and most portable of all the MIDI file 
  79. arrangements and is used for storing a sequence as a single stream of MIDI 
  80. file events.
  81.  
  82. Format 1 type files allow multiple simultaneous track sequences to be 
  83. handled. Such files will contain a header chunk followed by any number of 
  84. separate track chunks.
  85.  
  86. Format 2 files allow sets of independent sequences to be stored. A sequencer
  87. might save the individual sequences (intro, verse, chorus etc.) which make
  88. up a complete song as a single format 2 type MIDI file. 
  89.  
  90. Figure 1 shows the differences between these files types graphically. 
  91.  
  92.  
  93.                         ++++++++ header chunk
  94.    
  95.                         ******** track chunk
  96.  
  97.  
  98.                 +++++++++++++
  99. +++++++++++++          *** Trk 1 ***
  100. *** Trk 1 ***          *** Trk 2 ***  
  101.                 *** Trk 3 ***
  102.  
  103. Type 0 files hold      Type 1 files hold chunks of      
  104.  a single track       parallel track information.
  105.  of sequence data.          
  106.         
  107.  
  108.  
  109.    +++++++++++... *** Seq 1***... *** Seq 2 ***... *** Seq 3 *** etc.
  110.  
  111.      Type 2 files can handle sets of independent sequences by 
  112.        treating each track chunk as a separate sequence. 
  113.  
  114.  
  115.     Figure 1: Three types of MIDI files are currently supported.
  116.  
  117.  
  118.  
  119.  
  120.  
  121. The standard guarantees that all SMF files will start with a header 
  122. chunk, and that even if this header chunk is extended existing fields 
  123. will not be re-arranged. Programs can therefore assume that even though 
  124. they may find header chunks larger than they anticipated the fields 
  125. defined to-date will remain in the same relative positions.  
  126.  
  127.  
  128. Header Chunks
  129. -------------
  130.  
  131. As we've said, the 'MThd' header chunk is always the first chunk in a 
  132. MIDI file. Like all chunks these start with the identifier followed by 
  133. four bytes which specify the chunk's size. Current header chunks have six 
  134. bytes of data... the first word gives the file format (0,1, or 2), the 
  135. second word tells you how many track chunks are present in the file, and 
  136. the last word contains timing division information. The contents therefore 
  137. take this form...
  138.  
  139.  
  140.     4 Bytes     'MThd' identifier
  141.     4 Bytes     Byte size of following data (currently 6) 
  142.     2 Bytes     MIDI file type (0, 1 or 2)
  143.     2 Bytes     Number of Tracks (always 1 for file type 0)
  144.     2 Bytes     Division information
  145.  
  146. Interpretation-wise the only tricky item is the 'division' field because 
  147. it's contents and format may vary. If bit 15, ie the most significant bit, 
  148. is zero then bits 14-0 give a 15 bit number which specifies how many 
  149. delta-time ticks make up a crotchet...
  150.  
  151.  
  152.     bit 15   bit 14 -------------------- bit 0
  153.  
  154.     0         Ticks per crotchet
  155.  
  156.  
  157.  
  158. If bit 15 is set to 1 it indicates that a time-code based time is being 
  159. specified. In this case bits 14-8 give a negative number (two's complement)
  160. representing one of the four SMPTE or MIDI Timecode formats... -24,-25, -29
  161. (used for 30 drop frame) or -30. The second byte gives the resolution 
  162. within the frame with typical values being 4, 8, 10, 80 or 100...
  163.  
  164.  
  165.     bit 15   bit 14 -- bit 8  bit 7 -- bit 0
  166.     
  167.      1     Negative of the   Resolution
  168.           SMPTE format
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175. Track Chunks
  176. ------------
  177.  
  178. These consist of a 4 byte Track chunk identifier 'MTrk', a 32 bit length 
  179. field which tells you how much data the chunk contains, and one or more 
  180. Mtrk 'events'. The events themselves take a standardised form which 
  181. starts with a time field that specifies the amount of time which should 
  182. pass before the specified event occurs.
  183.  
  184. These time fields, which are an integral part of the syntax of a MIDI 
  185. file event are called 'delta times'. Like several MIDI file items these 
  186. values are stored in a variable length format containing 7 real bits per 
  187. byte. The most significant bit (bit 7) is used to indicate either the 
  188. continuation, or the end, of the number...
  189.  
  190.      1st Byte  2nd Byte....   n'th Byte
  191.      
  192.     1xxx xxxx  1xxx xxxx...   0xxx xxxx
  193.  
  194.     ^      ^         ^
  195.     |      |         |
  196.     |      |    indicates the last byte of the number
  197.    indicates that more
  198.    bytes are to follow
  199.  
  200. Using this arrangement, inter-event times which are less than 128 (usually 
  201. the majority of delta-times) can be stored in a single byte. The number 
  202. 127 for example can be stored simply as binary 0111 1111. Once we get 
  203. above 127 we need more bytes to store the number. The variable length 
  204. format can, if you're not used to it, seem awkward to decode so here is 
  205. an example using the number 128 decimal to show how it's done...
  206.  
  207. Basically we take the binary form of the number, peel off bits seven at a 
  208. time, and then push each group of seven bits into an eight bit (ie 1 byte)
  209. arrangement. Once a suitable number of bytes have been formed (and the 
  210. MIDI spec now specifies that a maximum of 4 bytes only may be used to 
  211. represent delta-times) we set the high bits in every byte except the last 
  212. one...
  213.  
  214. Decimal number                  128
  215.  
  216. Hex form                      80
  217.  
  218. Binary representation           1 000 0000
  219.  
  220. 7 bits per byte form       0000 0001 0000 0000
  221.  
  222. indicator bits          ^     ^
  223.                  |     |
  224.                  
  225.                 set this   keep this                                 
  226.                 bit high   bit clear
  227.  
  228.  
  229. Final binary form         1000 0001 0000 0000
  230.  
  231. Hex equivalent           81 hex   00
  232.  
  233. The number 128 decimal is therefore stored (most significant byte first) 
  234. as two bytes... 81 hex followed by 00 hex. 
  235.  
  236.  
  237.  
  238. MIDI File events themselves can be one of three types:
  239.  
  240.  
  241. 1: MIDI Events
  242. --------------
  243.  
  244. Nowadays these are defined as being any MIDI channel message. By definition
  245. this means that MIDI files can only contain channel voice or channel mode 
  246. MIDI messages. In this respect the standard has changed because the early 
  247. (pre IMA) MIDI file standard used to allow storage of both channel messages
  248. and system common messages. 
  249.  
  250.  
  251. 2: SYSEX Events
  252. ---------------
  253.  
  254. Normal SYSEX messages use a modified form which includes an additional 
  255. length, ie byte-count, field (stored as a variable length number)...
  256.  
  257.         SYSEX Event1   <F0 hex> <length> <data bytes>
  258.  
  259. If the SYSEX message is sent as a single packet then the last data byte 
  260. should be the EOX (F7 hex) SYSEX terminator. This may appear to be 
  261. unnecessary since a SYSEX message length field is also included. In the 
  262. original MIDI File standard it was indeed unnecessary and the terminal F7 
  263. byte was not required. The reason that the F7 terminator has been 
  264. re-introduced is that a new MIDI file SYSEX message has been devised 
  265. which allows large SYSEX messages to be broken up into time-stamped 
  266. packets.
  267.  
  268. The new message actually starts with the F7 hex terminator and takes this
  269. general form...
  270.  
  271.  
  272.         SYSEX Event2   <F7> <length> <data bytes>
  273.  
  274.  
  275. If you want to split a SYSEX message up into time-stamped packets you 
  276. can do it by using the F0 form for the first data packet, and F7 forms 
  277. for any subsequent packets. The last data byte of the last packet of 
  278. information containing a 'real' terminal F7 hex data byte.
  279.  
  280. There is no requirement within the current MIDI File Standard for this 
  281. second message type to include an initial F0 hex SYSEX status byte. This 
  282. means that it is permissible for non-legal MIDI file MIDI Messages, such 
  283. as Song Select, Song Position Pointer, real-time or MTC messages, to be 
  284. embedded within this latter form of SYSEX message. To be honest this is an 
  285. area of the standard that could lead to a number of compatibility problems.
  286.  
  287.  
  288. 3: Meta Events
  289. --------------
  290.  
  291. The current MIDI File standard supports a number of 'non-MIDI' events 
  292. known as Meta events. All start with an FF hex as the primary meta-event 
  293. identifier and this is followed by a meta event 'type' field, a byte count,
  294. and finally the data itself...
  295.  
  296.   Meta Event   <FF hex> <meta-event type> <length> <data bytes> 
  297.               
  298.  
  299. The meta-event type field is a 1 byte value between 0 and 127 and the 
  300. length field is stored in the same variable length format as is used for 
  301. delta-time values.
  302.  
  303. The following table gives a brief summary of the meta events currently 
  304. defined, their formats and their contents. Amongst the following meta 
  305. events types are some variations of the basic Text event which are used 
  306. to identify particular types of ASCII text messages. Meta event type 
  307. identifiers 02 hex - 0F hex have been reserved for such messages although 
  308. not all have been defined at the current time...
  309.  
  310.  
  311. Description   Type (Hex) Length   Details
  312. -----------   ---------  ------   -------
  313.  
  314. Sequence No.    00     2     16 bit number representing
  315.                     a sequence number. Must occur
  316.                     at the beginning of the track,
  317.                     ie before any non zero delta-times.
  318.  
  319. Text Event     01   variable   Text describing anything at all.
  320.  
  321.  
  322. Copyright Notice  02   variable   Should be the first event in the
  323.                     first track.
  324.  
  325.  
  326. Sequence or    03   variable   Another ASCII text event
  327. Track Name
  328.  
  329.  
  330. Instrument Name  04   variable   An indication of the instrumentaton
  331.                     to be used for a track. It can be 
  332.                     used in conjunction with a Prefix 
  333.                     event to specify which MIDI channel
  334.                     the description applies to, or the
  335.                     channel number can be specified 
  336.                     within the text itself.
  337.  
  338.  
  339. Lyric       05   variable   A lyric to be sung. The idea here 
  340.                     is that each syllable will be a
  341.                     separate lyric event which is to 
  342.                     begin at the events starting time.
  343.  
  344.  
  345.  
  346. Marker       06   variable   Used to mark a specific point in 
  347.                     a sequence.
  348.  
  349.  
  350. Cue Point     07   variable   A marker event used for video/film
  351.                     cue point marking.
  352.  
  353.  
  354.           08 - 0F       Reserved, but currently undefined
  355.  
  356.  
  357.  
  358. Channel Prefix   20     1     This event contains a single data 
  359.                     byte which associates subsequent 
  360.                     events with a particular MIDI 
  361.                     channel. This state is effective 
  362.                     until the next normal MIDI event, 
  363.                     or the next Channel Prefix event.
  364.  
  365.  
  366. End Of Track    2F     0     This event MUST be used so that 
  367.                     an exact ending point can be 
  368.                     specified for a track. It's use 
  369.                     is non-optional !
  370.                     
  371.                     
  372.                     
  373. Set Tempo     51     3     A 24 bit number which represents 
  374.                     microseconds per quarter note. 
  375.                     These events should really only 
  376.                     occur at positions where real MIDI
  377.                     clocks would be located.   
  378.                   
  379.                   
  380. SMPTE Offset    54     5     The five bytes of this message 
  381.                     indicate hours, minutes, seconds 
  382.                     frame and frame fraction (in 
  383.                     1/100th's of a frame).
  384.  
  385.                   
  386. Time Signature   58     4     The time signature is expressed 
  387.                     as four numbers NN DD CC and BB.
  388.                     NN is the time signature numerator,
  389.                     DD is the denominator expressed 
  390.                     as a power of 2, CC is the number 
  391.                     of MIDI clocks in a metronome click,
  392.                     and BB represents the number of 32nd
  393.                     notes in a MIDI quarter note.
  394.  
  395.                     
  396. Sequencer Specific 7F   variable   This event allows sequencers to 
  397.                     include their own sequencer specific
  398.                     info. Manufacturers who use such 
  399.                     events are SUPPOSED to publish the 
  400.                     formats so that others can interpret 
  401.                     them.
  402.  
  403.              
  404.              
  405.              
  406.   
  407. Last words
  408. ----------
  409.  
  410. So, that then is the basis of the current MIDI file standard as adopted 
  411. by the International MIDI Association. It's worth mentioning that running 
  412. status (ie the use of implied status bytes) is allowed within a stream of 
  413. MIDI events but this must not be carried across non-channel events. If a 
  414. stream of running status MIDI messages are interrupted by one or more Meta 
  415. or SYSEX events then a new status byte must be present in the first of any
  416. MIDI messages which follow. 
  417.  
  418. To those of us who were involved with early MIDI files it is evident that 
  419. several changes have occured which may make MIDI files written with early 
  420. programs incompatible with sequencers designed to read the current standard.
  421. If nothing else this might be a useful warning for many people who seem to 
  422. believe that early MIDI files are equivalent to the new type 0 MIDI files. 
  423. Agreed they are similar... but they are clearly NOT identical... The End 
  424. of track events, which used to be optional, must now always be present. Set 
  425. Tempo and Time Signature meta-events have changed size, SYSEX messages have
  426. been re-defined, and a variety of new meta-events have been proposed. There
  427. are now explicitly defined header and track chunks and of course system 
  428. common messages are no longer allowed. The standard was officially published 
  429. in July 1988, so if you've got software packages which support MIDI files, 
  430. but were released before this time it is quite likely that they are not 
  431. implementing the full IMA adopted standard. 
  432.  
  433. If you have had problems with MIDI files you should do two things: Firstly 
  434. write to your software supplier and ask whether the software you purchased
  435. conforms to the July 88 Standard MIDI File (1.0) document - and if not 
  436. press for details about the differences. Secondly, write to Atari ST 
  437. User... a lot of people seem to be having problems with MIDI files and, 
  438. with so many ST based musicians out there, we're getting rather interested 
  439. in finding out just what sort of difficulties you've been experiencing !
  440.  
  441.  
  442. Coming Soon
  443. -----------
  444.  
  445. Next month we're going to delve into some of the problems associated with 
  446. writing your own MIDI File programs. It's not exactly beginners stuff... 
  447. but it's not impossibly hard either. If you want to know how to isolate 
  448. chunks, read variable length fields, identify events and interpret them 
  449. etc., then we'll show you how it's done !
  450.  
  451.