all repos — mgba @ 56e40b118cadc6ee1689b02f6a72a49de50d462e

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 <mgba/internal/gb/audio.h>
   7
   8#include <mgba/core/blip_buf.h>
   9#include <mgba/core/interface.h>
  10#include <mgba/core/sync.h>
  11#include <mgba/internal/gb/gb.h>
  12#include <mgba/internal/gb/serialize.h>
  13#include <mgba/internal/gb/io.h>
  14
  15#ifdef _3DS
  16#define blip_add_delta blip_add_delta_fast
  17#endif
  18
  19#define FRAME_CYCLES (DMG_LR35902_FREQUENCY >> 9)
  20
  21const uint32_t DMG_LR35902_FREQUENCY = 0x400000;
  22static const int CLOCKS_PER_BLIP_FRAME = 0x1000;
  23static const unsigned BLIP_BUFFER_SIZE = 0x4000;
  24const int GB_AUDIO_VOLUME_MAX = 0x100;
  25
  26static bool _writeSweep(struct GBAudioSweep* sweep, uint8_t value);
  27static void _writeDuty(struct GBAudioEnvelope* envelope, uint8_t value);
  28static bool _writeEnvelope(struct GBAudioEnvelope* envelope, uint8_t value, enum GBAudioStyle style);
  29
  30static void _resetSweep(struct GBAudioSweep* sweep);
  31static bool _resetEnvelope(struct GBAudioEnvelope* sweep);
  32
  33static void _updateEnvelope(struct GBAudioEnvelope* envelope);
  34static void _updateEnvelopeDead(struct GBAudioEnvelope* envelope);
  35static bool _updateSweep(struct GBAudioSquareChannel* sweep, bool initial);
  36
  37static void _updateSquareSample(struct GBAudioSquareChannel* ch);
  38static int32_t _updateSquareChannel(struct GBAudioSquareChannel* ch);
  39
  40static void _updateFrame(struct mTiming* timing, void* user, uint32_t cyclesLate);
  41static void _updateChannel1(struct mTiming* timing, void* user, uint32_t cyclesLate);
  42static void _updateChannel2(struct mTiming* timing, void* user, uint32_t cyclesLate);
  43static void _updateChannel3(struct mTiming* timing, void* user, uint32_t cyclesLate);
  44static void _fadeChannel3(struct mTiming* timing, void* user, uint32_t cyclesLate);
  45static void _updateChannel4(struct mTiming* timing, void* user, uint32_t cyclesLate);
  46static void _sample(struct mTiming* timing, void* user, uint32_t cyclesLate);
  47
  48void GBAudioInit(struct GBAudio* audio, size_t samples, uint8_t* nr52, enum GBAudioStyle style) {
  49	audio->samples = samples;
  50	audio->left = blip_new(BLIP_BUFFER_SIZE);
  51	audio->right = blip_new(BLIP_BUFFER_SIZE);
  52	audio->clockRate = DMG_LR35902_FREQUENCY;
  53	// Guess too large; we hang producing extra samples if we guess too low
  54	blip_set_rates(audio->left, DMG_LR35902_FREQUENCY, 96000);
  55	blip_set_rates(audio->right, DMG_LR35902_FREQUENCY, 96000);
  56	audio->forceDisableCh[0] = false;
  57	audio->forceDisableCh[1] = false;
  58	audio->forceDisableCh[2] = false;
  59	audio->forceDisableCh[3] = false;
  60	audio->masterVolume = GB_AUDIO_VOLUME_MAX;
  61	audio->nr52 = nr52;
  62	audio->style = style;
  63	if (style == GB_AUDIO_GBA) {
  64		audio->timingFactor = 4;
  65	} else {
  66		audio->timingFactor = 1;
  67	}
  68
  69	audio->frameEvent.context = audio;
  70	audio->frameEvent.name = "GB Audio Frame Sequencer";
  71	audio->frameEvent.callback = _updateFrame;
  72	audio->frameEvent.priority = 0x10;
  73	audio->ch1Event.context = audio;
  74	audio->ch1Event.name = "GB Audio Channel 1";
  75	audio->ch1Event.callback = _updateChannel1;
  76	audio->ch1Event.priority = 0x11;
  77	audio->ch2Event.context = audio;
  78	audio->ch2Event.name = "GB Audio Channel 2";
  79	audio->ch2Event.callback = _updateChannel2;
  80	audio->ch2Event.priority = 0x12;
  81	audio->ch3Event.context = audio;
  82	audio->ch3Event.name = "GB Audio Channel 3";
  83	audio->ch3Event.callback = _updateChannel3;
  84	audio->ch3Event.priority = 0x13;
  85	audio->ch3Fade.context = audio;
  86	audio->ch3Fade.name = "GB Audio Channel 3 Memory";
  87	audio->ch3Fade.callback = _fadeChannel3;
  88	audio->ch3Fade.priority = 0x14;
  89	audio->ch4Event.context = audio;
  90	audio->ch4Event.name = "GB Audio Channel 4";
  91	audio->ch4Event.callback = _updateChannel4;
  92	audio->ch4Event.priority = 0x15;
  93	audio->sampleEvent.context = audio;
  94	audio->sampleEvent.name = "GB Audio Sample";
  95	audio->sampleEvent.callback = _sample;
  96	audio->ch1Event.priority = 0x18;
  97}
  98
  99void GBAudioDeinit(struct GBAudio* audio) {
 100	blip_delete(audio->left);
 101	blip_delete(audio->right);
 102}
 103
 104void GBAudioReset(struct GBAudio* audio) {
 105	mTimingDeschedule(audio->timing, &audio->frameEvent);
 106	mTimingDeschedule(audio->timing, &audio->ch1Event);
 107	mTimingDeschedule(audio->timing, &audio->ch2Event);
 108	mTimingDeschedule(audio->timing, &audio->ch3Event);
 109	mTimingDeschedule(audio->timing, &audio->ch3Fade);
 110	mTimingDeschedule(audio->timing, &audio->ch4Event);
 111	mTimingDeschedule(audio->timing, &audio->sampleEvent);
 112	if (audio->style != GB_AUDIO_GBA) {
 113		mTimingSchedule(audio->timing, &audio->sampleEvent, 0);
 114	}
 115	if (audio->style == GB_AUDIO_GBA) {
 116		mTimingSchedule(audio->timing, &audio->frameEvent, 0);
 117	}
 118	audio->ch1 = (struct GBAudioSquareChannel) { .envelope = { .dead = 2 } };
 119	audio->ch2 = (struct GBAudioSquareChannel) { .envelope = { .dead = 2 } };
 120	audio->ch3 = (struct GBAudioWaveChannel) { .bank = 0 };
 121	// TODO: DMG randomness
 122	audio->ch3.wavedata8[0] = 0x00;
 123	audio->ch3.wavedata8[1] = 0xFF;
 124	audio->ch3.wavedata8[2] = 0x00;
 125	audio->ch3.wavedata8[3] = 0xFF;
 126	audio->ch3.wavedata8[4] = 0x00;
 127	audio->ch3.wavedata8[5] = 0xFF;
 128	audio->ch3.wavedata8[6] = 0x00;
 129	audio->ch3.wavedata8[7] = 0xFF;
 130	audio->ch3.wavedata8[8] = 0x00;
 131	audio->ch3.wavedata8[9] = 0xFF;
 132	audio->ch3.wavedata8[10] = 0x00;
 133	audio->ch3.wavedata8[11] = 0xFF;
 134	audio->ch3.wavedata8[12] = 0x00;
 135	audio->ch3.wavedata8[13] = 0xFF;
 136	audio->ch3.wavedata8[14] = 0x00;
 137	audio->ch3.wavedata8[15] = 0xFF;
 138	audio->ch4 = (struct GBAudioNoiseChannel) { .envelope = { .dead = 2 } };
 139	audio->frame = 0;
 140	audio->sampleInterval = 128;
 141	audio->lastLeft = 0;
 142	audio->lastRight = 0;
 143	audio->capLeft = 0;
 144	audio->capRight = 0;
 145	audio->clock = 0;
 146	audio->volumeRight = 0;
 147	audio->volumeLeft = 0;
 148	audio->ch1Right = false;
 149	audio->ch2Right = false;
 150	audio->ch3Right = false;
 151	audio->ch4Right = false;
 152	audio->ch1Left = false;
 153	audio->ch2Left = false;
 154	audio->ch3Left = false;
 155	audio->ch4Left = false;
 156	audio->playingCh1 = false;
 157	audio->playingCh2 = false;
 158	audio->playingCh3 = false;
 159	audio->playingCh4 = false;
 160	if (audio->p && (audio->p->model == GB_MODEL_DMG || audio->p->model == GB_MODEL_CGB)) {
 161		audio->playingCh1 = true;
 162		*audio->nr52 |= 0x01;
 163	}
 164}
 165
 166void GBAudioResizeBuffer(struct GBAudio* audio, size_t samples) {
 167	mCoreSyncLockAudio(audio->p->sync);
 168	audio->samples = samples;
 169	blip_clear(audio->left);
 170	blip_clear(audio->right);
 171	audio->clock = 0;
 172	mCoreSyncConsumeAudio(audio->p->sync);
 173}
 174
 175void GBAudioWriteNR10(struct GBAudio* audio, uint8_t value) {
 176	if (!_writeSweep(&audio->ch1.sweep, value)) {
 177		mTimingDeschedule(audio->timing, &audio->ch1Event);
 178		audio->playingCh1 = false;
 179		*audio->nr52 &= ~0x0001;
 180	}
 181}
 182
 183void GBAudioWriteNR11(struct GBAudio* audio, uint8_t value) {
 184	_writeDuty(&audio->ch1.envelope, value);
 185	audio->ch1.control.length = 64 - audio->ch1.envelope.length;
 186}
 187
 188void GBAudioWriteNR12(struct GBAudio* audio, uint8_t value) {
 189	if (!_writeEnvelope(&audio->ch1.envelope, value, audio->style)) {
 190		mTimingDeschedule(audio->timing, &audio->ch1Event);
 191		audio->playingCh1 = false;
 192		*audio->nr52 &= ~0x0001;
 193	}
 194}
 195
 196void GBAudioWriteNR13(struct GBAudio* audio, uint8_t value) {
 197	audio->ch1.control.frequency &= 0x700;
 198	audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value);
 199}
 200
 201void GBAudioWriteNR14(struct GBAudio* audio, uint8_t value) {
 202	audio->ch1.control.frequency &= 0xFF;
 203	audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value << 8);
 204	bool wasStop = audio->ch1.control.stop;
 205	audio->ch1.control.stop = GBAudioRegisterControlGetStop(value << 8);
 206	if (!wasStop && audio->ch1.control.stop && audio->ch1.control.length && !(audio->frame & 1)) {
 207		--audio->ch1.control.length;
 208		if (audio->ch1.control.length == 0) {
 209			mTimingDeschedule(audio->timing, &audio->ch1Event);
 210			audio->playingCh1 = false;
 211		}
 212	}
 213	if (GBAudioRegisterControlIsRestart(value << 8)) {
 214		audio->playingCh1 = _resetEnvelope(&audio->ch1.envelope);
 215
 216		if (audio->playingCh1) {
 217			_updateSquareSample(&audio->ch1);
 218		}
 219
 220		audio->ch1.sweep.realFrequency = audio->ch1.control.frequency;
 221		_resetSweep(&audio->ch1.sweep);
 222		if (audio->playingCh1 && audio->ch1.sweep.shift) {
 223			audio->playingCh1 = _updateSweep(&audio->ch1, true);
 224		}
 225		if (!audio->ch1.control.length) {
 226			audio->ch1.control.length = 64;
 227			if (audio->ch1.control.stop && !(audio->frame & 1)) {
 228				--audio->ch1.control.length;
 229			}
 230		}
 231		if (audio->playingCh1 && audio->ch1.envelope.dead != 2 && !mTimingIsScheduled(audio->timing, &audio->ch1Event)) {
 232			mTimingSchedule(audio->timing, &audio->ch1Event, 0);
 233		}
 234	}
 235	*audio->nr52 &= ~0x0001;
 236	*audio->nr52 |= audio->playingCh1;
 237}
 238
 239void GBAudioWriteNR21(struct GBAudio* audio, uint8_t value) {
 240	_writeDuty(&audio->ch2.envelope, value);
 241	audio->ch2.control.length = 64 - audio->ch2.envelope.length;
 242}
 243
 244void GBAudioWriteNR22(struct GBAudio* audio, uint8_t value) {
 245	if (!_writeEnvelope(&audio->ch2.envelope, value, audio->style)) {
 246		mTimingDeschedule(audio->timing, &audio->ch2Event);
 247		audio->playingCh2 = false;
 248		*audio->nr52 &= ~0x0002;
 249	}
 250}
 251
 252void GBAudioWriteNR23(struct GBAudio* audio, uint8_t value) {
 253	audio->ch2.control.frequency &= 0x700;
 254	audio->ch2.control.frequency |= GBAudioRegisterControlGetFrequency(value);
 255}
 256
 257void GBAudioWriteNR24(struct GBAudio* audio, uint8_t value) {
 258	audio->ch2.control.frequency &= 0xFF;
 259	audio->ch2.control.frequency |= GBAudioRegisterControlGetFrequency(value << 8);
 260	bool wasStop = audio->ch2.control.stop;
 261	audio->ch2.control.stop = GBAudioRegisterControlGetStop(value << 8);
 262	if (!wasStop && audio->ch2.control.stop && audio->ch2.control.length && !(audio->frame & 1)) {
 263		--audio->ch2.control.length;
 264		if (audio->ch2.control.length == 0) {
 265			mTimingDeschedule(audio->timing, &audio->ch2Event);
 266			audio->playingCh2 = false;
 267		}
 268	}
 269	if (GBAudioRegisterControlIsRestart(value << 8)) {
 270		audio->playingCh2 = _resetEnvelope(&audio->ch2.envelope);
 271
 272		if (audio->playingCh2) {
 273			_updateSquareSample(&audio->ch2);
 274		}
 275
 276		if (!audio->ch2.control.length) {
 277			audio->ch2.control.length = 64;
 278			if (audio->ch2.control.stop && !(audio->frame & 1)) {
 279				--audio->ch2.control.length;
 280			}
 281		}
 282		if (audio->playingCh2 && audio->ch2.envelope.dead != 2 && !mTimingIsScheduled(audio->timing, &audio->ch2Event)) {
 283			mTimingSchedule(audio->timing, &audio->ch2Event, 0);
 284		}
 285	}
 286	*audio->nr52 &= ~0x0002;
 287	*audio->nr52 |= audio->playingCh2 << 1;
 288}
 289
 290void GBAudioWriteNR30(struct GBAudio* audio, uint8_t value) {
 291	audio->ch3.enable = GBAudioRegisterBankGetEnable(value);
 292	if (!audio->ch3.enable) {
 293		audio->playingCh3 = false;
 294		*audio->nr52 &= ~0x0004;
 295	}
 296}
 297
 298void GBAudioWriteNR31(struct GBAudio* audio, uint8_t value) {
 299	audio->ch3.length = 256 - value;
 300}
 301
 302void GBAudioWriteNR32(struct GBAudio* audio, uint8_t value) {
 303	audio->ch3.volume = GBAudioRegisterBankVolumeGetVolumeGB(value);
 304}
 305
 306void GBAudioWriteNR33(struct GBAudio* audio, uint8_t value) {
 307	audio->ch3.rate &= 0x700;
 308	audio->ch3.rate |= GBAudioRegisterControlGetRate(value);
 309}
 310
 311void GBAudioWriteNR34(struct GBAudio* audio, uint8_t value) {
 312	audio->ch3.rate &= 0xFF;
 313	audio->ch3.rate |= GBAudioRegisterControlGetRate(value << 8);
 314	bool wasStop = audio->ch3.stop;
 315	audio->ch3.stop = GBAudioRegisterControlGetStop(value << 8);
 316	if (!wasStop && audio->ch3.stop && audio->ch3.length && !(audio->frame & 1)) {
 317		--audio->ch3.length;
 318		if (audio->ch3.length == 0) {
 319			audio->playingCh3 = false;
 320		}
 321	}
 322	bool wasEnable = audio->playingCh3;
 323	if (GBAudioRegisterControlIsRestart(value << 8)) {
 324		audio->playingCh3 = audio->ch3.enable;
 325		if (!audio->ch3.length) {
 326			audio->ch3.length = 256;
 327			if (audio->ch3.stop && !(audio->frame & 1)) {
 328				--audio->ch3.length;
 329			}
 330		}
 331
 332		if (audio->style == GB_AUDIO_DMG && wasEnable && audio->playingCh3 && audio->ch3.readable) {
 333			if (audio->ch3.window < 8) {
 334				audio->ch3.wavedata8[0] = audio->ch3.wavedata8[audio->ch3.window >> 1];
 335			} else {
 336				audio->ch3.wavedata8[0] = audio->ch3.wavedata8[((audio->ch3.window >> 1) & ~3)];
 337				audio->ch3.wavedata8[1] = audio->ch3.wavedata8[((audio->ch3.window >> 1) & ~3) + 1];
 338				audio->ch3.wavedata8[2] = audio->ch3.wavedata8[((audio->ch3.window >> 1) & ~3) + 2];
 339				audio->ch3.wavedata8[3] = audio->ch3.wavedata8[((audio->ch3.window >> 1) & ~3) + 3];
 340			}
 341		}
 342		audio->ch3.window = 0;
 343	}
 344	mTimingDeschedule(audio->timing, &audio->ch3Fade);
 345	mTimingDeschedule(audio->timing, &audio->ch3Event);
 346	if (audio->playingCh3) {
 347		audio->ch3.readable = audio->style != GB_AUDIO_DMG;
 348		// TODO: Where does this cycle delay come from?
 349		mTimingSchedule(audio->timing, &audio->ch3Event, audio->timingFactor * 4 + 2 * (2048 - audio->ch3.rate));
 350	}
 351	*audio->nr52 &= ~0x0004;
 352	*audio->nr52 |= audio->playingCh3 << 2;
 353}
 354
 355void GBAudioWriteNR41(struct GBAudio* audio, uint8_t value) {
 356	_writeDuty(&audio->ch4.envelope, value);
 357	audio->ch4.length = 64 - audio->ch4.envelope.length;
 358}
 359
 360void GBAudioWriteNR42(struct GBAudio* audio, uint8_t value) {
 361	if (!_writeEnvelope(&audio->ch4.envelope, value, audio->style)) {
 362		mTimingDeschedule(audio->timing, &audio->ch4Event);
 363		audio->playingCh4 = false;
 364		*audio->nr52 &= ~0x0008;
 365	}
 366}
 367
 368void GBAudioWriteNR43(struct GBAudio* audio, uint8_t value) {
 369	audio->ch4.ratio = GBAudioRegisterNoiseFeedbackGetRatio(value);
 370	audio->ch4.frequency = GBAudioRegisterNoiseFeedbackGetFrequency(value);
 371	audio->ch4.power = GBAudioRegisterNoiseFeedbackGetPower(value);
 372}
 373
 374void GBAudioWriteNR44(struct GBAudio* audio, uint8_t value) {
 375	bool wasStop = audio->ch4.stop;
 376	audio->ch4.stop = GBAudioRegisterNoiseControlGetStop(value);
 377	if (!wasStop && audio->ch4.stop && audio->ch4.length && !(audio->frame & 1)) {
 378		--audio->ch4.length;
 379		if (audio->ch4.length == 0) {
 380			mTimingDeschedule(audio->timing, &audio->ch4Event);
 381			audio->playingCh4 = false;
 382		}
 383	}
 384	if (GBAudioRegisterNoiseControlIsRestart(value)) {
 385		audio->playingCh4 = _resetEnvelope(&audio->ch4.envelope);
 386
 387		if (audio->ch4.power) {
 388			audio->ch4.lfsr = 0x40;
 389		} else {
 390			audio->ch4.lfsr = 0x4000;
 391		}
 392		if (!audio->ch4.length) {
 393			audio->ch4.length = 64;
 394			if (audio->ch4.stop && !(audio->frame & 1)) {
 395				--audio->ch4.length;
 396			}
 397		}
 398		if (audio->playingCh4 && audio->ch4.envelope.dead != 2 && !mTimingIsScheduled(audio->timing, &audio->ch4Event)) {
 399			mTimingSchedule(audio->timing, &audio->ch4Event, 0);
 400		}
 401	}
 402	*audio->nr52 &= ~0x0008;
 403	*audio->nr52 |= audio->playingCh4 << 3;
 404}
 405
 406void GBAudioWriteNR50(struct GBAudio* audio, uint8_t value) {
 407	audio->volumeRight = GBRegisterNR50GetVolumeRight(value);
 408	audio->volumeLeft = GBRegisterNR50GetVolumeLeft(value);
 409}
 410
 411void GBAudioWriteNR51(struct GBAudio* audio, uint8_t value) {
 412	audio->ch1Right = GBRegisterNR51GetCh1Right(value);
 413	audio->ch2Right = GBRegisterNR51GetCh2Right(value);
 414	audio->ch3Right = GBRegisterNR51GetCh3Right(value);
 415	audio->ch4Right = GBRegisterNR51GetCh4Right(value);
 416	audio->ch1Left = GBRegisterNR51GetCh1Left(value);
 417	audio->ch2Left = GBRegisterNR51GetCh2Left(value);
 418	audio->ch3Left = GBRegisterNR51GetCh3Left(value);
 419	audio->ch4Left = GBRegisterNR51GetCh4Left(value);
 420}
 421
 422void GBAudioWriteNR52(struct GBAudio* audio, uint8_t value) {
 423	bool wasEnable = audio->enable;
 424	audio->enable = GBAudioEnableGetEnable(value);
 425	if (!audio->enable) {
 426		audio->playingCh1 = 0;
 427		audio->playingCh2 = 0;
 428		audio->playingCh3 = 0;
 429		audio->playingCh4 = 0;
 430		GBAudioWriteNR10(audio, 0);
 431		GBAudioWriteNR12(audio, 0);
 432		GBAudioWriteNR13(audio, 0);
 433		GBAudioWriteNR14(audio, 0);
 434		GBAudioWriteNR22(audio, 0);
 435		GBAudioWriteNR23(audio, 0);
 436		GBAudioWriteNR24(audio, 0);
 437		GBAudioWriteNR30(audio, 0);
 438		GBAudioWriteNR32(audio, 0);
 439		GBAudioWriteNR33(audio, 0);
 440		GBAudioWriteNR34(audio, 0);
 441		GBAudioWriteNR42(audio, 0);
 442		GBAudioWriteNR43(audio, 0);
 443		GBAudioWriteNR44(audio, 0);
 444		GBAudioWriteNR50(audio, 0);
 445		GBAudioWriteNR51(audio, 0);
 446		if (audio->style != GB_AUDIO_DMG) {
 447			GBAudioWriteNR11(audio, 0);
 448			GBAudioWriteNR21(audio, 0);
 449			GBAudioWriteNR31(audio, 0);
 450			GBAudioWriteNR41(audio, 0);
 451		}
 452
 453		if (audio->p) {
 454			audio->p->memory.io[REG_NR10] = 0;
 455			audio->p->memory.io[REG_NR11] = 0;
 456			audio->p->memory.io[REG_NR12] = 0;
 457			audio->p->memory.io[REG_NR13] = 0;
 458			audio->p->memory.io[REG_NR14] = 0;
 459			audio->p->memory.io[REG_NR21] = 0;
 460			audio->p->memory.io[REG_NR22] = 0;
 461			audio->p->memory.io[REG_NR23] = 0;
 462			audio->p->memory.io[REG_NR24] = 0;
 463			audio->p->memory.io[REG_NR30] = 0;
 464			audio->p->memory.io[REG_NR31] = 0;
 465			audio->p->memory.io[REG_NR32] = 0;
 466			audio->p->memory.io[REG_NR33] = 0;
 467			audio->p->memory.io[REG_NR34] = 0;
 468			audio->p->memory.io[REG_NR42] = 0;
 469			audio->p->memory.io[REG_NR43] = 0;
 470			audio->p->memory.io[REG_NR44] = 0;
 471			audio->p->memory.io[REG_NR50] = 0;
 472			audio->p->memory.io[REG_NR51] = 0;
 473			if (audio->style != GB_AUDIO_DMG) {
 474				audio->p->memory.io[REG_NR11] = 0;
 475				audio->p->memory.io[REG_NR21] = 0;
 476				audio->p->memory.io[REG_NR31] = 0;
 477				audio->p->memory.io[REG_NR41] = 0;
 478			}
 479		}
 480		*audio->nr52 &= ~0x000F;
 481	} else if (!wasEnable) {
 482		audio->frame = 7;
 483	}
 484}
 485
 486void _updateFrame(struct mTiming* timing, void* user, uint32_t cyclesLate) {
 487	struct GBAudio* audio = user;
 488	GBAudioUpdateFrame(audio, timing);
 489	if (audio->style == GB_AUDIO_GBA) {
 490		mTimingSchedule(timing, &audio->frameEvent, audio->timingFactor * FRAME_CYCLES - cyclesLate);
 491	}
 492}
 493
 494void GBAudioUpdateFrame(struct GBAudio* audio, struct mTiming* timing) {
 495	int frame = (audio->frame + 1) & 7;
 496	audio->frame = frame;
 497
 498	switch (frame) {
 499	case 2:
 500	case 6:
 501		if (audio->ch1.sweep.enable) {
 502			--audio->ch1.sweep.step;
 503			if (audio->ch1.sweep.step == 0) {
 504				audio->playingCh1 = _updateSweep(&audio->ch1, false);
 505				*audio->nr52 &= ~0x0001;
 506				*audio->nr52 |= audio->playingCh1;
 507			}
 508		}
 509		// Fall through
 510	case 0:
 511	case 4:
 512		if (audio->ch1.control.length && audio->ch1.control.stop) {
 513			--audio->ch1.control.length;
 514			if (audio->ch1.control.length == 0) {
 515				mTimingDeschedule(timing, &audio->ch1Event);
 516				audio->playingCh1 = 0;
 517				*audio->nr52 &= ~0x0001;
 518			}
 519		}
 520
 521		if (audio->ch2.control.length && audio->ch2.control.stop) {
 522			--audio->ch2.control.length;
 523			if (audio->ch2.control.length == 0) {
 524				mTimingDeschedule(timing, &audio->ch2Event);
 525				audio->playingCh2 = 0;
 526				*audio->nr52 &= ~0x0002;
 527			}
 528		}
 529
 530		if (audio->ch3.length && audio->ch3.stop) {
 531			--audio->ch3.length;
 532			if (audio->ch3.length == 0) {
 533				mTimingDeschedule(timing, &audio->ch3Event);
 534				audio->playingCh3 = 0;
 535				*audio->nr52 &= ~0x0004;
 536			}
 537		}
 538
 539		if (audio->ch4.length && audio->ch4.stop) {
 540			--audio->ch4.length;
 541			if (audio->ch4.length == 0) {
 542				mTimingDeschedule(timing, &audio->ch4Event);
 543				audio->playingCh4 = 0;
 544				*audio->nr52 &= ~0x0008;
 545			}
 546		}
 547		break;
 548	case 7:
 549		if (audio->playingCh1 && !audio->ch1.envelope.dead) {
 550			--audio->ch1.envelope.nextStep;
 551			if (audio->ch1.envelope.nextStep == 0) {
 552				_updateEnvelope(&audio->ch1.envelope);
 553				if (audio->ch1.envelope.dead == 2) {
 554					mTimingDeschedule(timing, &audio->ch1Event);
 555				}
 556				_updateSquareSample(&audio->ch1);
 557			}
 558		}
 559
 560		if (audio->playingCh2 && !audio->ch2.envelope.dead) {
 561			--audio->ch2.envelope.nextStep;
 562			if (audio->ch2.envelope.nextStep == 0) {
 563				_updateEnvelope(&audio->ch2.envelope);
 564				if (audio->ch2.envelope.dead == 2) {
 565					mTimingDeschedule(timing, &audio->ch2Event);
 566				}
 567				_updateSquareSample(&audio->ch2);
 568			}
 569		}
 570
 571		if (audio->playingCh4 && !audio->ch4.envelope.dead) {
 572			--audio->ch4.envelope.nextStep;
 573			if (audio->ch4.envelope.nextStep == 0) {
 574				int8_t sample = (audio->ch4.sample > 0) * 0x8;
 575				_updateEnvelope(&audio->ch4.envelope);
 576				if (audio->ch4.envelope.dead == 2) {
 577					mTimingDeschedule(timing, &audio->ch4Event);
 578				}
 579				audio->ch4.sample = sample * audio->ch4.envelope.currentVolume;
 580			}
 581		}
 582		break;
 583	}
 584}
 585
 586void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right) {
 587	int sampleLeft = 0;
 588	int sampleRight = 0;
 589
 590	if (audio->playingCh1 && !audio->forceDisableCh[0]) {
 591		if (audio->ch1Left) {
 592			sampleLeft += audio->ch1.sample;
 593		}
 594
 595		if (audio->ch1Right) {
 596			sampleRight += audio->ch1.sample;
 597		}
 598	}
 599
 600	if (audio->playingCh2 && !audio->forceDisableCh[1]) {
 601		if (audio->ch2Left) {
 602			sampleLeft += audio->ch2.sample;
 603		}
 604
 605		if (audio->ch2Right) {
 606			sampleRight += audio->ch2.sample;
 607		}
 608	}
 609
 610	if (audio->playingCh3 && !audio->forceDisableCh[2]) {
 611		if (audio->ch3Left) {
 612			sampleLeft += audio->ch3.sample;
 613		}
 614
 615		if (audio->ch3Right) {
 616			sampleRight += audio->ch3.sample;
 617		}
 618	}
 619
 620	if (audio->playingCh4 && !audio->forceDisableCh[3]) {
 621		if (audio->ch4Left) {
 622			sampleLeft += audio->ch4.sample;
 623		}
 624
 625		if (audio->ch4Right) {
 626			sampleRight += audio->ch4.sample;
 627		}
 628	}
 629
 630	int dcOffset = audio->style == GB_AUDIO_GBA ? 0 : 0x1FC;
 631	*left = (sampleLeft - dcOffset) * (1 + audio->volumeLeft);
 632	*right = (sampleRight - dcOffset) * (1 + audio->volumeRight);
 633}
 634
 635static void _sample(struct mTiming* timing, void* user, uint32_t cyclesLate) {
 636	struct GBAudio* audio = user;
 637	int16_t sampleLeft = 0;
 638	int16_t sampleRight = 0;
 639	GBAudioSamplePSG(audio, &sampleLeft, &sampleRight);
 640	sampleLeft = (sampleLeft * audio->masterVolume) >> 6;
 641	sampleRight = (sampleRight * audio->masterVolume) >> 6;
 642
 643	mCoreSyncLockAudio(audio->p->sync);
 644	unsigned produced;
 645
 646	int16_t degradedLeft = sampleLeft - (audio->capLeft >> 16);
 647	int16_t degradedRight = sampleRight - (audio->capRight >> 16);
 648	audio->capLeft = (sampleLeft << 16) - degradedLeft * 65184;
 649	audio->capRight = (sampleRight << 16) - degradedRight * 65184;
 650	sampleLeft = degradedLeft;
 651	sampleRight = degradedRight;
 652
 653	if ((size_t) blip_samples_avail(audio->left) < audio->samples) {
 654		blip_add_delta(audio->left, audio->clock, sampleLeft - audio->lastLeft);
 655		blip_add_delta(audio->right, audio->clock, sampleRight - audio->lastRight);
 656		audio->clock += audio->sampleInterval;
 657		if (audio->clock >= CLOCKS_PER_BLIP_FRAME) {
 658			blip_end_frame(audio->left, CLOCKS_PER_BLIP_FRAME);
 659			blip_end_frame(audio->right, CLOCKS_PER_BLIP_FRAME);
 660			audio->clock -= CLOCKS_PER_BLIP_FRAME;
 661		}
 662	}
 663	audio->lastLeft = sampleLeft;
 664	audio->lastRight = sampleRight;
 665	produced = blip_samples_avail(audio->left);
 666	if (audio->p->stream && audio->p->stream->postAudioFrame) {
 667		audio->p->stream->postAudioFrame(audio->p->stream, sampleLeft, sampleRight);
 668	}
 669	bool wait = produced >= audio->samples;
 670	mCoreSyncProduceAudio(audio->p->sync, wait);
 671
 672	if (wait && audio->p->stream && audio->p->stream->postAudioBuffer) {
 673		audio->p->stream->postAudioBuffer(audio->p->stream, audio->left, audio->right);
 674	}
 675	mTimingSchedule(timing, &audio->sampleEvent, audio->sampleInterval * audio->timingFactor - cyclesLate);
 676}
 677
 678bool _resetEnvelope(struct GBAudioEnvelope* envelope) {
 679	envelope->currentVolume = envelope->initialVolume;
 680	_updateEnvelopeDead(envelope);
 681	if (!envelope->dead) {
 682		envelope->nextStep = envelope->stepTime;
 683	}
 684	return envelope->initialVolume || envelope->direction;
 685}
 686
 687void _resetSweep(struct GBAudioSweep* sweep) {
 688	sweep->step = sweep->time;
 689	sweep->enable = (sweep->step != 8) || sweep->shift;
 690	sweep->occurred = false;
 691}
 692
 693bool _writeSweep(struct GBAudioSweep* sweep, uint8_t value) {
 694	sweep->shift = GBAudioRegisterSquareSweepGetShift(value);
 695	bool oldDirection = sweep->direction;
 696	sweep->direction = GBAudioRegisterSquareSweepGetDirection(value);
 697	bool on = true;
 698	if (sweep->occurred && oldDirection && !sweep->direction) {
 699		on = false;
 700	}
 701	sweep->occurred = false;
 702	sweep->time = GBAudioRegisterSquareSweepGetTime(value);
 703	if (!sweep->time) {
 704		sweep->time = 8;
 705	}
 706	return on;
 707}
 708
 709void _writeDuty(struct GBAudioEnvelope* envelope, uint8_t value) {
 710	envelope->length = GBAudioRegisterDutyGetLength(value);
 711	envelope->duty = GBAudioRegisterDutyGetDuty(value);
 712}
 713
 714bool _writeEnvelope(struct GBAudioEnvelope* envelope, uint8_t value, enum GBAudioStyle style) {
 715	envelope->stepTime = GBAudioRegisterSweepGetStepTime(value);
 716	envelope->direction = GBAudioRegisterSweepGetDirection(value);
 717	envelope->initialVolume = GBAudioRegisterSweepGetInitialVolume(value);
 718	if (style == GB_AUDIO_DMG && !envelope->stepTime) {
 719		// TODO: Improve "zombie" mode
 720		++envelope->currentVolume;
 721		envelope->currentVolume &= 0xF;
 722	}
 723	_updateEnvelopeDead(envelope);
 724	return (envelope->initialVolume || envelope->direction) && envelope->dead != 2;
 725}
 726
 727static void _updateSquareSample(struct GBAudioSquareChannel* ch) {
 728	ch->sample = ch->control.hi * ch->envelope.currentVolume * 0x8;
 729}
 730
 731static int32_t _updateSquareChannel(struct GBAudioSquareChannel* ch) {
 732	ch->control.hi = !ch->control.hi;
 733	_updateSquareSample(ch);
 734	int period = 4 * (2048 - ch->control.frequency);
 735	switch (ch->envelope.duty) {
 736	case 0:
 737		return ch->control.hi ? period : period * 7;
 738	case 1:
 739		return ch->control.hi ? period * 2 : period * 6;
 740	case 2:
 741		return period * 4;
 742	case 3:
 743		return ch->control.hi ? period * 6 : period * 2;
 744	default:
 745		// This should never be hit
 746		return period * 4;
 747	}
 748}
 749
 750static void _updateEnvelope(struct GBAudioEnvelope* envelope) {
 751	if (envelope->direction) {
 752		++envelope->currentVolume;
 753	} else {
 754		--envelope->currentVolume;
 755	}
 756	if (envelope->currentVolume >= 15) {
 757		envelope->currentVolume = 15;
 758		envelope->dead = 1;
 759	} else if (envelope->currentVolume <= 0) {
 760		envelope->currentVolume = 0;
 761		envelope->dead = 2;
 762	} else {
 763		envelope->nextStep = envelope->stepTime;
 764	}
 765}
 766
 767static void _updateEnvelopeDead(struct GBAudioEnvelope* envelope) {
 768	if (!envelope->stepTime) {
 769		envelope->dead = envelope->currentVolume ? 1 : 2;
 770	} else if (!envelope->direction && !envelope->currentVolume) {
 771		envelope->dead = 2;
 772	} else if (envelope->direction && envelope->currentVolume == 0xF) {
 773		envelope->dead = 1;
 774	} else {
 775		envelope->dead = 0;
 776	}
 777}
 778
 779static bool _updateSweep(struct GBAudioSquareChannel* ch, bool initial) {
 780	if (initial || ch->sweep.time != 8) {
 781		int frequency = ch->sweep.realFrequency;
 782		if (ch->sweep.direction) {
 783			frequency -= frequency >> ch->sweep.shift;
 784			if (!initial && frequency >= 0) {
 785				ch->control.frequency = frequency;
 786				ch->sweep.realFrequency = frequency;
 787			}
 788		} else {
 789			frequency += frequency >> ch->sweep.shift;
 790			if (frequency < 2048) {
 791				if (!initial && ch->sweep.shift) {
 792					ch->control.frequency = frequency;
 793					ch->sweep.realFrequency = frequency;
 794					if (!_updateSweep(ch, true)) {
 795						return false;
 796					}
 797				}
 798			} else {
 799				return false;
 800			}
 801		}
 802		ch->sweep.occurred = true;
 803	}
 804	ch->sweep.step = ch->sweep.time;
 805	return true;
 806}
 807
 808static void _updateChannel1(struct mTiming* timing, void* user, uint32_t cyclesLate) {
 809	struct GBAudio* audio = user;
 810	struct GBAudioSquareChannel* ch = &audio->ch1;
 811	int cycles = _updateSquareChannel(ch);
 812	mTimingSchedule(timing, &audio->ch1Event, audio->timingFactor * cycles - cyclesLate);
 813}
 814
 815static void _updateChannel2(struct mTiming* timing, void* user, uint32_t cyclesLate) {
 816	struct GBAudio* audio = user;
 817	struct GBAudioSquareChannel* ch = &audio->ch2;
 818	int cycles = _updateSquareChannel(ch);
 819	mTimingSchedule(timing, &audio->ch2Event, audio->timingFactor * cycles - cyclesLate);
 820}
 821
 822static void _updateChannel3(struct mTiming* timing, void* user, uint32_t cyclesLate) {
 823	struct GBAudio* audio = user;
 824	struct GBAudioWaveChannel* ch = &audio->ch3;
 825	int i;
 826	int volume;
 827	switch (ch->volume) {
 828	case 0:
 829		volume = 0;
 830		break;
 831	case 1:
 832		volume = 4;
 833		break;
 834	case 2:
 835		volume = 2;
 836		break;
 837	case 3:
 838		volume = 1;
 839		break;
 840	default:
 841		volume = 3;
 842		break;
 843	}
 844	int start;
 845	int end;
 846	switch (audio->style) {
 847	case GB_AUDIO_DMG:
 848	default:
 849		++ch->window;
 850		ch->window &= 0x1F;
 851		ch->sample = ch->wavedata8[ch->window >> 1];
 852		if (!(ch->window & 1)) {
 853			ch->sample >>= 4;
 854		}
 855		ch->sample &= 0xF;
 856		break;
 857	case GB_AUDIO_GBA:
 858		if (ch->size) {
 859			start = 7;
 860			end = 0;
 861		} else if (ch->bank) {
 862			start = 7;
 863			end = 4;
 864		} else {
 865			start = 3;
 866			end = 0;
 867		}
 868		uint32_t bitsCarry = ch->wavedata32[end] & 0x000000F0;
 869		uint32_t bits;
 870		for (i = start; i >= end; --i) {
 871			bits = ch->wavedata32[i] & 0x000000F0;
 872			ch->wavedata32[i] = ((ch->wavedata32[i] & 0x0F0F0F0F) << 4) | ((ch->wavedata32[i] & 0xF0F0F000) >> 12);
 873			ch->wavedata32[i] |= bitsCarry << 20;
 874			bitsCarry = bits;
 875		}
 876		ch->sample = bitsCarry >> 4;
 877		break;
 878	}
 879	ch->sample *= volume * 2;
 880	audio->ch3.readable = true;
 881	if (audio->style == GB_AUDIO_DMG) {
 882		mTimingDeschedule(audio->timing, &audio->ch3Fade);
 883		mTimingSchedule(timing, &audio->ch3Fade, 2 - cyclesLate);
 884	}
 885	int cycles = 2 * (2048 - ch->rate);
 886	mTimingSchedule(timing, &audio->ch3Event, audio->timingFactor * cycles - cyclesLate);
 887}
 888static void _fadeChannel3(struct mTiming* timing, void* user, uint32_t cyclesLate) {
 889	UNUSED(timing);
 890	UNUSED(cyclesLate);
 891	struct GBAudio* audio = user;
 892	audio->ch3.readable = false;
 893}
 894
 895static void _updateChannel4(struct mTiming* timing, void* user, uint32_t cyclesLate) {
 896	struct GBAudio* audio = user;
 897	struct GBAudioNoiseChannel* ch = &audio->ch4;
 898
 899	int32_t baseCycles = ch->ratio ? 2 * ch->ratio : 1;
 900	baseCycles <<= ch->frequency;
 901	baseCycles *= 8 * audio->timingFactor;
 902	int32_t cycles = 0;
 903
 904	do {
 905		int lsb = ch->lfsr & 1;
 906		ch->sample = lsb * 0x8;
 907		ch->sample *= ch->envelope.currentVolume;
 908		ch->lfsr >>= 1;
 909		ch->lfsr ^= (lsb * 0x60) << (ch->power ? 0 : 8);
 910		cycles += baseCycles;
 911	} while (cycles + baseCycles < audio->sampleInterval);
 912	mTimingSchedule(timing, &audio->ch4Event, cycles - cyclesLate);
 913}
 914
 915void GBAudioPSGSerialize(const struct GBAudio* audio, struct GBSerializedPSGState* state, uint32_t* flagsOut) {
 916	uint32_t flags = 0;
 917	uint32_t ch1Flags = 0;
 918	uint32_t ch2Flags = 0;
 919	uint32_t ch4Flags = 0;
 920
 921	flags = GBSerializedAudioFlagsSetFrame(flags, audio->frame);
 922	STORE_32LE(audio->frameEvent.when - mTimingCurrentTime(audio->timing), 0, &state->ch1.nextFrame);
 923
 924	flags = GBSerializedAudioFlagsSetCh1Volume(flags, audio->ch1.envelope.currentVolume);
 925	flags = GBSerializedAudioFlagsSetCh1Dead(flags, audio->ch1.envelope.dead);
 926	flags = GBSerializedAudioFlagsSetCh1Hi(flags, audio->ch1.control.hi);
 927	flags = GBSerializedAudioFlagsSetCh1SweepEnabled(flags, audio->ch1.sweep.enable);
 928	flags = GBSerializedAudioFlagsSetCh1SweepOccurred(flags, audio->ch1.sweep.occurred);
 929	ch1Flags = GBSerializedAudioEnvelopeSetLength(ch1Flags, audio->ch1.control.length);
 930	ch1Flags = GBSerializedAudioEnvelopeSetNextStep(ch1Flags, audio->ch1.envelope.nextStep);
 931	ch1Flags = GBSerializedAudioEnvelopeSetFrequency(ch1Flags, audio->ch1.sweep.realFrequency);
 932	STORE_32LE(ch1Flags, 0, &state->ch1.envelope);
 933	STORE_32LE(audio->ch1Event.when - mTimingCurrentTime(audio->timing), 0, &state->ch1.nextEvent);
 934
 935	flags = GBSerializedAudioFlagsSetCh2Volume(flags, audio->ch2.envelope.currentVolume);
 936	flags = GBSerializedAudioFlagsSetCh2Dead(flags, audio->ch2.envelope.dead);
 937	flags = GBSerializedAudioFlagsSetCh2Hi(flags, audio->ch2.control.hi);
 938	ch2Flags = GBSerializedAudioEnvelopeSetLength(ch2Flags, audio->ch2.control.length);
 939	ch2Flags = GBSerializedAudioEnvelopeSetNextStep(ch2Flags, audio->ch2.envelope.nextStep);
 940	STORE_32LE(ch2Flags, 0, &state->ch2.envelope);
 941	STORE_32LE(audio->ch2Event.when - mTimingCurrentTime(audio->timing), 0, &state->ch2.nextEvent);
 942
 943	flags = GBSerializedAudioFlagsSetCh3Readable(flags, audio->ch3.readable);
 944	memcpy(state->ch3.wavebanks, audio->ch3.wavedata32, sizeof(state->ch3.wavebanks));
 945	STORE_16LE(audio->ch3.length, 0, &state->ch3.length);
 946	STORE_32LE(audio->ch3Event.when - mTimingCurrentTime(audio->timing), 0, &state->ch3.nextEvent);
 947	STORE_32LE(audio->ch3Fade.when - mTimingCurrentTime(audio->timing), 0, &state->ch1.nextCh3Fade);
 948
 949	flags = GBSerializedAudioFlagsSetCh4Volume(flags, audio->ch4.envelope.currentVolume);
 950	flags = GBSerializedAudioFlagsSetCh4Dead(flags, audio->ch4.envelope.dead);
 951	STORE_32LE(audio->ch4.lfsr, 0, &state->ch4.lfsr);
 952	ch4Flags = GBSerializedAudioEnvelopeSetLength(ch4Flags, audio->ch4.length);
 953	ch4Flags = GBSerializedAudioEnvelopeSetNextStep(ch4Flags, audio->ch4.envelope.nextStep);
 954	STORE_32LE(ch4Flags, 0, &state->ch4.envelope);
 955	STORE_32LE(audio->ch4Event.when - mTimingCurrentTime(audio->timing), 0, &state->ch4.nextEvent);
 956
 957	STORE_32LE(flags, 0, flagsOut);
 958}
 959
 960void GBAudioPSGDeserialize(struct GBAudio* audio, const struct GBSerializedPSGState* state, const uint32_t* flagsIn) {
 961	uint32_t flags;
 962	uint32_t ch1Flags = 0;
 963	uint32_t ch2Flags = 0;
 964	uint32_t ch4Flags = 0;
 965	uint32_t when;
 966
 967	audio->playingCh1 = !!(*audio->nr52 & 0x0001);
 968	audio->playingCh2 = !!(*audio->nr52 & 0x0002);
 969	audio->playingCh3 = !!(*audio->nr52 & 0x0004);
 970	audio->playingCh4 = !!(*audio->nr52 & 0x0008);
 971	audio->enable = GBAudioEnableGetEnable(*audio->nr52);
 972
 973	if (audio->style == GB_AUDIO_GBA) {
 974		LOAD_32LE(when, 0, &state->ch1.nextFrame);
 975		mTimingSchedule(audio->timing, &audio->frameEvent, when);
 976	}
 977
 978	LOAD_32LE(flags, 0, flagsIn);
 979	audio->frame = GBSerializedAudioFlagsGetFrame(flags);
 980
 981	LOAD_32LE(ch1Flags, 0, &state->ch1.envelope);
 982	audio->ch1.envelope.currentVolume = GBSerializedAudioFlagsGetCh1Volume(flags);
 983	audio->ch1.envelope.dead = GBSerializedAudioFlagsGetCh1Dead(flags);
 984	audio->ch1.control.hi = GBSerializedAudioFlagsGetCh1Hi(flags);
 985	audio->ch1.sweep.enable = GBSerializedAudioFlagsGetCh1SweepEnabled(flags);
 986	audio->ch1.sweep.occurred = GBSerializedAudioFlagsGetCh1SweepOccurred(flags);
 987	audio->ch1.control.length = GBSerializedAudioEnvelopeGetLength(ch1Flags);
 988	audio->ch1.envelope.nextStep = GBSerializedAudioEnvelopeGetNextStep(ch1Flags);
 989	audio->ch1.sweep.realFrequency = GBSerializedAudioEnvelopeGetFrequency(ch1Flags);
 990	LOAD_32LE(when, 0, &state->ch1.nextEvent);
 991	if (audio->ch1.envelope.dead < 2 && audio->playingCh1) {
 992		mTimingSchedule(audio->timing, &audio->ch1Event, when);
 993	}
 994
 995	LOAD_32LE(ch2Flags, 0, &state->ch2.envelope);
 996	audio->ch2.envelope.currentVolume = GBSerializedAudioFlagsGetCh2Volume(flags);
 997	audio->ch2.envelope.dead = GBSerializedAudioFlagsGetCh2Dead(flags);
 998	audio->ch2.control.hi = GBSerializedAudioFlagsGetCh2Hi(flags);
 999	audio->ch2.control.length = GBSerializedAudioEnvelopeGetLength(ch2Flags);
1000	audio->ch2.envelope.nextStep = GBSerializedAudioEnvelopeGetNextStep(ch2Flags);
1001	LOAD_32LE(when, 0, &state->ch2.nextEvent);
1002	if (audio->ch2.envelope.dead < 2 && audio->playingCh2) {
1003		mTimingSchedule(audio->timing, &audio->ch2Event, when);
1004	}
1005
1006	audio->ch3.readable = GBSerializedAudioFlagsGetCh3Readable(flags);
1007	// TODO: Big endian?
1008	memcpy(audio->ch3.wavedata32, state->ch3.wavebanks, sizeof(audio->ch3.wavedata32));
1009	LOAD_16LE(audio->ch3.length, 0, &state->ch3.length);
1010	LOAD_32LE(when, 0, &state->ch3.nextEvent);
1011	if (audio->playingCh3) {
1012		mTimingSchedule(audio->timing, &audio->ch3Event, when);
1013	}
1014	LOAD_32LE(when, 0, &state->ch1.nextCh3Fade);
1015	if (audio->ch3.readable && audio->style == GB_AUDIO_DMG) {
1016		mTimingSchedule(audio->timing, &audio->ch3Fade, when);
1017	}
1018
1019	LOAD_32LE(ch4Flags, 0, &state->ch4.envelope);
1020	audio->ch4.envelope.currentVolume = GBSerializedAudioFlagsGetCh4Volume(flags);
1021	audio->ch4.envelope.dead = GBSerializedAudioFlagsGetCh4Dead(flags);
1022	audio->ch4.length = GBSerializedAudioEnvelopeGetLength(ch4Flags);
1023	audio->ch4.envelope.nextStep = GBSerializedAudioEnvelopeGetNextStep(ch4Flags);
1024	LOAD_32LE(audio->ch4.lfsr, 0, &state->ch4.lfsr);
1025	LOAD_32LE(when, 0, &state->ch4.nextEvent);
1026	if (audio->ch4.envelope.dead < 2 && audio->playingCh4) {
1027		mTimingSchedule(audio->timing, &audio->ch4Event, when);
1028	}
1029}
1030
1031void GBAudioSerialize(const struct GBAudio* audio, struct GBSerializedState* state) {
1032	GBAudioPSGSerialize(audio, &state->audio.psg, &state->audio.flags);
1033	STORE_32LE(audio->capLeft, 0, &state->audio.capLeft);
1034	STORE_32LE(audio->capRight, 0, &state->audio.capRight);
1035	STORE_32LE(audio->sampleEvent.when - mTimingCurrentTime(audio->timing), 0, &state->audio.nextSample);
1036}
1037
1038void GBAudioDeserialize(struct GBAudio* audio, const struct GBSerializedState* state) {
1039	GBAudioPSGDeserialize(audio, &state->audio.psg, &state->audio.flags);
1040	LOAD_32LE(audio->capLeft, 0, &state->audio.capLeft);
1041	LOAD_32LE(audio->capRight, 0, &state->audio.capRight);
1042	uint32_t when;
1043	LOAD_32LE(when, 0, &state->audio.nextSample);
1044	mTimingSchedule(audio->timing, &audio->sampleEvent, when);
1045}