all repos — mgba @ ad7f1ae9395fc3d4cd7c28c381ca89d416426246

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