From 3ca82b64afa0ffd66fba1f0facb3b82176fa4381 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Mon, 15 Mar 2021 20:45:21 -0700 Subject: [PATCH] GB Audio: Fix some channel 4 timing edge cases --- CHANGES | 1 + src/gb/audio.c | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index 593b8278c..f583abfc0 100644 --- a/CHANGES +++ b/CHANGES @@ -29,6 +29,7 @@ Emulation fixes: - GB: Downgrade DMG-only ROMs from CGB mode even without boot ROM - GB: Fix marking BIOS as unmapped when skipping BIOS (fixes mgba.io/i/2061) - GB Audio: Fix serializing sweep time + - GB Audio: Fix some channel 4 timing edge cases - GB MBC: Fix MBC1 mode changing behavior - GB MBC: Fix some MBC3 bit masking - GB Video: Fix state after skipping BIOS (fixes mgba.io/i/1715 and mgba.io/i/1716) diff --git a/src/gb/audio.c b/src/gb/audio.c index f20fa3fac..121db67d2 100644 --- a/src/gb/audio.c +++ b/src/gb/audio.c @@ -390,6 +390,7 @@ void GBAudioWriteNR44(struct GBAudio* audio, uint8_t value) { } } if (audio->playingCh4 && audio->ch4.envelope.dead != 2) { + audio->ch4.lastEvent = mTimingCurrentTime(audio->timing); mTimingDeschedule(audio->timing, &audio->ch4Event); mTimingSchedule(audio->timing, &audio->ch4Event, 0); } @@ -581,14 +582,16 @@ void GBAudioUpdateFrame(struct GBAudio* audio, struct mTiming* timing) { if (audio->playingCh4 && !audio->ch4.envelope.dead) { --audio->ch4.envelope.nextStep; if (audio->ch4.envelope.nextStep == 0) { - int8_t sample = audio->ch4.sample > 0; - audio->ch4.samples -= audio->ch4.sample; + int8_t sample = audio->ch4.sample; _updateEnvelope(&audio->ch4.envelope); if (audio->ch4.envelope.dead == 2) { mTimingDeschedule(timing, &audio->ch4Event); } - audio->ch4.sample = sample * audio->ch4.envelope.currentVolume; - audio->ch4.samples += audio->ch4.sample; + audio->ch4.sample = (sample > 0) * audio->ch4.envelope.currentVolume; + if (audio->ch4.nSamples) { + audio->ch4.samples -= sample; + audio->ch4.samples += audio->ch4.sample; + } } } break;