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.

333 lines
12KB

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