OpenShot Audio Library | OpenShotAudio  0.6.0
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>
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 
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  //==============================================================================
270 
271  private:
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 
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  //==============================================================================
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
ElementType * begin() noexcept
Definition: juce_Array.h:328
ElementType * getRawDataPointer() noexcept
Definition: juce_Array.h:310
void prepare(const ProcessSpec &) noexcept
SampleType JUCE_VECTOR_CALLTYPE processSample(SampleType sample) noexcept
typename Coefficients< NumericType >::Ptr CoefficientsPtr
typename SampleTypeHelpers::ElementType< SampleType >::Type NumericType
void process(const ProcessContext &context) noexcept
static std::array< NumericType, 6 > makeHighPass(double sampleRate, NumericType frequency)
static std::array< NumericType, 6 > makePeakFilter(double sampleRate, NumericType centreFrequency, NumericType Q, NumericType gainFactor)
static std::array< NumericType, 6 > makeBandPass(double sampleRate, NumericType frequency)
static std::array< NumericType, 4 > makeFirstOrderHighPass(double sampleRate, NumericType frequency)
static std::array< NumericType, 6 > makeLowPass(double sampleRate, NumericType frequency)
static std::array< NumericType, 6 > makeLowShelf(double sampleRate, NumericType cutOffFrequency, NumericType Q, NumericType gainFactor)
static std::array< NumericType, 4 > makeFirstOrderLowPass(double sampleRate, NumericType frequency)
static std::array< NumericType, 4 > makeFirstOrderAllPass(double sampleRate, NumericType frequency)
static std::array< NumericType, 6 > makeAllPass(double sampleRate, NumericType frequency)
static std::array< NumericType, 6 > makeHighShelf(double sampleRate, NumericType cutOffFrequency, NumericType Q, NumericType gainFactor)
static std::array< NumericType, 6 > makeNotch(double sampleRate, NumericType frequency)
static Ptr makeLowShelf(double sampleRate, NumericType cutOffFrequency, NumericType Q, NumericType gainFactor)
Coefficients(const std::array< NumericType, Num > &values)
static Ptr makeFirstOrderHighPass(double sampleRate, NumericType frequency)
static Ptr makeHighPass(double sampleRate, NumericType frequency)
static Ptr makeHighShelf(double sampleRate, NumericType cutOffFrequency, NumericType Q, NumericType gainFactor)
void getMagnitudeForFrequencyArray(const double *frequencies, double *magnitudes, size_t numSamples, double sampleRate) const noexcept
static Ptr makeLowPass(double sampleRate, NumericType frequency)
double getMagnitudeForFrequency(double frequency, double sampleRate) const noexcept
static Ptr makePeakFilter(double sampleRate, NumericType centreFrequency, NumericType Q, NumericType gainFactor)
void getPhaseForFrequencyArray(double *frequencies, double *phases, size_t numSamples, double sampleRate) const noexcept
static Ptr makeAllPass(double sampleRate, NumericType frequency)
static Ptr makeBandPass(double sampleRate, NumericType frequency)
double getPhaseForFrequency(double frequency, double sampleRate) const noexcept
static Ptr makeNotch(double sampleRate, NumericType frequency)
static Ptr makeFirstOrderLowPass(double sampleRate, NumericType frequency)
static Ptr makeFirstOrderAllPass(double sampleRate, NumericType frequency)
const NumericType * getRawCoefficients() const noexcept