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

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