-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsound.c
433 lines (404 loc) · 9.13 KB
/
sound.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include <gb/gb.h>
#include "sound.h"
#include "gamestate.h"
#include "mmlgb/driver/music.h"
#include "mmlgb/driver/notes.h"
#include "mmlgb/driver/freq.h"
#include "mmlgb/driver/noisefreq.h"
#include "mmlgb/driver/vib.h"
extern UBYTE sfx_bump_data;
extern UBYTE sfx_bump_alien_data;
extern UBYTE sfx_cat_disable_data;
extern UBYTE sfx_cat_enable_data;
extern UBYTE sfx_dash_data;
extern UBYTE sfx_highscore_switch_data;
extern UBYTE sfx_jetpack_data;
extern UBYTE sfx_menu_cancel_data;
extern UBYTE sfx_menu_confirm_data;
extern UBYTE sfx_menu_locked_data;
extern UBYTE sfx_menu_switch_data;
extern UBYTE sfx_player_die_data;
extern UBYTE sfx_stomp_alien_data;
extern UBYTE sfx_stomp_bat_data;
extern UBYTE sfx_stomp_bird_data;
extern UBYTE sfx_stomp_ghost_data;
extern UBYTE sfx_time_low_data;
extern UBYTE sfx_time_out_data;
extern UBYTE sfx_time_pickup_data;
extern UBYTE sfx_warp_start_data;
extern UBYTE sfx_rank_crash_data;
UBYTE snd_active1, snd_active4;
UBYTE snd_priority1, snd_priority4;
UWORD snd_freq1; UBYTE snd_freq4;
UBYTE *snd_data1, *snd_data4;
UBYTE snd_octave1, snd_octave4;
UBYTE snd_length1, snd_length4;
UBYTE snd_volume1, snd_volume4;
UBYTE snd_env1, snd_env4;
UBYTE snd_wait1, snd_wait4;
UWORD snd_target1, snd_target4;
UBYTE snd_slide1, snd_slide4;
UBYTE snd_vib_speed1;
UBYTE *snd_vib_table1;
UBYTE snd_vib_pos1;
UBYTE snd_noise_step;
UBYTE snd_po1;
const UBYTE sfx_priority[SFX_COUNT] = {
7U, // SFX_BUMP
7U, // SFX_BUMP_ALIEN
7U, // SFX_CAT_DISABLE
7U, // SFX_CAT_ENABLE
6U, // SFX_DASH
7U, // SFX_HIGHSCORE_SWITCH
6U, // SFX_JETPACK
7U, // SFX_MENU_CANCEL
7U, // SFX_MENU_CONFIRM
7U, // SFX_MENU_LOCKED
7U, // SFX_MENU_SWITCH
8U, // SFX_PLAYER_DIE
7U, // SFX_STOMP_ALIEN
7U, // SFX_STOMP_BAT
7U, // SFX_STOMP_BIRD
7U, // SFX_STOMP_GHOST
8U, // SFX_TIME_LOW
8U, // SFX_TIME_OUT
8U, // SFX_TIME_PICKUP
9U, // SFX_WARP_START
7U // SFX_RANK_CRASH
};
void snd_init() {
snd_active1 = snd_active4 = 0U;
snd_priority1 = snd_priority4 = 0U;
}
void wait_sound_done() {
while(snd_active1 || snd_active4) {
snd_update();
wait_vbl_done();
}
}
void stop_sound1() {
snd_active1 = 0U;
snd_priority1 = 0U;
mus_restore1();
}
void stop_sound4() {
snd_active4 = 0U;
snd_priority4 = 0U;
mus_restore4();
}
void playSound(UBYTE id) {
UBYTE *data, *data1, *data4;
UBYTE prio;
disable_interrupts();
SWITCH_ROM_MBC1(SOUND_BANK);
switch(id) {
case SFX_BUMP:
data = &sfx_bump_data; break;
case SFX_BUMP_ALIEN:
data = &sfx_bump_alien_data; break;
case SFX_CAT_DISABLE:
data = &sfx_cat_disable_data; break;
case SFX_CAT_ENABLE:
data = &sfx_cat_enable_data; break;
case SFX_DASH:
data = &sfx_dash_data; break;
case SFX_HIGHSCORE_SWITCH:
data = &sfx_highscore_switch_data; break;
case SFX_JETPACK:
data = &sfx_jetpack_data; break;
case SFX_MENU_CANCEL:
data = &sfx_menu_cancel_data; break;
case SFX_MENU_CONFIRM:
data = &sfx_menu_confirm_data; break;
case SFX_MENU_LOCKED:
data = &sfx_menu_locked_data; break;
case SFX_MENU_SWITCH:
data = &sfx_menu_switch_data; break;
case SFX_PLAYER_DIE:
data = &sfx_player_die_data; break;
case SFX_STOMP_ALIEN:
data = &sfx_stomp_alien_data; break;
case SFX_STOMP_BAT:
data = &sfx_stomp_bat_data; break;
case SFX_STOMP_BIRD:
data = &sfx_stomp_bird_data; break;
case SFX_STOMP_GHOST:
data = &sfx_stomp_ghost_data; break;
case SFX_TIME_LOW:
data = &sfx_time_low_data; break;
case SFX_TIME_OUT:
data = &sfx_time_out_data; break;
case SFX_TIME_PICKUP:
data = &sfx_time_pickup_data; break;
case SFX_WARP_START:
data = &sfx_warp_start_data; break;
case SFX_RANK_CRASH:
data = &sfx_rank_crash_data; break;
}
data1 = data + ((UWORD*)data)[CHN1_OFFSET];
data4 = data + ((UWORD*)data)[CHN4_OFFSET];
prio = sfx_priority[id];
if(*data1 != T_EOF && prio >= snd_priority1) {
mus_disable1();
snd_data1 = data1;
snd_active1 = 1U;
snd_priority1 = prio;
snd_wait1 = 0U;
snd_octave1 = 4U;
snd_length1 = 48U;
snd_volume1 = 0xE0U;
snd_env1 = 0U;
snd_slide1 = 0U;
snd_vib_speed1 = 0U;
snd_po1 = 128U;
NR51_REG |= 0x11U;
}
if(*data4 != T_EOF && prio >= snd_priority4) {
mus_disable4();
snd_data4 = data4;
snd_active4 = 1U;
snd_priority4 = prio;
snd_wait4 = 0U;
snd_octave4 = 4U;
snd_length4 = 48U;
snd_volume4 = 0xE0U;
snd_env4 = 0U;
snd_slide4 = 0U;
snd_noise_step = 0U;
NR51_REG |= 0x88U;
}
SWITCH_ROM_MBC1(game_bank);
enable_interrupts();
}
void snd_update() {
disable_interrupts();
SWITCH_ROM_MBC1(SOUND_BANK);
if(snd_active1) snd_update1();
if(snd_active4) snd_update4();
SWITCH_ROM_MBC1(game_bank);
enable_interrupts();
}
void snd_update1() {
UBYTE note;
UWORD vib_freq;
if(snd_slide1) {
if(snd_target1 > snd_freq1) {
snd_freq1 += snd_slide1;
if(snd_freq1 > snd_target1) {
snd_freq1 = snd_target1;
}
}
else if(snd_target1 < snd_freq1) {
snd_freq1 -= snd_slide1;
if(snd_freq1 < snd_target1) {
snd_freq1 = snd_target1;
}
}
NR13_REG = (UBYTE)snd_freq1;
NR14_REG = snd_freq1 >> 8;
}
if(snd_vib_speed1) {
snd_vib_pos1 = (snd_vib_pos1 + snd_vib_speed1) & 63U;
vib_freq = snd_freq1 - *snd_vib_table1 + snd_vib_table1[snd_vib_pos1];
NR13_REG = (UBYTE)vib_freq;
NR14_REG = vib_freq >> 8;
}
if(snd_wait1) {
snd_wait1--;
if(snd_wait1) return;
}
while(1U) {
note = *snd_data1++;
if(note >= MUS_FIRST_NOTE) {
if(note & MUS_HAS_LENGTH) {
note ^= MUS_HAS_LENGTH;
snd_wait1 = *snd_data1++;
}
else {
snd_wait1 = snd_length1;
}
if(note == T_WAIT) {
return;
} else if(note == T_REST) {
snd_freq1 = 0U;
NR12_REG = 0U;
} else {
if(snd_slide1) {
snd_target1 = freq[(snd_octave1 << 4) + note - MUS_FIRST_NOTE] + snd_po1 - 128U;
} else {
snd_freq1 = freq[(snd_octave1 << 4) + note - MUS_FIRST_NOTE] + snd_po1 - 128U;
}
NR12_REG = snd_volume1 | snd_env1;
}
NR13_REG = (UBYTE)snd_freq1;
NR14_REG = (snd_freq1 >> 8) | 0x80U;
return;
}
switch(note) {
case T_LENGTH:
snd_length1 = *snd_data1++;
break;
case T_OCTAVE:
snd_octave1 = *snd_data1++;
break;
case T_OCT_UP:
snd_octave1++;
break;
case T_OCT_DOWN:
snd_octave1--;
break;
case T_VOL:
snd_volume1 = *snd_data1++;
NR12_REG = snd_volume1 | snd_env1;
break;
case T_ENV:
snd_env1 = *snd_data1++;
NR12_REG = snd_volume1 | snd_env1;
break;
case T_WAVEDUTY:
NR11_REG = (*snd_data1++) << 5;
break;
case T_PAN:
break;
case T_PORTAMENTO:
snd_slide1 = *snd_data1++;
break;
case T_VIBRATO:
snd_vib_pos1 = 0U;
note = *snd_data1++;
snd_vib_speed1 = note & 15U;
note = note >> 4;
if(note == 1U) snd_vib_table1 = vib1;
else if(note == 2U) snd_vib_table1 = vib2;
else if(note == 3U) snd_vib_table1 = vib3;
else snd_vib_table1 = vib4;
break;
case T_VIBRATO_DELAY:
break;
case T_REP_START:
break;
case T_REP_END:
break;
case T_LOOP:
break;
case T_PITCH_OFFSET:
snd_po1 = *snd_data1++;
break;
case T_TEMPO:
break;
case T_NOISE_STEP:
break;
case T_WAVE:
break;
case T_EOF:
stop_sound1();
return;
}
}
}
void snd_update4() {
UBYTE note;
if(snd_slide4) {
if(snd_target4 > snd_freq4) {
snd_freq4 += snd_slide4;
if(snd_freq4 > snd_target4) {
snd_freq4 = snd_target4;
}
}
else if(snd_target4 < snd_freq4) {
snd_freq4 -= snd_slide4;
if(snd_freq4 < snd_target4) {
snd_freq4 = snd_target4;
}
}
NR43_REG = snd_freq4 | snd_noise_step;
}
if(snd_wait4) {
snd_wait4--;
if(snd_wait4) return;
}
while(1U) {
note = *snd_data4++;
if(note >= MUS_FIRST_NOTE) {
if(note & MUS_HAS_LENGTH) {
note ^= MUS_HAS_LENGTH;
snd_wait4 = *snd_data4++;
} else {
snd_wait4 = snd_length4;
}
if(note == T_WAIT) {
return;
} else if(note == T_REST) {
snd_freq4 = 0U;
NR42_REG = 0U;
} else {
if(snd_slide4) {
snd_target4 = noise_freq[((snd_octave4-MUS_NOISE_FIRST_OCTAVE) << 4) + note - MUS_FIRST_NOTE];
} else {
snd_freq4 = noise_freq[((snd_octave4-MUS_NOISE_FIRST_OCTAVE) << 4) + note - MUS_FIRST_NOTE];
}
NR42_REG = snd_volume4 | snd_env4;
}
NR43_REG = snd_freq4 | snd_noise_step;
NR44_REG = 0x80U;
return;
}
switch(note) {
case T_LENGTH:
snd_length4 = *snd_data4++;
break;
case T_OCTAVE:
snd_octave4 = *snd_data4++;
break;
case T_OCT_UP:
snd_octave4++;
break;
case T_OCT_DOWN:
snd_octave4--;
break;
case T_VOL:
snd_volume4 = *snd_data4++;
NR42_REG = snd_volume4 | snd_env4;
break;
case T_ENV:
snd_env4 = *snd_data4++;
NR42_REG = snd_volume4 | snd_env4;
break;
case T_WAVEDUTY:
break;
case T_PAN:
break;
case T_PORTAMENTO:
snd_slide4 = *snd_data4++;
break;
case T_VIBRATO:
break;
case T_VIBRATO_DELAY:
break;
case T_REP_START:
break;
case T_REP_END:
break;
case T_LOOP:
break;
case T_PITCH_OFFSET:
break;
case T_TEMPO:
break;
case T_NOISE_STEP:
snd_noise_step = (*snd_data4++) << 3;
break;
case T_WAVE:
break;
case T_EOF:
stop_sound4();
return;
}
}
}
UBYTE snd_isActive1() {
return snd_active1;
}
UBYTE snd_isActive4() {
return snd_active4;
}