home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / music / musicinc / musicinc.doc < prev    next >
Encoding:
Text File  |  1987-09-18  |  14.3 KB  |  286 lines

  1.                          MUSIC IN THE KEY OF C
  2.  
  3.                                            By Mark Lewis Baldwin
  4.  
  5. *****************************************************************************
  6.                                  NOTICE
  7.  
  8. This article and the attached program and code are Copyright (c) 1986 by
  9. Mark Lewis Baldwin.  It is realeased as shareware.  It may be copied and
  10. distributed freely so long as this notice and the copyright remain
  11. intact.
  12.  
  13. If you feel that the article/program is of value, please send me a
  14. pricely sum of $1 for its use.  If the code (or a substatial part
  15. thereof) is used in a commercial product, I ask that a sum of $100 be
  16. sent to me for licensed use of the code.  My mailing address is:
  17.  
  18.     Mark Baldwin
  19.     Beacon Technical Services
  20.     7711 S Curtice Way, #E
  21.     Littleton, CO 80120
  22.  
  23. I can also be reached through GEnie at M.L.BALDWIN and through
  24. Compuserve at 73637,3032.
  25.  
  26. I hope that you find the article of value.  Enjoy.
  27.  
  28. Mark Baldwin
  29.  
  30. *****************************************************************************
  31.  
  32.     Over the past months, there have been many articles about using the
  33. MIDI for music on the Atari ST.  But the ST has an internal sound chip
  34. as well capable of a wide range of sound and music.  Lets have a look at
  35. this sound chip and figure out how to make beautiful music with it. 
  36. What I'm going to do is describe the sound chip and how to talk to it. 
  37. Then we'll look at a program I wrote for the chip which allows us to use
  38. three independent voices and even describe instrument ADSR envelopes. 
  39. Before I talk about the program, let us look at the sound chip itself. 
  40.  
  41.                     The Chip and Its Registers
  42.  
  43.     The ST sound chip is a General Instruments AY-3-8910 programmable
  44. sound generator.  It has three tone generators and a noise generator
  45. which can be mixed and matched on three output channels (or voices).  It
  46. communicates with 16 registers, each an 8 bit word in length, from which
  47. the user can communicate with the chip.  These registers are numbered 0
  48. through 15.  We are actually only interested in the first 14 registers
  49. (0-13) since the last two are used to communicate with the I/O port. 
  50. Each of the first 14 registers has a specific purpose which are
  51. described below:
  52.  
  53. Register 0 and 1 - These registers control the frequency (or tone) of
  54. the first voice (Channel A) of the sound chip.  A 12 bit word is
  55. necessary to define the frequency, therefore the lower 8 bits of the
  56. word are stored in register 0 and the upper 4 bits of the word are
  57. stored in the lower half of register 1.  See the next section to find
  58. out how to convert musical notes to the proper frequency. 
  59.  
  60. Register 2 and 3 - These registers control the frequency of the second
  61. voice (Channel B). 
  62.  
  63. Register 4 and 5 - These registers control the frequency of the third
  64. voice (Channel C). 
  65.  
  66. Register 6 - The frequency of the noise generator is controlled through
  67. this register.  Only the first 5 bits are used so acceptable values are
  68. between 0 and 31. 
  69.  
  70. Register 7 - This register is actually 8 on/off flags used to control
  71. mixing of the tone and noise generator plus control of the I/O ports
  72. (registers 14 and 15).  Bits 0 through 2 control mixing the tone signals
  73. for channels A, B and C respectively.  A 1 in the bit position will turn
  74. the tone for that channel off while a 0 will turn it on.  Bits 3 through
  75. 5 do the same thing for the noise generator.  Note that by turning on
  76. both tone and noise, you can mix both signals together.  This may be a
  77. little confusing, so let's try an example.  Lets say we want to turn the
  78. tone on for Channel A and B plus merge the noise signal as well on
  79. Channel B and leave Channel C off.  As a result, we need bits 0, 1, and
  80. 4 to be 0 while 2, 3 and 5 should be set to 1.  Or in binary it looks
  81. like this 101100 (the right most bit is bit 0).  101100 in binary
  82. converts to 44 decimal which we would place in register 7. 
  83.  
  84. Register 8 through 10 - These registers control the volume for channels
  85. A, B and C after mixing.  Values of 0 through 15 are acceptable with 0
  86. being no sound and 15 being the loudest.  However, if bit 4 is set (i.e. 
  87. a value of 16), the volume is calculated from the sound envelope data
  88. described in registers 11 through 13. 
  89.  
  90. Register 11 through 13 - These registers describe a wave form that can
  91. be used to control the volume of a voice should the voices respective
  92. volume register be set to 16.  Registers 11 and 12 contain a two byte
  93. word (lower byte in register 11) describing the length of the wave form. 
  94. This is calculated by taking the incoming clock frequency (2 Megahertz),
  95. dividing by 256, and then dividing that result by the contents in
  96. registers 11 and 12.  Register 13 describes the shape of the wave form
  97. as shown in Figure 1. 
  98.  
  99. Register 14 and 15 - As mention before, these registers have nothing to
  100. do with producing sound but are used for the I/O ports of the ST so let
  101. us leave them alone. 
  102.  
  103.                   Converting Musical Notes For The Registers  
  104.  
  105.     Any musical note can be calculated as follows:
  106.  
  107.                 period = 284.0909091 * ( 1.059462 ** index )
  108.                 
  109.     After rounding 'period' to an integer, the lower byte would be
  110. stored in the first register (i.e.  registers 0, 2, or 4) while the
  111. upper byte would be stored in the second register (i.e.  1, 3, or 5). 
  112. The value of index is determined by counting the number of notes there
  113. are between the desired note and a 440hz "A".  When counting, higher
  114. notes are negative.  Remember that there are 12 notes to an octave, not
  115. 7 because of the sharps.  Therefore a "C" in the same octave as "A"
  116. would have an index of 9 (with register values of 222 and 1) while a "B"
  117. would be -2 (with register values of 253 and 1).  If you look in the
  118. attached program listing, the note_f and note_c arrays contain register
  119. data for 5 octaves around middle "A". 
  120.  
  121.                     Talking to the Sound Chip
  122.  
  123.     Now that we know what makes our chip sing (or at least croak), how
  124. do we get the data into the chip registers.  Atari has solve that for us
  125. by giving us two routines to communicate with the sound chip. 
  126.     The simpler of the two routines is 'Giaccess'.  The C form is
  127.  
  128.                      char Giaccess( data, register )
  129.                      char data
  130.                      int register
  131.  
  132. This routine allows us to read or write to any single register in the
  133. sound chip.  If you wish to write to a register (like putting in the
  134. volume for a channel), 'data' would contain the value you wish to write
  135. and 'register' would contain the register number plus 128.  The added
  136. 128 (which turns on bit 7) tells the routine that this is a write and
  137. not a read action.  If you wish to read a register, just use the
  138. register number itself, and the value will be returned. 
  139.     The second routine for accessing the sound chip is 'Dosound'.  This
  140. routine actually allows you to send an array of byte commands to the
  141. chip and then forget about it.  Therefore, it is a little more complex. 
  142. The C call is of the form:
  143.  
  144.                                 Dosound( ptr )
  145.                                 char *ptr
  146.  
  147. where *ptr points to a byte array containing the sound commands.  The
  148. following commands are allowed for the byte array:
  149.  
  150. Commands 0 - 15 ($00-$0F): These commands are perceived as register
  151. numbers in which the next byte will be inserted as data.  Therefore the
  152. sequence 8 10 would put the value 10 in register 8. 
  153.  
  154. Command 128 ($80): This command loads the next byte into a temporary
  155. register. 
  156.  
  157. Command 129 ($81): This command uses the next three bytes as data.  The
  158. first byte is the number of the register in which the contents of the
  159. temporary register will be loaded.  The second byte is a 2's compliment
  160. value to be added to the temporary register.  The third byte is the
  161. termination value.  The sound register will be loaded once each cycle
  162. until the temporary register equals the termination value. 
  163.  
  164. Command 130-255 ($82-$FF): The byte following this command either
  165. terminates the byte array (with a 0) or defines the number of 50HZ
  166. cycles before the next update. 
  167.  
  168.     The example program includes an example of using 'Dosound'.
  169.  
  170.                         Entering the Score
  171.  
  172.     Now that we know how to produce sound out of the sound chip, how can
  173. we turn that into music.  That's what the attached example program is
  174. for.  I designed this program to play a three voice score using multiply
  175. designed instruments.  You can use the program just as it is or include
  176. it in you own programming projects.  Let's go through it step by step
  177. and see how we use it to make the sound chip sing. 
  178.     Since we want to be able to include music easily, I had to invent a
  179. simple language to describe the musical score.  Two scores are included
  180. named 'interstel' and 'spake'.  'interstel' is the theme for software
  181. produced by The Interstel Company while 'spake' is "Thus Spake
  182. Zarathustra" by Richard Strauss.  I used these two songs because I
  183. previously used them in Star Fleet I for the ST and had the scores lying
  184. around.  Note that although the Interstel Theme is copyright, you are
  185. welcome to use it for your personal use with this program.  Each song is
  186. an integer array where each value has a meaning to the score processor. 
  187. To make things simple, I've defined all of the parameters we need to
  188. construct the score at the start of the program.  The following score
  189. instructions are available:
  190.  
  191. SPEED n, - Change the song speed to n.  The larger the number, the
  192. slower the song.  Start the score with a SPEED value, and insert it
  193. again if you need to change speed part way through the song.  Remember
  194. to put a comma after the number n. 
  195.  
  196. <instrument> - There are three instruments defined in the program being
  197. ORGAN, HORN and SNARE.  They indicate that all note entries following
  198. will be played with that instrument until a new instrument is used.  A
  199. little later I'll show you how to define your own instrument. 
  200.  
  201. <note duration> - The following durations are available, WH, HF, QU, EI,
  202. SX and TH representing WHole, HalF, QUarter, EIghth, SiXteenth and
  203. THirty second.  They indicate that all note entries following will be of
  204. the designated length until a new length is defined.  The length values
  205. can be modified for dotted and tripleted notes by adding a D or T to the
  206. end of the duration (i.e.  EID means eighth note dotted). 
  207.  
  208. <note> - Five octaves have been defined.  Two above and two below the
  209. middle octave.  A note is designated by the note (C,D,E,F,G,A or B), a
  210. optional S for sharp, and the octave (2-6) where 4 is the middle octave. 
  211. Therefore FS3 would be F sharp below middle C.  The note will use the
  212. currently active instrument for the currently active note duration. 
  213.  
  214. REST - Rest (no sound) for the current note duration.
  215.  
  216. P - Play the current notes you've defined since the last P.
  217.  
  218. VU - Increase the volume of all following notes by one.  
  219.  
  220. VD - Decrease the volume of all following notes by one.
  221.  
  222. VN - Return the notes to normal volume.
  223.  
  224. DO n, - Repeat the following music until the ENDDO n times.  
  225. Remember to put a comma after the n.
  226.  
  227. END - Terminate the music. 
  228.  
  229. All you have to do to create a score is to build a data array using the
  230. above defined parameters. 
  231.  
  232.                          The Instruments
  233.  
  234.     We also have a capability of defining instruments to a limited
  235. extent in the program.  We can define the ADSR envelope for each
  236. instrument.  ADSR stand for Attack, Decay, Sustain and Release and are a
  237. means of defining a sound envelope for the instrument.  See figure 2. 
  238. The magnitude of each phase of the envelope is contained in the array
  239. 'inst_mag'.  Values of 0 to 15 are allowable.  These correspond to the
  240. volume levels allowed on the sound chip.  The array 'inst_tim' describes
  241. the time allowed for each phase.  Values of 1 to 15 are allowed here
  242. with one exception.  If a value of 16 is used for the sustain phase (3rd
  243. value), the program will calculate the length of the sustain based on
  244. the length of the note being played.  Therefore, long notes will be
  245. drawn out while short notes will be cut short.  Go ahead and play with
  246. the values or add as many more instruments as you wish. 
  247.     Since we can also mix the noise generator with the tone generator,
  248. two more arrays have been defined.  The first is 'inst_tone'.  A value
  249. of TRUE mixes in the tone generator.  Secondly, we have 'inst_noise'
  250. which will mix in the noise generator if TRUE.  Note that either one can
  251. be turned on or even both of them at once as I did for the SNARE
  252. instrument. 
  253.  
  254.                             The Code  
  255.  
  256.     The program first calls 'init_music' to initialize some
  257. precalculations and clear the sound chip.  It then starts a song by
  258. entering 'play_music' passing a pointer to the song array.  'play_music'
  259. actually processes the song array separating out the notes, durations,
  260. instruments, etc.  When it's ready to play some notes, it calls
  261. 'inst_sound' with arrays containing the note(s) it wishes to play, the
  262. instrument assigned to each note, the duration of each note, and the
  263. time 'inst_sound' should play until returning to 'play_music'.  Note
  264. that if less than three notes are passed and 'inst_sound' still has some
  265. of a note left over from a previous call, it will continue to play the
  266. old note as well as the new ones. 
  267.     'inst_sound' actually builds the data array that we are going to
  268. pass to the sound chip through 'Dosound'.  The function 'sound_build'
  269. takes the register and value data and places them in 's_array' which is
  270. our data array.  The function 'calc_s_vol' used toward the bottom of
  271. 'inst_sound' is the routine which actually calculates where we are in
  272. the ADSR envelope for each of our voices, and returns a volume level. 
  273. Finally, 'sound_go' takes the sound array and passes it to 'Dosound'. 
  274.  
  275.                         Play It Again, Sam
  276.  
  277.      I hope that now that you've been exposed to the Atari sound chip,
  278. you'll see that there are a great deal of possibilities for it.  The
  279. program will play it's two songs as much as you wish, but I hope you dig
  280. into the program yourself and add more songs or instruments.  Or even
  281. improved on the processing code itself.  I can already think of half a
  282. dozen ways to improve upon the program.  If you come up with any
  283. improvements, I'd love to see them, so drop me a line.  Until then,
  284. happy sounds to you. 
  285.  
  286.