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.

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