The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

315 lines
11KB

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