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.

136 lines
4.8KB

  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. template <typename SampleType, typename InterpolationType>
  22. DelayLine<SampleType, InterpolationType>::DelayLine()
  23. : DelayLine (0)
  24. {
  25. }
  26. template <typename SampleType, typename InterpolationType>
  27. DelayLine<SampleType, InterpolationType>::DelayLine (int maximumDelayInSamples)
  28. {
  29. jassert (maximumDelayInSamples >= 0);
  30. sampleRate = 44100.0;
  31. setMaximumDelayInSamples (maximumDelayInSamples);
  32. }
  33. //==============================================================================
  34. template <typename SampleType, typename InterpolationType>
  35. void DelayLine<SampleType, InterpolationType>::setDelay (SampleType newDelayInSamples)
  36. {
  37. auto upperLimit = (SampleType) getMaximumDelayInSamples();
  38. jassert (isPositiveAndNotGreaterThan (newDelayInSamples, upperLimit));
  39. delay = jlimit ((SampleType) 0, upperLimit, newDelayInSamples);
  40. delayInt = static_cast<int> (std::floor (delay));
  41. delayFrac = delay - (SampleType) delayInt;
  42. updateInternalVariables();
  43. }
  44. template <typename SampleType, typename InterpolationType>
  45. SampleType DelayLine<SampleType, InterpolationType>::getDelay() const
  46. {
  47. return delay;
  48. }
  49. //==============================================================================
  50. template <typename SampleType, typename InterpolationType>
  51. void DelayLine<SampleType, InterpolationType>::prepare (const ProcessSpec& spec)
  52. {
  53. jassert (spec.numChannels > 0);
  54. bufferData.setSize ((int) spec.numChannels, totalSize, false, false, true);
  55. writePos.resize (spec.numChannels);
  56. readPos.resize (spec.numChannels);
  57. v.resize (spec.numChannels);
  58. sampleRate = spec.sampleRate;
  59. reset();
  60. }
  61. template <typename SampleType, typename InterpolationType>
  62. void DelayLine<SampleType, InterpolationType>::setMaximumDelayInSamples (int maxDelayInSamples)
  63. {
  64. jassert (maxDelayInSamples >= 0);
  65. totalSize = jmax (4, maxDelayInSamples + 2);
  66. bufferData.setSize ((int) bufferData.getNumChannels(), totalSize, false, false, true);
  67. reset();
  68. }
  69. template <typename SampleType, typename InterpolationType>
  70. void DelayLine<SampleType, InterpolationType>::reset()
  71. {
  72. for (auto vec : { &writePos, &readPos })
  73. std::fill (vec->begin(), vec->end(), 0);
  74. std::fill (v.begin(), v.end(), static_cast<SampleType> (0));
  75. bufferData.clear();
  76. }
  77. //==============================================================================
  78. template <typename SampleType, typename InterpolationType>
  79. void DelayLine<SampleType, InterpolationType>::pushSample (int channel, SampleType sample)
  80. {
  81. bufferData.setSample (channel, writePos[(size_t) channel], sample);
  82. writePos[(size_t) channel] = (writePos[(size_t) channel] + totalSize - 1) % totalSize;
  83. }
  84. template <typename SampleType, typename InterpolationType>
  85. SampleType DelayLine<SampleType, InterpolationType>::popSample (int channel, SampleType delayInSamples, bool updateReadPointer)
  86. {
  87. if (delayInSamples >= 0)
  88. setDelay (delayInSamples);
  89. auto result = interpolateSample (channel);
  90. if (updateReadPointer)
  91. readPos[(size_t) channel] = (readPos[(size_t) channel] + totalSize - 1) % totalSize;
  92. return result;
  93. }
  94. //==============================================================================
  95. template class DelayLine<float, DelayLineInterpolationTypes::None>;
  96. template class DelayLine<double, DelayLineInterpolationTypes::None>;
  97. template class DelayLine<float, DelayLineInterpolationTypes::Linear>;
  98. template class DelayLine<double, DelayLineInterpolationTypes::Linear>;
  99. template class DelayLine<float, DelayLineInterpolationTypes::Lagrange3rd>;
  100. template class DelayLine<double, DelayLineInterpolationTypes::Lagrange3rd>;
  101. template class DelayLine<float, DelayLineInterpolationTypes::Thiran>;
  102. template class DelayLine<double, DelayLineInterpolationTypes::Thiran>;
  103. } // namespace juce::dsp