home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_01 / 2n01068a < prev    next >
Text File  |  1990-12-02  |  3KB  |  136 lines

  1. /*
  2. **  Public domain by: Lynn R. Lively
  3. **  Modified by: Bob Stout
  4. */
  5.  
  6. #include <dos.h>
  7. #include <stdlib.h>
  8. #include "sound.h"
  9.  
  10. #ifdef __ZTC__
  11.  #include <int.h>
  12.  #define interrupt
  13. #else
  14.  static void (interrupt far *n_oldtmint) (void);
  15. #endif
  16.  
  17. #define TIMER_TICK_INTR 0x1c
  18.  
  19. static NOTE *     n_buff;
  20. static unsigned   n_buff_sz;
  21. static NOTE *     n_head;
  22. static NOTE *     n_tail;
  23. static unsigned   play_duration;
  24. static unsigned   play_freq;
  25.  
  26. /*
  27. ** Add note to note buff. Return = 1 (note added), 0 (Out of note buff)
  28. */
  29.  
  30. int playb_note (unsigned freq, unsigned duration)
  31. {
  32.         if (++n_tail == (n_buff + n_buff_sz))
  33.                 n_tail = n_buff;
  34.  
  35.         if (n_tail == n_head)
  36.         {
  37.                 --n_tail;
  38.                 return (0);
  39.         }
  40.  
  41.         n_tail->freq     = freq;
  42.         n_tail->duration = duration;
  43.  
  44.         return (1);
  45. }
  46.  
  47. /*
  48. ** ISR for background music.
  49. */
  50.  
  51. #ifndef __ZTC__
  52.  static void interrupt far play_intr (void)
  53. #else
  54.  static int play_intr (struct INT_DATA *idp)
  55. #endif
  56. {
  57.         int_off ();
  58.  
  59. #ifndef __ZTC__
  60.         (*n_oldtmint) ();                /* Call Old timer interrupt. */
  61. #else
  62.         int_prev(idp);
  63. #endif
  64.  
  65.         if (play_duration == 0)
  66.         {
  67.                 soundoff ();
  68.  
  69.                 if (++n_head == (n_buff + n_buff_sz))
  70.                         n_head = n_buff;
  71.  
  72.                 if (n_head == n_tail)
  73.                 {
  74.                         --n_head;
  75.                         int_on ();
  76.                         return;
  77.                 }
  78.  
  79.                 play_duration = n_head->duration;
  80.                 if (0 != (play_freq = n_head->freq))
  81.             soundon();
  82.                 dosound (play_freq);
  83.         }
  84.         else    --play_duration;
  85.  
  86.         int_on ();
  87.  
  88. #ifdef __ZTC__
  89.         return 1;                       /* Don't chain */
  90. #endif
  91. }
  92.  
  93. /*
  94. ** Call this function to init background music. buff_sz is number of
  95. ** notes in the note buffer. Returns pointer to buff or NULL if
  96. ** out of heap space.
  97. */
  98.  
  99. NOTE * playb_open (unsigned buff_sz)
  100. {
  101.         n_buff =
  102.         n_head =
  103.         n_tail =  (NOTE *) calloc (buff_sz, sizeof (NOTE));
  104.  
  105.         if (n_buff != (NOTE *) NULL)
  106.         {
  107.                 n_buff_sz     = buff_sz;
  108.  
  109.                 play_duration =
  110.                 play_freq     = 0;
  111.  
  112. #ifdef __ZTC__
  113.                 int_intercept(TIMER_TICK_INTR, play_intr, 256);
  114. #else
  115.                 n_oldtmint    = getvect (TIMER_TICK_INTR);
  116.                 setvect (TIMER_TICK_INTR, play_intr);
  117. #endif
  118.         }
  119.         return (n_buff);
  120. }
  121.  
  122. /*
  123. ** Return things to normal and free allocated space.
  124. */
  125.  
  126. void playb_close (void)
  127. {
  128.         soundoff ();
  129. #ifndef __ZTC__
  130.         setvect (TIMER_TICK_INTR, n_oldtmint);
  131. #else
  132.         int_restore(TIMER_TICK_INTR);
  133. #endif
  134.         free (n_buff);
  135. }
  136.