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.

183 lines
6.1KB

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