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.

310 lines
11KB

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