all repos — mgba @ ff637c84d1f229b0891e0d7f9a40ad5c7a8b12f0

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 = value;
235	audio->ch3.lengthShadow = 256 - value;
236}
237
238void GBAudioWriteNR32(struct GBAudio* audio, uint8_t value) {
239	audio->ch3.volume = GBAudioRegisterBankVolumeGetVolumeGB(value);
240}
241
242void GBAudioWriteNR33(struct GBAudio* audio, uint8_t value) {
243	audio->ch3.rate &= 0x700;
244	audio->ch3.rate |= GBAudioRegisterControlGetRate(value);
245}
246
247void GBAudioWriteNR34(struct GBAudio* audio, uint8_t value) {
248	audio->ch3.rate &= 0xFF;
249	audio->ch3.rate |= GBAudioRegisterControlGetRate(value << 8);
250	bool wasStop = audio->ch3.stop;
251	audio->ch3.stop = GBAudioRegisterControlGetStop(value << 8);
252	if (!wasStop && audio->ch3.stop && audio->ch3.lengthShadow && !(audio->frame & 1)) {
253		--audio->ch3.lengthShadow;
254		if (audio->ch3.lengthShadow == 0) {
255			audio->playingCh3 = false;
256		}
257	}
258	if (GBAudioRegisterControlIsRestart(value << 8)) {
259		audio->playingCh3 = audio->ch3.enable;
260		if (!audio->ch3.lengthShadow) {
261			audio->ch3.lengthShadow = 256;
262			if (audio->ch3.stop && !(audio->frame & 1)) {
263				--audio->ch3.lengthShadow;
264			}
265		}
266	}
267	if (audio->playingCh3) {
268		if (audio->nextEvent == INT_MAX) {
269			audio->eventDiff = 0;
270		}
271		audio->nextCh3 = audio->eventDiff;
272		audio->nextEvent = audio->eventDiff;
273		if (audio->p) {
274			// TODO: Don't need p
275			audio->p->cpu->nextEvent = audio->eventDiff;
276		}
277	}
278	*audio->nr52 &= ~0x0004;
279	*audio->nr52 |= audio->playingCh3 << 2;
280}
281
282void GBAudioWriteNR41(struct GBAudio* audio, uint8_t value) {
283	_writeDuty(&audio->ch4.envelope, value);
284	audio->ch4.length = 64 - audio->ch4.envelope.length;
285}
286
287void GBAudioWriteNR42(struct GBAudio* audio, uint8_t value) {
288	if (!_writeSweep(&audio->ch4.envelope, value)) {
289		audio->playingCh4 = false;
290		*audio->nr52 &= ~0x0008;
291	}
292}
293
294void GBAudioWriteNR43(struct GBAudio* audio, uint8_t value) {
295	audio->ch4.ratio = GBAudioRegisterNoiseFeedbackGetRatio(value);
296	audio->ch4.frequency = GBAudioRegisterNoiseFeedbackGetFrequency(value);
297	audio->ch4.power = GBAudioRegisterNoiseFeedbackGetPower(value);
298}
299
300void GBAudioWriteNR44(struct GBAudio* audio, uint8_t value) {
301	bool wasStop = audio->ch4.stop;
302	audio->ch4.stop = GBAudioRegisterNoiseControlGetStop(value);
303	if (!wasStop && audio->ch4.stop && audio->ch4.length && !(audio->frame & 1)) {
304		--audio->ch4.length;
305		if (audio->ch4.length == 0) {
306			audio->playingCh4 = false;
307		}
308	}
309	if (GBAudioRegisterNoiseControlIsRestart(value)) {
310		audio->playingCh4 = audio->ch4.envelope.initialVolume || audio->ch4.envelope.direction;
311		audio->ch4.envelope.currentVolume = audio->ch4.envelope.initialVolume;
312		if (audio->ch4.envelope.currentVolume > 0 && audio->ch4.envelope.stepTime) {
313			audio->ch4.envelope.dead = 0;
314		}
315		if (audio->ch4.power) {
316			audio->ch4.lfsr = 0x40;
317		} else {
318			audio->ch4.lfsr = 0x4000;
319		}
320		if (audio->nextEvent == INT_MAX) {
321			audio->eventDiff = 0;
322		}
323		if (!audio->playingCh4) {
324			audio->nextCh4 = audio->eventDiff;
325		}
326		if (!audio->ch4.length) {
327			audio->ch4.length = 64;
328			if (audio->ch4.stop && !(audio->frame & 1)) {
329				--audio->ch4.length;
330			}
331		}
332		audio->nextEvent = audio->eventDiff;
333		if (audio->p) {
334			// TODO: Don't need p
335			audio->p->cpu->nextEvent = audio->eventDiff;
336		}
337	}
338	*audio->nr52 &= ~0x0008;
339	*audio->nr52 |= audio->playingCh4 << 3;
340}
341
342void GBAudioWriteNR50(struct GBAudio* audio, uint8_t value) {
343	audio->volumeRight = GBRegisterNR50GetVolumeRight(value);
344	audio->volumeLeft = GBRegisterNR50GetVolumeLeft(value);
345}
346
347void GBAudioWriteNR51(struct GBAudio* audio, uint8_t value) {
348	audio->ch1Right = GBRegisterNR51GetCh1Right(value);
349	audio->ch2Right = GBRegisterNR51GetCh2Right(value);
350	audio->ch3Right = GBRegisterNR51GetCh3Right(value);
351	audio->ch4Right = GBRegisterNR51GetCh4Right(value);
352	audio->ch1Left = GBRegisterNR51GetCh1Left(value);
353	audio->ch2Left = GBRegisterNR51GetCh2Left(value);
354	audio->ch3Left = GBRegisterNR51GetCh3Left(value);
355	audio->ch4Left = GBRegisterNR51GetCh4Left(value);
356}
357
358void GBAudioWriteNR52(struct GBAudio* audio, uint8_t value) {
359	bool wasEnable = audio->enable;
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		}
408		*audio->nr52 &= ~0x000F;
409	} else if (!wasEnable) {
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 && frame == 7) {
437					--audio->ch1.envelope.nextStep;
438					if (audio->ch1.envelope.nextStep == 0) {
439						int8_t sample = audio->ch1.control.hi * 0x10 - 0x8;
440						_updateEnvelope(&audio->ch1.envelope);
441						audio->ch1.sample = sample * audio->ch1.envelope.currentVolume;
442					}
443				}
444
445				if (audio->ch1.sweepEnable && (frame & 3) == 2) {
446					--audio->ch1.sweepStep;
447					if (audio->ch1.sweepStep == 0) {
448						audio->playingCh1 = _updateSweep(&audio->ch1, false);
449					}
450				}
451
452				if (audio->ch1.envelope.dead != 2) {
453					if (audio->nextCh1 <= 0) {
454						audio->nextCh1 += _updateChannel1(&audio->ch1);
455					}
456					if (audio->nextCh1 < audio->nextEvent) {
457						audio->nextEvent = audio->nextCh1;
458					}
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->ch2.envelope.dead != 2) {
481					if (audio->nextCh2 <= 0) {
482						audio->nextCh2 += _updateChannel2(&audio->ch2);
483					}
484					if (audio->nextCh2 < audio->nextEvent) {
485						audio->nextEvent = audio->nextCh2;
486					}
487				}
488			}
489
490			if (audio->ch2.control.length && audio->ch2.control.stop && !(frame & 1)) {
491				--audio->ch2.control.length;
492				if (audio->ch2.control.length == 0) {
493					audio->playingCh2 = 0;
494				}
495			}
496
497			if (audio->playingCh3) {
498				audio->nextCh3 -= audio->eventDiff;
499				if (audio->nextCh3 <= 0) {
500					audio->nextCh3 += _updateChannel3(&audio->ch3);
501				}
502				if (audio->nextCh3 < audio->nextEvent) {
503					audio->nextEvent = audio->nextCh3;
504				}
505			}
506
507			if (audio->ch3.lengthShadow && audio->ch3.stop && !(frame & 1)) {
508				--audio->ch3.lengthShadow;
509				if (audio->ch3.lengthShadow == 0) {
510					audio->playingCh3 = 0;
511				}
512			}
513
514			if (audio->playingCh4) {
515				audio->nextCh4 -= audio->eventDiff;
516				if (!audio->ch4.envelope.dead && frame == 7) {
517					--audio->ch4.envelope.nextStep;
518					if (audio->ch4.envelope.nextStep == 0) {
519						int8_t sample = (audio->ch4.sample >> 31) * 0x8;
520						_updateEnvelope(&audio->ch4.envelope);
521						audio->ch4.sample = sample * audio->ch4.envelope.currentVolume;
522					}
523				}
524
525				if (audio->ch4.envelope.dead != 2) {
526					if (audio->nextCh4 <= 0) {
527						int32_t timing = _updateChannel4(&audio->ch4);
528						if (audio->nextCh4 < -timing) {
529							int32_t bound = timing * 16;
530							// Perform negative modulo to cap to 16 iterations
531							audio->nextCh4 = bound - (audio->nextCh4 - 1) % bound - 1;
532						}
533						audio->nextCh4 += timing;
534					}
535					if (audio->nextCh4 < audio->nextEvent) {
536						audio->nextEvent = audio->nextCh4;
537					}
538				}
539			}
540
541			if (audio->ch4.length && audio->ch4.stop && !(frame & 1)) {
542				--audio->ch4.length;
543				if (audio->ch4.length == 0) {
544					audio->playingCh4 = 0;
545				}
546			}
547		}
548
549		*audio->nr52 &= ~0x000F;
550		*audio->nr52 |= audio->playingCh1;
551		*audio->nr52 |= audio->playingCh2 << 1;
552		*audio->nr52 |= audio->playingCh3 << 2;
553		*audio->nr52 |= audio->playingCh4 << 3;
554
555		if (audio->p) {
556			audio->nextSample -= audio->eventDiff;
557			if (audio->nextSample <= 0) {
558				_sample(audio, audio->sampleInterval);
559				audio->nextSample += audio->sampleInterval;
560			}
561
562			if (audio->nextSample < audio->nextEvent) {
563				audio->nextEvent = audio->nextSample;
564			}
565		}
566		audio->eventDiff = 0;
567	}
568	return audio->nextEvent;
569}
570
571void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right) {
572	int sampleLeft = 0;
573	int sampleRight = 0;
574
575	if (audio->playingCh1 && !audio->forceDisableCh[0]) {
576		if (audio->ch1Left) {
577			sampleLeft += audio->ch1.sample;
578		}
579
580		if (audio->ch1Right) {
581			sampleRight += audio->ch1.sample;
582		}
583	}
584
585	if (audio->playingCh2 && !audio->forceDisableCh[1]) {
586		if (audio->ch2Left) {
587			sampleLeft += audio->ch2.sample;
588		}
589
590		if (audio->ch2Right) {
591			sampleRight += audio->ch2.sample;
592		}
593	}
594
595	if (audio->playingCh3 && !audio->forceDisableCh[2]) {
596		if (audio->ch3Left) {
597			sampleLeft += audio->ch3.sample;
598		}
599
600		if (audio->ch3Right) {
601			sampleRight += audio->ch3.sample;
602		}
603	}
604
605	if (audio->playingCh4 && !audio->forceDisableCh[3]) {
606		if (audio->ch4Left) {
607			sampleLeft += audio->ch4.sample;
608		}
609
610		if (audio->ch4Right) {
611			sampleRight += audio->ch4.sample;
612		}
613	}
614
615	*left = sampleLeft * (1 + audio->volumeLeft);
616	*right = sampleRight * (1 + audio->volumeRight);
617}
618
619void _sample(struct GBAudio* audio, int32_t cycles) {
620	int16_t sampleLeft = 0;
621	int16_t sampleRight = 0;
622	GBAudioSamplePSG(audio, &sampleLeft, &sampleRight);
623	sampleLeft = (sampleLeft * audio->masterVolume) >> 6;
624	sampleRight = (sampleRight * audio->masterVolume) >> 6;
625
626	mCoreSyncLockAudio(audio->p->sync);
627	unsigned produced;
628	if ((size_t) blip_samples_avail(audio->left) < audio->samples) {
629		blip_add_delta(audio->left, audio->clock, sampleLeft - audio->lastLeft);
630		blip_add_delta(audio->right, audio->clock, sampleRight - audio->lastRight);
631		audio->lastLeft = sampleLeft;
632		audio->lastRight = sampleRight;
633		audio->clock += cycles;
634		if (audio->clock >= CLOCKS_PER_BLIP_FRAME) {
635			blip_end_frame(audio->left, audio->clock);
636			blip_end_frame(audio->right, audio->clock);
637			audio->clock -= CLOCKS_PER_BLIP_FRAME;
638		}
639	}
640	produced = blip_samples_avail(audio->left);
641	bool wait = produced >= audio->samples;
642	mCoreSyncProduceAudio(audio->p->sync, wait);
643	// TODO: Put AVStream back
644}
645
646void _writeDuty(struct GBAudioEnvelope* envelope, uint8_t value) {
647	envelope->length = GBAudioRegisterDutyGetLength(value);
648	envelope->duty = GBAudioRegisterDutyGetDuty(value);
649}
650
651bool _writeSweep(struct GBAudioEnvelope* envelope, uint8_t value) {
652	envelope->stepTime = GBAudioRegisterSweepGetStepTime(value);
653	envelope->direction = GBAudioRegisterSweepGetDirection(value);
654	envelope->initialVolume = GBAudioRegisterSweepGetInitialVolume(value);
655	if (envelope->stepTime == 0) {
656		envelope->dead = envelope->currentVolume ? 1 : 2;
657	} else if (!envelope->direction && !envelope->currentVolume) {
658		envelope->dead = 2;
659	} else if (envelope->direction && envelope->currentVolume == 0xF) {
660		envelope->dead = 1;
661	} else {
662		envelope->dead = 0;
663	}
664	envelope->nextStep = envelope->stepTime;
665	return envelope->initialVolume || envelope->direction;
666}
667
668static int32_t _updateSquareChannel(struct GBAudioSquareControl* control, int duty) {
669	control->hi = !control->hi;
670	int period = 4 * (2048 - control->frequency);
671	switch (duty) {
672	case 0:
673		return control->hi ? period : period * 7;
674	case 1:
675		return control->hi ? period * 2 : period * 6;
676	case 2:
677		return period * 4;
678	case 3:
679		return control->hi ? period * 6 : period * 2;
680	default:
681		// This should never be hit
682		return period * 4;
683	}
684}
685
686static void _updateEnvelope(struct GBAudioEnvelope* envelope) {
687	if (envelope->direction) {
688		++envelope->currentVolume;
689	} else {
690		--envelope->currentVolume;
691	}
692	if (envelope->currentVolume >= 15) {
693		envelope->currentVolume = 15;
694		envelope->dead = 1;
695	} else if (envelope->currentVolume <= 0) {
696		envelope->currentVolume = 0;
697		envelope->dead = 2;
698	} else {
699		envelope->nextStep = envelope->stepTime;
700	}
701}
702
703static bool _updateSweep(struct GBAudioChannel1* ch, bool initial) {
704	if (initial || ch->time != 8) {
705		int frequency = ch->control.frequency;
706		if (ch->direction) {
707			frequency -= frequency >> ch->shift;
708			if (!initial && frequency >= 0) {
709				ch->control.frequency = frequency;
710				ch->realFrequency = frequency;
711			}
712		} else {
713			frequency += frequency >> ch->shift;
714			if (frequency < 2048) {
715				if (!initial && ch->shift) {
716					ch->control.frequency = frequency;
717					if (!_updateSweep(ch, true)) {
718						return false;
719					}
720					ch->realFrequency = frequency;
721				}
722			} else {
723				return false;
724			}
725		}
726		ch->sweepOccurred = true;
727	}
728	ch->sweepStep = ch->time;
729	return true;
730}
731
732static int32_t _updateChannel1(struct GBAudioChannel1* ch) {
733	int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
734	ch->sample = ch->control.hi * 0x10 - 0x8;
735	ch->sample *= ch->envelope.currentVolume;
736	return timing;
737}
738
739static int32_t _updateChannel2(struct GBAudioChannel2* ch) {
740	int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
741	ch->sample = ch->control.hi * 0x10 - 0x8;
742	ch->sample *= ch->envelope.currentVolume;
743	return timing;
744}
745
746static int32_t _updateChannel3(struct GBAudioChannel3* ch) {
747	int i;
748	int start;
749	int end;
750	int volume;
751	switch (ch->volume) {
752	case 0:
753		volume = 0;
754		break;
755	case 1:
756		volume = 4;
757		break;
758	case 2:
759		volume = 2;
760		break;
761	case 3:
762		volume = 1;
763		break;
764	default:
765		volume = 3;
766		break;
767	}
768	if (ch->size) {
769		start = 7;
770		end = 0;
771	} else if (ch->bank) {
772		start = 7;
773		end = 4;
774	} else {
775		start = 3;
776		end = 0;
777	}
778	uint32_t bitsCarry = ch->wavedata[end] & 0x000000F0;
779	uint32_t bits;
780	for (i = start; i >= end; --i) {
781		bits = ch->wavedata[i] & 0x000000F0;
782		ch->wavedata[i] = ((ch->wavedata[i] & 0x0F0F0F0F) << 4) | ((ch->wavedata[i] & 0xF0F0F000) >> 12);
783		ch->wavedata[i] |= bitsCarry << 20;
784		bitsCarry = bits;
785	}
786	ch->sample = bitsCarry >> 4;
787	ch->sample -= 8;
788	ch->sample *= volume * 4;
789	return 2 * (2048 - ch->rate);
790}
791
792static int32_t _updateChannel4(struct GBAudioChannel4* ch) {
793	int lsb = ch->lfsr & 1;
794	ch->sample = lsb * 0x10 - 0x8;
795	ch->sample *= ch->envelope.currentVolume;
796	ch->lfsr >>= 1;
797	ch->lfsr ^= (lsb * 0x60) << (ch->power ? 0 : 8);
798	int timing = ch->ratio ? 2 * ch->ratio : 1;
799	timing <<= ch->frequency;
800	timing *= 8;
801	return timing;
802}