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.

187 lines
6.3KB

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