OpenShot Audio Library | OpenShotAudio  0.6.0
juce_Chorus.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11  Agreement and JUCE Privacy Policy.
12 
13  End User License Agreement: www.juce.com/juce-7-licence
14  Privacy Policy: www.juce.com/juce-privacy-policy
15 
16  Or: You may also use this code under the terms of the GPL v3 (see
17  www.gnu.org/licenses).
18 
19  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21  DISCLAIMED.
22 
23  ==============================================================================
24 */
25 
26 namespace juce::dsp
27 {
28 
44 template <typename SampleType>
45 class Chorus
46 {
47 public:
48  //==============================================================================
50  Chorus();
51 
52  //==============================================================================
56  void setRate (SampleType newRateHz);
57 
60  void setDepth (SampleType newDepth);
61 
65  void setCentreDelay (SampleType newDelayMs);
66 
70  void setFeedback (SampleType newFeedback);
71 
75  void setMix (SampleType newMix);
76 
77  //==============================================================================
79  void prepare (const ProcessSpec& spec);
80 
82  void reset();
83 
84  //==============================================================================
86  template <typename ProcessContext>
87  void process (const ProcessContext& context) noexcept
88  {
89  const auto& inputBlock = context.getInputBlock();
90  auto& outputBlock = context.getOutputBlock();
91  const auto numChannels = outputBlock.getNumChannels();
92  const auto numSamples = outputBlock.getNumSamples();
93 
94  jassert (inputBlock.getNumChannels() == numChannels);
95  jassert (inputBlock.getNumChannels() == lastOutput.size());
96  jassert (inputBlock.getNumSamples() == numSamples);
97 
98  if (context.isBypassed)
99  {
100  outputBlock.copyFrom (inputBlock);
101  return;
102  }
103 
104  auto delayValuesBlock = AudioBlock<SampleType> (bufferDelayTimes).getSubBlock (0, numSamples);
105  auto contextDelay = ProcessContextReplacing<SampleType> (delayValuesBlock);
106  delayValuesBlock.clear();
107 
108  osc.process (contextDelay);
109  delayValuesBlock.multiplyBy (oscVolume);
110 
111  auto* delaySamples = bufferDelayTimes.getWritePointer (0);
112 
113  for (size_t i = 0; i < numSamples; ++i)
114  {
115  auto lfo = jmax (static_cast<SampleType> (1.0), maximumDelayModulation * delaySamples[i] + centreDelay);
116  delaySamples[i] = static_cast<SampleType> (lfo * sampleRate / 1000.0);
117  }
118 
119  dryWet.pushDrySamples (inputBlock);
120 
121  for (size_t channel = 0; channel < numChannels; ++channel)
122  {
123  auto* inputSamples = inputBlock .getChannelPointer (channel);
124  auto* outputSamples = outputBlock.getChannelPointer (channel);
125 
126  for (size_t i = 0; i < numSamples; ++i)
127  {
128  auto input = inputSamples[i];
129  auto output = input - lastOutput[channel];
130 
131  delay.pushSample ((int) channel, output);
132  delay.setDelay (delaySamples[i]);
133  output = delay.popSample ((int) channel);
134 
135  outputSamples[i] = output;
136  lastOutput[channel] = output * feedbackVolume[channel].getNextValue();
137  }
138  }
139 
140  dryWet.mixWetSamples (outputBlock);
141  }
142 
143 private:
144  //==============================================================================
145  void update();
146 
147  //==============================================================================
151  std::vector<SmoothedValue<SampleType, ValueSmoothingTypes::Linear>> feedbackVolume { 2 };
152  DryWetMixer<SampleType> dryWet;
153  std::vector<SampleType> lastOutput { 2 };
154  AudioBuffer<SampleType> bufferDelayTimes;
155 
156  double sampleRate = 44100.0;
157  SampleType rate = 1.0, depth = 0.25, feedback = 0.0, mix = 0.5,
158  centreDelay = 7.0;
159 
160  static constexpr SampleType maxDepth = 1.0,
161  maxCentreDelayMs = 100.0,
162  oscVolumeMultiplier = 0.5,
163  maximumDelayModulation = 20.0;
164 };
165 
166 } // namespace juce::dsp
Type * getWritePointer(int channelNumber) noexcept
AudioBlock getSubBlock(size_t newOffset, size_t newLength) const noexcept
void setFeedback(SampleType newFeedback)
Definition: juce_Chorus.cpp:66
void setDepth(SampleType newDepth)
Definition: juce_Chorus.cpp:49
void prepare(const ProcessSpec &spec)
Definition: juce_Chorus.cpp:85
void setMix(SampleType newMix)
Definition: juce_Chorus.cpp:75
void process(const ProcessContext &context) noexcept
Definition: juce_Chorus.h:87
void setRate(SampleType newRateHz)
Definition: juce_Chorus.cpp:40
void setCentreDelay(SampleType newDelayMs)
Definition: juce_Chorus.cpp:58