all repos — mgba @ 4a83ae20072bd6ce44ead20413bbc5ad8dff4620

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);
  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	mTimingSchedule(audio->timing, &audio->frameEvent, 0);
 113	if (audio->style != GB_AUDIO_GBA) {
 114		mTimingSchedule(audio->timing, &audio->sampleEvent, 0);
 115	}
 116	audio->ch1 = (struct GBAudioSquareChannel) { .envelope = { .dead = 2 } };
 117	audio->ch2 = (struct GBAudioSquareChannel) { .envelope = { .dead = 2 } };
 118	audio->ch3 = (struct GBAudioWaveChannel) { .bank = 0 };
 119	// TODO: DMG randomness
 120	audio->ch3.wavedata8[0] = 0x00;
 121	audio->ch3.wavedata8[1] = 0xFF;
 122	audio->ch3.wavedata8[2] = 0x00;
 123	audio->ch3.wavedata8[3] = 0xFF;
 124	audio->ch3.wavedata8[4] = 0x00;
 125	audio->ch3.wavedata8[5] = 0xFF;
 126	audio->ch3.wavedata8[6] = 0x00;
 127	audio->ch3.wavedata8[7] = 0xFF;
 128	audio->ch3.wavedata8[8] = 0x00;
 129	audio->ch3.wavedata8[9] = 0xFF;
 130	audio->ch3.wavedata8[10] = 0x00;
 131	audio->ch3.wavedata8[11] = 0xFF;
 132	audio->ch3.wavedata8[12] = 0x00;
 133	audio->ch3.wavedata8[13] = 0xFF;
 134	audio->ch3.wavedata8[14] = 0x00;
 135	audio->ch3.wavedata8[15] = 0xFF;
 136	audio->ch4 = (struct GBAudioNoiseChannel) { .envelope = { .dead = 2 } };
 137	audio->frame = 0;
 138	audio->sampleInterval = 128;
 139	audio->lastLeft = 0;
 140	audio->lastRight = 0;
 141	audio->clock = 0;
 142	audio->volumeRight = 0;
 143	audio->volumeLeft = 0;
 144	audio->ch1Right = false;
 145	audio->ch2Right = false;
 146	audio->ch3Right = false;
 147	audio->ch4Right = false;
 148	audio->ch1Left = false;
 149	audio->ch2Left = false;
 150	audio->ch3Left = false;
 151	audio->ch4Left = false;
 152	audio->playingCh1 = false;
 153	audio->playingCh2 = false;
 154	audio->playingCh3 = false;
 155	audio->playingCh4 = false;
 156	if (audio->p && (audio->p->model == GB_MODEL_DMG || audio->p->model == GB_MODEL_CGB)) {
 157		audio->playingCh1 = true;
 158		*audio->nr52 |= 0x01;
 159	}
 160}
 161
 162void GBAudioResizeBuffer(struct GBAudio* audio, size_t samples) {
 163	mCoreSyncLockAudio(audio->p->sync);
 164	audio->samples = samples;
 165	blip_clear(audio->left);
 166	blip_clear(audio->right);
 167	audio->clock = 0;
 168	mCoreSyncConsumeAudio(audio->p->sync);
 169}
 170
 171void GBAudioWriteNR10(struct GBAudio* audio, uint8_t value) {
 172	if (!_writeSweep(&audio->ch1.sweep, value)) {
 173		mTimingDeschedule(audio->timing, &audio->ch1Event);
 174		audio->playingCh1 = false;
 175		*audio->nr52 &= ~0x0001;
 176	}
 177}
 178
 179void GBAudioWriteNR11(struct GBAudio* audio, uint8_t value) {
 180	_writeDuty(&audio->ch1.envelope, value);
 181	audio->ch1.control.length = 64 - audio->ch1.envelope.length;
 182}
 183
 184void GBAudioWriteNR12(struct GBAudio* audio, uint8_t value) {
 185	if (!_writeEnvelope(&audio->ch1.envelope, value)) {
 186		mTimingDeschedule(audio->timing, &audio->ch1Event);
 187		audio->playingCh1 = false;
 188		*audio->nr52 &= ~0x0001;
 189	}
 190}
 191
 192void GBAudioWriteNR13(struct GBAudio* audio, uint8_t value) {
 193	audio->ch1.control.frequency &= 0x700;
 194	audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value);
 195}
 196
 197void GBAudioWriteNR14(struct GBAudio* audio, uint8_t value) {
 198	audio->ch1.control.frequency &= 0xFF;
 199	audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value << 8);
 200	bool wasStop = audio->ch1.control.stop;
 201	audio->ch1.control.stop = GBAudioRegisterControlGetStop(value << 8);
 202	if (!wasStop && audio->ch1.control.stop && audio->ch1.control.length && !(audio->frame & 1)) {
 203		--audio->ch1.control.length;
 204		if (audio->ch1.control.length == 0) {
 205			mTimingDeschedule(audio->timing, &audio->ch1Event);
 206			audio->playingCh1 = false;
 207		}
 208	}
 209	if (GBAudioRegisterControlIsRestart(value << 8)) {
 210		audio->playingCh1 = _resetEnvelope(&audio->ch1.envelope);
 211
 212		if (audio->playingCh1) {
 213			audio->ch1.control.hi = 0;
 214			_updateSquareSample(&audio->ch1);
 215		}
 216
 217		audio->ch1.sweep.realFrequency = audio->ch1.control.frequency;
 218		_resetSweep(&audio->ch1.sweep);
 219		if (audio->playingCh1 && audio->ch1.sweep.shift) {
 220			audio->playingCh1 = _updateSweep(&audio->ch1, true);
 221		}
 222		if (!audio->ch1.control.length) {
 223			audio->ch1.control.length = 64;
 224			if (audio->ch1.control.stop && !(audio->frame & 1)) {
 225				--audio->ch1.control.length;
 226			}
 227		}
 228		mTimingDeschedule(audio->timing, &audio->ch1Event);
 229		if (audio->playingCh1 && audio->ch1.envelope.dead != 2) {
 230			mTimingSchedule(audio->timing, &audio->ch1Event, 0);
 231		}
 232	}
 233	*audio->nr52 &= ~0x0001;
 234	*audio->nr52 |= audio->playingCh1;
 235}
 236
 237void GBAudioWriteNR21(struct GBAudio* audio, uint8_t value) {
 238	_writeDuty(&audio->ch2.envelope, value);
 239	audio->ch2.control.length = 64 - audio->ch2.envelope.length;
 240}
 241
 242void GBAudioWriteNR22(struct GBAudio* audio, uint8_t value) {
 243	if (!_writeEnvelope(&audio->ch2.envelope, value)) {
 244		mTimingDeschedule(audio->timing, &audio->ch2Event);
 245		audio->playingCh2 = false;
 246		*audio->nr52 &= ~0x0002;
 247	}
 248}
 249
 250void GBAudioWriteNR23(struct GBAudio* audio, uint8_t value) {
 251	audio->ch2.control.frequency &= 0x700;
 252	audio->ch2.control.frequency |= GBAudioRegisterControlGetFrequency(value);
 253}
 254
 255void GBAudioWriteNR24(struct GBAudio* audio, uint8_t value) {
 256	audio->ch2.control.frequency &= 0xFF;
 257	audio->ch2.control.frequency |= GBAudioRegisterControlGetFrequency(value << 8);
 258	bool wasStop = audio->ch2.control.stop;
 259	audio->ch2.control.stop = GBAudioRegisterControlGetStop(value << 8);
 260	if (!wasStop && audio->ch2.control.stop && audio->ch2.control.length && !(audio->frame & 1)) {
 261		--audio->ch2.control.length;
 262		if (audio->ch2.control.length == 0) {
 263			mTimingDeschedule(audio->timing, &audio->ch2Event);
 264			audio->playingCh2 = false;
 265		}
 266	}
 267	if (GBAudioRegisterControlIsRestart(value << 8)) {
 268		audio->playingCh2 = _resetEnvelope(&audio->ch2.envelope);
 269
 270		if (audio->playingCh2) {
 271			audio->ch2.control.hi = 0;
 272			_updateSquareSample(&audio->ch2);
 273		}
 274
 275		if (!audio->ch2.control.length) {
 276			audio->ch2.control.length = 64;
 277			if (audio->ch2.control.stop && !(audio->frame & 1)) {
 278				--audio->ch2.control.length;
 279			}
 280		}
 281		mTimingDeschedule(audio->timing, &audio->ch2Event);
 282		if (audio->playingCh2 && audio->ch2.envelope.dead != 2) {
 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)) {
 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		mTimingDeschedule(audio->timing, &audio->ch4Event);
 399		if (audio->playingCh4 && audio->ch4.envelope.dead != 2) {
 400			mTimingSchedule(audio->timing, &audio->ch4Event, 0);
 401		}
 402	}
 403	*audio->nr52 &= ~0x0008;
 404	*audio->nr52 |= audio->playingCh4 << 3;
 405}
 406
 407void GBAudioWriteNR50(struct GBAudio* audio, uint8_t value) {
 408	audio->volumeRight = GBRegisterNR50GetVolumeRight(value);
 409	audio->volumeLeft = GBRegisterNR50GetVolumeLeft(value);
 410}
 411
 412void GBAudioWriteNR51(struct GBAudio* audio, uint8_t value) {
 413	audio->ch1Right = GBRegisterNR51GetCh1Right(value);
 414	audio->ch2Right = GBRegisterNR51GetCh2Right(value);
 415	audio->ch3Right = GBRegisterNR51GetCh3Right(value);
 416	audio->ch4Right = GBRegisterNR51GetCh4Right(value);
 417	audio->ch1Left = GBRegisterNR51GetCh1Left(value);
 418	audio->ch2Left = GBRegisterNR51GetCh2Left(value);
 419	audio->ch3Left = GBRegisterNR51GetCh3Left(value);
 420	audio->ch4Left = GBRegisterNR51GetCh4Left(value);
 421}
 422
 423void GBAudioWriteNR52(struct GBAudio* audio, uint8_t value) {
 424	bool wasEnable = audio->enable;
 425	audio->enable = GBAudioEnableGetEnable(value);
 426	if (!audio->enable) {
 427		audio->playingCh1 = 0;
 428		audio->playingCh2 = 0;
 429		audio->playingCh3 = 0;
 430		audio->playingCh4 = 0;
 431		GBAudioWriteNR10(audio, 0);
 432		GBAudioWriteNR12(audio, 0);
 433		GBAudioWriteNR13(audio, 0);
 434		GBAudioWriteNR14(audio, 0);
 435		GBAudioWriteNR22(audio, 0);
 436		GBAudioWriteNR23(audio, 0);
 437		GBAudioWriteNR24(audio, 0);
 438		GBAudioWriteNR30(audio, 0);
 439		GBAudioWriteNR32(audio, 0);
 440		GBAudioWriteNR33(audio, 0);
 441		GBAudioWriteNR34(audio, 0);
 442		GBAudioWriteNR42(audio, 0);
 443		GBAudioWriteNR43(audio, 0);
 444		GBAudioWriteNR44(audio, 0);
 445		GBAudioWriteNR50(audio, 0);
 446		GBAudioWriteNR51(audio, 0);
 447		if (audio->style != GB_AUDIO_DMG) {
 448			GBAudioWriteNR11(audio, 0);
 449			GBAudioWriteNR21(audio, 0);
 450			GBAudioWriteNR31(audio, 0);
 451			GBAudioWriteNR41(audio, 0);
 452		}
 453
 454		if (audio->p) {
 455			audio->p->memory.io[REG_NR10] = 0;
 456			audio->p->memory.io[REG_NR11] = 0;
 457			audio->p->memory.io[REG_NR12] = 0;
 458			audio->p->memory.io[REG_NR13] = 0;
 459			audio->p->memory.io[REG_NR14] = 0;
 460			audio->p->memory.io[REG_NR21] = 0;
 461			audio->p->memory.io[REG_NR22] = 0;
 462			audio->p->memory.io[REG_NR23] = 0;
 463			audio->p->memory.io[REG_NR24] = 0;
 464			audio->p->memory.io[REG_NR30] = 0;
 465			audio->p->memory.io[REG_NR31] = 0;
 466			audio->p->memory.io[REG_NR32] = 0;
 467			audio->p->memory.io[REG_NR33] = 0;
 468			audio->p->memory.io[REG_NR34] = 0;
 469			audio->p->memory.io[REG_NR42] = 0;
 470			audio->p->memory.io[REG_NR43] = 0;
 471			audio->p->memory.io[REG_NR44] = 0;
 472			audio->p->memory.io[REG_NR50] = 0;
 473			audio->p->memory.io[REG_NR51] = 0;
 474			if (audio->style != GB_AUDIO_DMG) {
 475				audio->p->memory.io[REG_NR11] = 0;
 476				audio->p->memory.io[REG_NR21] = 0;
 477				audio->p->memory.io[REG_NR31] = 0;
 478				audio->p->memory.io[REG_NR41] = 0;
 479			}
 480		}
 481		*audio->nr52 &= ~0x000F;
 482	} else if (!wasEnable) {
 483		audio->frame = 7;
 484	}
 485}
 486
 487void _updateFrame(struct mTiming* timing, void* user, uint32_t cyclesLate) {
 488	struct GBAudio* audio = user;
 489
 490	int frame = (audio->frame + 1) & 7;
 491	audio->frame = frame;
 492
 493	switch (frame) {
 494	case 2:
 495	case 6:
 496		if (audio->ch1.sweep.enable) {
 497			--audio->ch1.sweep.step;
 498			if (audio->ch1.sweep.step == 0) {
 499				audio->playingCh1 = _updateSweep(&audio->ch1, false);
 500				*audio->nr52 &= ~0x0001;
 501				*audio->nr52 |= audio->playingCh1;
 502			}
 503		}
 504		// Fall through
 505	case 0:
 506	case 4:
 507		if (audio->ch1.control.length && audio->ch1.control.stop) {
 508			--audio->ch1.control.length;
 509			if (audio->ch1.control.length == 0) {
 510				mTimingDeschedule(timing, &audio->ch1Event);
 511				audio->playingCh1 = 0;
 512				*audio->nr52 &= ~0x0001;
 513			}
 514		}
 515
 516		if (audio->ch2.control.length && audio->ch2.control.stop) {
 517			--audio->ch2.control.length;
 518			if (audio->ch2.control.length == 0) {
 519				mTimingDeschedule(timing, &audio->ch2Event);
 520				audio->playingCh2 = 0;
 521				*audio->nr52 &= ~0x0002;
 522			}
 523		}
 524
 525		if (audio->ch3.length && audio->ch3.stop) {
 526			--audio->ch3.length;
 527			if (audio->ch3.length == 0) {
 528				mTimingDeschedule(timing, &audio->ch3Event);
 529				audio->playingCh3 = 0;
 530				*audio->nr52 &= ~0x0004;
 531			}
 532		}
 533
 534		if (audio->ch4.length && audio->ch4.stop) {
 535			--audio->ch4.length;
 536			if (audio->ch4.length == 0) {
 537				mTimingDeschedule(timing, &audio->ch4Event);
 538				audio->playingCh4 = 0;
 539				*audio->nr52 &= ~0x0008;
 540			}
 541		}
 542		break;
 543	case 7:
 544		if (audio->playingCh1 && !audio->ch1.envelope.dead) {
 545			--audio->ch1.envelope.nextStep;
 546			if (audio->ch1.envelope.nextStep == 0) {
 547				_updateEnvelope(&audio->ch1.envelope);
 548				if (audio->ch1.envelope.dead == 2) {
 549					mTimingDeschedule(timing, &audio->ch1Event);
 550				}
 551				_updateSquareSample(&audio->ch1);
 552			}
 553		}
 554
 555		if (audio->playingCh2 && !audio->ch2.envelope.dead) {
 556			--audio->ch2.envelope.nextStep;
 557			if (audio->ch2.envelope.nextStep == 0) {
 558				_updateEnvelope(&audio->ch2.envelope);
 559				if (audio->ch2.envelope.dead == 2) {
 560					mTimingDeschedule(timing, &audio->ch2Event);
 561				}
 562				_updateSquareSample(&audio->ch2);
 563			}
 564		}
 565
 566		if (audio->playingCh4 && !audio->ch4.envelope.dead) {
 567			--audio->ch4.envelope.nextStep;
 568			if (audio->ch4.envelope.nextStep == 0) {
 569				int8_t sample = (audio->ch4.sample >> 7) * 0x8;
 570				_updateEnvelope(&audio->ch4.envelope);
 571				if (audio->ch4.envelope.dead == 2) {
 572					mTimingDeschedule(timing, &audio->ch4Event);
 573				}
 574				audio->ch4.sample = sample * audio->ch4.envelope.currentVolume;
 575			}
 576		}
 577		break;
 578	}
 579
 580	mTimingSchedule(timing, &audio->frameEvent, audio->timingFactor * FRAME_CYCLES - cyclesLate);
 581}
 582
 583void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right) {
 584	int sampleLeft = 0;
 585	int sampleRight = 0;
 586
 587	if (audio->playingCh1 && !audio->forceDisableCh[0]) {
 588		if (audio->ch1Left) {
 589			sampleLeft += audio->ch1.sample;
 590		}
 591
 592		if (audio->ch1Right) {
 593			sampleRight += audio->ch1.sample;
 594		}
 595	}
 596
 597	if (audio->playingCh2 && !audio->forceDisableCh[1]) {
 598		if (audio->ch2Left) {
 599			sampleLeft += audio->ch2.sample;
 600		}
 601
 602		if (audio->ch2Right) {
 603			sampleRight += audio->ch2.sample;
 604		}
 605	}
 606
 607	if (audio->playingCh3 && !audio->forceDisableCh[2]) {
 608		if (audio->ch3Left) {
 609			sampleLeft += audio->ch3.sample;
 610		}
 611
 612		if (audio->ch3Right) {
 613			sampleRight += audio->ch3.sample;
 614		}
 615	}
 616
 617	if (audio->playingCh4 && !audio->forceDisableCh[3]) {
 618		if (audio->ch4Left) {
 619			sampleLeft += audio->ch4.sample;
 620		}
 621
 622		if (audio->ch4Right) {
 623			sampleRight += audio->ch4.sample;
 624		}
 625	}
 626
 627	int dcOffset = audio->style == GB_AUDIO_GBA ? 0 : 0x1FC;
 628	*left = (sampleLeft - dcOffset) * (1 + audio->volumeLeft);
 629	*right = (sampleRight - dcOffset) * (1 + audio->volumeRight);
 630}
 631
 632static void _sample(struct mTiming* timing, void* user, uint32_t cyclesLate) {
 633	struct GBAudio* audio = user;
 634	int16_t sampleLeft = 0;
 635	int16_t sampleRight = 0;
 636	GBAudioSamplePSG(audio, &sampleLeft, &sampleRight);
 637	sampleLeft = (sampleLeft * audio->masterVolume) >> 6;
 638	sampleRight = (sampleRight * audio->masterVolume) >> 6;
 639
 640	mCoreSyncLockAudio(audio->p->sync);
 641	unsigned produced;
 642	if ((size_t) blip_samples_avail(audio->left) < audio->samples) {
 643		blip_add_delta(audio->left, audio->clock, sampleLeft - audio->lastLeft);
 644		blip_add_delta(audio->right, audio->clock, sampleRight - audio->lastRight);
 645		audio->lastLeft = sampleLeft;
 646		audio->lastRight = sampleRight;
 647		audio->clock += audio->sampleInterval;
 648		if (audio->clock >= CLOCKS_PER_BLIP_FRAME) {
 649			blip_end_frame(audio->left, CLOCKS_PER_BLIP_FRAME);
 650			blip_end_frame(audio->right, CLOCKS_PER_BLIP_FRAME);
 651			audio->clock -= CLOCKS_PER_BLIP_FRAME;
 652		}
 653	}
 654	produced = blip_samples_avail(audio->left);
 655	if (audio->p->stream && audio->p->stream->postAudioFrame) {
 656		audio->p->stream->postAudioFrame(audio->p->stream, sampleLeft, sampleRight);
 657	}
 658	bool wait = produced >= audio->samples;
 659	mCoreSyncProduceAudio(audio->p->sync, wait);
 660
 661	if (wait && audio->p->stream && audio->p->stream->postAudioBuffer) {
 662		audio->p->stream->postAudioBuffer(audio->p->stream, audio->left, audio->right);
 663	}
 664	mTimingSchedule(timing, &audio->sampleEvent, audio->sampleInterval * audio->timingFactor - cyclesLate);
 665}
 666
 667bool _resetEnvelope(struct GBAudioEnvelope* envelope) {
 668	envelope->currentVolume = envelope->initialVolume;
 669	_updateEnvelopeDead(envelope);
 670	if (!envelope->dead) {
 671		envelope->nextStep = envelope->stepTime;
 672	}
 673	return envelope->initialVolume || envelope->direction;
 674}
 675
 676void _resetSweep(struct GBAudioSweep* sweep) {
 677	sweep->step = sweep->time;
 678	sweep->enable = (sweep->step != 8) || sweep->shift;
 679	sweep->occurred = false;
 680}
 681
 682bool _writeSweep(struct GBAudioSweep* sweep, uint8_t value) {
 683	sweep->shift = GBAudioRegisterSquareSweepGetShift(value);
 684	bool oldDirection = sweep->direction;
 685	sweep->direction = GBAudioRegisterSquareSweepGetDirection(value);
 686	bool on = true;
 687	if (sweep->occurred && oldDirection && !sweep->direction) {
 688		on = false;
 689	}
 690	sweep->occurred = false;
 691	sweep->time = GBAudioRegisterSquareSweepGetTime(value);
 692	if (!sweep->time) {
 693		sweep->time = 8;
 694	}
 695	return on;
 696}
 697
 698void _writeDuty(struct GBAudioEnvelope* envelope, uint8_t value) {
 699	envelope->length = GBAudioRegisterDutyGetLength(value);
 700	envelope->duty = GBAudioRegisterDutyGetDuty(value);
 701}
 702
 703bool _writeEnvelope(struct GBAudioEnvelope* envelope, uint8_t value) {
 704	envelope->stepTime = GBAudioRegisterSweepGetStepTime(value);
 705	envelope->direction = GBAudioRegisterSweepGetDirection(value);
 706	envelope->initialVolume = GBAudioRegisterSweepGetInitialVolume(value);
 707	if (!envelope->stepTime) {
 708		// TODO: Improve "zombie" mode
 709		++envelope->currentVolume;
 710		envelope->currentVolume &= 0xF;
 711	}
 712	_updateEnvelopeDead(envelope);
 713	envelope->nextStep = envelope->stepTime;
 714	return (envelope->initialVolume || envelope->direction) && envelope->dead != 2;
 715}
 716
 717static void _updateSquareSample(struct GBAudioSquareChannel* ch) {
 718	ch->sample = ch->control.hi * ch->envelope.currentVolume * 0x8;
 719}
 720
 721static int32_t _updateSquareChannel(struct GBAudioSquareChannel* ch) {
 722	ch->control.hi = !ch->control.hi;
 723	_updateSquareSample(ch);
 724	int period = 4 * (2048 - ch->control.frequency);
 725	switch (ch->envelope.duty) {
 726	case 0:
 727		return ch->control.hi ? period : period * 7;
 728	case 1:
 729		return ch->control.hi ? period * 2 : period * 6;
 730	case 2:
 731		return period * 4;
 732	case 3:
 733		return ch->control.hi ? period * 6 : period * 2;
 734	default:
 735		// This should never be hit
 736		return period * 4;
 737	}
 738}
 739
 740static void _updateEnvelope(struct GBAudioEnvelope* envelope) {
 741	if (envelope->direction) {
 742		++envelope->currentVolume;
 743	} else {
 744		--envelope->currentVolume;
 745	}
 746	if (envelope->currentVolume >= 15) {
 747		envelope->currentVolume = 15;
 748		envelope->dead = 1;
 749	} else if (envelope->currentVolume <= 0) {
 750		envelope->currentVolume = 0;
 751		envelope->dead = 2;
 752	} else {
 753		envelope->nextStep = envelope->stepTime;
 754	}
 755}
 756
 757static void _updateEnvelopeDead(struct GBAudioEnvelope* envelope) {
 758	if (!envelope->stepTime) {
 759		envelope->dead = envelope->currentVolume ? 1 : 2;
 760	} else if (!envelope->direction && !envelope->currentVolume) {
 761		envelope->dead = 2;
 762	} else if (envelope->direction && envelope->currentVolume == 0xF) {
 763		envelope->dead = 1;
 764	} else {
 765		envelope->dead = 0;
 766	}
 767}
 768
 769static bool _updateSweep(struct GBAudioSquareChannel* ch, bool initial) {
 770	if (initial || ch->sweep.time != 8) {
 771		int frequency = ch->sweep.realFrequency;
 772		if (ch->sweep.direction) {
 773			frequency -= frequency >> ch->sweep.shift;
 774			if (!initial && frequency >= 0) {
 775				ch->control.frequency = frequency;
 776				ch->sweep.realFrequency = frequency;
 777			}
 778		} else {
 779			frequency += frequency >> ch->sweep.shift;
 780			if (frequency < 2048) {
 781				if (!initial && ch->sweep.shift) {
 782					ch->control.frequency = frequency;
 783					ch->sweep.realFrequency = frequency;
 784					if (!_updateSweep(ch, true)) {
 785						return false;
 786					}
 787				}
 788			} else {
 789				return false;
 790			}
 791		}
 792		ch->sweep.occurred = true;
 793	}
 794	ch->sweep.step = ch->sweep.time;
 795	return true;
 796}
 797
 798static void _updateChannel1(struct mTiming* timing, void* user, uint32_t cyclesLate) {
 799	struct GBAudio* audio = user;
 800	struct GBAudioSquareChannel* ch = &audio->ch1;
 801	int cycles = _updateSquareChannel(ch);
 802	mTimingSchedule(timing, &audio->ch1Event, audio->timingFactor * cycles - cyclesLate);
 803}
 804
 805static void _updateChannel2(struct mTiming* timing, void* user, uint32_t cyclesLate) {
 806	struct GBAudio* audio = user;
 807	struct GBAudioSquareChannel* ch = &audio->ch2;
 808	int cycles = _updateSquareChannel(ch);
 809	mTimingSchedule(timing, &audio->ch2Event, audio->timingFactor * cycles - cyclesLate);
 810}
 811
 812static void _updateChannel3(struct mTiming* timing, void* user, uint32_t cyclesLate) {
 813	struct GBAudio* audio = user;
 814	struct GBAudioWaveChannel* ch = &audio->ch3;
 815	int i;
 816	int volume;
 817	switch (ch->volume) {
 818	case 0:
 819		volume = 0;
 820		break;
 821	case 1:
 822		volume = 4;
 823		break;
 824	case 2:
 825		volume = 2;
 826		break;
 827	case 3:
 828		volume = 1;
 829		break;
 830	default:
 831		volume = 3;
 832		break;
 833	}
 834	int start;
 835	int end;
 836	switch (audio->style) {
 837	case GB_AUDIO_DMG:
 838	default:
 839		++ch->window;
 840		ch->window &= 0x1F;
 841		ch->sample = ch->wavedata8[ch->window >> 1];
 842		if (!(ch->window & 1)) {
 843			ch->sample >>= 4;
 844		}
 845		ch->sample &= 0xF;
 846		break;
 847	case GB_AUDIO_GBA:
 848		if (ch->size) {
 849			start = 7;
 850			end = 0;
 851		} else if (ch->bank) {
 852			start = 7;
 853			end = 4;
 854		} else {
 855			start = 3;
 856			end = 0;
 857		}
 858		uint32_t bitsCarry = ch->wavedata32[end] & 0x000000F0;
 859		uint32_t bits;
 860		for (i = start; i >= end; --i) {
 861			bits = ch->wavedata32[i] & 0x000000F0;
 862			ch->wavedata32[i] = ((ch->wavedata32[i] & 0x0F0F0F0F) << 4) | ((ch->wavedata32[i] & 0xF0F0F000) >> 12);
 863			ch->wavedata32[i] |= bitsCarry << 20;
 864			bitsCarry = bits;
 865		}
 866		ch->sample = bitsCarry >> 4;
 867		break;
 868	}
 869	ch->sample *= volume * 2;
 870	audio->ch3.readable = true;
 871	if (audio->style == GB_AUDIO_DMG) {
 872		mTimingDeschedule(audio->timing, &audio->ch3Fade);
 873		mTimingSchedule(timing, &audio->ch3Fade, 2 - cyclesLate);
 874	}
 875	int cycles = 2 * (2048 - ch->rate);
 876	mTimingSchedule(timing, &audio->ch3Event, audio->timingFactor * cycles - cyclesLate);
 877}
 878static void _fadeChannel3(struct mTiming* timing, void* user, uint32_t cyclesLate) {
 879	UNUSED(timing);
 880	UNUSED(cyclesLate);
 881	struct GBAudio* audio = user;
 882	audio->ch3.readable = false;
 883}
 884
 885static void _updateChannel4(struct mTiming* timing, void* user, uint32_t cyclesLate) {
 886	struct GBAudio* audio = user;
 887	struct GBAudioNoiseChannel* ch = &audio->ch4;
 888
 889	int32_t baseCycles = ch->ratio ? 2 * ch->ratio : 1;
 890	baseCycles <<= ch->frequency;
 891	baseCycles *= 8 * audio->timingFactor;
 892	int32_t cycles = 0;
 893
 894	do {
 895		int lsb = ch->lfsr & 1;
 896		ch->sample = lsb * 0x8;
 897		ch->sample *= ch->envelope.currentVolume;
 898		ch->lfsr >>= 1;
 899		ch->lfsr ^= (lsb * 0x60) << (ch->power ? 0 : 8);
 900		cycles += baseCycles;
 901	} while (cycles + baseCycles < audio->sampleInterval);
 902	mTimingSchedule(timing, &audio->ch4Event, cycles - cyclesLate);
 903}
 904
 905void GBAudioPSGSerialize(const struct GBAudio* audio, struct GBSerializedPSGState* state, uint32_t* flagsOut) {
 906	uint32_t flags = 0;
 907	uint32_t ch1Flags = 0;
 908	uint32_t ch2Flags = 0;
 909	uint32_t ch4Flags = 0;
 910
 911	flags = GBSerializedAudioFlagsSetFrame(flags, audio->frame);
 912	STORE_32LE(audio->frameEvent.when - mTimingCurrentTime(audio->timing), 0, &state->ch1.nextFrame);
 913
 914	flags = GBSerializedAudioFlagsSetCh1Volume(flags, audio->ch1.envelope.currentVolume);
 915	flags = GBSerializedAudioFlagsSetCh1Dead(flags, audio->ch1.envelope.dead);
 916	flags = GBSerializedAudioFlagsSetCh1Hi(flags, audio->ch1.control.hi);
 917	flags = GBSerializedAudioFlagsSetCh1SweepEnabled(flags, audio->ch1.sweep.enable);
 918	flags = GBSerializedAudioFlagsSetCh1SweepOccurred(flags, audio->ch1.sweep.occurred);
 919	ch1Flags = GBSerializedAudioEnvelopeSetLength(ch1Flags, audio->ch1.control.length);
 920	ch1Flags = GBSerializedAudioEnvelopeSetNextStep(ch1Flags, audio->ch1.envelope.nextStep);
 921	ch1Flags = GBSerializedAudioEnvelopeSetFrequency(ch1Flags, audio->ch1.sweep.realFrequency);
 922	STORE_32LE(ch1Flags, 0, &state->ch1.envelope);
 923	STORE_32LE(audio->ch1Event.when - mTimingCurrentTime(audio->timing), 0, &state->ch1.nextEvent);
 924
 925	flags = GBSerializedAudioFlagsSetCh2Volume(flags, audio->ch2.envelope.currentVolume);
 926	flags = GBSerializedAudioFlagsSetCh2Dead(flags, audio->ch2.envelope.dead);
 927	flags = GBSerializedAudioFlagsSetCh2Hi(flags, audio->ch2.control.hi);
 928	ch2Flags = GBSerializedAudioEnvelopeSetLength(ch2Flags, audio->ch2.control.length);
 929	ch2Flags = GBSerializedAudioEnvelopeSetNextStep(ch2Flags, audio->ch2.envelope.nextStep);
 930	STORE_32LE(ch2Flags, 0, &state->ch2.envelope);
 931	STORE_32LE(audio->ch2Event.when - mTimingCurrentTime(audio->timing), 0, &state->ch2.nextEvent);
 932
 933	flags = GBSerializedAudioFlagsSetCh3Readable(flags, audio->ch3.readable);
 934	memcpy(state->ch3.wavebanks, audio->ch3.wavedata32, sizeof(state->ch3.wavebanks));
 935	STORE_16LE(audio->ch3.length, 0, &state->ch3.length);
 936	STORE_32LE(audio->ch3Event.when - mTimingCurrentTime(audio->timing), 0, &state->ch3.nextEvent);
 937	STORE_32LE(audio->ch3Fade.when - mTimingCurrentTime(audio->timing), 0, &state->ch1.nextCh3Fade);
 938
 939	flags = GBSerializedAudioFlagsSetCh4Volume(flags, audio->ch4.envelope.currentVolume);
 940	flags = GBSerializedAudioFlagsSetCh4Dead(flags, audio->ch4.envelope.dead);
 941	STORE_32LE(audio->ch4.lfsr, 0, &state->ch4.lfsr);
 942	ch4Flags = GBSerializedAudioEnvelopeSetLength(ch4Flags, audio->ch4.length);
 943	ch4Flags = GBSerializedAudioEnvelopeSetNextStep(ch4Flags, audio->ch4.envelope.nextStep);
 944	STORE_32LE(ch4Flags, 0, &state->ch4.envelope);
 945	STORE_32LE(audio->ch4Event.when - mTimingCurrentTime(audio->timing), 0, &state->ch4.nextEvent);
 946
 947	STORE_32LE(flags, 0, flagsOut);
 948}
 949
 950void GBAudioPSGDeserialize(struct GBAudio* audio, const struct GBSerializedPSGState* state, const uint32_t* flagsIn) {
 951	uint32_t flags;
 952	uint32_t ch1Flags = 0;
 953	uint32_t ch2Flags = 0;
 954	uint32_t ch4Flags = 0;
 955	uint32_t when;
 956
 957	audio->playingCh1 = !!(*audio->nr52 & 0x0001);
 958	audio->playingCh2 = !!(*audio->nr52 & 0x0002);
 959	audio->playingCh3 = !!(*audio->nr52 & 0x0004);
 960	audio->playingCh4 = !!(*audio->nr52 & 0x0008);
 961	audio->enable = GBAudioEnableGetEnable(*audio->nr52);
 962
 963	LOAD_32LE(when, 0, &state->ch1.nextFrame);
 964	mTimingSchedule(audio->timing, &audio->frameEvent, when);
 965
 966	LOAD_32LE(flags, 0, flagsIn);
 967	audio->frame = GBSerializedAudioFlagsGetFrame(flags);
 968
 969	LOAD_32LE(ch1Flags, 0, &state->ch1.envelope);
 970	audio->ch1.envelope.currentVolume = GBSerializedAudioFlagsGetCh1Volume(flags);
 971	audio->ch1.envelope.dead = GBSerializedAudioFlagsGetCh1Dead(flags);
 972	audio->ch1.control.hi = GBSerializedAudioFlagsGetCh1Hi(flags);
 973	audio->ch1.sweep.enable = GBSerializedAudioFlagsGetCh1SweepEnabled(flags);
 974	audio->ch1.sweep.occurred = GBSerializedAudioFlagsGetCh1SweepOccurred(flags);
 975	audio->ch1.control.length = GBSerializedAudioEnvelopeGetLength(ch1Flags);
 976	audio->ch1.envelope.nextStep = GBSerializedAudioEnvelopeGetNextStep(ch1Flags);
 977	audio->ch1.sweep.realFrequency = GBSerializedAudioEnvelopeGetFrequency(ch1Flags);
 978	LOAD_32LE(when, 0, &state->ch1.nextEvent);
 979	if (audio->ch1.envelope.dead < 2 && audio->playingCh1) {
 980		mTimingSchedule(audio->timing, &audio->ch1Event, when);
 981	}
 982
 983	LOAD_32LE(ch2Flags, 0, &state->ch2.envelope);
 984	audio->ch2.envelope.currentVolume = GBSerializedAudioFlagsGetCh2Volume(flags);
 985	audio->ch2.envelope.dead = GBSerializedAudioFlagsGetCh2Dead(flags);
 986	audio->ch2.control.hi = GBSerializedAudioFlagsGetCh2Hi(flags);
 987	audio->ch2.control.length = GBSerializedAudioEnvelopeGetLength(ch2Flags);
 988	audio->ch2.envelope.nextStep = GBSerializedAudioEnvelopeGetNextStep(ch2Flags);
 989	LOAD_32LE(when, 0, &state->ch2.nextEvent);
 990	if (audio->ch2.envelope.dead < 2 && audio->playingCh2) {
 991		mTimingSchedule(audio->timing, &audio->ch2Event, when);
 992	}
 993
 994	audio->ch3.readable = GBSerializedAudioFlagsGetCh3Readable(flags);
 995	// TODO: Big endian?
 996	memcpy(audio->ch3.wavedata32, state->ch3.wavebanks, sizeof(audio->ch3.wavedata32));
 997	LOAD_16LE(audio->ch3.length, 0, &state->ch3.length);
 998	LOAD_32LE(when, 0, &state->ch3.nextEvent);
 999	if (audio->playingCh3) {
1000		mTimingSchedule(audio->timing, &audio->ch3Event, when);
1001	}
1002	LOAD_32LE(when, 0, &state->ch1.nextCh3Fade);
1003	if (audio->ch3.readable && audio->style == GB_AUDIO_DMG) {
1004		mTimingSchedule(audio->timing, &audio->ch3Fade, when);
1005	}
1006
1007	LOAD_32LE(ch4Flags, 0, &state->ch4.envelope);
1008	audio->ch4.envelope.currentVolume = GBSerializedAudioFlagsGetCh4Volume(flags);
1009	audio->ch4.envelope.dead = GBSerializedAudioFlagsGetCh4Dead(flags);
1010	audio->ch4.length = GBSerializedAudioEnvelopeGetLength(ch4Flags);
1011	audio->ch4.envelope.nextStep = GBSerializedAudioEnvelopeGetNextStep(ch4Flags);
1012	LOAD_32LE(audio->ch4.lfsr, 0, &state->ch4.lfsr);
1013	LOAD_32LE(when, 0, &state->ch4.nextEvent);
1014	if (audio->ch4.envelope.dead < 2 && audio->playingCh4) {
1015		mTimingSchedule(audio->timing, &audio->ch4Event, when);
1016	}
1017}
1018
1019void GBAudioSerialize(const struct GBAudio* audio, struct GBSerializedState* state) {
1020	GBAudioPSGSerialize(audio, &state->audio.psg, &state->audio.flags);
1021	STORE_32LE(audio->sampleEvent.when - mTimingCurrentTime(audio->timing), 0, &state->audio.nextSample);
1022}
1023
1024void GBAudioDeserialize(struct GBAudio* audio, const struct GBSerializedState* state) {
1025	GBAudioPSGDeserialize(audio, &state->audio.psg, &state->audio.flags);
1026	uint32_t when;
1027	LOAD_32LE(when, 0, &state->audio.nextSample);
1028	mTimingSchedule(audio->timing, &audio->sampleEvent, when);
1029}