-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic.c
358 lines (303 loc) · 6.56 KB
/
music.c
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <stdint.h>
#include <stdlib.h>
#include <conio.h>
#define speaker_setup() \
outp( 0x43, 0xB6 )
#define speaker_mute() \
outp( 0x61, inp( 0x61 ) & 0xFC )
#define speaker_unmute() \
outp( 0x61, inp( 0x61 ) | 0x03 )
#define setfreq(_freq) do { \
outp( 0x42, _freq&0xFF ); \
outp( 0x42, (_freq>>8)&0xFF ); \
} while(0)
static unsigned int notes[] = {
/* Provisoning for more notes */
/* 0 */
/* NONE */ 5, /* Silence */
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* 12 */
/* C */ 9121,
/* C# */ 8609,
/* D */ 8126,
/* D# */ 7670,
/* E */ 7239,
/* F */ 6833,
/* F# */ 6449,
/* G */ 6087,
/* G# */ 5746,
/* A */ 5423,
/* A# */ 5119,
/* B */ 4831,
/* 24 */
/* C */ 4560,
/* C# */ 4304,
/* D */ 4063,
/* D# */ 3834,
/* E */ 3619,
/* F */ 3416,
/* F# */ 3224,
/* G */ 3043,
/* G# */ 2873,
/* A */ 2711, /* ~440 */
/* A# */ 2559,
/* B */ 2415,
/* 36 */
/* C */ 2280,
/* C# */ 2152,
/* D */ 2031,
/* D# */ 1917,
/* E */ 1809,
/* F */ 1715,
/* F# */ 1612,
/* G */ 1521,
/* G# */ 1436,
/* A */ 1355,
/* A# */ 1292,
/* B */ 1207,
/* 48 */
/* C */ 1140,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 1000,
/* NONE */ 0 /* Do not use! */
/* 64 */
};
/* ==== TRACKS ==== */
/*
ttnnnnnn - tt is type (see below), nnnnnn is note index from table above (except 111111 which is silence)
dddddddd - Duration in frames
xxxxxxxx - Effect data, optional for some events
| tt | description | Third byte
+----+----------------------------------------+---------------
| 00 | Note on (or pause if note = 111111) | Omitted
| 01 | Arpeggio | ppppdddd - Pattern number and note duration
| 10 | Glissando | Glissando offset/tick (calculated at compile time)
| 11 | Maybe "other effects" | Depends on effect type
Each byte in arpeggio pattern defines an offset from the base note. Pattern ends with ARPPAT_END
*/
#define TRACK_END NULL
#define PATTERN_END 0xFF
#define ARPPAT_END 0xFF
#define TYPE_MASK 0xC0
#define NOTE_MASK 0x3F
#define SILENCE 0
#define NOTE 0x00
#define ARPEGGIO 0x40
#define GLISSANDO 0x80
#define OTHER 0xC0
#define ARP(_pat, _dur) ((_pat<<4)|_dur)
/* Start track 0 */
/* QNL=40 sounds good in Dosbox, but is too slow on real CGA. I have a
* PAL/NTSC type hunch, because 33 sounds pretty good on my PC... */
#define QNL 32
#define ENL (QNL/2)
#define HNL (QNL*2)
static const unsigned char pattern0[] = {
NOTE|34, QNL+ENL-1,
NOTE|SILENCE, 1,
NOTE|41, ENL+QNL+ENL-1,
NOTE|SILENCE, 1,
NOTE|41, ENL-1,
NOTE|SILENCE, 1,
NOTE|39, ENL-1,
NOTE|SILENCE, 1,
NOTE|37, ENL-1,
NOTE|SILENCE, 1,
NOTE|36, ENL-1,
NOTE|SILENCE, 1,
NOTE|36, ENL+QNL-1,
NOTE|SILENCE, 1,
PATTERN_END
};
static const unsigned char pattern1[] = {
NOTE|37, ENL-1,
NOTE|SILENCE, 1,
NOTE|34, ENL+HNL+QNL-1,
NOTE|SILENCE, 1,
NOTE|46, ENL-1,
NOTE|SILENCE, 1,
NOTE|44, ENL+HNL+QNL-1,
NOTE|SILENCE, 1,
NOTE|34, ENL-1,
NOTE|SILENCE, 1,
NOTE|37, ENL-1,
NOTE|SILENCE, 1,
PATTERN_END
};
static const char pattern2[] = {
NOTE|34, ENL-1,
NOTE|SILENCE, 1,
NOTE|41, ENL+HNL-1,
NOTE|SILENCE, 1,
NOTE|SILENCE, ENL,
ARPEGGIO|34-12, ENL-1, ARP(1,1),
NOTE|SILENCE, 1,
ARPEGGIO|34-12, ENL, ARP(1,1),
NOTE|SILENCE, ENL,
ARPEGGIO|34-12, ENL-1, ARP(2,1),
NOTE|SILENCE, 1,
ARPEGGIO|34-12, ENL, ARP(2,1),
NOTE|SILENCE, QNL,
PATTERN_END
};
static const char* track0[] = {
pattern0,
pattern1,
pattern0,
pattern2,
TRACK_END
};
/* End track 0 */
/* ==== List of tracks ==== */
static const unsigned char** tracks[] = {
track0
};
/* ==== Arpeggio patterns ==== */
static unsigned char arp00[] = {
0, 19, ARPPAT_END
};
static unsigned char arp01[] = {
0, 3, 7, 10, ARPPAT_END
};
static unsigned char arp02[] = {
0, 5, 9, 12, ARPPAT_END
};
static unsigned char* arppats[] = {
arp00, arp01, arp02
};
/* Pointer to start of current track */
static unsigned char** curtrk;
/* Pointer to current position in track */
static unsigned char** trkptr;
/* Pointer to current position in pattern */
static unsigned char* patptr;
/* Countdown of ticks until next event */
static char tickctr;
/* Current arpeggio pattern */
static char arppat;
/* Pointer to current location in arpeggio pattern */
static char arpptr;
/* Arpeggio base note */
static char arpbase;
/* Arpeggio tick counter */
static char arpctr;
/* Arpeggio speed */
static char arpspd;
/* Current note frequency */
static unsigned int curfreq;
/* Glissando offset/tick */
static signed char glissoffs;
/* Current active effect */
static char effect;
void music_select(int track)
{
register unsigned char** d;
speaker_setup();
speaker_unmute();
d = trkptr = curtrk = tracks[track];
patptr = (*d)-1;
tickctr = 1;
effect = NOTE;
}
void music_pause(void)
{
speaker_mute();
}
void music_play(void)
{
speaker_unmute();
}
/* Call every tick or frame or whatever */
void music_process(void)
{
register unsigned char d;
register unsigned int i;
if (--tickctr) {
/* Handle effects and then return */
switch (effect) {
/* case NOTE:
break; */
case ARPEGGIO:
if (!--arpctr) {
/* Next note in arpeggio sequence */
arppat_restart:
d = arppats[arppat][++arpptr];
if (d==ARPPAT_END) {
arpptr=-1;
goto arppat_restart;
}
i = curfreq = notes[arpbase+d];
setfreq(i);
arpctr = arpspd;
}
break;
case GLISSANDO:
i = curfreq += glissoffs;
setfreq(i);
break;
case OTHER:
break;
}
return;
}
/* tickctr is 0, get next sound event */
get_sound_event:
d = *++patptr;
if (d == PATTERN_END) {
/* Sound event is PATTERN_END, go to next pattern */
if (*(++trkptr) == TRACK_END) {
/* Track is over, go to beginning of track */
trkptr = curtrk;
}
patptr = (*trkptr)-1;
goto get_sound_event;
}
switch (d & TYPE_MASK) {
case NOTE:
effect = NOTE;
i = curfreq = notes[d];
setfreq(i);
tickctr = *++patptr;
break;
case ARPEGGIO:
effect = ARPEGGIO;
arpbase = d&NOTE_MASK; /* Arpeggio note base */
tickctr = *(++patptr); /* Duration */
d = *(++patptr); /* Pattern and speed */
arpctr = arpspd = d&0x0F;
arppat = (d>>=4);
arpptr = -1;
i = curfreq = notes[arppats[arppat][0]];
setfreq(i);
break;
case GLISSANDO:
effect = GLISSANDO;
i = curfreq = notes[d&=NOTE_MASK];
setfreq(i);
tickctr = *(++patptr);
glissoffs = *(++patptr);
break;
case OTHER:
effect = OTHER;
tickctr = *(++patptr);
break;
}
}