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: Phaser: phasing effect using time-varying allpass filters
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: #include
andrewm@0:
andrewm@0: //==============================================================================
andrewm@0: PhaserAudioProcessor::PhaserAudioProcessor()
andrewm@0: {
andrewm@0: // Set default values:
andrewm@0: baseFrequency_ = 200.0;
andrewm@0: sweepWidth_ = 2000.0;
andrewm@0: depth_ = 1.0;
andrewm@0: feedback_ = 0.0;
andrewm@0: lfoFrequency_ = 0.5;
andrewm@0: waveform_ = kWaveformSine;
andrewm@0: stereo_ = 0;
andrewm@0:
andrewm@0: // Start with no filters (at least until we have some channels)
andrewm@0: allpassFilters_ = 0;
andrewm@0: filtersPerChannel_ = 4;
andrewm@0: totalNumFilters_ = 0;
andrewm@0: lastFilterOutputs_ = 0;
andrewm@0: numLastFilterOutputs_ = 0;
andrewm@0:
andrewm@0: lfoPhase_ = 0.0;
andrewm@0: inverseSampleRate_ = 1.0/44100.0;
andrewm@0: sampleCount_ = 0;
andrewm@0: filterUpdateInterval_ = 8;
andrewm@0:
andrewm@0: lastUIWidth_ = 550;
andrewm@0: lastUIHeight_ = 200;
andrewm@0: }
andrewm@0:
andrewm@0: PhaserAudioProcessor::~PhaserAudioProcessor()
andrewm@0: {
andrewm@0: deallocateFilters();
andrewm@0: }
andrewm@0:
andrewm@0: //==============================================================================
andrewm@0: const String PhaserAudioProcessor::getName() const
andrewm@0: {
andrewm@0: return JucePlugin_Name;
andrewm@0: }
andrewm@0:
andrewm@0: int PhaserAudioProcessor::getNumParameters()
andrewm@0: {
andrewm@0: return kNumParameters;
andrewm@0: }
andrewm@0:
andrewm@0: float PhaserAudioProcessor::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 kSweepWidthParam: return sweepWidth_;
andrewm@0: case kDepthParam: return depth_;
andrewm@0: case kFeedbackParam: return feedback_;
andrewm@0: case kLFOFrequencyParam: return lfoFrequency_;
andrewm@0: case kFiltersParam: return (float)filtersPerChannel_;
andrewm@0: case kWaveformParam: return (float)waveform_;
andrewm@0: case kStereoParam: return (float)stereo_;
andrewm@0: default: return 0.0f;
andrewm@0: }
andrewm@0: }
andrewm@0:
andrewm@0: void PhaserAudioProcessor::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 kSweepWidthParam:
andrewm@0: sweepWidth_ = newValue;
andrewm@0: break;
andrewm@0: case kDepthParam:
andrewm@0: depth_ = newValue;
andrewm@0: break;
andrewm@0: case kFeedbackParam:
andrewm@0: feedback_ = newValue;
andrewm@0: break;
andrewm@0: case kLFOFrequencyParam:
andrewm@0: lfoFrequency_ = newValue;
andrewm@0: break;
andrewm@0: case kFiltersParam:
andrewm@0: if(filtersPerChannel_ != (int)newValue) {
andrewm@0: filtersPerChannel_ = (int)newValue;
andrewm@0: reallocateFilters();
andrewm@0: }
andrewm@0: break;
andrewm@0: case kWaveformParam:
andrewm@0: waveform_ = (int)newValue;
andrewm@0: break;
andrewm@0: case kStereoParam:
andrewm@0: stereo_ = (int)newValue;
andrewm@0: break;
andrewm@0: default:
andrewm@0: break;
andrewm@0: }
andrewm@0: }
andrewm@0:
andrewm@0: const String PhaserAudioProcessor::getParameterName (int index)
andrewm@0: {
andrewm@0: switch (index)
andrewm@0: {
andrewm@0: case kBaseFrequencyParam: return "base frequency";
andrewm@0: case kSweepWidthParam: return "sweep width";
andrewm@0: case kDepthParam: return "depth";
andrewm@0: case kFeedbackParam: return "feedback";
andrewm@0: case kLFOFrequencyParam: return "LFO frequency";
andrewm@0: case kWaveformParam: return "waveform";
andrewm@0: case kStereoParam: return "stereo";
andrewm@0: default: break;
andrewm@0: }
andrewm@0:
andrewm@0: return String::empty;
andrewm@0: }
andrewm@0:
andrewm@0: const String PhaserAudioProcessor::getParameterText (int index)
andrewm@0: {
andrewm@0: return String (getParameter (index), 2);
andrewm@0: }
andrewm@0:
andrewm@0: const String PhaserAudioProcessor::getInputChannelName (int channelIndex) const
andrewm@0: {
andrewm@0: return String (channelIndex + 1);
andrewm@0: }
andrewm@0:
andrewm@0: const String PhaserAudioProcessor::getOutputChannelName (int channelIndex) const
andrewm@0: {
andrewm@0: return String (channelIndex + 1);
andrewm@0: }
andrewm@0:
andrewm@0: bool PhaserAudioProcessor::isInputChannelStereoPair (int index) const
andrewm@0: {
andrewm@0: return true;
andrewm@0: }
andrewm@0:
andrewm@0: bool PhaserAudioProcessor::isOutputChannelStereoPair (int index) const
andrewm@0: {
andrewm@0: return true;
andrewm@0: }
andrewm@0:
andrewm@0: bool PhaserAudioProcessor::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 PhaserAudioProcessor::getTailLengthSeconds() const
andrewm@0: {
andrewm@0: return 0.0;
andrewm@0: }
andrewm@0:
andrewm@0: bool PhaserAudioProcessor::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 PhaserAudioProcessor::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 PhaserAudioProcessor::getNumPrograms()
andrewm@0: {
andrewm@0: return 0;
andrewm@0: }
andrewm@0:
andrewm@0: int PhaserAudioProcessor::getCurrentProgram()
andrewm@0: {
andrewm@0: return 0;
andrewm@0: }
andrewm@0:
andrewm@0: void PhaserAudioProcessor::setCurrentProgram (int index)
andrewm@0: {
andrewm@0: }
andrewm@0:
andrewm@0: const String PhaserAudioProcessor::getProgramName (int index)
andrewm@0: {
andrewm@0: return String::empty;
andrewm@0: }
andrewm@0:
andrewm@0: void PhaserAudioProcessor::changeProgramName (int index, const String& newName)
andrewm@0: {
andrewm@0: }
andrewm@0:
andrewm@0: //==============================================================================
andrewm@0: void PhaserAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
andrewm@0: {
andrewm@0: lfoPhase_ = 0.0;
andrewm@0: inverseSampleRate_ = 1.0/sampleRate;
andrewm@0: sampleCount_ = 0;
andrewm@0:
andrewm@0: const ScopedLock sl (lock_);
andrewm@0: allocateFilters();
andrewm@0: }
andrewm@0:
andrewm@0: void PhaserAudioProcessor::releaseResources()
andrewm@0: {
andrewm@0: const ScopedLock sl (lock_);
andrewm@0: deallocateFilters();
andrewm@0: }
andrewm@0:
andrewm@0: void PhaserAudioProcessor::reset()
andrewm@0: {
andrewm@0: // Use this method as the place to clear any delay lines, buffers, etc, as it
andrewm@0: // means there's been a break in the audio's continuity.
andrewm@0:
andrewm@0: lfoPhase_ = 0.0;
andrewm@0: sampleCount_ = 0;
andrewm@0: for(int i = 0; i < numLastFilterOutputs_; i++)
andrewm@0: lastFilterOutputs_[i] = 0.0f;
andrewm@0: }
andrewm@0:
andrewm@0:
andrewm@0: void PhaserAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
andrewm@0: {
andrewm@0: const ScopedLock sl (lock_);
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: float ph, channel0EndPhase = lfoPhase_;
andrewm@0: unsigned int sc;
andrewm@0:
andrewm@0: // Go through each channel of audio that's passed in, applying one or more allpass filters
andrewm@0: // to each. Each channel will be treated identically in a (non-stereo) phaser, but we have
andrewm@0: // to have separate filter objects for each channel since the filters store the last few samples
andrewm@0: // passed through them.
andrewm@0:
andrewm@0: // Filters are stored with all channel 0 filters first, then all channel 1 filters, etc.
andrewm@0:
andrewm@0: for(int channel = 0; channel < numInputChannels; ++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: ph = lfoPhase_;
andrewm@0: sc = sampleCount_;
andrewm@0:
andrewm@0: // For stereo phasing, keep the channels 90 degrees out of phase with each other
andrewm@0: if(stereo_ != 0 && channel != 0)
andrewm@0: ph = fmodf(ph + 0.25, 1.0);
andrewm@0:
andrewm@0: for (int sample = 0; sample < numSamples; ++sample)
andrewm@0: {
andrewm@0: float out = channelData[sample];
andrewm@0:
andrewm@0: // If feedback is enabled, include the feedback from the last sample in the
andrewm@0: // input of the allpass filter chain. This is actually not accurate to how
andrewm@0: // analog phasers work because there is a sample of delay between output and
andrewm@0: // input, which adds a further phase shift of up to 180 degrees at half the
andrewm@0: // sampling frequency. To truly model an analog phaser with feedback involves
andrewm@0: // modelling a delay-free loop, which is beyond the scope of this example.
andrewm@0:
andrewm@0: if(feedback_ != 0.0 && channel < numLastFilterOutputs_)
andrewm@0: out += feedback_ * lastFilterOutputs_[channel];
andrewm@0:
andrewm@0: // See OnePoleAllpassFilter.cpp for calculation of coefficients and application
andrewm@0: // of filter to audio data. The filter processes the audio buffer in place,
andrewm@0: // putting the output sample in place of the input.
andrewm@0:
andrewm@0: for(int j = 0; j < filtersPerChannel_; ++j)
andrewm@0: {
andrewm@0: // Safety check
andrewm@0: if(channel * filtersPerChannel_ + j >= totalNumFilters_)
andrewm@0: continue;
andrewm@0:
andrewm@0: // First, update the current allpass filter coefficients depending on the parameter
andrewm@0: // settings and the LFO phase
andrewm@0:
andrewm@0: // Recalculating the filter coefficients is much more expensive than calculating
andrewm@0: // a sample. Only update the coefficients at a fraction of the sample rate; since
andrewm@0: // the LFO moves slowly, the difference won't generally be audible.
andrewm@0: if(sc % filterUpdateInterval_ == 0)
andrewm@0: {
andrewm@0: allpassFilters_[channel * filtersPerChannel_ + j]->makeAllpass(inverseSampleRate_,
andrewm@0: baseFrequency_ + sweepWidth_*lfo(ph, waveform_));
andrewm@0: }
andrewm@0: out = allpassFilters_[channel * filtersPerChannel_ + j]->processSingleSampleRaw(out);
andrewm@0: }
andrewm@0:
andrewm@0: if(channel < numLastFilterOutputs_)
andrewm@0: lastFilterOutputs_[channel] = out;
andrewm@0:
andrewm@0: // Add the allpass signal to the output, though maintaining constant level
andrewm@0: // depth = 0 --> input only ; depth = 1 --> evenly balanced input and output
andrewm@0: channelData[sample] = (1.0f-0.5f*depth_)*channelData[sample] + 0.5f*depth_*out;
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: sc++;
andrewm@0: }
andrewm@0:
andrewm@0: // Use channel 0 only to keep the phase in sync between calls to processBlock()
andrewm@0: // Otherwise quadrature phase on multiple channels will create problems.
andrewm@0: if(channel == 0)
andrewm@0: channel0EndPhase = ph;
andrewm@0: }
andrewm@0:
andrewm@0: lfoPhase_ = channel0EndPhase;
andrewm@0: sampleCount_ = sc;
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: for(int channel = numInputChannels; channel < numOutputChannels; ++channel)
andrewm@0: {
andrewm@0: buffer.clear (channel++, 0, buffer.getNumSamples());
andrewm@0: }
andrewm@0: }
andrewm@0:
andrewm@0: //==============================================================================
andrewm@0: bool PhaserAudioProcessor::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* PhaserAudioProcessor::createEditor()
andrewm@0: {
andrewm@0: return new PhaserAudioProcessorEditor (this);
andrewm@0: }
andrewm@0:
andrewm@0: //==============================================================================
andrewm@0: void PhaserAudioProcessor::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("feedback", feedback_);
andrewm@0: xml.setAttribute("sweepWidth", sweepWidth_);
andrewm@0: xml.setAttribute("depth", depth_);
andrewm@0: xml.setAttribute("lfoFrequency", lfoFrequency_);
andrewm@0: xml.setAttribute("filtersPerChannel", filtersPerChannel_);
andrewm@0: xml.setAttribute("waveform", waveform_);
andrewm@0: xml.setAttribute("stereo", stereo_);
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 PhaserAudioProcessor::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: baseFrequency_ = (float)xmlState->getDoubleAttribute("baseFrequency", baseFrequency_);
andrewm@0: feedback_ = (float)xmlState->getDoubleAttribute("feedback", feedback_);
andrewm@0: sweepWidth_ = (float)xmlState->getDoubleAttribute("sweepWidth", sweepWidth_);
andrewm@0: depth_ = (float)xmlState->getDoubleAttribute("depth", depth_);
andrewm@0: lfoFrequency_ = (float)xmlState->getDoubleAttribute("lfoFrequency", lfoFrequency_);
andrewm@0: filtersPerChannel_ = xmlState->getIntAttribute("filtersPerChannel", filtersPerChannel_);
andrewm@0: waveform_ = xmlState->getIntAttribute("waveform", waveform_);
andrewm@0: stereo_ = xmlState->getIntAttribute("stereo", stereo_);
andrewm@0: }
andrewm@0: }
andrewm@0: }
andrewm@0:
andrewm@0: //==============================================================================
andrewm@0: // Function for calculating LFO waveforms. Phase runs from 0-1, output is scaled
andrewm@0: // from 0 to 1 (note: not -1 to 1 as would be typical of sine).
andrewm@0: float PhaserAudioProcessor::lfo(float phase, int waveform)
andrewm@0: {
andrewm@0: switch(waveform)
andrewm@0: {
andrewm@0: case kWaveformTriangle:
andrewm@0: if(phase < 0.25f)
andrewm@0: return 0.5f + 2.0f*phase;
andrewm@0: else if(phase < 0.75f)
andrewm@0: return 1.0f - 2.0f*(phase - 0.25f);
andrewm@0: else
andrewm@0: return 2.0f*(phase-0.75f);
andrewm@0: case kWaveformSquare:
andrewm@0: if(phase < 0.5f)
andrewm@0: return 1.0f;
andrewm@0: else
andrewm@0: return 0.0f;
andrewm@0: case kWaveformSawtooth:
andrewm@0: if(phase < 0.5f)
andrewm@0: return 0.5f + phase;
andrewm@0: else
andrewm@0: return phase - 0.5f;
andrewm@0: case kWaveformSine:
andrewm@0: default:
andrewm@0: return 0.5f + 0.5f*sinf(2.0 * M_PI * phase);
andrewm@0: }
andrewm@0: }
andrewm@0:
andrewm@0: void PhaserAudioProcessor::allocateFilters()
andrewm@0: {
andrewm@0: // Create any filters we need; depends on number of channels and number of
andrewm@0: // filters per channel
andrewm@0: totalNumFilters_ = getNumInputChannels() * filtersPerChannel_;
andrewm@0: if(totalNumFilters_ > 0) {
andrewm@0: allpassFilters_ = (OnePoleAllpassFilter**)malloc(totalNumFilters_ * sizeof(OnePoleAllpassFilter*));
andrewm@0: if(allpassFilters_ == 0)
andrewm@0: totalNumFilters_ = 0;
andrewm@0: else {
andrewm@0: for(int i = 0; i < totalNumFilters_; i++)
andrewm@0: allpassFilters_[i] = new OnePoleAllpassFilter;
andrewm@0: }
andrewm@0: }
andrewm@0:
andrewm@0: numLastFilterOutputs_ = getNumInputChannels();
andrewm@0: lastFilterOutputs_ = (float *)malloc(numLastFilterOutputs_ * sizeof(float));
andrewm@0: for(int i = 0; i < numLastFilterOutputs_; i++)
andrewm@0: lastFilterOutputs_[i] = 0.0f;
andrewm@0:
andrewm@0: // Coefficients of allpass filters will get updated in processBlock()
andrewm@0: }
andrewm@0:
andrewm@0: void PhaserAudioProcessor::deallocateFilters()
andrewm@0: {
andrewm@0: // Release the filters that were created in prepareToPlay()
andrewm@0:
andrewm@0: for(int i = 0; i < totalNumFilters_; i++)
andrewm@0: delete allpassFilters_[i];
andrewm@0: if(totalNumFilters_ != 0)
andrewm@0: free(allpassFilters_);
andrewm@0: totalNumFilters_ = 0;
andrewm@0: allpassFilters_ = 0;
andrewm@0:
andrewm@0: if(numLastFilterOutputs_ != 0)
andrewm@0: free(lastFilterOutputs_);
andrewm@0: numLastFilterOutputs_ = 0;
andrewm@0: lastFilterOutputs_ = 0;
andrewm@0: }
andrewm@0:
andrewm@0: // Release and recreate the filters in one atomic operation:
andrewm@0: // the ScopedLock will not let the audio thread run between
andrewm@0: // release and allocation
andrewm@0: void PhaserAudioProcessor::reallocateFilters()
andrewm@0: {
andrewm@0: const ScopedLock sl (lock_);
andrewm@0: deallocateFilters();
andrewm@0: allocateFilters();
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 PhaserAudioProcessor();
andrewm@0: }