all repos — mgba @ b92482520c5361a1f9551d667ad662b4e4a1f57b

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