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.

326 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_REVERB_JUCEHEADER__
  19. #define __JUCE_REVERB_JUCEHEADER__
  20. //==============================================================================
  21. /**
  22. Performs a simple reverb effect on a stream of audio data.
  23. This is a simple stereo reverb, based on the technique and tunings used in FreeVerb.
  24. Use setSampleRate() to prepare it, and then call processStereo() or processMono() to
  25. apply the reverb to your audio data.
  26. @see ReverbAudioSource
  27. */
  28. class Reverb
  29. {
  30. public:
  31. //==============================================================================
  32. Reverb()
  33. {
  34. setParameters (Parameters());
  35. setSampleRate (44100.0);
  36. }
  37. //==============================================================================
  38. /** Holds the parameters being used by a Reverb object. */
  39. struct Parameters
  40. {
  41. Parameters() noexcept
  42. : roomSize (0.5f),
  43. damping (0.5f),
  44. wetLevel (0.33f),
  45. dryLevel (0.4f),
  46. width (1.0f),
  47. freezeMode (0)
  48. {}
  49. float roomSize; /**< Room size, 0 to 1.0, where 1.0 is big, 0 is small. */
  50. float damping; /**< Damping, 0 to 1.0, where 0 is not damped, 1.0 is fully damped. */
  51. float wetLevel; /**< Wet level, 0 to 1.0 */
  52. float dryLevel; /**< Dry level, 0 to 1.0 */
  53. float width; /**< Reverb width, 0 to 1.0, where 1.0 is very wide. */
  54. float freezeMode; /**< Freeze mode - values < 0.5 are "normal" mode, values > 0.5
  55. put the reverb into a continuous feedback loop. */
  56. };
  57. //==============================================================================
  58. /** Returns the reverb's current parameters. */
  59. const Parameters& getParameters() const noexcept { return parameters; }
  60. /** Applies a new set of parameters to the reverb.
  61. Note that this doesn't attempt to lock the reverb, so if you call this in parallel with
  62. the process method, you may get artifacts.
  63. */
  64. void setParameters (const Parameters& newParams)
  65. {
  66. const float wetScaleFactor = 3.0f;
  67. const float dryScaleFactor = 2.0f;
  68. const float wet = newParams.wetLevel * wetScaleFactor;
  69. wet1 = wet * (newParams.width * 0.5f + 0.5f);
  70. wet2 = wet * (1.0f - newParams.width) * 0.5f;
  71. dry = newParams.dryLevel * dryScaleFactor;
  72. gain = isFrozen (newParams.freezeMode) ? 0.0f : 0.015f;
  73. parameters = newParams;
  74. shouldUpdateDamping = true;
  75. }
  76. //==============================================================================
  77. /** Sets the sample rate that will be used for the reverb.
  78. You must call this before the process methods, in order to tell it the correct sample rate.
  79. */
  80. void setSampleRate (const double sampleRate)
  81. {
  82. jassert (sampleRate > 0);
  83. static const short combTunings[] = { 1116, 1188, 1277, 1356, 1422, 1491, 1557, 1617 }; // (at 44100Hz)
  84. static const short allPassTunings[] = { 556, 441, 341, 225 };
  85. const int stereoSpread = 23;
  86. const int intSampleRate = (int) sampleRate;
  87. int i;
  88. for (i = 0; i < numCombs; ++i)
  89. {
  90. comb[0][i].setSize ((intSampleRate * combTunings[i]) / 44100);
  91. comb[1][i].setSize ((intSampleRate * (combTunings[i] + stereoSpread)) / 44100);
  92. }
  93. for (i = 0; i < numAllPasses; ++i)
  94. {
  95. allPass[0][i].setSize ((intSampleRate * allPassTunings[i]) / 44100);
  96. allPass[1][i].setSize ((intSampleRate * (allPassTunings[i] + stereoSpread)) / 44100);
  97. }
  98. shouldUpdateDamping = true;
  99. }
  100. /** Clears the reverb's buffers. */
  101. void reset()
  102. {
  103. for (int j = 0; j < numChannels; ++j)
  104. {
  105. int i;
  106. for (i = 0; i < numCombs; ++i)
  107. comb[j][i].clear();
  108. for (i = 0; i < numAllPasses; ++i)
  109. allPass[j][i].clear();
  110. }
  111. }
  112. //==============================================================================
  113. /** Applies the reverb to two stereo channels of audio data. */
  114. void processStereo (float* const left, float* const right, const int numSamples) noexcept
  115. {
  116. jassert (left != nullptr && right != nullptr);
  117. if (shouldUpdateDamping)
  118. updateDamping();
  119. for (int i = 0; i < numSamples; ++i)
  120. {
  121. const float input = (left[i] + right[i]) * gain;
  122. float outL = 0, outR = 0;
  123. int j;
  124. for (j = 0; j < numCombs; ++j) // accumulate the comb filters in parallel
  125. {
  126. outL += comb[0][j].process (input);
  127. outR += comb[1][j].process (input);
  128. }
  129. for (j = 0; j < numAllPasses; ++j) // run the allpass filters in series
  130. {
  131. outL = allPass[0][j].process (outL);
  132. outR = allPass[1][j].process (outR);
  133. }
  134. left[i] = outL * wet1 + outR * wet2 + left[i] * dry;
  135. right[i] = outR * wet1 + outL * wet2 + right[i] * dry;
  136. }
  137. }
  138. /** Applies the reverb to a single mono channel of audio data. */
  139. void processMono (float* const samples, const int numSamples) noexcept
  140. {
  141. jassert (samples != nullptr);
  142. if (shouldUpdateDamping)
  143. updateDamping();
  144. for (int i = 0; i < numSamples; ++i)
  145. {
  146. const float input = samples[i] * gain;
  147. float output = 0;
  148. int j;
  149. for (j = 0; j < numCombs; ++j) // accumulate the comb filters in parallel
  150. output += comb[0][j].process (input);
  151. for (j = 0; j < numAllPasses; ++j) // run the allpass filters in series
  152. output = allPass[0][j].process (output);
  153. samples[i] = output * wet1 + input * dry;
  154. }
  155. }
  156. private:
  157. //==============================================================================
  158. Parameters parameters;
  159. volatile bool shouldUpdateDamping;
  160. float gain, wet1, wet2, dry;
  161. inline static bool isFrozen (const float freezeMode) noexcept { return freezeMode >= 0.5f; }
  162. void updateDamping() noexcept
  163. {
  164. const float roomScaleFactor = 0.28f;
  165. const float roomOffset = 0.7f;
  166. const float dampScaleFactor = 0.4f;
  167. shouldUpdateDamping = false;
  168. if (isFrozen (parameters.freezeMode))
  169. setDamping (1.0f, 0.0f);
  170. else
  171. setDamping (parameters.damping * dampScaleFactor,
  172. parameters.roomSize * roomScaleFactor + roomOffset);
  173. }
  174. void setDamping (const float dampingToUse, const float roomSizeToUse) noexcept
  175. {
  176. for (int j = 0; j < numChannels; ++j)
  177. for (int i = numCombs; --i >= 0;)
  178. comb[j][i].setFeedbackAndDamp (roomSizeToUse, dampingToUse);
  179. }
  180. //==============================================================================
  181. class CombFilter
  182. {
  183. public:
  184. CombFilter() noexcept : bufferSize (0), bufferIndex (0) {}
  185. void setSize (const int size)
  186. {
  187. if (size != bufferSize)
  188. {
  189. bufferIndex = 0;
  190. buffer.malloc ((size_t) size);
  191. bufferSize = size;
  192. }
  193. clear();
  194. }
  195. void clear() noexcept
  196. {
  197. last = 0;
  198. buffer.clear ((size_t) bufferSize);
  199. }
  200. void setFeedbackAndDamp (const float f, const float d) noexcept
  201. {
  202. damp1 = d;
  203. damp2 = 1.0f - d;
  204. feedback = f;
  205. }
  206. inline float process (const float input) noexcept
  207. {
  208. const float output = buffer [bufferIndex];
  209. last = (output * damp2) + (last * damp1);
  210. JUCE_UNDENORMALISE (last);
  211. float temp = input + (last * feedback);
  212. JUCE_UNDENORMALISE (temp);
  213. buffer [bufferIndex] = temp;
  214. bufferIndex = (bufferIndex + 1) % bufferSize;
  215. return output;
  216. }
  217. private:
  218. HeapBlock<float> buffer;
  219. int bufferSize, bufferIndex;
  220. float feedback, last, damp1, damp2;
  221. JUCE_DECLARE_NON_COPYABLE (CombFilter);
  222. };
  223. //==============================================================================
  224. class AllPassFilter
  225. {
  226. public:
  227. AllPassFilter() noexcept : bufferSize (0), bufferIndex (0) {}
  228. void setSize (const int size)
  229. {
  230. if (size != bufferSize)
  231. {
  232. bufferIndex = 0;
  233. buffer.malloc ((size_t) size);
  234. bufferSize = size;
  235. }
  236. clear();
  237. }
  238. void clear() noexcept
  239. {
  240. buffer.clear ((size_t) bufferSize);
  241. }
  242. inline float process (const float input) noexcept
  243. {
  244. const float bufferedValue = buffer [bufferIndex];
  245. float temp = input + (bufferedValue * 0.5f);
  246. JUCE_UNDENORMALISE (temp);
  247. buffer [bufferIndex] = temp;
  248. bufferIndex = (bufferIndex + 1) % bufferSize;
  249. return bufferedValue - input;
  250. }
  251. private:
  252. HeapBlock<float> buffer;
  253. int bufferSize, bufferIndex;
  254. JUCE_DECLARE_NON_COPYABLE (AllPassFilter);
  255. };
  256. enum { numCombs = 8, numAllPasses = 4, numChannels = 2 };
  257. CombFilter comb [numChannels][numCombs];
  258. AllPassFilter allPass [numChannels][numAllPasses];
  259. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Reverb);
  260. };
  261. #endif // __JUCE_REVERB_JUCEHEADER__