OpenShot Audio Library | OpenShotAudio  0.6.0
juce_Chorus.cpp
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 
29 //==============================================================================
30 template <typename SampleType>
32 {
33  auto oscFunction = [] (SampleType x) { return std::sin (x); };
34  osc.initialise (oscFunction);
35 
36  dryWet.setMixingRule (DryWetMixingRule::linear);
37 }
38 
39 template <typename SampleType>
40 void Chorus<SampleType>::setRate (SampleType newRateHz)
41 {
42  jassert (isPositiveAndBelow (newRateHz, static_cast<SampleType> (100.0)));
43 
44  rate = newRateHz;
45  update();
46 }
47 
48 template <typename SampleType>
49 void Chorus<SampleType>::setDepth (SampleType newDepth)
50 {
51  jassert (isPositiveAndNotGreaterThan (newDepth, maxDepth));
52 
53  depth = newDepth;
54  update();
55 }
56 
57 template <typename SampleType>
58 void Chorus<SampleType>::setCentreDelay (SampleType newDelayMs)
59 {
60  jassert (isPositiveAndBelow (newDelayMs, maxCentreDelayMs));
61 
62  centreDelay = jlimit (static_cast<SampleType> (1.0), maxCentreDelayMs, newDelayMs);
63 }
64 
65 template <typename SampleType>
66 void Chorus<SampleType>::setFeedback (SampleType newFeedback)
67 {
68  jassert (newFeedback >= static_cast<SampleType> (-1.0) && newFeedback <= static_cast<SampleType> (1.0));
69 
70  feedback = newFeedback;
71  update();
72 }
73 
74 template <typename SampleType>
75 void Chorus<SampleType>::setMix (SampleType newMix)
76 {
77  jassert (isPositiveAndNotGreaterThan (newMix, static_cast<SampleType> (1.0)));
78 
79  mix = newMix;
80  update();
81 }
82 
83 //==============================================================================
84 template <typename SampleType>
86 {
87  jassert (spec.sampleRate > 0);
88  jassert (spec.numChannels > 0);
89 
90  sampleRate = spec.sampleRate;
91 
92  const auto maxPossibleDelay = std::ceil ((maximumDelayModulation * maxDepth * oscVolumeMultiplier + maxCentreDelayMs)
93  * sampleRate / 1000.0);
94  delay = DelayLine<SampleType, DelayLineInterpolationTypes::Linear>{ static_cast<int> (maxPossibleDelay) };
95  delay.prepare (spec);
96 
97  dryWet.prepare (spec);
98  feedbackVolume.resize (spec.numChannels);
99  lastOutput.resize (spec.numChannels);
100 
101  osc.prepare (spec);
102  bufferDelayTimes.setSize (1, (int) spec.maximumBlockSize, false, false, true);
103 
104  update();
105  reset();
106 }
107 
108 template <typename SampleType>
110 {
111  std::fill (lastOutput.begin(), lastOutput.end(), static_cast<SampleType> (0));
112 
113  delay.reset();
114  osc.reset();
115  dryWet.reset();
116 
117  oscVolume.reset (sampleRate, 0.05);
118 
119  for (auto& vol : feedbackVolume)
120  vol.reset (sampleRate, 0.05);
121 }
122 
123 template <typename SampleType>
125 {
126  osc.setFrequency (rate);
127  oscVolume.setTargetValue (depth * oscVolumeMultiplier);
128  dryWet.setWetMixProportion (mix);
129 
130  for (auto& vol : feedbackVolume)
131  vol.setTargetValue (feedback);
132 }
133 
134 //==============================================================================
135 template class Chorus<float>;
136 template class Chorus<double>;
137 
138 } // namespace juce::dsp
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 setRate(SampleType newRateHz)
Definition: juce_Chorus.cpp:40
void setCentreDelay(SampleType newDelayMs)
Definition: juce_Chorus.cpp:58
void prepare(const ProcessSpec &spec)