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