From 2901efa7d547d77aec467bb1f41a623158fe3c18 Mon Sep 17 00:00:00 2001 From: Vincent Berthiaume Date: Fri, 25 Oct 2024 11:50:11 -0400 Subject: [PATCH] =?UTF-8?q?finally=20glitch-free=20=F0=9F=A5=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/DSP/PhatEffectsProcessor.hpp | 79 +++++++++++++++-------------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/source/DSP/PhatEffectsProcessor.hpp b/source/DSP/PhatEffectsProcessor.hpp index c02352a..164c0d9 100644 --- a/source/DSP/PhatEffectsProcessor.hpp +++ b/source/DSP/PhatEffectsProcessor.hpp @@ -46,23 +46,24 @@ class EffectsCrossfadeProcessor void changeEffect() { + //TODO: probagate back the failure to change the effect + //first reverse the smoothedGain. If we're not at one of the extremes, we got a double click which we'll ignore + if (smoothedGain.getTargetValue() == 1.) + smoothedGain.setTargetValue (0.); + else if (smoothedGain.getTargetValue() == 0.) + smoothedGain.setTargetValue (1.); + else + { + jassertfalse; + return; + } + if (curEffect == EffectType::verb) curEffect = EffectType::chorus; else if (curEffect == EffectType::chorus) curEffect = EffectType::verb; else jassertfalse; - - if (curEffect == EffectType::verb) - smoothedGain.setTargetValue (1.); - else - smoothedGain.setTargetValue (0.); - - //this does not resolve the click we get on the first transition - //if (curEffect == EffectType::verb) - // smoothedGain.setTargetValue (0.); - //else - // smoothedGain.setTargetValue (1.); }; EffectType getCurrentEffectType() const @@ -74,37 +75,44 @@ class EffectsCrossfadeProcessor return curEffect; } - /** so this needs to be a previous and next buffer, and the smoothing needs to always be 1 -> 0. - * I don' think this class should know anything about the processors... actually it should just get - * references to the processors, or even just their processed outputs? - */ - void process (const juce::AudioBuffer& leftBuffer, - const juce::AudioBuffer& rightBuffer, - juce::AudioBuffer& outputBuffer) + void process (const juce::AudioBuffer& previousEffectBuffer, + const juce::AudioBuffer& nextEffectBuffer, + juce::AudioBuffer& outputBuffer) { - jassert (leftBuffer.getNumChannels() == rightBuffer.getNumChannels() && rightBuffer.getNumChannels() == outputBuffer.getNumChannels()); - jassert (leftBuffer.getNumSamples() == rightBuffer.getNumSamples() && rightBuffer.getNumSamples() == outputBuffer.getNumSamples()); + jassert (previousEffectBuffer.getNumChannels() == nextEffectBuffer.getNumChannels() && nextEffectBuffer.getNumChannels() == outputBuffer.getNumChannels()); + jassert (previousEffectBuffer.getNumSamples() == nextEffectBuffer.getNumSamples() && nextEffectBuffer.getNumSamples() == outputBuffer.getNumSamples()); const auto channels = outputBuffer.getNumChannels(); const auto samples = outputBuffer.getNumSamples(); + const auto needToInverse = smoothedGain.getTargetValue() == 1; for (int channel = 0; channel < channels; ++channel) { for (int sample = 0; sample < samples; ++sample) { - // obtain the input samples from their respective buffers - const auto left = leftBuffer.getSample (channel, sample); - const auto right = rightBuffer.getSample (channel, sample); - - // get the next gain value in the smoothed ramp towards target - const auto gain = smoothedGain.getNextValue(); - DBG(gain); - - // calculate the output sample as a mix of left and right - auto output = left * gain + right * (1.0 - gain); - - // store the output sample value - outputBuffer.setSample (channel, sample, static_cast (output)); + //TODO: get this outta the loop lmao + if (needToInverse) + { + //get individual samples + const auto prev = previousEffectBuffer.getSample (channel, sample); + const auto next = nextEffectBuffer.getSample (channel, sample); + + //mix and send to output + const auto gain = 1 - smoothedGain.getNextValue(); + auto output = prev * gain + next * (1.0 - gain); + outputBuffer.setSample (channel, sample, static_cast (output)); + } + else + { + //get individual samples + const auto prev = nextEffectBuffer.getSample (channel, sample); + const auto next = previousEffectBuffer.getSample (channel, sample); + + //mix and send to output + const auto gain = smoothedGain.getNextValue(); + auto output = prev * gain + next * (1.0 - gain); + outputBuffer.setSample (channel, sample, static_cast (output)); + } } } } @@ -114,9 +122,6 @@ class EffectsCrossfadeProcessor //TODO: this needs to be stored and retreived from the state EffectType curEffect = EffectType::verb; - - //changing the default curEffect to chorus DOES fix the click on first transition. Weird man - //EffectType curEffect = EffectType::chorus; }; //================================================== @@ -237,4 +242,4 @@ class EffectsProcessor juce::AudioBuffer fade_buffer1, fade_buffer2; EffectsCrossfadeProcessor effectCrossFader; -}; \ No newline at end of file +};