all repos — mgba @ 3202811e416e8b7c6ddc8a68442536510aa2b85e

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