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.

114 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_LINEARSMOOTHEDVALUE_H_INCLUDED
  24. #define JUCE_LINEARSMOOTHEDVALUE_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. Utility class for linearly smoothed values like volume etc. that should
  28. not change abruptly but as a linear ramp, to avoid audio glitches.
  29. */
  30. //==============================================================================
  31. template <typename FloatType>
  32. class LinearSmoothedValue
  33. {
  34. public:
  35. /** Constructor. */
  36. LinearSmoothedValue() noexcept
  37. : currentValue (0), target (0), step (0), countdown (0), stepsToTarget (0)
  38. {
  39. }
  40. /** Constructor. */
  41. LinearSmoothedValue (FloatType initialValue) noexcept
  42. : currentValue (initialValue), target (initialValue), step (0), countdown (0), stepsToTarget (0)
  43. {
  44. }
  45. //==============================================================================
  46. /** Reset to a new sample rate and ramp length. */
  47. void reset (double sampleRate, double rampLengthInSeconds) noexcept
  48. {
  49. jassert (sampleRate > 0 && rampLengthInSeconds >= 0);
  50. stepsToTarget = (int) std::floor (rampLengthInSeconds * sampleRate);
  51. currentValue = target;
  52. countdown = 0;
  53. }
  54. /** Set a new target value. */
  55. void setValue (FloatType newValue) noexcept
  56. {
  57. if (target != newValue)
  58. {
  59. target = newValue;
  60. countdown = stepsToTarget;
  61. if (countdown <= 0)
  62. currentValue = target;
  63. else
  64. step = (target - currentValue) / (FloatType) countdown;
  65. }
  66. }
  67. /** Compute the next value. */
  68. FloatType getNextValue() noexcept
  69. {
  70. if (countdown <= 0)
  71. return target;
  72. --countdown;
  73. currentValue += step;
  74. return currentValue;
  75. }
  76. /** Returns true if the current value is currently being interpolated. */
  77. bool isSmoothing() const noexcept
  78. {
  79. return countdown > 0;
  80. }
  81. /** Returns the target value towards which the smoothed value is currently moving. */
  82. FloatType getTargetValue() const noexcept
  83. {
  84. return target;
  85. }
  86. private:
  87. //==============================================================================
  88. FloatType currentValue, target, step;
  89. int countdown, stepsToTarget;
  90. };
  91. #endif // JUCE_LINEARSMOOTHEDVALUE_H_INCLUDED