Loading...
Searching...
No Matches
juce_LogRampedValue.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
Agreement and JUCE Privacy Policy.
End User License Agreement: www.juce.com/juce-7-licence
Privacy Policy: www.juce.com/juce-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce::dsp
{
//==============================================================================
template <typename FloatType>
class LogRampedValue : public SmoothedValueBase <LogRampedValue <FloatType>>
{
public:
//==============================================================================
LogRampedValue() = default;
LogRampedValue (FloatType initialValue) noexcept
{
// Visual Studio can't handle base class initialisation with CRTP
this->currentValue = initialValue;
this->target = initialValue;
}
//==============================================================================
void setLogParameters (FloatType midPointAmplitudedB, bool rateOfChangeShouldIncrease) noexcept
{
jassert (midPointAmplitudedB < (FloatType) 0.0);
B = Decibels::decibelsToGain (midPointAmplitudedB);
increasingRateOfChange = rateOfChangeShouldIncrease;
}
//==============================================================================
void reset (double sampleRate, double rampLengthInSeconds) noexcept
{
jassert (sampleRate > 0 && rampLengthInSeconds >= 0);
reset ((int) std::floor (rampLengthInSeconds * sampleRate));
}
void reset (int numSteps) noexcept
{
stepsToTarget = numSteps;
this->setCurrentAndTargetValue (this->target);
updateRampParameters();
}
//==============================================================================
void setTargetValue (FloatType newValue) noexcept
{
if (approximatelyEqual (newValue, this->target))
return;
if (stepsToTarget <= 0)
{
this->setCurrentAndTargetValue (newValue);
return;
}
this->target = newValue;
this->countdown = stepsToTarget;
source = this->currentValue;
updateRampParameters();
}
//==============================================================================
FloatType getNextValue() noexcept
{
if (! this->isSmoothing())
return this->target;
--(this->countdown);
temp *= r; temp += d;
this->currentValue = jmap (temp, source, this->target);
return this->currentValue;
}
//==============================================================================
FloatType skip (int numSamples) noexcept
{
if (numSamples >= this->countdown)
{
this->setCurrentAndTargetValue (this->target);
return this->target;
}
this->countdown -= numSamples;
auto rN = (FloatType) std::pow (r, numSamples);
temp *= rN;
temp += d * (rN - (FloatType) 1) / (r - (FloatType) 1);
this->currentValue = jmap (temp, source, this->target);
return this->currentValue;
}
private:
//==============================================================================
void updateRampParameters()
{
auto D = increasingRateOfChange ? B : (FloatType) 1 - B;
auto base = ((FloatType) 1 / D) - (FloatType) 1;
r = std::pow (base, (FloatType) 2 / (FloatType) stepsToTarget);
auto rN = std::pow (r, (FloatType) stepsToTarget);
d = (r - (FloatType) 1) / (rN - (FloatType) 1);
temp = 0;
}
//==============================================================================
bool increasingRateOfChange = true;
FloatType B = Decibels::decibelsToGain ((FloatType) -40);
int stepsToTarget = 0;
FloatType temp = 0, source = 0, r = 0, d = 1;
};
} // namespace juce::dsp
juce::Decibels::decibelsToGain
static Type decibelsToGain(Type decibels, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Definition juce_Decibels.h:42
juce::SmoothedValueBase< LogRampedValue< FloatType > >::isSmoothing
bool isSmoothing() const noexcept
Definition juce_SmoothedValue.h:63
juce::SmoothedValueBase< LogRampedValue< FloatType > >::SmoothedValueBase
SmoothedValueBase()=default
juce::SmoothedValueBase< LogRampedValue< FloatType > >::setCurrentAndTargetValue
void setCurrentAndTargetValue(FloatType newValue)
Definition juce_SmoothedValue.h:75
juce::dsp::LogRampedValue::LogRampedValue
LogRampedValue()=default
juce::dsp::LogRampedValue::getNextValue
FloatType getNextValue() noexcept
Definition juce_LogRampedValue.h:129
juce::dsp::LogRampedValue::reset
void reset(double sampleRate, double rampLengthInSeconds) noexcept
Definition juce_LogRampedValue.h:84
juce::dsp::LogRampedValue::setLogParameters
void setLogParameters(FloatType midPointAmplitudedB, bool rateOfChangeShouldIncrease) noexcept
Definition juce_LogRampedValue.h:71
juce::dsp::LogRampedValue::skip
FloatType skip(int numSamples) noexcept
Definition juce_LogRampedValue.h:148
juce::dsp::LogRampedValue::LogRampedValue
LogRampedValue(FloatType initialValue) noexcept
Definition juce_LogRampedValue.h:53
juce::dsp::LogRampedValue::reset
void reset(int numSteps) noexcept
Definition juce_LogRampedValue.h:93
juce::dsp::LogRampedValue::setTargetValue
void setTargetValue(FloatType newValue) noexcept
Definition juce_LogRampedValue.h:107