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.

133 lines
4.9KB

  1. #include "CloneableDelayLine.hpp"
  2. /*
  3. ==============================================================================
  4. This file is part of the JUCE library.
  5. Copyright (c) 2022 - Raw Material Software Limited
  6. JUCE is an open source library subject to commercial or open-source
  7. licensing.
  8. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  9. Agreement and JUCE Privacy Policy.
  10. End User License Agreement: www.juce.com/juce-7-licence
  11. Privacy Policy: www.juce.com/juce-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. //==============================================================================
  20. template <typename SampleType, typename InterpolationType>
  21. CloneableDelayLine<SampleType, InterpolationType>::CloneableDelayLine()
  22. : CloneableDelayLine (0)
  23. {
  24. }
  25. template <typename SampleType, typename InterpolationType>
  26. CloneableDelayLine<SampleType, InterpolationType>::CloneableDelayLine (int maximumDelayInSamples)
  27. {
  28. jassert (maximumDelayInSamples >= 0);
  29. sampleRate = 44100.0;
  30. setMaximumDelayInSamples (maximumDelayInSamples);
  31. }
  32. //==============================================================================
  33. template <typename SampleType, typename InterpolationType>
  34. void CloneableDelayLine<SampleType, InterpolationType>::setDelay (SampleType newDelayInSamples)
  35. {
  36. auto upperLimit = (SampleType) getMaximumDelayInSamples();
  37. jassert (juce::isPositiveAndNotGreaterThan (newDelayInSamples, upperLimit));
  38. delay = juce::jlimit ((SampleType) 0, upperLimit, newDelayInSamples);
  39. delayInt = static_cast<int> (std::floor (delay));
  40. delayFrac = delay - (SampleType) delayInt;
  41. updateInternalVariables();
  42. }
  43. template <typename SampleType, typename InterpolationType>
  44. SampleType CloneableDelayLine<SampleType, InterpolationType>::getDelay() const
  45. {
  46. return delay;
  47. }
  48. //==============================================================================
  49. template <typename SampleType, typename InterpolationType>
  50. void CloneableDelayLine<SampleType, InterpolationType>::prepare (const juce::dsp::ProcessSpec& spec)
  51. {
  52. jassert (spec.numChannels > 0);
  53. bufferData.setSize ((int) spec.numChannels, totalSize, false, false, true);
  54. writePos.resize (spec.numChannels);
  55. readPos.resize (spec.numChannels);
  56. v.resize (spec.numChannels);
  57. sampleRate = spec.sampleRate;
  58. reset();
  59. }
  60. template <typename SampleType, typename InterpolationType>
  61. void CloneableDelayLine<SampleType, InterpolationType>::setMaximumDelayInSamples (int maxDelayInSamples)
  62. {
  63. jassert (maxDelayInSamples >= 0);
  64. totalSize = juce::jmax (4, maxDelayInSamples + 2);
  65. bufferData.setSize ((int) bufferData.getNumChannels(), totalSize, false, false, true);
  66. reset();
  67. }
  68. template <typename SampleType, typename InterpolationType>
  69. void CloneableDelayLine<SampleType, InterpolationType>::reset()
  70. {
  71. for (auto vec : { &writePos, &readPos })
  72. std::fill (vec->begin(), vec->end(), 0);
  73. std::fill (v.begin(), v.end(), static_cast<SampleType> (0));
  74. bufferData.clear();
  75. }
  76. //==============================================================================
  77. template <typename SampleType, typename InterpolationType>
  78. void CloneableDelayLine<SampleType, InterpolationType>::pushSample (int channel, SampleType sample)
  79. {
  80. bufferData.setSample (channel, writePos[(size_t) channel], sample);
  81. writePos[(size_t) channel] = (writePos[(size_t) channel] + totalSize - 1) % totalSize;
  82. }
  83. template <typename SampleType, typename InterpolationType>
  84. SampleType CloneableDelayLine<SampleType, InterpolationType>::popSample (int channel, SampleType delayInSamples, bool updateReadPointer)
  85. {
  86. if (delayInSamples >= 0)
  87. setDelay (delayInSamples);
  88. auto result = interpolateSample (channel);
  89. if (updateReadPointer)
  90. readPos[(size_t) channel] = (readPos[(size_t) channel] + totalSize - 1) % totalSize;
  91. return result;
  92. }
  93. //==============================================================================
  94. template class CloneableDelayLine<float, juce::dsp::DelayLineInterpolationTypes::None>;
  95. template class CloneableDelayLine<double, juce::dsp::DelayLineInterpolationTypes::None>;
  96. template class CloneableDelayLine<float, juce::dsp::DelayLineInterpolationTypes::Linear>;
  97. template class CloneableDelayLine<double, juce::dsp::DelayLineInterpolationTypes::Linear>;
  98. template class CloneableDelayLine<float, juce::dsp::DelayLineInterpolationTypes::Lagrange3rd>;
  99. template class CloneableDelayLine<double, juce::dsp::DelayLineInterpolationTypes::Lagrange3rd>;
  100. template class CloneableDelayLine<float, juce::dsp::DelayLineInterpolationTypes::Thiran>;
  101. template class CloneableDelayLine<double, juce::dsp::DelayLineInterpolationTypes::Thiran>;