quantumVERB  1.0.0
A FOSS convolution reverb plugin
Filter.cpp
1 /*
2  ==============================================================================
3 
4  Filter.cpp
5 
6  ==============================================================================
7 */
8 
9 #include "Filter.h"
10 #include "PluginProcessor.h"
11 
12 #include "Logger.h"
13 
14 namespace reverb
15 {
16 
17  //==============================================================================
18  /**
19  * @brief Constructs a Filter object with optional frequency/gain/Q parameters
20  *
21  * The Filter object is constructed from the processor pointer and from the optional frequency/gain/Q parameters.
22  * The frequency is the cut-off frequency of the derived LowShelfFilter/HighShelfFilter and the center frequency of the PeakFilter class.
23  *
24  * @param [in] processor Pointer to main processor
25  * @param [in] freq Band frequency
26  * @param [in] gain Band gain
27  * @param [in] q Q factor
28  */
29  Filter::Filter(juce::AudioProcessor * processor, float freq, float q, float gain)
30  : Task(processor), frequency(freq), Q(q), gainFactor(gain)
31  {
32 
33  }
34 
35  /**
36  * @brief Updates parameters from processor parameter tree
37  *
38  * @param [in] params Processor parameter tree
39  * @param [in] blockId ID of block whose paramters should be checked
40  *
41  */
42  void Filter::updateParams(const juce::AudioProcessorValueTreeState& params,
43  const juce::String& blockId)
44  {
45  // Frequency
46  float _frequency = getParam(params,
47  blockId + AudioProcessor::PID_FILTER_FREQ_SUFFIX);
48 
49  if (frequency != _frequency)
50  {
51  frequency = _frequency;
52  buildFilter();
53  mustExec = true;
54  }
55 
56  // Q factor
57  float _Q = getParam(params,
58  blockId + AudioProcessor::PID_FILTER_Q_SUFFIX);
59 
60  if (Q != _Q)
61  {
62  Q = _Q;
63  buildFilter();
64  mustExec = true;
65  }
66 
67 
68 
69 
70  }
71 
72  //==============================================================================
73  /**
74  * @brief Filters the audio in AudioSampleBuffer
75  *
76  * This function filters an AudioBuffer using the IIR filter's coefficients
77  *
78  * @param [in,out] ir Contains the audio to be filtered, the output is placed in that same buffer
79  *
80  */
82  {
83  buildFilter();
84 
85  if (ir.getNumChannels() != 1)
86  {
87  logger.dualPrint(Logger::Level::Warning, "Filter: AudioBuffer channel count is not 1");
88  return ir;
89  }
90 
91  juce::dsp::ProcessContextReplacing<float> context(ir);
92  juce::dsp::IIR::Filter<float>::process(context);
93 
94  // Reset mustExec flag
95  mustExec = false;
96 
97 
98  return ir;
99  }
100 
101  //==============================================================================
102  /**
103  * @brief Returns the filter absolute amplitude response at a given frequency
104  *
105  * @param [in] freq Frequency at which the filter magnitude is evaluated
106  */
107 
108 
109  float Filter::getAmplitude(float freq) {
110  // All filters are 2nd order and thus have five coefficients
111  float * coeffs = coefficients->getRawCoefficients();
112 
113  float b0 = coeffs[0];
114  float b1 = coeffs[1];
115  float b2 = coeffs[2];
116  float a1 = coeffs[3];
117  float a2 = coeffs[4];
118 
119  //Compute transfer function argument e^(jw)
120  std::complex<float> input;
121  std::complex<float> output;
122 
123  float minusOmega = -(2 * M_PI * freq) / processor->getSampleRate();
124 
125  input.real(std::cos(minusOmega));
126  input.imag(std::sin(minusOmega));
127 
128  //Compute and return transfer function amplitude
129 
130  return std::abs(b0 + b1 * input + b2 * std::pow(input, 2)) / std::abs(1.0f + a1 * input + a2 * std::pow(input, 2));
131  }
132 
133  //==============================================================================
134  /**
135  * @brief Returns the filter amplitude response in dB at a given frequency
136  *
137  * @param [in] freq Frequency at which the filter magnitude is evaluated
138  *
139  * @return Filter amplitude response in dB
140  */
141 
142  float Filter::getdBAmplitude(float freq)
143  {
144  return 20 * std::log10(getAmplitude(freq));
145  }
146 
147  //==============================================================================
148  /**
149  * @brief Sets the filter frequency and updates the IIR filter coefficients (Meant to be used by Equalizer class)
150  *
151  * @param [in] freq Frequency to be set
152  */
153 
154 
155  void Filter::setFrequency(float freq)
156  {
157 
158  frequency = freq;
159 
160  buildFilter();
161  }
162 
163  //==============================================================================
164  /**
165  * @brief Sets the filter Q factor and updates the IIR filter coefficients (Meant to be used by Equalizer class)
166  *
167  * @param [in] freq Q factor to be set
168  */
169 
170  void Filter::setQ(float q)
171  {
172 
173  Q = q;
174 
175  buildFilter();
176  }
177  //==============================================================================
178  /**
179  * @brief Sets the filter band gain and updates the IIR filter coefficients (Meant to be used by Equalizer class)
180  *
181  * @param [in] freq Band gain to be set
182  */
183 
184  void Filter::setGain(float gain)
185  {
186 
187  gainFactor = gain;
188 
189  buildFilter();
190  }
191 
192  //==============================================================================
193  /**
194  * @brief Generates and sets the low-shelf IIR filter coefficients according to the current filter parameters
195  *
196  */
198  {
199 
200  coefficients = juce::dsp::IIR::Coefficients<float>::makeLowShelf(processor->getSampleRate(),
201  frequency, Q, gainFactor);
202  }
203 
204  //==============================================================================
205  /**
206  * @brief Generates and sets the high-shelf IIR filter coefficients according to the current filter parameters
207  *
208  */
210  {
211 
212  coefficients = juce::dsp::IIR::Coefficients<float>::makeHighShelf(processor->getSampleRate(),
213  frequency, Q, gainFactor);
214  }
215 
216  //==============================================================================
217  /**
218  * @brief Generates and sets the peak IIR filter coefficients according to the current filter parameters
219  *
220  */
222  {
223 
224  coefficients = juce::dsp::IIR::Coefficients<float>::makePeakFilter(processor->getSampleRate(),
225  frequency, Q, gainFactor);
226  }
227 
228 }
float getdBAmplitude(float freq)
Returns the filter amplitude response in dB at a given frequency.
Definition: Filter.cpp:142
float getAmplitude(float freq)
Returns the filter absolute amplitude response at a given frequency.
Definition: Filter.cpp:109
float getParam(const juce::AudioProcessorValueTreeState &params, const juce::String &blockId) const
Internal method used to get (and check) a parameter&#39;s value.
Definition: Task.h:111
virtual void buildFilter() override
Generates and sets the peak IIR filter coefficients according to the current filter parameters...
Definition: Filter.cpp:221
void setQ(float)
Sets the filter Q factor and updates the IIR filter coefficients (Meant to be used by Equalizer class...
Definition: Filter.cpp:170
void setFrequency(float)
Sets the filter frequency and updates the IIR filter coefficients (Meant to be used by Equalizer clas...
Definition: Filter.cpp:155
virtual void updateParams(const juce::AudioProcessorValueTreeState &params, const juce::String &blockId) override
Updates parameters from processor parameter tree.
Definition: Filter.cpp:42
virtual AudioBlock exec(AudioBlock ir) override
Filters the audio in AudioSampleBuffer.
Definition: Filter.cpp:81
#define M_PI
Definition: Filter.h:14
void setGain(float)
Sets the filter band gain and updates the IIR filter coefficients (Meant to be used by Equalizer clas...
Definition: Filter.cpp:184
virtual void buildFilter() override
Generates and sets the low-shelf IIR filter coefficients according to the current filter parameters...
Definition: Filter.cpp:197
virtual void buildFilter() override
Generates and sets the high-shelf IIR filter coefficients according to the current filter parameters...
Definition: Filter.cpp:209
Filter(juce::AudioProcessor *processor, float freq=1000.0f, float q=0.71f, float gain=1.5f)
Constructs a Filter object with optional frequency/gain/Q parameters.
Definition: Filter.cpp:29