OpenShot Audio Library | OpenShotAudio  0.3.2
juce_dsp/processors/juce_IIRFilter.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
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 5 End-User License
11  Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
12  27th April 2017).
13 
14  End User License Agreement: www.juce.com/juce-5-licence
15  Privacy Policy: www.juce.com/juce-5-privacy-policy
16 
17  Or: You may also use this code under the terms of the GPL v3 (see
18  www.gnu.org/licenses).
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 namespace dsp
30 {
31 
35 namespace IIR
36 {
37  template <typename NumericType>
38  struct Coefficients;
39 
53  template <typename SampleType>
54  class Filter
55  {
56  public:
60  using NumericType = typename SampleTypeHelpers::ElementType<SampleType>::Type;
61 
64 
65  //==============================================================================
72  Filter();
73 
75  Filter (CoefficientsPtr coefficientsToUse);
76 
77  Filter (const Filter&) = default;
78  Filter (Filter&&) = default;
79  Filter& operator= (const Filter&) = default;
80  Filter& operator= (Filter&&) = default;
81 
82  //==============================================================================
90 
91  //==============================================================================
97  void reset() { reset (SampleType {0}); }
98 
102  void reset (SampleType resetToValue);
103 
104  //==============================================================================
106  void prepare (const ProcessSpec&) noexcept;
107 
109  template <typename ProcessContext>
110  void process (const ProcessContext& context) noexcept
111  {
112  if (context.isBypassed)
113  processInternal<ProcessContext, true> (context);
114  else
115  processInternal<ProcessContext, false> (context);
116  }
117 
125  SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType sample) noexcept;
126 
131  void snapToZero() noexcept;
132 
133  private:
134  //==============================================================================
135  void check();
136 
138  template <typename ProcessContext, bool isBypassed>
139  void processInternal (const ProcessContext& context) noexcept;
140 
141  //==============================================================================
142  HeapBlock<SampleType> memory;
143  SampleType* state = nullptr;
144  size_t order = 0;
145 
146  JUCE_LEAK_DETECTOR (Filter)
147  };
148 
149 
150  //==============================================================================
156  template <typename NumericType>
157  struct Coefficients : public ProcessorState
158  {
160  Coefficients();
161 
167  NumericType a0, NumericType a1);
168 
170  NumericType a0, NumericType a1, NumericType a2);
171 
174 
175  Coefficients (const Coefficients&) = default;
176  Coefficients (Coefficients&&) = default;
177  Coefficients& operator= (const Coefficients&) = default;
178  Coefficients& operator= (Coefficients&&) = default;
179 
184 
185  //==============================================================================
187  static Ptr makeFirstOrderLowPass (double sampleRate, NumericType frequency);
188 
190  static Ptr makeFirstOrderHighPass (double sampleRate, NumericType frequency);
191 
193  static Ptr makeFirstOrderAllPass (double sampleRate, NumericType frequency);
194 
195  //==============================================================================
197  static Ptr makeLowPass (double sampleRate, NumericType frequency);
198 
200  static Ptr makeLowPass (double sampleRate, NumericType frequency, NumericType Q);
201 
202  //==============================================================================
204  static Ptr makeHighPass (double sampleRate, NumericType frequency);
205 
207  static Ptr makeHighPass (double sampleRate, NumericType frequency, NumericType Q);
208 
209  //==============================================================================
211  static Ptr makeBandPass (double sampleRate, NumericType frequency);
212 
214  static Ptr makeBandPass (double sampleRate, NumericType frequency, NumericType Q);
215 
216  //==============================================================================
218  static Ptr makeNotch (double sampleRate, NumericType frequency);
219 
221  static Ptr makeNotch (double sampleRate, NumericType frequency, NumericType Q);
222 
223  //==============================================================================
225  static Ptr makeAllPass (double sampleRate, NumericType frequency);
226 
228  static Ptr makeAllPass (double sampleRate, NumericType frequency, NumericType Q);
229 
230  //==============================================================================
237  static Ptr makeLowShelf (double sampleRate, NumericType cutOffFrequency,
238  NumericType Q, NumericType gainFactor);
239 
246  static Ptr makeHighShelf (double sampleRate, NumericType cutOffFrequency,
247  NumericType Q, NumericType gainFactor);
248 
256  static Ptr makePeakFilter (double sampleRate, NumericType centreFrequency,
257  NumericType Q, NumericType gainFactor);
258 
259  //==============================================================================
261  size_t getFilterOrder() const noexcept;
262 
266  double getMagnitudeForFrequency (double frequency, double sampleRate) const noexcept;
267 
271  void getMagnitudeForFrequencyArray (const double* frequencies, double* magnitudes,
272  size_t numSamples, double sampleRate) const noexcept;
273 
277  double getPhaseForFrequency (double frequency, double sampleRate) const noexcept;
278 
282  void getPhaseForFrequencyArray (double* frequencies, double* phases,
283  size_t numSamples, double sampleRate) const noexcept;
284 
286  NumericType* getRawCoefficients() noexcept { return coefficients.getRawDataPointer(); }
287 
289  const NumericType* getRawCoefficients() const noexcept { return coefficients.begin(); }
290 
291  //==============================================================================
296 
297  private:
298  // Unfortunately, std::sqrt is not marked as constexpr just yet in all compilers
299  static constexpr NumericType inverseRootTwo = static_cast<NumericType> (0.70710678118654752440L);
300  };
301 
302 } // namespace IIR
303 } // namespace dsp
304 } // namespace juce
305 
306 #include "juce_IIRFilter_Impl.h"
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
const NumericType * getRawCoefficients() const noexcept