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.

158 lines
5.8KB

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