all repos — mgba @ c3c3bdc20c515892c9adf31336b928ac50bd5dc0

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}
 90
 91void GBAudioWriteNR12(struct GBAudio* audio, uint8_t value) {
 92	if (!_writeSweep(&audio->ch1.envelope, value)) {
 93		audio->ch1.sample = 0;
 94	}
 95}
 96
 97void GBAudioWriteNR13(struct GBAudio* audio, uint8_t value) {
 98	audio->ch1.control.frequency &= 0x700;
 99	audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value);
100}
101
102void GBAudioWriteNR14(struct GBAudio* audio, uint8_t value) {
103	audio->ch1.control.frequency &= 0xFF;
104	audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value << 8);
105	audio->ch1.control.stop = GBAudioRegisterControlGetStop(value << 8);
106	audio->ch1.control.endTime = (DMG_LR35902_FREQUENCY * (64 - audio->ch1.envelope.length)) >> 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}
136
137void GBAudioWriteNR22(struct GBAudio* audio, uint8_t value) {
138	if (!_writeSweep(&audio->ch2.envelope, value)) {
139		audio->ch2.sample = 0;
140	}
141}
142
143void GBAudioWriteNR23(struct GBAudio* audio, uint8_t value) {
144	audio->ch2.control.frequency &= 0x700;
145	audio->ch2.control.frequency |= GBAudioRegisterControlGetFrequency(value);
146}
147
148void GBAudioWriteNR24(struct GBAudio* audio, uint8_t value) {
149	audio->ch2.control.frequency &= 0xFF;
150	audio->ch2.control.frequency |= GBAudioRegisterControlGetFrequency(value << 8);
151	audio->ch2.control.stop = GBAudioRegisterControlGetStop(value << 8);
152	audio->ch2.control.endTime = (DMG_LR35902_FREQUENCY * (64 - audio->ch2.envelope.length)) >> 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}
184
185void GBAudioWriteNR32(struct GBAudio* audio, uint8_t value) {
186	audio->ch3.volume = GBAudioRegisterBankVolumeGetVolumeGB(value);
187}
188
189void GBAudioWriteNR33(struct GBAudio* audio, uint8_t value) {
190	audio->ch3.rate &= 0x700;
191	audio->ch3.rate |= GBAudioRegisterControlGetRate(value);
192}
193
194void GBAudioWriteNR34(struct GBAudio* audio, uint8_t value) {
195	audio->ch3.rate &= 0xFF;
196	audio->ch3.rate |= GBAudioRegisterControlGetRate(value << 8);
197	audio->ch3.stop = GBAudioRegisterControlGetStop(value << 8);
198	audio->ch3.endTime = (DMG_LR35902_FREQUENCY * (256 - audio->ch3.length)) >> 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}
214
215void GBAudioWriteNR42(struct GBAudio* audio, uint8_t value) {
216	if (!_writeSweep(&audio->ch4.envelope, value)) {
217		audio->ch4.sample = 0;
218	}
219}
220
221void GBAudioWriteNR43(struct GBAudio* audio, uint8_t value) {
222	audio->ch4.ratio = GBAudioRegisterNoiseFeedbackGetRatio(value);
223	audio->ch4.frequency = GBAudioRegisterNoiseFeedbackGetFrequency(value);
224	audio->ch4.power = GBAudioRegisterNoiseFeedbackGetPower(value);
225}
226
227void GBAudioWriteNR44(struct GBAudio* audio, uint8_t value) {
228	audio->ch4.stop = GBAudioRegisterNoiseControlGetStop(value);
229	audio->ch4.endTime = (DMG_LR35902_FREQUENCY * (64 - audio->ch4.envelope.length)) >> 8;
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}
275
276int32_t GBAudioProcessEvents(struct GBAudio* audio, int32_t cycles) {
277	if (audio->nextEvent == INT_MAX) {
278		return INT_MAX;
279	}
280	audio->nextEvent -= cycles;
281	audio->eventDiff += cycles;
282	while (audio->nextEvent <= 0) {
283		audio->nextEvent = INT_MAX;
284		if (audio->enable) {
285			if (audio->playingCh1 && !audio->ch1.envelope.dead) {
286				audio->nextCh1 -= audio->eventDiff;
287				if (audio->ch1.envelope.nextStep != INT_MAX) {
288					audio->ch1.envelope.nextStep -= audio->eventDiff;
289					if (audio->ch1.envelope.nextStep <= 0) {
290						int8_t sample = audio->ch1.control.hi * 0x10 - 0x8;
291						_updateEnvelope(&audio->ch1.envelope);
292						if (audio->ch1.envelope.nextStep < audio->nextEvent) {
293							audio->nextEvent = audio->ch1.envelope.nextStep;
294						}
295						audio->ch1.sample = sample * audio->ch1.envelope.currentVolume;
296					}
297				}
298
299				if (audio->ch1.nextSweep != INT_MAX) {
300					audio->ch1.nextSweep -= audio->eventDiff;
301					if (audio->ch1.nextSweep <= 0) {
302						audio->playingCh1 = _updateSweep(&audio->ch1);
303						if (audio->ch1.nextSweep < audio->nextEvent) {
304							audio->nextEvent = audio->ch1.nextSweep;
305						}
306					}
307				}
308
309				if (audio->nextCh1 <= 0) {
310					audio->nextCh1 += _updateChannel1(&audio->ch1);
311				}
312				if (audio->nextCh1 < audio->nextEvent) {
313					audio->nextEvent = audio->nextCh1;
314				}
315
316				if (audio->ch1.control.stop) {
317					audio->ch1.control.endTime -= audio->eventDiff;
318					if (audio->ch1.control.endTime <= 0) {
319						audio->playingCh1 = 0;
320					}
321				}
322			}
323
324			if (audio->playingCh2 && !audio->ch2.envelope.dead) {
325				audio->nextCh2 -= audio->eventDiff;
326				if (audio->ch2.envelope.nextStep != INT_MAX) {
327					audio->ch2.envelope.nextStep -= audio->eventDiff;
328					if (audio->ch2.envelope.nextStep <= 0) {
329						int8_t sample = audio->ch2.control.hi * 0x10 - 0x8;
330						_updateEnvelope(&audio->ch2.envelope);
331						if (audio->ch2.envelope.nextStep < audio->nextEvent) {
332							audio->nextEvent = audio->ch2.envelope.nextStep;
333						}
334						audio->ch2.sample = sample * audio->ch2.envelope.currentVolume;
335					}
336				}
337
338				if (audio->nextCh2 <= 0) {
339					audio->nextCh2 += _updateChannel2(&audio->ch2);
340				}
341				if (audio->nextCh2 < audio->nextEvent) {
342					audio->nextEvent = audio->nextCh2;
343				}
344
345				if (audio->ch2.control.stop) {
346					audio->ch2.control.endTime -= audio->eventDiff;
347					if (audio->ch2.control.endTime <= 0) {
348						audio->playingCh2 = 0;
349					}
350				}
351			}
352
353			if (audio->playingCh3) {
354				audio->nextCh3 -= audio->eventDiff;
355				if (audio->nextCh3 <= 0) {
356					audio->nextCh3 += _updateChannel3(&audio->ch3);
357				}
358				if (audio->nextCh3 < audio->nextEvent) {
359					audio->nextEvent = audio->nextCh3;
360				}
361
362				if (audio->ch3.stop) {
363					audio->ch3.endTime -= audio->eventDiff;
364					if (audio->ch3.endTime <= 0) {
365						audio->playingCh3 = 0;
366					}
367				}
368			}
369
370			if (audio->playingCh4 && !audio->ch4.envelope.dead) {
371				audio->nextCh4 -= audio->eventDiff;
372				if (audio->ch4.envelope.nextStep != INT_MAX) {
373					audio->ch4.envelope.nextStep -= audio->eventDiff;
374					if (audio->ch4.envelope.nextStep <= 0) {
375						int8_t sample = (audio->ch4.sample >> 31) * 0x8;
376						_updateEnvelope(&audio->ch4.envelope);
377						if (audio->ch4.envelope.nextStep < audio->nextEvent) {
378							audio->nextEvent = audio->ch4.envelope.nextStep;
379						}
380						audio->ch4.sample = sample * audio->ch4.envelope.currentVolume;
381					}
382				}
383
384				if (audio->nextCh4 <= 0) {
385					audio->nextCh4 += _updateChannel4(&audio->ch4);
386				}
387				if (audio->nextCh4 < audio->nextEvent) {
388					audio->nextEvent = audio->nextCh4;
389				}
390
391				if (audio->ch4.stop) {
392					audio->ch4.endTime -= audio->eventDiff;
393					if (audio->ch4.endTime <= 0) {
394						audio->playingCh4 = 0;
395					}
396				}
397			}
398		}
399
400		if (audio->p) {
401			audio->p->memory.io[REG_NR52] &= ~0x000F;
402			audio->p->memory.io[REG_NR52] |= audio->playingCh1;
403			audio->p->memory.io[REG_NR52] |= audio->playingCh2 << 1;
404			audio->p->memory.io[REG_NR52] |= audio->playingCh3 << 2;
405			audio->p->memory.io[REG_NR52] |= audio->playingCh4 << 3;
406			audio->nextSample -= audio->eventDiff;
407			if (audio->nextSample <= 0) {
408				_sample(audio, audio->sampleInterval);
409				audio->nextSample += audio->sampleInterval;
410			}
411
412			if (audio->nextSample < audio->nextEvent) {
413				audio->nextEvent = audio->nextSample;
414			}
415		}
416		audio->eventDiff = 0;
417	}
418	return audio->nextEvent;
419}
420
421void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right) {
422	int sampleLeft = 0;
423	int sampleRight = 0;
424
425	if (audio->playingCh1 && !audio->forceDisableCh[0]) {
426		if (audio->ch1Left) {
427			sampleLeft += audio->ch1.sample;
428		}
429
430		if (audio->ch1Right) {
431			sampleRight += audio->ch1.sample;
432		}
433	}
434
435	if (audio->playingCh2 && !audio->forceDisableCh[1]) {
436		if (audio->ch2Left) {
437			sampleLeft += audio->ch2.sample;
438		}
439
440		if (audio->ch2Right) {
441			sampleRight += audio->ch2.sample;
442		}
443	}
444
445	if (audio->playingCh3 && !audio->forceDisableCh[2]) {
446		if (audio->ch3Left) {
447			sampleLeft += audio->ch3.sample;
448		}
449
450		if (audio->ch3Right) {
451			sampleRight += audio->ch3.sample;
452		}
453	}
454
455	if (audio->playingCh4 && !audio->forceDisableCh[3]) {
456		if (audio->ch4Left) {
457			sampleLeft += audio->ch4.sample;
458		}
459
460		if (audio->ch4Right) {
461			sampleRight += audio->ch4.sample;
462		}
463	}
464
465	*left = sampleLeft * (1 + audio->volumeLeft);
466	*right = sampleRight * (1 + audio->volumeRight);
467}
468
469void _sample(struct GBAudio* audio, int32_t cycles) {
470	int16_t sampleLeft = 0;
471	int16_t sampleRight = 0;
472	GBAudioSamplePSG(audio, &sampleLeft, &sampleRight);
473	sampleLeft <<= 1;
474	sampleRight <<= 1;
475
476	mCoreSyncLockAudio(audio->p->sync);
477	unsigned produced;
478	if ((size_t) blip_samples_avail(audio->left) < audio->samples) {
479		blip_add_delta(audio->left, audio->clock, sampleLeft - audio->lastLeft);
480		blip_add_delta(audio->right, audio->clock, sampleRight - audio->lastRight);
481		audio->lastLeft = sampleLeft;
482		audio->lastRight = sampleRight;
483		audio->clock += cycles;
484		if (audio->clock >= CLOCKS_PER_FRAME) {
485			blip_end_frame(audio->left, audio->clock);
486			blip_end_frame(audio->right, audio->clock);
487			audio->clock -= CLOCKS_PER_FRAME;
488		}
489	}
490	produced = blip_samples_avail(audio->left);
491	bool wait = produced >= audio->samples;
492	mCoreSyncProduceAudio(audio->p->sync, wait);
493	// TODO: Put AVStream back
494}
495
496void _writeDuty(struct GBAudioEnvelope* envelope, uint8_t value) {
497	envelope->length = GBAudioRegisterDutyGetLength(value);
498	envelope->duty = GBAudioRegisterDutyGetDuty(value);
499}
500
501bool _writeSweep(struct GBAudioEnvelope* envelope, uint8_t value) {
502	envelope->stepTime = GBAudioRegisterSweepGetStepTime(value);
503	envelope->direction = GBAudioRegisterSweepGetDirection(value);
504	envelope->initialVolume = GBAudioRegisterSweepGetInitialVolume(value);
505	envelope->dead = 0;
506	if (envelope->stepTime) {
507		envelope->nextStep = 0;
508	} else {
509		envelope->nextStep = INT_MAX;
510		if (envelope->initialVolume == 0) {
511			envelope->dead = 1;
512			return false;
513		}
514	}
515	return true;
516}
517
518static int32_t _updateSquareChannel(struct GBAudioSquareControl* control, int duty) {
519	control->hi = !control->hi;
520	int period = 4 * (2048 - control->frequency);
521	switch (duty) {
522	case 0:
523		return control->hi ? period : period * 7;
524	case 1:
525		return control->hi ? period * 2 : period * 6;
526	case 2:
527		return period * 4;
528	case 3:
529		return control->hi ? period * 6 : period * 2;
530	default:
531		// This should never be hit
532		return period * 4;
533	}
534}
535
536static void _updateEnvelope(struct GBAudioEnvelope* envelope) {
537	if (envelope->direction) {
538		++envelope->currentVolume;
539	} else {
540		--envelope->currentVolume;
541	}
542	if (envelope->currentVolume >= 15) {
543		envelope->currentVolume = 15;
544		envelope->nextStep = INT_MAX;
545	} else if (envelope->currentVolume <= 0) {
546		envelope->currentVolume = 0;
547		envelope->dead = 1;
548		envelope->nextStep = INT_MAX;
549	} else {
550		envelope->nextStep += envelope->stepTime * (DMG_LR35902_FREQUENCY >> 6);
551	}
552}
553
554static bool _updateSweep(struct GBAudioChannel1* ch) {
555	if (ch->direction) {
556		int frequency = ch->control.frequency;
557		frequency -= frequency >> ch->shift;
558		if (frequency >= 0) {
559			ch->control.frequency = frequency;
560		}
561	} else {
562		int frequency = ch->control.frequency;
563		frequency += frequency >> ch->shift;
564		if (frequency < 2048) {
565			ch->control.frequency = frequency;
566		} else {
567			return false;
568		}
569	}
570	ch->nextSweep += ch->time * SWEEP_CYCLES;
571	return true;
572}
573
574static int32_t _updateChannel1(struct GBAudioChannel1* ch) {
575	int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
576	ch->sample = ch->control.hi * 0x10 - 0x8;
577	ch->sample *= ch->envelope.currentVolume;
578	return timing;
579}
580
581static int32_t _updateChannel2(struct GBAudioChannel2* ch) {
582	int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
583	ch->sample = ch->control.hi * 0x10 - 0x8;
584	ch->sample *= ch->envelope.currentVolume;
585	return timing;
586}
587
588static int32_t _updateChannel3(struct GBAudioChannel3* ch) {
589	int i;
590	int start;
591	int end;
592	int volume;
593	switch (ch->volume) {
594	case 0:
595		volume = 0;
596		break;
597	case 1:
598		volume = 4;
599		break;
600	case 2:
601		volume = 2;
602		break;
603	case 3:
604		volume = 1;
605		break;
606	default:
607		volume = 3;
608		break;
609	}
610	if (ch->size) {
611		start = 7;
612		end = 0;
613	} else if (ch->bank) {
614		start = 7;
615		end = 4;
616	} else {
617		start = 3;
618		end = 0;
619	}
620	uint32_t bitsCarry = ch->wavedata[end] & 0x000000F0;
621	uint32_t bits;
622	for (i = start; i >= end; --i) {
623		bits = ch->wavedata[i] & 0x000000F0;
624		ch->wavedata[i] = ((ch->wavedata[i] & 0x0F0F0F0F) << 4) | ((ch->wavedata[i] & 0xF0F0F000) >> 12);
625		ch->wavedata[i] |= bitsCarry << 20;
626		bitsCarry = bits;
627	}
628	ch->sample = bitsCarry >> 4;
629	ch->sample -= 8;
630	ch->sample *= volume * 4;
631	return 2 * (2048 - ch->rate);
632}
633
634static int32_t _updateChannel4(struct GBAudioChannel4* ch) {
635	int lsb = ch->lfsr & 1;
636	ch->sample = lsb * 0x10 - 0x8;
637	ch->sample *= ch->envelope.currentVolume;
638	ch->lfsr >>= 1;
639	ch->lfsr ^= (lsb * 0x60) << (ch->power ? 0 : 8);
640	int timing = ch->ratio ? 2 * ch->ratio : 1;
641	timing <<= ch->frequency;
642	timing *= 8;
643	return timing;
644}