LMMS
Loading...
Searching...
No Matches
juce_StateVariableFilter.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
35{
36 template <typename NumericType>
37 struct Parameters;
38
59 template <typename SampleType>
60 class Filter
61 {
62 public:
63 //==============================================================================
68
71
72 //==============================================================================
73 #ifndef DOXYGEN
75 [[deprecated ("The classes in the StateVariableFilter namespace are deprecated. you should "
76 "use the equivalent functionality in the StateVariableTPTFilter class.")]]
78
80 [[deprecated ("The classes in the StateVariableFilter namespace are deprecated. you should "
81 "use the equivalent functionality in the StateVariableTPTFilter class.")]]
82 Filter (ParametersPtr parametersToUse) : parameters (std::move (parametersToUse)) { reset(); }
83 #endif
84
86 Filter (const Filter&) = default;
87
89 Filter (Filter&&) = default;
90
91 //==============================================================================
93 void prepare (const ProcessSpec&) noexcept { reset(); }
94
96 void reset() noexcept { s1 = s2 = SampleType {0}; }
97
103
104 //==============================================================================
108
109 //==============================================================================
110 template <typename ProcessContext>
111 void process (const ProcessContext& context) noexcept
112 {
113 static_assert (std::is_same<typename ProcessContext::SampleType, SampleType>::value,
114 "The sample-type of the filter must match the sample-type supplied to this process callback");
115
116 if (context.isBypassed)
118 else
120 }
121
136
137 private:
138 //==============================================================================
139 template <bool isBypassed, typename Parameters<NumericType>::Type type>
140 SampleType JUCE_VECTOR_CALLTYPE processLoop (SampleType sample, Parameters<NumericType>& state) noexcept
141 {
142 y[2] = (sample - s1 * state.R2 - s1 * state.g - s2) * state.h;
143
144 y[1] = y[2] * state.g + s1;
145 s1 = y[2] * state.g + y[1];
146
147 y[0] = y[1] * state.g + s2;
148 s2 = y[1] * state.g + y[0];
149
150 return isBypassed ? sample : y[static_cast<size_t> (type)];
151 }
152
153 template <bool isBypassed, typename Parameters<NumericType>::Type type>
154 void processBlock (const SampleType* input, SampleType* output, size_t n) noexcept
155 {
156 auto state = *parameters;
157
158 for (size_t i = 0 ; i < n; ++i)
159 output[i] = processLoop<isBypassed, type> (input[i], state);
160
161 #if JUCE_DSP_ENABLE_SNAP_TO_ZERO
162 snapToZero();
163 #endif
164
165 *parameters = state;
166 }
167
168 template <bool isBypassed, typename ProcessContext>
169 void processInternal (const ProcessContext& context) noexcept
170 {
171 auto&& inputBlock = context.getInputBlock();
172 auto&& outputBlock = context.getOutputBlock();
173
174 // This class can only process mono signals. Use the ProcessorDuplicator class
175 // to apply this filter on a multi-channel audio stream.
176 jassert (inputBlock.getNumChannels() == 1);
177 jassert (outputBlock.getNumChannels() == 1);
178
179 auto n = inputBlock.getNumSamples();
180 auto* src = inputBlock .getChannelPointer (0);
181 auto* dst = outputBlock.getChannelPointer (0);
182
183 switch (parameters->type)
184 {
188 default: jassertfalse;
189 }
190 }
191
192 //==============================================================================
193 std::array<SampleType, 3> y;
194 SampleType s1, s2;
195
196 //==============================================================================
198 };
199
206
207 //==============================================================================
213 template <typename NumericType>
215 {
216 //==============================================================================
218
219 //==============================================================================
221 Type type = Type::lowPass;
222
229 void setCutOffFrequency (double sampleRate, NumericType frequency,
230 NumericType resonance = static_cast<NumericType> (1.0 / MathConstants<double>::sqrt2)) noexcept
231 {
232 jassert (sampleRate > 0);
233 jassert (resonance > NumericType (0));
234 jassert (frequency > NumericType (0) && frequency <= NumericType (sampleRate * 0.5));
235
236 g = static_cast<NumericType> (std::tan (MathConstants<double>::pi * frequency / sampleRate));
237 R2 = static_cast<NumericType> (1.0 / resonance);
238 h = static_cast<NumericType> (1.0 / (1.0 + R2 * g + g * g));
239 }
240
241 //==============================================================================
246
247 //==============================================================================
248 Parameters() = default;
249 Parameters (const Parameters& o) : g (o.g), R2 (o.R2), h (o.h) {}
250 Parameters& operator= (const Parameters& o) noexcept { g = o.g; R2 = o.R2; h = o.h; return *this; }
251
252 //==============================================================================
253 NumericType g = static_cast<NumericType> (std::tan (MathConstants<double>::pi * 200.0 / 44100.0));
254 NumericType R2 = static_cast<NumericType> (MathConstants<double>::sqrt2);
255 NumericType h = static_cast<NumericType> (1.0 / (1.0 + R2 * g + g * g));
256 };
257}
258
259} // namespace dsp
260} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
CAdPlugDatabase::CRecord::RecordType type
Definition adplugdb.cpp:93
Definition juce_ReferenceCountedObject.h:247
Filter()
Definition juce_StateVariableFilter.h:77
Parameters< NumericType >::Ptr parameters
Definition juce_StateVariableFilter.h:107
typename Parameters< NumericType >::Ptr ParametersPtr
Definition juce_StateVariableFilter.h:70
void prepare(const ProcessSpec &) noexcept
Definition juce_StateVariableFilter.h:93
SampleType s1
Definition juce_StateVariableFilter.h:194
void process(const ProcessContext &context) noexcept
Definition juce_StateVariableFilter.h:111
void reset() noexcept
Definition juce_StateVariableFilter.h:96
Filter(ParametersPtr parametersToUse)
Definition juce_StateVariableFilter.h:82
std::array< SampleType, 3 > y
Definition juce_StateVariableFilter.h:193
SampleType JUCE_VECTOR_CALLTYPE processSample(SampleType sample) noexcept
Definition juce_StateVariableFilter.h:124
SampleType s2
Definition juce_StateVariableFilter.h:194
void processInternal(const ProcessContext &context) noexcept
Definition juce_StateVariableFilter.h:169
void processBlock(const SampleType *input, SampleType *output, size_t n) noexcept
Definition juce_StateVariableFilter.h:154
void snapToZero() noexcept
Definition juce_StateVariableFilter.h:102
SampleType JUCE_VECTOR_CALLTYPE processLoop(SampleType sample, Parameters< NumericType > &state) noexcept
Definition juce_StateVariableFilter.h:140
typename SampleTypeHelpers::ElementType< SampleType >::Type NumericType
Definition juce_StateVariableFilter.h:67
register unsigned i
Definition inflate.c:1575
#define JUCE_LEAK_DETECTOR(OwnerClass)
Definition juce_LeakedObjectDetector.h:138
#define jassert(expression)
#define jassertfalse
#define JUCE_VECTOR_CALLTYPE
Definition juce_dsp.h:100
Definition juce_StateVariableFilter.h:35
StateVariableFilterType
Definition juce_StateVariableFilter.h:201
@ bandPass
Definition juce_StateVariableFilter.h:203
@ lowPass
Definition juce_StateVariableFilter.h:202
@ highPass
Definition juce_StateVariableFilter.h:204
void snapToZero(SIMDRegister< Type > &) noexcept
Definition juce_SIMDRegister_Impl.h:167
Definition juce_AudioBlock.h:29
bool isBypassed(const ProcessorChain< Processors... > &chain) noexcept
Definition juce_ProcessorChain.h:160
Definition carla_juce.cpp:31
Definition juce_Uuid.h:141
static constexpr FloatType sqrt2
Definition juce_MathsFunctions.h:394
static constexpr FloatType pi
Definition juce_MathsFunctions.h:382
Definition juce_ProcessContext.h:38
Definition juce_ProcessContext.h:67
T Type
Definition juce_AudioBlock.h:37
Definition juce_StateVariableFilter.h:215
Parameters(const Parameters &o)
Definition juce_StateVariableFilter.h:249
StateVariableFilterType Type
Definition juce_StateVariableFilter.h:217
void setCutOffFrequency(double sampleRate, NumericType frequency, NumericType resonance=static_cast< NumericType >(1.0/MathConstants< double >::sqrt2)) noexcept
Definition juce_StateVariableFilter.h:229
NumericType g
Definition juce_StateVariableFilter.h:253
Type type
Definition juce_StateVariableFilter.h:221
ReferenceCountedObjectPtr< Parameters > Ptr
Definition juce_StateVariableFilter.h:245
NumericType R2
Definition juce_StateVariableFilter.h:254
NumericType h
Definition juce_StateVariableFilter.h:255
signed int sample
Definition tap_dynamics_m.c:41
int n
Definition crypt.c:458