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
3.8KB

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