From 053598fa7f0eb01cb16184223e2359f24460d89a Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Wed, 21 Dec 2016 18:44:08 -0800 Subject: [PATCH] GB Audio: Simplify envelope code --- CHANGES | 1 + src/gb/audio.c | 44 ++++++++++++++++++++------------------------ 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/CHANGES b/CHANGES index bb1824606..f1781d2ce 100644 --- a/CHANGES +++ b/CHANGES @@ -57,6 +57,7 @@ Misc: - Util: Add size counting to Table - Qt: Move last directory setting from qt.ini to config.ini - 3DS, PSP2, Wii: Last directory loaded is saved + - GB Audio: Simplify envelope code 0.5.1: (2016-10-05) Bugfixes: diff --git a/src/gb/audio.c b/src/gb/audio.c index f8211c11e..1dc39d15c 100644 --- a/src/gb/audio.c +++ b/src/gb/audio.c @@ -26,6 +26,7 @@ static void _writeDuty(struct GBAudioEnvelope* envelope, uint8_t value); static bool _writeSweep(struct GBAudioEnvelope* envelope, uint8_t value); static int32_t _updateSquareChannel(struct GBAudioSquareControl* envelope, int duty); static void _updateEnvelope(struct GBAudioEnvelope* envelope); +static void _updateEnvelopeDead(struct GBAudioEnvelope* envelope); static bool _updateSweep(struct GBAudioChannel1* ch, bool initial); static int32_t _updateChannel1(struct GBAudioChannel1* ch); static int32_t _updateChannel2(struct GBAudioChannel2* ch); @@ -146,11 +147,8 @@ void GBAudioWriteNR14(struct GBAudio* audio, uint8_t value) { if (GBAudioRegisterControlIsRestart(value << 8)) { audio->playingCh1 = audio->ch1.envelope.initialVolume || audio->ch1.envelope.direction; audio->ch1.envelope.currentVolume = audio->ch1.envelope.initialVolume; - if (audio->ch1.envelope.currentVolume > 0) { - audio->ch1.envelope.dead = audio->ch1.envelope.stepTime ? 0 : 1; - } else { - audio->ch1.envelope.dead = audio->ch1.envelope.stepTime ? 0 : 2; - } + _updateEnvelopeDead(&audio->ch1.envelope); + if (audio->nextEvent == INT_MAX) { audio->eventDiff = 0; } @@ -209,11 +207,8 @@ void GBAudioWriteNR24(struct GBAudio* audio, uint8_t value) { if (GBAudioRegisterControlIsRestart(value << 8)) { audio->playingCh2 = audio->ch2.envelope.initialVolume || audio->ch2.envelope.direction; audio->ch2.envelope.currentVolume = audio->ch2.envelope.initialVolume; - if (audio->ch2.envelope.currentVolume > 0) { - audio->ch2.envelope.dead = audio->ch2.envelope.stepTime ? 0 : 1; - } else { - audio->ch2.envelope.dead = audio->ch2.envelope.stepTime ? 0 : 2; - } + _updateEnvelopeDead(&audio->ch2.envelope); + if (audio->nextEvent == INT_MAX) { audio->eventDiff = 0; } @@ -330,11 +325,8 @@ void GBAudioWriteNR44(struct GBAudio* audio, uint8_t value) { if (GBAudioRegisterNoiseControlIsRestart(value)) { audio->playingCh4 = audio->ch4.envelope.initialVolume || audio->ch4.envelope.direction; audio->ch4.envelope.currentVolume = audio->ch4.envelope.initialVolume; - if (audio->ch4.envelope.currentVolume > 0) { - audio->ch4.envelope.dead = audio->ch4.envelope.stepTime ? 0 : 1; - } else { - audio->ch4.envelope.dead = audio->ch4.envelope.stepTime ? 0 : 2; - } + _updateEnvelopeDead(&audio->ch4.envelope); + if (audio->ch4.power) { audio->ch4.lfsr = 0x40; } else { @@ -689,15 +681,7 @@ bool _writeSweep(struct GBAudioEnvelope* envelope, uint8_t value) { envelope->stepTime = GBAudioRegisterSweepGetStepTime(value); envelope->direction = GBAudioRegisterSweepGetDirection(value); envelope->initialVolume = GBAudioRegisterSweepGetInitialVolume(value); - if (envelope->stepTime == 0) { - envelope->dead = envelope->currentVolume ? 1 : 2; - } else if (!envelope->direction && !envelope->currentVolume) { - envelope->dead = 2; - } else if (envelope->direction && envelope->currentVolume == 0xF) { - envelope->dead = 1; - } else { - envelope->dead = 0; - } + _updateEnvelopeDead(envelope); envelope->nextStep = envelope->stepTime; return envelope->initialVolume || envelope->direction; } @@ -737,6 +721,18 @@ static void _updateEnvelope(struct GBAudioEnvelope* envelope) { } } +static void _updateEnvelopeDead(struct GBAudioEnvelope* envelope) { + if (envelope->stepTime == 0) { + envelope->dead = envelope->currentVolume ? 1 : 2; + } else if (!envelope->direction && !envelope->currentVolume) { + envelope->dead = 2; + } else if (envelope->direction && envelope->currentVolume == 0xF) { + envelope->dead = 1; + } else { + envelope->dead = 0; + } +} + static bool _updateSweep(struct GBAudioChannel1* ch, bool initial) { if (initial || ch->time != 8) { int frequency = ch->realFrequency;