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: Chorus: chorus effect based on time-varying delays andrewm@0: See textbook Chapter 2: Delay Line 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: const float ChorusAudioProcessor::kMaximumDelay = 0.05; andrewm@0: const float ChorusAudioProcessor::kMaximumSweepWidth = 0.05; andrewm@0: andrewm@0: //============================================================================== andrewm@0: ChorusAudioProcessor::ChorusAudioProcessor() : delayBuffer_ (2, 1) andrewm@0: { andrewm@0: // Set default values: andrewm@0: delay_ = .03; andrewm@0: sweepWidth_ = .02; andrewm@0: depth_ = 1.0; andrewm@0: frequency_ = 0.2; andrewm@0: waveform_ = kWaveformSine; andrewm@0: interpolation_ = kInterpolationLinear; andrewm@0: numVoices_ = 2; andrewm@0: stereo_ = 0; andrewm@0: andrewm@0: delayBufferLength_ = 1; andrewm@0: lfoPhase_ = 0.0; andrewm@0: inverseSampleRate_ = 1.0/44100.0; andrewm@0: andrewm@0: // Start the circular buffer pointer at the beginning andrewm@0: delayWritePosition_ = 0; andrewm@0: andrewm@0: lastUIWidth_ = 550; andrewm@0: lastUIHeight_ = 200; andrewm@0: } andrewm@0: andrewm@0: ChorusAudioProcessor::~ChorusAudioProcessor() andrewm@0: { andrewm@0: } andrewm@0: andrewm@0: //============================================================================== andrewm@0: const String ChorusAudioProcessor::getName() const andrewm@0: { andrewm@0: return JucePlugin_Name; andrewm@0: } andrewm@0: andrewm@0: int ChorusAudioProcessor::getNumParameters() andrewm@0: { andrewm@0: return kNumParameters; andrewm@0: } andrewm@0: andrewm@0: float ChorusAudioProcessor::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 kDelayParam: return delay_; andrewm@0: case kSweepWidthParam: return sweepWidth_; andrewm@0: case kDepthParam: return depth_; andrewm@0: case kFrequencyParam: return frequency_; andrewm@0: case kWaveformParam: return (float)waveform_; andrewm@0: case kInterpolationParam: return (float)interpolation_; andrewm@0: case kNumVoicesParam: return (float)numVoices_; andrewm@0: case kStereoParam: return (float)stereo_; andrewm@0: default: return 0.0f; andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: void ChorusAudioProcessor::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 kDelayParam: andrewm@0: delay_ = 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 kFrequencyParam: andrewm@0: frequency_ = newValue; andrewm@0: break; andrewm@0: case kWaveformParam: andrewm@0: waveform_ = (int)newValue; andrewm@0: break; andrewm@0: case kInterpolationParam: andrewm@0: interpolation_ = (int)newValue; andrewm@0: break; andrewm@0: case kNumVoicesParam: andrewm@0: numVoices_ = (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 ChorusAudioProcessor::getParameterName (int index) andrewm@0: { andrewm@0: switch (index) andrewm@0: { andrewm@0: case kDelayParam: return "delay"; andrewm@0: case kSweepWidthParam: return "sweep width"; andrewm@0: case kDepthParam: return "depth"; andrewm@0: case kFrequencyParam: return "frequency"; andrewm@0: case kWaveformParam: return "waveform"; andrewm@0: case kInterpolationParam: return "interpolation"; andrewm@0: case kNumVoicesParam: return "number of voices"; 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 ChorusAudioProcessor::getParameterText (int index) andrewm@0: { andrewm@0: return String (getParameter (index), 2); andrewm@0: } andrewm@0: andrewm@0: const String ChorusAudioProcessor::getInputChannelName (int channelIndex) const andrewm@0: { andrewm@0: return String (channelIndex + 1); andrewm@0: } andrewm@0: andrewm@0: const String ChorusAudioProcessor::getOutputChannelName (int channelIndex) const andrewm@0: { andrewm@0: return String (channelIndex + 1); andrewm@0: } andrewm@0: andrewm@0: bool ChorusAudioProcessor::isInputChannelStereoPair (int index) const andrewm@0: { andrewm@0: return true; andrewm@0: } andrewm@0: andrewm@0: bool ChorusAudioProcessor::isOutputChannelStereoPair (int index) const andrewm@0: { andrewm@0: return true; andrewm@0: } andrewm@0: andrewm@0: bool ChorusAudioProcessor::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 ChorusAudioProcessor::getTailLengthSeconds() const andrewm@0: { andrewm@0: return 0.0; andrewm@0: } andrewm@0: andrewm@0: bool ChorusAudioProcessor::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 ChorusAudioProcessor::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 ChorusAudioProcessor::getNumPrograms() andrewm@0: { andrewm@0: return 0; andrewm@0: } andrewm@0: andrewm@0: int ChorusAudioProcessor::getCurrentProgram() andrewm@0: { andrewm@0: return 0; andrewm@0: } andrewm@0: andrewm@0: void ChorusAudioProcessor::setCurrentProgram (int index) andrewm@0: { andrewm@0: } andrewm@0: andrewm@0: const String ChorusAudioProcessor::getProgramName (int index) andrewm@0: { andrewm@0: return String::empty; andrewm@0: } andrewm@0: andrewm@0: void ChorusAudioProcessor::changeProgramName (int index, const String& newName) andrewm@0: { andrewm@0: } andrewm@0: andrewm@0: //============================================================================== andrewm@0: void ChorusAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock) andrewm@0: { andrewm@0: // Allocate and zero the delay buffer (size will depend on current sample rate) andrewm@0: // Add 3 extra samples to allow cubic interpolation even at maximum delay andrewm@0: delayBufferLength_ = (int)((kMaximumDelay + kMaximumSweepWidth)*sampleRate) + 3; andrewm@0: delayBuffer_.setSize(2, delayBufferLength_); andrewm@0: delayBuffer_.clear(); andrewm@0: lfoPhase_ = 0.0; andrewm@0: andrewm@0: inverseSampleRate_ = 1.0/sampleRate; andrewm@0: } andrewm@0: andrewm@0: void ChorusAudioProcessor::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: // The delay buffer will stay in memory until the effect is unloaded. andrewm@0: } andrewm@0: andrewm@0: void ChorusAudioProcessor::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: delayBuffer_.clear(); andrewm@0: } andrewm@0: andrewm@0: andrewm@0: void ChorusAudioProcessor::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: andrewm@0: int channel, dpw; // dpr = delay read pointer; dpw = delay write pointer andrewm@0: float dpr, currentDelay, ph; andrewm@0: andrewm@0: // Go through each channel of audio that's passed in. In this example we apply identical andrewm@0: // effects to each channel, regardless of how many input channels there are. For some effects, like andrewm@0: // a stereo chorus or panner, you might do something different for each channel. andrewm@0: andrewm@0: for (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: // delayData is the circular buffer for implementing delay on this channel b@1: float* delayData = delayBuffer_.getWritePointer (jmin (channel, delayBuffer_.getNumChannels() - 1)); andrewm@0: andrewm@0: // Make a temporary copy of any state variables declared in PluginProcessor.h which need to be andrewm@0: // maintained between calls to processBlock(). Each channel needs to be processed identically andrewm@0: // which means that the activity of processing one channel can't affect the state variable for andrewm@0: // the next channel. andrewm@0: andrewm@0: dpw = delayWritePosition_; andrewm@0: ph = lfoPhase_; andrewm@0: andrewm@0: for (int i = 0; i < numSamples; ++i) andrewm@0: { andrewm@0: const float in = channelData[i]; andrewm@0: float interpolatedSample = 0.0; andrewm@0: float phaseOffset = 0.0; andrewm@0: float weight; andrewm@0: andrewm@0: // Chorus can have more than 2 voices (where the original, undelayed signal counts as a voice). andrewm@0: // In this implementation, all voices use the same LFO, but with different phase offsets. It andrewm@0: // is also possible to use different waveforms and different frequencies for each voice. andrewm@0: andrewm@0: for(int j = 0; j < numVoices_ - 1; ++j) andrewm@0: { andrewm@0: if(stereo_ != 0 && numVoices_ > 2) andrewm@0: { andrewm@0: // A stereo chorus pans each voice to a different location in the stereo field. andrewm@0: // How this is done depends on the number of voices: andrewm@0: // -- 2 voices: N/A (need at least 2 delayed voices for stereo chorus) andrewm@0: // -- 3 voices: 1 voice left, 1 voice right (0, 1) andrewm@0: // -- 4 voices: 1 voice left, 1 voice centre, 1 voice right (0, 0.5, 1) andrewm@0: // -- 5 voices: 1 voice left, 1 voice left-centre, andrewm@0: // 1 voice right-centre, 1 voice right (0, 0.33, 0.66, 1) andrewm@0: andrewm@0: weight = (float)j/(float)(numVoices_ - 2); andrewm@0: andrewm@0: // Left and right channels are mirrors of each other in weight andrewm@0: if(channel != 0) andrewm@0: weight = 1.0 - weight; andrewm@0: } andrewm@0: else andrewm@0: weight = 1.0; andrewm@0: andrewm@0: // Add the voice to the mix if it has nonzero weight andrewm@0: if(weight != 0.0) andrewm@0: { andrewm@0: // Recalculate the read pointer position with respect to the write pointer. A more efficient andrewm@0: // implementation might increment the read pointer based on the derivative of the LFO without andrewm@0: // running the whole equation again, but this format makes the operation clearer. andrewm@0: andrewm@0: currentDelay = delay_ + sweepWidth_*lfo(fmodf(ph + phaseOffset, 1.0f), waveform_); andrewm@0: dpr = fmodf((float)dpw - (float)(currentDelay * getSampleRate()) + (float)delayBufferLength_, andrewm@0: (float)delayBufferLength_); andrewm@0: andrewm@0: // In this example, the output is the input plus the contents of the delay buffer (weighted by delayMix) andrewm@0: // The last term implements a tremolo (variable amplitude) on the whole thing. andrewm@0: andrewm@0: if(interpolation_ == kInterpolationLinear) andrewm@0: { andrewm@0: // Find the fraction by which the read pointer sits between two andrewm@0: // samples and use this to adjust weights of the samples andrewm@0: float fraction = dpr - floorf(dpr); andrewm@0: int previousSample = (int)floorf(dpr); andrewm@0: int nextSample = (previousSample + 1) % delayBufferLength_; andrewm@0: interpolatedSample = fraction*delayData[nextSample] andrewm@0: + (1.0f-fraction)*delayData[previousSample]; andrewm@0: } andrewm@0: else if(interpolation_ == kInterpolationCubic) andrewm@0: { andrewm@0: // Cubic interpolation will produce cleaner results at the expense andrewm@0: // of more computation. This code uses the Catmull-Rom variant of andrewm@0: // cubic interpolation. To reduce the load, calculate a few quantities andrewm@0: // in advance that will be used several times in the equation: andrewm@0: andrewm@0: int sample1 = (int)floorf(dpr); andrewm@0: int sample2 = (sample1 + 1) % delayBufferLength_; andrewm@0: int sample3 = (sample2 + 1) % delayBufferLength_; andrewm@0: int sample0 = (sample1 - 1 + delayBufferLength_) % delayBufferLength_; andrewm@0: andrewm@0: float fraction = dpr - floorf(dpr); andrewm@0: float frsq = fraction*fraction; andrewm@0: andrewm@0: float a0 = -0.5f*delayData[sample0] + 1.5f*delayData[sample1] andrewm@0: - 1.5f*delayData[sample2] + 0.5f*delayData[sample3]; andrewm@0: float a1 = delayData[sample0] - 2.5f*delayData[sample1] andrewm@0: + 2.0f*delayData[sample2] - 0.5f*delayData[sample3]; andrewm@0: float a2 = -0.5f*delayData[sample0] + 0.5f*delayData[sample2]; andrewm@0: float a3 = delayData[sample1]; andrewm@0: andrewm@0: interpolatedSample = a0*fraction*frsq + a1*frsq + a2*fraction + a3; andrewm@0: } andrewm@0: else // Nearest neighbour interpolation andrewm@0: { andrewm@0: // Find the nearest input sample by rounding the fractional index to the andrewm@0: // nearest integer. It's possible this will round it to the end of the buffer, andrewm@0: // in which case we need to roll it back to the beginning. andrewm@0: int closestSample = (int)floorf(dpr + 0.5f); andrewm@0: if(closestSample == delayBufferLength_) andrewm@0: closestSample = 0; andrewm@0: interpolatedSample = delayData[closestSample]; andrewm@0: } andrewm@0: andrewm@0: // Store the output sample in the buffer, which starts by containing the input sample andrewm@0: channelData[i] += depth_ * weight * interpolatedSample; andrewm@0: } andrewm@0: andrewm@0: // 3-voice chorus uses two voices in quadrature phase (90 degrees apart). Otherwise, andrewm@0: // spread the voice phases evenly around the unit circle. (For 2-voice chorus, this andrewm@0: // code doesn't matter since the loop only runs once.) andrewm@0: if(numVoices_ < 3) andrewm@0: phaseOffset += 0.25f; andrewm@0: else andrewm@0: phaseOffset += 1.0f / (float)(numVoices_ - 1); andrewm@0: } andrewm@0: andrewm@0: // Store the current input in the delay buffer (no feedback in a chorus, unlike a flanger). andrewm@0: delayData[dpw] = in; andrewm@0: andrewm@0: // Increment the write pointer at a constant rate. The read pointer will move at different andrewm@0: // rates depending on the settings of the LFO, the delay and the sweep width. andrewm@0: andrewm@0: if (++dpw >= delayBufferLength_) andrewm@0: dpw = 0; andrewm@0: andrewm@0: // Update the LFO phase, keeping it in the range 0-1 andrewm@0: ph += frequency_*inverseSampleRate_; andrewm@0: if(ph >= 1.0) andrewm@0: ph -= 1.0; andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: // Having made a local copy of the state variables for each channel, now transfer the result andrewm@0: // back to the main state variable so they will be preserved for the next call of processBlock() andrewm@0: andrewm@0: delayWritePosition_ = dpw; andrewm@0: lfoPhase_ = ph; andrewm@0: andrewm@0: // In case we have more outputs than inputs, we'll clear any output andrewm@0: // channels that didn't contain input data, (because these aren't andrewm@0: // guaranteed to be empty - they may contain garbage). andrewm@0: for (int i = numInputChannels; i < numOutputChannels; ++i) andrewm@0: { andrewm@0: buffer.clear (i, 0, buffer.getNumSamples()); andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: //============================================================================== andrewm@0: bool ChorusAudioProcessor::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* ChorusAudioProcessor::createEditor() andrewm@0: { andrewm@0: return new ChorusAudioProcessorEditor (this); andrewm@0: } andrewm@0: andrewm@0: //============================================================================== andrewm@0: void ChorusAudioProcessor::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("delay", delay_); andrewm@0: xml.setAttribute("sweepWidth", sweepWidth_); andrewm@0: xml.setAttribute("depth", depth_); andrewm@0: xml.setAttribute("frequency", frequency_); andrewm@0: xml.setAttribute("waveform", waveform_); andrewm@0: xml.setAttribute("interpolation", interpolation_); andrewm@0: xml.setAttribute("numVoices", numVoices_); 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 ChorusAudioProcessor::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: delay_ = (float)xmlState->getDoubleAttribute("delay", delay_); andrewm@0: sweepWidth_ = (float)xmlState->getDoubleAttribute("sweepWidth", sweepWidth_); andrewm@0: depth_ = (float)xmlState->getDoubleAttribute("depth", depth_); andrewm@0: frequency_ = (float)xmlState->getDoubleAttribute("frequency", frequency_); andrewm@0: waveform_ = xmlState->getIntAttribute("waveform", waveform_); andrewm@0: interpolation_ = xmlState->getIntAttribute("interpolation", interpolation_); andrewm@0: numVoices_ = xmlState->getIntAttribute("numVoices", numVoices_); 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 ChorusAudioProcessor::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: //============================================================================== andrewm@0: // This creates new instances of the plugin.. andrewm@0: AudioProcessor* JUCE_CALLTYPE createPluginFilter() andrewm@0: { andrewm@0: return new ChorusAudioProcessor(); andrewm@0: }