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.

216 lines
6.8KB

  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. @tags{Audio}
  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 The new target value
  53. @param force If true, the value will be set immediately, bypassing the ramp
  54. */
  55. void setValue (FloatType newValue, bool force = false) noexcept
  56. {
  57. if (force)
  58. {
  59. target = currentValue = newValue;
  60. countdown = 0;
  61. return;
  62. }
  63. if (target != newValue)
  64. {
  65. target = newValue;
  66. countdown = stepsToTarget;
  67. if (countdown <= 0)
  68. currentValue = target;
  69. else
  70. step = (target - currentValue) / (FloatType) countdown;
  71. }
  72. }
  73. //==============================================================================
  74. /** Compute the next value.
  75. @returns Smoothed value
  76. */
  77. FloatType getNextValue() noexcept
  78. {
  79. if (countdown <= 0)
  80. return target;
  81. --countdown;
  82. currentValue += step;
  83. return currentValue;
  84. }
  85. /** Returns true if the current value is currently being interpolated. */
  86. bool isSmoothing() const noexcept
  87. {
  88. return countdown > 0;
  89. }
  90. /** Returns the target value towards which the smoothed value is currently moving. */
  91. FloatType getTargetValue() const noexcept
  92. {
  93. return target;
  94. }
  95. //==============================================================================
  96. /** Applies a linear smoothed gain to a stream of samples
  97. S[i] *= gain
  98. @param samples Pointer to a raw array of samples
  99. @param numSamples Length of array of samples
  100. */
  101. void applyGain (FloatType* samples, int numSamples) noexcept
  102. {
  103. jassert(numSamples >= 0);
  104. if (isSmoothing())
  105. {
  106. for (int i = 0; i < numSamples; i++)
  107. samples[i] *= getNextValue();
  108. }
  109. else
  110. {
  111. FloatVectorOperations::multiply (samples, target, numSamples);
  112. }
  113. }
  114. //==============================================================================
  115. /** Computes output as linear smoothed gain applied to a stream of samples.
  116. Sout[i] = Sin[i] * gain
  117. @param samplesOut A pointer to a raw array of output samples
  118. @param samplesIn A pointer to a raw array of input samples
  119. @param numSamples The length of the array of samples
  120. */
  121. void applyGain (FloatType* samplesOut, const FloatType* samplesIn, int numSamples) noexcept
  122. {
  123. jassert (numSamples >= 0);
  124. if (isSmoothing())
  125. {
  126. for (int i = 0; i < numSamples; i++)
  127. samplesOut[i] = samplesIn[i] * getNextValue();
  128. }
  129. else
  130. {
  131. FloatVectorOperations::multiply (samplesOut, samplesIn, target, numSamples);
  132. }
  133. }
  134. //==============================================================================
  135. /** Applies a linear smoothed gain to a buffer */
  136. void applyGain (AudioBuffer<FloatType>& buffer, int numSamples) noexcept
  137. {
  138. jassert (numSamples >= 0);
  139. if (isSmoothing())
  140. {
  141. if (buffer.getNumChannels() == 1)
  142. {
  143. auto samples = buffer.getWritePointer(0);
  144. for (int i = 0; i < numSamples; ++i)
  145. samples[i] *= getNextValue();
  146. }
  147. else
  148. {
  149. for (int i = 0; i < numSamples; ++i)
  150. {
  151. auto gain = getNextValue();
  152. for (int channel = 0; channel < buffer.getNumChannels(); channel++)
  153. buffer.setSample (channel, i, buffer.getSample (channel, i) * gain);
  154. }
  155. }
  156. }
  157. else
  158. {
  159. buffer.applyGain (0, numSamples, target);
  160. }
  161. }
  162. //==============================================================================
  163. /** Skip the next numSamples samples.
  164. This is identical to calling getNextValue numSamples times. It returns
  165. the new current value.
  166. @see getNextValue
  167. */
  168. FloatType skip (int numSamples) noexcept
  169. {
  170. if (numSamples >= countdown)
  171. {
  172. currentValue = target;
  173. countdown = 0;
  174. return target;
  175. }
  176. currentValue += (step * static_cast<FloatType> (numSamples));
  177. countdown -= numSamples;
  178. return currentValue;
  179. }
  180. private:
  181. //==============================================================================
  182. FloatType currentValue = 0, target = 0, step = 0;
  183. int countdown = 0, stepsToTarget = 0;
  184. };
  185. } // namespace juce