andrewm@0: /* andrewm@0: This code accompanies the textbook: andrewm@0: andrewm@0: Digital Audio Effects: Theory, Implementation and Application andrewm@0: Joshua D. Reiss and Andrew P. McPherson andrewm@0: andrewm@0: --- andrewm@0: andrewm@0: Parametric EQ: parametric equaliser adjusting frequency, Q and gain andrewm@0: See textbook Chapter 4: Filter Effects andrewm@0: andrewm@0: Code by Andrew McPherson, Brecht De Man and Joshua Reiss andrewm@0: andrewm@0: --- andrewm@0: andrewm@0: This program is free software: you can redistribute it and/or modify andrewm@0: it under the terms of the GNU General Public License as published by andrewm@0: the Free Software Foundation, either version 3 of the License, or andrewm@0: (at your option) any later version. andrewm@0: andrewm@0: This program is distributed in the hope that it will be useful, andrewm@0: but WITHOUT ANY WARRANTY; without even the implied warranty of andrewm@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrewm@0: GNU General Public License for more details. andrewm@0: andrewm@0: You should have received a copy of the GNU General Public License andrewm@0: along with this program. If not, see . andrewm@0: */ andrewm@0: andrewm@0: #include "PluginProcessor.h" andrewm@0: #include "PluginEditor.h" andrewm@0: andrewm@0: andrewm@0: //============================================================================== andrewm@0: ParametricEQAudioProcessor::ParametricEQAudioProcessor() andrewm@0: { andrewm@0: // Set default values: andrewm@0: centreFrequency_ = 1000.0; andrewm@0: q_ = 2.0; andrewm@0: gainDecibels_ = 0.0; andrewm@0: andrewm@0: // Initialise the filters later when we know how many channels andrewm@0: eqFilters_ = 0; andrewm@0: numEqFilters_ = 0; andrewm@0: andrewm@0: lastUIWidth_ = 550; andrewm@0: lastUIHeight_ = 100; andrewm@0: } andrewm@0: andrewm@0: ParametricEQAudioProcessor::~ParametricEQAudioProcessor() andrewm@0: { andrewm@0: } andrewm@0: andrewm@0: //============================================================================== andrewm@0: const String ParametricEQAudioProcessor::getName() const andrewm@0: { andrewm@0: return JucePlugin_Name; andrewm@0: } andrewm@0: andrewm@0: int ParametricEQAudioProcessor::getNumParameters() andrewm@0: { andrewm@0: return kNumParameters; andrewm@0: } andrewm@0: andrewm@0: float ParametricEQAudioProcessor::getParameter (int index) andrewm@0: { andrewm@0: // This method will be called by the host, probably on the audio thread, so andrewm@0: // it's absolutely time-critical. Don't use critical sections or anything andrewm@0: // UI-related, or anything at all that may block in any way! andrewm@0: switch (index) andrewm@0: { andrewm@0: case kCentreFrequencyParam: return centreFrequency_; andrewm@0: case kQParam: return q_; andrewm@0: case kGainDecibelsParam: return gainDecibels_; andrewm@0: default: return 0.0f; andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: void ParametricEQAudioProcessor::setParameter (int index, float newValue) andrewm@0: { andrewm@0: // This method will be called by the host, probably on the audio thread, so andrewm@0: // it's absolutely time-critical. Don't use critical sections or anything andrewm@0: // UI-related, or anything at all that may block in any way! andrewm@0: switch (index) andrewm@0: { andrewm@0: case kCentreFrequencyParam: andrewm@0: centreFrequency_ = newValue; andrewm@0: updateEQFilter(getSampleRate()); andrewm@0: break; andrewm@0: case kQParam: andrewm@0: q_ = newValue; andrewm@0: updateEQFilter(getSampleRate()); andrewm@0: break; andrewm@0: case kGainDecibelsParam: andrewm@0: gainDecibels_ = newValue; andrewm@0: updateEQFilter(getSampleRate()); andrewm@0: break; andrewm@0: default: andrewm@0: break; andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: const String ParametricEQAudioProcessor::getParameterName (int index) andrewm@0: { andrewm@0: switch (index) andrewm@0: { andrewm@0: case kCentreFrequencyParam: return "centre frequency"; andrewm@0: case kQParam: return "Q"; andrewm@0: case kGainDecibelsParam: return "gain (dB)"; andrewm@0: default: break; andrewm@0: } andrewm@0: andrewm@0: return String::empty; andrewm@0: } andrewm@0: andrewm@0: const String ParametricEQAudioProcessor::getParameterText (int index) andrewm@0: { andrewm@0: return String (getParameter (index), 2); andrewm@0: } andrewm@0: andrewm@0: const String ParametricEQAudioProcessor::getInputChannelName (int channelIndex) const andrewm@0: { andrewm@0: return String (channelIndex + 1); andrewm@0: } andrewm@0: andrewm@0: const String ParametricEQAudioProcessor::getOutputChannelName (int channelIndex) const andrewm@0: { andrewm@0: return String (channelIndex + 1); andrewm@0: } andrewm@0: andrewm@0: bool ParametricEQAudioProcessor::isInputChannelStereoPair (int index) const andrewm@0: { andrewm@0: return true; andrewm@0: } andrewm@0: andrewm@0: bool ParametricEQAudioProcessor::isOutputChannelStereoPair (int index) const andrewm@0: { andrewm@0: return true; andrewm@0: } andrewm@0: andrewm@0: bool ParametricEQAudioProcessor::silenceInProducesSilenceOut() const andrewm@0: { andrewm@0: #if JucePlugin_SilenceInProducesSilenceOut andrewm@0: return true; andrewm@0: #else andrewm@0: return false; andrewm@0: #endif andrewm@0: } andrewm@0: andrewm@0: double ParametricEQAudioProcessor::getTailLengthSeconds() const andrewm@0: { andrewm@0: return 0.0; andrewm@0: } andrewm@0: andrewm@0: bool ParametricEQAudioProcessor::acceptsMidi() const andrewm@0: { andrewm@0: #if JucePlugin_WantsMidiInput andrewm@0: return true; andrewm@0: #else andrewm@0: return false; andrewm@0: #endif andrewm@0: } andrewm@0: andrewm@0: bool ParametricEQAudioProcessor::producesMidi() const andrewm@0: { andrewm@0: #if JucePlugin_ProducesMidiOutput andrewm@0: return true; andrewm@0: #else andrewm@0: return false; andrewm@0: #endif andrewm@0: } andrewm@0: andrewm@0: int ParametricEQAudioProcessor::getNumPrograms() andrewm@0: { andrewm@0: return 0; andrewm@0: } andrewm@0: andrewm@0: int ParametricEQAudioProcessor::getCurrentProgram() andrewm@0: { andrewm@0: return 0; andrewm@0: } andrewm@0: andrewm@0: void ParametricEQAudioProcessor::setCurrentProgram (int index) andrewm@0: { andrewm@0: } andrewm@0: andrewm@0: const String ParametricEQAudioProcessor::getProgramName (int index) andrewm@0: { andrewm@0: return String::empty; andrewm@0: } andrewm@0: andrewm@0: void ParametricEQAudioProcessor::changeProgramName (int index, const String& newName) andrewm@0: { andrewm@0: } andrewm@0: andrewm@0: //============================================================================== andrewm@0: void ParametricEQAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock) andrewm@0: { andrewm@0: // Use this method as the place to do any pre-playback andrewm@0: // initialisation that you need.. andrewm@0: andrewm@0: // Create as many filters as we have input channels andrewm@0: numEqFilters_ = getNumInputChannels(); andrewm@0: eqFilters_ = (ParametricEQFilter**)malloc(numEqFilters_ * sizeof(ParametricEQFilter*)); andrewm@0: if(eqFilters_ == 0) andrewm@0: numEqFilters_ = 0; andrewm@0: else { andrewm@0: for(int i = 0; i < numEqFilters_; i++) andrewm@0: eqFilters_[i] = new ParametricEQFilter; andrewm@0: } andrewm@0: andrewm@0: // Update the filter settings to work with the current parameters and sample rate andrewm@0: updateEQFilter(sampleRate); andrewm@0: } andrewm@0: andrewm@0: void ParametricEQAudioProcessor::releaseResources() andrewm@0: { andrewm@0: // When playback stops, you can use this as an opportunity to free up any andrewm@0: // spare memory, etc. andrewm@0: for(int i = 0; i < numEqFilters_; i++) andrewm@0: delete eqFilters_[i]; andrewm@0: if(numEqFilters_ != 0) andrewm@0: free(eqFilters_); andrewm@0: numEqFilters_ = 0; andrewm@0: eqFilters_ = 0; andrewm@0: } andrewm@0: andrewm@0: void ParametricEQAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages) andrewm@0: { andrewm@0: // Helpful information about this block of samples: andrewm@0: const int numInputChannels = getNumInputChannels(); // How many input channels for our effect? andrewm@0: const int numOutputChannels = getNumOutputChannels(); // How many output channels for our effect? andrewm@0: const int numSamples = buffer.getNumSamples(); // How many samples in the buffer for this block? andrewm@0: int channel; andrewm@0: andrewm@0: // Go through each channel of audio that's passed in andrewm@0: andrewm@0: for (channel = 0; channel < jmin((int32)numInputChannels, numEqFilters_); ++channel) andrewm@0: { andrewm@0: // channelData is an array of length numSamples which contains the audio for one channel b@1: float* channelData = buffer.getWritePointer(channel); andrewm@0: andrewm@0: // Run the samples through the IIR filter whose coefficients define the parametric andrewm@0: // equaliser. See juce_IIRFilter.cpp for the implementation. andrewm@0: eqFilters_[channel]->processSamples(channelData, numSamples); andrewm@0: } andrewm@0: andrewm@0: // Go through the remaining channels. In case we have more outputs andrewm@0: // than inputs, or there aren't enough filters, we'll clear any andrewm@0: // remaining output channels (which could otherwise contain garbage) andrewm@0: while(channel < numOutputChannels) andrewm@0: { andrewm@0: buffer.clear (channel++, 0, buffer.getNumSamples()); andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: //============================================================================== andrewm@0: bool ParametricEQAudioProcessor::hasEditor() const andrewm@0: { andrewm@0: return true; // (change this to false if you choose to not supply an editor) andrewm@0: } andrewm@0: andrewm@0: AudioProcessorEditor* ParametricEQAudioProcessor::createEditor() andrewm@0: { andrewm@0: return new ParametricEQAudioProcessorEditor (this); andrewm@0: } andrewm@0: andrewm@0: //============================================================================== andrewm@0: void ParametricEQAudioProcessor::getStateInformation (MemoryBlock& destData) andrewm@0: { andrewm@0: // You should use this method to store your parameters in the memory block. andrewm@0: // You could do that either as raw data, or use the XML or ValueTree classes andrewm@0: // as intermediaries to make it easy to save and load complex data. andrewm@0: andrewm@0: // Create an outer XML element.. andrewm@0: XmlElement xml("C4DMPLUGINSETTINGS"); andrewm@0: andrewm@0: // add some attributes to it.. andrewm@0: xml.setAttribute("uiWidth", lastUIWidth_); andrewm@0: xml.setAttribute("uiHeight", lastUIHeight_); andrewm@0: xml.setAttribute("centreFrequency", centreFrequency_); andrewm@0: xml.setAttribute("q", q_); andrewm@0: xml.setAttribute("gainDecibels", gainDecibels_); andrewm@0: andrewm@0: // then use this helper function to stuff it into the binary blob and return it.. andrewm@0: copyXmlToBinary(xml, destData); andrewm@0: } andrewm@0: andrewm@0: void ParametricEQAudioProcessor::setStateInformation (const void* data, int sizeInBytes) andrewm@0: { andrewm@0: // You should use this method to restore your parameters from this memory block, andrewm@0: // whose contents will have been created by the getStateInformation() call. andrewm@0: andrewm@0: // This getXmlFromBinary() helper function retrieves our XML from the binary blob.. andrewm@0: ScopedPointer xmlState (getXmlFromBinary (data, sizeInBytes)); andrewm@0: andrewm@0: if(xmlState != 0) andrewm@0: { andrewm@0: // make sure that it's actually our type of XML object.. andrewm@0: if(xmlState->hasTagName("C4DMPLUGINSETTINGS")) andrewm@0: { andrewm@0: // ok, now pull out our parameters.. andrewm@0: lastUIWidth_ = xmlState->getIntAttribute("uiWidth", lastUIWidth_); andrewm@0: lastUIHeight_ = xmlState->getIntAttribute("uiHeight", lastUIHeight_); andrewm@0: andrewm@0: centreFrequency_ = (float)xmlState->getDoubleAttribute("centreFrequency", centreFrequency_); andrewm@0: q_ = (float)xmlState->getDoubleAttribute("q", q_); andrewm@0: gainDecibels_ = (float)xmlState->getDoubleAttribute("gainDecibels", gainDecibels_); andrewm@0: updateEQFilter(getSampleRate()); andrewm@0: } andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: //============================================================================== andrewm@0: // Update the coefficients of the parametric equaliser filter andrewm@0: void ParametricEQAudioProcessor::updateEQFilter(float sampleRate) andrewm@0: { andrewm@0: for(int i = 0; i < numEqFilters_; i++) andrewm@0: eqFilters_[i]->makeParametric(2.0 * M_PI * centreFrequency_ / sampleRate, andrewm@0: q_, powf(10.0f, gainDecibels_ / 20.0f)); andrewm@0: } andrewm@0: andrewm@0: //============================================================================== andrewm@0: // This creates new instances of the plugin.. andrewm@0: AudioProcessor* JUCE_CALLTYPE createPluginFilter() andrewm@0: { andrewm@0: return new ParametricEQAudioProcessor(); andrewm@0: }