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.

129 lines
4.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. totalSize = jmax (4, maximumDelayInSamples + 1);
  33. sampleRate = 44100.0;
  34. }
  35. //==============================================================================
  36. template <typename SampleType, typename InterpolationType>
  37. void DelayLine<SampleType, InterpolationType>::setDelay (SampleType newDelayInSamples)
  38. {
  39. auto upperLimit = (SampleType) (totalSize - 1);
  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>::reset()
  65. {
  66. for (auto vec : { &writePos, &readPos })
  67. std::fill (vec->begin(), vec->end(), 0);
  68. std::fill (v.begin(), v.end(), static_cast<SampleType> (0));
  69. bufferData.clear();
  70. }
  71. //==============================================================================
  72. template <typename SampleType, typename InterpolationType>
  73. void DelayLine<SampleType, InterpolationType>::pushSample (int channel, SampleType sample)
  74. {
  75. bufferData.setSample (channel, writePos[(size_t) channel], sample);
  76. writePos[(size_t) channel] = (writePos[(size_t) channel] + totalSize - 1) % totalSize;
  77. }
  78. template <typename SampleType, typename InterpolationType>
  79. SampleType DelayLine<SampleType, InterpolationType>::popSample (int channel, SampleType delayInSamples, bool updateReadPointer)
  80. {
  81. if (delayInSamples >= 0)
  82. setDelay(delayInSamples);
  83. auto result = interpolateSample (channel);
  84. if (updateReadPointer)
  85. readPos[(size_t) channel] = (readPos[(size_t) channel] + totalSize - 1) % totalSize;
  86. return result;
  87. }
  88. //==============================================================================
  89. template class DelayLine<float, DelayLineInterpolationTypes::None>;
  90. template class DelayLine<double, DelayLineInterpolationTypes::None>;
  91. template class DelayLine<float, DelayLineInterpolationTypes::Linear>;
  92. template class DelayLine<double, DelayLineInterpolationTypes::Linear>;
  93. template class DelayLine<float, DelayLineInterpolationTypes::Lagrange3rd>;
  94. template class DelayLine<double, DelayLineInterpolationTypes::Lagrange3rd>;
  95. template class DelayLine<float, DelayLineInterpolationTypes::Thiran>;
  96. template class DelayLine<double, DelayLineInterpolationTypes::Thiran>;
  97. } // namespace dsp
  98. } // namespace juce