LMMS
Loading...
Searching...
No Matches
juce_Phaser.h
Go to the documentation of this file.
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
26namespace juce
27{
28namespace dsp
29{
30
41template <typename SampleType>
42class Phaser
43{
44public:
45 //==============================================================================
47 Phaser();
48
49 //==============================================================================
53 void setRate (SampleType newRateHz);
54
58 void setDepth (SampleType newDepth);
59
62 void setCentreFrequency (SampleType newCentreHz);
63
67 void setFeedback (SampleType newFeedback);
68
72 void setMix (SampleType newMix);
73
74 //==============================================================================
76 void prepare (const ProcessSpec& spec);
77
79 void reset();
80
81 //==============================================================================
83 template <typename ProcessContext>
84 void process (const ProcessContext& context) noexcept
85 {
86 const auto& inputBlock = context.getInputBlock();
87 auto& outputBlock = context.getOutputBlock();
88 const auto numChannels = outputBlock.getNumChannels();
89 const auto numSamples = outputBlock.getNumSamples();
90
91 jassert (inputBlock.getNumChannels() == numChannels);
92 jassert (inputBlock.getNumChannels() == lastOutput.size());
93 jassert (inputBlock.getNumSamples() == numSamples);
94
95 if (context.isBypassed)
96 {
97 outputBlock.copyFrom (inputBlock);
98 return;
99 }
100
101 int numSamplesDown = 0;
102 auto counter = updateCounter;
103
104 for (size_t i = 0; i < numSamples; ++i)
105 {
106 if (counter == 0)
107 numSamplesDown++;
108
109 counter++;
110
111 if (counter == maxUpdateCounter)
112 counter = 0;
113 }
114
115 if (numSamplesDown > 0)
116 {
117 auto freqBlock = AudioBlock<SampleType>(bufferFrequency).getSubBlock (0, (size_t) numSamplesDown);
118 auto contextFreq = ProcessContextReplacing<SampleType> (freqBlock);
119 freqBlock.clear();
120
121 osc.process (contextFreq);
122 freqBlock.multiplyBy (oscVolume);
123 }
124
125 auto* freqSamples = bufferFrequency.getWritePointer (0);
126
127 for (int i = 0; i < numSamplesDown; ++i)
128 {
129 auto lfo = jlimit (static_cast<SampleType> (0.0),
130 static_cast<SampleType> (1.0),
131 freqSamples[i] + normCentreFrequency);
132
133 freqSamples[i] = mapToLog10 (lfo, static_cast<SampleType> (20.0),
134 static_cast<SampleType> (jmin (20000.0, 0.49 * sampleRate)));
135 }
136
137 auto currentFrequency = filters[0]->getCutoffFrequency();
138 dryWet.pushDrySamples (inputBlock);
139
140 for (size_t channel = 0; channel < numChannels; ++channel)
141 {
142 counter = updateCounter;
143 int k = 0;
144
145 auto* inputSamples = inputBlock .getChannelPointer (channel);
146 auto* outputSamples = outputBlock.getChannelPointer (channel);
147
148 for (size_t i = 0; i < numSamples; ++i)
149 {
150 auto input = inputSamples[i];
151 auto output = input - lastOutput[channel];
152
153 if (i == 0 && counter != 0)
154 for (int n = 0; n < numStages; ++n)
155 filters[n]->setCutoffFrequency (currentFrequency);
156
157 if (counter == 0)
158 {
159 for (int n = 0; n < numStages; ++n)
160 filters[n]->setCutoffFrequency (freqSamples[k]);
161
162 k++;
163 }
164
165 for (int n = 0; n < numStages; ++n)
166 output = filters[n]->processSample ((int) channel, output);
167
168 outputSamples[i] = output;
169 lastOutput[channel] = output * feedbackVolume[channel].getNextValue();
170
171 counter++;
172
173 if (counter == maxUpdateCounter)
174 counter = 0;
175 }
176 }
177
178 dryWet.mixWetSamples (outputBlock);
179 updateCounter = (updateCounter + (int) numSamples) % maxUpdateCounter;
180 }
181
182private:
183 //==============================================================================
184 void update();
185
186 //==============================================================================
190 std::vector<SmoothedValue<SampleType, ValueSmoothingTypes::Linear>> feedbackVolume { 2 };
192 std::vector<SampleType> lastOutput { 2 };
194 SampleType normCentreFrequency = 0.5;
195 double sampleRate = 44100.0;
196
198 static constexpr int maxUpdateCounter = 4;
199
200 SampleType rate = 1.0, depth = 0.5, feedback = 0.0, mix = 0.5;
201 SampleType centreFrequency = 1300.0;
202 static constexpr int numStages = 6;
203};
204
205} // namespace dsp
206} // namespace juce
Definition juce_AudioSampleBuffer.h:34
Definition juce_OwnedArray.h:51
Definition juce_SmoothedValue.h:227
Definition juce_AudioBlock.h:70
AudioBlock getSubBlock(size_t newOffset, size_t newLength) const noexcept
Definition juce_AudioBlock.h:371
Definition juce_DryWetMixer.h:54
Definition juce_Oscillator.h:38
void reset()
Definition juce_Phaser.cpp:120
DryWetMixer< SampleType > dryWet
Definition juce_Phaser.h:191
static constexpr int numStages
Definition juce_Phaser.h:202
void prepare(const ProcessSpec &spec)
Definition juce_Phaser.cpp:94
static constexpr int maxUpdateCounter
Definition juce_Phaser.h:198
std::vector< SampleType > lastOutput
Definition juce_Phaser.h:192
double sampleRate
Definition juce_Phaser.h:195
AudioBuffer< SampleType > bufferFrequency
Definition juce_Phaser.h:193
SampleType depth
Definition juce_Phaser.h:200
SmoothedValue< SampleType, ValueSmoothingTypes::Linear > oscVolume
Definition juce_Phaser.h:189
SampleType feedback
Definition juce_Phaser.h:200
void process(const ProcessContext &context) noexcept
Definition juce_Phaser.h:84
void setCentreFrequency(SampleType newCentreHz)
Definition juce_Phaser.cpp:66
SampleType mix
Definition juce_Phaser.h:200
Oscillator< SampleType > osc
Definition juce_Phaser.h:187
void setFeedback(SampleType newFeedback)
Definition juce_Phaser.cpp:75
std::vector< SmoothedValue< SampleType, ValueSmoothingTypes::Linear > > feedbackVolume
Definition juce_Phaser.h:190
Phaser()
Definition juce_Phaser.cpp:33
void setRate(SampleType newRateHz)
Definition juce_Phaser.cpp:48
SampleType centreFrequency
Definition juce_Phaser.h:201
int updateCounter
Definition juce_Phaser.h:197
void setMix(SampleType newMix)
Definition juce_Phaser.cpp:84
void setDepth(SampleType newDepth)
Definition juce_Phaser.cpp:57
SampleType rate
Definition juce_Phaser.h:200
OwnedArray< FirstOrderTPTFilter< SampleType > > filters
Definition juce_Phaser.h:188
SampleType normCentreFrequency
Definition juce_Phaser.h:194
register unsigned k
Definition inflate.c:946
register unsigned i
Definition inflate.c:1575
#define jassert(expression)
Definition juce_AudioBlock.h:29
Definition carla_juce.cpp:31
Type mapToLog10(Type value0To1, Type logRangeMin, Type logRangeMax)
Definition juce_MathsFunctions.h:144
constexpr Type jmin(Type a, Type b)
Definition juce_MathsFunctions.h:106
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Definition juce_MathsFunctions.h:262
Definition juce_ProcessContext.h:88
Definition juce_ProcessContext.h:38
int n
Definition crypt.c:458
typedef int(UZ_EXP MsgFn)()