Skip to content

Commit

Permalink
Add log and inverse log for float scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
xconverge committed Dec 31, 2024
1 parent 7fae3b9 commit 41a9f5d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
30 changes: 24 additions & 6 deletions Software/GuitarPedal/Effect-Modules/base_effect_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions Software/GuitarPedal/Effect-Modules/base_effect_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ union ParameterValue {
enum ParameterValueCurve {
Linear,
Log,
InverseLog,
ParameterValueCurve_LAST, // Last enum item
};

Expand Down

0 comments on commit 41a9f5d

Please sign in to comment.