summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/playlib.c
blob: 5ac3ddd4941b18e319989176bf201e9c0bd1874d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
/*
**  PLAYLIB.C
**
**  Public domain for TC only by Lynn R. Lively
**  Modified by Bob Stout
*/

#include <dos.h>
#include <stdlib.h>
#include "sound.h"

#ifdef __ZTC__
 #include <int.h>
 #define interrupt
#else
 static void (interrupt far *n_oldtmint) (void);
#endif

#define TIMER_TICK_INTR 0x1c

static NOTE *     n_buff;
static unsigned   n_buff_sz;
static NOTE *     n_head;
static NOTE *     n_tail;
static unsigned   play_duration;
static unsigned   play_freq;

/*
** Add note to note buff. Return = 1 (note added), 0 (Out of note buff)
*/

int playb_note (unsigned freq, unsigned duration)
{
      if (++n_tail == (n_buff + n_buff_sz))
            n_tail = n_buff;

      if (n_tail == n_head)
      {
            --n_tail;
            return (0);
      }

      n_tail->freq     = freq;
      n_tail->duration = duration;

      return (1);
}

/*
** ISR for background music.
*/

#ifndef __ZTC__
 static void interrupt far play_intr (void)
#else
 static int play_intr (struct INT_DATA *idp)
#endif
{
      int_off ();

#ifndef __ZTC__
      (*n_oldtmint) ();                   /* Call Old timer interrupt.  */
#else
      int_prev(idp);
#endif

      if (play_duration == 0)
      {
            soundoff ();

            if (++n_head == (n_buff + n_buff_sz))
                  n_head = n_buff;

            if (n_head == n_tail)
            {
                  --n_head;
                  int_on ();
                  return;
            }

            play_duration = n_head->duration;
            if (0 != (play_freq = n_head->freq))
                  soundon();
            dosound (play_freq);
      }
      else  --play_duration;

      int_on ();

#ifdef __ZTC__
      return 1;                           /* Don't chain                */
#endif
}

/*
** Call this function to init background music. buff_sz is number of
** notes in the note buffer. Returns pointer to buff or NULL if
** out of heap space.
*/

NOTE * playb_open (unsigned buff_sz)
{
      n_buff =
      n_head =
      n_tail =  (NOTE *) calloc (buff_sz, sizeof (NOTE));

      if (n_buff != (NOTE *) NULL)
      {
            n_buff_sz     = buff_sz;

            play_duration =
            play_freq     = 0;
            
#ifdef __ZTC__
            int_intercept(TIMER_TICK_INTR, play_intr, 256);
#else
            n_oldtmint    = getvect (TIMER_TICK_INTR);
            setvect (TIMER_TICK_INTR, play_intr);
#endif
      }
      return (n_buff);
}

/*
** Return things to normal and free allocated space.
*/

void playb_close (void)
{
      soundoff ();
#ifndef __ZTC__
      setvect (TIMER_TICK_INTR, n_oldtmint);
#else
      int_restore(TIMER_TICK_INTR);
#endif
      free (n_buff);
}