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);
30static void _scheduleEvent(struct GBAudio* audio);
31
32void GBAudioInit(struct GBAudio* audio, size_t samples, uint8_t* nr52, enum GBAudioStyle style) {
33 audio->samples = samples;
34 audio->left = blip_new(BLIP_BUFFER_SIZE);
35 audio->right = blip_new(BLIP_BUFFER_SIZE);
36 audio->clockRate = DMG_LR35902_FREQUENCY;
37 // Guess too large; we hang producing extra samples if we guess too low
38 blip_set_rates(audio->left, DMG_LR35902_FREQUENCY, 96000);
39 blip_set_rates(audio->right, DMG_LR35902_FREQUENCY, 96000);
40 audio->forceDisableCh[0] = false;
41 audio->forceDisableCh[1] = false;
42 audio->forceDisableCh[2] = false;
43 audio->forceDisableCh[3] = false;
44 audio->masterVolume = GB_AUDIO_VOLUME_MAX;
45 audio->nr52 = nr52;
46 audio->style = style;
47}
48
49void GBAudioDeinit(struct GBAudio* audio) {
50 blip_delete(audio->left);
51 blip_delete(audio->right);
52}
53
54void GBAudioReset(struct GBAudio* audio) {
55 audio->nextEvent = 0;
56 audio->nextCh1 = 0;
57 audio->nextCh2 = 0;
58 audio->nextCh3 = 0;
59 audio->fadeCh3 = 0;
60 audio->nextCh4 = 0;
61 audio->ch1 = (struct GBAudioChannel1) { .envelope = { .dead = 2 } };
62 audio->ch2 = (struct GBAudioChannel2) { .envelope = { .dead = 2 } };
63 audio->ch3 = (struct GBAudioChannel3) { .bank = 0 };
64 audio->ch4 = (struct GBAudioChannel4) { .envelope = { .dead = 2 } };
65 audio->eventDiff = 0;
66 audio->nextFrame = 0;
67 audio->frame = 0;
68 audio->nextSample = 0;
69 audio->sampleInterval = 128;
70 audio->lastLeft = 0;
71 audio->lastRight = 0;
72 audio->clock = 0;
73 audio->volumeRight = 0;
74 audio->volumeLeft = 0;
75 audio->ch1Right = false;
76 audio->ch2Right = false;
77 audio->ch3Right = false;
78 audio->ch4Right = false;
79 audio->ch1Left = false;
80 audio->ch2Left = false;
81 audio->ch3Left = false;
82 audio->ch4Left = false;
83 audio->playingCh1 = false;
84 audio->playingCh2 = false;
85 audio->playingCh3 = false;
86 audio->playingCh4 = false;
87}
88
89void GBAudioResizeBuffer(struct GBAudio* audio, size_t samples) {
90 mCoreSyncLockAudio(audio->p->sync);
91 audio->samples = samples;
92 blip_clear(audio->left);
93 blip_clear(audio->right);
94 audio->clock = 0;
95 mCoreSyncConsumeAudio(audio->p->sync);
96}
97
98void GBAudioWriteNR10(struct GBAudio* audio, uint8_t value) {
99 audio->ch1.shift = GBAudioRegisterSquareSweepGetShift(value);
100 bool oldDirection = audio->ch1.direction;
101 audio->ch1.direction = GBAudioRegisterSquareSweepGetDirection(value);
102 if (audio->ch1.sweepOccurred && oldDirection && !audio->ch1.direction) {
103 audio->playingCh1 = false;
104 *audio->nr52 &= ~0x0001;
105 }
106 audio->ch1.sweepOccurred = false;
107 audio->ch1.time = GBAudioRegisterSquareSweepGetTime(value);
108 if (!audio->ch1.time) {
109 audio->ch1.time = 8;
110 }
111}
112
113void GBAudioWriteNR11(struct GBAudio* audio, uint8_t value) {
114 _writeDuty(&audio->ch1.envelope, value);
115 audio->ch1.control.length = 64 - audio->ch1.envelope.length;
116}
117
118void GBAudioWriteNR12(struct GBAudio* audio, uint8_t value) {
119 if (!_writeSweep(&audio->ch1.envelope, value)) {
120 audio->playingCh1 = false;
121 *audio->nr52 &= ~0x0001;
122 }
123}
124
125void GBAudioWriteNR13(struct GBAudio* audio, uint8_t value) {
126 audio->ch1.control.frequency &= 0x700;
127 audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value);
128}
129
130void GBAudioWriteNR14(struct GBAudio* audio, uint8_t value) {
131 audio->ch1.control.frequency &= 0xFF;
132 audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value << 8);
133 bool wasStop = audio->ch1.control.stop;
134 audio->ch1.control.stop = GBAudioRegisterControlGetStop(value << 8);
135 if (!wasStop && audio->ch1.control.stop && audio->ch1.control.length && !(audio->frame & 1)) {
136 --audio->ch1.control.length;
137 if (audio->ch1.control.length == 0) {
138 audio->playingCh1 = false;
139 }
140 }
141 if (GBAudioRegisterControlIsRestart(value << 8)) {
142 if (audio->nextEvent == INT_MAX) {
143 audio->eventDiff = 0;
144 }
145 if (audio->playingCh1) {
146 audio->ch1.control.hi = !audio->ch1.control.hi;
147 }
148 audio->nextCh1 = audio->eventDiff;
149 audio->playingCh1 = audio->ch1.envelope.initialVolume || audio->ch1.envelope.direction;
150 audio->ch1.envelope.currentVolume = audio->ch1.envelope.initialVolume;
151 if (audio->ch1.envelope.currentVolume > 0) {
152 audio->ch1.envelope.dead = audio->ch1.envelope.stepTime ? 0 : 1;
153 } else {
154 audio->ch1.envelope.dead = audio->ch1.envelope.stepTime ? 0 : 2;
155 }
156 audio->ch1.realFrequency = audio->ch1.control.frequency;
157 audio->ch1.sweepStep = audio->ch1.time;
158 audio->ch1.sweepEnable = (audio->ch1.sweepStep != 8) || audio->ch1.shift;
159 audio->ch1.sweepOccurred = false;
160 if (audio->playingCh1 && audio->ch1.shift) {
161 audio->playingCh1 = _updateSweep(&audio->ch1, true);
162 }
163 if (!audio->ch1.control.length) {
164 audio->ch1.control.length = 64;
165 if (audio->ch1.control.stop && !(audio->frame & 1)) {
166 --audio->ch1.control.length;
167 }
168 }
169 _scheduleEvent(audio);
170 }
171 *audio->nr52 &= ~0x0001;
172 *audio->nr52 |= audio->playingCh1;
173}
174
175void GBAudioWriteNR21(struct GBAudio* audio, uint8_t value) {
176 _writeDuty(&audio->ch2.envelope, value);
177 audio->ch2.control.length = 64 - audio->ch2.envelope.length;
178}
179
180void GBAudioWriteNR22(struct GBAudio* audio, uint8_t value) {
181 if (!_writeSweep(&audio->ch2.envelope, value)) {
182 audio->playingCh2 = false;
183 *audio->nr52 &= ~0x0002;
184 }
185}
186
187void GBAudioWriteNR23(struct GBAudio* audio, uint8_t value) {
188 audio->ch2.control.frequency &= 0x700;
189 audio->ch2.control.frequency |= GBAudioRegisterControlGetFrequency(value);
190}
191
192void GBAudioWriteNR24(struct GBAudio* audio, uint8_t value) {
193 audio->ch2.control.frequency &= 0xFF;
194 audio->ch2.control.frequency |= GBAudioRegisterControlGetFrequency(value << 8);
195 bool wasStop = audio->ch2.control.stop;
196 audio->ch2.control.stop = GBAudioRegisterControlGetStop(value << 8);
197 if (!wasStop && audio->ch2.control.stop && audio->ch2.control.length && !(audio->frame & 1)) {
198 --audio->ch2.control.length;
199 if (audio->ch2.control.length == 0) {
200 audio->playingCh2 = false;
201 }
202 }
203 if (GBAudioRegisterControlIsRestart(value << 8)) {
204 audio->playingCh2 = audio->ch2.envelope.initialVolume || audio->ch2.envelope.direction;
205 audio->ch2.envelope.currentVolume = audio->ch2.envelope.initialVolume;
206 if (audio->ch2.envelope.currentVolume > 0) {
207 audio->ch2.envelope.dead = audio->ch2.envelope.stepTime ? 0 : 1;
208 } else {
209 audio->ch2.envelope.dead = audio->ch2.envelope.stepTime ? 0 : 2;
210 }
211 if (audio->nextEvent == INT_MAX) {
212 audio->eventDiff = 0;
213 }
214 if (audio->playingCh2) {
215 audio->ch2.control.hi = !audio->ch2.control.hi;
216 }
217 audio->nextCh2 = audio->eventDiff;
218 if (!audio->ch2.control.length) {
219 audio->ch2.control.length = 64;
220 if (audio->ch2.control.stop && !(audio->frame & 1)) {
221 --audio->ch2.control.length;
222 }
223 }
224 _scheduleEvent(audio);
225 }
226 *audio->nr52 &= ~0x0002;
227 *audio->nr52 |= audio->playingCh2 << 1;
228}
229
230void GBAudioWriteNR30(struct GBAudio* audio, uint8_t value) {
231 audio->ch3.enable = GBAudioRegisterBankGetEnable(value);
232 if (!audio->ch3.enable) {
233 audio->playingCh3 = false;
234 *audio->nr52 &= ~0x0004;
235 }
236}
237
238void GBAudioWriteNR31(struct GBAudio* audio, uint8_t value) {
239 audio->ch3.length = 256 - value;
240}
241
242void GBAudioWriteNR32(struct GBAudio* audio, uint8_t value) {
243 audio->ch3.volume = GBAudioRegisterBankVolumeGetVolumeGB(value);
244}
245
246void GBAudioWriteNR33(struct GBAudio* audio, uint8_t value) {
247 audio->ch3.rate &= 0x700;
248 audio->ch3.rate |= GBAudioRegisterControlGetRate(value);
249}
250
251void GBAudioWriteNR34(struct GBAudio* audio, uint8_t value) {
252 audio->ch3.rate &= 0xFF;
253 audio->ch3.rate |= GBAudioRegisterControlGetRate(value << 8);
254 bool wasStop = audio->ch3.stop;
255 audio->ch3.stop = GBAudioRegisterControlGetStop(value << 8);
256 if (!wasStop && audio->ch3.stop && audio->ch3.length && !(audio->frame & 1)) {
257 --audio->ch3.length;
258 if (audio->ch3.length == 0) {
259 audio->playingCh3 = false;
260 }
261 }
262 bool wasEnable = audio->playingCh3;
263 if (GBAudioRegisterControlIsRestart(value << 8)) {
264 audio->playingCh3 = audio->ch3.enable;
265 if (!audio->ch3.length) {
266 audio->ch3.length = 256;
267 if (audio->ch3.stop && !(audio->frame & 1)) {
268 --audio->ch3.length;
269 }
270 }
271
272 if (audio->style == GB_AUDIO_DMG && wasEnable && audio->playingCh3 && audio->ch3.readable) {
273 if (audio->ch3.window < 8) {
274 audio->ch3.wavedata8[0] = audio->ch3.wavedata8[audio->ch3.window >> 1];
275 } else {
276 audio->ch3.wavedata8[0] = audio->ch3.wavedata8[((audio->ch3.window >> 1) & ~3)];
277 audio->ch3.wavedata8[1] = audio->ch3.wavedata8[((audio->ch3.window >> 1) & ~3) + 1];
278 audio->ch3.wavedata8[2] = audio->ch3.wavedata8[((audio->ch3.window >> 1) & ~3) + 2];
279 audio->ch3.wavedata8[3] = audio->ch3.wavedata8[((audio->ch3.window >> 1) & ~3) + 3];
280 }
281 }
282 audio->ch3.window = 0;
283 }
284 if (audio->playingCh3) {
285 if (audio->nextEvent == INT_MAX) {
286 audio->eventDiff = 0;
287 }
288 audio->ch3.readable = audio->style != GB_AUDIO_DMG;
289 _scheduleEvent(audio);
290 // TODO: Where does this cycle delay come from?
291 audio->nextCh3 = audio->eventDiff + audio->nextEvent + 4 + 2 * (2048 - audio->ch3.rate);
292 }
293 *audio->nr52 &= ~0x0004;
294 *audio->nr52 |= audio->playingCh3 << 2;
295}
296
297void GBAudioWriteNR41(struct GBAudio* audio, uint8_t value) {
298 _writeDuty(&audio->ch4.envelope, value);
299 audio->ch4.length = 64 - audio->ch4.envelope.length;
300}
301
302void GBAudioWriteNR42(struct GBAudio* audio, uint8_t value) {
303 if (!_writeSweep(&audio->ch4.envelope, value)) {
304 audio->playingCh4 = false;
305 *audio->nr52 &= ~0x0008;
306 }
307}
308
309void GBAudioWriteNR43(struct GBAudio* audio, uint8_t value) {
310 audio->ch4.ratio = GBAudioRegisterNoiseFeedbackGetRatio(value);
311 audio->ch4.frequency = GBAudioRegisterNoiseFeedbackGetFrequency(value);
312 audio->ch4.power = GBAudioRegisterNoiseFeedbackGetPower(value);
313}
314
315void GBAudioWriteNR44(struct GBAudio* audio, uint8_t value) {
316 bool wasStop = audio->ch4.stop;
317 audio->ch4.stop = GBAudioRegisterNoiseControlGetStop(value);
318 if (!wasStop && audio->ch4.stop && audio->ch4.length && !(audio->frame & 1)) {
319 --audio->ch4.length;
320 if (audio->ch4.length == 0) {
321 audio->playingCh4 = false;
322 }
323 }
324 if (GBAudioRegisterNoiseControlIsRestart(value)) {
325 audio->playingCh4 = audio->ch4.envelope.initialVolume || audio->ch4.envelope.direction;
326 audio->ch4.envelope.currentVolume = audio->ch4.envelope.initialVolume;
327 if (audio->ch4.envelope.currentVolume > 0) {
328 audio->ch4.envelope.dead = audio->ch4.envelope.stepTime ? 0 : 1;
329 } else {
330 audio->ch4.envelope.dead = audio->ch4.envelope.stepTime ? 0 : 2;
331 }
332 if (audio->ch4.power) {
333 audio->ch4.lfsr = 0x40;
334 } else {
335 audio->ch4.lfsr = 0x4000;
336 }
337 if (audio->nextEvent == INT_MAX) {
338 audio->eventDiff = 0;
339 }
340 audio->nextCh4 = audio->eventDiff;
341 if (!audio->ch4.length) {
342 audio->ch4.length = 64;
343 if (audio->ch4.stop && !(audio->frame & 1)) {
344 --audio->ch4.length;
345 }
346 }
347 _scheduleEvent(audio);
348 }
349 *audio->nr52 &= ~0x0008;
350 *audio->nr52 |= audio->playingCh4 << 3;
351}
352
353void GBAudioWriteNR50(struct GBAudio* audio, uint8_t value) {
354 audio->volumeRight = GBRegisterNR50GetVolumeRight(value);
355 audio->volumeLeft = GBRegisterNR50GetVolumeLeft(value);
356}
357
358void GBAudioWriteNR51(struct GBAudio* audio, uint8_t value) {
359 audio->ch1Right = GBRegisterNR51GetCh1Right(value);
360 audio->ch2Right = GBRegisterNR51GetCh2Right(value);
361 audio->ch3Right = GBRegisterNR51GetCh3Right(value);
362 audio->ch4Right = GBRegisterNR51GetCh4Right(value);
363 audio->ch1Left = GBRegisterNR51GetCh1Left(value);
364 audio->ch2Left = GBRegisterNR51GetCh2Left(value);
365 audio->ch3Left = GBRegisterNR51GetCh3Left(value);
366 audio->ch4Left = GBRegisterNR51GetCh4Left(value);
367}
368
369void GBAudioWriteNR52(struct GBAudio* audio, uint8_t value) {
370 bool wasEnable = audio->enable;
371 audio->enable = GBAudioEnableGetEnable(value);
372 if (!audio->enable) {
373 audio->playingCh1 = 0;
374 audio->playingCh2 = 0;
375 audio->playingCh3 = 0;
376 audio->playingCh4 = 0;
377 GBAudioWriteNR10(audio, 0);
378 GBAudioWriteNR12(audio, 0);
379 GBAudioWriteNR13(audio, 0);
380 GBAudioWriteNR14(audio, 0);
381 GBAudioWriteNR22(audio, 0);
382 GBAudioWriteNR23(audio, 0);
383 GBAudioWriteNR24(audio, 0);
384 GBAudioWriteNR30(audio, 0);
385 GBAudioWriteNR32(audio, 0);
386 GBAudioWriteNR33(audio, 0);
387 GBAudioWriteNR34(audio, 0);
388 GBAudioWriteNR42(audio, 0);
389 GBAudioWriteNR43(audio, 0);
390 GBAudioWriteNR44(audio, 0);
391 GBAudioWriteNR50(audio, 0);
392 GBAudioWriteNR51(audio, 0);
393 if (audio->style != GB_AUDIO_DMG) {
394 GBAudioWriteNR11(audio, 0);
395 GBAudioWriteNR21(audio, 0);
396 GBAudioWriteNR31(audio, 0);
397 GBAudioWriteNR41(audio, 0);
398 }
399
400 if (audio->p) {
401 audio->p->memory.io[REG_NR10] = 0;
402 audio->p->memory.io[REG_NR11] = 0;
403 audio->p->memory.io[REG_NR12] = 0;
404 audio->p->memory.io[REG_NR13] = 0;
405 audio->p->memory.io[REG_NR14] = 0;
406 audio->p->memory.io[REG_NR21] = 0;
407 audio->p->memory.io[REG_NR22] = 0;
408 audio->p->memory.io[REG_NR23] = 0;
409 audio->p->memory.io[REG_NR24] = 0;
410 audio->p->memory.io[REG_NR30] = 0;
411 audio->p->memory.io[REG_NR31] = 0;
412 audio->p->memory.io[REG_NR32] = 0;
413 audio->p->memory.io[REG_NR33] = 0;
414 audio->p->memory.io[REG_NR34] = 0;
415 audio->p->memory.io[REG_NR42] = 0;
416 audio->p->memory.io[REG_NR43] = 0;
417 audio->p->memory.io[REG_NR44] = 0;
418 audio->p->memory.io[REG_NR50] = 0;
419 audio->p->memory.io[REG_NR51] = 0;
420 if (audio->style != GB_AUDIO_DMG) {
421 audio->p->memory.io[REG_NR11] = 0;
422 audio->p->memory.io[REG_NR21] = 0;
423 audio->p->memory.io[REG_NR31] = 0;
424 audio->p->memory.io[REG_NR41] = 0;
425 }
426 }
427 *audio->nr52 &= ~0x000F;
428 } else if (!wasEnable) {
429 audio->frame = 7;
430 }
431}
432
433int32_t GBAudioProcessEvents(struct GBAudio* audio, int32_t cycles) {
434 if (audio->nextEvent == INT_MAX) {
435 return INT_MAX;
436 }
437 audio->nextEvent -= cycles;
438 audio->eventDiff += cycles;
439 while (audio->nextEvent <= 0) {
440 audio->nextEvent = INT_MAX;
441 if (audio->enable) {
442 audio->nextFrame -= audio->eventDiff;
443 int frame = -1;
444 if (audio->nextFrame <= 0) {
445 frame = (audio->frame + 1) & 7;
446 audio->frame = frame;
447 audio->nextFrame += FRAME_CYCLES;
448 if (audio->nextFrame < audio->nextEvent) {
449 audio->nextEvent = audio->nextFrame;
450 }
451 }
452
453 if (audio->playingCh1) {
454 audio->nextCh1 -= audio->eventDiff;
455 if (!audio->ch1.envelope.dead && frame == 7) {
456 --audio->ch1.envelope.nextStep;
457 if (audio->ch1.envelope.nextStep == 0) {
458 int8_t sample = audio->ch1.control.hi * 0x10 - 0x8;
459 _updateEnvelope(&audio->ch1.envelope);
460 audio->ch1.sample = sample * audio->ch1.envelope.currentVolume;
461 }
462 }
463
464 if (audio->ch1.sweepEnable && (frame & 3) == 2) {
465 --audio->ch1.sweepStep;
466 if (audio->ch1.sweepStep == 0) {
467 audio->playingCh1 = _updateSweep(&audio->ch1, false);
468 }
469 }
470
471 if (audio->ch1.envelope.dead != 2) {
472 if (audio->nextCh1 <= 0) {
473 audio->nextCh1 += _updateChannel1(&audio->ch1);
474 }
475 if (audio->nextCh1 < audio->nextEvent) {
476 audio->nextEvent = audio->nextCh1;
477 }
478 }
479 }
480
481 if (audio->ch1.control.length && audio->ch1.control.stop && !(frame & 1)) {
482 --audio->ch1.control.length;
483 if (audio->ch1.control.length == 0) {
484 audio->playingCh1 = 0;
485 }
486 }
487
488 if (audio->playingCh2) {
489 audio->nextCh2 -= audio->eventDiff;
490 if (!audio->ch2.envelope.dead && frame == 7) {
491 --audio->ch2.envelope.nextStep;
492 if (audio->ch2.envelope.nextStep == 0) {
493 int8_t sample = audio->ch2.control.hi * 0x10 - 0x8;
494 _updateEnvelope(&audio->ch2.envelope);
495 audio->ch2.sample = sample * audio->ch2.envelope.currentVolume;
496 }
497 }
498
499 if (audio->ch2.envelope.dead != 2) {
500 if (audio->nextCh2 <= 0) {
501 audio->nextCh2 += _updateChannel2(&audio->ch2);
502 }
503 if (audio->nextCh2 < audio->nextEvent) {
504 audio->nextEvent = audio->nextCh2;
505 }
506 }
507 }
508
509 if (audio->ch2.control.length && audio->ch2.control.stop && !(frame & 1)) {
510 --audio->ch2.control.length;
511 if (audio->ch2.control.length == 0) {
512 audio->playingCh2 = 0;
513 }
514 }
515
516 if (audio->playingCh3) {
517 audio->nextCh3 -= audio->eventDiff;
518 audio->fadeCh3 -= audio->eventDiff;
519 if (audio->fadeCh3 <= 0) {
520 audio->ch3.readable = false;
521 audio->fadeCh3 = INT_MAX;
522 }
523 if (audio->nextCh3 <= 0) {
524 if (audio->style == GB_AUDIO_DMG) {
525 audio->fadeCh3 = audio->nextCh3 + 2;
526 }
527 audio->nextCh3 += _updateChannel3(&audio->ch3, audio->style);
528 audio->ch3.readable = true;
529 }
530 if (audio->fadeCh3 < audio->nextEvent) {
531 audio->nextEvent = audio->fadeCh3;
532 }
533 if (audio->nextCh3 < audio->nextEvent) {
534 audio->nextEvent = audio->nextCh3;
535 }
536 }
537
538 if (audio->ch3.length && audio->ch3.stop && !(frame & 1)) {
539 --audio->ch3.length;
540 if (audio->ch3.length == 0) {
541 audio->playingCh3 = 0;
542 }
543 }
544
545 if (audio->playingCh4) {
546 audio->nextCh4 -= audio->eventDiff;
547 if (!audio->ch4.envelope.dead && frame == 7) {
548 --audio->ch4.envelope.nextStep;
549 if (audio->ch4.envelope.nextStep == 0) {
550 int8_t sample = (audio->ch4.sample >> 31) * 0x8;
551 _updateEnvelope(&audio->ch4.envelope);
552 audio->ch4.sample = sample * audio->ch4.envelope.currentVolume;
553 }
554 }
555 }
556
557 if (audio->ch4.length && audio->ch4.stop && !(frame & 1)) {
558 --audio->ch4.length;
559 if (audio->ch4.length == 0) {
560 audio->playingCh4 = 0;
561 }
562 }
563 }
564
565 *audio->nr52 &= ~0x000F;
566 *audio->nr52 |= audio->playingCh1;
567 *audio->nr52 |= audio->playingCh2 << 1;
568 *audio->nr52 |= audio->playingCh3 << 2;
569 *audio->nr52 |= audio->playingCh4 << 3;
570
571 if (audio->p) {
572 audio->nextSample -= audio->eventDiff;
573 if (audio->nextSample <= 0) {
574 _sample(audio, audio->sampleInterval);
575 audio->nextSample += audio->sampleInterval;
576 }
577
578 if (audio->nextSample < audio->nextEvent) {
579 audio->nextEvent = audio->nextSample;
580 }
581 }
582 audio->eventDiff = 0;
583 }
584 return audio->nextEvent;
585}
586
587void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right) {
588 int sampleLeft = 0;
589 int sampleRight = 0;
590
591 if (audio->ch4.envelope.dead != 2) {
592 while (audio->nextCh4 <= 0) {
593 int32_t timing = _updateChannel4(&audio->ch4);
594 if (audio->nextCh4 < -timing) {
595 int32_t bound = timing * 16;
596 // Perform negative modulo to cap to 16 iterations
597 audio->nextCh4 = bound - (audio->nextCh4 - 1) % bound - 1;
598 }
599 audio->nextCh4 += timing;
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->nextEvent;
860 } else {
861 audio->nextEvent = 0;
862 }
863}