OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_dsp/processors/juce_IIRFilter.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
29 namespace juce::dsp::IIR
30{
35 template <typename NumericType>
36 struct ArrayCoefficients
37 {
39 static std::array<NumericType, 4> makeFirstOrderLowPass (double sampleRate, NumericType frequency);
40
42 static std::array<NumericType, 4> makeFirstOrderHighPass (double sampleRate, NumericType frequency);
43
45 static std::array<NumericType, 4> makeFirstOrderAllPass (double sampleRate, NumericType frequency);
46
48 static std::array<NumericType, 6> makeLowPass (double sampleRate, NumericType frequency);
49
51 static std::array<NumericType, 6> makeLowPass (double sampleRate, NumericType frequency, NumericType Q);
52
54 static std::array<NumericType, 6> makeHighPass (double sampleRate, NumericType frequency);
55
57 static std::array<NumericType, 6> makeHighPass (double sampleRate, NumericType frequency, NumericType Q);
58
60 static std::array<NumericType, 6> makeBandPass (double sampleRate, NumericType frequency);
61
63 static std::array<NumericType, 6> makeBandPass (double sampleRate, NumericType frequency, NumericType Q);
64
66 static std::array<NumericType, 6> makeNotch (double sampleRate, NumericType frequency);
67
69 static std::array<NumericType, 6> makeNotch (double sampleRate, NumericType frequency, NumericType Q);
70
72 static std::array<NumericType, 6> makeAllPass (double sampleRate, NumericType frequency);
73
75 static std::array<NumericType, 6> makeAllPass (double sampleRate, NumericType frequency, NumericType Q);
76
83 static std::array<NumericType, 6> makeLowShelf (double sampleRate,
84 NumericType cutOffFrequency,
85 NumericType Q,
86 NumericType gainFactor);
87
94 static std::array<NumericType, 6> makeHighShelf (double sampleRate,
95 NumericType cutOffFrequency,
96 NumericType Q,
97 NumericType gainFactor);
98
106 static std::array<NumericType, 6> makePeakFilter (double sampleRate,
107 NumericType centreFrequency,
108 NumericType Q,
109 NumericType gainFactor);
110
111 private:
112 // Unfortunately, std::sqrt is not marked as constexpr just yet in all compilers
113 static constexpr NumericType inverseRootTwo = static_cast<NumericType> (0.70710678118654752440L);
114 };
115
116 //==============================================================================
122 template <typename NumericType>
123 struct Coefficients : public ProcessorState
124 {
126 Coefficients();
127
132 Coefficients (NumericType b0, NumericType b1,
133 NumericType a0, NumericType a1);
134
135 Coefficients (NumericType b0, NumericType b1, NumericType b2,
136 NumericType a0, NumericType a1, NumericType a2);
137
138 Coefficients (NumericType b0, NumericType b1, NumericType b2, NumericType b3,
139 NumericType a0, NumericType a1, NumericType a2, NumericType a3);
140
141 Coefficients (const Coefficients&) = default;
142 Coefficients (Coefficients&&) = default;
143 Coefficients& operator= (const Coefficients&) = default;
144 Coefficients& operator= (Coefficients&&) = default;
145
147 template <size_t Num>
148 explicit Coefficients (const std::array<NumericType, Num>& values) { assignImpl<Num> (values.data()); }
149
151 template <size_t Num>
152 Coefficients& operator= (const std::array<NumericType, Num>& values) { return assignImpl<Num> (values.data()); }
153
157 using Ptr = ReferenceCountedObjectPtr<Coefficients>;
158
159 //==============================================================================
161 static Ptr makeFirstOrderLowPass (double sampleRate, NumericType frequency);
162
164 static Ptr makeFirstOrderHighPass (double sampleRate, NumericType frequency);
165
167 static Ptr makeFirstOrderAllPass (double sampleRate, NumericType frequency);
168
169 //==============================================================================
171 static Ptr makeLowPass (double sampleRate, NumericType frequency);
172
174 static Ptr makeLowPass (double sampleRate, NumericType frequency, NumericType Q);
175
176 //==============================================================================
178 static Ptr makeHighPass (double sampleRate, NumericType frequency);
179
181 static Ptr makeHighPass (double sampleRate, NumericType frequency, NumericType Q);
182
183 //==============================================================================
185 static Ptr makeBandPass (double sampleRate, NumericType frequency);
186
188 static Ptr makeBandPass (double sampleRate, NumericType frequency, NumericType Q);
189
190 //==============================================================================
192 static Ptr makeNotch (double sampleRate, NumericType frequency);
193
195 static Ptr makeNotch (double sampleRate, NumericType frequency, NumericType Q);
196
197 //==============================================================================
199 static Ptr makeAllPass (double sampleRate, NumericType frequency);
200
202 static Ptr makeAllPass (double sampleRate, NumericType frequency, NumericType Q);
203
204 //==============================================================================
211 static Ptr makeLowShelf (double sampleRate, NumericType cutOffFrequency,
212 NumericType Q, NumericType gainFactor);
213
220 static Ptr makeHighShelf (double sampleRate, NumericType cutOffFrequency,
221 NumericType Q, NumericType gainFactor);
222
230 static Ptr makePeakFilter (double sampleRate, NumericType centreFrequency,
231 NumericType Q, NumericType gainFactor);
232
233 //==============================================================================
235 size_t getFilterOrder() const noexcept;
236
240 double getMagnitudeForFrequency (double frequency, double sampleRate) const noexcept;
241
245 void getMagnitudeForFrequencyArray (const double* frequencies, double* magnitudes,
246 size_t numSamples, double sampleRate) const noexcept;
247
251 double getPhaseForFrequency (double frequency, double sampleRate) const noexcept;
252
256 void getPhaseForFrequencyArray (double* frequencies, double* phases,
257 size_t numSamples, double sampleRate) const noexcept;
258
260 NumericType* getRawCoefficients() noexcept { return coefficients.getRawDataPointer(); }
261
263 const NumericType* getRawCoefficients() const noexcept { return coefficients.begin(); }
264
265 //==============================================================================
269 Array<NumericType> coefficients;
270
271 private:
272 using ArrayCoeffs = ArrayCoefficients<NumericType>;
273
274 template <size_t Num>
275 Coefficients& assignImpl (const NumericType* values);
276
277 template <size_t Num>
278 Coefficients& assign (const NumericType (& values)[Num]) { return assignImpl<Num> (values); }
279 };
280
281 //==============================================================================
295 template <typename SampleType>
296 class Filter
297 {
298 public:
302 using NumericType = typename SampleTypeHelpers::ElementType<SampleType>::Type;
303
305 using CoefficientsPtr = typename Coefficients<NumericType>::Ptr;
306
307 //==============================================================================
314 Filter();
315
317 Filter (CoefficientsPtr coefficientsToUse);
318
319 Filter (const Filter&) = default;
320 Filter (Filter&&) = default;
321 Filter& operator= (const Filter&) = default;
322 Filter& operator= (Filter&&) = default;
323
324 //==============================================================================
331 CoefficientsPtr coefficients;
332
333 //==============================================================================
339 void reset() { reset (SampleType {0}); }
340
344 void reset (SampleType resetToValue);
345
346 //==============================================================================
348 void prepare (const ProcessSpec&) noexcept;
349
351 template <typename ProcessContext>
352 void process (const ProcessContext& context) noexcept
353 {
354 if (context.isBypassed)
355 processInternal<ProcessContext, true> (context);
356 else
357 processInternal<ProcessContext, false> (context);
358
359 #if JUCE_DSP_ENABLE_SNAP_TO_ZERO
360 snapToZero();
361 #endif
362 }
363
371 SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType sample) noexcept;
372
377 void snapToZero() noexcept;
378
379 private:
380 //==============================================================================
381 void check();
382
384 template <typename ProcessContext, bool isBypassed>
385 void processInternal (const ProcessContext& context) noexcept;
386
387 //==============================================================================
388 HeapBlock<SampleType> memory;
389 SampleType* state = nullptr;
390 size_t order = 0;
391
392 JUCE_LEAK_DETECTOR (Filter)
393 };
394} // namespace juce::dsp::IIR
juce::Array
Definition juce_Array.h:56
juce::Array::begin
ElementType * begin() noexcept
Definition juce_Array.h:328
juce::Array::getRawDataPointer
ElementType * getRawDataPointer() noexcept
Definition juce_Array.h:310
juce::HeapBlock
Definition juce_HeapBlock.h:87
juce::ReferenceCountedObjectPtr
Definition juce_ReferenceCountedObject.h:247
juce::dsp::IIR::Filter
Definition juce_dsp/processors/juce_IIRFilter.h:297
juce::dsp::IIR::Filter::prepare
void prepare(const ProcessSpec &) noexcept
Definition juce_IIRFilter_Impl.h:83
juce::dsp::IIR::Filter::Filter
Filter()
Definition juce_IIRFilter_Impl.h:54
juce::dsp::IIR::Filter::coefficients
CoefficientsPtr coefficients
Definition juce_dsp/processors/juce_IIRFilter.h:331
juce::dsp::IIR::Filter::snapToZero
void snapToZero() noexcept
Definition juce_IIRFilter_Impl.h:223
juce::dsp::IIR::Filter::reset
void reset()
Definition juce_dsp/processors/juce_IIRFilter.h:339
juce::dsp::IIR::Filter::processSample
SampleType JUCE_VECTOR_CALLTYPE processSample(SampleType sample) noexcept
Definition juce_IIRFilter_Impl.h:207
juce::dsp::IIR::Filter::CoefficientsPtr
typename Coefficients< NumericType >::Ptr CoefficientsPtr
Definition juce_dsp/processors/juce_IIRFilter.h:305
juce::dsp::IIR::Filter::NumericType
typename SampleTypeHelpers::ElementType< SampleType >::Type NumericType
Definition juce_dsp/processors/juce_IIRFilter.h:302
juce::dsp::IIR::Filter::process
void process(const ProcessContext &context) noexcept
Definition juce_dsp/processors/juce_IIRFilter.h:352
juce::dsp::IIR
Definition juce_dsp/processors/juce_IIRFilter.cpp:27
juce::dsp::IIR::ArrayCoefficients
Definition juce_dsp/processors/juce_IIRFilter.h:37
juce::dsp::IIR::ArrayCoefficients::makeHighPass
static std::array< NumericType, 6 > makeHighPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:94
juce::dsp::IIR::ArrayCoefficients::makePeakFilter
static std::array< NumericType, 6 > makePeakFilter(double sampleRate, NumericType centreFrequency, NumericType Q, NumericType gainFactor)
Definition juce_dsp/processors/juce_IIRFilter.cpp:251
juce::dsp::IIR::ArrayCoefficients::makeBandPass
static std::array< NumericType, 6 > makeBandPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:120
juce::dsp::IIR::ArrayCoefficients::makeFirstOrderHighPass
static std::array< NumericType, 4 > makeFirstOrderHighPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:44
juce::dsp::IIR::ArrayCoefficients::makeLowPass
static std::array< NumericType, 6 > makeLowPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:68
juce::dsp::IIR::ArrayCoefficients::makeLowShelf
static std::array< NumericType, 6 > makeLowShelf(double sampleRate, NumericType cutOffFrequency, NumericType Q, NumericType gainFactor)
Definition juce_dsp/processors/juce_IIRFilter.cpp:199
juce::dsp::IIR::ArrayCoefficients::makeFirstOrderLowPass
static std::array< NumericType, 4 > makeFirstOrderLowPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:32
juce::dsp::IIR::ArrayCoefficients::makeFirstOrderAllPass
static std::array< NumericType, 4 > makeFirstOrderAllPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:56
juce::dsp::IIR::ArrayCoefficients::makeAllPass
static std::array< NumericType, 6 > makeAllPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:173
juce::dsp::IIR::ArrayCoefficients::makeHighShelf
static std::array< NumericType, 6 > makeHighShelf(double sampleRate, NumericType cutOffFrequency, NumericType Q, NumericType gainFactor)
Definition juce_dsp/processors/juce_IIRFilter.cpp:225
juce::dsp::IIR::ArrayCoefficients::makeNotch
static std::array< NumericType, 6 > makeNotch(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:147
juce::dsp::IIR::Coefficients
Definition juce_dsp/processors/juce_IIRFilter.h:124
juce::dsp::IIR::Coefficients::makeLowShelf
static Ptr makeLowShelf(double sampleRate, NumericType cutOffFrequency, NumericType Q, NumericType gainFactor)
Definition juce_dsp/processors/juce_IIRFilter.cpp:403
juce::dsp::IIR::Coefficients::getRawCoefficients
const NumericType * getRawCoefficients() const noexcept
Definition juce_dsp/processors/juce_IIRFilter.h:263
juce::dsp::IIR::Coefficients::Coefficients
Coefficients(const std::array< NumericType, Num > &values)
Definition juce_dsp/processors/juce_IIRFilter.h:148
juce::dsp::IIR::Coefficients::makeFirstOrderHighPass
static Ptr makeFirstOrderHighPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:314
juce::dsp::IIR::Coefficients::makeHighPass
static Ptr makeHighPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:343
juce::dsp::IIR::Coefficients::makeHighShelf
static Ptr makeHighShelf(double sampleRate, NumericType cutOffFrequency, NumericType Q, NumericType gainFactor)
Definition juce_dsp/processors/juce_IIRFilter.cpp:412
juce::dsp::IIR::Coefficients::getMagnitudeForFrequencyArray
void getMagnitudeForFrequencyArray(const double *frequencies, double *magnitudes, size_t numSamples, double sampleRate) const noexcept
Definition juce_dsp/processors/juce_IIRFilter.cpp:466
juce::dsp::IIR::Coefficients::makeLowPass
static Ptr makeLowPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:328
juce::dsp::IIR::Coefficients::getMagnitudeForFrequency
double getMagnitudeForFrequency(double frequency, double sampleRate) const noexcept
Definition juce_dsp/processors/juce_IIRFilter.cpp:436
juce::dsp::IIR::Coefficients::makePeakFilter
static Ptr makePeakFilter(double sampleRate, NumericType centreFrequency, NumericType Q, NumericType gainFactor)
Definition juce_dsp/processors/juce_IIRFilter.cpp:421
juce::dsp::IIR::Coefficients::getPhaseForFrequencyArray
void getPhaseForFrequencyArray(double *frequencies, double *phases, size_t numSamples, double sampleRate) const noexcept
Definition juce_dsp/processors/juce_IIRFilter.cpp:532
juce::dsp::IIR::Coefficients::makeAllPass
static Ptr makeAllPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:388
juce::dsp::IIR::Coefficients::getRawCoefficients
NumericType * getRawCoefficients() noexcept
Definition juce_dsp/processors/juce_IIRFilter.h:260
juce::dsp::IIR::Coefficients::makeBandPass
static Ptr makeBandPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:358
juce::dsp::IIR::Coefficients::Coefficients
Coefficients()
Definition juce_dsp/processors/juce_IIRFilter.cpp:276
juce::dsp::IIR::Coefficients::getPhaseForFrequency
double getPhaseForFrequency(double frequency, double sampleRate) const noexcept
Definition juce_dsp/processors/juce_IIRFilter.cpp:502
juce::dsp::IIR::Coefficients::makeNotch
static Ptr makeNotch(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:373
juce::dsp::IIR::Coefficients::getFilterOrder
size_t getFilterOrder() const noexcept
Definition juce_dsp/processors/juce_IIRFilter.cpp:430
juce::dsp::IIR::Coefficients::makeFirstOrderLowPass
static Ptr makeFirstOrderLowPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:307
juce::dsp::IIR::Coefficients::makeFirstOrderAllPass
static Ptr makeFirstOrderAllPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:321
juce::dsp::IIR::Coefficients::coefficients
Array< NumericType > coefficients
Definition juce_dsp/processors/juce_IIRFilter.h:269
juce::dsp::ProcessSpec
Definition juce_ProcessContext.h:36
juce::dsp::ProcessorState
Definition juce_ProcessContext.h:68