home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / PLAYLIB.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  138 lines

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