src/gba/gba-audio.c (view raw)
1#include "gba-audio.h"
2
3#include "gba.h"
4#include "gba-io.h"
5#include "gba-thread.h"
6
7#include <limits.h>
8
9const unsigned GBA_AUDIO_SAMPLES = 512;
10const unsigned GBA_AUDIO_FIFO_SIZE = 8 * sizeof(int32_t);
11#define SWEEP_CYCLES (GBA_ARM7TDMI_FREQUENCY / 128)
12
13static int32_t _updateSquareChannel(struct GBAAudioSquareControl* envelope, int duty);
14static void _updateEnvelope(struct GBAAudioEnvelope* envelope);
15static int _updateSweep(struct GBAAudioChannel1* ch);
16static int32_t _updateChannel1(struct GBAAudioChannel1* ch);
17static int32_t _updateChannel2(struct GBAAudioChannel2* ch);
18static int32_t _updateChannel3(struct GBAAudioChannel3* ch);
19static int32_t _updateChannel4(struct GBAAudioChannel4* ch);
20static void _sample(struct GBAAudio* audio);
21
22void GBAAudioInit(struct GBAAudio* audio) {
23 audio->nextEvent = 0;
24 audio->nextCh1 = 0;
25 audio->nextCh2 = 0;
26 audio->nextCh3 = 0;
27 audio->nextCh4 = 0;
28 audio->ch1.envelope.nextStep = INT_MAX;
29 audio->ch1.control.nextStep = 0;
30 audio->ch1.nextSweep = INT_MAX;
31 audio->ch1.sample = 0;
32 audio->ch2.envelope.nextStep = INT_MAX;
33 audio->ch2.control.nextStep = 0;
34 audio->ch2.sample = 0;
35 audio->ch3.bank.packed = 0;
36 audio->ch3.sample = 0;
37 audio->ch4.sample = 0;
38 audio->ch4.envelope.nextStep = INT_MAX;
39 audio->eventDiff = 0;
40 audio->nextSample = 0;
41 audio->sampleRate = 0x8000;
42 audio->soundcntLo = 0;
43 audio->soundcntHi = 0;
44 audio->soundcntX = 0;
45 audio->sampleInterval = GBA_ARM7TDMI_FREQUENCY / audio->sampleRate;
46
47 CircleBufferInit(&audio->left, GBA_AUDIO_SAMPLES * sizeof(int32_t));
48 CircleBufferInit(&audio->right, GBA_AUDIO_SAMPLES * sizeof(int32_t));
49 CircleBufferInit(&audio->chA.fifo, GBA_AUDIO_FIFO_SIZE);
50 CircleBufferInit(&audio->chB.fifo, GBA_AUDIO_FIFO_SIZE);
51
52 pthread_mutex_init(&audio->bufferMutex, 0);
53}
54
55void GBAAudioDeinit(struct GBAAudio* audio) {
56 CircleBufferDeinit(&audio->left);
57 CircleBufferDeinit(&audio->right);
58 CircleBufferDeinit(&audio->chA.fifo);
59 CircleBufferDeinit(&audio->chB.fifo);
60
61 pthread_mutex_lock(&audio->bufferMutex);
62 pthread_mutex_destroy(&audio->bufferMutex);
63}
64
65int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
66 audio->nextEvent -= cycles;
67 audio->eventDiff += cycles;
68 while (audio->nextEvent <= 0) {
69 audio->nextEvent = INT_MAX;
70 if (audio->enable) {
71 if (audio->playingCh1 && !audio->ch1.envelope.dead) {
72 audio->nextCh1 -= audio->eventDiff;
73 if (audio->ch1.envelope.nextStep != INT_MAX) {
74 audio->ch1.envelope.nextStep -= audio->eventDiff;
75 if (audio->ch1.envelope.nextStep <= 0) {
76 int8_t sample = audio->ch1.control.hi * 0x10 - 0x8;
77 _updateEnvelope(&audio->ch1.envelope);
78 if (audio->ch1.envelope.nextStep < audio->nextEvent) {
79 audio->nextEvent = audio->ch1.envelope.nextStep;
80 }
81 audio->ch1.sample = sample * audio->ch1.envelope.currentVolume;
82 }
83 }
84
85 if (audio->ch1.nextSweep != INT_MAX) {
86 audio->ch1.nextSweep -= audio->eventDiff;
87 if (audio->ch1.nextSweep <= 0) {
88 audio->playingCh1 = _updateSweep(&audio->ch1);
89 if (audio->ch1.nextSweep < audio->nextEvent) {
90 audio->nextEvent = audio->ch1.nextSweep;
91 }
92 }
93 }
94
95 if (audio->nextCh1 <= 0) {
96 audio->nextCh1 += _updateChannel1(&audio->ch1);
97 if (audio->nextCh1 < audio->nextEvent) {
98 audio->nextEvent = audio->nextCh1;
99 }
100 }
101
102 if (audio->ch1.control.stop) {
103 audio->ch1.control.endTime -= audio->eventDiff;
104 if (audio->ch1.control.endTime <= 0) {
105 audio->playingCh1 = 0;
106 }
107 }
108 }
109
110 if (audio->playingCh2 && !audio->ch2.envelope.dead) {
111 audio->nextCh2 -= audio->eventDiff;
112 if (audio->ch2.envelope.nextStep != INT_MAX) {
113 audio->ch2.envelope.nextStep -= audio->eventDiff;
114 if (audio->ch2.envelope.nextStep <= 0) {
115 int8_t sample = audio->ch2.control.hi * 0x10 - 0x8;
116 _updateEnvelope(&audio->ch2.envelope);
117 if (audio->ch2.envelope.nextStep < audio->nextEvent) {
118 audio->nextEvent = audio->ch2.envelope.nextStep;
119 }
120 audio->ch2.sample = sample * audio->ch2.envelope.currentVolume;
121 }
122 }
123
124 if (audio->nextCh2 <= 0) {
125 audio->nextCh2 += _updateChannel2(&audio->ch2);
126 if (audio->nextCh2 < audio->nextEvent) {
127 audio->nextEvent = audio->nextCh2;
128 }
129 }
130
131 if (audio->ch2.control.stop) {
132 audio->ch2.control.endTime -= audio->eventDiff;
133 if (audio->ch2.control.endTime <= 0) {
134 audio->playingCh2 = 0;
135 }
136 }
137 }
138
139 if (audio->playingCh3) {
140 audio->nextCh3 -= audio->eventDiff;
141 if (audio->nextCh3 <= 0) {
142 audio->nextCh3 += _updateChannel3(&audio->ch3);
143 if (audio->nextCh3 < audio->nextEvent) {
144 audio->nextEvent = audio->nextCh3;
145 }
146 }
147
148 if (audio->ch3.control.stop) {
149 audio->ch3.control.endTime -= audio->eventDiff;
150 if (audio->ch3.control.endTime <= 0) {
151 audio->playingCh3 = 0;
152 }
153 }
154 }
155
156 if (audio->playingCh4 && !audio->ch4.envelope.dead) {
157 audio->nextCh4 -= audio->eventDiff;
158 if (audio->ch4.envelope.nextStep != INT_MAX) {
159 audio->ch4.envelope.nextStep -= audio->eventDiff;
160 if (audio->ch4.envelope.nextStep <= 0) {
161 int8_t sample = (audio->ch4.sample >> 31) * 0x8;
162 _updateEnvelope(&audio->ch4.envelope);
163 if (audio->ch4.envelope.nextStep < audio->nextEvent) {
164 audio->nextEvent = audio->ch4.envelope.nextStep;
165 }
166 audio->ch4.sample = sample * audio->ch4.envelope.currentVolume;
167 }
168 }
169
170 if (audio->nextCh4 <= 0) {
171 audio->nextCh4 += _updateChannel4(&audio->ch4);
172 if (audio->nextCh4 < audio->nextEvent) {
173 audio->nextEvent = audio->nextCh4;
174 }
175 }
176
177 if (audio->ch4.control.stop) {
178 audio->ch4.control.endTime -= audio->eventDiff;
179 if (audio->ch4.control.endTime <= 0) {
180 audio->playingCh4 = 0;
181 }
182 }
183 }
184 }
185
186 audio->nextSample -= audio->eventDiff;
187 if (audio->nextSample <= 0) {
188 _sample(audio);
189 audio->nextSample += audio->sampleInterval;
190 }
191
192 if (audio->nextSample < audio->nextEvent) {
193 audio->nextEvent = audio->nextSample;
194 }
195 audio->eventDiff = 0;
196 }
197 return audio->nextEvent;
198}
199
200void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info) {
201 switch (info->dest) {
202 case BASE_IO | REG_FIFO_A_LO:
203 audio->chA.dmaSource = number;
204 break;
205 case BASE_IO | REG_FIFO_B_LO:
206 audio->chB.dmaSource = number;
207 break;
208 default:
209 GBALog(audio->p, GBA_LOG_GAME_ERROR, "Invalid FIFO destination: 0x%08X", info->dest);
210 return;
211 }
212 info->dstControl = DMA_FIXED;
213}
214
215void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value) {
216 audio->ch1.sweep.packed = value;
217 if (audio->ch1.sweep.time) {
218 audio->ch1.nextSweep = audio->ch1.sweep.time * SWEEP_CYCLES;
219 } else {
220 audio->ch1.nextSweep = INT_MAX;
221 }
222}
223
224void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value) {
225 audio->ch1.envelope.packed = value;
226 audio->ch1.envelope.dead = 0;
227 if (audio->ch1.envelope.stepTime) {
228 audio->ch1.envelope.nextStep = 0;
229 } else {
230 audio->ch1.envelope.nextStep = INT_MAX;
231 if (audio->ch1.envelope.initialVolume == 0) {
232 audio->ch1.envelope.dead = 1;
233 audio->ch1.sample = 0;
234 }
235 }
236}
237
238void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value) {
239 audio->ch1.control.packed = value;
240 audio->ch1.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch1.envelope.length)) >> 8;
241 if (audio->ch1.control.restart) {
242 if (audio->ch1.sweep.time) {
243 audio->ch1.nextSweep = audio->ch1.sweep.time * SWEEP_CYCLES;
244 } else {
245 audio->ch1.nextSweep = INT_MAX;
246 }
247 if (!audio->playingCh1) {
248 audio->nextCh1 = 0;
249 }
250 audio->playingCh1 = 1;
251 if (audio->ch1.envelope.stepTime) {
252 audio->ch1.envelope.nextStep = 0;
253 } else {
254 audio->ch1.envelope.nextStep = INT_MAX;
255 }
256 audio->ch1.envelope.currentVolume = audio->ch1.envelope.initialVolume;
257 if (audio->ch1.envelope.stepTime) {
258 audio->ch1.envelope.nextStep = 0;
259 } else {
260 audio->ch1.envelope.nextStep = INT_MAX;
261 }
262 }
263}
264
265void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value) {
266 audio->ch2.envelope.packed = value;
267 audio->ch2.envelope.dead = 0;
268 if (audio->ch2.envelope.stepTime) {
269 audio->ch2.envelope.nextStep = 0;
270 } else {
271 audio->ch2.envelope.nextStep = INT_MAX;
272 if (audio->ch2.envelope.initialVolume == 0) {
273 audio->ch2.envelope.dead = 1;
274 audio->ch2.sample = 0;
275 }
276 }
277}
278
279void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value) {
280 audio->ch2.control.packed = value;
281 audio->ch1.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch2.envelope.length)) >> 8;
282 if (audio->ch2.control.restart) {
283 audio->playingCh2 = 1;
284 audio->ch2.envelope.currentVolume = audio->ch2.envelope.initialVolume;
285 if (audio->ch2.envelope.stepTime) {
286 audio->ch2.envelope.nextStep = 0;
287 } else {
288 audio->ch2.envelope.nextStep = INT_MAX;
289 }
290 audio->nextCh2 = 0;
291 }
292}
293
294void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value) {
295 audio->ch3.bank.packed = value;
296 if (audio->ch3.control.endTime >= 0) {
297 audio->playingCh3 = audio->ch3.bank.enable;
298 }
299}
300
301void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value) {
302 audio->ch3.wave.packed = value;
303}
304
305void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value) {
306 audio->ch3.control.packed = value;
307 audio->ch3.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (256 - audio->ch3.wave.length)) >> 8;
308 if (audio->ch3.control.restart) {
309 audio->playingCh3 = audio->ch3.bank.enable;
310 }
311}
312
313void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value) {
314 audio->ch4.envelope.packed = value;
315 audio->ch4.envelope.dead = 0;
316 if (audio->ch4.envelope.stepTime) {
317 audio->ch4.envelope.nextStep = 0;
318 } else {
319 audio->ch4.envelope.nextStep = INT_MAX;
320 if (audio->ch4.envelope.initialVolume == 0) {
321 audio->ch4.envelope.dead = 1;
322 audio->ch4.sample = 0;
323 }
324 }
325}
326
327void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value) {
328 audio->ch4.control.packed = value;
329 audio->ch4.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch4.envelope.length)) >> 8;
330 if (audio->ch4.control.restart) {
331 audio->playingCh4 = 1;
332 audio->ch4.envelope.currentVolume = audio->ch4.envelope.initialVolume;
333 if (audio->ch4.envelope.stepTime) {
334 audio->ch4.envelope.nextStep = 0;
335 } else {
336 audio->ch4.envelope.nextStep = INT_MAX;
337 }
338 if (audio->ch4.control.power) {
339 audio->ch4.lfsr = 0x40;
340 } else {
341 audio->ch4.lfsr = 0x4000;
342 }
343 audio->nextCh4 = 0;
344 }
345}
346
347void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value) {
348 audio->soundcntLo = value;
349}
350
351void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value) {
352 audio->soundcntHi = value;
353}
354
355void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value) {
356 audio->soundcntX = (value & 0x80) | (audio->soundcntX & 0x0F);
357}
358
359void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value) {
360 audio->ch3.wavedata[address | (!audio->ch3.bank.bank * 4)] = value;
361}
362
363void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value) {
364 struct CircleBuffer* fifo;
365 switch (address) {
366 case REG_FIFO_A_LO:
367 fifo = &audio->chA.fifo;
368 break;
369 case REG_FIFO_B_LO:
370 fifo = &audio->chB.fifo;
371 break;
372 default:
373 GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", address);
374 return;
375 }
376 while (!CircleBufferWrite32(fifo, value)) {
377 int32_t dummy;
378 CircleBufferRead32(fifo, &dummy);
379 }
380}
381
382void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId) {
383 struct GBAAudioFIFO* channel;
384 if (fifoId == 0) {
385 channel = &audio->chA;
386 } else if (fifoId == 1) {
387 channel = &audio->chB;
388 } else {
389 GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", fifoId);
390 return;
391 }
392 if (CircleBufferSize(&channel->fifo) <= 4 * sizeof(int32_t)) {
393 struct GBADMA* dma = &audio->p->memory.dma[channel->dmaSource];
394 dma->nextCount = 4;
395 GBAMemoryServiceDMA(&audio->p->memory, channel->dmaSource, dma);
396 }
397 CircleBufferRead8(&channel->fifo, &channel->sample);
398}
399
400static int32_t _updateSquareChannel(struct GBAAudioSquareControl* control, int duty) {
401 control->hi = !control->hi;
402 int period = 16 * (2048 - control->frequency);
403 switch (duty) {
404 case 0:
405 return control->hi ? period : period * 7;
406 case 1:
407 return control->hi ? period * 2 : period * 6;
408 case 2:
409 return period * 4;
410 case 3:
411 return control->hi ? period * 6 : period * 2;
412 default:
413 // This should never be hit
414 return period * 4;
415 }
416}
417
418static void _updateEnvelope(struct GBAAudioEnvelope* envelope) {
419 if (envelope->direction) {
420 ++envelope->currentVolume;
421 } else {
422 --envelope->currentVolume;
423 }
424 if (envelope->currentVolume >= 15) {
425 envelope->currentVolume = 15;
426 envelope->nextStep = INT_MAX;
427 } else if (envelope->currentVolume <= 0) {
428 envelope->currentVolume = 0;
429 envelope->dead = 1;
430 envelope->nextStep = INT_MAX;
431 } else {
432 envelope->nextStep += envelope->stepTime * (GBA_ARM7TDMI_FREQUENCY >> 6);
433 }
434}
435
436static int _updateSweep(struct GBAAudioChannel1* ch) {
437 if (ch->sweep.direction) {
438 int frequency = ch->control.frequency;
439 frequency -= frequency >> ch->sweep.shift;
440 if (frequency >= 0) {
441 ch->control.frequency = frequency;
442 }
443 } else {
444 int frequency = ch->control.frequency;
445 frequency += frequency >> ch->sweep.shift;
446 if (frequency < 2048) {
447 ch->control.frequency = frequency;
448 } else {
449 return 0;
450 }
451 }
452 ch->nextSweep += ch->sweep.time * SWEEP_CYCLES;
453 return 1;
454}
455
456static int32_t _updateChannel1(struct GBAAudioChannel1* ch) {
457 int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
458 ch->sample = ch->control.hi * 0x10 - 0x8;
459 ch->sample *= ch->envelope.currentVolume;
460 return timing;
461}
462
463static int32_t _updateChannel2(struct GBAAudioChannel2* ch) {
464 int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
465 ch->sample = ch->control.hi * 0x10 - 0x8;
466 ch->sample *= ch->envelope.currentVolume;
467 return timing;
468}
469
470static int32_t _updateChannel3(struct GBAAudioChannel3* ch) {
471 int i;
472 int start;
473 int end;
474 int volume;
475 switch (ch->wave.volume) {
476 case 0:
477 volume = 0;
478 break;
479 case 1:
480 volume = 4;
481 break;
482 case 2:
483 volume = 2;
484 break;
485 case 3:
486 volume = 1;
487 break;
488 default:
489 volume = 3;
490 break;
491 }
492 if (ch->bank.size) {
493 start = 7;
494 end = 0;
495 } else if (ch->bank.bank) {
496 start = 7;
497 end = 4;
498 } else {
499 start = 3;
500 end = 0;
501 }
502 uint32_t bitsCarry = ch->wavedata[end] & 0xF0000000;
503 uint32_t bits;
504 for (i = start; i >= end; --i) {
505 bits = ch->wavedata[i] & 0xF0000000;
506 ch->wavedata[i] <<= 4;
507 ch->wavedata[i] |= bitsCarry >> 28;
508 bitsCarry = bits;
509 }
510 ch->sample = ((bitsCarry >> 26) - 0x20) * volume;
511 return 8 * (2048 - ch->control.rate);
512}
513
514static int32_t _updateChannel4(struct GBAAudioChannel4* ch) {
515 int lsb = ch->lfsr & 1;
516 ch->sample = lsb * 0x10 - 0x8;
517 ch->sample *= ch->envelope.currentVolume;
518 ch->lfsr >>= 1;
519 ch->lfsr ^= (lsb * 0x60) << (ch->control.power ? 0 : 8);
520 int timing = ch->control.ratio ? 2 * ch->control.ratio : 1;
521 timing <<= ch->control.frequency;
522 timing *= 32;
523 return timing;
524}
525
526static void _sample(struct GBAAudio* audio) {
527 int32_t sampleLeft = 0;
528 int32_t sampleRight = 0;
529 int psgShift = 1 + audio->volume;
530
531 if (audio->ch1Left) {
532 sampleLeft += audio->ch1.sample;
533 }
534
535 if (audio->ch1Right) {
536 sampleRight += audio->ch1.sample;
537 }
538
539 if (audio->ch2Left) {
540 sampleLeft += audio->ch2.sample;
541 }
542
543 if (audio->ch2Right) {
544 sampleRight += audio->ch2.sample;
545 }
546
547 if (audio->ch3Left) {
548 sampleLeft += audio->ch3.sample;
549 }
550
551 if (audio->ch3Right) {
552 sampleRight += audio->ch3.sample;
553 }
554
555 if (audio->ch4Left) {
556 sampleLeft += audio->ch4.sample;
557 }
558
559 if (audio->ch4Right) {
560 sampleRight += audio->ch4.sample;
561 }
562
563 sampleLeft = (sampleLeft * (1 + audio->volumeLeft)) >> psgShift;
564 sampleRight = (sampleRight * (1 + audio->volumeRight)) >> psgShift;
565
566 if (audio->chALeft) {
567 sampleLeft += (audio->chA.sample << 2) >> !audio->volumeChA;
568 }
569
570 if (audio->chARight) {
571 sampleRight += (audio->chA.sample << 2) >> !audio->volumeChA;
572 }
573
574 if (audio->chBLeft) {
575 sampleLeft += (audio->chB.sample << 2) >> !audio->volumeChB;
576 }
577
578 if (audio->chBRight) {
579 sampleRight += (audio->chB.sample << 2) >> !audio->volumeChB;
580 }
581
582 pthread_mutex_lock(&audio->bufferMutex);
583 while (CircleBufferSize(&audio->left) + (GBA_AUDIO_SAMPLES * 2 / 5) >= audio->left.capacity) {
584 if (!audio->p->sync->audioWait) {
585 break;
586 }
587 GBASyncProduceAudio(audio->p->sync, &audio->bufferMutex);
588 }
589 CircleBufferWrite32(&audio->left, sampleLeft);
590 CircleBufferWrite32(&audio->right, sampleRight);
591 pthread_mutex_unlock(&audio->bufferMutex);
592}