OpenShot Library | OpenShotAudio  0.2.2
juce_audio_basics/utilities/juce_IIRFilter.h
1 
2 /** @weakgroup juce_audio_basics-utilities
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
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 
30 class IIRFilter;
31 
32 //==============================================================================
33 /**
34  A set of coefficients for use in an IIRFilter object.
35 
36  @see IIRFilter
37 
38  @tags{Audio}
39 */
41 {
42 public:
43  //==============================================================================
44  /** Creates a null set of coefficients (which will produce silence). */
45  IIRCoefficients() noexcept;
46 
47  /** Directly constructs an object from the raw coefficients.
48  Most people will want to use the static methods instead of this, but
49  the constructor is public to allow tinkerers to create their own custom
50  filters!
51  */
52  IIRCoefficients (double c1, double c2, double c3,
53  double c4, double c5, double c6) noexcept;
54 
55  /** Creates a copy of another filter. */
56  IIRCoefficients (const IIRCoefficients&) noexcept;
57  /** Creates a copy of another filter. */
58  IIRCoefficients& operator= (const IIRCoefficients&) noexcept;
59  /** Destructor. */
60  ~IIRCoefficients() noexcept;
61 
62  //==============================================================================
63  /** Returns the coefficients for a low-pass filter. */
64  static IIRCoefficients makeLowPass (double sampleRate,
65  double frequency) noexcept;
66 
67  /** Returns the coefficients for a low-pass filter with variable Q. */
68  static IIRCoefficients makeLowPass (double sampleRate,
69  double frequency,
70  double Q) noexcept;
71 
72  //==============================================================================
73  /** Returns the coefficients for a high-pass filter. */
74  static IIRCoefficients makeHighPass (double sampleRate,
75  double frequency) noexcept;
76 
77  /** Returns the coefficients for a high-pass filter with variable Q. */
78  static IIRCoefficients makeHighPass (double sampleRate,
79  double frequency,
80  double Q) noexcept;
81 
82  //==============================================================================
83  /** Returns the coefficients for a band-pass filter. */
84  static IIRCoefficients makeBandPass (double sampleRate, double frequency) noexcept;
85 
86  /** Returns the coefficients for a band-pass filter with variable Q. */
87  static IIRCoefficients makeBandPass (double sampleRate,
88  double frequency,
89  double Q) noexcept;
90 
91  //==============================================================================
92  /** Returns the coefficients for a notch filter. */
93  static IIRCoefficients makeNotchFilter (double sampleRate, double frequency) noexcept;
94 
95  /** Returns the coefficients for a notch filter with variable Q. */
96  static IIRCoefficients makeNotchFilter (double sampleRate,
97  double frequency,
98  double Q) noexcept;
99 
100  //==============================================================================
101  /** Returns the coefficients for an all-pass filter. */
102  static IIRCoefficients makeAllPass (double sampleRate, double frequency) noexcept;
103 
104  /** Returns the coefficients for an all-pass filter with variable Q. */
105  static IIRCoefficients makeAllPass (double sampleRate,
106  double frequency,
107  double Q) noexcept;
108 
109  //==============================================================================
110  /** Returns the coefficients for a low-pass shelf filter with variable Q and gain.
111 
112  The gain is a scale factor that the low frequencies are multiplied by, so values
113  greater than 1.0 will boost the low frequencies, values less than 1.0 will
114  attenuate them.
115  */
116  static IIRCoefficients makeLowShelf (double sampleRate,
117  double cutOffFrequency,
118  double Q,
119  float gainFactor) noexcept;
120 
121  /** Returns the coefficients for a high-pass shelf filter with variable Q and gain.
122 
123  The gain is a scale factor that the high frequencies are multiplied by, so values
124  greater than 1.0 will boost the high frequencies, values less than 1.0 will
125  attenuate them.
126  */
127  static IIRCoefficients makeHighShelf (double sampleRate,
128  double cutOffFrequency,
129  double Q,
130  float gainFactor) noexcept;
131 
132  /** Returns the coefficients for a peak filter centred around a
133  given frequency, with a variable Q and gain.
134 
135  The gain is a scale factor that the centre frequencies are multiplied by, so
136  values greater than 1.0 will boost the centre frequencies, values less than
137  1.0 will attenuate them.
138  */
139  static IIRCoefficients makePeakFilter (double sampleRate,
140  double centreFrequency,
141  double Q,
142  float gainFactor) noexcept;
143 
144  //==============================================================================
145  /** The raw coefficients.
146  You should leave these numbers alone unless you really know what you're doing.
147  */
148  float coefficients[5];
149 };
150 
151 //==============================================================================
152 /**
153  An IIR filter that can perform low, high, or band-pass filtering on an
154  audio signal.
155 
156  @see IIRCoefficient, IIRFilterAudioSource
157 
158  @tags{Audio}
159 */
161 {
162 public:
163  //==============================================================================
164  /** Creates a filter.
165 
166  Initially the filter is inactive, so will have no effect on samples that
167  you process with it. Use the setCoefficients() method to turn it into the
168  type of filter needed.
169  */
170  IIRFilter() noexcept;
171 
172  /** Creates a copy of another filter. */
173  IIRFilter (const IIRFilter&) noexcept;
174 
175  /** Destructor. */
176  ~IIRFilter() noexcept;
177 
178  //==============================================================================
179  /** Clears the filter so that any incoming data passes through unchanged. */
180  void makeInactive() noexcept;
181 
182  /** Applies a set of coefficients to this filter. */
183  void setCoefficients (const IIRCoefficients& newCoefficients) noexcept;
184 
185  /** Returns the coefficients that this filter is using. */
186  IIRCoefficients getCoefficients() const noexcept { return coefficients; }
187 
188  //==============================================================================
189  /** Resets the filter's processing pipeline, ready to start a new stream of data.
190 
191  Note that this clears the processing state, but the type of filter and
192  its coefficients aren't changed. To put a filter into an inactive state, use
193  the makeInactive() method.
194  */
195  void reset() noexcept;
196 
197  /** Performs the filter operation on the given set of samples. */
198  void processSamples (float* samples, int numSamples) noexcept;
199 
200  /** Processes a single sample, without any locking or checking.
201 
202  Use this if you need fast processing of a single value, but be aware that
203  this isn't thread-safe in the way that processSamples() is.
204  */
205  float processSingleSampleRaw (float sample) noexcept;
206 
207 protected:
208  //==============================================================================
209  SpinLock processLock;
210  IIRCoefficients coefficients;
211  float v1 = 0, v2 = 0;
212  bool active = false;
213 
214  // The exact meaning of an assignment operator would be ambiguous since the filters are
215  // stateful. If you want to copy the coefficients, then just use setCoefficients().
216  IIRFilter& operator= (const IIRFilter&) = delete;
217 
218  JUCE_LEAK_DETECTOR (IIRFilter)
219 };
220 
221 } // namespace juce
222 
223 /** @}*/
#define JUCE_API
This macro is added to all JUCE public class declarations.
IIRCoefficients getCoefficients() const noexcept
Returns the coefficients that this filter is using.
A simple spin-lock class that can be used as a simple, low-overhead mutex for uncontended situations...
Definition: juce_SpinLock.h:45
An IIR filter that can perform low, high, or band-pass filtering on an audio signal.
A set of coefficients for use in an IIRFilter object.