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