OpenShot Audio Library | OpenShotAudio 0.4.0
juce_LogRampedValue.h
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 //==============================================================================
71 void setLogParameters (FloatType midPointAmplitudedB, bool rateOfChangeShouldIncrease) noexcept
72 {
73 jassert (midPointAmplitudedB < (FloatType) 0.0);
74 B = Decibels::decibelsToGain (midPointAmplitudedB);
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))
Definition: juce_Decibels.h:42
FloatType getNextValue() noexcept
void reset(double sampleRate, double rampLengthInSeconds) noexcept
void setLogParameters(FloatType midPointAmplitudedB, bool rateOfChangeShouldIncrease) noexcept
FloatType skip(int numSamples) noexcept
LogRampedValue(FloatType initialValue) noexcept
void reset(int numSteps) noexcept
void setTargetValue(FloatType newValue) noexcept