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 Wah-Wah: filter effect using variable-frequency filter
|
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 // The filter will produce a resonant peak of amplitude Q; bring everything
|
andrewm@0
|
34 // down somewhat to compensate, though try to maintain some perceptual balance
|
andrewm@0
|
35 // of being similar loudness. (This factor has been chosen somewhat arbitrarily.)
|
andrewm@0
|
36 const double kWahwahFilterGain = 0.5;
|
andrewm@0
|
37
|
andrewm@0
|
38 //==============================================================================
|
andrewm@0
|
39 WahwahAudioProcessor::WahwahAudioProcessor()
|
andrewm@0
|
40 {
|
andrewm@0
|
41 // Set default values:
|
andrewm@0
|
42 centreFrequency_ = 400.0;
|
andrewm@0
|
43 q_ = 5.0;
|
andrewm@0
|
44
|
andrewm@0
|
45 // Initialise the filters later when we know how many channels
|
andrewm@0
|
46 wahFilters_ = 0;
|
andrewm@0
|
47 numWahFilters_ = 0;
|
andrewm@0
|
48
|
andrewm@0
|
49 inverseSampleRate_ = 1.0/44100.0; // start with a sensible default
|
andrewm@0
|
50
|
andrewm@0
|
51 lastUIWidth_ = 370;
|
andrewm@0
|
52 lastUIHeight_ = 100;
|
andrewm@0
|
53 }
|
andrewm@0
|
54
|
andrewm@0
|
55 WahwahAudioProcessor::~WahwahAudioProcessor()
|
andrewm@0
|
56 {
|
andrewm@0
|
57 }
|
andrewm@0
|
58
|
andrewm@0
|
59 //==============================================================================
|
andrewm@0
|
60 const String WahwahAudioProcessor::getName() const
|
andrewm@0
|
61 {
|
andrewm@0
|
62 return JucePlugin_Name;
|
andrewm@0
|
63 }
|
andrewm@0
|
64
|
andrewm@0
|
65 int WahwahAudioProcessor::getNumParameters()
|
andrewm@0
|
66 {
|
andrewm@0
|
67 return kNumParameters;
|
andrewm@0
|
68 }
|
andrewm@0
|
69
|
andrewm@0
|
70 float WahwahAudioProcessor::getParameter (int index)
|
andrewm@0
|
71 {
|
andrewm@0
|
72 // This method will be called by the host, probably on the audio thread, so
|
andrewm@0
|
73 // it's absolutely time-critical. Don't use critical sections or anything
|
andrewm@0
|
74 // UI-related, or anything at all that may block in any way!
|
andrewm@0
|
75 switch (index)
|
andrewm@0
|
76 {
|
andrewm@0
|
77 case kCentreFrequencyParam: return centreFrequency_;
|
andrewm@0
|
78 case kQParam: return q_;
|
andrewm@0
|
79 default: return 0.0f;
|
andrewm@0
|
80 }
|
andrewm@0
|
81 }
|
andrewm@0
|
82
|
andrewm@0
|
83 void WahwahAudioProcessor::setParameter (int index, float newValue)
|
andrewm@0
|
84 {
|
andrewm@0
|
85 // This method will be called by the host, probably on the audio thread, so
|
andrewm@0
|
86 // it's absolutely time-critical. Don't use critical sections or anything
|
andrewm@0
|
87 // UI-related, or anything at all that may block in any way!
|
andrewm@0
|
88 switch (index)
|
andrewm@0
|
89 {
|
andrewm@0
|
90 case kCentreFrequencyParam:
|
andrewm@0
|
91 centreFrequency_ = newValue;
|
andrewm@0
|
92 updateFilter();
|
andrewm@0
|
93 break;
|
andrewm@0
|
94 case kQParam:
|
andrewm@0
|
95 q_ = newValue;
|
andrewm@0
|
96 updateFilter();
|
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 WahwahAudioProcessor::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 default: break;
|
andrewm@0
|
110 }
|
andrewm@0
|
111
|
andrewm@0
|
112 return String::empty;
|
andrewm@0
|
113 }
|
andrewm@0
|
114
|
andrewm@0
|
115 const String WahwahAudioProcessor::getParameterText (int index)
|
andrewm@0
|
116 {
|
andrewm@0
|
117 return String (getParameter (index), 2);
|
andrewm@0
|
118 }
|
andrewm@0
|
119
|
andrewm@0
|
120 const String WahwahAudioProcessor::getInputChannelName (int channelIndex) const
|
andrewm@0
|
121 {
|
andrewm@0
|
122 return String (channelIndex + 1);
|
andrewm@0
|
123 }
|
andrewm@0
|
124
|
andrewm@0
|
125 const String WahwahAudioProcessor::getOutputChannelName (int channelIndex) const
|
andrewm@0
|
126 {
|
andrewm@0
|
127 return String (channelIndex + 1);
|
andrewm@0
|
128 }
|
andrewm@0
|
129
|
andrewm@0
|
130 bool WahwahAudioProcessor::isInputChannelStereoPair (int index) const
|
andrewm@0
|
131 {
|
andrewm@0
|
132 return true;
|
andrewm@0
|
133 }
|
andrewm@0
|
134
|
andrewm@0
|
135 bool WahwahAudioProcessor::isOutputChannelStereoPair (int index) const
|
andrewm@0
|
136 {
|
andrewm@0
|
137 return true;
|
andrewm@0
|
138 }
|
andrewm@0
|
139
|
andrewm@0
|
140 bool WahwahAudioProcessor::silenceInProducesSilenceOut() const
|
andrewm@0
|
141 {
|
andrewm@0
|
142 #if JucePlugin_SilenceInProducesSilenceOut
|
andrewm@0
|
143 return true;
|
andrewm@0
|
144 #else
|
andrewm@0
|
145 return false;
|
andrewm@0
|
146 #endif
|
andrewm@0
|
147 }
|
andrewm@0
|
148
|
andrewm@0
|
149 double WahwahAudioProcessor::getTailLengthSeconds() const
|
andrewm@0
|
150 {
|
andrewm@0
|
151 return 0.0;
|
andrewm@0
|
152 }
|
andrewm@0
|
153
|
andrewm@0
|
154 bool WahwahAudioProcessor::acceptsMidi() const
|
andrewm@0
|
155 {
|
andrewm@0
|
156 #if JucePlugin_WantsMidiInput
|
andrewm@0
|
157 return true;
|
andrewm@0
|
158 #else
|
andrewm@0
|
159 return false;
|
andrewm@0
|
160 #endif
|
andrewm@0
|
161 }
|
andrewm@0
|
162
|
andrewm@0
|
163 bool WahwahAudioProcessor::producesMidi() const
|
andrewm@0
|
164 {
|
andrewm@0
|
165 #if JucePlugin_ProducesMidiOutput
|
andrewm@0
|
166 return true;
|
andrewm@0
|
167 #else
|
andrewm@0
|
168 return false;
|
andrewm@0
|
169 #endif
|
andrewm@0
|
170 }
|
andrewm@0
|
171
|
andrewm@0
|
172 int WahwahAudioProcessor::getNumPrograms()
|
andrewm@0
|
173 {
|
andrewm@0
|
174 return 0;
|
andrewm@0
|
175 }
|
andrewm@0
|
176
|
andrewm@0
|
177 int WahwahAudioProcessor::getCurrentProgram()
|
andrewm@0
|
178 {
|
andrewm@0
|
179 return 0;
|
andrewm@0
|
180 }
|
andrewm@0
|
181
|
andrewm@0
|
182 void WahwahAudioProcessor::setCurrentProgram (int index)
|
andrewm@0
|
183 {
|
andrewm@0
|
184 }
|
andrewm@0
|
185
|
andrewm@0
|
186 const String WahwahAudioProcessor::getProgramName (int index)
|
andrewm@0
|
187 {
|
andrewm@0
|
188 return String::empty;
|
andrewm@0
|
189 }
|
andrewm@0
|
190
|
andrewm@0
|
191 void WahwahAudioProcessor::changeProgramName (int index, const String& newName)
|
andrewm@0
|
192 {
|
andrewm@0
|
193 }
|
andrewm@0
|
194
|
andrewm@0
|
195 //==============================================================================
|
andrewm@0
|
196 void WahwahAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
|
andrewm@0
|
197 {
|
andrewm@0
|
198 // Use this method as the place to do any pre-playback
|
andrewm@0
|
199 // initialisation that you need..
|
andrewm@0
|
200
|
andrewm@0
|
201 // Create as many filters as we have input channels
|
andrewm@0
|
202 numWahFilters_ = getNumInputChannels();
|
andrewm@0
|
203 wahFilters_ = (ResonantLowpassFilter**)malloc(numWahFilters_ * sizeof(ResonantLowpassFilter*));
|
andrewm@0
|
204 if(wahFilters_ == 0)
|
andrewm@0
|
205 numWahFilters_ = 0;
|
andrewm@0
|
206 else {
|
andrewm@0
|
207 for(int i = 0; i < numWahFilters_; i++)
|
andrewm@0
|
208 wahFilters_[i] = new ResonantLowpassFilter;
|
andrewm@0
|
209 }
|
andrewm@0
|
210
|
andrewm@0
|
211 inverseSampleRate_ = 1.0 / sampleRate;
|
andrewm@0
|
212
|
andrewm@0
|
213 // Update the filter settings to work with the current parameters and sample rate
|
andrewm@0
|
214 updateFilter();
|
andrewm@0
|
215 }
|
andrewm@0
|
216
|
andrewm@0
|
217 void WahwahAudioProcessor::releaseResources()
|
andrewm@0
|
218 {
|
andrewm@0
|
219 // When playback stops, you can use this as an opportunity to free up any
|
andrewm@0
|
220 // spare memory, etc.
|
andrewm@0
|
221 for(int i = 0; i < numWahFilters_; i++)
|
andrewm@0
|
222 delete wahFilters_[i];
|
andrewm@0
|
223 if(numWahFilters_ != 0)
|
andrewm@0
|
224 free(wahFilters_);
|
andrewm@0
|
225 numWahFilters_ = 0;
|
andrewm@0
|
226 wahFilters_ = 0;
|
andrewm@0
|
227 }
|
andrewm@0
|
228
|
andrewm@0
|
229 void WahwahAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
|
andrewm@0
|
230 {
|
andrewm@0
|
231 // Helpful information about this block of samples:
|
andrewm@0
|
232 const int numInputChannels = getNumInputChannels(); // How many input channels for our effect?
|
andrewm@0
|
233 const int numOutputChannels = getNumOutputChannels(); // How many output channels for our effect?
|
andrewm@0
|
234 const int numSamples = buffer.getNumSamples(); // How many samples in the buffer for this block?
|
andrewm@0
|
235 int channel;
|
andrewm@0
|
236
|
andrewm@0
|
237 // Go through each channel of audio that's passed in
|
andrewm@0
|
238
|
andrewm@0
|
239 for (channel = 0; channel < jmin(numInputChannels, numWahFilters_); ++channel)
|
andrewm@0
|
240 {
|
andrewm@0
|
241 // channelData is an array of length numSamples which contains the audio for one channel
|
b@1
|
242 float* channelData = buffer.getWritePointer(channel);
|
andrewm@0
|
243
|
andrewm@0
|
244 // Run the samples through the IIR filter whose coefficients define the parametric
|
andrewm@0
|
245 // equaliser. See juce_IIRFilter.cpp for the implementation.
|
andrewm@0
|
246 wahFilters_[channel]->processSamples(channelData, numSamples);
|
andrewm@0
|
247 }
|
andrewm@0
|
248
|
andrewm@0
|
249 // Go through the remaining channels. In case we have more outputs
|
andrewm@0
|
250 // than inputs, or there aren't enough filters, we'll clear any
|
andrewm@0
|
251 // remaining output channels (which could otherwise contain garbage)
|
andrewm@0
|
252 while(channel < numOutputChannels)
|
andrewm@0
|
253 {
|
andrewm@0
|
254 buffer.clear (channel++, 0, buffer.getNumSamples());
|
andrewm@0
|
255 }
|
andrewm@0
|
256 }
|
andrewm@0
|
257
|
andrewm@0
|
258 //==============================================================================
|
andrewm@0
|
259 bool WahwahAudioProcessor::hasEditor() const
|
andrewm@0
|
260 {
|
andrewm@0
|
261 return true; // (change this to false if you choose to not supply an editor)
|
andrewm@0
|
262 }
|
andrewm@0
|
263
|
andrewm@0
|
264 AudioProcessorEditor* WahwahAudioProcessor::createEditor()
|
andrewm@0
|
265 {
|
andrewm@0
|
266 return new WahwahAudioProcessorEditor (this);
|
andrewm@0
|
267 }
|
andrewm@0
|
268
|
andrewm@0
|
269 //==============================================================================
|
andrewm@0
|
270 void WahwahAudioProcessor::getStateInformation (MemoryBlock& destData)
|
andrewm@0
|
271 {
|
andrewm@0
|
272 // You should use this method to store your parameters in the memory block.
|
andrewm@0
|
273 // You could do that either as raw data, or use the XML or ValueTree classes
|
andrewm@0
|
274 // as intermediaries to make it easy to save and load complex data.
|
andrewm@0
|
275
|
andrewm@0
|
276 // Create an outer XML element..
|
andrewm@0
|
277 XmlElement xml("C4DMPLUGINSETTINGS");
|
andrewm@0
|
278
|
andrewm@0
|
279 // add some attributes to it..
|
andrewm@0
|
280 xml.setAttribute("uiWidth", lastUIWidth_);
|
andrewm@0
|
281 xml.setAttribute("uiHeight", lastUIHeight_);
|
andrewm@0
|
282 xml.setAttribute("centreFrequency", centreFrequency_);
|
andrewm@0
|
283 xml.setAttribute("q", q_);
|
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 WahwahAudioProcessor::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 q_ = (float)xmlState->getDoubleAttribute("q", q_);
|
andrewm@0
|
307 centreFrequency_ = (float)xmlState->getDoubleAttribute("centreFrequency", centreFrequency_);
|
andrewm@0
|
308 inverseSampleRate_ = 1.0 / getSampleRate();
|
andrewm@0
|
309 updateFilter();
|
andrewm@0
|
310 }
|
andrewm@0
|
311 }
|
andrewm@0
|
312 }
|
andrewm@0
|
313
|
andrewm@0
|
314 //==============================================================================
|
andrewm@0
|
315 // Update the coefficients of the resonant lowpass filter
|
andrewm@0
|
316 void WahwahAudioProcessor::updateFilter()
|
andrewm@0
|
317 {
|
andrewm@0
|
318 for(int i = 0; i < numWahFilters_; i++)
|
andrewm@0
|
319 wahFilters_[i]->makeResonantLowpass(inverseSampleRate_, centreFrequency_, q_,
|
andrewm@0
|
320 kWahwahFilterGain);
|
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 WahwahAudioProcessor();
|
andrewm@0
|
328 }
|