src/gb/audio.c (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "audio.h"
7
8#include "core/interface.h"
9#include "core/sync.h"
10#include "gb/gb.h"
11#include "gb/serialize.h"
12#include "gb/io.h"
13
14#ifdef _3DS
15#define blip_add_delta blip_add_delta_fast
16#endif
17
18#define FRAME_CYCLES (DMG_LR35902_FREQUENCY >> 9)
19
20const uint32_t DMG_LR35902_FREQUENCY = 0x400000;
21static const int CLOCKS_PER_BLIP_FRAME = 0x1000;
22static const unsigned BLIP_BUFFER_SIZE = 0x4000;
23const int GB_AUDIO_VOLUME_MAX = 0x100;
24
25static bool _writeSweep(struct GBAudioSweep* sweep, uint8_t value);
26static void _writeDuty(struct GBAudioEnvelope* envelope, uint8_t value);
27static bool _writeEnvelope(struct GBAudioEnvelope* envelope, uint8_t value);
28
29static void _resetSweep(struct GBAudioSweep* sweep);
30static bool _resetEnvelope(struct GBAudioEnvelope* sweep);
31
32static void _updateEnvelope(struct GBAudioEnvelope* envelope);
33static void _updateEnvelopeDead(struct GBAudioEnvelope* envelope);
34static bool _updateSweep(struct GBAudioSquareChannel* sweep, bool initial);
35
36static void _updateSquareSample(struct GBAudioSquareChannel* ch);
37static int32_t _updateSquareChannel(struct GBAudioSquareChannel* ch);
38
39static void _updateFrame(struct mTiming* timing, void* user, uint32_t cyclesLate);
40static void _updateChannel1(struct mTiming* timing, void* user, uint32_t cyclesLate);
41static void _updateChannel2(struct mTiming* timing, void* user, uint32_t cyclesLate);
42static void _updateChannel3(struct mTiming* timing, void* user, uint32_t cyclesLate);
43static void _fadeChannel3(struct mTiming* timing, void* user, uint32_t cyclesLate);
44static void _updateChannel4(struct mTiming* timing, void* user, uint32_t cyclesLate);
45static void _sample(struct mTiming* timing, void* user, uint32_t cyclesLate);
46
47void GBAudioInit(struct GBAudio* audio, size_t samples, uint8_t* nr52, enum GBAudioStyle style) {
48 audio->samples = samples;
49 audio->left = blip_new(BLIP_BUFFER_SIZE);
50 audio->right = blip_new(BLIP_BUFFER_SIZE);
51 audio->clockRate = DMG_LR35902_FREQUENCY;
52 // Guess too large; we hang producing extra samples if we guess too low
53 blip_set_rates(audio->left, DMG_LR35902_FREQUENCY, 96000);
54 blip_set_rates(audio->right, DMG_LR35902_FREQUENCY, 96000);
55 audio->forceDisableCh[0] = false;
56 audio->forceDisableCh[1] = false;
57 audio->forceDisableCh[2] = false;
58 audio->forceDisableCh[3] = false;
59 audio->masterVolume = GB_AUDIO_VOLUME_MAX;
60 audio->nr52 = nr52;
61 audio->style = style;
62 if (style == GB_AUDIO_GBA) {
63 audio->timingFactor = 4;
64 } else {
65 audio->timingFactor = 1;
66 }
67
68 audio->frameEvent.context = audio;
69 audio->frameEvent.name = "GB Audio Frame Sequencer";
70 audio->frameEvent.callback = _updateFrame;
71 audio->frameEvent.priority = 0x10;
72 audio->ch1Event.context = audio;
73 audio->ch1Event.name = "GB Audio Channel 1";
74 audio->ch1Event.callback = _updateChannel1;
75 audio->ch1Event.priority = 0x11;
76 audio->ch2Event.context = audio;
77 audio->ch2Event.name = "GB Audio Channel 2";
78 audio->ch2Event.callback = _updateChannel2;
79 audio->ch2Event.priority = 0x12;
80 audio->ch3Event.context = audio;
81 audio->ch3Event.name = "GB Audio Channel 3";
82 audio->ch3Event.callback = _updateChannel3;
83 audio->ch3Event.priority = 0x13;
84 audio->ch3Fade.context = audio;
85 audio->ch3Fade.name = "GB Audio Channel 3 Memory";
86 audio->ch3Fade.callback = _fadeChannel3;
87 audio->ch3Fade.priority = 0x14;
88 audio->ch4Event.context = audio;
89 audio->ch4Event.name = "GB Audio Channel 4";
90 audio->ch4Event.callback = _updateChannel4;
91 audio->ch4Event.priority = 0x15;
92 audio->sampleEvent.context = audio;
93 audio->sampleEvent.name = "GB Audio Sample";
94 audio->sampleEvent.callback = _sample;
95 audio->ch1Event.priority = 0x18;
96}
97
98void GBAudioDeinit(struct GBAudio* audio) {
99 blip_delete(audio->left);
100 blip_delete(audio->right);
101}
102
103void GBAudioReset(struct GBAudio* audio) {
104 mTimingDeschedule(audio->timing, &audio->frameEvent);
105 mTimingDeschedule(audio->timing, &audio->ch1Event);
106 mTimingDeschedule(audio->timing, &audio->ch2Event);
107 mTimingDeschedule(audio->timing, &audio->ch3Event);
108 mTimingDeschedule(audio->timing, &audio->ch3Fade);
109 mTimingDeschedule(audio->timing, &audio->ch4Event);
110 mTimingDeschedule(audio->timing, &audio->sampleEvent);
111 mTimingSchedule(audio->timing, &audio->frameEvent, 0);
112 if (audio->style != GB_AUDIO_GBA) {
113 mTimingSchedule(audio->timing, &audio->sampleEvent, 0);
114 }
115 audio->ch1 = (struct GBAudioSquareChannel) { .envelope = { .dead = 2 } };
116 audio->ch2 = (struct GBAudioSquareChannel) { .envelope = { .dead = 2 } };
117 audio->ch3 = (struct GBAudioWaveChannel) { .bank = 0 };
118 // TODO: DMG randomness
119 audio->ch3.wavedata8[0] = 0x00;
120 audio->ch3.wavedata8[1] = 0xFF;
121 audio->ch3.wavedata8[2] = 0x00;
122 audio->ch3.wavedata8[3] = 0xFF;
123 audio->ch3.wavedata8[4] = 0x00;
124 audio->ch3.wavedata8[5] = 0xFF;
125 audio->ch3.wavedata8[6] = 0x00;
126 audio->ch3.wavedata8[7] = 0xFF;
127 audio->ch3.wavedata8[8] = 0x00;
128 audio->ch3.wavedata8[9] = 0xFF;
129 audio->ch3.wavedata8[10] = 0x00;
130 audio->ch3.wavedata8[11] = 0xFF;
131 audio->ch3.wavedata8[12] = 0x00;
132 audio->ch3.wavedata8[13] = 0xFF;
133 audio->ch3.wavedata8[14] = 0x00;
134 audio->ch3.wavedata8[15] = 0xFF;
135 audio->ch4 = (struct GBAudioNoiseChannel) { .envelope = { .dead = 2 } };
136 audio->frame = 0;
137 audio->sampleInterval = 128;
138 audio->lastLeft = 0;
139 audio->lastRight = 0;
140 audio->clock = 0;
141 audio->volumeRight = 0;
142 audio->volumeLeft = 0;
143 audio->ch1Right = false;
144 audio->ch2Right = false;
145 audio->ch3Right = false;
146 audio->ch4Right = false;
147 audio->ch1Left = false;
148 audio->ch2Left = false;
149 audio->ch3Left = false;
150 audio->ch4Left = false;
151 audio->playingCh1 = false;
152 audio->playingCh2 = false;
153 audio->playingCh3 = false;
154 audio->playingCh4 = false;
155}
156
157void GBAudioResizeBuffer(struct GBAudio* audio, size_t samples) {
158 mCoreSyncLockAudio(audio->p->sync);
159 audio->samples = samples;
160 blip_clear(audio->left);
161 blip_clear(audio->right);
162 audio->clock = 0;
163 mCoreSyncConsumeAudio(audio->p->sync);
164}
165
166void GBAudioWriteNR10(struct GBAudio* audio, uint8_t value) {
167 if (!_writeSweep(&audio->ch1.sweep, value)) {
168 mTimingDeschedule(audio->timing, &audio->ch1Event);
169 audio->playingCh1 = false;
170 *audio->nr52 &= ~0x0001;
171 }
172}
173
174void GBAudioWriteNR11(struct GBAudio* audio, uint8_t value) {
175 _writeDuty(&audio->ch1.envelope, value);
176 audio->ch1.control.length = 64 - audio->ch1.envelope.length;
177}
178
179void GBAudioWriteNR12(struct GBAudio* audio, uint8_t value) {
180 if (!_writeEnvelope(&audio->ch1.envelope, value)) {
181 mTimingDeschedule(audio->timing, &audio->ch1Event);
182 audio->playingCh1 = false;
183 *audio->nr52 &= ~0x0001;
184 }
185}
186
187void GBAudioWriteNR13(struct GBAudio* audio, uint8_t value) {
188 audio->ch1.control.frequency &= 0x700;
189 audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value);
190}
191
192void GBAudioWriteNR14(struct GBAudio* audio, uint8_t value) {
193 audio->ch1.control.frequency &= 0xFF;
194 audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value << 8);
195 bool wasStop = audio->ch1.control.stop;
196 audio->ch1.control.stop = GBAudioRegisterControlGetStop(value << 8);
197 if (!wasStop && audio->ch1.control.stop && audio->ch1.control.length && !(audio->frame & 1)) {
198 --audio->ch1.control.length;
199 if (audio->ch1.control.length == 0) {
200 mTimingDeschedule(audio->timing, &audio->ch1Event);
201 audio->playingCh1 = false;
202 }
203 }
204 if (GBAudioRegisterControlIsRestart(value << 8)) {
205 audio->playingCh1 = _resetEnvelope(&audio->ch1.envelope);
206
207 if (audio->playingCh1) {
208 audio->ch1.control.hi = 0;
209 _updateSquareSample(&audio->ch1);
210 }
211
212 audio->ch1.sweep.realFrequency = audio->ch1.control.frequency;
213 _resetSweep(&audio->ch1.sweep);
214 if (audio->playingCh1 && audio->ch1.sweep.shift) {
215 audio->playingCh1 = _updateSweep(&audio->ch1, true);
216 }
217 if (!audio->ch1.control.length) {
218 audio->ch1.control.length = 64;
219 if (audio->ch1.control.stop && !(audio->frame & 1)) {
220 --audio->ch1.control.length;
221 }
222 }
223 mTimingDeschedule(audio->timing, &audio->ch1Event);
224 if (audio->playingCh1 && audio->ch1.envelope.dead != 2) {
225 mTimingSchedule(audio->timing, &audio->ch1Event, 0);
226 }
227 }
228 *audio->nr52 &= ~0x0001;
229 *audio->nr52 |= audio->playingCh1;
230}
231
232void GBAudioWriteNR21(struct GBAudio* audio, uint8_t value) {
233 _writeDuty(&audio->ch2.envelope, value);
234 audio->ch2.control.length = 64 - audio->ch2.envelope.length;
235}
236
237void GBAudioWriteNR22(struct GBAudio* audio, uint8_t value) {
238 if (!_writeEnvelope(&audio->ch2.envelope, value)) {
239 mTimingDeschedule(audio->timing, &audio->ch2Event);
240 audio->playingCh2 = false;
241 *audio->nr52 &= ~0x0002;
242 }
243}
244
245void GBAudioWriteNR23(struct GBAudio* audio, uint8_t value) {
246 audio->ch2.control.frequency &= 0x700;
247 audio->ch2.control.frequency |= GBAudioRegisterControlGetFrequency(value);
248}
249
250void GBAudioWriteNR24(struct GBAudio* audio, uint8_t value) {
251 audio->ch2.control.frequency &= 0xFF;
252 audio->ch2.control.frequency |= GBAudioRegisterControlGetFrequency(value << 8);
253 bool wasStop = audio->ch2.control.stop;
254 audio->ch2.control.stop = GBAudioRegisterControlGetStop(value << 8);
255 if (!wasStop && audio->ch2.control.stop && audio->ch2.control.length && !(audio->frame & 1)) {
256 --audio->ch2.control.length;
257 if (audio->ch2.control.length == 0) {
258 mTimingDeschedule(audio->timing, &audio->ch2Event);
259 audio->playingCh2 = false;
260 }
261 }
262 if (GBAudioRegisterControlIsRestart(value << 8)) {
263 audio->playingCh2 = _resetEnvelope(&audio->ch2.envelope);
264
265 if (audio->playingCh2) {
266 audio->ch2.control.hi = 0;
267 _updateSquareSample(&audio->ch2);
268 }
269
270 if (!audio->ch2.control.length) {
271 audio->ch2.control.length = 64;
272 if (audio->ch2.control.stop && !(audio->frame & 1)) {
273 --audio->ch2.control.length;
274 }
275 }
276 mTimingDeschedule(audio->timing, &audio->ch2Event);
277 if (audio->playingCh2 && audio->ch2.envelope.dead != 2) {
278 mTimingSchedule(audio->timing, &audio->ch2Event, 0);
279 }
280 }
281 *audio->nr52 &= ~0x0002;
282 *audio->nr52 |= audio->playingCh2 << 1;
283}
284
285void GBAudioWriteNR30(struct GBAudio* audio, uint8_t value) {
286 audio->ch3.enable = GBAudioRegisterBankGetEnable(value);
287 if (!audio->ch3.enable) {
288 audio->playingCh3 = false;
289 *audio->nr52 &= ~0x0004;
290 }
291}
292
293void GBAudioWriteNR31(struct GBAudio* audio, uint8_t value) {
294 audio->ch3.length = 256 - value;
295}
296
297void GBAudioWriteNR32(struct GBAudio* audio, uint8_t value) {
298 audio->ch3.volume = GBAudioRegisterBankVolumeGetVolumeGB(value);
299}
300
301void GBAudioWriteNR33(struct GBAudio* audio, uint8_t value) {
302 audio->ch3.rate &= 0x700;
303 audio->ch3.rate |= GBAudioRegisterControlGetRate(value);
304}
305
306void GBAudioWriteNR34(struct GBAudio* audio, uint8_t value) {
307 audio->ch3.rate &= 0xFF;
308 audio->ch3.rate |= GBAudioRegisterControlGetRate(value << 8);
309 bool wasStop = audio->ch3.stop;
310 audio->ch3.stop = GBAudioRegisterControlGetStop(value << 8);
311 if (!wasStop && audio->ch3.stop && audio->ch3.length && !(audio->frame & 1)) {
312 --audio->ch3.length;
313 if (audio->ch3.length == 0) {
314 audio->playingCh3 = false;
315 }
316 }
317 bool wasEnable = audio->playingCh3;
318 if (GBAudioRegisterControlIsRestart(value << 8)) {
319 audio->playingCh3 = audio->ch3.enable;
320 if (!audio->ch3.length) {
321 audio->ch3.length = 256;
322 if (audio->ch3.stop && !(audio->frame & 1)) {
323 --audio->ch3.length;
324 }
325 }
326
327 if (audio->style == GB_AUDIO_DMG && wasEnable && audio->playingCh3 && audio->ch3.readable) {
328 if (audio->ch3.window < 8) {
329 audio->ch3.wavedata8[0] = audio->ch3.wavedata8[audio->ch3.window >> 1];
330 } else {
331 audio->ch3.wavedata8[0] = audio->ch3.wavedata8[((audio->ch3.window >> 1) & ~3)];
332 audio->ch3.wavedata8[1] = audio->ch3.wavedata8[((audio->ch3.window >> 1) & ~3) + 1];
333 audio->ch3.wavedata8[2] = audio->ch3.wavedata8[((audio->ch3.window >> 1) & ~3) + 2];
334 audio->ch3.wavedata8[3] = audio->ch3.wavedata8[((audio->ch3.window >> 1) & ~3) + 3];
335 }
336 }
337 audio->ch3.window = 0;
338 }
339 mTimingDeschedule(audio->timing, &audio->ch3Event);
340 if (audio->playingCh3) {
341 audio->ch3.readable = audio->style != GB_AUDIO_DMG;
342 // TODO: Where does this cycle delay come from?
343 mTimingSchedule(audio->timing, &audio->ch3Event, audio->timingFactor * 4 + 2 * (2048 - audio->ch3.rate));
344 }
345 *audio->nr52 &= ~0x0004;
346 *audio->nr52 |= audio->playingCh3 << 2;
347}
348
349void GBAudioWriteNR41(struct GBAudio* audio, uint8_t value) {
350 _writeDuty(&audio->ch4.envelope, value);
351 audio->ch4.length = 64 - audio->ch4.envelope.length;
352}
353
354void GBAudioWriteNR42(struct GBAudio* audio, uint8_t value) {
355 if (!_writeEnvelope(&audio->ch4.envelope, value)) {
356 mTimingDeschedule(audio->timing, &audio->ch4Event);
357 audio->playingCh4 = false;
358 *audio->nr52 &= ~0x0008;
359 }
360}
361
362void GBAudioWriteNR43(struct GBAudio* audio, uint8_t value) {
363 audio->ch4.ratio = GBAudioRegisterNoiseFeedbackGetRatio(value);
364 audio->ch4.frequency = GBAudioRegisterNoiseFeedbackGetFrequency(value);
365 audio->ch4.power = GBAudioRegisterNoiseFeedbackGetPower(value);
366}
367
368void GBAudioWriteNR44(struct GBAudio* audio, uint8_t value) {
369 bool wasStop = audio->ch4.stop;
370 audio->ch4.stop = GBAudioRegisterNoiseControlGetStop(value);
371 if (!wasStop && audio->ch4.stop && audio->ch4.length && !(audio->frame & 1)) {
372 --audio->ch4.length;
373 if (audio->ch4.length == 0) {
374 mTimingDeschedule(audio->timing, &audio->ch4Event);
375 audio->playingCh4 = false;
376 }
377 }
378 if (GBAudioRegisterNoiseControlIsRestart(value)) {
379 audio->playingCh4 = _resetEnvelope(&audio->ch4.envelope);
380
381 if (audio->ch4.power) {
382 audio->ch4.lfsr = 0x40;
383 } else {
384 audio->ch4.lfsr = 0x4000;
385 }
386 if (!audio->ch4.length) {
387 audio->ch4.length = 64;
388 if (audio->ch4.stop && !(audio->frame & 1)) {
389 --audio->ch4.length;
390 }
391 }
392 mTimingDeschedule(audio->timing, &audio->ch4Event);
393 if (audio->playingCh4 && audio->ch4.envelope.dead != 2) {
394 mTimingSchedule(audio->timing, &audio->ch4Event, 0);
395 }
396 }
397 *audio->nr52 &= ~0x0008;
398 *audio->nr52 |= audio->playingCh4 << 3;
399}
400
401void GBAudioWriteNR50(struct GBAudio* audio, uint8_t value) {
402 audio->volumeRight = GBRegisterNR50GetVolumeRight(value);
403 audio->volumeLeft = GBRegisterNR50GetVolumeLeft(value);
404}
405
406void GBAudioWriteNR51(struct GBAudio* audio, uint8_t value) {
407 audio->ch1Right = GBRegisterNR51GetCh1Right(value);
408 audio->ch2Right = GBRegisterNR51GetCh2Right(value);
409 audio->ch3Right = GBRegisterNR51GetCh3Right(value);
410 audio->ch4Right = GBRegisterNR51GetCh4Right(value);
411 audio->ch1Left = GBRegisterNR51GetCh1Left(value);
412 audio->ch2Left = GBRegisterNR51GetCh2Left(value);
413 audio->ch3Left = GBRegisterNR51GetCh3Left(value);
414 audio->ch4Left = GBRegisterNR51GetCh4Left(value);
415}
416
417void GBAudioWriteNR52(struct GBAudio* audio, uint8_t value) {
418 bool wasEnable = audio->enable;
419 audio->enable = GBAudioEnableGetEnable(value);
420 if (!audio->enable) {
421 audio->playingCh1 = 0;
422 audio->playingCh2 = 0;
423 audio->playingCh3 = 0;
424 audio->playingCh4 = 0;
425 GBAudioWriteNR10(audio, 0);
426 GBAudioWriteNR12(audio, 0);
427 GBAudioWriteNR13(audio, 0);
428 GBAudioWriteNR14(audio, 0);
429 GBAudioWriteNR22(audio, 0);
430 GBAudioWriteNR23(audio, 0);
431 GBAudioWriteNR24(audio, 0);
432 GBAudioWriteNR30(audio, 0);
433 GBAudioWriteNR32(audio, 0);
434 GBAudioWriteNR33(audio, 0);
435 GBAudioWriteNR34(audio, 0);
436 GBAudioWriteNR42(audio, 0);
437 GBAudioWriteNR43(audio, 0);
438 GBAudioWriteNR44(audio, 0);
439 GBAudioWriteNR50(audio, 0);
440 GBAudioWriteNR51(audio, 0);
441 if (audio->style != GB_AUDIO_DMG) {
442 GBAudioWriteNR11(audio, 0);
443 GBAudioWriteNR21(audio, 0);
444 GBAudioWriteNR31(audio, 0);
445 GBAudioWriteNR41(audio, 0);
446 }
447
448 if (audio->p) {
449 audio->p->memory.io[REG_NR10] = 0;
450 audio->p->memory.io[REG_NR11] = 0;
451 audio->p->memory.io[REG_NR12] = 0;
452 audio->p->memory.io[REG_NR13] = 0;
453 audio->p->memory.io[REG_NR14] = 0;
454 audio->p->memory.io[REG_NR21] = 0;
455 audio->p->memory.io[REG_NR22] = 0;
456 audio->p->memory.io[REG_NR23] = 0;
457 audio->p->memory.io[REG_NR24] = 0;
458 audio->p->memory.io[REG_NR30] = 0;
459 audio->p->memory.io[REG_NR31] = 0;
460 audio->p->memory.io[REG_NR32] = 0;
461 audio->p->memory.io[REG_NR33] = 0;
462 audio->p->memory.io[REG_NR34] = 0;
463 audio->p->memory.io[REG_NR42] = 0;
464 audio->p->memory.io[REG_NR43] = 0;
465 audio->p->memory.io[REG_NR44] = 0;
466 audio->p->memory.io[REG_NR50] = 0;
467 audio->p->memory.io[REG_NR51] = 0;
468 if (audio->style != GB_AUDIO_DMG) {
469 audio->p->memory.io[REG_NR11] = 0;
470 audio->p->memory.io[REG_NR21] = 0;
471 audio->p->memory.io[REG_NR31] = 0;
472 audio->p->memory.io[REG_NR41] = 0;
473 }
474 }
475 *audio->nr52 &= ~0x000F;
476 } else if (!wasEnable) {
477 audio->frame = 7;
478 }
479}
480
481void _updateFrame(struct mTiming* timing, void* user, uint32_t cyclesLate) {
482 struct GBAudio* audio = user;
483
484 int frame = (audio->frame + 1) & 7;
485 audio->frame = frame;
486
487 switch (frame) {
488 case 2:
489 case 6:
490 if (audio->ch1.sweep.enable) {
491 --audio->ch1.sweep.step;
492 if (audio->ch1.sweep.step == 0) {
493 audio->playingCh1 = _updateSweep(&audio->ch1, false);
494 *audio->nr52 &= ~0x0001;
495 *audio->nr52 |= audio->playingCh1;
496 }
497 }
498 // Fall through
499 case 0:
500 case 4:
501 if (audio->ch1.control.length && audio->ch1.control.stop) {
502 --audio->ch1.control.length;
503 if (audio->ch1.control.length == 0) {
504 mTimingDeschedule(timing, &audio->ch1Event);
505 audio->playingCh1 = 0;
506 *audio->nr52 &= ~0x0001;
507 }
508 }
509
510 if (audio->ch2.control.length && audio->ch2.control.stop) {
511 --audio->ch2.control.length;
512 if (audio->ch2.control.length == 0) {
513 mTimingDeschedule(timing, &audio->ch2Event);
514 audio->playingCh2 = 0;
515 *audio->nr52 &= ~0x0002;
516 }
517 }
518
519 if (audio->ch3.length && audio->ch3.stop) {
520 --audio->ch3.length;
521 if (audio->ch3.length == 0) {
522 mTimingDeschedule(timing, &audio->ch3Event);
523 audio->playingCh3 = 0;
524 *audio->nr52 &= ~0x0004;
525 }
526 }
527
528 if (audio->ch4.length && audio->ch4.stop) {
529 --audio->ch4.length;
530 if (audio->ch4.length == 0) {
531 mTimingDeschedule(timing, &audio->ch4Event);
532 audio->playingCh4 = 0;
533 *audio->nr52 &= ~0x0008;
534 }
535 }
536 break;
537 case 7:
538 if (audio->playingCh1 && !audio->ch1.envelope.dead) {
539 --audio->ch1.envelope.nextStep;
540 if (audio->ch1.envelope.nextStep == 0) {
541 _updateEnvelope(&audio->ch1.envelope);
542 if (audio->ch1.envelope.dead == 2) {
543 mTimingDeschedule(timing, &audio->ch1Event);
544 }
545 _updateSquareSample(&audio->ch1);
546 }
547 }
548
549 if (audio->playingCh2 && !audio->ch2.envelope.dead) {
550 --audio->ch2.envelope.nextStep;
551 if (audio->ch2.envelope.nextStep == 0) {
552 _updateEnvelope(&audio->ch2.envelope);
553 if (audio->ch2.envelope.dead == 2) {
554 mTimingDeschedule(timing, &audio->ch2Event);
555 }
556 _updateSquareSample(&audio->ch1);
557 }
558 }
559
560 if (audio->playingCh4 && !audio->ch4.envelope.dead) {
561 --audio->ch4.envelope.nextStep;
562 if (audio->ch4.envelope.nextStep == 0) {
563 int8_t sample = (audio->ch4.sample >> 7) * 0x8;
564 _updateEnvelope(&audio->ch4.envelope);
565 if (audio->ch4.envelope.dead == 2) {
566 mTimingDeschedule(timing, &audio->ch4Event);
567 }
568 audio->ch4.sample = sample * audio->ch4.envelope.currentVolume;
569 }
570 }
571 break;
572 }
573
574 mTimingSchedule(timing, &audio->frameEvent, audio->timingFactor * FRAME_CYCLES - cyclesLate);
575}
576
577void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right) {
578 int sampleLeft = 0;
579 int sampleRight = 0;
580
581 if (audio->playingCh1 && !audio->forceDisableCh[0]) {
582 if (audio->ch1Left) {
583 sampleLeft += audio->ch1.sample;
584 }
585
586 if (audio->ch1Right) {
587 sampleRight += audio->ch1.sample;
588 }
589 }
590
591 if (audio->playingCh2 && !audio->forceDisableCh[1]) {
592 if (audio->ch2Left) {
593 sampleLeft += audio->ch2.sample;
594 }
595
596 if (audio->ch2Right) {
597 sampleRight += audio->ch2.sample;
598 }
599 }
600
601 if (audio->playingCh3 && !audio->forceDisableCh[2]) {
602 if (audio->ch3Left) {
603 sampleLeft += audio->ch3.sample;
604 }
605
606 if (audio->ch3Right) {
607 sampleRight += audio->ch3.sample;
608 }
609 }
610
611 if (audio->playingCh4 && !audio->forceDisableCh[3]) {
612 if (audio->ch4Left) {
613 sampleLeft += audio->ch4.sample;
614 }
615
616 if (audio->ch4Right) {
617 sampleRight += audio->ch4.sample;
618 }
619 }
620
621 *left = sampleLeft * (1 + audio->volumeLeft);
622 *right = sampleRight * (1 + audio->volumeRight);
623}
624
625static void _sample(struct mTiming* timing, void* user, uint32_t cyclesLate) {
626 struct GBAudio* audio = user;
627 int16_t sampleLeft = 0;
628 int16_t sampleRight = 0;
629 GBAudioSamplePSG(audio, &sampleLeft, &sampleRight);
630 sampleLeft = (sampleLeft * audio->masterVolume) >> 6;
631 sampleRight = (sampleRight * audio->masterVolume) >> 6;
632
633 mCoreSyncLockAudio(audio->p->sync);
634 unsigned produced;
635 if ((size_t) blip_samples_avail(audio->left) < audio->samples) {
636 blip_add_delta(audio->left, audio->clock, sampleLeft - audio->lastLeft);
637 blip_add_delta(audio->right, audio->clock, sampleRight - audio->lastRight);
638 audio->lastLeft = sampleLeft;
639 audio->lastRight = sampleRight;
640 audio->clock += audio->sampleInterval;
641 if (audio->clock >= CLOCKS_PER_BLIP_FRAME) {
642 blip_end_frame(audio->left, audio->clock);
643 blip_end_frame(audio->right, audio->clock);
644 audio->clock -= CLOCKS_PER_BLIP_FRAME;
645 }
646 }
647 produced = blip_samples_avail(audio->left);
648 if (audio->p->stream && audio->p->stream->postAudioFrame) {
649 audio->p->stream->postAudioFrame(audio->p->stream, sampleLeft, sampleRight);
650 }
651 bool wait = produced >= audio->samples;
652 mCoreSyncProduceAudio(audio->p->sync, wait);
653
654 if (wait && audio->p->stream && audio->p->stream->postAudioBuffer) {
655 audio->p->stream->postAudioBuffer(audio->p->stream, audio->left, audio->right);
656 }
657 mTimingSchedule(timing, &audio->sampleEvent, audio->sampleInterval * audio->timingFactor - cyclesLate);
658}
659
660bool _resetEnvelope(struct GBAudioEnvelope* envelope) {
661 envelope->currentVolume = envelope->initialVolume;
662 _updateEnvelopeDead(envelope);
663 if (!envelope->dead) {
664 envelope->nextStep = envelope->stepTime;
665 }
666 return envelope->initialVolume || envelope->direction;
667}
668
669void _resetSweep(struct GBAudioSweep* sweep) {
670 sweep->step = sweep->time;
671 sweep->enable = (sweep->step != 8) || sweep->shift;
672 sweep->occurred = false;
673}
674
675bool _writeSweep(struct GBAudioSweep* sweep, uint8_t value) {
676 sweep->shift = GBAudioRegisterSquareSweepGetShift(value);
677 bool oldDirection = sweep->direction;
678 sweep->direction = GBAudioRegisterSquareSweepGetDirection(value);
679 bool on = true;
680 if (sweep->occurred && oldDirection && !sweep->direction) {
681 on = false;
682 }
683 sweep->occurred = false;
684 sweep->time = GBAudioRegisterSquareSweepGetTime(value);
685 if (!sweep->time) {
686 sweep->time = 8;
687 }
688 return on;
689}
690
691void _writeDuty(struct GBAudioEnvelope* envelope, uint8_t value) {
692 envelope->length = GBAudioRegisterDutyGetLength(value);
693 envelope->duty = GBAudioRegisterDutyGetDuty(value);
694}
695
696bool _writeEnvelope(struct GBAudioEnvelope* envelope, uint8_t value) {
697 envelope->stepTime = GBAudioRegisterSweepGetStepTime(value);
698 envelope->direction = GBAudioRegisterSweepGetDirection(value);
699 envelope->initialVolume = GBAudioRegisterSweepGetInitialVolume(value);
700 _updateEnvelopeDead(envelope);
701 envelope->nextStep = envelope->stepTime;
702 return (envelope->initialVolume || envelope->direction) && envelope->dead != 2;
703}
704
705static void _updateSquareSample(struct GBAudioSquareChannel* ch) {
706 ch->sample = (ch->control.hi * ch->envelope.currentVolume - 8) * 0x10;
707}
708
709static int32_t _updateSquareChannel(struct GBAudioSquareChannel* ch) {
710 ch->control.hi = !ch->control.hi;
711 _updateSquareSample(ch);
712 int period = 4 * (2048 - ch->control.frequency);
713 switch (ch->envelope.duty) {
714 case 0:
715 return ch->control.hi ? period : period * 7;
716 case 1:
717 return ch->control.hi ? period * 2 : period * 6;
718 case 2:
719 return period * 4;
720 case 3:
721 return ch->control.hi ? period * 6 : period * 2;
722 default:
723 // This should never be hit
724 return period * 4;
725 }
726}
727
728static void _updateEnvelope(struct GBAudioEnvelope* envelope) {
729 if (envelope->direction) {
730 ++envelope->currentVolume;
731 } else {
732 --envelope->currentVolume;
733 }
734 if (envelope->currentVolume >= 15) {
735 envelope->currentVolume = 15;
736 envelope->dead = 1;
737 } else if (envelope->currentVolume <= 0) {
738 envelope->currentVolume = 0;
739 envelope->dead = 2;
740 } else {
741 envelope->nextStep = envelope->stepTime;
742 }
743}
744
745static void _updateEnvelopeDead(struct GBAudioEnvelope* envelope) {
746 if (!envelope->stepTime) {
747 envelope->dead = envelope->currentVolume ? 1 : 2;
748 } else if (!envelope->direction && !envelope->currentVolume) {
749 envelope->dead = 2;
750 } else if (envelope->direction && envelope->currentVolume == 0xF) {
751 envelope->dead = 1;
752 } else {
753 envelope->dead = 0;
754 }
755}
756
757static bool _updateSweep(struct GBAudioSquareChannel* ch, bool initial) {
758 if (initial || ch->sweep.time != 8) {
759 int frequency = ch->sweep.realFrequency;
760 if (ch->sweep.direction) {
761 frequency -= frequency >> ch->sweep.shift;
762 if (!initial && frequency >= 0) {
763 ch->control.frequency = frequency;
764 ch->sweep.realFrequency = frequency;
765 }
766 } else {
767 frequency += frequency >> ch->sweep.shift;
768 if (frequency < 2048) {
769 if (!initial && ch->sweep.shift) {
770 ch->control.frequency = frequency;
771 ch->sweep.realFrequency = frequency;
772 if (!_updateSweep(ch, true)) {
773 return false;
774 }
775 }
776 } else {
777 return false;
778 }
779 }
780 ch->sweep.occurred = true;
781 }
782 ch->sweep.step = ch->sweep.time;
783 return true;
784}
785
786static void _updateChannel1(struct mTiming* timing, void* user, uint32_t cyclesLate) {
787 struct GBAudio* audio = user;
788 struct GBAudioSquareChannel* ch = &audio->ch1;
789 int cycles = _updateSquareChannel(ch);
790 mTimingSchedule(timing, &audio->ch1Event, audio->timingFactor * cycles - cyclesLate);
791}
792
793static void _updateChannel2(struct mTiming* timing, void* user, uint32_t cyclesLate) {
794 struct GBAudio* audio = user;
795 struct GBAudioSquareChannel* ch = &audio->ch2;
796 int cycles = _updateSquareChannel(ch);
797 mTimingSchedule(timing, &audio->ch2Event, audio->timingFactor * cycles - cyclesLate);
798}
799
800static void _updateChannel3(struct mTiming* timing, void* user, uint32_t cyclesLate) {
801 struct GBAudio* audio = user;
802 struct GBAudioWaveChannel* ch = &audio->ch3;
803 int i;
804 int volume;
805 switch (ch->volume) {
806 case 0:
807 volume = 0;
808 break;
809 case 1:
810 volume = 4;
811 break;
812 case 2:
813 volume = 2;
814 break;
815 case 3:
816 volume = 1;
817 break;
818 default:
819 volume = 3;
820 break;
821 }
822 int start;
823 int end;
824 switch (audio->style) {
825 case GB_AUDIO_DMG:
826 default:
827 ++ch->window;
828 ch->window &= 0x1F;
829 ch->sample = ch->wavedata8[ch->window >> 1];
830 if (!(ch->window & 1)) {
831 ch->sample >>= 4;
832 }
833 ch->sample &= 0xF;
834 break;
835 case GB_AUDIO_GBA:
836 if (ch->size) {
837 start = 7;
838 end = 0;
839 } else if (ch->bank) {
840 start = 7;
841 end = 4;
842 } else {
843 start = 3;
844 end = 0;
845 }
846 uint32_t bitsCarry = ch->wavedata32[end] & 0x000000F0;
847 uint32_t bits;
848 for (i = start; i >= end; --i) {
849 bits = ch->wavedata32[i] & 0x000000F0;
850 ch->wavedata32[i] = ((ch->wavedata32[i] & 0x0F0F0F0F) << 4) | ((ch->wavedata32[i] & 0xF0F0F000) >> 12);
851 ch->wavedata32[i] |= bitsCarry << 20;
852 bitsCarry = bits;
853 }
854 ch->sample = bitsCarry >> 4;
855 break;
856 }
857 ch->sample -= 8;
858 ch->sample *= volume * 4;
859 audio->ch3.readable = true;
860 if (audio->style == GB_AUDIO_DMG) {
861 mTimingSchedule(timing, &audio->ch3Fade, 2 - cyclesLate);
862 }
863 int cycles = 2 * (2048 - ch->rate);
864 mTimingSchedule(timing, &audio->ch3Event, audio->timingFactor * cycles - cyclesLate);
865}
866static void _fadeChannel3(struct mTiming* timing, void* user, uint32_t cyclesLate) {
867 UNUSED(timing);
868 UNUSED(cyclesLate);
869 struct GBAudio* audio = user;
870 audio->ch3.readable = false;
871}
872
873static void _updateChannel4(struct mTiming* timing, void* user, uint32_t cyclesLate) {
874 struct GBAudio* audio = user;
875 struct GBAudioNoiseChannel* ch = &audio->ch4;
876
877 int32_t baseCycles = ch->ratio ? 2 * ch->ratio : 1;
878 baseCycles <<= ch->frequency;
879 baseCycles *= 8 * audio->timingFactor;
880 int32_t cycles = 0;
881
882 do {
883 int lsb = ch->lfsr & 1;
884 ch->sample = lsb * 0x10 - 0x8;
885 ch->sample *= ch->envelope.currentVolume;
886 ch->lfsr >>= 1;
887 ch->lfsr ^= (lsb * 0x60) << (ch->power ? 0 : 8);
888 cycles += baseCycles;
889 } while (cycles < audio->sampleInterval);
890 mTimingSchedule(timing, &audio->ch4Event, cycles - cyclesLate);
891}
892
893void GBAudioPSGSerialize(const struct GBAudio* audio, struct GBSerializedPSGState* state, uint32_t* flagsOut) {
894 uint32_t flags = 0;
895 uint32_t ch1Flags = 0;
896 uint32_t ch2Flags = 0;
897 uint32_t ch4Flags = 0;
898
899 flags = GBSerializedAudioFlagsSetFrame(flags, audio->frame);
900 STORE_32LE(audio->frameEvent.when - mTimingCurrentTime(audio->timing), 0, &state->ch1.nextFrame);
901
902 flags = GBSerializedAudioFlagsSetCh1Volume(flags, audio->ch1.envelope.currentVolume);
903 flags = GBSerializedAudioFlagsSetCh1Dead(flags, audio->ch1.envelope.dead);
904 flags = GBSerializedAudioFlagsSetCh1Hi(flags, audio->ch1.control.hi);
905 flags = GBSerializedAudioFlagsSetCh1SweepEnabled(flags, audio->ch1.sweep.enable);
906 flags = GBSerializedAudioFlagsSetCh1SweepOccurred(flags, audio->ch1.sweep.occurred);
907 ch1Flags = GBSerializedAudioEnvelopeSetLength(ch1Flags, audio->ch1.control.length);
908 ch1Flags = GBSerializedAudioEnvelopeSetNextStep(ch1Flags, audio->ch1.envelope.nextStep);
909 ch1Flags = GBSerializedAudioEnvelopeSetFrequency(ch1Flags, audio->ch1.sweep.realFrequency);
910 STORE_32LE(ch1Flags, 0, &state->ch1.envelope);
911 STORE_32LE(audio->ch1Event.when - mTimingCurrentTime(audio->timing), 0, &state->ch1.nextEvent);
912
913 flags = GBSerializedAudioFlagsSetCh2Volume(flags, audio->ch2.envelope.currentVolume);
914 flags = GBSerializedAudioFlagsSetCh2Dead(flags, audio->ch2.envelope.dead);
915 flags = GBSerializedAudioFlagsSetCh2Hi(flags, audio->ch2.control.hi);
916 ch2Flags = GBSerializedAudioEnvelopeSetLength(ch2Flags, audio->ch2.control.length);
917 ch2Flags = GBSerializedAudioEnvelopeSetNextStep(ch2Flags, audio->ch2.envelope.nextStep);
918 STORE_32LE(ch2Flags, 0, &state->ch2.envelope);
919 STORE_32LE(audio->ch2Event.when - mTimingCurrentTime(audio->timing), 0, &state->ch2.nextEvent);
920
921 flags = GBSerializedAudioFlagsSetCh3Readable(flags, audio->ch3.readable);
922 memcpy(state->ch3.wavebanks, audio->ch3.wavedata32, sizeof(state->ch3.wavebanks));
923 STORE_16LE(audio->ch3.length, 0, &state->ch3.length);
924 STORE_32LE(audio->ch3Event.when - mTimingCurrentTime(audio->timing), 0, &state->ch3.nextEvent);
925 STORE_32LE(audio->ch3Fade.when - mTimingCurrentTime(audio->timing), 0, &state->ch1.nextCh3Fade);
926
927 flags = GBSerializedAudioFlagsSetCh4Volume(flags, audio->ch4.envelope.currentVolume);
928 flags = GBSerializedAudioFlagsSetCh4Dead(flags, audio->ch4.envelope.dead);
929 STORE_32LE(audio->ch4.lfsr, 0, &state->ch4.lfsr);
930 ch4Flags = GBSerializedAudioEnvelopeSetLength(ch4Flags, audio->ch4.length);
931 ch4Flags = GBSerializedAudioEnvelopeSetNextStep(ch4Flags, audio->ch4.envelope.nextStep);
932 STORE_32LE(ch4Flags, 0, &state->ch4.envelope);
933 STORE_32LE(audio->ch4Event.when - mTimingCurrentTime(audio->timing), 0, &state->ch4.nextEvent);
934
935 STORE_32LE(flags, 0, flagsOut);
936}
937
938void GBAudioPSGDeserialize(struct GBAudio* audio, const struct GBSerializedPSGState* state, const uint32_t* flagsIn) {
939 uint32_t flags;
940 uint32_t ch1Flags = 0;
941 uint32_t ch2Flags = 0;
942 uint32_t ch4Flags = 0;
943 uint32_t when;
944
945 audio->playingCh1 = !!(*audio->nr52 & 0x0001);
946 audio->playingCh2 = !!(*audio->nr52 & 0x0002);
947 audio->playingCh3 = !!(*audio->nr52 & 0x0004);
948 audio->playingCh4 = !!(*audio->nr52 & 0x0008);
949 audio->enable = GBAudioEnableGetEnable(*audio->nr52);
950
951 LOAD_32LE(flags, 0, flagsIn);
952 LOAD_32LE(ch1Flags, 0, &state->ch1.envelope);
953 audio->ch1.envelope.currentVolume = GBSerializedAudioFlagsGetCh1Volume(flags);
954 audio->ch1.envelope.dead = GBSerializedAudioFlagsGetCh1Dead(flags);
955 audio->ch1.control.hi = GBSerializedAudioFlagsGetCh1Hi(flags);
956 audio->ch1.sweep.enable = GBSerializedAudioFlagsGetCh1SweepEnabled(flags);
957 audio->ch1.sweep.occurred = GBSerializedAudioFlagsGetCh1SweepOccurred(flags);
958 audio->ch1.control.length = GBSerializedAudioEnvelopeGetLength(ch1Flags);
959 audio->ch1.envelope.nextStep = GBSerializedAudioEnvelopeGetNextStep(ch1Flags);
960 audio->ch1.sweep.realFrequency = GBSerializedAudioEnvelopeGetFrequency(ch1Flags);
961 LOAD_32LE(when, 0, &state->ch1.nextEvent);
962 mTimingDeschedule(audio->timing, &audio->ch1Event);
963 if (audio->ch1.envelope.dead < 2 && audio->playingCh1) {
964 mTimingSchedule(audio->timing, &audio->ch1Event, when);
965 }
966
967 LOAD_32LE(ch2Flags, 0, &state->ch2.envelope);
968 audio->ch2.envelope.currentVolume = GBSerializedAudioFlagsGetCh2Volume(flags);
969 audio->ch2.envelope.dead = GBSerializedAudioFlagsGetCh2Dead(flags);
970 audio->ch2.control.hi = GBSerializedAudioFlagsGetCh2Hi(flags);
971 audio->ch2.control.length = GBSerializedAudioEnvelopeGetLength(ch2Flags);
972 audio->ch2.envelope.nextStep = GBSerializedAudioEnvelopeGetNextStep(ch2Flags);
973 LOAD_32LE(when, 0, &state->ch2.nextEvent);
974 mTimingDeschedule(audio->timing, &audio->ch2Event);
975 if (audio->ch2.envelope.dead < 2 && audio->playingCh2) {
976 mTimingSchedule(audio->timing, &audio->ch2Event, when);
977 }
978
979 audio->ch3.readable = GBSerializedAudioFlagsGetCh3Readable(flags);
980 // TODO: Big endian?
981 memcpy(audio->ch3.wavedata32, state->ch3.wavebanks, sizeof(audio->ch3.wavedata32));
982 LOAD_16LE(audio->ch3.length, 0, &state->ch3.length);
983 LOAD_32LE(when, 0, &state->ch3.nextEvent);
984 mTimingDeschedule(audio->timing, &audio->ch3Event);
985 if (audio->playingCh3) {
986 mTimingSchedule(audio->timing, &audio->ch3Event, when);
987 }
988 LOAD_32LE(when, 0, &state->ch1.nextCh3Fade);
989 if (audio->ch3.readable && audio->style == GB_AUDIO_DMG) {
990 mTimingSchedule(audio->timing, &audio->ch3Fade, when);
991 }
992
993 LOAD_32LE(ch4Flags, 0, &state->ch4.envelope);
994 audio->ch4.envelope.currentVolume = GBSerializedAudioFlagsGetCh4Volume(flags);
995 audio->ch4.envelope.dead = GBSerializedAudioFlagsGetCh4Dead(flags);
996 audio->ch4.length = GBSerializedAudioEnvelopeGetLength(ch4Flags);
997 audio->ch4.envelope.nextStep = GBSerializedAudioEnvelopeGetNextStep(ch4Flags);
998 LOAD_32LE(audio->ch4.lfsr, 0, &state->ch4.lfsr);
999 LOAD_32LE(when, 0, &state->ch4.nextEvent);
1000 mTimingDeschedule(audio->timing, &audio->ch4Event);
1001 if (audio->ch4.envelope.dead < 2 && audio->playingCh4) {
1002 mTimingSchedule(audio->timing, &audio->ch4Event, when);
1003 }
1004}
1005
1006void GBAudioSerialize(const struct GBAudio* audio, struct GBSerializedState* state) {
1007 GBAudioPSGSerialize(audio, &state->audio.psg, &state->audio.flags);
1008 STORE_32LE(audio->sampleEvent.when - mTimingCurrentTime(audio->timing), 0, &state->audio.nextSample);
1009}
1010
1011void GBAudioDeserialize(struct GBAudio* audio, const struct GBSerializedState* state) {
1012 GBAudioPSGDeserialize(audio, &state->audio.psg, &state->audio.flags);
1013 uint32_t when;
1014 LOAD_32LE(when, 0, &state->audio.nextSample);
1015 mTimingDeschedule(audio->timing, &audio->sampleEvent);
1016 mTimingSchedule(audio->timing, &audio->sampleEvent, when);
1017}