OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_Phaser.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>
31 Phaser<SampleType>::Phaser()
32{
33 auto oscFunction = [] (SampleType x) { return std::sin (x); };
34 osc.initialise (oscFunction);
35
36 for (auto n = 0; n < numStages; ++n)
37 {
38 filters.add (new FirstOrderTPTFilter<SampleType>());
39 filters[n]->setType (FirstOrderTPTFilterType::allpass);
40 }
41
42 dryWet.setMixingRule (DryWetMixingRule::linear);
43}
44
45 template <typename SampleType>
46 void Phaser<SampleType>::setRate (SampleType newRateHz)
47{
48 jassert (isPositiveAndBelow (newRateHz, static_cast<SampleType> (100.0)));
49
50 rate = newRateHz;
51 update();
52}
53
54 template <typename SampleType>
55 void Phaser<SampleType>::setDepth (SampleType newDepth)
56{
57 jassert (isPositiveAndNotGreaterThan (newDepth, static_cast<SampleType> (1.0)));
58
59 depth = newDepth;
60 update();
61}
62
63 template <typename SampleType>
64 void Phaser<SampleType>::setCentreFrequency (SampleType newCentreHz)
65{
66 jassert (isPositiveAndBelow (newCentreHz, static_cast<SampleType> (sampleRate * 0.5)));
67
68 centreFrequency = newCentreHz;
69 normCentreFrequency = mapFromLog10 (centreFrequency, static_cast<SampleType> (20.0), static_cast<SampleType> (jmin (20000.0, 0.49 * sampleRate)));
70}
71
72 template <typename SampleType>
73 void Phaser<SampleType>::setFeedback (SampleType newFeedback)
74{
75 jassert (newFeedback >= static_cast<SampleType> (-1.0) && newFeedback <= static_cast<SampleType> (1.0));
76
77 feedback = newFeedback;
78 update();
79}
80
81 template <typename SampleType>
82 void Phaser<SampleType>::setMix (SampleType newMix)
83{
84 jassert (isPositiveAndNotGreaterThan (newMix, static_cast<SampleType> (1.0)));
85
86 mix = newMix;
87 update();
88}
89
90//==============================================================================
91 template <typename SampleType>
92 void Phaser<SampleType>::prepare (const ProcessSpec& spec)
93{
94 jassert (spec.sampleRate > 0);
95 jassert (spec.numChannels > 0);
96
97 sampleRate = spec.sampleRate;
98
99 for (auto n = 0; n < numStages; ++n)
100 filters[n]->prepare (spec);
101
102 dryWet.prepare (spec);
103 feedbackVolume.resize (spec.numChannels);
104 lastOutput.resize (spec.numChannels);
105
106 auto specDown = spec;
107 specDown.sampleRate /= (double) maxUpdateCounter;
108 specDown.maximumBlockSize = specDown.maximumBlockSize / (uint32) maxUpdateCounter + 1;
109
110 osc.prepare (specDown);
111 bufferFrequency.setSize (1, (int) specDown.maximumBlockSize, false, false, true);
112
113 update();
114 reset();
115}
116
117 template <typename SampleType>
118 void Phaser<SampleType>::reset()
119{
120 std::fill (lastOutput.begin(), lastOutput.end(), static_cast<SampleType> (0));
121
122 for (auto n = 0; n < numStages; ++n)
123 filters[n]->reset();
124
125 osc.reset();
126 dryWet.reset();
127
128 oscVolume.reset (sampleRate / (double) maxUpdateCounter, 0.05);
129
130 for (auto& vol : feedbackVolume)
131 vol.reset (sampleRate, 0.05);
132
133 updateCounter = 0;
134}
135
136 template <typename SampleType>
137 void Phaser<SampleType>::update()
138{
139 osc.setFrequency (rate);
140 oscVolume.setTargetValue (depth * (SampleType) 0.5);
141 dryWet.setWetMixProportion (mix);
142
143 for (auto& vol : feedbackVolume)
144 vol.setTargetValue (feedback);
145}
146
147//==============================================================================
148 template class Phaser<float>;
149 template class Phaser<double>;
150
151} // namespace juce::dsp
juce::dsp::FirstOrderTPTFilter
Definition juce_FirstOrderTPTFilter.h:54
juce::dsp::Phaser
Definition juce_Phaser.h:41
juce::dsp::Phaser::prepare
void prepare(const ProcessSpec &spec)
Definition juce_Phaser.cpp:92
juce::dsp::Phaser::Phaser
Phaser()
Definition juce_Phaser.cpp:31
juce::dsp::Phaser::reset
void reset()
Definition juce_Phaser.cpp:118
juce::dsp::Phaser::setDepth
void setDepth(SampleType newDepth)
Definition juce_Phaser.cpp:55
juce::dsp::Phaser::setFeedback
void setFeedback(SampleType newFeedback)
Definition juce_Phaser.cpp:73
juce::dsp::Phaser::setMix
void setMix(SampleType newMix)
Definition juce_Phaser.cpp:82
juce::dsp::Phaser::setRate
void setRate(SampleType newRateHz)
Definition juce_Phaser.cpp:46
juce::dsp::Phaser::setCentreFrequency
void setCentreFrequency(SampleType newCentreHz)
Definition juce_Phaser.cpp:64
juce::dsp::ProcessSpec
Definition juce_ProcessContext.h:36
juce::dsp::ProcessSpec::numChannels
uint32 numChannels
Definition juce_ProcessContext.h:44
juce::dsp::ProcessSpec::sampleRate
double sampleRate
Definition juce_ProcessContext.h:38