From 41a9f5d345c9443e71002737accc44d3347c2dae Mon Sep 17 00:00:00 2001 From: Sean Kelly Date: Mon, 30 Dec 2024 20:45:57 -0800 Subject: [PATCH] Add log and inverse log for float scaling --- .../Effect-Modules/base_effect_module.cpp | 30 +++++++++++++++---- .../Effect-Modules/base_effect_module.h | 1 + 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Software/GuitarPedal/Effect-Modules/base_effect_module.cpp b/Software/GuitarPedal/Effect-Modules/base_effect_module.cpp index 8697f5d..f1c1a6e 100644 --- a/Software/GuitarPedal/Effect-Modules/base_effect_module.cpp +++ b/Software/GuitarPedal/Effect-Modules/base_effect_module.cpp @@ -247,7 +247,7 @@ void BaseEffectModule::SetParameterAsMagnitude(int parameter_id, float value) { int max = GetParameterMax(parameter_id); // Handle different ParameterValueTypes Correctly - ParameterValueType paramType = (ParameterValueType)GetParameterType(parameter_id); + ParameterValueType paramType = GetParameterType(parameter_id); if (paramType == ParameterValueType::Raw) { // This is an unsupported operation, so do nothing. @@ -262,11 +262,29 @@ void BaseEffectModule::SetParameterAsMagnitude(int parameter_id, float value) { return; } - // TODO: if param curve is log apply it here - - // Use the 0..1 magnitue to set the underlying value to between min..max - float tmp = (value * ((float)max - (float)min) + (float)min); - SetParameterAsFloat(parameter_id, tmp); + // Set the scaled parameter using the magnitude and the curve type + switch (GetParameterValueCurve(parameter_id)) { + case ParameterValueCurve::Log: { + // Logarithmic scaling: interpolate in the log domain + const float tmp = std::pow(10.0, std::log10((float)min) + value * (std::log10((float)max) - std::log10((float)min))); + SetParameterAsFloat(parameter_id, tmp); + break; + } + case ParameterValueCurve::InverseLog: { + // Inverse Logarithmic scaling: reverse the mapping + const float tmp = + std::pow(10.0, std::log10((float)min) + (1.0f - value) * (std::log10((float)max) - std::log10((float)min))); + SetParameterAsFloat(parameter_id, tmp); + break; + } + case ParameterValueCurve::Linear: + default: { + // Linear scaling: simple interpolation + const float tmp = (value * ((float)max - (float)min) + (float)min); + SetParameterAsFloat(parameter_id, tmp); + break; + } + } } else if (paramType == ParameterValueType::Bool) { // Set the Bool Value to False if the magnitude is less then 0.5f, true otherwise if (value < 0.5f) { diff --git a/Software/GuitarPedal/Effect-Modules/base_effect_module.h b/Software/GuitarPedal/Effect-Modules/base_effect_module.h index 697d366..e690d3b 100644 --- a/Software/GuitarPedal/Effect-Modules/base_effect_module.h +++ b/Software/GuitarPedal/Effect-Modules/base_effect_module.h @@ -30,6 +30,7 @@ union ParameterValue { enum ParameterValueCurve { Linear, Log, + InverseLog, ParameterValueCurve_LAST, // Last enum item };