From 06e7086f023a56503835507da96e2f3363a95d4f Mon Sep 17 00:00:00 2001 From: Sean Kelly Date: Sun, 26 Jan 2025 00:07:35 -0800 Subject: [PATCH] Cleanup in distortion module for readability --- .../Effect-Modules/distortion_module.cpp | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/Software/GuitarPedal/Effect-Modules/distortion_module.cpp b/Software/GuitarPedal/Effect-Modules/distortion_module.cpp index 636f476..d4326ce 100644 --- a/Software/GuitarPedal/Effect-Modules/distortion_module.cpp +++ b/Software/GuitarPedal/Effect-Modules/distortion_module.cpp @@ -1,12 +1,15 @@ #include "distortion_module.h" +#include #include using namespace bkshepherd; static const char *s_clippingOptions[5] = {"Hard Clip", "Soft Clip", "Fuzz", "Tube", "Multi Stage"}; -cycfi::q::highpass preFilter(80.0f, 48000); -cycfi::q::lowpass postFilter(8000.0f, 48000); +constexpr float preFilterCutoff = 80.0f; +constexpr float postFilterCutoff = 8000.0f; +cycfi::q::highpass preFilter(preFilterCutoff, 48000); +cycfi::q::lowpass postFilter(postFilterCutoff, 48000); constexpr uint8_t oversamplingFactor = 8; static const int s_paramCount = 6; @@ -91,12 +94,12 @@ void DistortionModule::Init(float sample_rate) { } void DistortionModule::InitializeFilters() { - preFilter.config(80.0f, GetSampleRate()); + preFilter.config(preFilterCutoff, GetSampleRate()); if (m_oversampling) { - postFilter.config(8000.0f, GetSampleRate() * oversamplingFactor); + postFilter.config(postFilterCutoff, GetSampleRate() * oversamplingFactor); } else { - postFilter.config(8000.0f, GetSampleRate()); + postFilter.config(postFilterCutoff, GetSampleRate()); } } @@ -110,15 +113,7 @@ void DistortionModule::ParameterChanged(int parameter_id) { } } -float hardClipping(float input, float threshold) { - if (input > threshold) { - return threshold; - } else if (input < -threshold) { - return -threshold; - } else { - return input; - } -} +float hardClipping(float input, float threshold) { return std::clamp(input, -threshold, threshold); } float softClipping(float input, float gain) { return std::tanh(input * gain); } @@ -236,7 +231,7 @@ void DistortionModule::ProcessMono(float in) { if (m_oversampling) { // Prepare signal for oversampling std::vector monoInput = {distorted}; - std::vector oversampledInput = upsample(monoInput, oversamplingFactor, GetSampleRate()); // 4x oversampling + std::vector oversampledInput = upsample(monoInput, oversamplingFactor, GetSampleRate()); // Apply gain and distortion processing for (float &sample : oversampledInput) { @@ -279,12 +274,12 @@ float DistortionModule::ProcessTiltToneControl(float input) { const float toneAmount = GetParameterAsFloat(2); // Process input with one-pole low-pass - float lp = m_tone.Process(input); + const float lp = m_tone.Process(input); // Compute the high-passed portion - float hp = input - lp; + const float hp = input - lp; - // Crossfade: toneAmount=0 => all LP (bassier), toneAmount=1 => all HP (treblier) + // Crossfade: toneAmount=0 => all LP (more bass), toneAmount=1 => all HP (more treble) return lp * (1.f - toneAmount) + hp * toneAmount; }