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