Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors for safety and ability to use different curve for float parameters #49

Merged
merged 7 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Software/GuitarPedal/Effect-Modules/autopan_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void AutoPanModule::SetTempo(uint32_t bpm) {
}
}

float AutoPanModule::GetBrightnessForLED(int led_id) {
float AutoPanModule::GetBrightnessForLED(int led_id) const {
float value = BaseEffectModule::GetBrightnessForLED(led_id);

if (led_id == 1) {
Expand Down
2 changes: 1 addition & 1 deletion Software/GuitarPedal/Effect-Modules/autopan_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AutoPanModule : public BaseEffectModule {
void ProcessMono(float in) override;
void ProcessStereo(float inL, float inR) override;
void SetTempo(uint32_t bpm) override;
float GetBrightnessForLED(int led_id) override;
float GetBrightnessForLED(int led_id) const override;
void UpdateUI(float elapsedTime) override;
void DrawUI(OneBitGraphicsDisplay &display, int currentIndex, int numItemsTotal, Rectangle boundsToDrawIn,
bool isEditing) override;
Expand Down
87 changes: 58 additions & 29 deletions Software/GuitarPedal/Effect-Modules/base_effect_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void BaseEffectModule::InitParams(int count) {
// Init all parameters to their default value or zero if there is no meta data
for (int i = 0; i < m_paramCount; i++) {
if (m_paramMetaData != NULL) {
if ((ParameterValueType)GetParameterType(i) == ParameterValueType::Float) {
if (GetParameterType(i) == ParameterValueType::Float) {
uint32_t tmp;
float value = m_paramMetaData[i].defaultValue.float_value;
std::memcpy(&tmp, &value, sizeof(float));
Expand All @@ -55,21 +55,21 @@ void BaseEffectModule::InitParams(int count) {
}
}

uint16_t BaseEffectModule::GetParameterCount() { return m_paramCount; }
uint16_t BaseEffectModule::GetParameterCount() const { return m_paramCount; }

uint16_t BaseEffectModule::GetPresetCount() { return m_presetCount; }
uint16_t BaseEffectModule::GetPresetCount() const { return m_presetCount; }

void BaseEffectModule::SetPresetCount(uint16_t preset_count) { m_presetCount = preset_count; }

void BaseEffectModule::SetCurrentPreset(uint32_t preset) { m_currentPreset = preset; }

uint32_t BaseEffectModule::GetCurrentPreset() { return m_currentPreset; }
uint32_t BaseEffectModule::GetCurrentPreset() const { return m_currentPreset; }

void BaseEffectModule::SetSettingsArrayStartIdx(uint32_t start_idx) { m_settingsArrayStartIdx = start_idx; }

uint32_t BaseEffectModule::GetSettingsArrayStartIdx() { return m_settingsArrayStartIdx; }
uint32_t BaseEffectModule::GetSettingsArrayStartIdx() const { return m_settingsArrayStartIdx; }

const char *BaseEffectModule::GetParameterName(int parameter_id) {
const char *BaseEffectModule::GetParameterName(int parameter_id) const {
// Make sure parameter_id is valid.
if (m_params == NULL || parameter_id < 0 || parameter_id >= m_paramCount || m_paramMetaData == NULL) {
return "Unknown";
Expand All @@ -78,30 +78,39 @@ const char *BaseEffectModule::GetParameterName(int parameter_id) {
return m_paramMetaData[parameter_id].name;
}

int BaseEffectModule::GetParameterType(int parameter_id) {
ParameterValueType BaseEffectModule::GetParameterType(int parameter_id) const {
// Make sure parameter_id is valid.
if (m_params == NULL || parameter_id < 0 || parameter_id >= m_paramCount || m_paramMetaData == NULL) {
return -1;
return ParameterValueType::Unknown;
}

return m_paramMetaData[parameter_id].valueType;
}

int BaseEffectModule::GetParameterBinCount(int parameter_id) {
ParameterValueCurve BaseEffectModule::GetParameterValueCurve(int parameter_id) const {
// Make sure parameter_id is valid.
if (m_params == NULL || parameter_id < 0 || parameter_id >= m_paramCount || m_paramMetaData == NULL) {
return ParameterValueCurve::Linear;
}

return m_paramMetaData[parameter_id].valueCurve;
}

int BaseEffectModule::GetParameterBinCount(int parameter_id) const {
// Make sure parameter_id is valid.
if (m_params == NULL || parameter_id < 0 || parameter_id >= m_paramCount || m_paramMetaData == NULL) {
return -1;
}

// Check to see if this is a binned type, if not return -1
if ((ParameterValueType)GetParameterType(parameter_id) != ParameterValueType::Binned) {
if (GetParameterType(parameter_id) != ParameterValueType::Binned) {
return -1;
}

return m_paramMetaData[parameter_id].valueBinCount;
}

const char **BaseEffectModule::GetParameterBinNames(int parameter_id) {
const char **BaseEffectModule::GetParameterBinNames(int parameter_id) const {
// Make sure parameter_id is valid.
if (m_params == NULL || parameter_id < 0 || parameter_id >= m_paramCount || m_paramMetaData == NULL) {
return NULL;
Expand All @@ -118,7 +127,7 @@ const char **BaseEffectModule::GetParameterBinNames(int parameter_id) {
return m_paramMetaData[parameter_id].valueBinNames;
}

const float BaseEffectModule::GetParameterDefaultValueAsFloat(int parameter_id) {
const float BaseEffectModule::GetParameterDefaultValueAsFloat(int parameter_id) const {
// Make sure parameter_id is valid.
if (m_params == NULL || parameter_id < 0 || parameter_id >= m_paramCount || m_paramMetaData == NULL) {
return 0.0f;
Expand All @@ -127,7 +136,7 @@ const float BaseEffectModule::GetParameterDefaultValueAsFloat(int parameter_id)
return m_paramMetaData[parameter_id].defaultValue.float_value;
}

uint32_t BaseEffectModule::GetParameterRaw(int parameter_id) {
uint32_t BaseEffectModule::GetParameterRaw(int parameter_id) const {
// Make sure parameter_id is valid.
if (m_params == NULL || parameter_id < 0 || parameter_id >= m_paramCount) {
return 0;
Expand All @@ -136,7 +145,7 @@ uint32_t BaseEffectModule::GetParameterRaw(int parameter_id) {
return m_params[parameter_id];
}

float BaseEffectModule::GetParameterAsFloat(int parameter_id) {
float BaseEffectModule::GetParameterAsFloat(int parameter_id) const {
if (parameter_id >= 0 || parameter_id < m_paramCount) {
float ret;
uint32_t tmp = m_params[parameter_id];
Expand All @@ -146,9 +155,9 @@ float BaseEffectModule::GetParameterAsFloat(int parameter_id) {
return -1.0f;
}

bool BaseEffectModule::GetParameterAsBool(int parameter_id) { return (GetParameterRaw(parameter_id) > 0); }
bool BaseEffectModule::GetParameterAsBool(int parameter_id) const { return (GetParameterRaw(parameter_id) > 0); }

int BaseEffectModule::GetParameterAsBinnedValue(int parameter_id) {
int BaseEffectModule::GetParameterAsBinnedValue(int parameter_id) const {
int binCount = GetParameterBinCount(parameter_id);

// If this is not a binned value type aways return 1
Expand All @@ -171,7 +180,7 @@ int BaseEffectModule::GetParameterAsBinnedValue(int parameter_id) {
return bin;
}

int BaseEffectModule::GetMappedParameterIDForKnob(int knob_id) {
int BaseEffectModule::GetMappedParameterIDForKnob(int knob_id) const {
if (m_paramMetaData != NULL) {
for (int i = 0; i < m_paramCount; i++) {
if (m_paramMetaData[i].knobMapping == knob_id) {
Expand All @@ -183,7 +192,7 @@ int BaseEffectModule::GetMappedParameterIDForKnob(int knob_id) {
return -1;
}

int BaseEffectModule::GetMappedParameterIDForMidiCC(int midiCC_id) {
int BaseEffectModule::GetMappedParameterIDForMidiCC(int midiCC_id) const {
if (m_paramMetaData != NULL) {
for (int i = 0; i < m_paramCount; i++) {
if (m_paramMetaData[i].midiCCMapping == midiCC_id) {
Expand All @@ -195,23 +204,23 @@ int BaseEffectModule::GetMappedParameterIDForMidiCC(int midiCC_id) {
return -1;
}

int BaseEffectModule::GetParameterMin(int parameter_id) {
int BaseEffectModule::GetParameterMin(int parameter_id) const {
if (m_paramMetaData != NULL && parameter_id < m_paramCount) {
return m_paramMetaData[parameter_id].minValue;
}

return -1;
}

int BaseEffectModule::GetParameterMax(int parameter_id) {
int BaseEffectModule::GetParameterMax(int parameter_id) const {
if (m_paramMetaData != NULL && parameter_id < m_paramCount) {
return m_paramMetaData[parameter_id].maxValue;
}

return -1;
}

float BaseEffectModule::GetParameterFineStepSize(int parameter_id) {
float BaseEffectModule::GetParameterFineStepSize(int parameter_id) const {
if (m_paramMetaData != NULL && parameter_id < m_paramCount) {
return m_paramMetaData[parameter_id].fineStepSize;
}
Expand All @@ -238,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 @@ -253,9 +262,29 @@ void BaseEffectModule::SetParameterAsMagnitude(int parameter_id, float value) {
return;
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this chunk is the main implementation change

// 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 Expand Up @@ -327,11 +356,11 @@ void BaseEffectModule::ProcessStereo(float inL, float inR) {
m_audioRight = inR;
}

float BaseEffectModule::GetAudioLeft() { return m_audioLeft; }
float BaseEffectModule::GetAudioLeft() const { return m_audioLeft; }

float BaseEffectModule::GetAudioRight() { return m_audioRight; }
float BaseEffectModule::GetAudioRight() const { return m_audioRight; }

float BaseEffectModule::GetBrightnessForLED(int led_id) {
float BaseEffectModule::GetBrightnessForLED(int led_id) const {
// By default will always return 1.0f if the effect is enabled and 0.0f if the effect is bypassed.
// Each effect module is expected to override this function to treat the LEDs apropriately.
// By convention LED_ID 0 should always reflect the status of the Effect as Enabled or Bypassed.
Expand All @@ -345,7 +374,7 @@ float BaseEffectModule::GetBrightnessForLED(int led_id) {

void BaseEffectModule::SetEnabled(bool isEnabled) { m_isEnabled = isEnabled; }

bool BaseEffectModule::IsEnabled() { return m_isEnabled; }
bool BaseEffectModule::IsEnabled() const { return m_isEnabled; }

void BaseEffectModule::SetTempo(uint32_t bpm) {
// Do nothing.
Expand Down
Loading
Loading