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.

108 lines
3.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_LINEARSMOOTHEDVALUE_H_INCLUDED
  18. #define JUCE_LINEARSMOOTHEDVALUE_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Utility class for linearly smoothed values like volume etc. that should
  22. not change abruptly but as a linear ramp, to avoid audio glitches.
  23. */
  24. //==============================================================================
  25. template <typename FloatType>
  26. class LinearSmoothedValue
  27. {
  28. public:
  29. /** Constructor. */
  30. LinearSmoothedValue() noexcept
  31. : currentValue (0), target (0), step (0), countdown (0), stepsToTarget (0)
  32. {
  33. }
  34. /** Constructor. */
  35. LinearSmoothedValue (FloatType initialValue) noexcept
  36. : currentValue (initialValue), target (initialValue), step (0), countdown (0), stepsToTarget (0)
  37. {
  38. }
  39. //==============================================================================
  40. /** Reset to a new sample rate and ramp length. */
  41. void reset (double sampleRate, double rampLengthInSeconds) noexcept
  42. {
  43. jassert (sampleRate > 0 && rampLengthInSeconds >= 0);
  44. stepsToTarget = (int) std::floor (rampLengthInSeconds * sampleRate);
  45. currentValue = target;
  46. countdown = 0;
  47. }
  48. /** Set a new target value. */
  49. void setValue (FloatType newValue) noexcept
  50. {
  51. if (target != newValue)
  52. {
  53. target = newValue;
  54. countdown = stepsToTarget;
  55. if (countdown <= 0)
  56. currentValue = target;
  57. else
  58. step = (target - currentValue) / (FloatType) countdown;
  59. }
  60. }
  61. /** Compute the next value. */
  62. FloatType getNextValue() noexcept
  63. {
  64. if (countdown <= 0)
  65. return target;
  66. --countdown;
  67. currentValue += step;
  68. return currentValue;
  69. }
  70. /** Returns true if the current value is currently being interpolated. */
  71. bool isSmoothing() const noexcept
  72. {
  73. return countdown > 0;
  74. }
  75. /** Returns the target value towards which the smoothed value is currently moving. */
  76. FloatType getTargetValue() const noexcept
  77. {
  78. return target;
  79. }
  80. private:
  81. //==============================================================================
  82. FloatType currentValue, target, step;
  83. int countdown, stepsToTarget;
  84. };
  85. #endif // JUCE_LINEARSMOOTHEDVALUE_H_INCLUDED