all repos — mgba @ c60251de732a63ce3fa4c8723fbb429fb4a35fac

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