andrewm@0
|
1 /*
|
andrewm@0
|
2 This code accompanies the textbook:
|
andrewm@0
|
3
|
andrewm@0
|
4 Digital Audio Effects: Theory, Implementation and Application
|
andrewm@0
|
5 Joshua D. Reiss and Andrew P. McPherson
|
andrewm@0
|
6
|
andrewm@0
|
7 ---
|
andrewm@0
|
8
|
andrewm@0
|
9 Parametric EQ: parametric equaliser adjusting frequency, Q and gain
|
andrewm@0
|
10 See textbook Chapter 4: Filter Effects
|
andrewm@0
|
11
|
andrewm@0
|
12 Code by Andrew McPherson, Brecht De Man and Joshua Reiss
|
andrewm@0
|
13
|
andrewm@0
|
14 ---
|
andrewm@0
|
15
|
andrewm@0
|
16 This program is free software: you can redistribute it and/or modify
|
andrewm@0
|
17 it under the terms of the GNU General Public License as published by
|
andrewm@0
|
18 the Free Software Foundation, either version 3 of the License, or
|
andrewm@0
|
19 (at your option) any later version.
|
andrewm@0
|
20
|
andrewm@0
|
21 This program is distributed in the hope that it will be useful,
|
andrewm@0
|
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
andrewm@0
|
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
andrewm@0
|
24 GNU General Public License for more details.
|
andrewm@0
|
25
|
andrewm@0
|
26 You should have received a copy of the GNU General Public License
|
andrewm@0
|
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
andrewm@0
|
28 */
|
andrewm@0
|
29
|
andrewm@0
|
30 #include "PluginProcessor.h"
|
andrewm@0
|
31 #include "PluginEditor.h"
|
andrewm@0
|
32
|
andrewm@0
|
33
|
andrewm@0
|
34 //==============================================================================
|
andrewm@0
|
35 ParametricEQAudioProcessor::ParametricEQAudioProcessor()
|
andrewm@0
|
36 {
|
andrewm@0
|
37 // Set default values:
|
andrewm@0
|
38 centreFrequency_ = 1000.0;
|
andrewm@0
|
39 q_ = 2.0;
|
andrewm@0
|
40 gainDecibels_ = 0.0;
|
andrewm@0
|
41
|
andrewm@0
|
42 // Initialise the filters later when we know how many channels
|
andrewm@0
|
43 eqFilters_ = 0;
|
andrewm@0
|
44 numEqFilters_ = 0;
|
andrewm@0
|
45
|
andrewm@0
|
46 lastUIWidth_ = 550;
|
andrewm@0
|
47 lastUIHeight_ = 100;
|
andrewm@0
|
48 }
|
andrewm@0
|
49
|
andrewm@0
|
50 ParametricEQAudioProcessor::~ParametricEQAudioProcessor()
|
andrewm@0
|
51 {
|
andrewm@0
|
52 }
|
andrewm@0
|
53
|
andrewm@0
|
54 //==============================================================================
|
andrewm@0
|
55 const String ParametricEQAudioProcessor::getName() const
|
andrewm@0
|
56 {
|
andrewm@0
|
57 return JucePlugin_Name;
|
andrewm@0
|
58 }
|
andrewm@0
|
59
|
andrewm@0
|
60 int ParametricEQAudioProcessor::getNumParameters()
|
andrewm@0
|
61 {
|
andrewm@0
|
62 return kNumParameters;
|
andrewm@0
|
63 }
|
andrewm@0
|
64
|
andrewm@0
|
65 float ParametricEQAudioProcessor::getParameter (int index)
|
andrewm@0
|
66 {
|
andrewm@0
|
67 // This method will be called by the host, probably on the audio thread, so
|
andrewm@0
|
68 // it's absolutely time-critical. Don't use critical sections or anything
|
andrewm@0
|
69 // UI-related, or anything at all that may block in any way!
|
andrewm@0
|
70 switch (index)
|
andrewm@0
|
71 {
|
andrewm@0
|
72 case kCentreFrequencyParam: return centreFrequency_;
|
andrewm@0
|
73 case kQParam: return q_;
|
andrewm@0
|
74 case kGainDecibelsParam: return gainDecibels_;
|
andrewm@0
|
75 default: return 0.0f;
|
andrewm@0
|
76 }
|
andrewm@0
|
77 }
|
andrewm@0
|
78
|
andrewm@0
|
79 void ParametricEQAudioProcessor::setParameter (int index, float newValue)
|
andrewm@0
|
80 {
|
andrewm@0
|
81 // This method will be called by the host, probably on the audio thread, so
|
andrewm@0
|
82 // it's absolutely time-critical. Don't use critical sections or anything
|
andrewm@0
|
83 // UI-related, or anything at all that may block in any way!
|
andrewm@0
|
84 switch (index)
|
andrewm@0
|
85 {
|
andrewm@0
|
86 case kCentreFrequencyParam:
|
andrewm@0
|
87 centreFrequency_ = newValue;
|
andrewm@0
|
88 updateEQFilter(getSampleRate());
|
andrewm@0
|
89 break;
|
andrewm@0
|
90 case kQParam:
|
andrewm@0
|
91 q_ = newValue;
|
andrewm@0
|
92 updateEQFilter(getSampleRate());
|
andrewm@0
|
93 break;
|
andrewm@0
|
94 case kGainDecibelsParam:
|
andrewm@0
|
95 gainDecibels_ = newValue;
|
andrewm@0
|
96 updateEQFilter(getSampleRate());
|
andrewm@0
|
97 break;
|
andrewm@0
|
98 default:
|
andrewm@0
|
99 break;
|
andrewm@0
|
100 }
|
andrewm@0
|
101 }
|
andrewm@0
|
102
|
andrewm@0
|
103 const String ParametricEQAudioProcessor::getParameterName (int index)
|
andrewm@0
|
104 {
|
andrewm@0
|
105 switch (index)
|
andrewm@0
|
106 {
|
andrewm@0
|
107 case kCentreFrequencyParam: return "centre frequency";
|
andrewm@0
|
108 case kQParam: return "Q";
|
andrewm@0
|
109 case kGainDecibelsParam: return "gain (dB)";
|
andrewm@0
|
110 default: break;
|
andrewm@0
|
111 }
|
andrewm@0
|
112
|
andrewm@0
|
113 return String::empty;
|
andrewm@0
|
114 }
|
andrewm@0
|
115
|
andrewm@0
|
116 const String ParametricEQAudioProcessor::getParameterText (int index)
|
andrewm@0
|
117 {
|
andrewm@0
|
118 return String (getParameter (index), 2);
|
andrewm@0
|
119 }
|
andrewm@0
|
120
|
andrewm@0
|
121 const String ParametricEQAudioProcessor::getInputChannelName (int channelIndex) const
|
andrewm@0
|
122 {
|
andrewm@0
|
123 return String (channelIndex + 1);
|
andrewm@0
|
124 }
|
andrewm@0
|
125
|
andrewm@0
|
126 const String ParametricEQAudioProcessor::getOutputChannelName (int channelIndex) const
|
andrewm@0
|
127 {
|
andrewm@0
|
128 return String (channelIndex + 1);
|
andrewm@0
|
129 }
|
andrewm@0
|
130
|
andrewm@0
|
131 bool ParametricEQAudioProcessor::isInputChannelStereoPair (int index) const
|
andrewm@0
|
132 {
|
andrewm@0
|
133 return true;
|
andrewm@0
|
134 }
|
andrewm@0
|
135
|
andrewm@0
|
136 bool ParametricEQAudioProcessor::isOutputChannelStereoPair (int index) const
|
andrewm@0
|
137 {
|
andrewm@0
|
138 return true;
|
andrewm@0
|
139 }
|
andrewm@0
|
140
|
andrewm@0
|
141 bool ParametricEQAudioProcessor::silenceInProducesSilenceOut() const
|
andrewm@0
|
142 {
|
andrewm@0
|
143 #if JucePlugin_SilenceInProducesSilenceOut
|
andrewm@0
|
144 return true;
|
andrewm@0
|
145 #else
|
andrewm@0
|
146 return false;
|
andrewm@0
|
147 #endif
|
andrewm@0
|
148 }
|
andrewm@0
|
149
|
andrewm@0
|
150 double ParametricEQAudioProcessor::getTailLengthSeconds() const
|
andrewm@0
|
151 {
|
andrewm@0
|
152 return 0.0;
|
andrewm@0
|
153 }
|
andrewm@0
|
154
|
andrewm@0
|
155 bool ParametricEQAudioProcessor::acceptsMidi() const
|
andrewm@0
|
156 {
|
andrewm@0
|
157 #if JucePlugin_WantsMidiInput
|
andrewm@0
|
158 return true;
|
andrewm@0
|
159 #else
|
andrewm@0
|
160 return false;
|
andrewm@0
|
161 #endif
|
andrewm@0
|
162 }
|
andrewm@0
|
163
|
andrewm@0
|
164 bool ParametricEQAudioProcessor::producesMidi() const
|
andrewm@0
|
165 {
|
andrewm@0
|
166 #if JucePlugin_ProducesMidiOutput
|
andrewm@0
|
167 return true;
|
andrewm@0
|
168 #else
|
andrewm@0
|
169 return false;
|
andrewm@0
|
170 #endif
|
andrewm@0
|
171 }
|
andrewm@0
|
172
|
andrewm@0
|
173 int ParametricEQAudioProcessor::getNumPrograms()
|
andrewm@0
|
174 {
|
andrewm@0
|
175 return 0;
|
andrewm@0
|
176 }
|
andrewm@0
|
177
|
andrewm@0
|
178 int ParametricEQAudioProcessor::getCurrentProgram()
|
andrewm@0
|
179 {
|
andrewm@0
|
180 return 0;
|
andrewm@0
|
181 }
|
andrewm@0
|
182
|
andrewm@0
|
183 void ParametricEQAudioProcessor::setCurrentProgram (int index)
|
andrewm@0
|
184 {
|
andrewm@0
|
185 }
|
andrewm@0
|
186
|
andrewm@0
|
187 const String ParametricEQAudioProcessor::getProgramName (int index)
|
andrewm@0
|
188 {
|
andrewm@0
|
189 return String::empty;
|
andrewm@0
|
190 }
|
andrewm@0
|
191
|
andrewm@0
|
192 void ParametricEQAudioProcessor::changeProgramName (int index, const String& newName)
|
andrewm@0
|
193 {
|
andrewm@0
|
194 }
|
andrewm@0
|
195
|
andrewm@0
|
196 //==============================================================================
|
andrewm@0
|
197 void ParametricEQAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
|
andrewm@0
|
198 {
|
andrewm@0
|
199 // Use this method as the place to do any pre-playback
|
andrewm@0
|
200 // initialisation that you need..
|
andrewm@0
|
201
|
andrewm@0
|
202 // Create as many filters as we have input channels
|
andrewm@0
|
203 numEqFilters_ = getNumInputChannels();
|
andrewm@0
|
204 eqFilters_ = (ParametricEQFilter**)malloc(numEqFilters_ * sizeof(ParametricEQFilter*));
|
andrewm@0
|
205 if(eqFilters_ == 0)
|
andrewm@0
|
206 numEqFilters_ = 0;
|
andrewm@0
|
207 else {
|
andrewm@0
|
208 for(int i = 0; i < numEqFilters_; i++)
|
andrewm@0
|
209 eqFilters_[i] = new ParametricEQFilter;
|
andrewm@0
|
210 }
|
andrewm@0
|
211
|
andrewm@0
|
212 // Update the filter settings to work with the current parameters and sample rate
|
andrewm@0
|
213 updateEQFilter(sampleRate);
|
andrewm@0
|
214 }
|
andrewm@0
|
215
|
andrewm@0
|
216 void ParametricEQAudioProcessor::releaseResources()
|
andrewm@0
|
217 {
|
andrewm@0
|
218 // When playback stops, you can use this as an opportunity to free up any
|
andrewm@0
|
219 // spare memory, etc.
|
andrewm@0
|
220 for(int i = 0; i < numEqFilters_; i++)
|
andrewm@0
|
221 delete eqFilters_[i];
|
andrewm@0
|
222 if(numEqFilters_ != 0)
|
andrewm@0
|
223 free(eqFilters_);
|
andrewm@0
|
224 numEqFilters_ = 0;
|
andrewm@0
|
225 eqFilters_ = 0;
|
andrewm@0
|
226 }
|
andrewm@0
|
227
|
andrewm@0
|
228 void ParametricEQAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
|
andrewm@0
|
229 {
|
andrewm@0
|
230 // Helpful information about this block of samples:
|
andrewm@0
|
231 const int numInputChannels = getNumInputChannels(); // How many input channels for our effect?
|
andrewm@0
|
232 const int numOutputChannels = getNumOutputChannels(); // How many output channels for our effect?
|
andrewm@0
|
233 const int numSamples = buffer.getNumSamples(); // How many samples in the buffer for this block?
|
andrewm@0
|
234 int channel;
|
andrewm@0
|
235
|
andrewm@0
|
236 // Go through each channel of audio that's passed in
|
andrewm@0
|
237
|
andrewm@0
|
238 for (channel = 0; channel < jmin((int32)numInputChannels, numEqFilters_); ++channel)
|
andrewm@0
|
239 {
|
andrewm@0
|
240 // channelData is an array of length numSamples which contains the audio for one channel
|
b@1
|
241 float* channelData = buffer.getWritePointer(channel);
|
andrewm@0
|
242
|
andrewm@0
|
243 // Run the samples through the IIR filter whose coefficients define the parametric
|
andrewm@0
|
244 // equaliser. See juce_IIRFilter.cpp for the implementation.
|
andrewm@0
|
245 eqFilters_[channel]->processSamples(channelData, numSamples);
|
andrewm@0
|
246 }
|
andrewm@0
|
247
|
andrewm@0
|
248 // Go through the remaining channels. In case we have more outputs
|
andrewm@0
|
249 // than inputs, or there aren't enough filters, we'll clear any
|
andrewm@0
|
250 // remaining output channels (which could otherwise contain garbage)
|
andrewm@0
|
251 while(channel < numOutputChannels)
|
andrewm@0
|
252 {
|
andrewm@0
|
253 buffer.clear (channel++, 0, buffer.getNumSamples());
|
andrewm@0
|
254 }
|
andrewm@0
|
255 }
|
andrewm@0
|
256
|
andrewm@0
|
257 //==============================================================================
|
andrewm@0
|
258 bool ParametricEQAudioProcessor::hasEditor() const
|
andrewm@0
|
259 {
|
andrewm@0
|
260 return true; // (change this to false if you choose to not supply an editor)
|
andrewm@0
|
261 }
|
andrewm@0
|
262
|
andrewm@0
|
263 AudioProcessorEditor* ParametricEQAudioProcessor::createEditor()
|
andrewm@0
|
264 {
|
andrewm@0
|
265 return new ParametricEQAudioProcessorEditor (this);
|
andrewm@0
|
266 }
|
andrewm@0
|
267
|
andrewm@0
|
268 //==============================================================================
|
andrewm@0
|
269 void ParametricEQAudioProcessor::getStateInformation (MemoryBlock& destData)
|
andrewm@0
|
270 {
|
andrewm@0
|
271 // You should use this method to store your parameters in the memory block.
|
andrewm@0
|
272 // You could do that either as raw data, or use the XML or ValueTree classes
|
andrewm@0
|
273 // as intermediaries to make it easy to save and load complex data.
|
andrewm@0
|
274
|
andrewm@0
|
275 // Create an outer XML element..
|
andrewm@0
|
276 XmlElement xml("C4DMPLUGINSETTINGS");
|
andrewm@0
|
277
|
andrewm@0
|
278 // add some attributes to it..
|
andrewm@0
|
279 xml.setAttribute("uiWidth", lastUIWidth_);
|
andrewm@0
|
280 xml.setAttribute("uiHeight", lastUIHeight_);
|
andrewm@0
|
281 xml.setAttribute("centreFrequency", centreFrequency_);
|
andrewm@0
|
282 xml.setAttribute("q", q_);
|
andrewm@0
|
283 xml.setAttribute("gainDecibels", gainDecibels_);
|
andrewm@0
|
284
|
andrewm@0
|
285 // then use this helper function to stuff it into the binary blob and return it..
|
andrewm@0
|
286 copyXmlToBinary(xml, destData);
|
andrewm@0
|
287 }
|
andrewm@0
|
288
|
andrewm@0
|
289 void ParametricEQAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
|
andrewm@0
|
290 {
|
andrewm@0
|
291 // You should use this method to restore your parameters from this memory block,
|
andrewm@0
|
292 // whose contents will have been created by the getStateInformation() call.
|
andrewm@0
|
293
|
andrewm@0
|
294 // This getXmlFromBinary() helper function retrieves our XML from the binary blob..
|
andrewm@0
|
295 ScopedPointer<XmlElement> xmlState (getXmlFromBinary (data, sizeInBytes));
|
andrewm@0
|
296
|
andrewm@0
|
297 if(xmlState != 0)
|
andrewm@0
|
298 {
|
andrewm@0
|
299 // make sure that it's actually our type of XML object..
|
andrewm@0
|
300 if(xmlState->hasTagName("C4DMPLUGINSETTINGS"))
|
andrewm@0
|
301 {
|
andrewm@0
|
302 // ok, now pull out our parameters..
|
andrewm@0
|
303 lastUIWidth_ = xmlState->getIntAttribute("uiWidth", lastUIWidth_);
|
andrewm@0
|
304 lastUIHeight_ = xmlState->getIntAttribute("uiHeight", lastUIHeight_);
|
andrewm@0
|
305
|
andrewm@0
|
306 centreFrequency_ = (float)xmlState->getDoubleAttribute("centreFrequency", centreFrequency_);
|
andrewm@0
|
307 q_ = (float)xmlState->getDoubleAttribute("q", q_);
|
andrewm@0
|
308 gainDecibels_ = (float)xmlState->getDoubleAttribute("gainDecibels", gainDecibels_);
|
andrewm@0
|
309 updateEQFilter(getSampleRate());
|
andrewm@0
|
310 }
|
andrewm@0
|
311 }
|
andrewm@0
|
312 }
|
andrewm@0
|
313
|
andrewm@0
|
314 //==============================================================================
|
andrewm@0
|
315 // Update the coefficients of the parametric equaliser filter
|
andrewm@0
|
316 void ParametricEQAudioProcessor::updateEQFilter(float sampleRate)
|
andrewm@0
|
317 {
|
andrewm@0
|
318 for(int i = 0; i < numEqFilters_; i++)
|
andrewm@0
|
319 eqFilters_[i]->makeParametric(2.0 * M_PI * centreFrequency_ / sampleRate,
|
andrewm@0
|
320 q_, powf(10.0f, gainDecibels_ / 20.0f));
|
andrewm@0
|
321 }
|
andrewm@0
|
322
|
andrewm@0
|
323 //==============================================================================
|
andrewm@0
|
324 // This creates new instances of the plugin..
|
andrewm@0
|
325 AudioProcessor* JUCE_CALLTYPE createPluginFilter()
|
andrewm@0
|
326 {
|
andrewm@0
|
327 return new ParametricEQAudioProcessor();
|
andrewm@0
|
328 }
|