Hello JUCE users,
So I just started using JUCE like a week ago and I finally noticed that all plugin parameters have to eventually be within the range of 0-1. Then I started wondering how I would accomplish this, then remembered that I did this exact thing in my work in Reaktor. What i would do in Reaktor is I would set the actual knob to have a range from 0-1 (or 0-Max then muliply it by a fraction to get it to the range 0-1). Then I would use a crossfader to set the range that the knob would eventually output. So the actual position of the knob would change the position of the crossfader, and the two inputs (in1, in2) would be the min and max values that the crossfader would output.
So I took the same crossfader algorithm from Reaktor and implemented it in C++. I thought I would share it with y'all in case there's any noobs like me. :) Hey, maybe this can help the more experience users that just haven't thought of crossfading. This is for doubles, so you can just change it as needed. I also use a function that clips the X-Fade's position value, so you can't go out of the range 0-1. I'll give you the code for that too. Hope this helps someone.
Some of you may not like the argument names I gave to the crossfader (in0, in1), but you could change them to something that makes more sense to you. I use them because in Reaktor we do something similar, and it more closely describes how the xFadeCtrl is affecting the output. You know, if xFadeCtrl = 0 then it returns in0. Just makes more sense to me that way.
Crossfader:
/** Crossfades linearly between two values (in0, in1). The value returned is determined by the value of the xFadeCtrl parameter. xFadeCtrl Range: 0->1 - xFadeCtrl = 0 (only in0 comes through) - xFadeCtrl = 0.5 (equal mix of in0 and in1) - xfadeCtrl = 1 (only in1 comes through) */ double xFadeLin(double xFadeCtrl, double in0, double in1) { // Clip the xFadeCtrl parameter to only have range of 0->1 xFadeCtrl = clipMinMax(xFadeCtrl, 0.0, 1.0); // Perform crossfading and return the value return (in0 * (1.0 - xFadeCtrl) + in1 * xFadeCtrl); }
Min/Max Clipper:
/** Takes a value as input and clips it according to the minimum and maximum values Returns the input if (minValue <= in <= maxValue). If (in < minValue), then return minValue. If (in > maxValue), then return maxValue. */ double clipMinMax(double in, double minValue, double maxValue) { if (in < minValue) return minValue; else if (in > maxValue) return maxValue; else return in; }
Note:
I think you could use this same function for crossfading audio, but I would recommend a parabolic (or sine) crossfader instead of the linear crossfader shown here. If anyone's interested, I'd be happy to make them a parabolic or sine crossfader.
Since I'm discussing this, maybe y'all can help me to figure out where I shoud actually be doing this range conversion to make everything work right. I'm still not totally familiar with how JUCE and the host treats parameters.