all repos — mgba @ 0d088f6e99dc571d3133e2bf9492e553c7a06d68

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