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