src/gba/audio.c (view raw)
1/* Copyright (c) 2013-2015 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 "gba/gba.h"
9#include "gba/io.h"
10#include "gba/serialize.h"
11#include "gba/supervisor/thread.h"
12#include "gba/video.h"
13
14const unsigned GBA_AUDIO_SAMPLES = 2048;
15const unsigned BLIP_BUFFER_SIZE = 0x4000;
16const unsigned GBA_AUDIO_FIFO_SIZE = 8 * sizeof(int32_t);
17const int GBA_AUDIO_VOLUME_MAX = 0x100;
18#define SWEEP_CYCLES (GBA_ARM7TDMI_FREQUENCY / 128)
19
20#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
21static const int CLOCKS_PER_FRAME = 0x400;
22#endif
23
24static bool _writeEnvelope(struct GBAAudioEnvelope* envelope, uint16_t value);
25static int32_t _updateSquareChannel(struct GBAAudioSquareControl* envelope, int duty);
26static void _updateEnvelope(struct GBAAudioEnvelope* envelope);
27static bool _updateSweep(struct GBAAudioChannel1* ch);
28static int32_t _updateChannel1(struct GBAAudioChannel1* ch);
29static int32_t _updateChannel2(struct GBAAudioChannel2* ch);
30static int32_t _updateChannel3(struct GBAAudioChannel3* ch);
31static int32_t _updateChannel4(struct GBAAudioChannel4* ch);
32static int _applyBias(struct GBAAudio* audio, int sample);
33static void _sample(struct GBAAudio* audio);
34
35void GBAAudioInit(struct GBAAudio* audio, size_t samples) {
36 audio->samples = samples;
37#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
38 CircleBufferInit(&audio->left, samples * sizeof(int16_t));
39 CircleBufferInit(&audio->right, samples * sizeof(int16_t));
40#else
41 audio->left = blip_new(BLIP_BUFFER_SIZE);
42 audio->right = blip_new(BLIP_BUFFER_SIZE);
43 // Guess too large; we hang producing extra samples if we guess too low
44 blip_set_rates(audio->left, GBA_ARM7TDMI_FREQUENCY, 96000);
45 blip_set_rates(audio->right, GBA_ARM7TDMI_FREQUENCY, 96000);
46#endif
47 CircleBufferInit(&audio->chA.fifo, GBA_AUDIO_FIFO_SIZE);
48 CircleBufferInit(&audio->chB.fifo, GBA_AUDIO_FIFO_SIZE);
49
50 audio->forceDisableCh[0] = false;
51 audio->forceDisableCh[1] = false;
52 audio->forceDisableCh[2] = false;
53 audio->forceDisableCh[3] = false;
54 audio->forceDisableChA = false;
55 audio->forceDisableChB = false;
56 audio->masterVolume = GBA_AUDIO_VOLUME_MAX;
57}
58
59void GBAAudioReset(struct GBAAudio* audio) {
60 audio->nextEvent = 0;
61 audio->nextCh1 = 0;
62 audio->nextCh2 = 0;
63 audio->nextCh3 = 0;
64 audio->nextCh4 = 0;
65 audio->ch1 = (struct GBAAudioChannel1) { .envelope = { .nextStep = INT_MAX }, .nextSweep = INT_MAX };
66 audio->ch2 = (struct GBAAudioChannel2) { .envelope = { .nextStep = INT_MAX } };
67 audio->ch3 = (struct GBAAudioChannel3) { .bank = { .bank = 0 } };
68 audio->ch4 = (struct GBAAudioChannel4) { .envelope = { .nextStep = INT_MAX } };
69 audio->chA.dmaSource = 1;
70 audio->chB.dmaSource = 2;
71 audio->chA.sample = 0;
72 audio->chB.sample = 0;
73 audio->eventDiff = 0;
74 audio->nextSample = 0;
75 audio->sampleRate = 0x8000;
76 audio->soundbias = 0x200;
77 audio->volumeRight = 0;
78 audio->volumeLeft = 0;
79 audio->ch1Right = false;
80 audio->ch2Right = false;
81 audio->ch3Right = false;
82 audio->ch4Right = false;
83 audio->ch1Left = false;
84 audio->ch2Left = false;
85 audio->ch3Left = false;
86 audio->ch4Left = false;
87 audio->volume = 0;
88 audio->volumeChA = false;
89 audio->volumeChB = false;
90 audio->chARight = false;
91 audio->chALeft = false;
92 audio->chATimer = false;
93 audio->chBRight = false;
94 audio->chBLeft = false;
95 audio->chBTimer = false;
96 audio->playingCh1 = false;
97 audio->playingCh2 = false;
98 audio->playingCh3 = false;
99 audio->playingCh4 = false;
100 audio->enable = false;
101 audio->sampleInterval = GBA_ARM7TDMI_FREQUENCY / audio->sampleRate;
102
103#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
104 CircleBufferClear(&audio->left);
105 CircleBufferClear(&audio->right);
106#else
107 blip_clear(audio->left);
108 blip_clear(audio->right);
109 audio->clock = 0;
110#endif
111 CircleBufferClear(&audio->chA.fifo);
112 CircleBufferClear(&audio->chB.fifo);
113}
114
115void GBAAudioDeinit(struct GBAAudio* audio) {
116#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
117 CircleBufferDeinit(&audio->left);
118 CircleBufferDeinit(&audio->right);
119#else
120 blip_delete(audio->left);
121 blip_delete(audio->right);
122#endif
123 CircleBufferDeinit(&audio->chA.fifo);
124 CircleBufferDeinit(&audio->chB.fifo);
125}
126
127void GBAAudioResizeBuffer(struct GBAAudio* audio, size_t samples) {
128 GBASyncLockAudio(audio->p->sync);
129 audio->samples = samples;
130#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
131 size_t oldCapacity = audio->left.capacity;
132 int16_t* buffer = malloc(oldCapacity);
133 int16_t dummy;
134 size_t read;
135 size_t i;
136
137 read = CircleBufferDump(&audio->left, buffer, oldCapacity);
138 CircleBufferDeinit(&audio->left);
139 CircleBufferInit(&audio->left, samples * sizeof(int16_t));
140 for (i = 0; i * sizeof(int16_t) < read; ++i) {
141 if (!CircleBufferWrite16(&audio->left, buffer[i])) {
142 CircleBufferRead16(&audio->left, &dummy);
143 CircleBufferWrite16(&audio->left, buffer[i]);
144 }
145 }
146
147 read = CircleBufferDump(&audio->right, buffer, oldCapacity);
148 CircleBufferDeinit(&audio->right);
149 CircleBufferInit(&audio->right, samples * sizeof(int16_t));
150 for (i = 0; i * sizeof(int16_t) < read; ++i) {
151 if (!CircleBufferWrite16(&audio->right, buffer[i])) {
152 CircleBufferRead16(&audio->right, &dummy);
153 CircleBufferWrite16(&audio->right, buffer[i]);
154 }
155 }
156
157 free(buffer);
158#else
159 blip_clear(audio->left);
160 blip_clear(audio->right);
161 audio->clock = 0;
162#endif
163
164 GBASyncConsumeAudio(audio->p->sync);
165}
166
167int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
168 audio->nextEvent -= cycles;
169 audio->eventDiff += cycles;
170 while (audio->nextEvent <= 0) {
171 audio->nextEvent = INT_MAX;
172 if (audio->enable) {
173 if (audio->playingCh1 && !audio->ch1.envelope.dead) {
174 audio->nextCh1 -= audio->eventDiff;
175 if (audio->ch1.envelope.nextStep != INT_MAX) {
176 audio->ch1.envelope.nextStep -= audio->eventDiff;
177 if (audio->ch1.envelope.nextStep <= 0) {
178 int8_t sample = audio->ch1.control.hi * 0x10 - 0x8;
179 _updateEnvelope(&audio->ch1.envelope);
180 if (audio->ch1.envelope.nextStep < audio->nextEvent) {
181 audio->nextEvent = audio->ch1.envelope.nextStep;
182 }
183 audio->ch1.sample = sample * audio->ch1.envelope.currentVolume;
184 }
185 }
186
187 if (audio->ch1.nextSweep != INT_MAX) {
188 audio->ch1.nextSweep -= audio->eventDiff;
189 if (audio->ch1.nextSweep <= 0) {
190 audio->playingCh1 = _updateSweep(&audio->ch1);
191 if (audio->ch1.nextSweep < audio->nextEvent) {
192 audio->nextEvent = audio->ch1.nextSweep;
193 }
194 }
195 }
196
197 if (audio->nextCh1 <= 0) {
198 audio->nextCh1 += _updateChannel1(&audio->ch1);
199 if (audio->nextCh1 < audio->nextEvent) {
200 audio->nextEvent = audio->nextCh1;
201 }
202 }
203
204 if (audio->ch1.control.stop) {
205 audio->ch1.control.endTime -= audio->eventDiff;
206 if (audio->ch1.control.endTime <= 0) {
207 audio->playingCh1 = 0;
208 }
209 }
210 }
211
212 if (audio->playingCh2 && !audio->ch2.envelope.dead) {
213 audio->nextCh2 -= audio->eventDiff;
214 if (audio->ch2.envelope.nextStep != INT_MAX) {
215 audio->ch2.envelope.nextStep -= audio->eventDiff;
216 if (audio->ch2.envelope.nextStep <= 0) {
217 int8_t sample = audio->ch2.control.hi * 0x10 - 0x8;
218 _updateEnvelope(&audio->ch2.envelope);
219 if (audio->ch2.envelope.nextStep < audio->nextEvent) {
220 audio->nextEvent = audio->ch2.envelope.nextStep;
221 }
222 audio->ch2.sample = sample * audio->ch2.envelope.currentVolume;
223 }
224 }
225
226 if (audio->nextCh2 <= 0) {
227 audio->nextCh2 += _updateChannel2(&audio->ch2);
228 if (audio->nextCh2 < audio->nextEvent) {
229 audio->nextEvent = audio->nextCh2;
230 }
231 }
232
233 if (audio->ch2.control.stop) {
234 audio->ch2.control.endTime -= audio->eventDiff;
235 if (audio->ch2.control.endTime <= 0) {
236 audio->playingCh2 = 0;
237 }
238 }
239 }
240
241 if (audio->playingCh3) {
242 audio->nextCh3 -= audio->eventDiff;
243 if (audio->nextCh3 <= 0) {
244 audio->nextCh3 += _updateChannel3(&audio->ch3);
245 if (audio->nextCh3 < audio->nextEvent) {
246 audio->nextEvent = audio->nextCh3;
247 }
248 }
249
250 if (audio->ch3.control.stop) {
251 audio->ch3.control.endTime -= audio->eventDiff;
252 if (audio->ch3.control.endTime <= 0) {
253 audio->playingCh3 = 0;
254 }
255 }
256 }
257
258 if (audio->playingCh4 && !audio->ch4.envelope.dead) {
259 audio->nextCh4 -= audio->eventDiff;
260 if (audio->ch4.envelope.nextStep != INT_MAX) {
261 audio->ch4.envelope.nextStep -= audio->eventDiff;
262 if (audio->ch4.envelope.nextStep <= 0) {
263 int8_t sample = (audio->ch4.sample >> 31) * 0x8;
264 _updateEnvelope(&audio->ch4.envelope);
265 if (audio->ch4.envelope.nextStep < audio->nextEvent) {
266 audio->nextEvent = audio->ch4.envelope.nextStep;
267 }
268 audio->ch4.sample = sample * audio->ch4.envelope.currentVolume;
269 }
270 }
271
272 if (audio->nextCh4 <= 0) {
273 audio->nextCh4 += _updateChannel4(&audio->ch4);
274 if (audio->nextCh4 < audio->nextEvent) {
275 audio->nextEvent = audio->nextCh4;
276 }
277 }
278
279 if (audio->ch4.control.stop) {
280 audio->ch4.control.endTime -= audio->eventDiff;
281 if (audio->ch4.control.endTime <= 0) {
282 audio->playingCh4 = 0;
283 }
284 }
285 }
286 }
287
288 audio->nextSample -= audio->eventDiff;
289 if (audio->nextSample <= 0) {
290 _sample(audio);
291 audio->nextSample += audio->sampleInterval;
292 }
293
294 if (audio->nextSample < audio->nextEvent) {
295 audio->nextEvent = audio->nextSample;
296 }
297 audio->eventDiff = 0;
298 }
299 return audio->nextEvent;
300}
301
302void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info) {
303 switch (info->dest) {
304 case BASE_IO | REG_FIFO_A_LO:
305 audio->chA.dmaSource = number;
306 break;
307 case BASE_IO | REG_FIFO_B_LO:
308 audio->chB.dmaSource = number;
309 break;
310 default:
311 GBALog(audio->p, GBA_LOG_GAME_ERROR, "Invalid FIFO destination: 0x%08X", info->dest);
312 return;
313 }
314 info->reg = GBADMARegisterSetDestControl(info->reg, DMA_FIXED);
315}
316
317void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value) {
318 audio->ch1.sweep.shift = GBAAudioRegisterSquareSweepGetShift(value);
319 audio->ch1.sweep.direction = GBAAudioRegisterSquareSweepGetDirection(value);
320 audio->ch1.sweep.time = GBAAudioRegisterSquareSweepGetTime(value);
321 if (audio->ch1.sweep.time) {
322 audio->ch1.nextSweep = audio->ch1.sweep.time * SWEEP_CYCLES;
323 } else {
324 audio->ch1.nextSweep = INT_MAX;
325 }
326}
327
328void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value) {
329 if (!_writeEnvelope(&audio->ch1.envelope, value)) {
330 audio->ch1.sample = 0;
331 }
332}
333
334void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value) {
335 audio->ch1.control.frequency = GBAAudioRegisterControlGetFrequency(value);
336 audio->ch1.control.stop = GBAAudioRegisterControlGetStop(value);
337 audio->ch1.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch1.envelope.length)) >> 8;
338 if (GBAAudioRegisterControlIsRestart(value)) {
339 if (audio->ch1.sweep.time) {
340 audio->ch1.nextSweep = audio->ch1.sweep.time * SWEEP_CYCLES;
341 } else {
342 audio->ch1.nextSweep = INT_MAX;
343 }
344 if (!audio->playingCh1) {
345 audio->nextCh1 = 0;
346 }
347 audio->playingCh1 = 1;
348 audio->ch1.envelope.currentVolume = audio->ch1.envelope.initialVolume;
349 if (audio->ch1.envelope.currentVolume > 0) {
350 audio->ch1.envelope.dead = 0;
351 }
352 if (audio->ch1.envelope.stepTime) {
353 audio->ch1.envelope.nextStep = 0;
354 } else {
355 audio->ch1.envelope.nextStep = INT_MAX;
356 }
357 }
358}
359
360void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value) {
361 if (!_writeEnvelope(&audio->ch2.envelope, value)) {
362 audio->ch2.sample = 0;
363 }
364}
365
366void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value) {
367 audio->ch2.control.frequency = GBAAudioRegisterControlGetFrequency(value);
368 audio->ch2.control.stop = GBAAudioRegisterControlGetStop(value);
369 audio->ch2.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch2.envelope.length)) >> 8;
370 if (GBAAudioRegisterControlIsRestart(value)) {
371 audio->playingCh2 = 1;
372 audio->ch2.envelope.currentVolume = audio->ch2.envelope.initialVolume;
373 if (audio->ch2.envelope.currentVolume > 0) {
374 audio->ch2.envelope.dead = 0;
375 }
376 if (audio->ch2.envelope.stepTime) {
377 audio->ch2.envelope.nextStep = 0;
378 } else {
379 audio->ch2.envelope.nextStep = INT_MAX;
380 }
381 audio->nextCh2 = 0;
382 }
383}
384
385void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value) {
386 audio->ch3.bank.size = GBAAudioRegisterBankGetSize(value);
387 audio->ch3.bank.bank = GBAAudioRegisterBankGetBank(value);
388 audio->ch3.bank.enable = GBAAudioRegisterBankGetEnable(value);
389 if (audio->ch3.control.endTime >= 0) {
390 audio->playingCh3 = audio->ch3.bank.enable;
391 }
392}
393
394void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value) {
395 audio->ch3.wave.length = GBAAudioRegisterBankWaveGetLength(value);
396 audio->ch3.wave.volume = GBAAudioRegisterBankWaveGetVolume(value);
397}
398
399void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value) {
400 audio->ch3.control.rate = GBAAudioRegisterControlGetRate(value);
401 audio->ch3.control.stop = GBAAudioRegisterControlGetStop(value);
402 audio->ch3.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (256 - audio->ch3.wave.length)) >> 8;
403 if (GBAAudioRegisterControlIsRestart(value)) {
404 audio->playingCh3 = audio->ch3.bank.enable;
405 }
406}
407
408void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value) {
409 if (!_writeEnvelope(&audio->ch4.envelope, value)) {
410 audio->ch4.sample = 0;
411 }
412}
413
414void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value) {
415 audio->ch4.control.ratio = GBAAudioRegisterCh4ControlGetRatio(value);
416 audio->ch4.control.frequency = GBAAudioRegisterCh4ControlGetFrequency(value);
417 audio->ch4.control.power = GBAAudioRegisterCh4ControlGetPower(value);
418 audio->ch4.control.stop = GBAAudioRegisterCh4ControlGetStop(value);
419 audio->ch4.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch4.envelope.length)) >> 8;
420 if (GBAAudioRegisterCh4ControlIsRestart(value)) {
421 audio->playingCh4 = 1;
422 audio->ch4.envelope.currentVolume = audio->ch4.envelope.initialVolume;
423 if (audio->ch4.envelope.currentVolume > 0) {
424 audio->ch4.envelope.dead = 0;
425 }
426 if (audio->ch4.envelope.stepTime) {
427 audio->ch4.envelope.nextStep = 0;
428 } else {
429 audio->ch4.envelope.nextStep = INT_MAX;
430 }
431 if (audio->ch4.control.power) {
432 audio->ch4.lfsr = 0x40;
433 } else {
434 audio->ch4.lfsr = 0x4000;
435 }
436 audio->nextCh4 = 0;
437 }
438}
439
440void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value) {
441 audio->volumeRight = GBARegisterSOUNDCNT_LOGetVolumeRight(value);
442 audio->volumeLeft = GBARegisterSOUNDCNT_LOGetVolumeLeft(value);
443 audio->ch1Right = GBARegisterSOUNDCNT_LOGetCh1Right(value);
444 audio->ch2Right = GBARegisterSOUNDCNT_LOGetCh2Right(value);
445 audio->ch3Right = GBARegisterSOUNDCNT_LOGetCh3Right(value);
446 audio->ch4Right = GBARegisterSOUNDCNT_LOGetCh4Right(value);
447 audio->ch1Left = GBARegisterSOUNDCNT_LOGetCh1Left(value);
448 audio->ch2Left = GBARegisterSOUNDCNT_LOGetCh2Left(value);
449 audio->ch3Left = GBARegisterSOUNDCNT_LOGetCh3Left(value);
450 audio->ch4Left = GBARegisterSOUNDCNT_LOGetCh4Left(value);
451}
452
453void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value) {
454 audio->volume = GBARegisterSOUNDCNT_HIGetVolume(value);
455 audio->volumeChA = GBARegisterSOUNDCNT_HIGetVolumeChA(value);
456 audio->volumeChB = GBARegisterSOUNDCNT_HIGetVolumeChB(value);
457 audio->chARight = GBARegisterSOUNDCNT_HIGetChARight(value);
458 audio->chALeft = GBARegisterSOUNDCNT_HIGetChALeft(value);
459 audio->chATimer = GBARegisterSOUNDCNT_HIGetChATimer(value);
460 audio->chBRight = GBARegisterSOUNDCNT_HIGetChBRight(value);
461 audio->chBLeft = GBARegisterSOUNDCNT_HIGetChBLeft(value);
462 audio->chBTimer = GBARegisterSOUNDCNT_HIGetChBTimer(value);
463 if (GBARegisterSOUNDCNT_HIIsChAReset(value)) {
464 CircleBufferClear(&audio->chA.fifo);
465 }
466 if (GBARegisterSOUNDCNT_HIIsChBReset(value)) {
467 CircleBufferClear(&audio->chB.fifo);
468 }
469}
470
471void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value) {
472 audio->enable = GBARegisterSOUNDCNT_XGetEnable(value);
473}
474
475void GBAAudioWriteSOUNDBIAS(struct GBAAudio* audio, uint16_t value) {
476 audio->soundbias = value;
477}
478
479void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value) {
480 audio->ch3.wavedata[address | (!audio->ch3.bank.bank * 4)] = value;
481}
482
483void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value) {
484 struct CircleBuffer* fifo;
485 switch (address) {
486 case REG_FIFO_A_LO:
487 fifo = &audio->chA.fifo;
488 break;
489 case REG_FIFO_B_LO:
490 fifo = &audio->chB.fifo;
491 break;
492 default:
493 GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", address);
494 return;
495 }
496 int i;
497 for (i = 0; i < 4; ++i) {
498 while (!CircleBufferWrite8(fifo, value >> (8 * i))) {
499 int8_t dummy;
500 CircleBufferRead8(fifo, &dummy);
501 }
502 }
503}
504
505void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId, int32_t cycles) {
506 struct GBAAudioFIFO* channel;
507 if (fifoId == 0) {
508 channel = &audio->chA;
509 } else if (fifoId == 1) {
510 channel = &audio->chB;
511 } else {
512 GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", fifoId);
513 return;
514 }
515 if (CircleBufferSize(&channel->fifo) <= 4 * sizeof(int32_t) && channel->dmaSource > 0) {
516 struct GBADMA* dma = &audio->p->memory.dma[channel->dmaSource];
517 if (GBADMARegisterGetTiming(dma->reg) == DMA_TIMING_CUSTOM) {
518 dma->nextCount = 4;
519 dma->nextEvent = 0;
520 dma->reg = GBADMARegisterSetWidth(dma->reg, 1);
521 GBAMemoryUpdateDMAs(audio->p, -cycles);
522 } else {
523 channel->dmaSource = 0;
524 }
525 }
526 CircleBufferRead8(&channel->fifo, &channel->sample);
527}
528
529#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
530unsigned GBAAudioCopy(struct GBAAudio* audio, void* left, void* right, unsigned nSamples) {
531 GBASyncLockAudio(audio->p->sync);
532 unsigned read = 0;
533 if (left) {
534 unsigned readL = CircleBufferRead(&audio->left, left, nSamples * sizeof(int16_t)) >> 1;
535 if (readL < nSamples) {
536 memset((int16_t*) left + readL, 0, nSamples - readL);
537 }
538 read = readL;
539 }
540 if (right) {
541 unsigned readR = CircleBufferRead(&audio->right, right, nSamples * sizeof(int16_t)) >> 1;
542 if (readR < nSamples) {
543 memset((int16_t*) right + readR, 0, nSamples - readR);
544 }
545 read = read >= readR ? read : readR;
546 }
547 GBASyncConsumeAudio(audio->p->sync);
548 return read;
549}
550
551unsigned GBAAudioResampleNN(struct GBAAudio* audio, float ratio, float* drift, struct GBAStereoSample* output, unsigned nSamples) {
552 int16_t left[GBA_AUDIO_SAMPLES];
553 int16_t right[GBA_AUDIO_SAMPLES];
554
555 // toRead is in GBA samples
556 // TODO: Do this with fixed-point math
557 unsigned toRead = ceilf(nSamples / ratio);
558 unsigned totalRead = 0;
559 while (nSamples) {
560 unsigned currentRead = GBA_AUDIO_SAMPLES;
561 if (currentRead > toRead) {
562 currentRead = toRead;
563 }
564 unsigned read = GBAAudioCopy(audio, left, right, currentRead);
565 toRead -= read;
566 unsigned i;
567 for (i = 0; i < read; ++i) {
568 *drift += ratio;
569 while (*drift >= 1.f) {
570 output->left = left[i];
571 output->right = right[i];
572 ++output;
573 ++totalRead;
574 --nSamples;
575 *drift -= 1.f;
576 if (!nSamples) {
577 return totalRead;
578 }
579 }
580 }
581 if (read < currentRead) {
582 memset(output, 0, nSamples * sizeof(struct GBAStereoSample));
583 break;
584 }
585 }
586 return totalRead;
587}
588#endif
589
590bool _writeEnvelope(struct GBAAudioEnvelope* envelope, uint16_t value) {
591 envelope->length = GBAAudioRegisterEnvelopeGetLength(value);
592 envelope->duty = GBAAudioRegisterEnvelopeGetDuty(value);
593 envelope->stepTime = GBAAudioRegisterEnvelopeGetStepTime(value);
594 envelope->direction = GBAAudioRegisterEnvelopeGetDirection(value);
595 envelope->initialVolume = GBAAudioRegisterEnvelopeGetInitialVolume(value);
596 envelope->dead = 0;
597 if (envelope->stepTime) {
598 envelope->nextStep = 0;
599 } else {
600 envelope->nextStep = INT_MAX;
601 if (envelope->initialVolume == 0) {
602 envelope->dead = 1;
603 return false;
604 }
605 }
606 return true;
607}
608
609static int32_t _updateSquareChannel(struct GBAAudioSquareControl* control, int duty) {
610 control->hi = !control->hi;
611 int period = 16 * (2048 - control->frequency);
612 switch (duty) {
613 case 0:
614 return control->hi ? period : period * 7;
615 case 1:
616 return control->hi ? period * 2 : period * 6;
617 case 2:
618 return period * 4;
619 case 3:
620 return control->hi ? period * 6 : period * 2;
621 default:
622 // This should never be hit
623 return period * 4;
624 }
625}
626
627static void _updateEnvelope(struct GBAAudioEnvelope* envelope) {
628 if (envelope->direction) {
629 ++envelope->currentVolume;
630 } else {
631 --envelope->currentVolume;
632 }
633 if (envelope->currentVolume >= 15) {
634 envelope->currentVolume = 15;
635 envelope->nextStep = INT_MAX;
636 } else if (envelope->currentVolume <= 0) {
637 envelope->currentVolume = 0;
638 envelope->dead = 1;
639 envelope->nextStep = INT_MAX;
640 } else {
641 envelope->nextStep += envelope->stepTime * (GBA_ARM7TDMI_FREQUENCY >> 6);
642 }
643}
644
645static bool _updateSweep(struct GBAAudioChannel1* ch) {
646 if (ch->sweep.direction) {
647 int frequency = ch->control.frequency;
648 frequency -= frequency >> ch->sweep.shift;
649 if (frequency >= 0) {
650 ch->control.frequency = frequency;
651 }
652 } else {
653 int frequency = ch->control.frequency;
654 frequency += frequency >> ch->sweep.shift;
655 if (frequency < 2048) {
656 ch->control.frequency = frequency;
657 } else {
658 return false;
659 }
660 }
661 ch->nextSweep += ch->sweep.time * SWEEP_CYCLES;
662 return true;
663}
664
665static int32_t _updateChannel1(struct GBAAudioChannel1* ch) {
666 int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
667 ch->sample = ch->control.hi * 0x10 - 0x8;
668 ch->sample *= ch->envelope.currentVolume;
669 return timing;
670}
671
672static int32_t _updateChannel2(struct GBAAudioChannel2* ch) {
673 int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
674 ch->sample = ch->control.hi * 0x10 - 0x8;
675 ch->sample *= ch->envelope.currentVolume;
676 return timing;
677}
678
679static int32_t _updateChannel3(struct GBAAudioChannel3* ch) {
680 int i;
681 int start;
682 int end;
683 int volume;
684 switch (ch->wave.volume) {
685 case 0:
686 volume = 0;
687 break;
688 case 1:
689 volume = 4;
690 break;
691 case 2:
692 volume = 2;
693 break;
694 case 3:
695 volume = 1;
696 break;
697 default:
698 volume = 3;
699 break;
700 }
701 if (ch->bank.size) {
702 start = 7;
703 end = 0;
704 } else if (ch->bank.bank) {
705 start = 7;
706 end = 4;
707 } else {
708 start = 3;
709 end = 0;
710 }
711 uint32_t bitsCarry = ch->wavedata[end] & 0x000000F0;
712 uint32_t bits;
713 for (i = start; i >= end; --i) {
714 bits = ch->wavedata[i] & 0x000000F0;
715 ch->wavedata[i] = ((ch->wavedata[i] & 0x0F0F0F0F) << 4) | ((ch->wavedata[i] & 0xF0F0F000) >> 12);
716 ch->wavedata[i] |= bitsCarry << 20;
717 bitsCarry = bits;
718 }
719 ch->sample = bitsCarry >> 4;
720 ch->sample -= 8;
721 ch->sample *= volume * 4;
722 return 8 * (2048 - ch->control.rate);
723}
724
725static int32_t _updateChannel4(struct GBAAudioChannel4* ch) {
726 int lsb = ch->lfsr & 1;
727 ch->sample = lsb * 0x10 - 0x8;
728 ch->sample *= ch->envelope.currentVolume;
729 ch->lfsr >>= 1;
730 ch->lfsr ^= (lsb * 0x60) << (ch->control.power ? 0 : 8);
731 int timing = ch->control.ratio ? 2 * ch->control.ratio : 1;
732 timing <<= ch->control.frequency;
733 timing *= 32;
734 return timing;
735}
736
737static int _applyBias(struct GBAAudio* audio, int sample) {
738 sample += GBARegisterSOUNDBIASGetBias(audio->soundbias);
739 if (sample >= 0x400) {
740 sample = 0x3FF;
741 } else if (sample < 0) {
742 sample = 0;
743 }
744 return ((sample - GBARegisterSOUNDBIASGetBias(audio->soundbias)) * audio->masterVolume) >> 3;
745}
746
747static void _sample(struct GBAAudio* audio) {
748 int16_t sampleLeft = 0;
749 int16_t sampleRight = 0;
750 int psgShift = 5 - audio->volume;
751
752 if (audio->playingCh1 && !audio->forceDisableCh[0]) {
753 if (audio->ch1Left) {
754 sampleLeft += audio->ch1.sample;
755 }
756
757 if (audio->ch1Right) {
758 sampleRight += audio->ch1.sample;
759 }
760 }
761
762 if (audio->playingCh2 && !audio->forceDisableCh[1]) {
763 if (audio->ch2Left) {
764 sampleLeft += audio->ch2.sample;
765 }
766
767 if (audio->ch2Right) {
768 sampleRight += audio->ch2.sample;
769 }
770 }
771
772 if (audio->playingCh3 && !audio->forceDisableCh[2]) {
773 if (audio->ch3Left) {
774 sampleLeft += audio->ch3.sample;
775 }
776
777 if (audio->ch3Right) {
778 sampleRight += audio->ch3.sample;
779 }
780 }
781
782 if (audio->playingCh4 && !audio->forceDisableCh[3]) {
783 if (audio->ch4Left) {
784 sampleLeft += audio->ch4.sample;
785 }
786
787 if (audio->ch4Right) {
788 sampleRight += audio->ch4.sample;
789 }
790 }
791
792 sampleLeft = (sampleLeft * (1 + audio->volumeLeft)) >> psgShift;
793 sampleRight = (sampleRight * (1 + audio->volumeRight)) >> psgShift;
794
795 if (!audio->forceDisableChA) {
796 if (audio->chALeft) {
797 sampleLeft += (audio->chA.sample << 2) >> !audio->volumeChA;
798 }
799
800 if (audio->chARight) {
801 sampleRight += (audio->chA.sample << 2) >> !audio->volumeChA;
802 }
803 }
804
805 if (!audio->forceDisableChB) {
806 if (audio->chBLeft) {
807 sampleLeft += (audio->chB.sample << 2) >> !audio->volumeChB;
808 }
809
810 if (audio->chBRight) {
811 sampleRight += (audio->chB.sample << 2) >> !audio->volumeChB;
812 }
813 }
814
815 sampleLeft = _applyBias(audio, sampleLeft);
816 sampleRight = _applyBias(audio, sampleRight);
817
818 GBASyncLockAudio(audio->p->sync);
819 unsigned produced;
820#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
821 CircleBufferWrite16(&audio->left, sampleLeft);
822 CircleBufferWrite16(&audio->right, sampleRight);
823 produced = CircleBufferSize(&audio->left) / 2;
824#else
825 if ((size_t) blip_samples_avail(audio->left) < audio->samples) {
826 blip_add_delta(audio->left, audio->clock, sampleLeft - audio->lastLeft);
827 blip_add_delta(audio->right, audio->clock, sampleRight - audio->lastRight);
828 audio->lastLeft = sampleLeft;
829 audio->lastRight = sampleRight;
830 audio->clock += audio->sampleInterval;
831 if (audio->clock >= CLOCKS_PER_FRAME) {
832 blip_end_frame(audio->left, audio->clock);
833 blip_end_frame(audio->right, audio->clock);
834 audio->clock -= CLOCKS_PER_FRAME;
835 }
836 }
837 produced = blip_samples_avail(audio->left);
838#endif
839 if (audio->p->stream && audio->p->stream->postAudioFrame) {
840 audio->p->stream->postAudioFrame(audio->p->stream, sampleLeft, sampleRight);
841 }
842 bool wait = produced >= audio->samples;
843 GBASyncProduceAudio(audio->p->sync, wait);
844
845 if (wait && audio->p->stream && audio->p->stream->postAudioBuffer) {
846 audio->p->stream->postAudioBuffer(audio->p->stream, audio);
847 }
848}
849
850void GBAAudioSerialize(const struct GBAAudio* audio, struct GBASerializedState* state) {
851 state->audio.ch1Volume = audio->ch1.envelope.currentVolume;
852 state->audio.ch1Dead = audio->ch1.envelope.dead;
853 state->audio.ch1Hi = audio->ch1.control.hi;
854 state->audio.ch1.envelopeNextStep = audio->ch1.envelope.nextStep;
855 state->audio.ch1.waveNextStep = audio->ch1.control.nextStep;
856 state->audio.ch1.sweepNextStep = audio->ch1.nextSweep;
857 state->audio.ch1.endTime = audio->ch1.control.endTime;
858 state->audio.ch1.nextEvent = audio->nextCh1;
859
860 state->audio.ch2Volume = audio->ch2.envelope.currentVolume;
861 state->audio.ch2Dead = audio->ch2.envelope.dead;
862 state->audio.ch2Hi = audio->ch2.control.hi;
863 state->audio.ch2.envelopeNextStep = audio->ch2.envelope.nextStep;
864 state->audio.ch2.waveNextStep = audio->ch2.control.nextStep;
865 state->audio.ch2.endTime = audio->ch2.control.endTime;
866 state->audio.ch2.nextEvent = audio->nextCh2;
867
868 memcpy(state->audio.ch3.wavebanks, audio->ch3.wavedata, sizeof(state->audio.ch3.wavebanks));
869 state->audio.ch3.endTime = audio->ch3.control.endTime;
870 state->audio.ch3.nextEvent = audio->nextCh3;
871
872 state->audio.ch4Volume = audio->ch4.envelope.currentVolume;
873 state->audio.ch4Dead = audio->ch4.envelope.dead;
874 state->audio.ch4.envelopeNextStep = audio->ch4.envelope.nextStep;
875 state->audio.ch4.lfsr = audio->ch4.lfsr;
876 state->audio.ch4.endTime = audio->ch4.control.endTime;
877 state->audio.ch4.nextEvent = audio->nextCh4;
878
879 CircleBufferDump(&audio->chA.fifo, state->audio.fifoA, sizeof(state->audio.fifoA));
880 CircleBufferDump(&audio->chB.fifo, state->audio.fifoB, sizeof(state->audio.fifoB));
881 state->audio.fifoSize = CircleBufferSize(&audio->chA.fifo);
882
883 state->audio.nextEvent = audio->nextEvent;
884 state->audio.eventDiff = audio->eventDiff;
885 state->audio.nextSample = audio->nextSample;
886}
887
888void GBAAudioDeserialize(struct GBAAudio* audio, const struct GBASerializedState* state) {
889 audio->ch1.envelope.currentVolume = state->audio.ch1Volume;
890 audio->ch1.envelope.dead = state->audio.ch1Dead;
891 audio->ch1.control.hi = state->audio.ch1Hi;
892 audio->ch1.envelope.nextStep = state->audio.ch1.envelopeNextStep;
893 audio->ch1.control.nextStep = state->audio.ch1.waveNextStep;
894 audio->ch1.nextSweep = state->audio.ch1.sweepNextStep;
895 audio->ch1.control.endTime = state->audio.ch1.endTime;
896 audio->nextCh1 = state->audio.ch1.nextEvent;
897
898 audio->ch2.envelope.currentVolume = state->audio.ch2Volume;
899 audio->ch2.envelope.dead = state->audio.ch2Dead;
900 audio->ch2.control.hi = state->audio.ch2Hi;
901 audio->ch2.envelope.nextStep = state->audio.ch2.envelopeNextStep;
902 audio->ch2.control.nextStep = state->audio.ch2.waveNextStep;
903 audio->ch2.control.endTime = state->audio.ch2.endTime;
904 audio->nextCh2 = state->audio.ch2.nextEvent;
905
906 memcpy(audio->ch3.wavedata, state->audio.ch3.wavebanks, sizeof(audio->ch3.wavedata));
907 audio->ch3.control.endTime = state->audio.ch3.endTime;
908 audio->nextCh3 = state->audio.ch3.nextEvent;
909
910 audio->ch4.envelope.currentVolume = state->audio.ch4Volume;
911 audio->ch4.envelope.dead = state->audio.ch4Dead;
912 audio->ch4.envelope.nextStep = state->audio.ch4.envelopeNextStep;
913 audio->ch4.lfsr = state->audio.ch4.lfsr;
914 audio->ch4.control.endTime = state->audio.ch4.endTime;
915 audio->nextCh4 = state->audio.ch4.nextEvent;
916
917 CircleBufferClear(&audio->chA.fifo);
918 CircleBufferClear(&audio->chB.fifo);
919 size_t fifoSize = state->audio.fifoSize;
920 if (state->audio.fifoSize > CircleBufferCapacity(&audio->chA.fifo)) {
921 fifoSize = CircleBufferCapacity(&audio->chA.fifo);
922 }
923 size_t i;
924 for (i = 0; i < fifoSize; ++i) {
925 CircleBufferWrite8(&audio->chA.fifo, state->audio.fifoA[i]);
926 CircleBufferWrite8(&audio->chB.fifo, state->audio.fifoB[i]);
927 }
928
929 audio->nextEvent = state->audio.nextEvent;
930 audio->eventDiff = state->audio.eventDiff;
931 audio->nextSample = state->audio.nextSample;
932}
933
934float GBAAudioCalculateRatio(float inputSampleRate, float desiredFPS, float desiredSampleRate) {
935 return desiredSampleRate * GBA_ARM7TDMI_FREQUENCY / (VIDEO_TOTAL_LENGTH * desiredFPS * inputSampleRate);
936}