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.

340 lines
12KB

  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. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. namespace dsp
  21. {
  22. //==============================================================================
  23. /**
  24. A collection of structs to pass as the template argument when setting the
  25. interpolation type for the DelayLine class.
  26. */
  27. namespace DelayLineInterpolationTypes
  28. {
  29. /**
  30. No interpolation between successive samples in the delay line will be
  31. performed. This is useful when the delay is a constant integer or to
  32. create lo-fi audio effects.
  33. @tags{DSP}
  34. */
  35. struct None {};
  36. /**
  37. Successive samples in the delay line will be linearly interpolated. This
  38. type of interpolation has a low computational cost where the delay can be
  39. modulated in real time, but it also introduces a low-pass filtering effect
  40. into your audio signal.
  41. @tags{DSP}
  42. */
  43. struct Linear {};
  44. /**
  45. Successive samples in the delay line will be interpolated using a 3rd order
  46. Lagrange interpolator. This method incurs more computational overhead than
  47. linear interpolation but reduces the low-pass filtering effect whilst
  48. remaining amenable to real time delay modulation.
  49. @tags{DSP}
  50. */
  51. struct Lagrange3rd {};
  52. /**
  53. Successive samples in the delay line will be interpolated using 1st order
  54. Thiran interpolation. This method is very efficient, and features a flat
  55. amplitude frequency response in exchange for less accuracy in the phase
  56. response. This interpolation method is stateful so is unsuitable for
  57. applications requiring fast delay modulation.
  58. @tags{DSP}
  59. */
  60. struct Thiran {};
  61. }
  62. //==============================================================================
  63. /**
  64. A delay line processor featuring several algorithms for the fractional delay
  65. calculation, block processing, and sample-by-sample processing useful when
  66. modulating the delay in real time or creating a standard delay effect with
  67. feedback.
  68. Note: If you intend to change the delay in real time, you may want to smooth
  69. changes to the delay systematically using either a ramp or a low-pass filter.
  70. @see SmoothedValue, FirstOrderTPTFilter
  71. @tags{DSP}
  72. */
  73. template <typename SampleType, typename InterpolationType = DelayLineInterpolationTypes::Linear>
  74. class DelayLine
  75. {
  76. public:
  77. //==============================================================================
  78. /** Default constructor. */
  79. DelayLine();
  80. /** Constructor. */
  81. explicit DelayLine (int maximumDelayInSamples);
  82. //==============================================================================
  83. /** Sets the delay in samples. */
  84. void setDelay (SampleType newDelayInSamples);
  85. /** Returns the current delay in samples. */
  86. SampleType getDelay() const;
  87. //==============================================================================
  88. /** Initialises the processor. */
  89. void prepare (const ProcessSpec& spec);
  90. /** Sets a new maximum delay in samples.
  91. Also clears the delay line.
  92. This may allocate internally, so you should never call it from the audio thread.
  93. */
  94. void setMaximumDelayInSamples (int maxDelayInSamples);
  95. /** Gets the maximum possible delay in samples.
  96. For very short delay times, the result of getMaximumDelayInSamples() may
  97. differ from the last value passed to setMaximumDelayInSamples().
  98. */
  99. int getMaximumDelayInSamples() const noexcept { return totalSize - 1; }
  100. /** Resets the internal state variables of the processor. */
  101. void reset();
  102. //==============================================================================
  103. /** Pushes a single sample into one channel of the delay line.
  104. Use this function and popSample instead of process if you need to modulate
  105. the delay in real time instead of using a fixed delay value, or if you want
  106. to code a delay effect with a feedback loop.
  107. @see setDelay, popSample, process
  108. */
  109. void pushSample (int channel, SampleType sample);
  110. /** Pops a single sample from one channel of the delay line.
  111. Use this function to modulate the delay in real time or implement standard
  112. delay effects with feedback.
  113. @param channel the target channel for the delay line.
  114. @param delayInSamples sets the wanted fractional delay in samples, or -1
  115. to use the value being used before or set with
  116. setDelay function.
  117. @param updateReadPointer should be set to true if you use the function
  118. once for each sample, or false if you need
  119. multi-tap delay capabilities.
  120. @see setDelay, pushSample, process
  121. */
  122. SampleType popSample (int channel, SampleType delayInSamples = -1, bool updateReadPointer = true);
  123. //==============================================================================
  124. /** Processes the input and output samples supplied in the processing context.
  125. Can be used for block processing when the delay is not going to change
  126. during processing. The delay must first be set by calling setDelay.
  127. @see setDelay
  128. */
  129. template <typename ProcessContext>
  130. void process (const ProcessContext& context) noexcept
  131. {
  132. const auto& inputBlock = context.getInputBlock();
  133. auto& outputBlock = context.getOutputBlock();
  134. const auto numChannels = outputBlock.getNumChannels();
  135. const auto numSamples = outputBlock.getNumSamples();
  136. jassert (inputBlock.getNumChannels() == numChannels);
  137. jassert (inputBlock.getNumChannels() == writePos.size());
  138. jassert (inputBlock.getNumSamples() == numSamples);
  139. if (context.isBypassed)
  140. {
  141. outputBlock.copyFrom (inputBlock);
  142. return;
  143. }
  144. for (size_t channel = 0; channel < numChannels; ++channel)
  145. {
  146. auto* inputSamples = inputBlock.getChannelPointer (channel);
  147. auto* outputSamples = outputBlock.getChannelPointer (channel);
  148. for (size_t i = 0; i < numSamples; ++i)
  149. {
  150. pushSample ((int) channel, inputSamples[i]);
  151. outputSamples[i] = popSample ((int) channel);
  152. }
  153. }
  154. }
  155. private:
  156. //==============================================================================
  157. template <typename T = InterpolationType>
  158. typename std::enable_if <std::is_same <T, DelayLineInterpolationTypes::None>::value, SampleType>::type
  159. interpolateSample (int channel) const
  160. {
  161. auto index = (readPos[(size_t) channel] + delayInt) % totalSize;
  162. return bufferData.getSample (channel, index);
  163. }
  164. template <typename T = InterpolationType>
  165. typename std::enable_if <std::is_same <T, DelayLineInterpolationTypes::Linear>::value, SampleType>::type
  166. interpolateSample (int channel) const
  167. {
  168. auto index1 = readPos[(size_t) channel] + delayInt;
  169. auto index2 = index1 + 1;
  170. if (index2 >= totalSize)
  171. {
  172. index1 %= totalSize;
  173. index2 %= totalSize;
  174. }
  175. auto value1 = bufferData.getSample (channel, index1);
  176. auto value2 = bufferData.getSample (channel, index2);
  177. return value1 + delayFrac * (value2 - value1);
  178. }
  179. template <typename T = InterpolationType>
  180. typename std::enable_if <std::is_same <T, DelayLineInterpolationTypes::Lagrange3rd>::value, SampleType>::type
  181. interpolateSample (int channel) const
  182. {
  183. auto index1 = readPos[(size_t) channel] + delayInt;
  184. auto index2 = index1 + 1;
  185. auto index3 = index2 + 1;
  186. auto index4 = index3 + 1;
  187. if (index4 >= totalSize)
  188. {
  189. index1 %= totalSize;
  190. index2 %= totalSize;
  191. index3 %= totalSize;
  192. index4 %= totalSize;
  193. }
  194. auto* samples = bufferData.getReadPointer (channel);
  195. auto value1 = samples[index1];
  196. auto value2 = samples[index2];
  197. auto value3 = samples[index3];
  198. auto value4 = samples[index4];
  199. auto d1 = delayFrac - 1.f;
  200. auto d2 = delayFrac - 2.f;
  201. auto d3 = delayFrac - 3.f;
  202. auto c1 = -d1 * d2 * d3 / 6.f;
  203. auto c2 = d2 * d3 * 0.5f;
  204. auto c3 = -d1 * d3 * 0.5f;
  205. auto c4 = d1 * d2 / 6.f;
  206. return value1 * c1 + delayFrac * (value2 * c2 + value3 * c3 + value4 * c4);
  207. }
  208. template <typename T = InterpolationType>
  209. typename std::enable_if <std::is_same <T, DelayLineInterpolationTypes::Thiran>::value, SampleType>::type
  210. interpolateSample (int channel)
  211. {
  212. auto index1 = readPos[(size_t) channel] + delayInt;
  213. auto index2 = index1 + 1;
  214. if (index2 >= totalSize)
  215. {
  216. index1 %= totalSize;
  217. index2 %= totalSize;
  218. }
  219. auto value1 = bufferData.getSample (channel, index1);
  220. auto value2 = bufferData.getSample (channel, index2);
  221. auto output = delayFrac == 0 ? value1 : value2 + alpha * (value1 - v[(size_t) channel]);
  222. v[(size_t) channel] = output;
  223. return output;
  224. }
  225. //==============================================================================
  226. template <typename T = InterpolationType>
  227. typename std::enable_if <std::is_same <T, DelayLineInterpolationTypes::None>::value, void>::type
  228. updateInternalVariables()
  229. {
  230. }
  231. template <typename T = InterpolationType>
  232. typename std::enable_if <std::is_same <T, DelayLineInterpolationTypes::Linear>::value, void>::type
  233. updateInternalVariables()
  234. {
  235. }
  236. template <typename T = InterpolationType>
  237. typename std::enable_if <std::is_same <T, DelayLineInterpolationTypes::Lagrange3rd>::value, void>::type
  238. updateInternalVariables()
  239. {
  240. if (delayInt >= 1)
  241. {
  242. delayFrac++;
  243. delayInt--;
  244. }
  245. }
  246. template <typename T = InterpolationType>
  247. typename std::enable_if <std::is_same <T, DelayLineInterpolationTypes::Thiran>::value, void>::type
  248. updateInternalVariables()
  249. {
  250. if (delayFrac < (SampleType) 0.618 && delayInt >= 1)
  251. {
  252. delayFrac++;
  253. delayInt--;
  254. }
  255. alpha = (1 - delayFrac) / (1 + delayFrac);
  256. }
  257. //==============================================================================
  258. double sampleRate;
  259. //==============================================================================
  260. AudioBuffer<SampleType> bufferData;
  261. std::vector<SampleType> v;
  262. std::vector<int> writePos, readPos;
  263. SampleType delay = 0.0, delayFrac = 0.0;
  264. int delayInt = 0, totalSize = 4;
  265. SampleType alpha = 0.0;
  266. };
  267. } // namespace dsp
  268. } // namespace juce