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.

390 lines
13KB

  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. A base class for the smoothed value classes.
  22. This class is used to provide common functionality to the SmoothedValue and
  23. dsp::LogRampedValue classes.
  24. @tags{Audio}
  25. */
  26. template <typename SmoothedValueType>
  27. class SmoothedValueBase
  28. {
  29. private:
  30. //==============================================================================
  31. template <typename T> struct FloatTypeHelper;
  32. template <template <typename> class SmoothedValueClass, typename FloatType>
  33. struct FloatTypeHelper <SmoothedValueClass <FloatType>>
  34. {
  35. using Type = FloatType;
  36. };
  37. template <template <typename, typename> class SmoothedValueClass, typename FloatType, typename SmoothingType>
  38. struct FloatTypeHelper <SmoothedValueClass <FloatType, SmoothingType>>
  39. {
  40. using Type = FloatType;
  41. };
  42. public:
  43. using FloatType = typename FloatTypeHelper<SmoothedValueType>::Type;
  44. //==============================================================================
  45. /** Constructor. */
  46. SmoothedValueBase() = default;
  47. virtual ~SmoothedValueBase() {}
  48. //==============================================================================
  49. /** Returns true if the current value is currently being interpolated. */
  50. bool isSmoothing() const noexcept { return countdown > 0; }
  51. /** Returns the current value of the ramp. */
  52. FloatType getCurrentValue() const noexcept { return currentValue; }
  53. //==============================================================================
  54. /** Returns the target value towards which the smoothed value is currently moving. */
  55. FloatType getTargetValue() const noexcept { return target; }
  56. /** Sets the current value and the target value.
  57. @param newValue the new value to take
  58. */
  59. void setCurrentAndTargetValue (FloatType newValue)
  60. {
  61. target = currentValue = newValue;
  62. countdown = 0;
  63. }
  64. //==============================================================================
  65. /** Applies a smoothed gain to a stream of samples
  66. S[i] *= gain
  67. @param samples Pointer to a raw array of samples
  68. @param numSamples Length of array of samples
  69. */
  70. void applyGain (FloatType* samples, int numSamples) noexcept
  71. {
  72. jassert (numSamples >= 0);
  73. if (isSmoothing())
  74. {
  75. for (int i = 0; i < numSamples; ++i)
  76. samples[i] *= getNextSmoothedValue();
  77. }
  78. else
  79. {
  80. FloatVectorOperations::multiply (samples, target, numSamples);
  81. }
  82. }
  83. /** Computes output as a smoothed gain applied to a stream of samples.
  84. Sout[i] = Sin[i] * gain
  85. @param samplesOut A pointer to a raw array of output samples
  86. @param samplesIn A pointer to a raw array of input samples
  87. @param numSamples The length of the array of samples
  88. */
  89. void applyGain (FloatType* samplesOut, const FloatType* samplesIn, int numSamples) noexcept
  90. {
  91. jassert (numSamples >= 0);
  92. if (isSmoothing())
  93. {
  94. for (int i = 0; i < numSamples; ++i)
  95. samplesOut[i] = samplesIn[i] * getNextSmoothedValue();
  96. }
  97. else
  98. {
  99. FloatVectorOperations::multiply (samplesOut, samplesIn, target, numSamples);
  100. }
  101. }
  102. /** Applies a smoothed gain to a buffer */
  103. void applyGain (AudioBuffer<FloatType>& buffer, int numSamples) noexcept
  104. {
  105. jassert (numSamples >= 0);
  106. if (isSmoothing())
  107. {
  108. if (buffer.getNumChannels() == 1)
  109. {
  110. auto* samples = buffer.getWritePointer (0);
  111. for (int i = 0; i < numSamples; ++i)
  112. samples[i] *= getNextSmoothedValue();
  113. }
  114. else
  115. {
  116. for (auto i = 0; i < numSamples; ++i)
  117. {
  118. auto gain = getNextSmoothedValue();
  119. for (int channel = 0; channel < buffer.getNumChannels(); channel++)
  120. buffer.setSample (channel, i, buffer.getSample (channel, i) * gain);
  121. }
  122. }
  123. }
  124. else
  125. {
  126. buffer.applyGain (0, numSamples, target);
  127. }
  128. }
  129. private:
  130. //==============================================================================
  131. FloatType getNextSmoothedValue() noexcept
  132. {
  133. return static_cast <SmoothedValueType*> (this)->getNextValue();
  134. }
  135. protected:
  136. //==============================================================================
  137. FloatType currentValue = 0;
  138. FloatType target = currentValue;
  139. int countdown = 0;
  140. };
  141. //==============================================================================
  142. /**
  143. A namespace containing a set of types used for specifying the smoothing
  144. behaviour of the SmoothedValue class.
  145. For example:
  146. @code
  147. SmoothedValue<float, ValueSmoothingTypes::Multiplicative> frequency (1.0f);
  148. @endcode
  149. */
  150. namespace ValueSmoothingTypes
  151. {
  152. /** Used to indicate a linear smoothing between values. */
  153. struct Linear {};
  154. /** Used to indicate a smoothing between multiplicative values. */
  155. struct Multiplicative {};
  156. }
  157. //==============================================================================
  158. /**
  159. A utility class for values that need smoothing, like volume, that should not
  160. change abruptly to avoid audio glitches.
  161. To smooth values spread across an exponential range, where the increments
  162. between the current and target value are multiplicative (like frequencies),
  163. you should pass the multiplicative smoothing type as a template parameter:
  164. @code
  165. SmoothedValue<float, ValueSmoothingTypes::Multiplicative> yourSmoothedValue;
  166. @endcode
  167. Note that when you are using multiplicative smoothing you cannot ever reach a
  168. target value of zero!
  169. @tags{Audio}
  170. */
  171. template <typename FloatType, typename SmoothingType = ValueSmoothingTypes::Linear>
  172. class SmoothedValue : public SmoothedValueBase <SmoothedValue <FloatType, SmoothingType>>
  173. {
  174. public:
  175. //==============================================================================
  176. /** Constructor. */
  177. SmoothedValue() noexcept
  178. : SmoothedValue ((FloatType) (std::is_same<SmoothingType, ValueSmoothingTypes::Linear>::value ? 0 : 1))
  179. {
  180. }
  181. /** Constructor. */
  182. SmoothedValue (FloatType initialValue) noexcept
  183. {
  184. // Multiplicative smoothed values cannot ever reach 0!
  185. jassert (! (std::is_same<SmoothingType, ValueSmoothingTypes::Multiplicative>::value && initialValue == 0));
  186. // Visual Studio can't handle base class initialisation with CRTP
  187. this->currentValue = initialValue;
  188. this->target = this->currentValue;
  189. }
  190. //==============================================================================
  191. /** Reset to a new sample rate and ramp length.
  192. @param sampleRate The sample rate
  193. @param rampLengthInSeconds The duration of the ramp in seconds
  194. */
  195. void reset (double sampleRate, double rampLengthInSeconds) noexcept
  196. {
  197. jassert (sampleRate > 0 && rampLengthInSeconds >= 0);
  198. reset ((int) std::floor (rampLengthInSeconds * sampleRate));
  199. }
  200. /** Set a new ramp length directly in samples.
  201. @param numSteps The number of samples over which the ramp should be active
  202. */
  203. void reset (int numSteps) noexcept
  204. {
  205. stepsToTarget = numSteps;
  206. this->setCurrentAndTargetValue (this->target);
  207. }
  208. //==============================================================================
  209. /** Set the next value to ramp towards.
  210. @param newValue The new target value
  211. */
  212. void setTargetValue (FloatType newValue) noexcept
  213. {
  214. if (newValue == this->target)
  215. return;
  216. if (stepsToTarget <= 0)
  217. {
  218. this->setCurrentAndTargetValue (newValue);
  219. return;
  220. }
  221. // Multiplicative smoothed values cannot ever reach 0!
  222. jassert (! (std::is_same<SmoothingType, ValueSmoothingTypes::Multiplicative>::value && newValue == 0));
  223. this->target = newValue;
  224. this->countdown = stepsToTarget;
  225. setStepSize();
  226. }
  227. //==============================================================================
  228. /** Compute the next value.
  229. @returns Smoothed value
  230. */
  231. FloatType getNextValue() noexcept
  232. {
  233. if (! this->isSmoothing())
  234. return this->target;
  235. --(this->countdown);
  236. if (this->isSmoothing())
  237. setNextValue();
  238. else
  239. this->currentValue = this->target;
  240. return this->currentValue;
  241. }
  242. //==============================================================================
  243. /** Skip the next numSamples samples.
  244. This is identical to calling getNextValue numSamples times. It returns
  245. the new current value.
  246. @see getNextValue
  247. */
  248. FloatType skip (int numSamples) noexcept
  249. {
  250. if (numSamples >= this->countdown)
  251. {
  252. this->setCurrentAndTargetValue (this->target);
  253. return this->target;
  254. }
  255. skipCurrentValue (numSamples);
  256. this->countdown -= numSamples;
  257. return this->currentValue;
  258. }
  259. //==============================================================================
  260. /** THIS FUNCTION IS DEPRECATED.
  261. Use `setTargetValue (float)` and `setCurrentAndTargetValue()` instead:
  262. lsv.setValue (x, false); -> lsv.setTargetValue (x);
  263. lsv.setValue (x, true); -> lsv.setCurrentAndTargetValue (x);
  264. @param newValue The new target value
  265. @param force If true, the value will be set immediately, bypassing the ramp
  266. */
  267. JUCE_DEPRECATED_WITH_BODY (void setValue (FloatType newValue, bool force = false) noexcept,
  268. {
  269. if (force)
  270. {
  271. this->setCurrentAndTargetValue (newValue);
  272. return;
  273. }
  274. setTargetValue (newValue);
  275. })
  276. private:
  277. //==============================================================================
  278. template <typename T>
  279. using LinearVoid = typename std::enable_if <std::is_same <T, ValueSmoothingTypes::Linear>::value, void>::type;
  280. template <typename T>
  281. using MultiplicativeVoid = typename std::enable_if <std::is_same <T, ValueSmoothingTypes::Multiplicative>::value, void>::type;
  282. //==============================================================================
  283. template <typename T = SmoothingType>
  284. LinearVoid<T> setStepSize() noexcept
  285. {
  286. step = (this->target - this->currentValue) / (FloatType) this->countdown;
  287. }
  288. template <typename T = SmoothingType>
  289. MultiplicativeVoid<T> setStepSize()
  290. {
  291. step = std::exp ((std::log (std::abs (this->target)) - std::log (std::abs (this->currentValue))) / this->countdown);
  292. }
  293. //==============================================================================
  294. template <typename T = SmoothingType>
  295. LinearVoid<T> setNextValue() noexcept
  296. {
  297. this->currentValue += step;
  298. }
  299. template <typename T = SmoothingType>
  300. MultiplicativeVoid<T> setNextValue() noexcept
  301. {
  302. this->currentValue *= step;
  303. }
  304. //==============================================================================
  305. template <typename T = SmoothingType>
  306. LinearVoid<T> skipCurrentValue (int numSamples) noexcept
  307. {
  308. this->currentValue += step * (FloatType) numSamples;
  309. }
  310. template <typename T = SmoothingType>
  311. MultiplicativeVoid<T> skipCurrentValue (int numSamples)
  312. {
  313. this->currentValue *= (FloatType) std::pow (step, numSamples);
  314. }
  315. //==============================================================================
  316. FloatType step = FloatType();
  317. int stepsToTarget = 0;
  318. };
  319. template <typename FloatType>
  320. using LinearSmoothedValue = SmoothedValue <FloatType, ValueSmoothingTypes::Linear>;
  321. } // namespace juce