Audio plugin host https://kx.studio/carla
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.

juce_Reverb.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. Performs a simple reverb effect on a stream of audio data.
  22. This is a simple stereo reverb, based on the technique and tunings used in FreeVerb.
  23. Use setSampleRate() to prepare it, and then call processStereo() or processMono() to
  24. apply the reverb to your audio data.
  25. @see ReverbAudioSource
  26. @tags{Audio}
  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. float roomSize = 0.5f; /**< Room size, 0 to 1.0, where 1.0 is big, 0 is small. */
  42. float damping = 0.5f; /**< Damping, 0 to 1.0, where 0 is not damped, 1.0 is fully damped. */
  43. float wetLevel = 0.33f; /**< Wet level, 0 to 1.0 */
  44. float dryLevel = 0.4f; /**< Dry level, 0 to 1.0 */
  45. float width = 1.0f; /**< Reverb width, 0 to 1.0, where 1.0 is very wide. */
  46. float freezeMode = 0.0f; /**< Freeze mode - values < 0.5 are "normal" mode, values > 0.5
  47. put the reverb into a continuous feedback loop. */
  48. };
  49. //==============================================================================
  50. /** Returns the reverb's current parameters. */
  51. const Parameters& getParameters() const noexcept { return parameters; }
  52. /** Applies a new set of parameters to the reverb.
  53. Note that this doesn't attempt to lock the reverb, so if you call this in parallel with
  54. the process method, you may get artifacts.
  55. */
  56. void setParameters (const Parameters& newParams)
  57. {
  58. const float wetScaleFactor = 3.0f;
  59. const float dryScaleFactor = 2.0f;
  60. const float wet = newParams.wetLevel * wetScaleFactor;
  61. dryGain.setTargetValue (newParams.dryLevel * dryScaleFactor);
  62. wetGain1.setTargetValue (0.5f * wet * (1.0f + newParams.width));
  63. wetGain2.setTargetValue (0.5f * wet * (1.0f - newParams.width));
  64. gain = isFrozen (newParams.freezeMode) ? 0.0f : 0.015f;
  65. parameters = newParams;
  66. updateDamping();
  67. }
  68. //==============================================================================
  69. /** Sets the sample rate that will be used for the reverb.
  70. You must call this before the process methods, in order to tell it the correct sample rate.
  71. */
  72. void setSampleRate (const double sampleRate)
  73. {
  74. jassert (sampleRate > 0);
  75. static const short combTunings[] = { 1116, 1188, 1277, 1356, 1422, 1491, 1557, 1617 }; // (at 44100Hz)
  76. static const short allPassTunings[] = { 556, 441, 341, 225 };
  77. const int stereoSpread = 23;
  78. const int intSampleRate = (int) sampleRate;
  79. for (int i = 0; i < numCombs; ++i)
  80. {
  81. comb[0][i].setSize ((intSampleRate * combTunings[i]) / 44100);
  82. comb[1][i].setSize ((intSampleRate * (combTunings[i] + stereoSpread)) / 44100);
  83. }
  84. for (int i = 0; i < numAllPasses; ++i)
  85. {
  86. allPass[0][i].setSize ((intSampleRate * allPassTunings[i]) / 44100);
  87. allPass[1][i].setSize ((intSampleRate * (allPassTunings[i] + stereoSpread)) / 44100);
  88. }
  89. const double smoothTime = 0.01;
  90. damping .reset (sampleRate, smoothTime);
  91. feedback.reset (sampleRate, smoothTime);
  92. dryGain .reset (sampleRate, smoothTime);
  93. wetGain1.reset (sampleRate, smoothTime);
  94. wetGain2.reset (sampleRate, smoothTime);
  95. }
  96. /** Clears the reverb's buffers. */
  97. void reset()
  98. {
  99. for (int j = 0; j < numChannels; ++j)
  100. {
  101. for (int i = 0; i < numCombs; ++i)
  102. comb[j][i].clear();
  103. for (int i = 0; i < numAllPasses; ++i)
  104. allPass[j][i].clear();
  105. }
  106. }
  107. //==============================================================================
  108. /** Applies the reverb to two stereo channels of audio data. */
  109. void processStereo (float* const left, float* const right, const int numSamples) noexcept
  110. {
  111. JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6011)
  112. jassert (left != nullptr && right != nullptr);
  113. for (int i = 0; i < numSamples; ++i)
  114. {
  115. const float input = (left[i] + right[i]) * gain;
  116. float outL = 0, outR = 0;
  117. const float damp = damping.getNextValue();
  118. const float feedbck = feedback.getNextValue();
  119. for (int j = 0; j < numCombs; ++j) // accumulate the comb filters in parallel
  120. {
  121. outL += comb[0][j].process (input, damp, feedbck);
  122. outR += comb[1][j].process (input, damp, feedbck);
  123. }
  124. for (int j = 0; j < numAllPasses; ++j) // run the allpass filters in series
  125. {
  126. outL = allPass[0][j].process (outL);
  127. outR = allPass[1][j].process (outR);
  128. }
  129. const float dry = dryGain.getNextValue();
  130. const float wet1 = wetGain1.getNextValue();
  131. const float wet2 = wetGain2.getNextValue();
  132. left[i] = outL * wet1 + outR * wet2 + left[i] * dry;
  133. right[i] = outR * wet1 + outL * wet2 + right[i] * dry;
  134. }
  135. JUCE_END_IGNORE_WARNINGS_MSVC
  136. }
  137. /** Applies the reverb to a single mono channel of audio data. */
  138. void processMono (float* const samples, const int numSamples) noexcept
  139. {
  140. JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6011)
  141. jassert (samples != nullptr);
  142. for (int i = 0; i < numSamples; ++i)
  143. {
  144. const float input = samples[i] * gain;
  145. float output = 0;
  146. const float damp = damping.getNextValue();
  147. const float feedbck = feedback.getNextValue();
  148. for (int j = 0; j < numCombs; ++j) // accumulate the comb filters in parallel
  149. output += comb[0][j].process (input, damp, feedbck);
  150. for (int j = 0; j < numAllPasses; ++j) // run the allpass filters in series
  151. output = allPass[0][j].process (output);
  152. const float dry = dryGain.getNextValue();
  153. const float wet1 = wetGain1.getNextValue();
  154. samples[i] = output * wet1 + samples[i] * dry;
  155. }
  156. JUCE_END_IGNORE_WARNINGS_MSVC
  157. }
  158. private:
  159. //==============================================================================
  160. static bool isFrozen (const float freezeMode) noexcept { return freezeMode >= 0.5f; }
  161. void updateDamping() noexcept
  162. {
  163. const float roomScaleFactor = 0.28f;
  164. const float roomOffset = 0.7f;
  165. const float dampScaleFactor = 0.4f;
  166. if (isFrozen (parameters.freezeMode))
  167. setDamping (0.0f, 1.0f);
  168. else
  169. setDamping (parameters.damping * dampScaleFactor,
  170. parameters.roomSize * roomScaleFactor + roomOffset);
  171. }
  172. void setDamping (const float dampingToUse, const float roomSizeToUse) noexcept
  173. {
  174. damping.setTargetValue (dampingToUse);
  175. feedback.setTargetValue (roomSizeToUse);
  176. }
  177. //==============================================================================
  178. class CombFilter
  179. {
  180. public:
  181. CombFilter() noexcept {}
  182. void setSize (const int size)
  183. {
  184. if (size != bufferSize)
  185. {
  186. bufferIndex = 0;
  187. buffer.malloc (size);
  188. bufferSize = size;
  189. }
  190. clear();
  191. }
  192. void clear() noexcept
  193. {
  194. last = 0;
  195. buffer.clear ((size_t) bufferSize);
  196. }
  197. float process (const float input, const float damp, const float feedbackLevel) noexcept
  198. {
  199. const float output = buffer[bufferIndex];
  200. last = (output * (1.0f - damp)) + (last * damp);
  201. JUCE_UNDENORMALISE (last);
  202. float temp = input + (last * feedbackLevel);
  203. JUCE_UNDENORMALISE (temp);
  204. buffer[bufferIndex] = temp;
  205. bufferIndex = (bufferIndex + 1) % bufferSize;
  206. return output;
  207. }
  208. private:
  209. HeapBlock<float> buffer;
  210. int bufferSize = 0, bufferIndex = 0;
  211. float last = 0.0f;
  212. JUCE_DECLARE_NON_COPYABLE (CombFilter)
  213. };
  214. //==============================================================================
  215. class AllPassFilter
  216. {
  217. public:
  218. AllPassFilter() noexcept {}
  219. void setSize (const int size)
  220. {
  221. if (size != bufferSize)
  222. {
  223. bufferIndex = 0;
  224. buffer.malloc (size);
  225. bufferSize = size;
  226. }
  227. clear();
  228. }
  229. void clear() noexcept
  230. {
  231. buffer.clear ((size_t) bufferSize);
  232. }
  233. float process (const float input) noexcept
  234. {
  235. const float bufferedValue = buffer [bufferIndex];
  236. float temp = input + (bufferedValue * 0.5f);
  237. JUCE_UNDENORMALISE (temp);
  238. buffer [bufferIndex] = temp;
  239. bufferIndex = (bufferIndex + 1) % bufferSize;
  240. return bufferedValue - input;
  241. }
  242. private:
  243. HeapBlock<float> buffer;
  244. int bufferSize = 0, bufferIndex = 0;
  245. JUCE_DECLARE_NON_COPYABLE (AllPassFilter)
  246. };
  247. //==============================================================================
  248. enum { numCombs = 8, numAllPasses = 4, numChannels = 2 };
  249. Parameters parameters;
  250. float gain;
  251. CombFilter comb [numChannels][numCombs];
  252. AllPassFilter allPass [numChannels][numAllPasses];
  253. SmoothedValue<float> damping, feedback, dryGain, wetGain1, wetGain2;
  254. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Reverb)
  255. };
  256. } // namespace juce