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