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