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 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. namespace juce
  18. {
  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. {
  32. }
  33. /** Constructor. */
  34. LinearSmoothedValue (FloatType initialValue) noexcept
  35. : currentValue (initialValue), target (initialValue)
  36. {
  37. }
  38. //==============================================================================
  39. /** Reset to a new sample rate and ramp length.
  40. @param sampleRate The sampling rate
  41. @param rampLengthInSeconds The duration of the ramp in seconds
  42. */
  43. void reset (double sampleRate, double rampLengthInSeconds) noexcept
  44. {
  45. jassert (sampleRate > 0 && rampLengthInSeconds >= 0);
  46. stepsToTarget = (int) std::floor (rampLengthInSeconds * sampleRate);
  47. currentValue = target;
  48. countdown = 0;
  49. }
  50. //==============================================================================
  51. /** Set a new target value.
  52. @param newValue New target value
  53. */
  54. void setValue (FloatType newValue) noexcept
  55. {
  56. if (target != newValue)
  57. {
  58. target = newValue;
  59. countdown = stepsToTarget;
  60. if (countdown <= 0)
  61. currentValue = target;
  62. else
  63. step = (target - currentValue) / (FloatType) countdown;
  64. }
  65. }
  66. //==============================================================================
  67. /** Compute the next value.
  68. @returns Smoothed value
  69. */
  70. FloatType getNextValue() noexcept
  71. {
  72. if (countdown <= 0)
  73. return target;
  74. --countdown;
  75. currentValue += step;
  76. return currentValue;
  77. }
  78. /** Returns true if the current value is currently being interpolated. */
  79. bool isSmoothing() const noexcept
  80. {
  81. return countdown > 0;
  82. }
  83. /** Returns the target value towards which the smoothed value is currently moving. */
  84. FloatType getTargetValue() const noexcept
  85. {
  86. return target;
  87. }
  88. //==============================================================================
  89. /** Applies a linear smoothed gain to a stream of samples
  90. S[i] *= gain
  91. @param samples Pointer to a raw array of samples
  92. @param numSamples Length of array of samples
  93. */
  94. void applyGain (FloatType* samples, int numSamples) noexcept
  95. {
  96. jassert(numSamples >= 0);
  97. if (isSmoothing())
  98. {
  99. for (int i = 0; i < numSamples; i++)
  100. samples[i] *= getNextValue();
  101. }
  102. else
  103. {
  104. FloatVectorOperations::multiply (samples, target, numSamples);
  105. }
  106. }
  107. //==============================================================================
  108. /** Computes output as linear smoothed gain applied to a stream of samples.
  109. Sout[i] = Sin[i] * gain
  110. @param samplesOut A pointer to a raw array of output samples
  111. @param samplesIn A pointer to a raw array of input samples
  112. @param numSamples The length of the array of samples
  113. */
  114. void applyGain (FloatType* samplesOut, const FloatType* samplesIn, int numSamples) noexcept
  115. {
  116. jassert (numSamples >= 0);
  117. if (isSmoothing())
  118. {
  119. for (int i = 0; i < numSamples; i++)
  120. samplesOut[i] = samplesIn[i] * getNextValue();
  121. }
  122. else
  123. {
  124. FloatVectorOperations::multiply (samplesOut, samplesIn, target, numSamples);
  125. }
  126. }
  127. //==============================================================================
  128. /** Applies a linear smoothed gain to a buffer */
  129. void applyGain (AudioBuffer<FloatType>& buffer, int numSamples) noexcept
  130. {
  131. jassert (numSamples >= 0);
  132. if (isSmoothing())
  133. {
  134. if (buffer.getNumChannels() == 1)
  135. {
  136. FloatType* samples = buffer.getWritePointer(0);
  137. for (int i = 0; i < numSamples; i++)
  138. samples[i] *= getNextValue();
  139. }
  140. else
  141. {
  142. for (int i = 0; i < numSamples; i++)
  143. {
  144. const FloatType gain = getNextValue();
  145. for (int channel = 0; channel < buffer.getNumChannels(); channel++)
  146. buffer.setSample (channel, i, buffer.getSample (channel, i) * gain);
  147. }
  148. }
  149. }
  150. else
  151. {
  152. buffer.applyGain (0, numSamples, target);
  153. }
  154. }
  155. private:
  156. //==============================================================================
  157. FloatType currentValue = 0, target = 0, step = 0;
  158. int countdown = 0, stepsToTarget = 0;
  159. };
  160. } // namespace juce