all repos — mgba @ 4fa94019712a9eb6177f65962ea032ed784ed019

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