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: Auto-Wah: LFO or envelope-operated wah effect 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: // The filter will produce a resonant peak of amplitude Q; bring everything andrewm@0: // down somewhat to compensate, though try to maintain some perceptual balance andrewm@0: // of being similar loudness. (This factor has been chosen somewhat arbitrarily.) andrewm@0: const double kWahwahFilterGain = 0.5; andrewm@0: andrewm@0: //============================================================================== andrewm@0: AutoWahAudioProcessor::AutoWahAudioProcessor() andrewm@0: { andrewm@0: // Set default values: andrewm@0: baseFrequency_ = 350.0; andrewm@0: q_ = 5.0; andrewm@0: lfoFrequency_ = 2.0; andrewm@0: lfoWidth_ = 1000.0; andrewm@0: envelopeWidth_ = 0.0; andrewm@0: envelopeAttack_ = 0.005; andrewm@0: envelopeDecay_ = 0.1; andrewm@0: andrewm@0: // Initialise the filters later when we know how many channels andrewm@0: wahFilters_ = 0; andrewm@0: numWahFilters_ = 0; andrewm@0: envelopes_ = 0; andrewm@0: numEnvelopes_ = 0; andrewm@0: attackMultiplier_ = 1.0; andrewm@0: decayMultiplier_ = 0.0; andrewm@0: andrewm@0: inverseSampleRate_ = 1.0/44100.0; // start with a sensible default andrewm@0: andrewm@0: lastUIWidth_ = 550; andrewm@0: lastUIHeight_ = 200; andrewm@0: } andrewm@0: andrewm@0: AutoWahAudioProcessor::~AutoWahAudioProcessor() andrewm@0: { andrewm@0: deallocateFilters(); andrewm@0: } andrewm@0: andrewm@0: //============================================================================== andrewm@0: const String AutoWahAudioProcessor::getName() const andrewm@0: { andrewm@0: return JucePlugin_Name; andrewm@0: } andrewm@0: andrewm@0: int AutoWahAudioProcessor::getNumParameters() andrewm@0: { andrewm@0: return kNumParameters; andrewm@0: } andrewm@0: andrewm@0: float AutoWahAudioProcessor::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 kBaseFrequencyParam: return baseFrequency_; andrewm@0: case kQParam: return q_; andrewm@0: case kLFOFrequencyParam: return lfoFrequency_; andrewm@0: case kLFOWidthParam: return lfoWidth_; andrewm@0: case kEnvelopeWidthParam: return envelopeWidth_; andrewm@0: case kEnvelopeAttackParam: return envelopeAttack_; andrewm@0: case kEnvelopeDecayParam: return envelopeDecay_; andrewm@0: default: return 0.0f; andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: void AutoWahAudioProcessor::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: andrewm@0: switch (index) andrewm@0: { andrewm@0: case kBaseFrequencyParam: andrewm@0: baseFrequency_ = newValue; andrewm@0: break; andrewm@0: case kQParam: andrewm@0: q_ = newValue; andrewm@0: break; andrewm@0: case kLFOFrequencyParam: andrewm@0: lfoFrequency_ = newValue; andrewm@0: break; andrewm@0: case kLFOWidthParam: andrewm@0: lfoWidth_ = newValue; andrewm@0: break; andrewm@0: case kEnvelopeWidthParam: andrewm@0: envelopeWidth_ = newValue; andrewm@0: break; andrewm@0: case kEnvelopeAttackParam: andrewm@0: envelopeAttack_ = newValue; andrewm@0: // See comment below for justification andrewm@0: if(envelopeAttack_ == 0.0) andrewm@0: attackMultiplier_ = 0.0; andrewm@0: else andrewm@0: attackMultiplier_ = pow(1.0 / M_E, inverseSampleRate_ / envelopeAttack_); andrewm@0: break; andrewm@0: case kEnvelopeDecayParam: andrewm@0: envelopeDecay_ = newValue; andrewm@0: // envelopeDecay_ sets the time constant tau. The decay is andrewm@0: // given as e^-(t/tau) so after tau seconds, it will have andrewm@0: // decayed to 1/e of its original value. tau*sampleRate samples andrewm@0: // will have passed by then, each of which multiplies the signal andrewm@0: // by decayMultiplier_. andrewm@0: if(envelopeDecay_ == 0.0) andrewm@0: decayMultiplier_ = 0.0; andrewm@0: else andrewm@0: decayMultiplier_ = pow(1.0 / M_E, inverseSampleRate_ / envelopeDecay_); andrewm@0: break; andrewm@0: default: andrewm@0: break; andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: const String AutoWahAudioProcessor::getParameterName (int index) andrewm@0: { andrewm@0: switch (index) andrewm@0: { andrewm@0: case kBaseFrequencyParam: return "base frequency"; andrewm@0: case kQParam: return "Q"; andrewm@0: case kLFOFrequencyParam: return "LFO frequency"; andrewm@0: case kLFOWidthParam: return "LFO width"; andrewm@0: case kEnvelopeWidthParam: return "envelope width"; andrewm@0: case kEnvelopeAttackParam: return "envelope attack"; andrewm@0: case kEnvelopeDecayParam: return "envelope decay"; andrewm@0: default: break; andrewm@0: } andrewm@0: andrewm@0: return String::empty; andrewm@0: } andrewm@0: andrewm@0: const String AutoWahAudioProcessor::getParameterText (int index) andrewm@0: { andrewm@0: return String (getParameter (index), 2); andrewm@0: } andrewm@0: andrewm@0: const String AutoWahAudioProcessor::getInputChannelName (int channelIndex) const andrewm@0: { andrewm@0: return String (channelIndex + 1); andrewm@0: } andrewm@0: andrewm@0: const String AutoWahAudioProcessor::getOutputChannelName (int channelIndex) const andrewm@0: { andrewm@0: return String (channelIndex + 1); andrewm@0: } andrewm@0: andrewm@0: bool AutoWahAudioProcessor::isInputChannelStereoPair (int index) const andrewm@0: { andrewm@0: return true; andrewm@0: } andrewm@0: andrewm@0: bool AutoWahAudioProcessor::isOutputChannelStereoPair (int index) const andrewm@0: { andrewm@0: return true; andrewm@0: } andrewm@0: andrewm@0: bool AutoWahAudioProcessor::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 AutoWahAudioProcessor::getTailLengthSeconds() const andrewm@0: { andrewm@0: return 0.0; andrewm@0: } andrewm@0: andrewm@0: bool AutoWahAudioProcessor::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 AutoWahAudioProcessor::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 AutoWahAudioProcessor::getNumPrograms() andrewm@0: { andrewm@0: return 0; andrewm@0: } andrewm@0: andrewm@0: int AutoWahAudioProcessor::getCurrentProgram() andrewm@0: { andrewm@0: return 0; andrewm@0: } andrewm@0: andrewm@0: void AutoWahAudioProcessor::setCurrentProgram (int index) andrewm@0: { andrewm@0: } andrewm@0: andrewm@0: const String AutoWahAudioProcessor::getProgramName (int index) andrewm@0: { andrewm@0: return String::empty; andrewm@0: } andrewm@0: andrewm@0: void AutoWahAudioProcessor::changeProgramName (int index, const String& newName) andrewm@0: { andrewm@0: } andrewm@0: andrewm@0: //============================================================================== andrewm@0: void AutoWahAudioProcessor::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: allocateFilters(); andrewm@0: inverseSampleRate_ = 1.0 / sampleRate; andrewm@0: if(envelopeDecay_ == 0.0) andrewm@0: decayMultiplier_ = 0.0; andrewm@0: else andrewm@0: decayMultiplier_ = pow(1.0 / M_E, inverseSampleRate_ / envelopeDecay_); andrewm@0: if(envelopeAttack_ == 0.0) andrewm@0: attackMultiplier_ = 0.0; andrewm@0: else andrewm@0: attackMultiplier_ = pow(1.0 / M_E, inverseSampleRate_ / envelopeAttack_); andrewm@0: } andrewm@0: andrewm@0: void AutoWahAudioProcessor::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: andrewm@0: deallocateFilters(); andrewm@0: } andrewm@0: andrewm@0: void AutoWahAudioProcessor::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: float ph; andrewm@0: andrewm@0: // Go through each channel and put it through the resonant lowpass filter, updating andrewm@0: // the coefficients as we go along. Each channel is processed identically in this effect. andrewm@0: andrewm@0: for(channel = 0; channel < jmin(numInputChannels, numWahFilters_); ++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: ph = lfoPhase_; andrewm@0: andrewm@0: for (int sample = 0; sample < numSamples; ++sample) andrewm@0: { andrewm@0: const float in = channelData[sample]; andrewm@0: float centreFrequency = baseFrequency_; andrewm@0: andrewm@0: // Calculate the envelope of the signal. Do this even if we're not currently andrewm@0: // changing the frequeny based on it, since it involves maintaining a history andrewm@0: // of the signal's behaviour. andrewm@0: andrewm@0: if(channel < numEnvelopes_) { // Safety check andrewm@0: if(fabs(in) > envelopes_[channel]) { andrewm@0: envelopes_[channel] += (1.0 - attackMultiplier_) * (fabs(in) - (double)envelopes_[channel]); andrewm@0: } andrewm@0: else andrewm@0: envelopes_[channel] *= decayMultiplier_; andrewm@0: } andrewm@0: andrewm@0: // Calculate the centre frequency of the filter based on the LFO and the andrewm@0: // signal envelope andrewm@0: if(lfoWidth_ > 0.0) { andrewm@0: centreFrequency += lfoWidth_ * (0.5f + 0.5f*sinf(2.0 * M_PI * ph)); andrewm@0: } andrewm@0: if(envelopeWidth_ > 0.0 && channel < numEnvelopes_) { andrewm@0: centreFrequency += envelopeWidth_ * envelopes_[channel]; andrewm@0: } andrewm@0: andrewm@0: // Update filter coefficients (see ResonantLowpassFilter.cpp for calculation) andrewm@0: wahFilters_[channel]->makeResonantLowpass(inverseSampleRate_, andrewm@0: centreFrequency, andrewm@0: q_, andrewm@0: kWahwahFilterGain); andrewm@0: andrewm@0: // Process one sample and store it back in place. See juce_IIRFilter.cpp for the andrewm@0: // application of the IIR filter. andrewm@0: channelData[sample] = wahFilters_[channel]->processSingleSampleRaw(in); andrewm@0: andrewm@0: // Update the LFO phase, keeping it in the range 0-1 andrewm@0: ph += lfoFrequency_*inverseSampleRate_; andrewm@0: if(ph >= 1.0) andrewm@0: ph -= 1.0; andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: lfoPhase_ = ph; 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 AutoWahAudioProcessor::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* AutoWahAudioProcessor::createEditor() andrewm@0: { andrewm@0: return new AutoWahAudioProcessorEditor (this); andrewm@0: } andrewm@0: andrewm@0: //============================================================================== andrewm@0: void AutoWahAudioProcessor::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("baseFrequency", baseFrequency_); andrewm@0: xml.setAttribute("q", q_); andrewm@0: xml.setAttribute("lfoFrequency", lfoFrequency_); andrewm@0: xml.setAttribute("lfoWidth", lfoWidth_); andrewm@0: xml.setAttribute("envelopeWidth", envelopeWidth_); andrewm@0: xml.setAttribute("envelopeAttack", envelopeAttack_); andrewm@0: xml.setAttribute("envelopeDecay", envelopeDecay_); 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 AutoWahAudioProcessor::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: q_ = (float)xmlState->getDoubleAttribute("q", q_); andrewm@0: baseFrequency_ = (float)xmlState->getDoubleAttribute("baseFrequency", baseFrequency_); andrewm@0: lfoFrequency_ = (float)xmlState->getDoubleAttribute("lfoFrequency", lfoFrequency_); andrewm@0: lfoWidth_ = (float)xmlState->getDoubleAttribute("lfoWidth", lfoWidth_); andrewm@0: envelopeWidth_ = (float)xmlState->getDoubleAttribute("envelopeWidth", envelopeWidth_); andrewm@0: envelopeAttack_ = (float)xmlState->getDoubleAttribute("envelopeAttack", envelopeAttack_); andrewm@0: envelopeDecay_ = (float)xmlState->getDoubleAttribute("envelopeDecay", envelopeDecay_); andrewm@0: inverseSampleRate_ = 1.0 / getSampleRate(); andrewm@0: if(envelopeDecay_ == 0.0) andrewm@0: decayMultiplier_ = 0.0; andrewm@0: else andrewm@0: decayMultiplier_ = pow(1.0 / M_E, inverseSampleRate_ / envelopeDecay_); andrewm@0: if(envelopeAttack_ == 0.0) andrewm@0: attackMultiplier_ = 0.0; andrewm@0: else andrewm@0: attackMultiplier_ = pow(1.0 / M_E, inverseSampleRate_ / envelopeAttack_); andrewm@0: } andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: void AutoWahAudioProcessor::allocateFilters() andrewm@0: { andrewm@0: // Prevent leaks from reallocation andrewm@0: if(wahFilters_ != 0 || envelopes_ != 0) andrewm@0: deallocateFilters(); andrewm@0: andrewm@0: // Create as many filters as we have input channels andrewm@0: numWahFilters_ = getNumInputChannels(); andrewm@0: wahFilters_ = (ResonantLowpassFilter**)malloc(numWahFilters_ * sizeof(ResonantLowpassFilter*)); andrewm@0: if(wahFilters_ == 0) andrewm@0: numWahFilters_ = 0; andrewm@0: else { andrewm@0: for(int i = 0; i < numWahFilters_; i++) andrewm@0: wahFilters_[i] = new ResonantLowpassFilter; andrewm@0: } andrewm@0: andrewm@0: numEnvelopes_ = getNumInputChannels(); andrewm@0: envelopes_ = (double *)malloc(numEnvelopes_ * sizeof(double)); andrewm@0: if(envelopes_ == 0) andrewm@0: numEnvelopes_ = 0; andrewm@0: else { andrewm@0: for(int i = 0; i < numEnvelopes_; i++) andrewm@0: envelopes_[i] = 0.0; andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: void AutoWahAudioProcessor::deallocateFilters() andrewm@0: { andrewm@0: for(int i = 0; i < numWahFilters_; i++) andrewm@0: delete wahFilters_[i]; andrewm@0: if(numWahFilters_ != 0) andrewm@0: free(wahFilters_); andrewm@0: numWahFilters_ = 0; andrewm@0: wahFilters_ = 0; andrewm@0: if(envelopes_ != 0) andrewm@0: free(envelopes_); andrewm@0: envelopes_ = 0; andrewm@0: numEnvelopes_ = 0; 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 AutoWahAudioProcessor(); andrewm@0: }