all repos — mgba @ d7968ccc0ddc0beb93d8b4044d2324a5e390f391

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