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.

185 lines
5.9KB

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