JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_LogRampedValue.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce::dsp
27{
28
29//==============================================================================
44template <typename FloatType>
45class LogRampedValue : public SmoothedValueBase <LogRampedValue <FloatType>>
46{
47public:
48 //==============================================================================
50 LogRampedValue() = default;
51
53 LogRampedValue (FloatType initialValue) noexcept
54 {
55 // Visual Studio can't handle base class initialisation with CRTP
56 this->currentValue = initialValue;
57 this->target = initialValue;
58 }
59
60 //==============================================================================
72 {
73 jassert (midPointAmplitudedB < (FloatType) 0.0);
75
76 increasingRateOfChange = rateOfChangeShouldIncrease;
77 }
78
79 //==============================================================================
84 void reset (double sampleRate, double rampLengthInSeconds) noexcept
85 {
86 jassert (sampleRate > 0 && rampLengthInSeconds >= 0);
87 reset ((int) std::floor (rampLengthInSeconds * sampleRate));
88 }
89
93 void reset (int numSteps) noexcept
94 {
95 stepsToTarget = numSteps;
96
97 this->setCurrentAndTargetValue (this->target);
98
99 updateRampParameters();
100 }
101
102 //==============================================================================
107 void setTargetValue (FloatType newValue) noexcept
108 {
109 if (approximatelyEqual (newValue, this->target))
110 return;
111
112 if (stepsToTarget <= 0)
113 {
114 this->setCurrentAndTargetValue (newValue);
115 return;
116 }
117
118 this->target = newValue;
119 this->countdown = stepsToTarget;
120 source = this->currentValue;
121
122 updateRampParameters();
123 }
124
125 //==============================================================================
129 FloatType getNextValue() noexcept
130 {
131 if (! this->isSmoothing())
132 return this->target;
133
134 --(this->countdown);
135
136 temp *= r; temp += d;
137 this->currentValue = jmap (temp, source, this->target);
138
139 return this->currentValue;
140 }
141
142 //==============================================================================
148 FloatType skip (int numSamples) noexcept
149 {
150 if (numSamples >= this->countdown)
151 {
152 this->setCurrentAndTargetValue (this->target);
153 return this->target;
154 }
155
156 this->countdown -= numSamples;
157
158 auto rN = (FloatType) std::pow (r, numSamples);
159 temp *= rN;
160 temp += d * (rN - (FloatType) 1) / (r - (FloatType) 1);
161
162 this->currentValue = jmap (temp, source, this->target);
163 return this->currentValue;
164 }
165
166private:
167 //==============================================================================
168 void updateRampParameters()
169 {
170 auto D = increasingRateOfChange ? B : (FloatType) 1 - B;
171 auto base = ((FloatType) 1 / D) - (FloatType) 1;
172 r = std::pow (base, (FloatType) 2 / (FloatType) stepsToTarget);
173 auto rN = std::pow (r, (FloatType) stepsToTarget);
174 d = (r - (FloatType) 1) / (rN - (FloatType) 1);
175 temp = 0;
176 }
177
178 //==============================================================================
179 bool increasingRateOfChange = true;
180 FloatType B = Decibels::decibelsToGain ((FloatType) -40);
181
182 int stepsToTarget = 0;
183 FloatType temp = 0, source = 0, r = 0, d = 1;
184};
185
186} // namespace juce::dsp
static Type decibelsToGain(Type decibels, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Converts a dBFS value to its equivalent gain level.
A base class for the smoothed value classes.
bool isSmoothing() const noexcept
Returns true if the current value is currently being interpolated.
void setCurrentAndTargetValue(FloatType newValue)
Sets the current value and the target value.
Utility class for logarithmically smoothed linear values.
LogRampedValue()=default
Constructor.
FloatType getNextValue() noexcept
Compute the next value.
void reset(double sampleRate, double rampLengthInSeconds) noexcept
Reset to a new sample rate and ramp length.
void setLogParameters(FloatType midPointAmplitudedB, bool rateOfChangeShouldIncrease) noexcept
Sets the behaviour of the log ramp.
FloatType skip(int numSamples) noexcept
Skip the next numSamples samples.
LogRampedValue(FloatType initialValue) noexcept
Constructor.
void reset(int numSteps) noexcept
Set a new ramp length directly in samples.
void setTargetValue(FloatType newValue) noexcept
Set a new target value.
T floor(T... args)
#define jassert(expression)
Platform-independent assertion macro.
constexpr Type jmap(Type value0To1, Type targetRangeMin, Type targetRangeMax)
Remaps a normalised value (between 0 and 1) to a target range.
constexpr bool approximatelyEqual(Type a, Type b, Tolerance< Type > tolerance=Tolerance< Type >{} .withAbsolute(std::numeric_limits< Type >::min()) .withRelative(std::numeric_limits< Type >::epsilon()))
Returns true if the two floating-point numbers are approximately equal.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
T pow(T... args)