all repos — mgba @ 246142fd551a1e8ba3cd41a49a1821505ed874da

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