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.

170 lines
6.3KB

  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. A simple chorus DSP widget that modulates the delay of a delay line in order to
  24. create sweeping notches in the magnitude frequency response.
  25. This audio effect can be controlled via the speed and depth of the LFO controlling
  26. the frequency response, a mix control, a feedback control, and the centre delay
  27. of the modulation.
  28. Note: To get classic chorus sounds try to use a centre delay time around 7-8 ms
  29. with a low feedback volume and a low depth. This effect can also be used as a
  30. flanger with a lower centre delay time and a lot of feedback, and as a vibrato
  31. effect if the mix value is 1.
  32. @tags{DSP}
  33. */
  34. template <typename SampleType>
  35. class Chorus
  36. {
  37. public:
  38. //==============================================================================
  39. /** Constructor. */
  40. Chorus();
  41. //==============================================================================
  42. /** Sets the rate (in Hz) of the LFO modulating the chorus delay line. This rate
  43. must be lower than 100 Hz.
  44. */
  45. void setRate (SampleType newRateHz);
  46. /** Sets the volume of the LFO modulating the chorus delay line (between 0 and 1).
  47. */
  48. void setDepth (SampleType newDepth);
  49. /** Sets the centre delay in milliseconds of the chorus delay line modulation.
  50. This delay must be between 1 and 100 ms.
  51. */
  52. void setCentreDelay (SampleType newDelayMs);
  53. /** Sets the feedback volume (between -1 and 1) of the chorus delay line.
  54. Negative values can be used to get specific chorus sounds.
  55. */
  56. void setFeedback (SampleType newFeedback);
  57. /** Sets the amount of dry and wet signal in the output of the chorus (between 0
  58. for full dry and 1 for full wet).
  59. */
  60. void setMix (SampleType newMix);
  61. //==============================================================================
  62. /** Initialises the processor. */
  63. void prepare (const ProcessSpec& spec);
  64. /** Resets the internal state variables of the processor. */
  65. void reset();
  66. //==============================================================================
  67. /** Processes the input and output samples supplied in the processing context. */
  68. template <typename ProcessContext>
  69. void process (const ProcessContext& context) noexcept
  70. {
  71. const auto& inputBlock = context.getInputBlock();
  72. auto& outputBlock = context.getOutputBlock();
  73. const auto numChannels = outputBlock.getNumChannels();
  74. const auto numSamples = outputBlock.getNumSamples();
  75. jassert (inputBlock.getNumChannels() == numChannels);
  76. jassert (inputBlock.getNumChannels() == lastOutput.size());
  77. jassert (inputBlock.getNumSamples() == numSamples);
  78. if (context.isBypassed)
  79. {
  80. outputBlock.copyFrom (inputBlock);
  81. return;
  82. }
  83. auto delayValuesBlock = AudioBlock<SampleType>(bufferDelayTimes).getSubBlock (0, numSamples);
  84. auto contextDelay = ProcessContextReplacing<SampleType> (delayValuesBlock);
  85. delayValuesBlock.clear();
  86. osc.process (contextDelay);
  87. delayValuesBlock.multiplyBy (oscVolume);
  88. auto* delaySamples = bufferDelayTimes.getWritePointer (0);
  89. for (size_t i = 0; i < numSamples; ++i)
  90. {
  91. auto lfo = jmax (static_cast<SampleType> (1.0), maximumDelayModulation * delaySamples[i] + centreDelay);
  92. delaySamples[i] = static_cast<SampleType> (lfo * sampleRate / 1000.0);
  93. }
  94. dryWet.pushDrySamples (inputBlock);
  95. for (size_t channel = 0; channel < numChannels; ++channel)
  96. {
  97. auto* inputSamples = inputBlock .getChannelPointer (channel);
  98. auto* outputSamples = outputBlock.getChannelPointer (channel);
  99. for (size_t i = 0; i < numSamples; ++i)
  100. {
  101. auto input = inputSamples[i];
  102. auto output = input - lastOutput[channel];
  103. delay.pushSample ((int) channel, output);
  104. delay.setDelay (delaySamples[i]);
  105. output = delay.popSample ((int) channel);
  106. outputSamples[i] = output;
  107. lastOutput[channel] = output * feedbackVolume[channel].getNextValue();
  108. }
  109. }
  110. dryWet.mixWetSamples (outputBlock);
  111. }
  112. private:
  113. //==============================================================================
  114. void update();
  115. //==============================================================================
  116. Oscillator<SampleType> osc;
  117. DelayLine<SampleType, DelayLineInterpolationTypes::Linear> delay;
  118. SmoothedValue<SampleType, ValueSmoothingTypes::Linear> oscVolume;
  119. std::vector<SmoothedValue<SampleType, ValueSmoothingTypes::Linear>> feedbackVolume { 2 };
  120. DryWetMixer<SampleType> dryWet;
  121. std::vector<SampleType> lastOutput { 2 };
  122. AudioBuffer<SampleType> bufferDelayTimes;
  123. double sampleRate = 44100.0;
  124. SampleType rate = 1.0, depth = 0.25, feedback = 0.0, mix = 0.5,
  125. centreDelay = 7.0;
  126. static constexpr SampleType maxDepth = 1.0,
  127. maxCentreDelayMs = 100.0,
  128. oscVolumeMultiplier = 0.5,
  129. maximumDelayModulation = 20.0;
  130. };
  131. } // namespace dsp
  132. } // namespace juce