From 6e28a8b01ca2f8d00ba398af4a61ac56ad40fe18 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Thu, 24 Aug 2023 14:23:00 -0500 Subject: [PATCH] WaveSynth: fix distorted sound issue #1411 --- src/engine/waveSynth.cpp | 28 +++++++++++++++++++++++----- src/engine/waveSynth.h | 3 ++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/engine/waveSynth.cpp b/src/engine/waveSynth.cpp index cdb696881..6fe34f261 100644 --- a/src/engine/waveSynth.cpp +++ b/src/engine/waveSynth.cpp @@ -22,6 +22,22 @@ #include "instrument.h" #include "../ta-log.h" +inline bool effectOnlyAltersOutput(unsigned char effect) { + switch (effect) { + case DIV_WS_NONE: + case DIV_WS_INVERT: + case DIV_WS_ADD: + case DIV_WS_SUBTRACT: + case DIV_WS_AVERAGE: + return true; + break; + default: + return false; + break; + } + return false; +} + bool DivWaveSynth::activeChanged() { if (activeChangedB) { activeChangedB=false; @@ -211,19 +227,21 @@ void DivWaveSynth::setWidth(int val) { if (width>256) width=256; } -void DivWaveSynth::changeWave1(int num) { +#define SHALL_UPDATE_OUT (!state.enabled || force || (state.enabled && effectOnlyAltersOutput(state.effect))) + +void DivWaveSynth::changeWave1(int num, bool force) { DivWavetable* w1=e->getWave(num); if (width<1) return; for (int i=0; imax<1 || w1->len<1) { wave1[i]=0; - output[i]=0; + if (SHALL_UPDATE_OUT) output[i]=0; } else { int data=w1->data[i*w1->len/width]*height/w1->max; if (data<0) data=0; if (data>height) data=height; wave1[i]=data; - output[i]=data; + if (SHALL_UPDATE_OUT) output[i]=data; } } first=true; @@ -280,9 +298,9 @@ void DivWaveSynth::init(DivInstrument* which, int w, int h, bool insChanged) { divCounter=0; subDivCounter=0; - changeWave1(state.wave1); + changeWave1(state.wave1,true); changeWave2(state.wave2); - tick(true); + //tick(true); // ??? first=true; } } diff --git a/src/engine/waveSynth.h b/src/engine/waveSynth.h index 81a3cbe19..21b6055ff 100644 --- a/src/engine/waveSynth.h +++ b/src/engine/waveSynth.h @@ -55,8 +55,9 @@ class DivWaveSynth { /** * change the first wave. * @param num wavetable number. + * @param force whether to force overwriting the current wave. */ - void changeWave1(int num); + void changeWave1(int num, bool force=false); /** * change the second wave. * @param num wavetable number.