LMMS
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
27{
28namespace dsp
29{
30
31//==============================================================================
46template <typename FloatType>
47class LogRampedValue : public SmoothedValueBase <LogRampedValue <FloatType>>
48{
49public:
50 //==============================================================================
52 LogRampedValue() = default;
53
55 LogRampedValue (FloatType initialValue) noexcept
56 {
57 // Visual Studio can't handle base class initialisation with CRTP
58 this->currentValue = initialValue;
59 this->target = initialValue;
60 }
61
62 //==============================================================================
73 void setLogParameters (FloatType midPointAmplitudedB, bool rateOfChangeShouldIncrease) noexcept
74 {
75 jassert (midPointAmplitudedB < (FloatType) 0.0);
76 B = Decibels::decibelsToGain (midPointAmplitudedB);
77
78 increasingRateOfChange = rateOfChangeShouldIncrease;
79 }
80
81 //==============================================================================
86 void reset (double sampleRate, double rampLengthInSeconds) noexcept
87 {
88 jassert (sampleRate > 0 && rampLengthInSeconds >= 0);
89 reset ((int) std::floor (rampLengthInSeconds * sampleRate));
90 }
91
95 void reset (int numSteps) noexcept
96 {
97 stepsToTarget = numSteps;
98
99 this->setCurrentAndTargetValue (this->target);
100
102 }
103
104 //==============================================================================
109 void setTargetValue (FloatType newValue) noexcept
110 {
111 if (newValue == this->target)
112 return;
113
114 if (stepsToTarget <= 0)
115 {
116 this->setCurrentAndTargetValue (newValue);
117 return;
118 }
119
120 this->target = newValue;
121 this->countdown = stepsToTarget;
122 source = this->currentValue;
123
125 }
126
127 //==============================================================================
132 {
133 if (! this->isSmoothing())
134 return this->target;
135
136 --(this->countdown);
137
138 temp *= r; temp += d;
139 this->currentValue = jmap (temp, source, this->target);
140
141 return this->currentValue;
142 }
143
144 //==============================================================================
150 FloatType skip (int numSamples) noexcept
151 {
152 if (numSamples >= this->countdown)
153 {
154 this->setCurrentAndTargetValue (this->target);
155 return this->target;
156 }
157
158 this->countdown -= numSamples;
159
160 auto rN = (FloatType) std::pow (r, numSamples);
161 temp *= rN;
162 temp += d * (rN - (FloatType) 1) / (r - (FloatType) 1);
163
164 this->currentValue = jmap (temp, source, this->target);
165 return this->currentValue;
166 }
167
168private:
169 //==============================================================================
171 {
172 auto D = increasingRateOfChange ? B : (FloatType) 1 - B;
173 auto base = ((FloatType) 1 / D) - (FloatType) 1;
174 r = std::pow (base, (FloatType) 2 / (FloatType) stepsToTarget);
175 auto rN = std::pow (r, (FloatType) stepsToTarget);
176 d = (r - (FloatType) 1) / (rN - (FloatType) 1);
177 temp = 0;
178 }
179
180 //==============================================================================
183
185 FloatType temp = 0, source = 0, r = 0, d = 1;
186};
187
188} // namespace dsp
189} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
static Type decibelsToGain(Type decibels, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Definition juce_Decibels.h:42
FloatType target
Definition juce_SmoothedValue.h:164
FloatType currentValue
Definition juce_SmoothedValue.h:163
bool isSmoothing() const noexcept
Definition juce_SmoothedValue.h:63
int countdown
Definition juce_SmoothedValue.h:165
void setCurrentAndTargetValue(FloatType newValue)
Definition juce_SmoothedValue.h:75
typename FloatTypeHelper< LogRampedValue< FloatType > >::Type FloatType
Definition juce_SmoothedValue.h:55
FloatType temp
Definition juce_LogRampedValue.h:185
int stepsToTarget
Definition juce_LogRampedValue.h:184
FloatType r
Definition juce_LogRampedValue.h:185
FloatType getNextValue() noexcept
Definition juce_LogRampedValue.h:131
void reset(double sampleRate, double rampLengthInSeconds) noexcept
Definition juce_LogRampedValue.h:86
void setLogParameters(FloatType midPointAmplitudedB, bool rateOfChangeShouldIncrease) noexcept
Definition juce_LogRampedValue.h:73
FloatType B
Definition juce_LogRampedValue.h:182
FloatType d
Definition juce_LogRampedValue.h:185
FloatType skip(int numSamples) noexcept
Definition juce_LogRampedValue.h:150
LogRampedValue(FloatType initialValue) noexcept
Definition juce_LogRampedValue.h:55
void reset(int numSteps) noexcept
Definition juce_LogRampedValue.h:95
void updateRampParameters()
Definition juce_LogRampedValue.h:170
void setTargetValue(FloatType newValue) noexcept
Definition juce_LogRampedValue.h:109
FloatType source
Definition juce_LogRampedValue.h:185
bool increasingRateOfChange
Definition juce_LogRampedValue.h:181
#define jassert(expression)
Definition juce_AudioBlock.h:29
Definition carla_juce.cpp:31
constexpr Type jmap(Type value0To1, Type targetRangeMin, Type targetRangeMax)
Definition juce_MathsFunctions.h:120
static float D(float x)
Definition tap_tubewarmth.c:156