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.

135 lines
3.7KB

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