all repos — mgba @ ebe543e482699a068d34f53466aec433fa8ba1fc

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