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