Audio plugin host https://kx.studio/carla
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.

juce_Chorus.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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
  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, maxDepth));
  41. depth = newDepth;
  42. update();
  43. }
  44. template <typename SampleType>
  45. void Chorus<SampleType>::setCentreDelay (SampleType newDelayMs)
  46. {
  47. jassert (isPositiveAndBelow (newDelayMs, maxCentreDelayMs));
  48. centreDelay = jlimit (static_cast<SampleType> (1.0), maxCentreDelayMs, 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. const auto maxPossibleDelay = std::ceil ((maximumDelayModulation * maxDepth * oscVolumeMultiplier + maxCentreDelayMs)
  72. * sampleRate / 1000.0);
  73. delay = DelayLine<SampleType, DelayLineInterpolationTypes::Linear>{ static_cast<int> (maxPossibleDelay) };
  74. delay.prepare (spec);
  75. dryWet.prepare (spec);
  76. feedbackVolume.resize (spec.numChannels);
  77. lastOutput.resize (spec.numChannels);
  78. osc.prepare (spec);
  79. bufferDelayTimes.setSize (1, (int) spec.maximumBlockSize, false, false, true);
  80. update();
  81. reset();
  82. }
  83. template <typename SampleType>
  84. void Chorus<SampleType>::reset()
  85. {
  86. std::fill (lastOutput.begin(), lastOutput.end(), static_cast<SampleType> (0));
  87. delay.reset();
  88. osc.reset();
  89. dryWet.reset();
  90. oscVolume.reset (sampleRate, 0.05);
  91. for (auto& vol : feedbackVolume)
  92. vol.reset (sampleRate, 0.05);
  93. }
  94. template <typename SampleType>
  95. void Chorus<SampleType>::update()
  96. {
  97. osc.setFrequency (rate);
  98. oscVolume.setTargetValue (depth * oscVolumeMultiplier);
  99. dryWet.setWetMixProportion (mix);
  100. for (auto& vol : feedbackVolume)
  101. vol.setTargetValue (feedback);
  102. }
  103. //==============================================================================
  104. template class Chorus<float>;
  105. template class Chorus<double>;
  106. } // namespace dsp
  107. } // namespace juce