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.

327 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #pragma once
  24. //==============================================================================
  25. /**
  26. Performs a simple reverb effect on a stream of audio data.
  27. This is a simple stereo reverb, based on the technique and tunings used in FreeVerb.
  28. Use setSampleRate() to prepare it, and then call processStereo() or processMono() to
  29. apply the reverb to your audio data.
  30. @see ReverbAudioSource
  31. */
  32. class Reverb
  33. {
  34. public:
  35. //==============================================================================
  36. Reverb()
  37. {
  38. setParameters (Parameters());
  39. setSampleRate (44100.0);
  40. }
  41. //==============================================================================
  42. /** Holds the parameters being used by a Reverb object. */
  43. struct Parameters
  44. {
  45. Parameters() noexcept
  46. : roomSize (0.5f),
  47. damping (0.5f),
  48. wetLevel (0.33f),
  49. dryLevel (0.4f),
  50. width (1.0f),
  51. freezeMode (0)
  52. {}
  53. float roomSize; /**< Room size, 0 to 1.0, where 1.0 is big, 0 is small. */
  54. float damping; /**< Damping, 0 to 1.0, where 0 is not damped, 1.0 is fully damped. */
  55. float wetLevel; /**< Wet level, 0 to 1.0 */
  56. float dryLevel; /**< Dry level, 0 to 1.0 */
  57. float width; /**< Reverb width, 0 to 1.0, where 1.0 is very wide. */
  58. float freezeMode; /**< Freeze mode - values < 0.5 are "normal" mode, values > 0.5
  59. put the reverb into a continuous feedback loop. */
  60. };
  61. //==============================================================================
  62. /** Returns the reverb's current parameters. */
  63. const Parameters& getParameters() const noexcept { return parameters; }
  64. /** Applies a new set of parameters to the reverb.
  65. Note that this doesn't attempt to lock the reverb, so if you call this in parallel with
  66. the process method, you may get artifacts.
  67. */
  68. void setParameters (const Parameters& newParams)
  69. {
  70. const float wetScaleFactor = 3.0f;
  71. const float dryScaleFactor = 2.0f;
  72. const float wet = newParams.wetLevel * wetScaleFactor;
  73. dryGain.setValue (newParams.dryLevel * dryScaleFactor);
  74. wetGain1.setValue (0.5f * wet * (1.0f + newParams.width));
  75. wetGain2.setValue (0.5f * wet * (1.0f - newParams.width));
  76. gain = isFrozen (newParams.freezeMode) ? 0.0f : 0.015f;
  77. parameters = newParams;
  78. updateDamping();
  79. }
  80. //==============================================================================
  81. /** Sets the sample rate that will be used for the reverb.
  82. You must call this before the process methods, in order to tell it the correct sample rate.
  83. */
  84. void setSampleRate (const double sampleRate)
  85. {
  86. jassert (sampleRate > 0);
  87. static const short combTunings[] = { 1116, 1188, 1277, 1356, 1422, 1491, 1557, 1617 }; // (at 44100Hz)
  88. static const short allPassTunings[] = { 556, 441, 341, 225 };
  89. const int stereoSpread = 23;
  90. const int intSampleRate = (int) sampleRate;
  91. for (int i = 0; i < numCombs; ++i)
  92. {
  93. comb[0][i].setSize ((intSampleRate * combTunings[i]) / 44100);
  94. comb[1][i].setSize ((intSampleRate * (combTunings[i] + stereoSpread)) / 44100);
  95. }
  96. for (int i = 0; i < numAllPasses; ++i)
  97. {
  98. allPass[0][i].setSize ((intSampleRate * allPassTunings[i]) / 44100);
  99. allPass[1][i].setSize ((intSampleRate * (allPassTunings[i] + stereoSpread)) / 44100);
  100. }
  101. const double smoothTime = 0.01;
  102. damping .reset (sampleRate, smoothTime);
  103. feedback.reset (sampleRate, smoothTime);
  104. dryGain .reset (sampleRate, smoothTime);
  105. wetGain1.reset (sampleRate, smoothTime);
  106. wetGain2.reset (sampleRate, smoothTime);
  107. }
  108. /** Clears the reverb's buffers. */
  109. void reset()
  110. {
  111. for (int j = 0; j < numChannels; ++j)
  112. {
  113. for (int i = 0; i < numCombs; ++i)
  114. comb[j][i].clear();
  115. for (int i = 0; i < numAllPasses; ++i)
  116. allPass[j][i].clear();
  117. }
  118. }
  119. //==============================================================================
  120. /** Applies the reverb to two stereo channels of audio data. */
  121. void processStereo (float* const left, float* const right, const int numSamples) noexcept
  122. {
  123. jassert (left != nullptr && right != nullptr);
  124. for (int i = 0; i < numSamples; ++i)
  125. {
  126. const float input = (left[i] + right[i]) * gain;
  127. float outL = 0, outR = 0;
  128. const float damp = damping.getNextValue();
  129. const float feedbck = feedback.getNextValue();
  130. for (int j = 0; j < numCombs; ++j) // accumulate the comb filters in parallel
  131. {
  132. outL += comb[0][j].process (input, damp, feedbck);
  133. outR += comb[1][j].process (input, damp, feedbck);
  134. }
  135. for (int j = 0; j < numAllPasses; ++j) // run the allpass filters in series
  136. {
  137. outL = allPass[0][j].process (outL);
  138. outR = allPass[1][j].process (outR);
  139. }
  140. const float dry = dryGain.getNextValue();
  141. const float wet1 = wetGain1.getNextValue();
  142. const float wet2 = wetGain2.getNextValue();
  143. left[i] = outL * wet1 + outR * wet2 + left[i] * dry;
  144. right[i] = outR * wet1 + outL * wet2 + right[i] * dry;
  145. }
  146. }
  147. /** Applies the reverb to a single mono channel of audio data. */
  148. void processMono (float* const samples, const int numSamples) noexcept
  149. {
  150. jassert (samples != nullptr);
  151. for (int i = 0; i < numSamples; ++i)
  152. {
  153. const float input = samples[i] * gain;
  154. float output = 0;
  155. const float damp = damping.getNextValue();
  156. const float feedbck = feedback.getNextValue();
  157. for (int j = 0; j < numCombs; ++j) // accumulate the comb filters in parallel
  158. output += comb[0][j].process (input, damp, feedbck);
  159. for (int j = 0; j < numAllPasses; ++j) // run the allpass filters in series
  160. output = allPass[0][j].process (output);
  161. const float dry = dryGain.getNextValue();
  162. const float wet1 = wetGain1.getNextValue();
  163. samples[i] = output * wet1 + samples[i] * dry;
  164. }
  165. }
  166. private:
  167. //==============================================================================
  168. static bool isFrozen (const float freezeMode) noexcept { return freezeMode >= 0.5f; }
  169. void updateDamping() noexcept
  170. {
  171. const float roomScaleFactor = 0.28f;
  172. const float roomOffset = 0.7f;
  173. const float dampScaleFactor = 0.4f;
  174. if (isFrozen (parameters.freezeMode))
  175. setDamping (0.0f, 1.0f);
  176. else
  177. setDamping (parameters.damping * dampScaleFactor,
  178. parameters.roomSize * roomScaleFactor + roomOffset);
  179. }
  180. void setDamping (const float dampingToUse, const float roomSizeToUse) noexcept
  181. {
  182. damping.setValue (dampingToUse);
  183. feedback.setValue (roomSizeToUse);
  184. }
  185. //==============================================================================
  186. class CombFilter
  187. {
  188. public:
  189. CombFilter() noexcept : bufferSize (0), bufferIndex (0), last (0) {}
  190. void setSize (const int size)
  191. {
  192. if (size != bufferSize)
  193. {
  194. bufferIndex = 0;
  195. buffer.malloc ((size_t) size);
  196. bufferSize = size;
  197. }
  198. clear();
  199. }
  200. void clear() noexcept
  201. {
  202. last = 0;
  203. buffer.clear ((size_t) bufferSize);
  204. }
  205. float process (const float input, const float damp, const float feedbackLevel) noexcept
  206. {
  207. const float output = buffer[bufferIndex];
  208. last = (output * (1.0f - damp)) + (last * damp);
  209. JUCE_UNDENORMALISE (last);
  210. float temp = input + (last * feedbackLevel);
  211. JUCE_UNDENORMALISE (temp);
  212. buffer[bufferIndex] = temp;
  213. bufferIndex = (bufferIndex + 1) % bufferSize;
  214. return output;
  215. }
  216. private:
  217. HeapBlock<float> buffer;
  218. int bufferSize, bufferIndex;
  219. float last;
  220. JUCE_DECLARE_NON_COPYABLE (CombFilter)
  221. };
  222. //==============================================================================
  223. class AllPassFilter
  224. {
  225. public:
  226. AllPassFilter() noexcept : bufferSize (0), bufferIndex (0) {}
  227. void setSize (const int size)
  228. {
  229. if (size != bufferSize)
  230. {
  231. bufferIndex = 0;
  232. buffer.malloc ((size_t) size);
  233. bufferSize = size;
  234. }
  235. clear();
  236. }
  237. void clear() noexcept
  238. {
  239. buffer.clear ((size_t) bufferSize);
  240. }
  241. float process (const float input) noexcept
  242. {
  243. const float bufferedValue = buffer [bufferIndex];
  244. float temp = input + (bufferedValue * 0.5f);
  245. JUCE_UNDENORMALISE (temp);
  246. buffer [bufferIndex] = temp;
  247. bufferIndex = (bufferIndex + 1) % bufferSize;
  248. return bufferedValue - input;
  249. }
  250. private:
  251. HeapBlock<float> buffer;
  252. int bufferSize, bufferIndex;
  253. JUCE_DECLARE_NON_COPYABLE (AllPassFilter)
  254. };
  255. //==============================================================================
  256. enum { numCombs = 8, numAllPasses = 4, numChannels = 2 };
  257. Parameters parameters;
  258. float gain;
  259. CombFilter comb [numChannels][numCombs];
  260. AllPassFilter allPass [numChannels][numAllPasses];
  261. LinearSmoothedValue<float> damping, feedback, dryGain, wetGain1, wetGain2;
  262. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Reverb)
  263. };