home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource3 / 191_01 / sound.txt < prev    next >
Encoding:
Text File  |  1986-05-25  |  51.6 KB  |  1,783 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.                             S O U N D   E F F E C T S
  16.  
  17.                                C Function Library
  18.                                   Version 1.0b
  19.  
  20.  
  21.                        Copyright (C) 1986 by Paul Canniff
  22.                               All Rights Reserved.
  23.  
  24.  
  25.  
  26.  
  27.                                   Paul Canniff
  28.                                   P.O. Box 1056
  29.                                 Marlton NJ 08053
  30.  
  31.                             CompuServe ID 73047,3715
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.                             
  56.                                                         Updated 05 Mar 86
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.                                  C O N T E N T S
  75.  
  76.                   Introduction  .......................  1
  77.                   Installation for Aztec C  ...........  2
  78.                   Installation for Ecosoft C  .........  3
  79.                   Installation for Lattice C  .........  4
  80.                   Installation for other compilers  ...  5
  81.                   Using The Library  ..................  6
  82.                   Making Music (using PLAY)  ..........  8
  83.                   Making Noise (using SOUND)  ......... 10
  84.                   Function Summary List  .............. 11
  85.                   Individual Function Descriptions  ... 12
  86.                   Technical Information  .............. 18
  87.                   What To Do When It Doesn't Work  .... 23
  88.                   Change Log .......................... 24
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.                                   Introduction
  138.  
  139.         This library facilitates the use of sound and music on the IBM-PC 
  140.  
  141.         family  and  compatibles.   It has been tested with  the  Lattice 
  142.  
  143.         compiler  version 2.15 and the Manx Aztec compiler version  3.20.  
  144.  
  145.         Other compilers (or versions of these compilers) should work just 
  146.  
  147.         fine,  provided  you can rewrite or modify the  assembly-language 
  148.  
  149.         portions.   I  will be glad to help get the library working  with 
  150.  
  151.         other compilers.
  152.  
  153.         The  main  functions in the  library  are  play(),  sound(),  and 
  154.  
  155.         sounds().   Play()  accepts  a form of  musical  notation,  while 
  156.  
  157.         sounds()  and sounds() accept specific frequencies and  durations 
  158.  
  159.         for  greater  flexibility  (at the expense of more work  for  the 
  160.  
  161.         programmer).  These functions are discussed in more detail in the 
  162.  
  163.         sections "Making Music" and "Making Noise".  Calls to sound() and 
  164.  
  165.         play()   can  be  freely  intermixed  in  either  foreground   or 
  166.  
  167.         background,  providing  a  flexible high-level interface  to  the 
  168.  
  169.         speaker.
  170.  
  171.         All  of  these  sound-making routines can run  in  foreground  or 
  172.  
  173.         background.  Foreground means that the sound is generated in much 
  174.  
  175.         the  same  way  that most other  PC  output  is  generated;   the 
  176.  
  177.         application  requests that it be done,  the driver does  it,  and 
  178.  
  179.         then it returns to the application.   This is straightforward and 
  180.  
  181.         clean.
  182.  
  183.         Background is the fancy way.   The music or the sound effects are 
  184.  
  185.         requested  by  the application,  which can then continue  to  run 
  186.  
  187.         while the driver controls the speaker.   This allows you to  play 
  188.  
  189.         some  music  while your program performs some  operations,  which 
  190.  
  191.         might be better than just flashing WAIT at the user.
  192.  
  193.         In addition,  the library includes a number of low-level routines 
  194.  
  195.         that access the speaker directly.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.                                         1
  218.  
  219.  
  220.  
  221.  
  222.  
  223.         Sound Effects                                General Installation
  224.  
  225.  
  226.  
  227.         The  following  general  installation procedure  applies  to  all 
  228.  
  229.         supported compilers.  
  230.  
  231.         (1)  First, set up your "work area", whether it be a subdirectory 
  232.  
  233.              on  a hard disk,  or a floppy disk.   If you have room on  a 
  234.  
  235.              current work disk,  or in your usual C subdirectory, you may 
  236.  
  237.              want  to use them.   Wherever you decide upon,  this disk or 
  238.  
  239.              diectory will be referred to as your "work area".
  240.  
  241.         (2)  Copy all .C and .H files from the distribution disk to  your 
  242.  
  243.              work  area.   
  244.  
  245.         (3)  There are also some assembly-langauge source files which you 
  246.  
  247.              will need, called DELAY and SOUNDINT.  There are versions of 
  248.  
  249.              these files on the disk for each supported  compiler.   Copy 
  250.  
  251.              the  proper files to your work area,  changing the extension 
  252.  
  253.              to .ASM in the process.
  254.  
  255.                     AZTEC:    *.ASZ  ->  *.ASM
  256.                     LATTICE:  *.ASL  ->  *.ASM
  257.                     ECOSOFT:  *.ASE  ->  *.ASM
  258.             
  259.         (*)  Keep  the original distribution disk in a safe  place;   all 
  260.  
  261.              further actions discussed should be done on your work  area, 
  262.  
  263.              not the distribution disk.
  264.  
  265.         (2)  Edit  the file SOUND.H and make sure that the only  compiler 
  266.  
  267.              flag set is the one for your compiler.   That flag should be 
  268.  
  269.              set to 1, all others to 0.
  270.  
  271.         (3)  Compile  the C files.   They include the following H  files, 
  272.  
  273.              which should be included with your compiler:
  274.  
  275.                   Lattice, Ecosoft:     STDIO.H, DOS.H
  276.                   Aztec:                STDIO.H, MODEL.H
  277.  
  278.         (4)  Assemble  the  .ASM files using the assembler provided  with 
  279.  
  280.              your   compiler,   the  Miscrosoft   Macro   Assembler,   or 
  281.  
  282.              equivalent.   The  following "macro" headers are included by 
  283.  
  284.              the assembly files:
  285.  
  286.                   Lattice:       DOS.MAC
  287.                   (all others)   -none-     
  288.  
  289.         (5)  The  file  SOUNDTST  is  not a part  of  the  Sound  Effects 
  290.  
  291.              library,  but  is furnished as an example of an  application 
  292.  
  293.              calling  the sound functions.   To test  the  library,  link 
  294.  
  295.              SOUNDTST  to  all  the sound functions,  and  any  necessary 
  296.  
  297.              modules.  The following modules are required:
  298.  
  299.                   Lattice:       C.OBJ, LC.LIB
  300.                   Ecosoft:       ??
  301.                   Aztec:         C.LIB
  302.  
  303.  
  304.  
  305.                                         2
  306.  
  307.  
  308.  
  309.  
  310.  
  311.         Sound Effects                         Installation - Manx / Aztec
  312.  
  313.  
  314.  
  315.                      Manx Aztec C Version 3.20 Installation
  316.  
  317.         Note:  Older Manx compilers (such as version 1.06) should require 
  318.  
  319.                no change to the following procedure.
  320.  
  321.         Before  starting,  set  up  your "work area",  whether  it  be  a 
  322.  
  323.         subdirectory on a hard disk, or a floppy disk.   Then ...
  324.  
  325.         (1)  Copy  the .C and .H files to your work diskette or hard disk 
  326.  
  327.              subdirectory.   Copy  all .ASL files to .ASM files  on  your 
  328.  
  329.              work  area.   Keep the original distribution disk in a  safe 
  330.  
  331.              place;  all further actions discussed should be done on your 
  332.  
  333.              work area, not the distribution disk.
  334.  
  335.         (2)  Edit  the file SOUND.H and make sure that the only  compiler 
  336.  
  337.              flag set to 1 is C_AZTEC. 
  338.  
  339.         (3)  Compile  the C files.   They include the following H  files, 
  340.  
  341.              which should be included with your compiler:
  342.  
  343.                   STDIO.H     MODEL.H
  344.  
  345.         (4)  Assemble  the  .ASM  files using the Manx  Aztec  Assembler, 
  346.  
  347.              Miscrosoft Macro Assembler or equivalent.
  348.  
  349.         (5)  Link  the file SOUNDTEST.OBJ to the rest of .OBJ's  produced 
  350.  
  351.              by the above commands, and to the C library (C.LIB). 
  352.  
  353.         (6)  If  you hear music,  you have correctly installed your sound 
  354.  
  355.              routines.   You  may use LIB or PLIB86 to form them  into  a 
  356.  
  357.              library if you want.   Note that you will have to repeat the 
  358.  
  359.              installation procedure for every memory model you use.   Oh, 
  360.  
  361.              yes,  you  can delete the SOUNDTST files when you are  done, 
  362.  
  363.              though the source gives a good example of the correct way to 
  364.  
  365.              access the library.
  366.  
  367.                                    Aztec Notes
  368.  
  369.         (1)  Sound_done() must also be called before exiting an  overlay, 
  370.  
  371.              if the overlay contains the Sound Effects code and data.  If 
  372.  
  373.              both  code and data of the Sound Effects functions reside in 
  374.  
  375.              the root, this is not necessary.
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.                                         3
  391.  
  392.  
  393.  
  394.  
  395.  
  396.         Sound Effects                              Installation - Ecosoft
  397.  
  398.  
  399.  
  400.                        Ecosoft C Version 3.02 Installation
  401.  
  402.         Before  starting,  set  up  your "work area",  whether  it  be  a 
  403.  
  404.         subdirectory on a hard disk, or a floppy disk.   Then ...
  405.  
  406.         (1)  Copy  the .C and .H files to your work diskette or hard disk 
  407.  
  408.              subdirectory.   Copy  all .ASE files to .ASM files  on  your 
  409.  
  410.              work  area.   Keep the original distribution disk in a  safe 
  411.  
  412.              place;  all further actions discussed should be done on your 
  413.  
  414.              work area, not the distribution disk.
  415.  
  416.         (2)  Edit  the file SOUND.H and make sure that the only  compiler 
  417.  
  418.              flag set to 1 is C_ECOSOFT.
  419.  
  420.         (3)  Compile  the C files.   They include the following H  files, 
  421.  
  422.              which should be included with your Lattice Compiler:
  423.  
  424.                   STDIO.H     DOS.H
  425.  
  426.         (4)  Assemble  the .ASM files using the Microsoft Macro Assembler 
  427.  
  428.              or equivalent.   The file PRO.H must be present.   This file 
  429.  
  430.              is included in the Ecosoft C package and allows the assembly 
  431.  
  432.              modules to know the memory model in use.
  433.  
  434.         (5)  Link  the file SOUNDTEST.OBJ to the rest of .OBJ's  produced 
  435.  
  436.              by  the  above  commands,  and  to  the  Ecosoft  C  library 
  437.  
  438.              (ECOC.LIB). 
  439.  
  440.         (6)  If  you hear music,  you have correctly installed your sound 
  441.  
  442.              routines.   You  may use LIB or PLIB86 to form them  into  a 
  443.  
  444.              library if you want.   Note that you will have to repeat the 
  445.  
  446.              installation procedure for every memory model you use.   Oh, 
  447.  
  448.              yes,  you  can delete the SOUNDTST files when you are  done, 
  449.  
  450.              though the source gives a good example of the correct way to 
  451.  
  452.              access the library.
  453.  
  454.                                   Ecosoft Notes
  455.  
  456.         (1)  The  file SOUNDP.H is provided to define the  Sound  Effects 
  457.  
  458.              callable  functions,  using the prototype form.   If you are 
  459.  
  460.              using  prototyping,  this  file should be  included  in  any 
  461.  
  462.              source module that calls the Sound Effects library.
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.                                         4
  478.  
  479.  
  480.  
  481.  
  482.  
  483.         Sound Effects                              Installation - Lattice
  484.  
  485.  
  486.  
  487.                    Lattice C Version 2.14 or 2.15 Installation
  488.  
  489.         Note:  The new Lattice compilers (version 3.xx) should require no 
  490.  
  491.                change to the following procedure.
  492.  
  493.         Before  starting,  set  up  your "work area",  whether  it  be  a 
  494.  
  495.         subdirectory on a hard disk, or a floppy disk.   Then ...
  496.  
  497.         (1)  Copy  the .C and .H files to your work diskette or hard disk 
  498.  
  499.              subdirectory.   Copy  all .ASL files to .ASM files  on  your 
  500.  
  501.              work  area.   Keep the original distribution disk in a  safe 
  502.  
  503.              place;  all further actions discussed should be done on your 
  504.  
  505.              work area, not the distribution disk.
  506.  
  507.         (2)  Edit  the file SOUND.H and make sure that the only  compiler 
  508.  
  509.              flag set to 1 is C_LATTICE.
  510.  
  511.         (3)  Compile  the C files.   They include the following H  files, 
  512.  
  513.              which should be included with your Lattice Compiler:
  514.  
  515.                   STDIO.H     DOS.H
  516.  
  517.         (4)  Assemble the .ASM files using the Microsoft Macro  Assembler 
  518.  
  519.              or equivalent.  The file DOS.MAC must be present.  This file 
  520.  
  521.              is included in the Lattice C package and allows the assembly 
  522.  
  523.              modules to know the memory model in use.
  524.  
  525.         (5)  Link  the file SOUNDTEST.OBJ to the rest of .OBJ's  produced 
  526.  
  527.              by  the  above  commands,  and  to  the  Lattice  C  library 
  528.  
  529.              (LC.LIB). 
  530.  
  531.         (6)  If  you hear music,  you have correctly installed your sound 
  532.  
  533.              routines.   You  may use LIB or PLIB86 to form them  into  a 
  534.  
  535.              library if you want.   Note that you will have to repeat the 
  536.  
  537.              installation procedure for every memory model you use.   Oh, 
  538.  
  539.              yes,  you  can delete the SOUNDTST files when you are  done, 
  540.  
  541.              though the source gives a good example of the correct way to 
  542.  
  543.              access the library.
  544.  
  545.                                   Lattice Notes
  546.  
  547.         (1)  When compiling the Sound Effects functions,  be usure to use 
  548.  
  549.              the  correct  compiler flags and DOS.MAC file,  so that  the 
  550.  
  551.              Sound  Effects functions are using the same memory model  as 
  552.  
  553.              your application.   You may want to compile four versions of 
  554.  
  555.              the Sound Effects functions, one for each memory model.
  556.  
  557.         (2)  Be  sure  to compile Sound Effects functions using the  "-v" 
  558.  
  559.              compiler flag to disable stack checking.  This is compatible 
  560.  
  561.              with  the  Lattice library.   Not disabling  stack  checking 
  562.  
  563.              could result in detection of a bogus "stack overflow" during 
  564.  
  565.              the   timer  interrupt  handler's  execution.    This   will 
  566.  
  567.              terminate the application.
  568.  
  569.  
  570.  
  571.                                         5
  572.  
  573.  
  574.  
  575.  
  576.  
  577.         Sound Effects                               Installation - Others
  578.  
  579.  
  580.  
  581.  
  582.                              Non-Supported Compilers
  583.  
  584.         To  use  the  Sound Effects library with a  compiler  other  than 
  585.  
  586.         Lattice  or  Aztec,  you  will  have to  be  familiar  with  your 
  587.  
  588.         compiler  in order to customize the few assembler routines.   The 
  589.  
  590.         following  questions should be answered first.   The answers  for 
  591.  
  592.         the supported compilers are given for reference.
  593.  
  594.  
  595.                                                     Lattice  Manx  Yours?
  596.                                                     -------  ----  ------
  597.          o Does  your compiler append or  prepend 
  598.  
  599.            an  underscore  to  the name  of  each 
  600.  
  601.            function?                                   N      Y
  602.  
  603.          o Where does the compiler put parameters?   Stack   Stack
  604.  
  605.          o Where  does  your compiler  place  its 
  606.  
  607.            return values for int?                     AX      AX
  608.            For long int?                             AX,BX   DX,AX
  609.  
  610.          o Does your compiler use near calls, far 
  611.  
  612.            calls, or allows both?                    Both    Both
  613.  
  614.          o What provision does your library make     inp()  inport()
  615.            for access to the I/O ports?             outp()  outport()
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.                                         6
  646.  
  647.  
  648.  
  649.  
  650.  
  651.         Sound Effects                                 General Information
  652.  
  653.  
  654.  
  655.                                 Using The Library
  656.  
  657.         Using the Sound Effects library is similar to using the  standard 
  658.  
  659.         library's  I/O functions.   There is an "open",  some operations, 
  660.  
  661.         the  a  "close".   In the case,  the opening function  is  called 
  662.  
  663.         sound_init() and the closing function is sound_done().  One point 
  664.  
  665.         that  cannot  be stressed overmuch is the importance  of  calling 
  666.  
  667.         sound_done()  before exiting the program.   Any application which 
  668.  
  669.         uses  the sound library should call sound_done() at all  possible 
  670.  
  671.         exit points.
  672.  
  673.         Note the following things about sound_done():
  674.  
  675.              (1)  It  never  hurts to call  it.   Sound_done()  knows  if 
  676.  
  677.                   sound_init() has been called, and acts accordingly.
  678.  
  679.              (2)  When called it will kill any music still playing.
  680.  
  681.         The opposite number of sound_done() is sound_init(),  who  should 
  682.  
  683.         (of  course) be called first,  before any other sound  functions.  
  684.  
  685.         It  is possible to get away without calling sound_init(),  if you 
  686.  
  687.         want to play in mode 1.   But it really is much neater to call it 
  688.  
  689.         anyway.
  690.  
  691.         What is mode 1?   Glad you asked.   In the play() function, there 
  692.  
  693.         is a MODE sub-command which has to do with how notes are  played.  
  694.  
  695.         But  outside  of that little corner,  when we say "mode"  we  are 
  696.  
  697.         talking about then WHEN and HOW of the sound output.
  698.  
  699.         You  got  a  taste of the meaning of modes in the  discussion  of 
  700.  
  701.         Background  and  Foreground  in  the   introduction.    Real-time 
  702.  
  703.         programmers,  forget your usual understanding of these terms.  In 
  704.  
  705.         terms  of this library music playing in Foreground plays when you 
  706.  
  707.         tell  it to,  and your application can do nothing else until  the 
  708.  
  709.         music is finished.   Music in Background is just "set up" by your 
  710.  
  711.         command to play,  and actually plays while your application runs.  
  712.  
  713.         So,  it  is  "background music" for the  application,  hence  the 
  714.  
  715.         terminology.  
  716.  
  717.         Note   that   the   terms  music  and  sound  are   used   rather 
  718.  
  719.         interchangeably.    Music  using  normal  notes  is  most  easily 
  720.  
  721.         generated  by  the play() function,  while sound() gives  a  more 
  722.  
  723.         flexible  interface if you are into  strange  frequencies.   Both 
  724.  
  725.         functions can operate in Foreground and Background.
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.  
  733.  
  734.  
  735.  
  736.  
  737.  
  738.                                         7
  739.  
  740.  
  741.  
  742.  
  743.  
  744.         Sound Effects                                 General Information
  745.  
  746.  
  747.         There  are  actually five modes in which the sound  routines  can 
  748.  
  749.         run.  See the "Technical Information" section for a more complete 
  750.  
  751.         description of the timing techniques.  The default mode is 1.
  752.  
  753.           Mode      When          Timing Method
  754.           ----   ----------    -------------------------
  755.             1    Foreground    Delay Loop
  756.             2    Foreground    Standard Clock Interrupt
  757.             3    Foreground    Enhanced Clock Interrupt  (*)
  758.             4    Background    Standard Clock Interrupt
  759.             5    Background    Enhanced Clock Interrupt  (*)
  760.  
  761.  
  762.         The  mode number shown above is the value you should give to  the 
  763.  
  764.         first parameter to sound_init().  The second parameter is sued as 
  765.  
  766.         a  true/false flag,  and tells sound_init() whether to install  a 
  767.  
  768.         break handler.   The break handler acts as a safety feature;   it 
  769.  
  770.         makes sure that sound_done() is called before the application  is 
  771.  
  772.         terminated by the user typing Control-C or Control-Break.
  773.  
  774.         If  you provide your own break handler,  you will want to disable 
  775.  
  776.         the  built-in  handler.   Just make sure that you  included  that 
  777.  
  778.         handler  in  the  list  of  possible  exits,  so  that  it  calls 
  779.  
  780.         sound_done() before terminating the application.  
  781.  
  782.         If  you don't have a break handler built in to  the  application, 
  783.  
  784.         let sound_init() put one in for you.
  785.  
  786.         OK,  now   you  know that your application should look  something 
  787.  
  788.         like this:
  789.  
  790.              main()
  791.              {
  792.                   sound_init(m,1);
  793.                   .
  794.                   .     
  795.                   .
  796.                   sound_done();
  797.              }
  798.  
  799.         That's  pretty  boring.   You can spice it up after  reading  the 
  800.  
  801.         sections  "Making  Music" and "Making Noise".   One  more  useful 
  802.  
  803.         function before you go ...  quiet().   If you set up a long piece 
  804.  
  805.         of  background music,  and for some reason have to kill it  early 
  806.  
  807.         (but not get ready to exit),  call quiet().   It has no effect in 
  808.  
  809.         Foreground modes.
  810.  
  811.  
  812.                                    ----------
  813.  
  814.              (*)  The enhanced modes currently produce the same
  815.                   results as the standard modes.  In a future 
  816.                   version these modes will provide better time
  817.                   resolution.  See "Tech Tips" for details.
  818.  
  819.  
  820.  
  821.                                         8
  822.  
  823.  
  824.  
  825.  
  826.  
  827.         Sound Effects                                        Making Music
  828.  
  829.  
  830.  
  831.                                   Making Music 
  832.                                   (Using Play)
  833.  
  834.         The  play function translates a musical score into  sounds.   The 
  835.  
  836.         score is written using a special notation described below.   This 
  837.  
  838.         notation  allows  the  use of 8 octaves of 12  notes  each,  with 
  839.  
  840.         various tempos, etc.  The format of pla() is:
  841.  
  842.              void play(m)
  843.              char *m;
  844.  
  845.         Commands are a single letter followed immediately by one or  more 
  846.  
  847.         arguments,  if  necessary.   Spaces,  commas,  and semicolons may 
  848.  
  849.         appear between commands for clarity, but are ignored by play().
  850.  
  851.  
  852.         A to G:   These are the notes A through G, in the current octave.  
  853.  
  854.                   A  note  may be followed by up to three  modifiers,  in 
  855.  
  856.                   this order only:
  857.  
  858.                        Sharp/Flat indication:  # or + is sharp;
  859.                                                - is flat.
  860.                        Note length:  number from 1 to 64 (see L)
  861.                        Dots: Up to 10 dots.  A dotted note plays for 3/2
  862.                              of it's normal time.  Two dots = 9/4, three
  863.                              dots = 27/8, etc.
  864.  
  865.                   Examples (assuming the current note length is 4):
  866.  
  867.                        C+        C sharp 1/4 note
  868.                        C+8       C sharp 1/8 note
  869.                        B-.       C sharp 1/4 note dotted = 3/8 note
  870.                        A2.       A 1/2 note dotted = 3/4 note
  871.  
  872.  
  873.         Ln        Sets the current note length.  Note length is 1/n, so a 
  874.  
  875.                   parameter  of 4 will set subsequent note to 1/4  notes.  
  876.  
  877.                   Individual  notes may override the current length  with 
  878.  
  879.                   an explicit length (see above examples).   The range is 
  880.  
  881.                   1 to 64.
  882.  
  883.                   Note  length  combines  with  tempo  to  determine  the 
  884.  
  885.                   duration of the note.  Length is measured as a fraction 
  886.  
  887.                   of  a whole note,  while tempo is in quarter-notes  per 
  888.  
  889.                   second.   For  example,  with a tempo of 32 and a  note 
  890.  
  891.                   length of 2, a note will last for 1/16 of a second.
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902.                                         9
  903.  
  904.  
  905.  
  906.  
  907.  
  908.         Sound Effects                                        Making Music
  909.  
  910.  
  911.         Mx        Sets the mode of the music.  There are three modes:
  912.  
  913.                      x=N (NORMAL):   notes are played for 7/8 of the note 
  914.  
  915.                          time, with the remaining 1/8 being a rest.
  916.  
  917.                      x=S (STACCATO): notes are played for 3/4 of the note 
  918.  
  919.                          time, with the remaining 1/4 being a rest.
  920.  
  921.                      x=L (LEGATO):  notes  are played for the  full  note 
  922.  
  923.                          time, with no rest.
  924.  
  925.                   The modes Foreground and Background have nothing to  do 
  926.  
  927.                   with these modes !
  928.  
  929.         Nn        Plays note n (range 0..84),  where 0 means silence.  If 
  930.  
  931.                   the  argument is missing or out of range,  no  note  is 
  932.  
  933.                   played.  Cannot be dotted or given explicit length.
  934.  
  935.         On        Sets octave to n (range 0..7).   If argument is out  of 
  936.  
  937.                   range, current octave remains unchanged.
  938.  
  939.         P[n][.]   Pause or rest;   see the rules for note length (n)  and 
  940.  
  941.                   dotting  for  the  regular notes  A-G  above.   If  the 
  942.  
  943.                   argument is missing or invalid, no pause takes place.
  944.  
  945.         R         Resets to default values.  Resets the following:
  946.                             Octave = 4
  947.                             Length = 1
  948.                             Mode = Normal
  949.                             Tempo = 120
  950.  
  951.         Tn        Tempo,  which  may range from 32 to 255,  controls  the 
  952.  
  953.                   number  of quarter-notes per second.   The  default  is 
  954.  
  955.                   120.
  956.  
  957.         <         Moves down one octave.   If already at octave 0,  stays 
  958.  
  959.                   there.
  960.  
  961.         >         Moves  up one octave.   If already at octave  7,  stays 
  962.  
  963.                   there.
  964.  
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.                                        10
  982.  
  983.  
  984.  
  985.  
  986.  
  987.         Sound Effects                                        Making Noise
  988.  
  989.  
  990.  
  991.                                   Making Noise 
  992.                             (Using Sound and Sounds)
  993.  
  994.         The  function  sound() and sounds() allow the program to  produce 
  995.  
  996.         sounds other than those on the normal musical scale.  The penalty 
  997.  
  998.         for  this flexibility is that more work is usually  required   to 
  999.  
  1000.         specify the frequency and duration of each sound, especially when 
  1001.  
  1002.         forming complex noises.
  1003.  
  1004.         To  produce a single sound,  call  sound().   Simple,  eh?   Just 
  1005.  
  1006.         provide  the  frequency  as  (Hz * 100),  and  the  duration  in 
  1007.  
  1008.         milliseconds.   To produce a series of sounds,  you can set up an 
  1009.  
  1010.         array  of  structures  of the type sound_element and pass  it  to 
  1011.  
  1012.         sounds(),  along  with a count of the number of  elements.   Each 
  1013.  
  1014.         element  takes up six bytes;  four bytes for the frequency  (long 
  1015.  
  1016.         int) and two for the duration (unsigned).
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033.  
  1034.  
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.                                        11
  1058.  
  1059.  
  1060.  
  1061.  
  1062.  
  1063.         Sound Effects                               Function Descriptions
  1064.  
  1065.  
  1066.  
  1067.                               Summary Function List
  1068.  
  1069.         These  functions  are described in detail later,  but here  is  a 
  1070.  
  1071.         comprehensive list of the functions in the library, with the file 
  1072.  
  1073.         location and a short description of each one.
  1074.  
  1075.  
  1076.                                    High-Level
  1077.  
  1078.         sound_init   SOUNDCTL    Call to set mode, etc.
  1079.         sound_done   SOUNDCTL    Call before exit.
  1080.         sound_mode   SOUNDCTL    Returns current mode.
  1081.         sound        SOUND       Make a noise.
  1082.         sounds       SOUND       Make several noises.
  1083.         play         PLAY        Make pleasant noises using easy notation.
  1084.         quiet        SOUNDOUT    Kill playing background music.
  1085.         sound_bchk   SOUNDOUT    Check status of sound buffer.
  1086.          
  1087.                                     Low-Level
  1088.                   
  1089.         spkr_on      SPKRCTL     Speaker on  (continuous noise) 
  1090.         spkr_off     SPKRCTL     Speaker off (blissful quiet)
  1091.         spkr_freq    SPKRCTL     Sets the frequency of the noise
  1092.         spkr_cntr    SPKRCTL     Sets frequency also (see func description)
  1093.                   
  1094.                              Internal --- DO NOT USE
  1095.                   
  1096.         sound_out    SOUNDOUT    Interrupt-driven output routine
  1097.         snd_irh      SOUNDINT    Assembly interface for sound_out
  1098.         snd_brk      SOUNDINT    Built-in break handler
  1099.         snd_giv      SOUNDINT    Utility - get interrupt vector
  1100.         snd_siv      SOUNDINT    Utility - set interrupt vector
  1101.         delay        DELAY       Internal delay loop used in mode 1               
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.                                        12
  1126.  
  1127.  
  1128.  
  1129.  
  1130.  
  1131.         Sound Effects                               Function Descriptions
  1132.  
  1133.  
  1134.                               SOUND_INIT and SOUND_DONE
  1135.  
  1136.         Purpose:
  1137.  
  1138.              Set up and tear down the interrupt vectors needed to provide 
  1139.  
  1140.              sound.
  1141.  
  1142.         Summary:
  1143.  
  1144.              int sound_init(mode,install_bh)
  1145.              int mode, install_bh;
  1146.  
  1147.              int sound_done();
  1148.  
  1149.         Parameters:
  1150.  
  1151.              Mode:          Must be in the range 1..5;  sets the mode for 
  1152.  
  1153.                             subsequent   calls  to  sound()  and  play().  
  1154.  
  1155.                             Values are:
  1156.  
  1157.                              1  Foreground w/ Delay Loop               
  1158.                              2  Foreground w/ Standard Clock Interrupt 
  1159.                              3  Foreground w/ Enhanced Clock Interrupt
  1160.                              4  Background w/ Standard Clock Interrupt 
  1161.                              5  Background w/ Enhanced Clock Interrupt
  1162.  
  1163.              Install_bh:    If  this  is  0,  no  break-handler  will  be 
  1164.  
  1165.                             installed.   Otherwise,  a break handler will 
  1166.  
  1167.                             be installed.   See Technical Information for 
  1168.  
  1169.                             a complete description of the break handler.
  1170.  
  1171.         Returns:
  1172.  
  1173.              Zero  indicates success.   Sound_init will return -1  if  an 
  1174.  
  1175.              invalid mode is passed.
  1176.  
  1177.         Notes:
  1178.  
  1179.              Sound_init()  may  be called more than once without  harmful 
  1180.  
  1181.              effects.   When called,  any music currently playing will be 
  1182.  
  1183.              killed.   You can use calls to sound_init() to change  modes 
  1184.  
  1185.              during the application.
  1186.  
  1187.              Sound_done() shust off background music and the speaker, and 
  1188.  
  1189.              cleans  up any interrupt vectors which may have been changed 
  1190.  
  1191.              by sound_init().  This lets the application safely exit.
  1192.  
  1193.  
  1194.  
  1195.  
  1196.  
  1197.  
  1198.  
  1199.  
  1200.  
  1201.  
  1202.  
  1203.                                        13
  1204.  
  1205.  
  1206.  
  1207.  
  1208.  
  1209.         Sound Effects                               Function Descriptions
  1210.  
  1211.  
  1212.                                    SOUND_MODE
  1213.  
  1214.         Purpose:
  1215.  
  1216.              Returns  the current sound mode.   See sound_init() for  the 
  1217.  
  1218.              possible values.
  1219.  
  1220.         Summary:
  1221.  
  1222.              int sound_mode()
  1223.  
  1224.         Parameters:
  1225.  
  1226.              None
  1227.  
  1228.         Returns:
  1229.  
  1230.              Returns   current  mode,   range  1-5.    See   sound_init() 
  1231.  
  1232.              description for complete list of modes.
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.  
  1259.  
  1260.  
  1261.  
  1262.  
  1263.  
  1264.  
  1265.  
  1266.  
  1267.  
  1268.  
  1269.  
  1270.  
  1271.                                        14
  1272.  
  1273.  
  1274.  
  1275.  
  1276.  
  1277.         Sound Effects                               Function Descriptions
  1278.  
  1279.  
  1280.                                 SOUND  &  SOUNDS
  1281.  
  1282.         Purpose:
  1283.  
  1284.              Produce a sound or sounds for a certain time.
  1285.  
  1286.         Summary:
  1287.  
  1288.              void sound(freq,dur)
  1289.              long freq;
  1290.              unsigned dur;
  1291.  
  1292.              void sounds(n,sa)
  1293.              int n;
  1294.              struct sound_desc *sa;     
  1295.  
  1296.         Parameters:
  1297.  
  1298.              freq:  Frequency in MHz * 10.   Ex: 400MHz passed as 40000L.  
  1299.  
  1300.                     The  allowable  range is ???.   A frequency of  0  is 
  1301.  
  1302.                     treated  as "silence".   A sound with a duration of 0 
  1303.  
  1304.                     is ignored.
  1305.  
  1306.              dur:   Duration of sound in milliseconds.
  1307.  
  1308.              n:     Number of sounds passed.   Numbers less than zero are 
  1309.  
  1310.                     treated as zero.
  1311.  
  1312.              sa:    Array  of  sound  descriptions,   each  containing  a 
  1313.  
  1314.                     frequency  and a duration,  as per the freq  and  dur 
  1315.  
  1316.                     parameters passed to sound.
  1317.  
  1318.         Returns:
  1319.  
  1320.              0 if all parameters all OK
  1321.             -1 if error in parameters (out of range).     
  1322.  
  1323.         Error Handling:
  1324.  
  1325.              Checks parameter range only.
  1326.  
  1327.         Notes:
  1328.  
  1329.              These  functions  are effected by the mode currently set  by 
  1330.  
  1331.              the  most recent call to sound_init.   They may be used  for 
  1332.  
  1333.              either foreground or background operation.
  1334.  
  1335.  
  1336.  
  1337.  
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.                                        15
  1346.  
  1347.  
  1348.  
  1349.  
  1350.  
  1351.         Sound Effects                               Function Descriptions
  1352.  
  1353.  
  1354.                                       PLAY
  1355.  
  1356.         Purpose:
  1357.  
  1358.              Play a series of musical notes using an easy notation.
  1359.  
  1360.         Summary:
  1361.  
  1362.              void play(music)
  1363.              char *music;
  1364.  
  1365.         Parameters:
  1366.  
  1367.              The  music  parameter  is a string which  can  contain  many 
  1368.  
  1369.              complex  operations,  all  of  which are  described  in  the 
  1370.  
  1371.              section "Making Music".
  1372.  
  1373.         Returns:
  1374.  
  1375.              Nothing.
  1376.  
  1377.         Error Handling:
  1378.  
  1379.              Any  problems encountered in the music string are handled as 
  1380.  
  1381.              described in the section "Making Music."
  1382.  
  1383.         Notes:
  1384.  
  1385.              The  play function introduces an amount of overhead in terms 
  1386.  
  1387.              of code size,  as it must parse the string.  A program which 
  1388.  
  1389.              is  memory-critical  should use  sounds();   of  course  the 
  1390.  
  1391.              programmer  must  then perform the task of  converting  each 
  1392.  
  1393.              note into one or more sounds. 
  1394.  
  1395.              See "Making Music" for details on play()'s operation.
  1396.  
  1397.  
  1398.  
  1399.  
  1400.  
  1401.  
  1402.  
  1403.  
  1404.  
  1405.  
  1406.  
  1407.  
  1408.  
  1409.  
  1410.  
  1411.  
  1412.  
  1413.  
  1414.  
  1415.  
  1416.  
  1417.  
  1418.  
  1419.                                        16
  1420.  
  1421.  
  1422.  
  1423.  
  1424.  
  1425.         Sound Effects                               Function Descriptions
  1426.  
  1427.  
  1428.                         DIRECT SPEAKER CONTROL FUNCTIONS
  1429.  
  1430.         Purpose:
  1431.  
  1432.              Direct control of speaker port.
  1433.  
  1434.         Summary:
  1435.  
  1436.              void spkr_on()
  1437.  
  1438.              void spkr_off()
  1439.  
  1440.              void spkr_freq(f)
  1441.              long f;
  1442.  
  1443.              void spkr_cntr(c)
  1444.              unsigned c;
  1445.  
  1446.         Parameters:
  1447.  
  1448.              The  parameter  to spkr_freq is the frequency in MHz to  set 
  1449.  
  1450.              the  speaker to.   The parameter to spkr_cntr is the  actual 
  1451.  
  1452.              counter  value to be passed to the 8253  timer  chip.   Note 
  1453.  
  1454.              that  the only functions to actually turn the speaker on  or 
  1455.  
  1456.              off  are spkr_on and spkr_off;   setting the frequency  does 
  1457.  
  1458.              not imply activating or deactivating the speaker.
  1459.  
  1460.  
  1461.         Returns:
  1462.  
  1463.              Nothing.
  1464.  
  1465.         Error Handling:
  1466.  
  1467.              None.  
  1468.  
  1469.         Notes:
  1470.  
  1471.              Unlike sound(),  sounds(),  and play(),  which can be freely 
  1472.  
  1473.              intermixed,  use  of  these  low-level  functions  while  in 
  1474.  
  1475.              background  mode  can disrupt the background  music.   While 
  1476.  
  1477.              this  is far from fatal,  is can produce unpleasant  noises.  
  1478.  
  1479.              Call quiet() before using these functions.
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489.  
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495.                                        17
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501.         Sound Effects                               Function Descriptions
  1502.  
  1503.  
  1504.  
  1505.                            BACKGROUND-BUFFER FUNCTIONS
  1506.  
  1507.         Purpose:
  1508.  
  1509.              Control music playing in background.
  1510.  
  1511.         Summary:
  1512.  
  1513.              void quiet()
  1514.  
  1515.              void sound_bchk(used,avail)
  1516.              int *used, *avail;
  1517.  
  1518.         Parameters:         
  1519.  
  1520.              quite:              none.
  1521.  
  1522.              sound_buffchk():    used to return values (see below).
  1523.  
  1524.         Returns:            
  1525.  
  1526.              quiet() has no return value.
  1527.  
  1528.              buffchk() returns the number of sound elements in the buffer 
  1529.  
  1530.              and the number of free elements,  via the parameters  "used" 
  1531.  
  1532.              and "avail" respectively.
  1533.  
  1534.         Error Handling:     
  1535.  
  1536.              None.
  1537.  
  1538.         Notes:
  1539.  
  1540.              A  call to quiet will kill any background music,  flush  the 
  1541.  
  1542.              buffer, and shut off the speaker.  
  1543.  
  1544.  
  1545.  
  1546.  
  1547.  
  1548.  
  1549.  
  1550.  
  1551.  
  1552.  
  1553.  
  1554.  
  1555.  
  1556.  
  1557.  
  1558.  
  1559.  
  1560.  
  1561.  
  1562.  
  1563.  
  1564.                                        18
  1565.  
  1566.  
  1567.  
  1568.  
  1569.  
  1570.         Sound Effects                               Technical Information
  1571.  
  1572.  
  1573.  
  1574.                              =======================
  1575.                               TECHNICAL INFORMATION
  1576.                              =======================
  1577.  
  1578.         I  have tried to be clear and precise in these  technical  notes.  
  1579.  
  1580.         Should  you  find any errors in code or  documentation,  or  have 
  1581.  
  1582.         suggestions for improvement,   I would appreciate your  feedback.  
  1583.  
  1584.         These  paragraphs  are  not for the faint  of  heart.   For  more 
  1585.  
  1586.         details,  I  suggest some of Peter Norton's  excellent PC  books, 
  1587.  
  1588.         and the ever-cryptic but fact-filled IBM Technical Reference.
  1589.  
  1590.         Here is a brief summary of this section:
  1591.  
  1592.  
  1593.              Timing Sound Duration (MODES)
  1594.                 Delay Loop - How, Pro & Con
  1595.                 Timer Interrupt - How, Pro & Con
  1596.                 Enhanced Interrupt - How, Pro & Con
  1597.  
  1598.              Sound Generation Mechanics
  1599.                 Speaker Logic
  1600.                 Setting The Frequency
  1601.  
  1602.              The Background Buffer
  1603.  
  1604.              The Break Handler
  1605.  
  1606.              The Interrupt Handler
  1607.  
  1608.                   
  1609.                             =========================
  1610.                             TIMING THE SOUND DURATION
  1611.                             =========================
  1612.  
  1613.         There  are five modes in which the sound routines can run.   They 
  1614.  
  1615.         define  WHEN  the music plays,  and how the  proper  DURATION  is 
  1616.  
  1617.         determined.  The FREQUENCY of the sounds is always determined and 
  1618.  
  1619.         produced by the same method regardless of mode,  and is described 
  1620.  
  1621.         a few paragraphs below.
  1622.  
  1623.           Mode   When          Timing
  1624.           ----   ----          ------
  1625.             1    Foreground    Delay Loop
  1626.             2    Foreground    Standard Clock Interrupt
  1627.             3    Foreground    Enhanced Clock Interrupt
  1628.             4    Background    Standard Clock Interrupt
  1629.             5    Background    Enhanced Clock Interrupt
  1630.  
  1631.         The  default mode is mode 1.   It poses the fewest problems,  but 
  1632.  
  1633.         has some limitations.  The timing is achieved using a delay loop, 
  1634.  
  1635.         which  is  simply  a small piece of code with a  known  execution 
  1636.  
  1637.         time.   By looping through this piece of code you can approximate 
  1638.  
  1639.         the duration of the sounds fairly well.   
  1640.  
  1641.  
  1642.  
  1643.                                        19
  1644.  
  1645.  
  1646.  
  1647.  
  1648.  
  1649.         Sound Effects                               Technical Information
  1650.  
  1651.  
  1652.         Unfortunately,  the  delay loop method has problems.   It is very 
  1653.  
  1654.         dependent on the hardware, for one thing.   A processor  running 
  1655.  
  1656.         at  8MHz  will  execute the code in close to half the time  of  a 
  1657.  
  1658.         standard  PC running at 4.77MHz,  so the sounds will be  shorter.  
  1659.  
  1660.         Also, the timing becomes dependent on processor type.  The normal 
  1661.  
  1662.         PC has an 8088;  on an  AT&T 6300  (8086) or on a PC-AT (80286),
  1663.  
  1664.         there  will be a noticeable timing difference due to the  varying 
  1665.  
  1666.         bus widths and execution speeds, even if the base processor clock 
  1667.  
  1668.         speed was the same.   And as the coup-de-grace,  this method will 
  1669.  
  1670.         be thrown off by the occurance of interrupts.   An interrupt will 
  1671.  
  1672.         cause  other code to be executed which will extend the  execution 
  1673.  
  1674.         time of the loop.
  1675.  
  1676.         Fortunately there is a better timing method available.   There is 
  1677.  
  1678.         an  on-board main system clock chip which provides a pulse with a 
  1679.  
  1680.         frequency  of about 1.19MHz.   This is stepped down to  a  more 
  1681.  
  1682.         reasonable rate by the 8253 counter chip.   The most familiar use 
  1683.  
  1684.         of  this is to provide the system tick,  which occurs 18.2 time a 
  1685.  
  1686.         second and generates the hardware clock interrupt (IRQ0,  INT 8).  
  1687.  
  1688.         This  is  generally  independent  of  the  clock  speed  of   the 
  1689.  
  1690.         processor,  and so is a good timing source across the extended PC 
  1691.  
  1692.         family.
  1693.  
  1694.         We can use this system tick to time the sounds.   Both foreground 
  1695.  
  1696.         and  background methods can do this;   the foreground modes  will 
  1697.  
  1698.         merely  wait  around  until the sound  is  over.   Therefore  the 
  1699.  
  1700.         following  discussions  of the use of the system  tick  interrupt 
  1701.  
  1702.         apply to both foreground and background.
  1703.  
  1704.         The  standard  interrupt modes (2 and 4) tie  into  the  software 
  1705.  
  1706.         interrupt  vector  0x1C.   This  is the  "user  timer  interrupt" 
  1707.  
  1708.         vector, and is provided by the BIOS as a nice way to tie software 
  1709.  
  1710.         into   the  timer.    The  BIOS  interrupt  handler  retains  all 
  1711.  
  1712.         responsibility  for handling the messy tasks of  sending  end-of-
  1713.  
  1714.         interrupt  to  the interrupt controller  chip,  incrementing  the 
  1715.  
  1716.         time-of-day  clock tick,  etc.   This is a clean way to  use  the 
  1717.  
  1718.         system tick to time events.
  1719.  
  1720.                                 Possible Problems
  1721.  
  1722.         There  are  only two possible problems (that I can see) to  using 
  1723.  
  1724.         the  system  tick.   One  is that some  memory-resident  software 
  1725.  
  1726.         packages  are very possessive about interrupts.   That is why  so 
  1727.  
  1728.         many of them are incompatible with each other.   In  theory,  any 
  1729.  
  1730.         number  of  drivers could share the user system tick by  chaining 
  1731.  
  1732.         themselves together,  but they often don't.   This software does, 
  1733.  
  1734.         so it will work with well-behaved utilities.
  1735.  
  1736.         The other problem mainly involves background mode.   If the  user 
  1737.  
  1738.         aborts the program by using control Ctrl-Break, interrupt vectors 
  1739.  
  1740.         will  not be restored,  and eventually the system will crash  and 
  1741.  
  1742.         need  rebooting.   This  can be prevented by using  the  provided 
  1743.  
  1744.         break handler (see SOUND_INIT) or by calling sound_done() if your 
  1745.  
  1746.         application already provides its own break handler.
  1747.  
  1748.  
  1749.  
  1750.                                        20
  1751.  
  1752.  
  1753.  
  1754.  
  1755.  
  1756.         Sound Effects                               Technical Information
  1757.  
  1758.  
  1759.                                  Enhanced Modes 
  1760.  
  1761.         The  enhanced  interrupt  modes  (3 and  5)  are  not  yet  fully 
  1762.  
  1763.         implemented,  and  are  presently  the  same as  modes  2  and  4 
  1764.  
  1765.         respectively.   In a future release, they will follow the example 
  1766.  
  1767.         of  BASIC and change the system tick generation to provide better 
  1768.  
  1769.         resolution.   This tick is sped up by a factor of 4.   The  sound 
  1770.  
  1771.         system  must intercept this tick at the INT 8 level,  and provide 
  1772.  
  1773.         the tick to the BIOS only every fourth time,  to keep the  system 
  1774.  
  1775.         time-of-day  clock from speeding ahead.   The advantage is better 
  1776.  
  1777.         time resolution.
  1778.  
  1779.         The  disadvantages to the enhanced modes include all of the  ones 
  1780.  
  1781.         given for the standard clock modes, plus the code has to dig even 
  1782.  
  1783.         deeper  into  territory  where  applications  programs  are   not 
  1784.  
  1785.         encouraged  to  venture.   Still,  taking proper care to  restore 
  1786.  
  1787.         vectors upon exit,  this should be clean enough for everyday use.  
  1788.  
  1789.         There is a question as to the value of the more precise timing of 
  1790.  
  1791.         the sounds, which may not be of great importance anyway.
  1792.  
  1793.  
  1794.                            ==========================
  1795.                            SOUND GENERATION MECHANICS
  1796.                            ==========================
  1797.  
  1798.                                   Speaker Logic
  1799.  
  1800.         The  sounds  are produced using the speaker attached to  the  IBM 
  1801.  
  1802.         motherboard.   The  speaker  is pulsed to provide  sound  of  the 
  1803.  
  1804.         desired  frequency.   The pulsing action is provided courtesy  of 
  1805.  
  1806.         the   main   system  clock,   stepped  down  through   the   8253 
  1807.  
  1808.         programmable counter.  There is also an on/off switch provided by 
  1809.  
  1810.         a  bit  in  the  PPI chip.   The  speaker  control  circuitry  is 
  1811.  
  1812.         logically (not electrically) diagrammed as:
  1813.  
  1814.             +-------+    +--------+
  1815.             | 8284A |--->| 8253-5 |----+                      
  1816.             +-------+    +--------+    |    ____               /|
  1817.                                        +--> )   \             / |
  1818.                                             |AND >---------->[  |
  1819.                                        +--> )___/             \ |
  1820.                       +-----+          |                       \|
  1821.                       | PPI |----------+                    
  1822.                       +-----+                                Speaker
  1823.  
  1824.         The  PPI switch is accessed as bits 0 and 1 at I/O address  0x61.  
  1825.  
  1826.         When the bits are set the speaker switch is on,  when cleared the 
  1827.  
  1828.         speaker  switch  is  off.    See  the  spkr_on()  and  spkr_off() 
  1829.  
  1830.         functions to see how to do this from C.   Note that the value  of 
  1831.  
  1832.         the  PPI  is  first read,  the required bits  changed,  and  then 
  1833.  
  1834.         written  out.   This is necessary because the PPI has many  other 
  1835.  
  1836.         uses for the rest of the bits.
  1837.  
  1838.  
  1839.  
  1840.  
  1841.  
  1842.                                        21
  1843.  
  1844.  
  1845.  
  1846.  
  1847.  
  1848.         Sound Effects                               Technical Information
  1849.  
  1850.  
  1851.                         Setting Frequency using the 8253
  1852.  
  1853.         The   system  clock  oscillates  at  about  1.19MHz.    The  8253 
  1854.  
  1855.         programmable counter steps down this frequency by sending a pulse 
  1856.  
  1857.         for  every so many input pulses.   The well-known system tick  of 
  1858.  
  1859.         18.2  ticks per second is obtained using this chip and a  divisor 
  1860.  
  1861.         of 0xFFFF.  The chip has two counters;  while one output connects 
  1862.  
  1863.         to  the system clock interrupt,  the other ties to  the  speaker.  
  1864.  
  1865.         So,  by  changing the divisor in the counter,  we can change  the 
  1866.  
  1867.         frequency  of  the  pulses  sent to the  speaker  and  hence  the 
  1868.  
  1869.         frequency of the sound it produces.
  1870.  
  1871.         For  more  details on the use of the 8253  programmable  counter, 
  1872.  
  1873.         consult the Intel part spec.  
  1874.  
  1875.  
  1876.                               =====================
  1877.                               THE BACKGROUND BUFFER
  1878.                               =====================
  1879.  
  1880.         Sounds  to be played in modes 2 through 5 are placed in a  buffer 
  1881.  
  1882.         and  played  under interrupt control.   Since this buffer  is  of 
  1883.  
  1884.         finite size, it is possible to overflow.  Overflowing sounds will 
  1885.  
  1886.         be discarded.
  1887.  
  1888.         Each entry takes up four (4) bytes,  and the number of entries in 
  1889.  
  1890.         the  buffer  is fixed when you build the library.   The  compiled 
  1891.  
  1892.         files on the disk have a default buffer size of 64 entries.   You 
  1893.  
  1894.         can   change  this  by  changing  the  value  of   the   constant 
  1895.  
  1896.         SND_BUFFSIZE in SOUND.H and recompiling the system.
  1897.  
  1898.         Each  note sent to play() will usually generate two entries,  one 
  1899.  
  1900.         for the note and one for the inter-note pause.  Using sound() and 
  1901.  
  1902.         sounds() will generate exactly one entry per sound element.
  1903.  
  1904.         The  sound buffer is circular,  so that as entries are output  to 
  1905.  
  1906.         the speaker,  more can be added.   Check the status of the buffer 
  1907.  
  1908.         using the function sound_bchk(),  which will get you both  the 
  1909.  
  1910.         number of entries used and the number of entries free.
  1911.  
  1912.  
  1913.                                ===================
  1914.                                 THE BREAK HANDLER
  1915.                                ===================
  1916.  
  1917.         The  built-in  break handler has a simplistic view of  life.   It 
  1918.  
  1919.         accepts  control,  calls sound_done() to clean  up,  then  passes 
  1920.  
  1921.         control  to the break handler that precedes it.   This means that 
  1922.  
  1923.         the usual net result is an exit to DOS, but safely.  Feel free to 
  1924.  
  1925.         use  the  given function as a template,  but  keep  the  original 
  1926.  
  1927.         around in case you accidentally muck it up a bit.
  1928.  
  1929.  
  1930.  
  1931.  
  1932.  
  1933.  
  1934.                                        22
  1935.  
  1936.  
  1937.  
  1938.  
  1939.  
  1940.         Sound Effects                               Technical Information
  1941.  
  1942.  
  1943.                              =======================
  1944.                               THE INTERRUPT HANDLER
  1945.                              =======================
  1946.  
  1947.         This  refers   to  the timer interrupt  handler,  a  very  simple 
  1948.  
  1949.         routine.   Its  job is to save the registers,  load  the  segment 
  1950.  
  1951.         registers that C functions expect, call sound_out() to do all the 
  1952.  
  1953.         work,  and restore all the registers.   It then calls the routine 
  1954.  
  1955.         which  was  previously  the interrupt handler.   This is  a  nice 
  1956.  
  1957.         courteous  thing  to do,  as it allows sharing of  this  valuable 
  1958.  
  1959.         resource, the system tick.
  1960.  
  1961.  
  1962.  
  1963.  
  1964.  
  1965.  
  1966.  
  1967.  
  1968.  
  1969.  
  1970.  
  1971.  
  1972.  
  1973.  
  1974.  
  1975.  
  1976.  
  1977.  
  1978.  
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984.  
  1985.  
  1986.  
  1987.  
  1988.  
  1989.  
  1990.  
  1991.  
  1992.  
  1993.  
  1994.  
  1995.  
  1996.  
  1997.  
  1998.  
  1999.  
  2000.  
  2001.  
  2002.  
  2003.  
  2004.  
  2005.  
  2006.                                        23
  2007.  
  2008.  
  2009.  
  2010.  
  2011.  
  2012.         Sound Effects                               Technical Information
  2013.  
  2014.  
  2015.                               WHEN THINGS GO WRONG
  2016.  
  2017.         First of all,  please feel free to ask me (the author) questions.  
  2018.  
  2019.         I cannot accept phone questions at this time, mainly because I am 
  2020.  
  2021.         not  near any particular phone during the day.   You can reach me 
  2022.  
  2023.         by mail or by CompuServe EASYMAIL (73047,3715).   But first,  see 
  2024.  
  2025.         if the following list has your problem on it.
  2026.  
  2027.  
  2028.         "I cannot read the distribution disk."
  2029.  
  2030.              The  distribution  disk is provided in IBM  double-sided  9-
  2031.  
  2032.              sector format.  You will not be able to read it with single-
  2033.  
  2034.              sided drives or with a DOS version before 2.0.
  2035.  
  2036.              If you can read such disks normally,  there is a chance that 
  2037.  
  2038.              the diskette has been damaged in transit.  Please return the 
  2039.  
  2040.              diskette and you will get another copy, no charge.
  2041.  
  2042.  
  2043.         "The files will not compile on my system."
  2044.  
  2045.              If  you are using a non-supported compiler,  all I can do is 
  2046.  
  2047.              refer you to the section on Setting Up for Other Compilers".  
  2048.  
  2049.              If  you  do  get  everything working,  even  if  just  on  a 
  2050.  
  2051.              different  version  of  Lattice  or  Manx/Aztec,   I   would 
  2052.  
  2053.              appreciate  if you let me know.   I will do everything I can 
  2054.  
  2055.              to help you get it compiled and running,  but without a copy 
  2056.  
  2057.              of  the  compiler (DO NOT SEND ME  COPYRIGHTED  SOFTWARE)  I 
  2058.  
  2059.              cannot promise much.
  2060.  
  2061.              Be  sure  you have the file SOUND.H where your compiler  can 
  2062.  
  2063.              find  it.   Also be sure that you have enough room  on  your 
  2064.  
  2065.              diskettes for the compiler's output.
  2066.  
  2067.         "My system locks up when I run a program using sound."
  2068.  
  2069.              First,  if you have any memory-resident utilities, let's get 
  2070.  
  2071.              rid  of them for the moment.   De-install them,  even remove 
  2072.  
  2073.              them  from your AUTOEXEC.BAT file,  then reboot the  system.  
  2074.  
  2075.              Try the program again.  If it still crashes ...
  2076.  
  2077.              Make sure you are calling quiet() before the program exits.
  2078.  
  2079.  
  2080.  
  2081.  
  2082.  
  2083.  
  2084.  
  2085.  
  2086.  
  2087.  
  2088.  
  2089.  
  2090.  
  2091.  
  2092.                                        24
  2093.  
  2094.  
  2095.  
  2096.  
  2097.  
  2098.         Sound Effects                               Technical Information
  2099.  
  2100.  
  2101.  
  2102.  
  2103.                                    ==========
  2104.                                    CHANGE LOG
  2105.                                    ==========
  2106.  
  2107.         Version 1.0b  (OPEN)
  2108.  
  2109.                 Added new file SOUNDP.H containing prototypes for all
  2110.                 user-callable functions, for compilers which support
  2111.                 prototyping.
  2112.  
  2113.                 Added support for Ecosoft Eco-C88 compiler.
  2114.  
  2115.                 Compressed definition of Install in SOUNDCTL, by combining
  2116.                 all definitions using typedef's and #defines to allow for
  2117.                 compiler and library differences.
  2118.  
  2119.  
  2120.         Version 1.0a  (2/20/85)
  2121.  
  2122.                 Delay now written in assembler.
  2123.  
  2124.                 Corrected error in play(), which would not play dotted
  2125.                 notes if a length was also specified.  
  2126.  
  2127.                 Corrected error in SOUND.H's definition of MS2TICKS, which
  2128.                 was miscalculated and rounding badly.
  2129.  
  2130.  
  2131.  
  2132.  
  2133.  
  2134.  
  2135.  
  2136.  
  2137.  
  2138.  
  2139.  
  2140.  
  2141.  
  2142.  
  2143.  
  2144.  
  2145.  
  2146.  
  2147.  
  2148.  
  2149.  
  2150.  
  2151.  
  2152.  
  2153.  
  2154.  
  2155.  
  2156.  
  2157.  
  2158.                                        25
  2159.  
  2160.  
  2161.  
  2162.  
  2163.  
  2164.