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.

314 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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. jassert (left != nullptr && right != nullptr);
  112. for (int i = 0; i < numSamples; ++i)
  113. {
  114. const float input = (left[i] + right[i]) * gain;
  115. float outL = 0, outR = 0;
  116. const float damp = damping.getNextValue();
  117. const float feedbck = feedback.getNextValue();
  118. for (int j = 0; j < numCombs; ++j) // accumulate the comb filters in parallel
  119. {
  120. outL += comb[0][j].process (input, damp, feedbck);
  121. outR += comb[1][j].process (input, damp, feedbck);
  122. }
  123. for (int j = 0; j < numAllPasses; ++j) // run the allpass filters in series
  124. {
  125. outL = allPass[0][j].process (outL);
  126. outR = allPass[1][j].process (outR);
  127. }
  128. const float dry = dryGain.getNextValue();
  129. const float wet1 = wetGain1.getNextValue();
  130. const float wet2 = wetGain2.getNextValue();
  131. left[i] = outL * wet1 + outR * wet2 + left[i] * dry;
  132. right[i] = outR * wet1 + outL * wet2 + right[i] * dry;
  133. }
  134. }
  135. /** Applies the reverb to a single mono channel of audio data. */
  136. void processMono (float* const samples, const int numSamples) noexcept
  137. {
  138. jassert (samples != nullptr);
  139. for (int i = 0; i < numSamples; ++i)
  140. {
  141. const float input = samples[i] * gain;
  142. float output = 0;
  143. const float damp = damping.getNextValue();
  144. const float feedbck = feedback.getNextValue();
  145. for (int j = 0; j < numCombs; ++j) // accumulate the comb filters in parallel
  146. output += comb[0][j].process (input, damp, feedbck);
  147. for (int j = 0; j < numAllPasses; ++j) // run the allpass filters in series
  148. output = allPass[0][j].process (output);
  149. const float dry = dryGain.getNextValue();
  150. const float wet1 = wetGain1.getNextValue();
  151. samples[i] = output * wet1 + samples[i] * dry;
  152. }
  153. }
  154. private:
  155. //==============================================================================
  156. 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. if (isFrozen (parameters.freezeMode))
  163. setDamping (0.0f, 1.0f);
  164. else
  165. setDamping (parameters.damping * dampScaleFactor,
  166. parameters.roomSize * roomScaleFactor + roomOffset);
  167. }
  168. void setDamping (const float dampingToUse, const float roomSizeToUse) noexcept
  169. {
  170. damping.setTargetValue (dampingToUse);
  171. feedback.setTargetValue (roomSizeToUse);
  172. }
  173. //==============================================================================
  174. class CombFilter
  175. {
  176. public:
  177. CombFilter() noexcept {}
  178. void setSize (const int size)
  179. {
  180. if (size != bufferSize)
  181. {
  182. bufferIndex = 0;
  183. buffer.malloc (size);
  184. bufferSize = size;
  185. }
  186. clear();
  187. }
  188. void clear() noexcept
  189. {
  190. last = 0;
  191. buffer.clear ((size_t) bufferSize);
  192. }
  193. float process (const float input, const float damp, const float feedbackLevel) noexcept
  194. {
  195. const float output = buffer[bufferIndex];
  196. last = (output * (1.0f - damp)) + (last * damp);
  197. JUCE_UNDENORMALISE (last);
  198. float temp = input + (last * feedbackLevel);
  199. JUCE_UNDENORMALISE (temp);
  200. buffer[bufferIndex] = temp;
  201. bufferIndex = (bufferIndex + 1) % bufferSize;
  202. return output;
  203. }
  204. private:
  205. HeapBlock<float> buffer;
  206. int bufferSize = 0, bufferIndex = 0;
  207. float last = 0.0f;
  208. JUCE_DECLARE_NON_COPYABLE (CombFilter)
  209. };
  210. //==============================================================================
  211. class AllPassFilter
  212. {
  213. public:
  214. AllPassFilter() noexcept {}
  215. void setSize (const int size)
  216. {
  217. if (size != bufferSize)
  218. {
  219. bufferIndex = 0;
  220. buffer.malloc (size);
  221. bufferSize = size;
  222. }
  223. clear();
  224. }
  225. void clear() noexcept
  226. {
  227. buffer.clear ((size_t) bufferSize);
  228. }
  229. float process (const float input) noexcept
  230. {
  231. const float bufferedValue = buffer [bufferIndex];
  232. float temp = input + (bufferedValue * 0.5f);
  233. JUCE_UNDENORMALISE (temp);
  234. buffer [bufferIndex] = temp;
  235. bufferIndex = (bufferIndex + 1) % bufferSize;
  236. return bufferedValue - input;
  237. }
  238. private:
  239. HeapBlock<float> buffer;
  240. int bufferSize = 0, bufferIndex = 0;
  241. JUCE_DECLARE_NON_COPYABLE (AllPassFilter)
  242. };
  243. //==============================================================================
  244. enum { numCombs = 8, numAllPasses = 4, numChannels = 2 };
  245. Parameters parameters;
  246. float gain;
  247. CombFilter comb [numChannels][numCombs];
  248. AllPassFilter allPass [numChannels][numAllPasses];
  249. SmoothedValue<float> damping, feedback, dryGain, wetGain1, wetGain2;
  250. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Reverb)
  251. };
  252. } // namespace juce