As I beginner I often feel like I’m doing something wrong. Today I coded 2 functions that basically “automate” the process of having to repeat code and create new variables for smoothing variables, it works but is this code bad/slow? And what parts of it is and how can I fix it?
float smoothLinear(AudioParameterFloat* paramPtr, float speed = 0.05) {
static std::map<String, float> smoothedVars;
float& smoothedVar = smoothedVars[paramPtr->paramID];
if (abs(smoothedVar - paramPtr->get()) >= speed) {
smoothedVar = smoothedVar - speed * (smoothedVar - paramPtr->get());
}
return smoothedVar;
}
float smoothExp(AudioParameterFloat* paramPtr, float speed = 0.05) {
static std::map<String, float> smoothedVars;
float& smoothedVar = smoothedVars[paramPtr->paramID];
if (abs(smoothedVar - paramPtr->get()) >= speed) {
if (smoothedVar == 0) {
smoothedVar = paramPtr->get();
}
else {
smoothedVar *= exp((log(paramPtr->get()) - log(smoothedVar)) * speed);
}
}
return smoothedVar;
}
Here’s an example of how I use this code:
A pointer to a parameter I have is mMixParameter
and I want a smoothed value for it, all I do is: float mixSmoothed = smoothedLinear(mMixParameter)