OpenShot Audio Library | OpenShotAudio  0.3.2
juce_LogRampedValue.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
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 5 End-User License
11  Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
12  27th April 2017).
13 
14  End User License Agreement: www.juce.com/juce-5-licence
15  Privacy Policy: www.juce.com/juce-5-privacy-policy
16 
17  Or: You may also use this code under the terms of the GPL v3 (see
18  www.gnu.org/licenses).
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 namespace dsp
30 {
31 
32 //==============================================================================
47 template <typename FloatType>
48 class LogRampedValue : public SmoothedValueBase <LogRampedValue <FloatType>>
49 {
50 public:
51  //==============================================================================
53  LogRampedValue() = default;
54 
56  LogRampedValue (FloatType initialValue) noexcept
57  {
58  // Visual Studio can't handle base class initialisation with CRTP
59  this->currentValue = initialValue;
60  this->target = initialValue;
61  }
62 
63  //==============================================================================
74  void setLogParameters (FloatType midPointAmplitudedB, bool rateOfChangeShouldIncrease) noexcept
75  {
76  jassert (midPointAmplitudedB < (FloatType) 0.0);
77  B = Decibels::decibelsToGain (midPointAmplitudedB);
78 
79  increasingRateOfChange = rateOfChangeShouldIncrease;
80  }
81 
82  //==============================================================================
87  void reset (double sampleRate, double rampLengthInSeconds) noexcept
88  {
89  jassert (sampleRate > 0 && rampLengthInSeconds >= 0);
90  reset ((int) std::floor (rampLengthInSeconds * sampleRate));
91  }
92 
96  void reset (int numSteps) noexcept
97  {
98  stepsToTarget = numSteps;
99 
100  this->setCurrentAndTargetValue (this->target);
101 
102  updateRampParameters();
103  }
104 
105  //==============================================================================
110  void setTargetValue (FloatType newValue) noexcept
111  {
112  if (newValue == this->target)
113  return;
114 
115  if (stepsToTarget <= 0)
116  {
117  this->setCurrentAndTargetValue (newValue);
118  return;
119  }
120 
121  this->target = newValue;
122  this->countdown = stepsToTarget;
123  source = this->currentValue;
124 
125  updateRampParameters();
126  }
127 
128  //==============================================================================
132  FloatType getNextValue() noexcept
133  {
134  if (! this->isSmoothing())
135  return this->target;
136 
137  --(this->countdown);
138 
139  temp *= r; temp += d;
140  this->currentValue = jmap (temp, source, this->target);
141 
142  return this->currentValue;
143  }
144 
145  //==============================================================================
151  FloatType skip (int numSamples) noexcept
152  {
153  if (numSamples >= this->countdown)
154  {
155  this->setCurrentAndTargetValue (this->target);
156  return this->target;
157  }
158 
159  this->countdown -= numSamples;
160 
161  auto rN = (FloatType) std::pow (r, numSamples);
162  temp *= rN;
163  temp += d * (rN - (FloatType) 1) / (r - (FloatType) 1);
164 
165  this->currentValue = jmap (temp, source, this->target);
166  return this->currentValue;
167  }
168 
169 private:
170  //==============================================================================
171  void updateRampParameters()
172  {
173  auto D = increasingRateOfChange ? B : (FloatType) 1 - B;
174  auto base = ((FloatType) 1 / D) - (FloatType) 1;
175  r = std::pow (base, (FloatType) 2 / (FloatType) stepsToTarget);
176  auto rN = std::pow (r, (FloatType) stepsToTarget);
177  d = (r - (FloatType) 1) / (rN - (FloatType) 1);
178  temp = 0;
179  }
180 
181  //==============================================================================
182  bool increasingRateOfChange = true;
183  FloatType B = Decibels::decibelsToGain ((FloatType) -40);
184 
185  int stepsToTarget = 0;
186  FloatType temp = 0, source = 0, r = 0, d = 1;
187 };
188 
189 } // namespace dsp
190 } // namespace juce
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