The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
6.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. namespace dsp
  22. {
  23. //==============================================================================
  24. /**
  25. Utility class for logarithmically smoothed linear values.
  26. Logarithmically smoothed values can be more relevant than linear ones for
  27. specific cases such as algorithm change smoothing, using two of them in
  28. opposite directions.
  29. The gradient of the logarithmic/exponential slope can be configured by
  30. calling LogRampedValue::setLogParameters.
  31. @see SmoothedValue
  32. @tags{DSP}
  33. */
  34. template <typename FloatType>
  35. class LogRampedValue : public SmoothedValueBase <LogRampedValue <FloatType>>
  36. {
  37. public:
  38. //==============================================================================
  39. /** Constructor. */
  40. LogRampedValue() = default;
  41. /** Constructor. */
  42. LogRampedValue (FloatType initialValue) noexcept
  43. {
  44. // Visual Studio can't handle base class initialisation with CRTP
  45. this->currentValue = initialValue;
  46. this->target = initialValue;
  47. }
  48. //==============================================================================
  49. /** Sets the behaviour of the log ramp.
  50. @param midPointAmplitudedB Sets the amplitude of the mid point in
  51. decibels, with the target value at 0 dB
  52. and the initial value at -inf dB
  53. @param rateOfChangeShouldIncrease If true then the ramp starts shallow
  54. and gets progressively steeper, if false
  55. then the ramp is initially steep and
  56. flattens out as you approach the target
  57. value
  58. */
  59. void setLogParameters (FloatType midPointAmplitudedB, bool rateOfChangeShouldIncrease) noexcept
  60. {
  61. jassert (midPointAmplitudedB < (FloatType) 0.0);
  62. B = Decibels::decibelsToGain (midPointAmplitudedB);
  63. increasingRateOfChange = rateOfChangeShouldIncrease;
  64. }
  65. //==============================================================================
  66. /** Reset to a new sample rate and ramp length.
  67. @param sampleRate The sample rate
  68. @param rampLengthInSeconds The duration of the ramp in seconds
  69. */
  70. void reset (double sampleRate, double rampLengthInSeconds) noexcept
  71. {
  72. jassert (sampleRate > 0 && rampLengthInSeconds >= 0);
  73. reset ((int) std::floor (rampLengthInSeconds * sampleRate));
  74. }
  75. /** Set a new ramp length directly in samples.
  76. @param numSteps The number of samples over which the ramp should be active
  77. */
  78. void reset (int numSteps) noexcept
  79. {
  80. stepsToTarget = numSteps;
  81. this->setCurrentAndTargetValue (this->target);
  82. updateRampParameters();
  83. }
  84. //==============================================================================
  85. /** Set a new target value.
  86. @param newValue The new target value
  87. */
  88. void setTargetValue (FloatType newValue) noexcept
  89. {
  90. if (newValue == this->target)
  91. return;
  92. if (stepsToTarget <= 0)
  93. {
  94. this->setCurrentAndTargetValue (newValue);
  95. return;
  96. }
  97. this->target = newValue;
  98. this->countdown = stepsToTarget;
  99. source = this->currentValue;
  100. updateRampParameters();
  101. }
  102. //==============================================================================
  103. /** Compute the next value.
  104. @returns Smoothed value
  105. */
  106. FloatType getNextValue() noexcept
  107. {
  108. if (! this->isSmoothing())
  109. return this->target;
  110. --(this->countdown);
  111. temp *= r; temp += d;
  112. this->currentValue = jmap (temp, source, this->target);
  113. return this->currentValue;
  114. }
  115. //==============================================================================
  116. /** Skip the next numSamples samples.
  117. This is identical to calling getNextValue numSamples times.
  118. @see getNextValue
  119. */
  120. FloatType skip (int numSamples) noexcept
  121. {
  122. if (numSamples >= this->countdown)
  123. {
  124. this->setCurrentAndTargetValue (this->target);
  125. return this->target;
  126. }
  127. this->countdown -= numSamples;
  128. auto rN = (FloatType) std::pow (r, numSamples);
  129. temp *= rN;
  130. temp += d * (rN - (FloatType) 1) / (r - (FloatType) 1);
  131. this->currentValue = jmap (temp, source, this->target);
  132. return this->currentValue;
  133. }
  134. private:
  135. //==============================================================================
  136. void updateRampParameters()
  137. {
  138. auto D = increasingRateOfChange ? B : (FloatType) 1 - B;
  139. auto base = ((FloatType) 1 / D) - (FloatType) 1;
  140. r = std::pow (base, (FloatType) 2 / (FloatType) stepsToTarget);
  141. auto rN = std::pow (r, (FloatType) stepsToTarget);
  142. d = (r - (FloatType) 1) / (rN - (FloatType) 1);
  143. temp = 0;
  144. }
  145. //==============================================================================
  146. bool increasingRateOfChange = true;
  147. FloatType B = Decibels::decibelsToGain ((FloatType) -40);
  148. int stepsToTarget = 0;
  149. FloatType temp = 0, source = 0, r = 0, d = 1;
  150. };
  151. } // namespace dsp
  152. } // namespace juce