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.

139 lines
4.0KB

  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>
  22. Chorus<SampleType>::Chorus()
  23. {
  24. auto oscFunction = [] (SampleType x) { return std::sin (x); };
  25. osc.initialise (oscFunction);
  26. dryWet.setMixingRule (DryWetMixingRule::linear);
  27. }
  28. template <typename SampleType>
  29. void Chorus<SampleType>::setRate (SampleType newRateHz)
  30. {
  31. jassert (isPositiveAndBelow (newRateHz, static_cast<SampleType> (100.0)));
  32. rate = newRateHz;
  33. update();
  34. }
  35. template <typename SampleType>
  36. void Chorus<SampleType>::setDepth (SampleType newDepth)
  37. {
  38. jassert (isPositiveAndNotGreaterThan (newDepth, maxDepth));
  39. depth = newDepth;
  40. update();
  41. }
  42. template <typename SampleType>
  43. void Chorus<SampleType>::setCentreDelay (SampleType newDelayMs)
  44. {
  45. jassert (isPositiveAndBelow (newDelayMs, maxCentreDelayMs));
  46. centreDelay = jlimit (static_cast<SampleType> (1.0), maxCentreDelayMs, newDelayMs);
  47. }
  48. template <typename SampleType>
  49. void Chorus<SampleType>::setFeedback (SampleType newFeedback)
  50. {
  51. jassert (newFeedback >= static_cast<SampleType> (-1.0) && newFeedback <= static_cast<SampleType> (1.0));
  52. feedback = newFeedback;
  53. update();
  54. }
  55. template <typename SampleType>
  56. void Chorus<SampleType>::setMix (SampleType newMix)
  57. {
  58. jassert (isPositiveAndNotGreaterThan (newMix, static_cast<SampleType> (1.0)));
  59. mix = newMix;
  60. update();
  61. }
  62. //==============================================================================
  63. template <typename SampleType>
  64. void Chorus<SampleType>::prepare (const ProcessSpec& spec)
  65. {
  66. jassert (spec.sampleRate > 0);
  67. jassert (spec.numChannels > 0);
  68. sampleRate = spec.sampleRate;
  69. const auto maxPossibleDelay = std::ceil ((maximumDelayModulation * maxDepth * oscVolumeMultiplier + maxCentreDelayMs)
  70. * sampleRate / 1000.0);
  71. delay = DelayLine<SampleType, DelayLineInterpolationTypes::Linear>{ static_cast<int> (maxPossibleDelay) };
  72. delay.prepare (spec);
  73. dryWet.prepare (spec);
  74. feedbackVolume.resize (spec.numChannels);
  75. lastOutput.resize (spec.numChannels);
  76. osc.prepare (spec);
  77. bufferDelayTimes.setSize (1, (int) spec.maximumBlockSize, false, false, true);
  78. update();
  79. reset();
  80. }
  81. template <typename SampleType>
  82. void Chorus<SampleType>::reset()
  83. {
  84. std::fill (lastOutput.begin(), lastOutput.end(), static_cast<SampleType> (0));
  85. delay.reset();
  86. osc.reset();
  87. dryWet.reset();
  88. oscVolume.reset (sampleRate, 0.05);
  89. for (auto& vol : feedbackVolume)
  90. vol.reset (sampleRate, 0.05);
  91. }
  92. template <typename SampleType>
  93. void Chorus<SampleType>::update()
  94. {
  95. osc.setFrequency (rate);
  96. oscVolume.setTargetValue (depth * oscVolumeMultiplier);
  97. dryWet.setWetMixProportion (mix);
  98. for (auto& vol : feedbackVolume)
  99. vol.setTargetValue (feedback);
  100. }
  101. //==============================================================================
  102. template class Chorus<float>;
  103. template class Chorus<double>;
  104. } // namespace juce::dsp