Audio plugin host https://kx.studio/carla
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.

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