all repos — mgba @ 6e40c7ec29b0b31343d427d51ea4c029cd71ce8c

mGBA Game Boy Advance Emulator

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