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.

122 lines
4.2KB

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