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 12KB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. #ifndef JUCE_REVERB_H_INCLUDED
  24. #define JUCE_REVERB_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. Performs a simple reverb effect on a stream of audio data.
  28. This is a simple stereo reverb, based on the technique and tunings used in FreeVerb.
  29. Use setSampleRate() to prepare it, and then call processStereo() or processMono() to
  30. apply the reverb to your audio data.
  31. @see ReverbAudioSource
  32. */
  33. class Reverb
  34. {
  35. public:
  36. //==============================================================================
  37. Reverb()
  38. {
  39. setParameters (Parameters());
  40. setSampleRate (44100.0);
  41. }
  42. //==============================================================================
  43. /** Holds the parameters being used by a Reverb object. */
  44. struct Parameters
  45. {
  46. Parameters() noexcept
  47. : roomSize (0.5f),
  48. damping (0.5f),
  49. wetLevel (0.33f),
  50. dryLevel (0.4f),
  51. width (1.0f),
  52. freezeMode (0)
  53. {}
  54. float roomSize; /**< Room size, 0 to 1.0, where 1.0 is big, 0 is small. */
  55. float damping; /**< Damping, 0 to 1.0, where 0 is not damped, 1.0 is fully damped. */
  56. float wetLevel; /**< Wet level, 0 to 1.0 */
  57. float dryLevel; /**< Dry level, 0 to 1.0 */
  58. float width; /**< Reverb width, 0 to 1.0, where 1.0 is very wide. */
  59. float freezeMode; /**< Freeze mode - values < 0.5 are "normal" mode, values > 0.5
  60. put the reverb into a continuous feedback loop. */
  61. };
  62. //==============================================================================
  63. /** Returns the reverb's current parameters. */
  64. const Parameters& getParameters() const noexcept { return parameters; }
  65. /** Applies a new set of parameters to the reverb.
  66. Note that this doesn't attempt to lock the reverb, so if you call this in parallel with
  67. the process method, you may get artifacts.
  68. */
  69. void setParameters (const Parameters& newParams)
  70. {
  71. const float wetScaleFactor = 3.0f;
  72. const float dryScaleFactor = 2.0f;
  73. const float wet = newParams.wetLevel * wetScaleFactor;
  74. dryGain.setValue (newParams.dryLevel * dryScaleFactor);
  75. wetGain1.setValue (0.5f * wet * (1.0f + newParams.width));
  76. wetGain2.setValue (0.5f * wet * (1.0f - newParams.width));
  77. gain = isFrozen (newParams.freezeMode) ? 0.0f : 0.015f;
  78. parameters = newParams;
  79. updateDamping();
  80. }
  81. //==============================================================================
  82. /** Sets the sample rate that will be used for the reverb.
  83. You must call this before the process methods, in order to tell it the correct sample rate.
  84. */
  85. void setSampleRate (const double sampleRate)
  86. {
  87. jassert (sampleRate > 0);
  88. static const short combTunings[] = { 1116, 1188, 1277, 1356, 1422, 1491, 1557, 1617 }; // (at 44100Hz)
  89. static const short allPassTunings[] = { 556, 441, 341, 225 };
  90. const int stereoSpread = 23;
  91. const int intSampleRate = (int) sampleRate;
  92. for (int i = 0; i < numCombs; ++i)
  93. {
  94. comb[0][i].setSize ((intSampleRate * combTunings[i]) / 44100);
  95. comb[1][i].setSize ((intSampleRate * (combTunings[i] + stereoSpread)) / 44100);
  96. }
  97. for (int i = 0; i < numAllPasses; ++i)
  98. {
  99. allPass[0][i].setSize ((intSampleRate * allPassTunings[i]) / 44100);
  100. allPass[1][i].setSize ((intSampleRate * (allPassTunings[i] + stereoSpread)) / 44100);
  101. }
  102. const double smoothTime = 0.01;
  103. damping .reset (sampleRate, smoothTime);
  104. feedback.reset (sampleRate, smoothTime);
  105. dryGain .reset (sampleRate, smoothTime);
  106. wetGain1.reset (sampleRate, smoothTime);
  107. wetGain2.reset (sampleRate, smoothTime);
  108. }
  109. /** Clears the reverb's buffers. */
  110. void reset()
  111. {
  112. for (int j = 0; j < numChannels; ++j)
  113. {
  114. for (int i = 0; i < numCombs; ++i)
  115. comb[j][i].clear();
  116. for (int i = 0; i < numAllPasses; ++i)
  117. allPass[j][i].clear();
  118. }
  119. }
  120. //==============================================================================
  121. /** Applies the reverb to two stereo channels of audio data. */
  122. void processStereo (float* const left, float* const right, const int numSamples) noexcept
  123. {
  124. jassert (left != nullptr && right != nullptr);
  125. for (int i = 0; i < numSamples; ++i)
  126. {
  127. const float input = (left[i] + right[i]) * gain;
  128. float outL = 0, outR = 0;
  129. const float damp = damping.getNextValue();
  130. const float feedbck = feedback.getNextValue();
  131. for (int j = 0; j < numCombs; ++j) // accumulate the comb filters in parallel
  132. {
  133. outL += comb[0][j].process (input, damp, feedbck);
  134. outR += comb[1][j].process (input, damp, feedbck);
  135. }
  136. for (int j = 0; j < numAllPasses; ++j) // run the allpass filters in series
  137. {
  138. outL = allPass[0][j].process (outL);
  139. outR = allPass[1][j].process (outR);
  140. }
  141. const float dry = dryGain.getNextValue();
  142. const float wet1 = wetGain1.getNextValue();
  143. const float wet2 = wetGain2.getNextValue();
  144. left[i] = outL * wet1 + outR * wet2 + left[i] * dry;
  145. right[i] = outR * wet1 + outL * wet2 + right[i] * dry;
  146. }
  147. }
  148. /** Applies the reverb to a single mono channel of audio data. */
  149. void processMono (float* const samples, const int numSamples) noexcept
  150. {
  151. jassert (samples != nullptr);
  152. for (int i = 0; i < numSamples; ++i)
  153. {
  154. const float input = samples[i] * gain;
  155. float output = 0;
  156. const float damp = damping.getNextValue();
  157. const float feedbck = feedback.getNextValue();
  158. for (int j = 0; j < numCombs; ++j) // accumulate the comb filters in parallel
  159. output += comb[0][j].process (input, damp, feedbck);
  160. for (int j = 0; j < numAllPasses; ++j) // run the allpass filters in series
  161. output = allPass[0][j].process (output);
  162. const float dry = dryGain.getNextValue();
  163. const float wet1 = wetGain1.getNextValue();
  164. samples[i] = output * wet1 + samples[i] * dry;
  165. }
  166. }
  167. private:
  168. //==============================================================================
  169. static bool isFrozen (const float freezeMode) noexcept { return freezeMode >= 0.5f; }
  170. void updateDamping() noexcept
  171. {
  172. const float roomScaleFactor = 0.28f;
  173. const float roomOffset = 0.7f;
  174. const float dampScaleFactor = 0.4f;
  175. if (isFrozen (parameters.freezeMode))
  176. setDamping (0.0f, 1.0f);
  177. else
  178. setDamping (parameters.damping * dampScaleFactor,
  179. parameters.roomSize * roomScaleFactor + roomOffset);
  180. }
  181. void setDamping (const float dampingToUse, const float roomSizeToUse) noexcept
  182. {
  183. damping.setValue (dampingToUse);
  184. feedback.setValue (roomSizeToUse);
  185. }
  186. //==============================================================================
  187. class CombFilter
  188. {
  189. public:
  190. CombFilter() noexcept : bufferSize (0), bufferIndex (0), last (0) {}
  191. void setSize (const int size)
  192. {
  193. if (size != bufferSize)
  194. {
  195. bufferIndex = 0;
  196. buffer.malloc ((size_t) size);
  197. bufferSize = size;
  198. }
  199. clear();
  200. }
  201. void clear() noexcept
  202. {
  203. last = 0;
  204. buffer.clear ((size_t) bufferSize);
  205. }
  206. float process (const float input, const float damp, const float feedbackLevel) noexcept
  207. {
  208. const float output = buffer[bufferIndex];
  209. last = (output * (1.0f - damp)) + (last * damp);
  210. JUCE_UNDENORMALISE (last);
  211. float temp = input + (last * feedbackLevel);
  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 last;
  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. 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. //==============================================================================
  257. enum { numCombs = 8, numAllPasses = 4, numChannels = 2 };
  258. Parameters parameters;
  259. float gain;
  260. CombFilter comb [numChannels][numCombs];
  261. AllPassFilter allPass [numChannels][numAllPasses];
  262. LinearSmoothedValue<float> damping, feedback, dryGain, wetGain1, wetGain2;
  263. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Reverb)
  264. };
  265. #endif // JUCE_REVERB_H_INCLUDED