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: Ring Modulator: modulation using a carrier oscillator
andrewm@0: See textbook Chapter 5: Amplitude Modulation
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: RingModulatorAudioProcessor::RingModulatorAudioProcessor()
andrewm@0: {
andrewm@0: // Set default values:
andrewm@0: carrierFrequency_ = 100.0;
andrewm@0: sweepWidth_ = 0.0;
andrewm@0: lfoFrequency_ = 1.0;
andrewm@0: waveform_ = kWaveformSine;
andrewm@0:
andrewm@0: lfoPhase_ = carrierPhase_ = 0.0;
andrewm@0: inverseSampleRate_ = 1.0/44100.0;
andrewm@0:
andrewm@0: lastUIWidth_ = 370;
andrewm@0: lastUIHeight_ = 160;
andrewm@0: }
andrewm@0:
andrewm@0: RingModulatorAudioProcessor::~RingModulatorAudioProcessor()
andrewm@0: {
andrewm@0: }
andrewm@0:
andrewm@0: //==============================================================================
andrewm@0: const String RingModulatorAudioProcessor::getName() const
andrewm@0: {
andrewm@0: return JucePlugin_Name;
andrewm@0: }
andrewm@0:
andrewm@0: int RingModulatorAudioProcessor::getNumParameters()
andrewm@0: {
andrewm@0: return kNumParameters;
andrewm@0: }
andrewm@0:
andrewm@0: float RingModulatorAudioProcessor::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 kCarrierFrequencyParam: return carrierFrequency_;
andrewm@0: case kSweepWidthParam: return sweepWidth_;
andrewm@0: case kLFOFrequencyParam: return lfoFrequency_;
andrewm@0: case kWaveformParam: return (float)waveform_;
andrewm@0: default: return 0.0f;
andrewm@0: }
andrewm@0: }
andrewm@0:
andrewm@0: void RingModulatorAudioProcessor::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 kCarrierFrequencyParam:
andrewm@0: carrierFrequency_ = newValue;
andrewm@0: break;
andrewm@0: case kSweepWidthParam:
andrewm@0: sweepWidth_ = newValue;
andrewm@0: break;
andrewm@0: case kLFOFrequencyParam:
andrewm@0: lfoFrequency_ = newValue;
andrewm@0: break;
andrewm@0: case kWaveformParam:
andrewm@0: waveform_ = (int)newValue;
andrewm@0: break;
andrewm@0: default:
andrewm@0: break;
andrewm@0: }
andrewm@0: }
andrewm@0:
andrewm@0: const String RingModulatorAudioProcessor::getParameterName (int index)
andrewm@0: {
andrewm@0: switch (index)
andrewm@0: {
andrewm@0: case kCarrierFrequencyParam: return "carrier frequency";
andrewm@0: case kSweepWidthParam: return "LFO sweep width";
andrewm@0: case kLFOFrequencyParam: return "LFO frequency";
andrewm@0: case kWaveformParam: return "LFO waveform";
andrewm@0: default: break;
andrewm@0: }
andrewm@0:
andrewm@0: return String::empty;
andrewm@0: }
andrewm@0:
andrewm@0: const String RingModulatorAudioProcessor::getParameterText (int index)
andrewm@0: {
andrewm@0: return String (getParameter (index), 2);
andrewm@0: }
andrewm@0:
andrewm@0: const String RingModulatorAudioProcessor::getInputChannelName (int channelIndex) const
andrewm@0: {
andrewm@0: return String (channelIndex + 1);
andrewm@0: }
andrewm@0:
andrewm@0: const String RingModulatorAudioProcessor::getOutputChannelName (int channelIndex) const
andrewm@0: {
andrewm@0: return String (channelIndex + 1);
andrewm@0: }
andrewm@0:
andrewm@0: bool RingModulatorAudioProcessor::isInputChannelStereoPair (int index) const
andrewm@0: {
andrewm@0: return true;
andrewm@0: }
andrewm@0:
andrewm@0: bool RingModulatorAudioProcessor::isOutputChannelStereoPair (int index) const
andrewm@0: {
andrewm@0: return true;
andrewm@0: }
andrewm@0:
andrewm@0: bool RingModulatorAudioProcessor::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 RingModulatorAudioProcessor::getTailLengthSeconds() const
andrewm@0: {
andrewm@0: return 0.0;
andrewm@0: }
andrewm@0:
andrewm@0: bool RingModulatorAudioProcessor::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 RingModulatorAudioProcessor::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 RingModulatorAudioProcessor::getNumPrograms()
andrewm@0: {
andrewm@0: return 0;
andrewm@0: }
andrewm@0:
andrewm@0: int RingModulatorAudioProcessor::getCurrentProgram()
andrewm@0: {
andrewm@0: return 0;
andrewm@0: }
andrewm@0:
andrewm@0: void RingModulatorAudioProcessor::setCurrentProgram (int index)
andrewm@0: {
andrewm@0: }
andrewm@0:
andrewm@0: const String RingModulatorAudioProcessor::getProgramName (int index)
andrewm@0: {
andrewm@0: return String::empty;
andrewm@0: }
andrewm@0:
andrewm@0: void RingModulatorAudioProcessor::changeProgramName (int index, const String& newName)
andrewm@0: {
andrewm@0: }
andrewm@0:
andrewm@0: //==============================================================================
andrewm@0: void RingModulatorAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
andrewm@0: {
andrewm@0: // Calculations that happen before play begins. Pretty simple in this effect, just
andrewm@0: // reset the previous state.
andrewm@0: lfoPhase_ = 0.0;
andrewm@0: carrierPhase_ = 0.0;
andrewm@0: inverseSampleRate_ = 1.0/sampleRate;
andrewm@0: }
andrewm@0:
andrewm@0: void RingModulatorAudioProcessor::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:
andrewm@0: void RingModulatorAudioProcessor::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: carrierPhase_ = 0.0;
andrewm@0: }
andrewm@0:
andrewm@0:
andrewm@0: void RingModulatorAudioProcessor::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;
andrewm@0: float cph, lph;
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: // 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: cph = carrierPhase_;
andrewm@0: lph = lfoPhase_;
andrewm@0:
andrewm@0: for (int i = 0; i < numSamples; ++i)
andrewm@0: {
andrewm@0: const float in = channelData[i];
andrewm@0:
andrewm@0: // Ring modulation is easy! Just multiply the waveform by a periodic carrier
andrewm@0: channelData[i] = in * sinf(2.0 * M_PI * cph);
andrewm@0:
andrewm@0: // Update the carrier and LFO phases, keeping them in the range 0-1
andrewm@0: lph += lfoFrequency_*inverseSampleRate_;
andrewm@0: if(lph >= 1.0)
andrewm@0: lph -= 1.0;
andrewm@0: cph += (carrierFrequency_ + sweepWidth_*lfo(lfoPhase_, waveform_))*inverseSampleRate_;
andrewm@0: if(cph >= 1.0)
andrewm@0: cph -= 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: carrierPhase_ = cph;
andrewm@0: lfoPhase_ = lph;
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 RingModulatorAudioProcessor::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* RingModulatorAudioProcessor::createEditor()
andrewm@0: {
andrewm@0: return new RingModulatorAudioProcessorEditor (this);
andrewm@0: }
andrewm@0:
andrewm@0: //==============================================================================
andrewm@0: void RingModulatorAudioProcessor::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("carrierFrequency", carrierFrequency_);
andrewm@0: xml.setAttribute("sweepWidth", sweepWidth_);
andrewm@0: xml.setAttribute("lfoFrequency", lfoFrequency_);
andrewm@0: xml.setAttribute("waveform", waveform_);
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 RingModulatorAudioProcessor::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: carrierFrequency_ = (float)xmlState->getDoubleAttribute("carrierFrequency", carrierFrequency_);
andrewm@0: sweepWidth_ = (float)xmlState->getDoubleAttribute("sweepWidth", sweepWidth_);
andrewm@0: lfoFrequency_ = (float)xmlState->getDoubleAttribute("lfoFrequency", lfoFrequency_);
andrewm@0: waveform_ = xmlState->getIntAttribute("waveform", waveform_);
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 -1 to 1 (note: not 0 to 1 as in delay-based effects)
andrewm@0: float RingModulatorAudioProcessor::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 4.0f*phase;
andrewm@0: else if(phase < 0.75f)
andrewm@0: return 1.0f - 4.0f*(phase - 0.25f);
andrewm@0: else
andrewm@0: return -1.0f + 4.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 -1.0f;
andrewm@0: case kWaveformSawtooth:
andrewm@0: if(phase < 0.5f)
andrewm@0: return 2.0f*phase;
andrewm@0: else
andrewm@0: return 2.0f*phase - 2.0f;
andrewm@0: case kWaveformInverseSawtooth:
andrewm@0: if(phase < 0.5f)
andrewm@0: return -2.0f*phase;
andrewm@0: else
andrewm@0: return 2.0f - 2.0f*phase;
andrewm@0: case kWaveformSine:
andrewm@0: default:
andrewm@0: return 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 RingModulatorAudioProcessor();
andrewm@0: }