all repos — mgba @ 0601c6f21c213af8b89a57ec6a51986458427de5

mGBA Game Boy Advance Emulator

src/gb/audio.c (view raw)

  1/* Copyright (c) 2013-2016 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "audio.h"
  7
  8#include "core/sync.h"
  9#include "gb/gb.h"
 10#include "gb/io.h"
 11
 12#define FRAME_CYCLES (DMG_LR35902_FREQUENCY >> 9)
 13
 14static const int CLOCKS_PER_BLIP_FRAME = 0x1000;
 15static const unsigned BLIP_BUFFER_SIZE = 0x4000;
 16
 17static void _writeDuty(struct GBAudioEnvelope* envelope, uint8_t value);
 18static bool _writeSweep(struct GBAudioEnvelope* envelope, uint8_t value);
 19static int32_t _updateSquareChannel(struct GBAudioSquareControl* envelope, int duty);
 20static void _updateEnvelope(struct GBAudioEnvelope* envelope);
 21static bool _updateSweep(struct GBAudioChannel1* ch, bool initial);
 22static int32_t _updateChannel1(struct GBAudioChannel1* ch);
 23static int32_t _updateChannel2(struct GBAudioChannel2* ch);
 24static int32_t _updateChannel3(struct GBAudioChannel3* ch);
 25static int32_t _updateChannel4(struct GBAudioChannel4* ch);
 26static void _sample(struct GBAudio* audio, int32_t cycles);
 27
 28void GBAudioInit(struct GBAudio* audio, size_t samples) {
 29	audio->samples = samples;
 30	audio->left = blip_new(BLIP_BUFFER_SIZE);
 31	audio->right = blip_new(BLIP_BUFFER_SIZE);
 32	audio->clockRate = DMG_LR35902_FREQUENCY;
 33	// Guess too large; we hang producing extra samples if we guess too low
 34	blip_set_rates(audio->left, DMG_LR35902_FREQUENCY, 96000);
 35	blip_set_rates(audio->right, DMG_LR35902_FREQUENCY, 96000);
 36	audio->forceDisableCh[0] = false;
 37	audio->forceDisableCh[1] = false;
 38	audio->forceDisableCh[2] = false;
 39	audio->forceDisableCh[3] = false;
 40}
 41
 42void GBAudioDeinit(struct GBAudio* audio) {
 43	blip_delete(audio->left);
 44	blip_delete(audio->right);
 45}
 46
 47void GBAudioReset(struct GBAudio* audio) {
 48	audio->nextEvent = 0;
 49	audio->nextCh1 = 0;
 50	audio->nextCh2 = 0;
 51	audio->nextCh3 = 0;
 52	audio->nextCh4 = 0;
 53	audio->ch1 = (struct GBAudioChannel1) { .envelope = { .dead = 1 } };
 54	audio->ch2 = (struct GBAudioChannel2) { .envelope = { .dead = 1 } };
 55	audio->ch3 = (struct GBAudioChannel3) { .bank = 0 };
 56	audio->ch4 = (struct GBAudioChannel4) { .envelope = { .dead = 1 } };
 57	audio->eventDiff = 0;
 58	audio->nextFrame = 0;
 59	audio->frame = 0;
 60	audio->nextSample = 0;
 61	audio->sampleInterval = 128;
 62	audio->volumeRight = 0;
 63	audio->volumeLeft = 0;
 64	audio->ch1Right = false;
 65	audio->ch2Right = false;
 66	audio->ch3Right = false;
 67	audio->ch4Right = false;
 68	audio->ch1Left = false;
 69	audio->ch2Left = false;
 70	audio->ch3Left = false;
 71	audio->ch4Left = false;
 72	audio->playingCh1 = false;
 73	audio->playingCh2 = false;
 74	audio->playingCh3 = false;
 75	audio->playingCh4 = false;
 76}
 77
 78void GBAudioWriteNR10(struct GBAudio* audio, uint8_t value) {
 79	audio->ch1.shift = GBAudioRegisterSquareSweepGetShift(value);
 80	audio->ch1.direction = GBAudioRegisterSquareSweepGetDirection(value);
 81	audio->ch1.time = GBAudioRegisterSquareSweepGetTime(value);
 82	audio->ch1.sweepStep = audio->ch1.time;
 83}
 84
 85void GBAudioWriteNR11(struct GBAudio* audio, uint8_t value) {
 86	_writeDuty(&audio->ch1.envelope, value);
 87	audio->ch1.control.length = 64 - audio->ch1.envelope.length;
 88}
 89
 90void GBAudioWriteNR12(struct GBAudio* audio, uint8_t value) {
 91	if (!_writeSweep(&audio->ch1.envelope, value)) {
 92		audio->playingCh1 = false;
 93		// TODO: Don't need p
 94		if (audio->p) {
 95			audio->p->memory.io[REG_NR52] &= ~0x0001;
 96		}
 97	}
 98}
 99
100void GBAudioWriteNR13(struct GBAudio* audio, uint8_t value) {
101	audio->ch1.control.frequency &= 0x700;
102	audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value);
103}
104
105void GBAudioWriteNR14(struct GBAudio* audio, uint8_t value) {
106	audio->ch1.control.frequency &= 0xFF;
107	audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value << 8);
108	bool wasStop = audio->ch1.control.stop;
109	audio->ch1.control.stop = GBAudioRegisterControlGetStop(value << 8);
110	if (!wasStop && audio->ch1.control.stop && audio->ch1.control.length && !(audio->frame & 1)) {
111		--audio->ch1.control.length;
112		if (audio->ch1.control.length == 0) {
113			audio->playingCh1 = false;
114		}
115	}
116	if (GBAudioRegisterControlIsRestart(value << 8)) {
117		if (audio->nextEvent == INT_MAX) {
118			audio->eventDiff = 0;
119		}
120		if (!audio->playingCh1) {
121			audio->nextCh1 = audio->eventDiff;
122		}
123		audio->playingCh1 = audio->ch1.envelope.initialVolume || audio->ch1.envelope.direction;
124		audio->ch1.envelope.currentVolume = audio->ch1.envelope.initialVolume;
125		if (audio->ch1.envelope.currentVolume > 0 && audio->ch1.envelope.stepTime) {
126			audio->ch1.envelope.dead = 0;
127		}
128		audio->ch1.sweepStep = audio->ch1.time;
129		audio->ch1.sweepEnable = audio->ch1.sweepStep || audio->ch1.shift;
130		if (audio->playingCh1 && audio->ch1.shift) {
131			audio->playingCh1 = _updateSweep(&audio->ch1, true);
132		}
133		if (!audio->ch1.control.length) {
134			audio->ch1.control.length = 64;
135			if (audio->ch1.control.stop && !(audio->frame & 1)) {
136				--audio->ch1.control.length;
137			}
138		}
139		audio->nextEvent = audio->eventDiff;
140		if (audio->p) {
141			// TODO: Don't need
142			audio->p->cpu->nextEvent = audio->eventDiff;
143		}
144	}
145	// TODO: Don't need p
146	if (audio->p) {
147		audio->p->memory.io[REG_NR52] &= ~0x0001;
148		audio->p->memory.io[REG_NR52] |= audio->playingCh1;
149	}
150}
151
152void GBAudioWriteNR21(struct GBAudio* audio, uint8_t value) {
153	_writeDuty(&audio->ch2.envelope, value);
154	audio->ch2.control.length = 64 - audio->ch2.envelope.length;
155}
156
157void GBAudioWriteNR22(struct GBAudio* audio, uint8_t value) {
158	if (!_writeSweep(&audio->ch2.envelope, value)) {
159		audio->playingCh2 = false;
160		// TODO: Don't need p
161		if (audio->p) {
162			audio->p->memory.io[REG_NR52] &= ~0x0002;
163		}
164	}
165}
166
167void GBAudioWriteNR23(struct GBAudio* audio, uint8_t value) {
168	audio->ch2.control.frequency &= 0x700;
169	audio->ch2.control.frequency |= GBAudioRegisterControlGetFrequency(value);
170}
171
172void GBAudioWriteNR24(struct GBAudio* audio, uint8_t value) {
173	audio->ch2.control.frequency &= 0xFF;
174	audio->ch2.control.frequency |= GBAudioRegisterControlGetFrequency(value << 8);
175	bool wasStop = audio->ch2.control.stop;
176	audio->ch2.control.stop = GBAudioRegisterControlGetStop(value << 8);
177	if (!wasStop && audio->ch2.control.stop && audio->ch2.control.length && !(audio->frame & 1)) {
178		--audio->ch2.control.length;
179		if (audio->ch2.control.length == 0) {
180			audio->playingCh2 = false;
181		}
182	}
183	if (GBAudioRegisterControlIsRestart(value << 8)) {
184		audio->playingCh2 = audio->ch2.envelope.initialVolume || audio->ch2.envelope.direction;
185		audio->ch2.envelope.currentVolume = audio->ch2.envelope.initialVolume;
186		if (audio->ch2.envelope.currentVolume > 0 && audio->ch2.envelope.stepTime) {
187			audio->ch2.envelope.dead = 0;
188		}
189		if (audio->nextEvent == INT_MAX) {
190			audio->eventDiff = 0;
191		}
192		if (!audio->playingCh2) {
193			audio->nextCh2 = audio->eventDiff;
194		}
195		if (!audio->ch2.control.length) {
196			audio->ch2.control.length = 64;
197			if (audio->ch2.control.stop && !(audio->frame & 1)) {
198				--audio->ch2.control.length;
199			}
200		}
201		audio->nextEvent = audio->eventDiff;
202		if (audio->p) {
203			// TODO: Don't need p
204			audio->p->cpu->nextEvent = audio->eventDiff;
205		}
206	}
207	// TODO: Don't need p
208	if (audio->p) {
209		audio->p->memory.io[REG_NR52] &= ~0x0002;
210		audio->p->memory.io[REG_NR52] |= audio->playingCh2 << 1;
211	}
212}
213
214void GBAudioWriteNR30(struct GBAudio* audio, uint8_t value) {
215	audio->ch3.enable = GBAudioRegisterBankGetEnable(value);
216	if (!audio->ch3.enable) {
217		audio->playingCh3 = false;
218		// TODO: Don't need p
219		if (audio->p) {
220			audio->p->memory.io[REG_NR52] &= ~0x0004;
221		}
222	}
223}
224
225void GBAudioWriteNR31(struct GBAudio* audio, uint8_t value) {
226	audio->ch3.length = value;
227	audio->ch3.lengthShadow = 256 - value;
228}
229
230void GBAudioWriteNR32(struct GBAudio* audio, uint8_t value) {
231	audio->ch3.volume = GBAudioRegisterBankVolumeGetVolumeGB(value);
232}
233
234void GBAudioWriteNR33(struct GBAudio* audio, uint8_t value) {
235	audio->ch3.rate &= 0x700;
236	audio->ch3.rate |= GBAudioRegisterControlGetRate(value);
237}
238
239void GBAudioWriteNR34(struct GBAudio* audio, uint8_t value) {
240	audio->ch3.rate &= 0xFF;
241	audio->ch3.rate |= GBAudioRegisterControlGetRate(value << 8);
242	bool wasStop = audio->ch3.stop;
243	audio->ch3.stop = GBAudioRegisterControlGetStop(value << 8);
244	if (!wasStop && audio->ch3.stop && audio->ch3.lengthShadow && !(audio->frame & 1)) {
245		--audio->ch3.lengthShadow;
246		if (audio->ch3.lengthShadow == 0) {
247			audio->playingCh3 = false;
248		}
249	}
250	if (GBAudioRegisterControlIsRestart(value << 8)) {
251		audio->playingCh3 = audio->ch3.enable;
252		if (!audio->ch3.lengthShadow) {
253			audio->ch3.lengthShadow = 256;
254			if (audio->ch3.stop && !(audio->frame & 1)) {
255				--audio->ch3.lengthShadow;
256			}
257		}
258	}
259	if (audio->playingCh3) {
260		if (audio->nextEvent == INT_MAX) {
261			audio->eventDiff = 0;
262		}
263		audio->nextCh3 = audio->eventDiff;
264		audio->nextEvent = audio->eventDiff;
265		if (audio->p) {
266			// TODO: Don't need p
267			audio->p->cpu->nextEvent = audio->eventDiff;
268		}
269	}
270	// TODO: Don't need p
271	if (audio->p) {
272		audio->p->memory.io[REG_NR52] &= ~0x0004;
273		audio->p->memory.io[REG_NR52] |= audio->playingCh3 << 2;
274	}
275}
276
277void GBAudioWriteNR41(struct GBAudio* audio, uint8_t value) {
278	_writeDuty(&audio->ch4.envelope, value);
279	audio->ch4.length = 64 - audio->ch4.envelope.length;
280}
281
282void GBAudioWriteNR42(struct GBAudio* audio, uint8_t value) {
283	if (!_writeSweep(&audio->ch4.envelope, value)) {
284		audio->playingCh4 = false;
285		// TODO: Don't need p
286		if (audio->p) {
287			audio->p->memory.io[REG_NR52] &= ~0x0008;
288		}
289	}
290}
291
292void GBAudioWriteNR43(struct GBAudio* audio, uint8_t value) {
293	audio->ch4.ratio = GBAudioRegisterNoiseFeedbackGetRatio(value);
294	audio->ch4.frequency = GBAudioRegisterNoiseFeedbackGetFrequency(value);
295	audio->ch4.power = GBAudioRegisterNoiseFeedbackGetPower(value);
296}
297
298void GBAudioWriteNR44(struct GBAudio* audio, uint8_t value) {
299	bool wasStop = audio->ch4.stop;
300	audio->ch4.stop = GBAudioRegisterNoiseControlGetStop(value);
301	if (!wasStop && audio->ch4.stop && audio->ch4.length && !(audio->frame & 1)) {
302		--audio->ch4.length;
303		if (audio->ch4.length == 0) {
304			audio->playingCh4 = false;
305		}
306	}
307	if (GBAudioRegisterNoiseControlIsRestart(value)) {
308		audio->playingCh4 = audio->ch4.envelope.initialVolume || audio->ch4.envelope.direction;
309		audio->ch4.envelope.currentVolume = audio->ch4.envelope.initialVolume;
310		if (audio->ch4.envelope.currentVolume > 0 && audio->ch4.envelope.stepTime) {
311			audio->ch4.envelope.dead = 0;
312		}
313		if (audio->ch4.power) {
314			audio->ch4.lfsr = 0x40;
315		} else {
316			audio->ch4.lfsr = 0x4000;
317		}
318		if (audio->nextEvent == INT_MAX) {
319			audio->eventDiff = 0;
320		}
321		if (!audio->playingCh4) {
322			audio->nextCh4 = audio->eventDiff;
323		}
324		if (!audio->ch4.length) {
325			audio->ch4.length = 64;
326			if (audio->ch4.stop && !(audio->frame & 1)) {
327				--audio->ch4.length;
328			}
329		}
330		audio->nextEvent = audio->eventDiff;
331		if (audio->p) {
332			// TODO: Don't need p
333			audio->p->cpu->nextEvent = audio->eventDiff;
334		}
335	}
336	// TODO: Don't need p
337	if (audio->p) {
338		audio->p->memory.io[REG_NR52] &= ~0x0008;
339		audio->p->memory.io[REG_NR52] |= audio->playingCh4 << 3;
340	}
341}
342
343void GBAudioWriteNR50(struct GBAudio* audio, uint8_t value) {
344	audio->volumeRight = GBRegisterNR50GetVolumeRight(value);
345	audio->volumeLeft = GBRegisterNR50GetVolumeLeft(value);
346}
347
348void GBAudioWriteNR51(struct GBAudio* audio, uint8_t value) {
349	audio->ch1Right = GBRegisterNR51GetCh1Right(value);
350	audio->ch2Right = GBRegisterNR51GetCh2Right(value);
351	audio->ch3Right = GBRegisterNR51GetCh3Right(value);
352	audio->ch4Right = GBRegisterNR51GetCh4Right(value);
353	audio->ch1Left = GBRegisterNR51GetCh1Left(value);
354	audio->ch2Left = GBRegisterNR51GetCh2Left(value);
355	audio->ch3Left = GBRegisterNR51GetCh3Left(value);
356	audio->ch4Left = GBRegisterNR51GetCh4Left(value);
357}
358
359void GBAudioWriteNR52(struct GBAudio* audio, uint8_t value) {
360	audio->enable = GBAudioEnableGetEnable(value);
361	if (!audio->enable) {
362		audio->playingCh1 = 0;
363		audio->playingCh2 = 0;
364		audio->playingCh3 = 0;
365		audio->playingCh4 = 0;
366		GBAudioWriteNR10(audio, 0);
367		GBAudioWriteNR11(audio, 0);
368		GBAudioWriteNR12(audio, 0);
369		GBAudioWriteNR13(audio, 0);
370		GBAudioWriteNR14(audio, 0);
371		GBAudioWriteNR21(audio, 0);
372		GBAudioWriteNR22(audio, 0);
373		GBAudioWriteNR23(audio, 0);
374		GBAudioWriteNR24(audio, 0);
375		GBAudioWriteNR30(audio, 0);
376		GBAudioWriteNR31(audio, 0);
377		GBAudioWriteNR32(audio, 0);
378		GBAudioWriteNR33(audio, 0);
379		GBAudioWriteNR34(audio, 0);
380		GBAudioWriteNR41(audio, 0);
381		GBAudioWriteNR42(audio, 0);
382		GBAudioWriteNR43(audio, 0);
383		GBAudioWriteNR44(audio, 0);
384		GBAudioWriteNR50(audio, 0);
385		GBAudioWriteNR51(audio, 0);
386		if (audio->p) {
387			audio->p->memory.io[REG_NR10] = 0;
388			audio->p->memory.io[REG_NR11] = 0;
389			audio->p->memory.io[REG_NR12] = 0;
390			audio->p->memory.io[REG_NR13] = 0;
391			audio->p->memory.io[REG_NR14] = 0;
392			audio->p->memory.io[REG_NR21] = 0;
393			audio->p->memory.io[REG_NR22] = 0;
394			audio->p->memory.io[REG_NR23] = 0;
395			audio->p->memory.io[REG_NR24] = 0;
396			audio->p->memory.io[REG_NR30] = 0;
397			audio->p->memory.io[REG_NR31] = 0;
398			audio->p->memory.io[REG_NR32] = 0;
399			audio->p->memory.io[REG_NR33] = 0;
400			audio->p->memory.io[REG_NR34] = 0;
401			audio->p->memory.io[REG_NR41] = 0;
402			audio->p->memory.io[REG_NR42] = 0;
403			audio->p->memory.io[REG_NR43] = 0;
404			audio->p->memory.io[REG_NR44] = 0;
405			audio->p->memory.io[REG_NR50] = 0;
406			audio->p->memory.io[REG_NR51] = 0;
407			audio->p->memory.io[REG_NR52] &= ~0x000F;
408		}
409	} else {
410		audio->frame = 7;
411	}
412}
413
414int32_t GBAudioProcessEvents(struct GBAudio* audio, int32_t cycles) {
415	if (audio->nextEvent == INT_MAX) {
416		return INT_MAX;
417	}
418	audio->nextEvent -= cycles;
419	audio->eventDiff += cycles;
420	while (audio->nextEvent <= 0) {
421		audio->nextEvent = INT_MAX;
422		if (audio->enable) {
423			audio->nextFrame -= audio->eventDiff;
424			int frame = -1;
425			if (audio->nextFrame <= 0) {
426				frame = (audio->frame + 1) & 7;
427				audio->frame = frame;
428				audio->nextFrame += FRAME_CYCLES;
429				if (audio->nextFrame < audio->nextEvent) {
430					audio->nextEvent = audio->nextFrame;
431				}
432			}
433
434			if (audio->playingCh1) {
435				audio->nextCh1 -= audio->eventDiff;
436				if (!audio->ch1.envelope.dead) {
437					if (frame == 7) {
438						--audio->ch1.envelope.nextStep;
439						if (audio->ch1.envelope.nextStep == 0) {
440							int8_t sample = audio->ch1.control.hi * 0x10 - 0x8;
441							_updateEnvelope(&audio->ch1.envelope);
442							audio->ch1.sample = sample * audio->ch1.envelope.currentVolume;
443						}
444					}
445				}
446
447				if (audio->ch1.sweepEnable && (frame & 3) == 2) {
448					--audio->ch1.sweepStep;
449					if (audio->ch1.sweepStep == 0) {
450						audio->playingCh1 = _updateSweep(&audio->ch1, false);
451					}
452				}
453
454				if (audio->nextCh1 <= 0) {
455					audio->nextCh1 += _updateChannel1(&audio->ch1);
456				}
457				if (audio->nextCh1 < audio->nextEvent) {
458					audio->nextEvent = audio->nextCh1;
459				}
460			}
461
462			if (audio->ch1.control.length && audio->ch1.control.stop && !(frame & 1)) {
463				--audio->ch1.control.length;
464				if (audio->ch1.control.length == 0) {
465					audio->playingCh1 = 0;
466				}
467			}
468
469			if (audio->playingCh2) {
470				audio->nextCh2 -= audio->eventDiff;
471				if (!audio->ch2.envelope.dead && frame == 7) {
472					--audio->ch2.envelope.nextStep;
473					if (audio->ch2.envelope.nextStep == 0) {
474						int8_t sample = audio->ch2.control.hi * 0x10 - 0x8;
475						_updateEnvelope(&audio->ch2.envelope);
476						audio->ch2.sample = sample * audio->ch2.envelope.currentVolume;
477					}
478				}
479
480				if (audio->nextCh2 <= 0) {
481					audio->nextCh2 += _updateChannel2(&audio->ch2);
482				}
483				if (audio->nextCh2 < audio->nextEvent) {
484					audio->nextEvent = audio->nextCh2;
485				}
486			}
487
488			if (audio->ch2.control.length && audio->ch2.control.stop && !(frame & 1)) {
489				--audio->ch2.control.length;
490				if (audio->ch2.control.length == 0) {
491					audio->playingCh2 = 0;
492				}
493			}
494
495			if (audio->playingCh3) {
496				audio->nextCh3 -= audio->eventDiff;
497				if (audio->nextCh3 <= 0) {
498					audio->nextCh3 += _updateChannel3(&audio->ch3);
499				}
500				if (audio->nextCh3 < audio->nextEvent) {
501					audio->nextEvent = audio->nextCh3;
502				}
503			}
504
505			if (audio->ch3.lengthShadow && audio->ch3.stop && !(frame & 1)) {
506				--audio->ch3.lengthShadow;
507				if (audio->ch3.lengthShadow == 0) {
508					audio->playingCh3 = 0;
509				}
510			}
511
512			if (audio->playingCh4) {
513				audio->nextCh4 -= audio->eventDiff;
514				if (!audio->ch4.envelope.dead && frame == 7) {
515					--audio->ch4.envelope.nextStep;
516					if (audio->ch4.envelope.nextStep == 0) {
517						int8_t sample = (audio->ch4.sample >> 31) * 0x8;
518						_updateEnvelope(&audio->ch4.envelope);
519						audio->ch4.sample = sample * audio->ch4.envelope.currentVolume;
520					}
521				}
522
523				if (audio->nextCh4 <= 0) {
524					audio->nextCh4 += _updateChannel4(&audio->ch4);
525				}
526				if (audio->nextCh4 < audio->nextEvent) {
527					audio->nextEvent = audio->nextCh4;
528				}
529			}
530
531			if (audio->ch4.length && audio->ch4.stop && !(frame & 1)) {
532				--audio->ch4.length;
533				if (audio->ch4.length == 0) {
534					audio->playingCh4 = 0;
535				}
536			}
537		}
538
539		if (audio->p) {
540			audio->p->memory.io[REG_NR52] &= ~0x000F;
541			audio->p->memory.io[REG_NR52] |= audio->playingCh1;
542			audio->p->memory.io[REG_NR52] |= audio->playingCh2 << 1;
543			audio->p->memory.io[REG_NR52] |= audio->playingCh3 << 2;
544			audio->p->memory.io[REG_NR52] |= audio->playingCh4 << 3;
545			audio->nextSample -= audio->eventDiff;
546			if (audio->nextSample <= 0) {
547				_sample(audio, audio->sampleInterval);
548				audio->nextSample += audio->sampleInterval;
549			}
550
551			if (audio->nextSample < audio->nextEvent) {
552				audio->nextEvent = audio->nextSample;
553			}
554		}
555		audio->eventDiff = 0;
556	}
557	return audio->nextEvent;
558}
559
560void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right) {
561	int sampleLeft = 0;
562	int sampleRight = 0;
563
564	if (audio->playingCh1 && !audio->forceDisableCh[0]) {
565		if (audio->ch1Left) {
566			sampleLeft += audio->ch1.sample;
567		}
568
569		if (audio->ch1Right) {
570			sampleRight += audio->ch1.sample;
571		}
572	}
573
574	if (audio->playingCh2 && !audio->forceDisableCh[1]) {
575		if (audio->ch2Left) {
576			sampleLeft += audio->ch2.sample;
577		}
578
579		if (audio->ch2Right) {
580			sampleRight += audio->ch2.sample;
581		}
582	}
583
584	if (audio->playingCh3 && !audio->forceDisableCh[2]) {
585		if (audio->ch3Left) {
586			sampleLeft += audio->ch3.sample;
587		}
588
589		if (audio->ch3Right) {
590			sampleRight += audio->ch3.sample;
591		}
592	}
593
594	if (audio->playingCh4 && !audio->forceDisableCh[3]) {
595		if (audio->ch4Left) {
596			sampleLeft += audio->ch4.sample;
597		}
598
599		if (audio->ch4Right) {
600			sampleRight += audio->ch4.sample;
601		}
602	}
603
604	*left = sampleLeft * (1 + audio->volumeLeft);
605	*right = sampleRight * (1 + audio->volumeRight);
606}
607
608void _sample(struct GBAudio* audio, int32_t cycles) {
609	int16_t sampleLeft = 0;
610	int16_t sampleRight = 0;
611	GBAudioSamplePSG(audio, &sampleLeft, &sampleRight);
612	sampleLeft <<= 1;
613	sampleRight <<= 1;
614
615	mCoreSyncLockAudio(audio->p->sync);
616	unsigned produced;
617	if ((size_t) blip_samples_avail(audio->left) < audio->samples) {
618		blip_add_delta(audio->left, audio->clock, sampleLeft - audio->lastLeft);
619		blip_add_delta(audio->right, audio->clock, sampleRight - audio->lastRight);
620		audio->lastLeft = sampleLeft;
621		audio->lastRight = sampleRight;
622		audio->clock += cycles;
623		if (audio->clock >= CLOCKS_PER_BLIP_FRAME) {
624			blip_end_frame(audio->left, audio->clock);
625			blip_end_frame(audio->right, audio->clock);
626			audio->clock -= CLOCKS_PER_BLIP_FRAME;
627		}
628	}
629	produced = blip_samples_avail(audio->left);
630	bool wait = produced >= audio->samples;
631	mCoreSyncProduceAudio(audio->p->sync, wait);
632	// TODO: Put AVStream back
633}
634
635void _writeDuty(struct GBAudioEnvelope* envelope, uint8_t value) {
636	envelope->length = GBAudioRegisterDutyGetLength(value);
637	envelope->duty = GBAudioRegisterDutyGetDuty(value);
638}
639
640bool _writeSweep(struct GBAudioEnvelope* envelope, uint8_t value) {
641	envelope->stepTime = GBAudioRegisterSweepGetStepTime(value);
642	envelope->direction = GBAudioRegisterSweepGetDirection(value);
643	envelope->initialVolume = GBAudioRegisterSweepGetInitialVolume(value);
644	envelope->dead = envelope->stepTime == 0;
645	envelope->nextStep = envelope->stepTime;
646	return envelope->initialVolume || envelope->direction;
647}
648
649static int32_t _updateSquareChannel(struct GBAudioSquareControl* control, int duty) {
650	control->hi = !control->hi;
651	int period = 4 * (2048 - control->frequency);
652	switch (duty) {
653	case 0:
654		return control->hi ? period : period * 7;
655	case 1:
656		return control->hi ? period * 2 : period * 6;
657	case 2:
658		return period * 4;
659	case 3:
660		return control->hi ? period * 6 : period * 2;
661	default:
662		// This should never be hit
663		return period * 4;
664	}
665}
666
667static void _updateEnvelope(struct GBAudioEnvelope* envelope) {
668	if (envelope->direction) {
669		++envelope->currentVolume;
670	} else {
671		--envelope->currentVolume;
672	}
673	if (envelope->currentVolume >= 15) {
674		envelope->currentVolume = 15;
675		envelope->dead = 1;
676	} else if (envelope->currentVolume <= 0) {
677		envelope->currentVolume = 0;
678		envelope->dead = 1;
679	} else {
680		envelope->nextStep = envelope->stepTime;
681	}
682}
683
684static bool _updateSweep(struct GBAudioChannel1* ch, bool initial) {
685	if (ch->direction) {
686		int frequency = ch->control.frequency;
687		frequency -= frequency >> ch->shift;
688		if (frequency >= 0) {
689			ch->control.frequency = frequency;
690		}
691	} else {
692		int frequency = ch->control.frequency;
693		frequency += frequency >> ch->shift;
694		if (frequency < 2048) {
695			if (!initial && ch->shift) {
696				ch->control.frequency = frequency;
697				if (!_updateSweep(ch, true)) {
698					return false;
699				}
700			}
701		} else {
702			return false;
703		}
704	}
705	ch->sweepStep = ch->time;
706	return true;
707}
708
709static int32_t _updateChannel1(struct GBAudioChannel1* ch) {
710	int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
711	ch->sample = ch->control.hi * 0x10 - 0x8;
712	ch->sample *= ch->envelope.currentVolume;
713	return timing;
714}
715
716static int32_t _updateChannel2(struct GBAudioChannel2* ch) {
717	int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
718	ch->sample = ch->control.hi * 0x10 - 0x8;
719	ch->sample *= ch->envelope.currentVolume;
720	return timing;
721}
722
723static int32_t _updateChannel3(struct GBAudioChannel3* ch) {
724	int i;
725	int start;
726	int end;
727	int volume;
728	switch (ch->volume) {
729	case 0:
730		volume = 0;
731		break;
732	case 1:
733		volume = 4;
734		break;
735	case 2:
736		volume = 2;
737		break;
738	case 3:
739		volume = 1;
740		break;
741	default:
742		volume = 3;
743		break;
744	}
745	if (ch->size) {
746		start = 7;
747		end = 0;
748	} else if (ch->bank) {
749		start = 7;
750		end = 4;
751	} else {
752		start = 3;
753		end = 0;
754	}
755	uint32_t bitsCarry = ch->wavedata[end] & 0x000000F0;
756	uint32_t bits;
757	for (i = start; i >= end; --i) {
758		bits = ch->wavedata[i] & 0x000000F0;
759		ch->wavedata[i] = ((ch->wavedata[i] & 0x0F0F0F0F) << 4) | ((ch->wavedata[i] & 0xF0F0F000) >> 12);
760		ch->wavedata[i] |= bitsCarry << 20;
761		bitsCarry = bits;
762	}
763	ch->sample = bitsCarry >> 4;
764	ch->sample -= 8;
765	ch->sample *= volume * 4;
766	return 2 * (2048 - ch->rate);
767}
768
769static int32_t _updateChannel4(struct GBAudioChannel4* ch) {
770	int lsb = ch->lfsr & 1;
771	ch->sample = lsb * 0x10 - 0x8;
772	ch->sample *= ch->envelope.currentVolume;
773	ch->lfsr >>= 1;
774	ch->lfsr ^= (lsb * 0x60) << (ch->power ? 0 : 8);
775	int timing = ch->ratio ? 2 * ch->ratio : 1;
776	timing <<= ch->frequency;
777	timing *= 8;
778	return timing;
779}