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.

325 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. dryGain.setValue (newParams.dryLevel * dryScaleFactor);
  69. wetGain1.setValue (0.5f * wet * (1.0f + newParams.width));
  70. wetGain2.setValue (0.5f * wet * (1.0f - newParams.width));
  71. gain = isFrozen (newParams.freezeMode) ? 0.0f : 0.015f;
  72. parameters = newParams;
  73. updateDamping();
  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. const double smoothTime = 0.01;
  97. damping .reset (sampleRate, smoothTime);
  98. feedback.reset (sampleRate, smoothTime);
  99. dryGain .reset (sampleRate, smoothTime);
  100. wetGain1.reset (sampleRate, smoothTime);
  101. wetGain2.reset (sampleRate, smoothTime);
  102. }
  103. /** Clears the reverb's buffers. */
  104. void reset()
  105. {
  106. for (int j = 0; j < numChannels; ++j)
  107. {
  108. for (int i = 0; i < numCombs; ++i)
  109. comb[j][i].clear();
  110. for (int i = 0; i < numAllPasses; ++i)
  111. allPass[j][i].clear();
  112. }
  113. }
  114. //==============================================================================
  115. /** Applies the reverb to two stereo channels of audio data. */
  116. void processStereo (float* const left, float* const right, const int numSamples) noexcept
  117. {
  118. jassert (left != nullptr && right != nullptr);
  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. const float damp = damping.getNextValue();
  124. const float feedbck = feedback.getNextValue();
  125. for (int j = 0; j < numCombs; ++j) // accumulate the comb filters in parallel
  126. {
  127. outL += comb[0][j].process (input, damp, feedbck);
  128. outR += comb[1][j].process (input, damp, feedbck);
  129. }
  130. for (int j = 0; j < numAllPasses; ++j) // run the allpass filters in series
  131. {
  132. outL = allPass[0][j].process (outL);
  133. outR = allPass[1][j].process (outR);
  134. }
  135. const float dry = dryGain.getNextValue();
  136. const float wet1 = wetGain1.getNextValue();
  137. const float wet2 = wetGain2.getNextValue();
  138. left[i] = outL * wet1 + outR * wet2 + left[i] * dry;
  139. right[i] = outR * wet1 + outL * wet2 + right[i] * dry;
  140. }
  141. }
  142. /** Applies the reverb to a single mono channel of audio data. */
  143. void processMono (float* const samples, const int numSamples) noexcept
  144. {
  145. jassert (samples != nullptr);
  146. for (int i = 0; i < numSamples; ++i)
  147. {
  148. const float input = samples[i] * gain;
  149. float output = 0;
  150. const float damp = damping.getNextValue();
  151. const float feedbck = feedback.getNextValue();
  152. for (int j = 0; j < numCombs; ++j) // accumulate the comb filters in parallel
  153. output += comb[0][j].process (input, damp, feedbck);
  154. for (int j = 0; j < numAllPasses; ++j) // run the allpass filters in series
  155. output = allPass[0][j].process (output);
  156. const float dry = dryGain.getNextValue();
  157. const float wet1 = wetGain1.getNextValue();
  158. samples[i] = output * wet1 + samples[i] * dry;
  159. }
  160. }
  161. private:
  162. //==============================================================================
  163. static bool isFrozen (const float freezeMode) noexcept { return freezeMode >= 0.5f; }
  164. void updateDamping() noexcept
  165. {
  166. const float roomScaleFactor = 0.28f;
  167. const float roomOffset = 0.7f;
  168. const float dampScaleFactor = 0.4f;
  169. if (isFrozen (parameters.freezeMode))
  170. setDamping (0.0f, 1.0f);
  171. else
  172. setDamping (parameters.damping * dampScaleFactor,
  173. parameters.roomSize * roomScaleFactor + roomOffset);
  174. }
  175. void setDamping (const float dampingToUse, const float roomSizeToUse) noexcept
  176. {
  177. damping.setValue (dampingToUse);
  178. feedback.setValue (roomSizeToUse);
  179. }
  180. //==============================================================================
  181. class CombFilter
  182. {
  183. public:
  184. CombFilter() noexcept : bufferSize (0), bufferIndex (0), last (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. float process (const float input, const float damp, const float feedbackLevel) noexcept
  201. {
  202. const float output = buffer[bufferIndex];
  203. last = (output * (1.0f - damp)) + (last * damp);
  204. JUCE_UNDENORMALISE (last);
  205. float temp = input + (last * feedbackLevel);
  206. JUCE_UNDENORMALISE (temp);
  207. buffer[bufferIndex] = temp;
  208. bufferIndex = (bufferIndex + 1) % bufferSize;
  209. return output;
  210. }
  211. private:
  212. HeapBlock<float> buffer;
  213. int bufferSize, bufferIndex;
  214. float last;
  215. JUCE_DECLARE_NON_COPYABLE (CombFilter)
  216. };
  217. //==============================================================================
  218. class AllPassFilter
  219. {
  220. public:
  221. AllPassFilter() noexcept : bufferSize (0), bufferIndex (0) {}
  222. void setSize (const int size)
  223. {
  224. if (size != bufferSize)
  225. {
  226. bufferIndex = 0;
  227. buffer.malloc ((size_t) size);
  228. bufferSize = size;
  229. }
  230. clear();
  231. }
  232. void clear() noexcept
  233. {
  234. buffer.clear ((size_t) bufferSize);
  235. }
  236. float process (const float input) noexcept
  237. {
  238. const float bufferedValue = buffer [bufferIndex];
  239. float temp = input + (bufferedValue * 0.5f);
  240. JUCE_UNDENORMALISE (temp);
  241. buffer [bufferIndex] = temp;
  242. bufferIndex = (bufferIndex + 1) % bufferSize;
  243. return bufferedValue - input;
  244. }
  245. private:
  246. HeapBlock<float> buffer;
  247. int bufferSize, bufferIndex;
  248. JUCE_DECLARE_NON_COPYABLE (AllPassFilter)
  249. };
  250. //==============================================================================
  251. enum { numCombs = 8, numAllPasses = 4, numChannels = 2 };
  252. Parameters parameters;
  253. float gain;
  254. CombFilter comb [numChannels][numCombs];
  255. AllPassFilter allPass [numChannels][numAllPasses];
  256. LinearSmoothedValue<float> damping, feedback, dryGain, wetGain1, wetGain2;
  257. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Reverb)
  258. };
  259. #endif // JUCE_REVERB_H_INCLUDED