home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / msdos / turbo_c / sound.arc / SOUND.C < prev    next >
Text File  |  1987-06-20  |  4KB  |  111 lines

  1. #include <dos.h>
  2.  
  3. int _delaycounter;  /* used by delay() to count number of loops = 1/100 sec */
  4.  
  5. void setupdelay(void);
  6. void delay(int HundredthSec);
  7. void sound(int Freq, int Duration);
  8. void beep(void);
  9.  
  10. /*
  11.  *  I wrote these procedures not because I'm an expert in C (by no means)
  12.  *  but because I was used to the Turbo Pascal functions for:
  13.  *
  14.  *  TP  delay(time) <->  TC delay(time)
  15.  *  TP  sound(freq) <->  TC sound(freq,time)
  16.  *  TP  nosound     <--^
  17.  *
  18.  *  I created my own sound() and delay() functions.  I did not create a
  19.  *  nosound() function simply because I included an additional parameter
  20.  *  in the sound statement to ask for the length of time that the speaker
  21.  *  should be left on.
  22.  *
  23.  *  NOTE:  In order to use either the sound() or the delay() functions you
  24.  *         MUST have called setupdelay() at least once.  (you only need to
  25.  *         do it once, thereafter the delay counter is already set up).
  26.  *
  27.  *  The delay() function accepts a parameter signifying one hundredth (1/100)
  28.  *  of a second.  In other words  delay(50)  will cause a delay of 1/2 second
  29.  *  while delay(505) will pause for 5.05 seconds.
  30.  *
  31.  *  The sound() function accepts two parameters, a frequency and a time
  32.  *  delay.  Since in TP I was always doing sound(x); delay; nosound;  I
  33.  *  figured why not include all three statements in one function.
  34.  *
  35.  *  The beep() function is just a sample bell that I use all of the time.
  36.  *  Anyway, I hope this benefits somebody...
  37.  *                                             David W. Terry 71560,3550
  38.  */
  39.  
  40. /*************************************************************************
  41.  *
  42.  *  setupdelay()   MUST BE CALLED IN ORDER TO USE:  delay() or sound()
  43.  */
  44.  
  45. void setupdelay(void) {
  46.   unsigned long int far *Timer = 0x46CL;
  47.   unsigned long int Stop;
  48.   int register Count,SubCount;
  49.  
  50.   Count = 0;
  51.   disable();
  52.   Stop = *Timer;
  53.   enable();
  54.   while (*Timer == Stop);       /* wait until the beginning of a clock tick */
  55.  
  56.   disable();
  57.   Stop = *Timer + 4;            /* Stop = *Timer + 4 clock ticks */
  58.   enable();
  59.   while (*Timer < Stop) {       /* wait until stopping point is reached   */
  60.     Count++;
  61.     for (SubCount = 200; SubCount; SubCount--);
  62.   }
  63.   _delaycounter =((long)Count * 455) / 10000; /* number of loops = 1/100 sec */
  64. }
  65.  
  66. /*************************************************************************
  67.  *
  68.  *  delay()   parameter = 1/100 sec.  i.e.  delay(50) = pause of 1/2 sec.
  69.  */
  70.  
  71. void delay(int HundredthSec) {
  72.  
  73.   unsigned int register Count,SubCount;
  74.  
  75.   for (Count = HundredthSec * _delaycounter; Count; Count--)
  76.     for (SubCount = 200; SubCount; SubCount--);
  77. }
  78.  
  79. /*************************************************************************
  80.  *
  81.  *  sound()   parameters = Sound Frequency and Duration of sound - see delay()
  82.  */
  83.  
  84. void sound(int Freq, int Duration) {
  85.   unsigned int Count;
  86.   unsigned char Off,On;
  87.  
  88.   Count = 1193280L / Freq;  /* determine the timer frequency */
  89.  
  90.   outportb(67,182);         /* set up the timer */
  91.   outportb(66,Count & 255);
  92.   outportb(66,Count >> 8);
  93.  
  94.   Off = inportb(97) & 0xFC; /* get current status   */
  95.   On  = Off | 3;            /* turn on bits 0 and 1 */
  96.  
  97.   outportb(97,On);          /* turn on the speaker            */
  98.   delay(Duration);          /* wait for Duration/1000 seconds */
  99.   outportb(97,Off);         /* turn off the speaker           */
  100. }
  101.  
  102. /*************************************************************************
  103.  *
  104.  *  beep()  An example bell I use in my programs
  105.  */
  106.  
  107. void beep(void) {
  108.   sound(440,10);
  109.   sound(220,10);
  110. }
  111.