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.control.frequency &= 0x700;
122 audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value);
123}
124
125void GBAudioWriteNR14(struct GBAudio* audio, uint8_t value) {
126 audio->ch1.control.frequency &= 0xFF;
127 audio->ch1.control.frequency |= 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.realFrequency = audio->ch1.control.frequency;
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 = audio->style != GB_AUDIO_DMG;
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 if (audio->style == GB_AUDIO_DMG) {
539 audio->fadeCh3 = audio->nextCh3 + 2;
540 }
541 audio->nextCh3 += _updateChannel3(&audio->ch3, audio->style);
542 audio->ch3.readable = true;
543 }
544 if (audio->fadeCh3 < audio->nextEvent) {
545 audio->nextEvent = audio->fadeCh3;
546 }
547 if (audio->nextCh3 < audio->nextEvent) {
548 audio->nextEvent = audio->nextCh3;
549 }
550 }
551
552 if (audio->ch3.length && audio->ch3.stop && !(frame & 1)) {
553 --audio->ch3.length;
554 if (audio->ch3.length == 0) {
555 audio->playingCh3 = 0;
556 }
557 }
558
559 if (audio->playingCh4) {
560 audio->nextCh4 -= audio->eventDiff;
561 if (!audio->ch4.envelope.dead && frame == 7) {
562 --audio->ch4.envelope.nextStep;
563 if (audio->ch4.envelope.nextStep == 0) {
564 int8_t sample = (audio->ch4.sample >> 31) * 0x8;
565 _updateEnvelope(&audio->ch4.envelope);
566 audio->ch4.sample = sample * audio->ch4.envelope.currentVolume;
567 }
568 }
569
570 if (audio->ch4.envelope.dead != 2) {
571 if (audio->nextCh4 <= 0) {
572 int32_t timing = _updateChannel4(&audio->ch4);
573 if (audio->nextCh4 < -timing) {
574 int32_t bound = timing * 16;
575 // Perform negative modulo to cap to 16 iterations
576 audio->nextCh4 = bound - (audio->nextCh4 - 1) % bound - 1;
577 }
578 audio->nextCh4 += timing;
579 }
580 if (audio->nextCh4 < audio->nextEvent) {
581 audio->nextEvent = audio->nextCh4;
582 }
583 }
584 }
585
586 if (audio->ch4.length && audio->ch4.stop && !(frame & 1)) {
587 --audio->ch4.length;
588 if (audio->ch4.length == 0) {
589 audio->playingCh4 = 0;
590 }
591 }
592 }
593
594 *audio->nr52 &= ~0x000F;
595 *audio->nr52 |= audio->playingCh1;
596 *audio->nr52 |= audio->playingCh2 << 1;
597 *audio->nr52 |= audio->playingCh3 << 2;
598 *audio->nr52 |= audio->playingCh4 << 3;
599
600 if (audio->p) {
601 audio->nextSample -= audio->eventDiff;
602 if (audio->nextSample <= 0) {
603 _sample(audio, audio->sampleInterval);
604 audio->nextSample += audio->sampleInterval;
605 }
606
607 if (audio->nextSample < audio->nextEvent) {
608 audio->nextEvent = audio->nextSample;
609 }
610 }
611 audio->eventDiff = 0;
612 }
613 return audio->nextEvent;
614}
615
616void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right) {
617 int sampleLeft = 0;
618 int sampleRight = 0;
619
620 if (audio->playingCh1 && !audio->forceDisableCh[0]) {
621 if (audio->ch1Left) {
622 sampleLeft += audio->ch1.sample;
623 }
624
625 if (audio->ch1Right) {
626 sampleRight += audio->ch1.sample;
627 }
628 }
629
630 if (audio->playingCh2 && !audio->forceDisableCh[1]) {
631 if (audio->ch2Left) {
632 sampleLeft += audio->ch2.sample;
633 }
634
635 if (audio->ch2Right) {
636 sampleRight += audio->ch2.sample;
637 }
638 }
639
640 if (audio->playingCh3 && !audio->forceDisableCh[2]) {
641 if (audio->ch3Left) {
642 sampleLeft += audio->ch3.sample;
643 }
644
645 if (audio->ch3Right) {
646 sampleRight += audio->ch3.sample;
647 }
648 }
649
650 if (audio->playingCh4 && !audio->forceDisableCh[3]) {
651 if (audio->ch4Left) {
652 sampleLeft += audio->ch4.sample;
653 }
654
655 if (audio->ch4Right) {
656 sampleRight += audio->ch4.sample;
657 }
658 }
659
660 *left = sampleLeft * (1 + audio->volumeLeft);
661 *right = sampleRight * (1 + audio->volumeRight);
662}
663
664void _sample(struct GBAudio* audio, int32_t cycles) {
665 int16_t sampleLeft = 0;
666 int16_t sampleRight = 0;
667 GBAudioSamplePSG(audio, &sampleLeft, &sampleRight);
668 sampleLeft = (sampleLeft * audio->masterVolume) >> 6;
669 sampleRight = (sampleRight * audio->masterVolume) >> 6;
670
671 mCoreSyncLockAudio(audio->p->sync);
672 unsigned produced;
673 if ((size_t) blip_samples_avail(audio->left) < audio->samples) {
674 blip_add_delta(audio->left, audio->clock, sampleLeft - audio->lastLeft);
675 blip_add_delta(audio->right, audio->clock, sampleRight - audio->lastRight);
676 audio->lastLeft = sampleLeft;
677 audio->lastRight = sampleRight;
678 audio->clock += cycles;
679 if (audio->clock >= CLOCKS_PER_BLIP_FRAME) {
680 blip_end_frame(audio->left, audio->clock);
681 blip_end_frame(audio->right, audio->clock);
682 audio->clock -= CLOCKS_PER_BLIP_FRAME;
683 }
684 }
685 produced = blip_samples_avail(audio->left);
686 if (audio->p->stream && audio->p->stream->postAudioFrame) {
687 audio->p->stream->postAudioFrame(audio->p->stream, sampleLeft, sampleRight);
688 }
689 bool wait = produced >= audio->samples;
690 mCoreSyncProduceAudio(audio->p->sync, wait);
691
692 if (wait && audio->p->stream && audio->p->stream->postAudioBuffer) {
693 audio->p->stream->postAudioBuffer(audio->p->stream, audio->left, audio->right);
694 }
695}
696
697void _writeDuty(struct GBAudioEnvelope* envelope, uint8_t value) {
698 envelope->length = GBAudioRegisterDutyGetLength(value);
699 envelope->duty = GBAudioRegisterDutyGetDuty(value);
700}
701
702bool _writeSweep(struct GBAudioEnvelope* envelope, uint8_t value) {
703 envelope->stepTime = GBAudioRegisterSweepGetStepTime(value);
704 envelope->direction = GBAudioRegisterSweepGetDirection(value);
705 envelope->initialVolume = GBAudioRegisterSweepGetInitialVolume(value);
706 if (envelope->stepTime == 0) {
707 envelope->dead = envelope->currentVolume ? 1 : 2;
708 } else if (!envelope->direction && !envelope->currentVolume) {
709 envelope->dead = 2;
710 } else if (envelope->direction && envelope->currentVolume == 0xF) {
711 envelope->dead = 1;
712 } else {
713 envelope->dead = 0;
714 }
715 envelope->nextStep = envelope->stepTime;
716 return envelope->initialVolume || envelope->direction;
717}
718
719static int32_t _updateSquareChannel(struct GBAudioSquareControl* control, int duty) {
720 control->hi = !control->hi;
721 int period = 4 * (2048 - control->frequency);
722 switch (duty) {
723 case 0:
724 return control->hi ? period : period * 7;
725 case 1:
726 return control->hi ? period * 2 : period * 6;
727 case 2:
728 return period * 4;
729 case 3:
730 return control->hi ? period * 6 : period * 2;
731 default:
732 // This should never be hit
733 return period * 4;
734 }
735}
736
737static void _updateEnvelope(struct GBAudioEnvelope* envelope) {
738 if (envelope->direction) {
739 ++envelope->currentVolume;
740 } else {
741 --envelope->currentVolume;
742 }
743 if (envelope->currentVolume >= 15) {
744 envelope->currentVolume = 15;
745 envelope->dead = 1;
746 } else if (envelope->currentVolume <= 0) {
747 envelope->currentVolume = 0;
748 envelope->dead = 2;
749 } else {
750 envelope->nextStep = envelope->stepTime;
751 }
752}
753
754static bool _updateSweep(struct GBAudioChannel1* ch, bool initial) {
755 if (initial || ch->time != 8) {
756 int frequency = ch->realFrequency;
757 if (ch->direction) {
758 frequency -= frequency >> ch->shift;
759 if (!initial && frequency >= 0) {
760 ch->control.frequency = frequency;
761 ch->realFrequency = frequency;
762 }
763 } else {
764 frequency += frequency >> ch->shift;
765 if (frequency < 2048) {
766 if (!initial && ch->shift) {
767 ch->control.frequency = frequency;
768 ch->realFrequency = frequency;
769 if (!_updateSweep(ch, true)) {
770 return false;
771 }
772 }
773 } else {
774 return false;
775 }
776 }
777 ch->sweepOccurred = true;
778 }
779 ch->sweepStep = ch->time;
780 return true;
781}
782
783static int32_t _updateChannel1(struct GBAudioChannel1* ch) {
784 int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
785 ch->sample = ch->control.hi * 0x10 - 0x8;
786 ch->sample *= ch->envelope.currentVolume;
787 return timing;
788}
789
790static int32_t _updateChannel2(struct GBAudioChannel2* ch) {
791 int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
792 ch->sample = ch->control.hi * 0x10 - 0x8;
793 ch->sample *= ch->envelope.currentVolume;
794 return timing;
795}
796
797static int32_t _updateChannel3(struct GBAudioChannel3* ch, enum GBAudioStyle style) {
798 int i;
799 int volume;
800 switch (ch->volume) {
801 case 0:
802 volume = 0;
803 break;
804 case 1:
805 volume = 4;
806 break;
807 case 2:
808 volume = 2;
809 break;
810 case 3:
811 volume = 1;
812 break;
813 default:
814 volume = 3;
815 break;
816 }
817 switch (style) {
818 int start;
819 int end;
820 case GB_AUDIO_DMG:
821 default:
822 ++ch->window;
823 ch->window &= 0x1F;
824 ch->sample = ch->wavedata8[ch->window >> 1];
825 if (!(ch->window & 1)) {
826 ch->sample >>= 4;
827 }
828 ch->sample &= 0xF;
829 break;
830 case GB_AUDIO_GBA:
831 if (ch->size) {
832 start = 7;
833 end = 0;
834 } else if (ch->bank) {
835 start = 7;
836 end = 4;
837 } else {
838 start = 3;
839 end = 0;
840 }
841 uint32_t bitsCarry = ch->wavedata32[end] & 0x000000F0;
842 uint32_t bits;
843 for (i = start; i >= end; --i) {
844 bits = ch->wavedata32[i] & 0x000000F0;
845 ch->wavedata32[i] = ((ch->wavedata32[i] & 0x0F0F0F0F) << 4) | ((ch->wavedata32[i] & 0xF0F0F000) >> 12);
846 ch->wavedata32[i] |= bitsCarry << 20;
847 bitsCarry = bits;
848 }
849 ch->sample = bitsCarry >> 4;
850 break;
851 }
852 ch->sample -= 8;
853 ch->sample *= volume * 4;
854 return 2 * (2048 - ch->rate);
855}
856
857static int32_t _updateChannel4(struct GBAudioChannel4* ch) {
858 int lsb = ch->lfsr & 1;
859 ch->sample = lsb * 0x10 - 0x8;
860 ch->sample *= ch->envelope.currentVolume;
861 ch->lfsr >>= 1;
862 ch->lfsr ^= (lsb * 0x60) << (ch->power ? 0 : 8);
863 int timing = ch->ratio ? 2 * ch->ratio : 1;
864 timing <<= ch->frequency;
865 timing *= 8;
866 return timing;
867}