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 if (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 if (audio->ch1.envelope.stepTime) {
349 audio->ch1.envelope.nextStep = 0;
350 } else {
351 audio->ch1.envelope.nextStep = INT_MAX;
352 }
353 audio->ch1.envelope.currentVolume = audio->ch1.envelope.initialVolume;
354 if (audio->ch1.envelope.stepTime) {
355 audio->ch1.envelope.nextStep = 0;
356 } else {
357 audio->ch1.envelope.nextStep = INT_MAX;
358 }
359 }
360}
361
362void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value) {
363 if (!_writeEnvelope(&audio->ch2.envelope, value)) {
364 audio->ch2.sample = 0;
365 }
366}
367
368void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value) {
369 audio->ch2.control.frequency = GBAAudioRegisterControlGetFrequency(value);
370 audio->ch2.control.stop = GBAAudioRegisterControlGetStop(value);
371 audio->ch2.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch2.envelope.length)) >> 8;
372 if (GBAAudioRegisterControlIsRestart(value)) {
373 audio->playingCh2 = 1;
374 audio->ch2.envelope.currentVolume = audio->ch2.envelope.initialVolume;
375 if (audio->ch2.envelope.stepTime) {
376 audio->ch2.envelope.nextStep = 0;
377 } else {
378 audio->ch2.envelope.nextStep = INT_MAX;
379 }
380 audio->nextCh2 = 0;
381 }
382}
383
384void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value) {
385 audio->ch3.bank.size = GBAAudioRegisterBankGetSize(value);
386 audio->ch3.bank.bank = GBAAudioRegisterBankGetBank(value);
387 audio->ch3.bank.enable = GBAAudioRegisterBankGetEnable(value);
388 if (audio->ch3.control.endTime >= 0) {
389 audio->playingCh3 = audio->ch3.bank.enable;
390 }
391}
392
393void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value) {
394 audio->ch3.wave.length = GBAAudioRegisterBankWaveGetLength(value);
395 audio->ch3.wave.volume = GBAAudioRegisterBankWaveGetVolume(value);
396}
397
398void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value) {
399 audio->ch3.control.rate = GBAAudioRegisterControlGetRate(value);
400 audio->ch3.control.stop = GBAAudioRegisterControlGetStop(value);
401 audio->ch3.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (256 - audio->ch3.wave.length)) >> 8;
402 if (GBAAudioRegisterControlIsRestart(value)) {
403 audio->playingCh3 = audio->ch3.bank.enable;
404 }
405}
406
407void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value) {
408 if (!_writeEnvelope(&audio->ch4.envelope, value)) {
409 audio->ch4.sample = 0;
410 }
411}
412
413void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value) {
414 audio->ch4.control.ratio = GBAAudioRegisterCh4ControlGetRatio(value);
415 audio->ch4.control.frequency = GBAAudioRegisterCh4ControlGetFrequency(value);
416 audio->ch4.control.power = GBAAudioRegisterCh4ControlGetPower(value);
417 audio->ch4.control.stop = GBAAudioRegisterCh4ControlGetStop(value);
418 audio->ch4.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch4.envelope.length)) >> 8;
419 if (GBAAudioRegisterCh4ControlIsRestart(value)) {
420 audio->playingCh4 = 1;
421 audio->ch4.envelope.currentVolume = audio->ch4.envelope.initialVolume;
422 if (audio->ch4.envelope.stepTime) {
423 audio->ch4.envelope.nextStep = 0;
424 } else {
425 audio->ch4.envelope.nextStep = INT_MAX;
426 }
427 if (audio->ch4.control.power) {
428 audio->ch4.lfsr = 0x40;
429 } else {
430 audio->ch4.lfsr = 0x4000;
431 }
432 audio->nextCh4 = 0;
433 }
434}
435
436void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value) {
437 audio->volumeRight = GBARegisterSOUNDCNT_LOGetVolumeRight(value);
438 audio->volumeLeft = GBARegisterSOUNDCNT_LOGetVolumeLeft(value);
439 audio->ch1Right = GBARegisterSOUNDCNT_LOGetCh1Right(value);
440 audio->ch2Right = GBARegisterSOUNDCNT_LOGetCh2Right(value);
441 audio->ch3Right = GBARegisterSOUNDCNT_LOGetCh3Right(value);
442 audio->ch4Right = GBARegisterSOUNDCNT_LOGetCh4Right(value);
443 audio->ch1Left = GBARegisterSOUNDCNT_LOGetCh1Left(value);
444 audio->ch2Left = GBARegisterSOUNDCNT_LOGetCh2Left(value);
445 audio->ch3Left = GBARegisterSOUNDCNT_LOGetCh3Left(value);
446 audio->ch4Left = GBARegisterSOUNDCNT_LOGetCh4Left(value);
447}
448
449void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value) {
450 audio->volume = GBARegisterSOUNDCNT_HIGetVolume(value);
451 audio->volumeChA = GBARegisterSOUNDCNT_HIGetVolumeChA(value);
452 audio->volumeChB = GBARegisterSOUNDCNT_HIGetVolumeChB(value);
453 audio->chARight = GBARegisterSOUNDCNT_HIGetChARight(value);
454 audio->chALeft = GBARegisterSOUNDCNT_HIGetChALeft(value);
455 audio->chATimer = GBARegisterSOUNDCNT_HIGetChATimer(value);
456 audio->chBRight = GBARegisterSOUNDCNT_HIGetChBRight(value);
457 audio->chBLeft = GBARegisterSOUNDCNT_HIGetChBLeft(value);
458 audio->chBTimer = GBARegisterSOUNDCNT_HIGetChBTimer(value);
459 if (GBARegisterSOUNDCNT_HIIsChAReset(value)) {
460 CircleBufferClear(&audio->chA.fifo);
461 }
462 if (GBARegisterSOUNDCNT_HIIsChBReset(value)) {
463 CircleBufferClear(&audio->chB.fifo);
464 }
465}
466
467void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value) {
468 audio->enable = GBARegisterSOUNDCNT_XGetEnable(value);
469}
470
471void GBAAudioWriteSOUNDBIAS(struct GBAAudio* audio, uint16_t value) {
472 audio->soundbias = value;
473}
474
475void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value) {
476 audio->ch3.wavedata[address | (!audio->ch3.bank.bank * 4)] = value;
477}
478
479void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value) {
480 struct CircleBuffer* fifo;
481 switch (address) {
482 case REG_FIFO_A_LO:
483 fifo = &audio->chA.fifo;
484 break;
485 case REG_FIFO_B_LO:
486 fifo = &audio->chB.fifo;
487 break;
488 default:
489 GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", address);
490 return;
491 }
492 int i;
493 for (i = 0; i < 4; ++i) {
494 while (!CircleBufferWrite8(fifo, value >> (8 * i))) {
495 int8_t dummy;
496 CircleBufferRead8(fifo, &dummy);
497 }
498 }
499}
500
501void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId, int32_t cycles) {
502 struct GBAAudioFIFO* channel;
503 if (fifoId == 0) {
504 channel = &audio->chA;
505 } else if (fifoId == 1) {
506 channel = &audio->chB;
507 } else {
508 GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", fifoId);
509 return;
510 }
511 if (CircleBufferSize(&channel->fifo) <= 4 * sizeof(int32_t) && channel->dmaSource > 0) {
512 struct GBADMA* dma = &audio->p->memory.dma[channel->dmaSource];
513 if (GBADMARegisterGetTiming(dma->reg) == DMA_TIMING_CUSTOM) {
514 dma->nextCount = 4;
515 dma->nextEvent = 0;
516 dma->reg = GBADMARegisterSetWidth(dma->reg, 1);
517 GBAMemoryUpdateDMAs(audio->p, -cycles);
518 } else {
519 channel->dmaSource = 0;
520 }
521 }
522 CircleBufferRead8(&channel->fifo, &channel->sample);
523}
524
525#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
526unsigned GBAAudioCopy(struct GBAAudio* audio, void* left, void* right, unsigned nSamples) {
527 GBASyncLockAudio(audio->p->sync);
528 unsigned read = 0;
529 if (left) {
530 unsigned readL = CircleBufferRead(&audio->left, left, nSamples * sizeof(int16_t)) >> 1;
531 if (readL < nSamples) {
532 memset((int16_t*) left + readL, 0, nSamples - readL);
533 }
534 read = readL;
535 }
536 if (right) {
537 unsigned readR = CircleBufferRead(&audio->right, right, nSamples * sizeof(int16_t)) >> 1;
538 if (readR < nSamples) {
539 memset((int16_t*) right + readR, 0, nSamples - readR);
540 }
541 read = read >= readR ? read : readR;
542 }
543 GBASyncConsumeAudio(audio->p->sync);
544 return read;
545}
546
547unsigned GBAAudioResampleNN(struct GBAAudio* audio, float ratio, float* drift, struct GBAStereoSample* output, unsigned nSamples) {
548 int16_t left[GBA_AUDIO_SAMPLES];
549 int16_t right[GBA_AUDIO_SAMPLES];
550
551 // toRead is in GBA samples
552 // TODO: Do this with fixed-point math
553 unsigned toRead = ceilf(nSamples / ratio);
554 unsigned totalRead = 0;
555 while (nSamples) {
556 unsigned currentRead = GBA_AUDIO_SAMPLES;
557 if (currentRead > toRead) {
558 currentRead = toRead;
559 }
560 unsigned read = GBAAudioCopy(audio, left, right, currentRead);
561 toRead -= read;
562 unsigned i;
563 for (i = 0; i < read; ++i) {
564 *drift += ratio;
565 while (*drift >= 1.f) {
566 output->left = left[i];
567 output->right = right[i];
568 ++output;
569 ++totalRead;
570 --nSamples;
571 *drift -= 1.f;
572 if (!nSamples) {
573 return totalRead;
574 }
575 }
576 }
577 if (read < currentRead) {
578 memset(output, 0, nSamples * sizeof(struct GBAStereoSample));
579 break;
580 }
581 }
582 return totalRead;
583}
584#endif
585
586bool _writeEnvelope(struct GBAAudioEnvelope* envelope, uint16_t value) {
587 envelope->length = GBAAudioRegisterEnvelopeGetLength(value);
588 envelope->duty = GBAAudioRegisterEnvelopeGetDuty(value);
589 envelope->stepTime = GBAAudioRegisterEnvelopeGetStepTime(value);
590 envelope->direction = GBAAudioRegisterEnvelopeGetDirection(value);
591 envelope->initialVolume = GBAAudioRegisterEnvelopeGetInitialVolume(value);
592 envelope->dead = 0;
593 if (envelope->stepTime) {
594 envelope->nextStep = 0;
595 } else {
596 envelope->nextStep = INT_MAX;
597 if (envelope->initialVolume == 0) {
598 envelope->dead = 1;
599 return false;
600 }
601 }
602 return true;
603}
604
605static int32_t _updateSquareChannel(struct GBAAudioSquareControl* control, int duty) {
606 control->hi = !control->hi;
607 int period = 16 * (2048 - control->frequency);
608 switch (duty) {
609 case 0:
610 return control->hi ? period : period * 7;
611 case 1:
612 return control->hi ? period * 2 : period * 6;
613 case 2:
614 return period * 4;
615 case 3:
616 return control->hi ? period * 6 : period * 2;
617 default:
618 // This should never be hit
619 return period * 4;
620 }
621}
622
623static void _updateEnvelope(struct GBAAudioEnvelope* envelope) {
624 if (envelope->direction) {
625 ++envelope->currentVolume;
626 } else {
627 --envelope->currentVolume;
628 }
629 if (envelope->currentVolume >= 15) {
630 envelope->currentVolume = 15;
631 envelope->nextStep = INT_MAX;
632 } else if (envelope->currentVolume <= 0) {
633 envelope->currentVolume = 0;
634 envelope->dead = 1;
635 envelope->nextStep = INT_MAX;
636 } else {
637 envelope->nextStep += envelope->stepTime * (GBA_ARM7TDMI_FREQUENCY >> 6);
638 }
639}
640
641static bool _updateSweep(struct GBAAudioChannel1* ch) {
642 if (ch->sweep.direction) {
643 int frequency = ch->control.frequency;
644 frequency -= frequency >> ch->sweep.shift;
645 if (frequency >= 0) {
646 ch->control.frequency = frequency;
647 }
648 } else {
649 int frequency = ch->control.frequency;
650 frequency += frequency >> ch->sweep.shift;
651 if (frequency < 2048) {
652 ch->control.frequency = frequency;
653 } else {
654 return false;
655 }
656 }
657 ch->nextSweep += ch->sweep.time * SWEEP_CYCLES;
658 return true;
659}
660
661static int32_t _updateChannel1(struct GBAAudioChannel1* ch) {
662 int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
663 ch->sample = ch->control.hi * 0x10 - 0x8;
664 ch->sample *= ch->envelope.currentVolume;
665 return timing;
666}
667
668static int32_t _updateChannel2(struct GBAAudioChannel2* ch) {
669 int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
670 ch->sample = ch->control.hi * 0x10 - 0x8;
671 ch->sample *= ch->envelope.currentVolume;
672 return timing;
673}
674
675static int32_t _updateChannel3(struct GBAAudioChannel3* ch) {
676 int i;
677 int start;
678 int end;
679 int volume;
680 switch (ch->wave.volume) {
681 case 0:
682 volume = 0;
683 break;
684 case 1:
685 volume = 4;
686 break;
687 case 2:
688 volume = 2;
689 break;
690 case 3:
691 volume = 1;
692 break;
693 default:
694 volume = 3;
695 break;
696 }
697 if (ch->bank.size) {
698 start = 7;
699 end = 0;
700 } else if (ch->bank.bank) {
701 start = 7;
702 end = 4;
703 } else {
704 start = 3;
705 end = 0;
706 }
707 uint32_t bitsCarry = ch->wavedata[end] & 0x000000F0;
708 uint32_t bits;
709 for (i = start; i >= end; --i) {
710 bits = ch->wavedata[i] & 0x000000F0;
711 ch->wavedata[i] = ((ch->wavedata[i] & 0x0F0F0F0F) << 4) | ((ch->wavedata[i] & 0xF0F0F000) >> 12);
712 ch->wavedata[i] |= bitsCarry << 20;
713 bitsCarry = bits;
714 }
715 ch->sample = bitsCarry >> 4;
716 ch->sample -= 8;
717 ch->sample *= volume * 4;
718 return 8 * (2048 - ch->control.rate);
719}
720
721static int32_t _updateChannel4(struct GBAAudioChannel4* ch) {
722 int lsb = ch->lfsr & 1;
723 ch->sample = lsb * 0x10 - 0x8;
724 ch->sample *= ch->envelope.currentVolume;
725 ch->lfsr >>= 1;
726 ch->lfsr ^= (lsb * 0x60) << (ch->control.power ? 0 : 8);
727 int timing = ch->control.ratio ? 2 * ch->control.ratio : 1;
728 timing <<= ch->control.frequency;
729 timing *= 32;
730 return timing;
731}
732
733static int _applyBias(struct GBAAudio* audio, int sample) {
734 sample += GBARegisterSOUNDBIASGetBias(audio->soundbias);
735 if (sample >= 0x400) {
736 sample = 0x3FF;
737 } else if (sample < 0) {
738 sample = 0;
739 }
740 return ((sample - GBARegisterSOUNDBIASGetBias(audio->soundbias)) * audio->masterVolume) >> 3;
741}
742
743static void _sample(struct GBAAudio* audio) {
744 int16_t sampleLeft = 0;
745 int16_t sampleRight = 0;
746 int psgShift = 5 - audio->volume;
747
748 if (audio->playingCh1 && !audio->forceDisableCh[0]) {
749 if (audio->ch1Left) {
750 sampleLeft += audio->ch1.sample;
751 }
752
753 if (audio->ch1Right) {
754 sampleRight += audio->ch1.sample;
755 }
756 }
757
758 if (audio->playingCh2 && !audio->forceDisableCh[1]) {
759 if (audio->ch2Left) {
760 sampleLeft += audio->ch2.sample;
761 }
762
763 if (audio->ch2Right) {
764 sampleRight += audio->ch2.sample;
765 }
766 }
767
768 if (audio->playingCh3 && !audio->forceDisableCh[2]) {
769 if (audio->ch3Left) {
770 sampleLeft += audio->ch3.sample;
771 }
772
773 if (audio->ch3Right) {
774 sampleRight += audio->ch3.sample;
775 }
776 }
777
778 if (audio->playingCh4 && !audio->forceDisableCh[3]) {
779 if (audio->ch4Left) {
780 sampleLeft += audio->ch4.sample;
781 }
782
783 if (audio->ch4Right) {
784 sampleRight += audio->ch4.sample;
785 }
786 }
787
788 sampleLeft = (sampleLeft * (1 + audio->volumeLeft)) >> psgShift;
789 sampleRight = (sampleRight * (1 + audio->volumeRight)) >> psgShift;
790
791 if (!audio->forceDisableChA) {
792 if (audio->chALeft) {
793 sampleLeft += (audio->chA.sample << 2) >> !audio->volumeChA;
794 }
795
796 if (audio->chARight) {
797 sampleRight += (audio->chA.sample << 2) >> !audio->volumeChA;
798 }
799 }
800
801 if (!audio->forceDisableChB) {
802 if (audio->chBLeft) {
803 sampleLeft += (audio->chB.sample << 2) >> !audio->volumeChB;
804 }
805
806 if (audio->chBRight) {
807 sampleRight += (audio->chB.sample << 2) >> !audio->volumeChB;
808 }
809 }
810
811 sampleLeft = _applyBias(audio, sampleLeft);
812 sampleRight = _applyBias(audio, sampleRight);
813
814 GBASyncLockAudio(audio->p->sync);
815 unsigned produced;
816#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
817 CircleBufferWrite16(&audio->left, sampleLeft);
818 CircleBufferWrite16(&audio->right, sampleRight);
819 produced = CircleBufferSize(&audio->left) / 2;
820#else
821 if ((size_t) blip_samples_avail(audio->left) < audio->samples) {
822 blip_add_delta(audio->left, audio->clock, sampleLeft - audio->lastLeft);
823 blip_add_delta(audio->right, audio->clock, sampleRight - audio->lastRight);
824 audio->lastLeft = sampleLeft;
825 audio->lastRight = sampleRight;
826 audio->clock += audio->sampleInterval;
827 if (audio->clock >= CLOCKS_PER_FRAME) {
828 blip_end_frame(audio->left, audio->clock);
829 blip_end_frame(audio->right, audio->clock);
830 audio->clock -= CLOCKS_PER_FRAME;
831 }
832 }
833 produced = blip_samples_avail(audio->left);
834#endif
835 if (audio->p->stream && audio->p->stream->postAudioFrame) {
836 audio->p->stream->postAudioFrame(audio->p->stream, sampleLeft, sampleRight);
837 }
838 bool wait = produced >= audio->samples;
839 GBASyncProduceAudio(audio->p->sync, wait);
840
841 if (wait && audio->p->stream && audio->p->stream->postAudioBuffer) {
842 audio->p->stream->postAudioBuffer(audio->p->stream, audio);
843 }
844}
845
846void GBAAudioSerialize(const struct GBAAudio* audio, struct GBASerializedState* state) {
847 state->audio.ch1Volume = audio->ch1.envelope.currentVolume;
848 state->audio.ch1Dead = audio->ch1.envelope.dead;
849 state->audio.ch1Hi = audio->ch1.control.hi;
850 state->audio.ch1.envelopeNextStep = audio->ch1.envelope.nextStep;
851 state->audio.ch1.waveNextStep = audio->ch1.control.nextStep;
852 state->audio.ch1.sweepNextStep = audio->ch1.nextSweep;
853 state->audio.ch1.endTime = audio->ch1.control.endTime;
854 state->audio.ch1.nextEvent = audio->nextCh1;
855
856 state->audio.ch2Volume = audio->ch2.envelope.currentVolume;
857 state->audio.ch2Dead = audio->ch2.envelope.dead;
858 state->audio.ch2Hi = audio->ch2.control.hi;
859 state->audio.ch2.envelopeNextStep = audio->ch2.envelope.nextStep;
860 state->audio.ch2.waveNextStep = audio->ch2.control.nextStep;
861 state->audio.ch2.endTime = audio->ch2.control.endTime;
862 state->audio.ch2.nextEvent = audio->nextCh2;
863
864 memcpy(state->audio.ch3.wavebanks, audio->ch3.wavedata, sizeof(state->audio.ch3.wavebanks));
865 state->audio.ch3.endTime = audio->ch3.control.endTime;
866 state->audio.ch3.nextEvent = audio->nextCh3;
867
868 state->audio.ch4Volume = audio->ch4.envelope.currentVolume;
869 state->audio.ch4Dead = audio->ch4.envelope.dead;
870 state->audio.ch4.envelopeNextStep = audio->ch4.envelope.nextStep;
871 state->audio.ch4.lfsr = audio->ch4.lfsr;
872 state->audio.ch4.endTime = audio->ch4.control.endTime;
873 state->audio.ch4.nextEvent = audio->nextCh4;
874
875 CircleBufferDump(&audio->chA.fifo, state->audio.fifoA, sizeof(state->audio.fifoA));
876 CircleBufferDump(&audio->chB.fifo, state->audio.fifoB, sizeof(state->audio.fifoB));
877 state->audio.fifoSize = CircleBufferSize(&audio->chA.fifo);
878
879 state->audio.nextEvent = audio->nextEvent;
880 state->audio.eventDiff = audio->eventDiff;
881 state->audio.nextSample = audio->nextSample;
882}
883
884void GBAAudioDeserialize(struct GBAAudio* audio, const struct GBASerializedState* state) {
885 audio->ch1.envelope.currentVolume = state->audio.ch1Volume;
886 audio->ch1.envelope.dead = state->audio.ch1Dead;
887 audio->ch1.control.hi = state->audio.ch1Hi;
888 audio->ch1.envelope.nextStep = state->audio.ch1.envelopeNextStep;
889 audio->ch1.control.nextStep = state->audio.ch1.waveNextStep;
890 audio->ch1.nextSweep = state->audio.ch1.sweepNextStep;
891 audio->ch1.control.endTime = state->audio.ch1.endTime;
892 audio->nextCh1 = state->audio.ch1.nextEvent;
893
894 audio->ch2.envelope.currentVolume = state->audio.ch2Volume;
895 audio->ch2.envelope.dead = state->audio.ch2Dead;
896 audio->ch2.control.hi = state->audio.ch2Hi;
897 audio->ch2.envelope.nextStep = state->audio.ch2.envelopeNextStep;
898 audio->ch2.control.nextStep = state->audio.ch2.waveNextStep;
899 audio->ch2.control.endTime = state->audio.ch2.endTime;
900 audio->nextCh2 = state->audio.ch2.nextEvent;
901
902 memcpy(audio->ch3.wavedata, state->audio.ch3.wavebanks, sizeof(audio->ch3.wavedata));
903 audio->ch3.control.endTime = state->audio.ch3.endTime;
904 audio->nextCh3 = state->audio.ch3.nextEvent;
905
906 audio->ch4.envelope.currentVolume = state->audio.ch4Volume;
907 audio->ch4.envelope.dead = state->audio.ch4Dead;
908 audio->ch4.envelope.nextStep = state->audio.ch4.envelopeNextStep;
909 audio->ch4.lfsr = state->audio.ch4.lfsr;
910 audio->ch4.control.endTime = state->audio.ch4.endTime;
911 audio->nextCh4 = state->audio.ch4.nextEvent;
912
913 CircleBufferClear(&audio->chA.fifo);
914 CircleBufferClear(&audio->chB.fifo);
915 size_t fifoSize = state->audio.fifoSize;
916 if (state->audio.fifoSize > CircleBufferCapacity(&audio->chA.fifo)) {
917 fifoSize = CircleBufferCapacity(&audio->chA.fifo);
918 }
919 size_t i;
920 for (i = 0; i < fifoSize; ++i) {
921 CircleBufferWrite8(&audio->chA.fifo, state->audio.fifoA[i]);
922 CircleBufferWrite8(&audio->chB.fifo, state->audio.fifoB[i]);
923 }
924
925 audio->nextEvent = state->audio.nextEvent;
926 audio->eventDiff = state->audio.eventDiff;
927 audio->nextSample = state->audio.nextSample;
928}
929
930float GBAAudioCalculateRatio(float inputSampleRate, float desiredFPS, float desiredSampleRate) {
931 return desiredSampleRate * GBA_ARM7TDMI_FREQUENCY / (VIDEO_TOTAL_LENGTH * desiredFPS * inputSampleRate);
932}