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.

102 lines
3.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /**
  20. Utility class for linearly smoothed values like volume etc. that should
  21. not change abruptly but as a linear ramp, to avoid audio glitches.
  22. */
  23. //==============================================================================
  24. template <typename FloatType>
  25. class LinearSmoothedValue
  26. {
  27. public:
  28. /** Constructor. */
  29. LinearSmoothedValue() noexcept
  30. : currentValue (0), target (0), step (0), countdown (0), stepsToTarget (0)
  31. {
  32. }
  33. /** Constructor. */
  34. LinearSmoothedValue (FloatType initialValue) noexcept
  35. : currentValue (initialValue), target (initialValue), step (0), countdown (0), stepsToTarget (0)
  36. {
  37. }
  38. //==============================================================================
  39. /** Reset to a new sample rate and ramp length. */
  40. void reset (double sampleRate, double rampLengthInSeconds) noexcept
  41. {
  42. jassert (sampleRate > 0 && rampLengthInSeconds >= 0);
  43. stepsToTarget = (int) std::floor (rampLengthInSeconds * sampleRate);
  44. currentValue = target;
  45. countdown = 0;
  46. }
  47. /** Set a new target value. */
  48. void setValue (FloatType newValue) noexcept
  49. {
  50. if (target != newValue)
  51. {
  52. target = newValue;
  53. countdown = stepsToTarget;
  54. if (countdown <= 0)
  55. currentValue = target;
  56. else
  57. step = (target - currentValue) / (FloatType) countdown;
  58. }
  59. }
  60. /** Compute the next value. */
  61. FloatType getNextValue() noexcept
  62. {
  63. if (countdown <= 0)
  64. return target;
  65. --countdown;
  66. currentValue += step;
  67. return currentValue;
  68. }
  69. /** Returns true if the current value is currently being interpolated. */
  70. bool isSmoothing() const noexcept
  71. {
  72. return countdown > 0;
  73. }
  74. /** Returns the target value towards which the smoothed value is currently moving. */
  75. FloatType getTargetValue() const noexcept
  76. {
  77. return target;
  78. }
  79. private:
  80. //==============================================================================
  81. FloatType currentValue, target, step;
  82. int countdown, stepsToTarget;
  83. };