all repos — mgba @ 38cb3eeaa857e744917d3ed57263f7585bf74233

mGBA Game Boy Advance Emulator

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 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 = 0;
 57	audio->chB.dmaSource = 0;
 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#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
456	unsigned newSampleRate = 0x8000 << GBARegisterSOUNDBIASGetResolution(audio->soundbias);
457	if (audio->sampleRate != newSampleRate) {
458		audio->sampleRate = newSampleRate;
459		audio->sampleInterval = GBA_ARM7TDMI_FREQUENCY / audio->sampleRate;
460	}
461#endif
462}
463
464void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value) {
465	audio->ch3.wavedata[address | (!audio->ch3.bank.bank * 4)] = value;
466}
467
468void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value) {
469	struct CircleBuffer* fifo;
470	switch (address) {
471	case REG_FIFO_A_LO:
472		fifo = &audio->chA.fifo;
473		break;
474	case REG_FIFO_B_LO:
475		fifo = &audio->chB.fifo;
476		break;
477	default:
478		GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", address);
479		return;
480	}
481	int i;
482	for (i = 0; i < 4; ++i) {
483		while (!CircleBufferWrite8(fifo, value >> (8 * i))) {
484			int8_t dummy;
485			CircleBufferRead8(fifo, &dummy);
486		}
487	}
488}
489
490void GBAAudioWriteFIFO16(struct GBAAudio* audio, int address, uint16_t value) {
491	struct CircleBuffer* fifo;
492	switch (address) {
493	case REG_FIFO_A_LO:
494	case REG_FIFO_A_HI:
495		fifo = &audio->chA.fifo;
496		break;
497	case REG_FIFO_B_LO:
498	case REG_FIFO_B_HI:
499		fifo = &audio->chB.fifo;
500		break;
501	default:
502		GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", address);
503		return;
504	}
505	int i;
506	for (i = 0; i < 2; ++i) {
507		while (!CircleBufferWrite8(fifo, value >> (8 * i))) {
508			int8_t dummy;
509			CircleBufferRead8(fifo, &dummy);
510		}
511	}
512}
513
514void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId, int32_t cycles) {
515	struct GBAAudioFIFO* channel;
516	if (fifoId == 0) {
517		channel = &audio->chA;
518	} else if (fifoId == 1) {
519		channel = &audio->chB;
520	} else {
521		GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", fifoId);
522		return;
523	}
524	if (CircleBufferSize(&channel->fifo) <= 4 * sizeof(int32_t)) {
525		struct GBADMA* dma = &audio->p->memory.dma[channel->dmaSource];
526		dma->nextCount = 4;
527		dma->nextEvent = 0;
528		GBAMemoryUpdateDMAs(audio->p, -cycles);
529	}
530	CircleBufferRead8(&channel->fifo, &channel->sample);
531}
532
533#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
534unsigned GBAAudioCopy(struct GBAAudio* audio, void* left, void* right, unsigned nSamples) {
535	GBASyncLockAudio(audio->p->sync);
536	unsigned read = 0;
537	if (left) {
538		unsigned readL = CircleBufferRead(&audio->left, left, nSamples * sizeof(int16_t)) >> 1;
539		if (readL < nSamples) {
540			memset((int16_t*) left + readL, 0, nSamples - readL);
541		}
542		read = readL;
543	}
544	if (right) {
545		unsigned readR = CircleBufferRead(&audio->right, right, nSamples * sizeof(int16_t)) >> 1;
546		if (readR < nSamples) {
547			memset((int16_t*) right + readR, 0, nSamples - readR);
548		}
549		read = read >= readR ? read : readR;
550	}
551	GBASyncConsumeAudio(audio->p->sync);
552	return read;
553}
554
555unsigned GBAAudioResampleNN(struct GBAAudio* audio, float ratio, float* drift, struct GBAStereoSample* output, unsigned nSamples) {
556	int16_t left[GBA_AUDIO_SAMPLES];
557	int16_t right[GBA_AUDIO_SAMPLES];
558
559	// toRead is in GBA samples
560	// TODO: Do this with fixed-point math
561	unsigned toRead = ceilf(nSamples / ratio);
562	unsigned totalRead = 0;
563	while (nSamples) {
564		unsigned currentRead = GBA_AUDIO_SAMPLES;
565		if (currentRead > toRead) {
566			currentRead = toRead;
567		}
568		unsigned read = GBAAudioCopy(audio, left, right, currentRead);
569		toRead -= read;
570		unsigned i;
571		for (i = 0; i < read; ++i) {
572			*drift += ratio;
573			while (*drift >= 1.f) {
574				output->left = left[i];
575				output->right = right[i];
576				++output;
577				++totalRead;
578				--nSamples;
579				*drift -= 1.f;
580				if (!nSamples) {
581					return totalRead;
582				}
583			}
584		}
585		if (read < currentRead) {
586			memset(output, 0, nSamples * sizeof(struct GBAStereoSample));
587			break;
588		}
589	}
590	return totalRead;
591}
592#endif
593
594bool _writeEnvelope(struct GBAAudioEnvelope* envelope, uint16_t value) {
595	envelope->length = GBAAudioRegisterEnvelopeGetLength(value);
596	envelope->duty = GBAAudioRegisterEnvelopeGetDuty(value);
597	envelope->stepTime = GBAAudioRegisterEnvelopeGetStepTime(value);
598	envelope->direction = GBAAudioRegisterEnvelopeGetDirection(value);
599	envelope->initialVolume = GBAAudioRegisterEnvelopeGetInitialVolume(value);
600	envelope->dead = 0;
601	if (envelope->stepTime) {
602		envelope->nextStep = 0;
603	} else {
604		envelope->nextStep = INT_MAX;
605		if (envelope->initialVolume == 0) {
606			envelope->dead = 1;
607			return false;
608		}
609	}
610	return true;
611}
612
613static int32_t _updateSquareChannel(struct GBAAudioSquareControl* control, int duty) {
614	control->hi = !control->hi;
615	int period = 16 * (2048 - control->frequency);
616	switch (duty) {
617	case 0:
618		return control->hi ? period : period * 7;
619	case 1:
620		return control->hi ? period * 2 : period * 6;
621	case 2:
622		return period * 4;
623	case 3:
624		return control->hi ? period * 6 : period * 2;
625	default:
626		// This should never be hit
627		return period * 4;
628	}
629}
630
631static void _updateEnvelope(struct GBAAudioEnvelope* envelope) {
632	if (envelope->direction) {
633		++envelope->currentVolume;
634	} else {
635		--envelope->currentVolume;
636	}
637	if (envelope->currentVolume >= 15) {
638		envelope->currentVolume = 15;
639		envelope->nextStep = INT_MAX;
640	} else if (envelope->currentVolume <= 0) {
641		envelope->currentVolume = 0;
642		envelope->dead = 1;
643		envelope->nextStep = INT_MAX;
644	} else {
645		envelope->nextStep += envelope->stepTime * (GBA_ARM7TDMI_FREQUENCY >> 6);
646	}
647}
648
649static bool _updateSweep(struct GBAAudioChannel1* ch) {
650	if (ch->sweep.direction) {
651		int frequency = ch->control.frequency;
652		frequency -= frequency >> ch->sweep.shift;
653		if (frequency >= 0) {
654			ch->control.frequency = frequency;
655		}
656	} else {
657		int frequency = ch->control.frequency;
658		frequency += frequency >> ch->sweep.shift;
659		if (frequency < 2048) {
660			ch->control.frequency = frequency;
661		} else {
662			return false;
663		}
664	}
665	ch->nextSweep += ch->sweep.time * SWEEP_CYCLES;
666	return true;
667}
668
669static int32_t _updateChannel1(struct GBAAudioChannel1* 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 _updateChannel2(struct GBAAudioChannel2* ch) {
677	int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
678	ch->sample = ch->control.hi * 0x10 - 0x8;
679	ch->sample *= ch->envelope.currentVolume;
680	return timing;
681}
682
683static int32_t _updateChannel3(struct GBAAudioChannel3* ch) {
684	int i;
685	int start;
686	int end;
687	int volume;
688	switch (ch->wave.volume) {
689	case 0:
690		volume = 0;
691		break;
692	case 1:
693		volume = 4;
694		break;
695	case 2:
696		volume = 2;
697		break;
698	case 3:
699		volume = 1;
700		break;
701	default:
702		volume = 3;
703		break;
704	}
705	if (ch->bank.size) {
706		start = 7;
707		end = 0;
708	} else if (ch->bank.bank) {
709		start = 7;
710		end = 4;
711	} else {
712		start = 3;
713		end = 0;
714	}
715	uint32_t bitsCarry = ch->wavedata[end] & 0x0F000000;
716	uint32_t bits;
717	for (i = start; i >= end; --i) {
718		bits = ch->wavedata[i] & 0x0F000000;
719		ch->wavedata[i] = ((ch->wavedata[i] & 0xF0F0F0F0) >> 4) | ((ch->wavedata[i] & 0x000F0F0F) << 12);
720		ch->wavedata[i] |= bitsCarry >> 20;
721		bitsCarry = bits;
722	}
723	ch->sample = bitsCarry >> 24;
724	ch->sample -= 8;
725	ch->sample *= volume * 4;
726	return 8 * (2048 - ch->control.rate);
727}
728
729static int32_t _updateChannel4(struct GBAAudioChannel4* ch) {
730	int lsb = ch->lfsr & 1;
731	ch->sample = lsb * 0x10 - 0x8;
732	ch->sample *= ch->envelope.currentVolume;
733	ch->lfsr >>= 1;
734	ch->lfsr ^= (lsb * 0x60) << (ch->control.power ? 0 : 8);
735	int timing = ch->control.ratio ? 2 * ch->control.ratio : 1;
736	timing <<= ch->control.frequency;
737	timing *= 32;
738	return timing;
739}
740
741static int _applyBias(struct GBAAudio* audio, int sample) {
742	sample += GBARegisterSOUNDBIASGetBias(audio->soundbias);
743	if (sample >= 0x400) {
744		sample = 0x3FF;
745	} else if (sample < 0) {
746		sample = 0;
747	}
748	return (sample - GBARegisterSOUNDBIASGetBias(audio->soundbias)) << 5;
749}
750
751static void _sample(struct GBAAudio* audio) {
752	int16_t sampleLeft = 0;
753	int16_t sampleRight = 0;
754	int psgShift = 6 - audio->volume;
755
756	if (audio->ch1Left) {
757		sampleLeft += audio->ch1.sample;
758	}
759
760	if (audio->ch1Right) {
761		sampleRight += audio->ch1.sample;
762	}
763
764	if (audio->ch2Left) {
765		sampleLeft += audio->ch2.sample;
766	}
767
768	if (audio->ch2Right) {
769		sampleRight += audio->ch2.sample;
770	}
771
772	if (audio->ch3Left) {
773		sampleLeft += audio->ch3.sample;
774	}
775
776	if (audio->ch3Right) {
777		sampleRight += audio->ch3.sample;
778	}
779
780	if (audio->ch4Left) {
781		sampleLeft += audio->ch4.sample;
782	}
783
784	if (audio->ch4Right) {
785		sampleRight += audio->ch4.sample;
786	}
787
788	sampleLeft = (sampleLeft * (1 + audio->volumeLeft)) >> psgShift;
789	sampleRight = (sampleRight * (1 + audio->volumeRight)) >> psgShift;
790
791	if (audio->chALeft) {
792		sampleLeft += (audio->chA.sample << 2) >> !audio->volumeChA;
793	}
794
795	if (audio->chARight) {
796		sampleRight += (audio->chA.sample << 2) >> !audio->volumeChA;
797	}
798
799	if (audio->chBLeft) {
800		sampleLeft += (audio->chB.sample << 2) >> !audio->volumeChB;
801	}
802
803	if (audio->chBRight) {
804		sampleRight += (audio->chB.sample << 2) >> !audio->volumeChB;
805	}
806
807	sampleLeft = _applyBias(audio, sampleLeft);
808	sampleRight = _applyBias(audio, sampleRight);
809
810	GBASyncLockAudio(audio->p->sync);
811	unsigned produced;
812#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
813	CircleBufferWrite16(&audio->left, sampleLeft);
814	CircleBufferWrite16(&audio->right, sampleRight);
815	produced = CircleBufferSize(&audio->left) / 2;
816#else
817	if ((size_t) blip_samples_avail(audio->left) < audio->samples) {
818		blip_add_delta(audio->left, audio->clock, sampleLeft - audio->lastLeft);
819		blip_add_delta(audio->right, audio->clock, sampleRight - audio->lastRight);
820		audio->lastLeft = sampleLeft;
821		audio->lastRight = sampleRight;
822		audio->clock += audio->sampleInterval;
823		int clockNeeded = blip_clocks_needed(audio->left, audio->samples / 32);
824		if (audio->clock >= clockNeeded) {
825			blip_end_frame(audio->left, audio->clock);
826			blip_end_frame(audio->right, audio->clock);
827			audio->clock -= clockNeeded;
828		}
829	}
830	produced = blip_samples_avail(audio->left);
831#endif
832	struct GBAThread* thread = GBAThreadGetContext();
833	if (thread && thread->stream) {
834		thread->stream->postAudioFrame(thread->stream, sampleLeft, sampleRight);
835	}
836	GBASyncProduceAudio(audio->p->sync, produced >= audio->samples);
837}
838
839void GBAAudioSerialize(const struct GBAAudio* audio, struct GBASerializedState* state) {
840	state->audio.ch1Volume = audio->ch1.envelope.currentVolume;
841	state->audio.ch1Dead = audio->ch1.envelope.dead;
842	state->audio.ch1Hi = audio->ch1.control.hi;
843	state->audio.ch1.envelopeNextStep = audio->ch1.envelope.nextStep;
844	state->audio.ch1.waveNextStep = audio->ch1.control.nextStep;
845	state->audio.ch1.sweepNextStep = audio->ch1.nextSweep;
846	state->audio.ch1.endTime = audio->ch1.control.endTime;
847	state->audio.ch1.nextEvent = audio->nextCh1;
848
849	state->audio.ch2Volume = audio->ch2.envelope.currentVolume;
850	state->audio.ch2Dead = audio->ch2.envelope.dead;
851	state->audio.ch2Hi = audio->ch2.control.hi;
852	state->audio.ch2.envelopeNextStep = audio->ch2.envelope.nextStep;
853	state->audio.ch2.waveNextStep = audio->ch2.control.nextStep;
854	state->audio.ch2.endTime = audio->ch2.control.endTime;
855	state->audio.ch2.nextEvent = audio->nextCh2;
856
857	memcpy(state->audio.ch3.wavebanks, audio->ch3.wavedata, sizeof(state->audio.ch3.wavebanks));
858	state->audio.ch3.endTime = audio->ch3.control.endTime;
859	state->audio.ch3.nextEvent = audio->nextCh3;
860
861	state->audio.ch4Volume = audio->ch4.envelope.currentVolume;
862	state->audio.ch4Dead = audio->ch4.envelope.dead;
863	state->audio.ch4.envelopeNextStep = audio->ch4.envelope.nextStep;
864	state->audio.ch4.lfsr = audio->ch4.lfsr;
865	state->audio.ch4.endTime = audio->ch4.control.endTime;
866	state->audio.ch4.nextEvent = audio->nextCh4;
867
868	CircleBufferDump(&audio->chA.fifo, state->audio.fifoA, sizeof(state->audio.fifoA));
869	CircleBufferDump(&audio->chB.fifo, state->audio.fifoB, sizeof(state->audio.fifoB));
870	state->audio.fifoSize = CircleBufferSize(&audio->chA.fifo);
871
872	state->audio.nextEvent = audio->nextEvent;
873	state->audio.eventDiff = audio->eventDiff;
874	state->audio.nextSample = audio->nextSample;
875}
876
877void GBAAudioDeserialize(struct GBAAudio* audio, const struct GBASerializedState* state) {
878	audio->ch1.envelope.currentVolume = state->audio.ch1Volume;
879	audio->ch1.envelope.dead = state->audio.ch1Dead;
880	audio->ch1.control.hi = state->audio.ch1Hi;
881	audio->ch1.envelope.nextStep = state->audio.ch1.envelopeNextStep;
882	audio->ch1.control.nextStep = state->audio.ch1.waveNextStep;
883	audio->ch1.nextSweep = state->audio.ch1.sweepNextStep;
884	audio->ch1.control.endTime = state->audio.ch1.endTime;
885	audio->nextCh1 = state->audio.ch1.nextEvent;
886
887	audio->ch2.envelope.currentVolume = state->audio.ch2Volume;
888	audio->ch2.envelope.dead = state->audio.ch2Dead;
889	audio->ch2.control.hi = state->audio.ch2Hi;
890	audio->ch2.envelope.nextStep = state->audio.ch2.envelopeNextStep;
891	audio->ch2.control.nextStep = state->audio.ch2.waveNextStep;
892	audio->ch2.control.endTime = state->audio.ch2.endTime;
893	audio->nextCh2 = state->audio.ch2.nextEvent;
894
895	memcpy(audio->ch3.wavedata, state->audio.ch3.wavebanks, sizeof(audio->ch3.wavedata));
896	audio->ch3.control.endTime = state->audio.ch3.endTime;
897	audio->nextCh3 = state->audio.ch3.nextEvent;
898
899	audio->ch4.envelope.currentVolume = state->audio.ch4Volume;
900	audio->ch4.envelope.dead = state->audio.ch4Dead;
901	audio->ch4.envelope.nextStep = state->audio.ch4.envelopeNextStep;
902	audio->ch4.lfsr = state->audio.ch4.lfsr;
903	audio->ch4.control.endTime = state->audio.ch4.endTime;
904	audio->nextCh4 = state->audio.ch4.nextEvent;
905
906	CircleBufferClear(&audio->chA.fifo);
907	CircleBufferClear(&audio->chB.fifo);
908	int i;
909	for (i = 0; i < state->audio.fifoSize; ++i) {
910		CircleBufferWrite8(&audio->chA.fifo, state->audio.fifoA[i]);
911		CircleBufferWrite8(&audio->chB.fifo, state->audio.fifoB[i]);
912	}
913
914	audio->nextEvent = state->audio.nextEvent;
915	audio->eventDiff = state->audio.eventDiff;
916	audio->nextSample = state->audio.nextSample;
917}
918
919float GBAAudioCalculateRatio(float inputSampleRate, float desiredFPS, float desiredSampleRate) {
920	return desiredSampleRate * GBA_ARM7TDMI_FREQUENCY / (VIDEO_TOTAL_LENGTH * desiredFPS * inputSampleRate);
921}