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.

juce_LinearSmoothedValue.h 3.2KB

8 years ago
8 years ago
8 years ago
8 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 JUCE_API 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. //==============================================================================
  49. /** Set a new target value. */
  50. void setValue (FloatType newValue) noexcept
  51. {
  52. if (target != newValue)
  53. {
  54. target = newValue;
  55. countdown = stepsToTarget;
  56. if (countdown <= 0)
  57. currentValue = target;
  58. else
  59. step = (target - currentValue) / (FloatType) countdown;
  60. }
  61. }
  62. //==============================================================================
  63. /** Compute the next value. */
  64. FloatType getNextValue() noexcept
  65. {
  66. if (countdown <= 0)
  67. return target;
  68. --countdown;
  69. currentValue += step;
  70. return currentValue;
  71. }
  72. private:
  73. //==============================================================================
  74. FloatType currentValue, target, step;
  75. int countdown, stepsToTarget;
  76. };
  77. #endif // JUCE_LINEARSMOOTHEDVALUE_H_INCLUDED