all repos — mgba @ 8c9790bb3bd8ac8c388c11d131333236796af194

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