Does anyone know how I could make a triggered linear ramp that goes from 0 to 1 over a specified amount of time (ms or sec)? I mostly just need one right now for a parameter smoother, but I can think of some other things it could be useful for. Maybe like a simple animation or something.
It seems like such a simple thing to do, but I just can't figure it out and I can't find anything on the web about it. I know you could make a triggered ramp oscillator and make it stop after one cycle, but I can't even get that to work right. I can make oscillator's just fine, but for some reason I can't make this. Maybe one of you more experienced guys could help me out?
I'm also thinking that a single cycle ramp oscillator might not be the best choice for a general ramp because it would have to be processed on an audio process, since the frequency usually depends on that and the sample rate. Maybe it would be a good idea to have two ramps, one for the audio process and one without. I'm guessing that without an audio process, it would need a timer or something. Idk. I'd appreciate any help from you guys.
Here is a little snippet of my codebase which is a general Ramper, but it should do what you need:
/** A Ramper applies linear ramping to a value.
* @ingroup utility
*
*/
class Ramper
{
public:
Ramper():
targetValue(0.0f),
stepDelta(0.0f),
stepAmount(-1)
{};
/** Sets the step amount that the ramper will use. You can overwrite this value by supplying a step number in setTarget. */
void setStepAmount(int newStepAmount) { stepAmount = newStepAmount; };
/** sets the new target and recalculates the step size using either the supplied step number or the step amount previously set by setStepAmount(). */
void setTarget(float currentValue, float newTarget, int numberOfSteps=-1)
{
if(numberOfSteps != -1) stepDelta = (newTarget - currentValue) / numberOfSteps;
else if (stepAmount != -1) stepDelta = (newTarget - currentValue) / stepAmount;
else jassertfalse; // Either the step amount should be set, or a new step amount should be supplied
targetValue = newTarget;
};
/** Sets the ramper value and the target to the new value and stops ramping. */
void setValue(float newValue)
{
targetValue = newValue;
stepDelta = 0.0f;
};
/** ramps the supplied value and returns true if the targetValue is reached. */
inline bool ramp(float &valueToChange)
{
valueToChange += stepDelta;
return abs(targetValue - valueToChange) > 0.001;
};
private:
float targetValue, stepDelta;
int stepAmount;
};
Do something like this for your 0...1 ramp:
float rampedValue = 0.0f;
Ramper r;
r.setTargetValue(0.0f, 1.0f, 44100) // 1 sec in 44,1kHz
while(!r.ramp(rampedValue))
{
// do something
}