annotate effects/ringmod/Source/PluginProcessor.cpp @ 1:04e171d2a747 tip

JUCE 4 compatible. Standardised paths on Mac: modules '../../juce/modules'; VST folder '~/SDKs/vstsdk2.4' (JUCE default). Replaced deprecated 'getSampleData(channel)'; getToggleState(...); setToggleState(...); setSelectedId(...). Removed unused variables. Ignore JUCE code and build files.
author Brecht De Man <b.deman@qmul.ac.uk>
date Sun, 22 Nov 2015 15:23:40 +0000
parents e32fe563e124
children
rev   line source
andrewm@0 1 /*
andrewm@0 2 This code accompanies the textbook:
andrewm@0 3
andrewm@0 4 Digital Audio Effects: Theory, Implementation and Application
andrewm@0 5 Joshua D. Reiss and Andrew P. McPherson
andrewm@0 6
andrewm@0 7 ---
andrewm@0 8
andrewm@0 9 Ring Modulator: modulation using a carrier oscillator
andrewm@0 10 See textbook Chapter 5: Amplitude Modulation
andrewm@0 11
andrewm@0 12 Code by Andrew McPherson, Brecht De Man and Joshua Reiss
andrewm@0 13
andrewm@0 14 ---
andrewm@0 15
andrewm@0 16 This program is free software: you can redistribute it and/or modify
andrewm@0 17 it under the terms of the GNU General Public License as published by
andrewm@0 18 the Free Software Foundation, either version 3 of the License, or
andrewm@0 19 (at your option) any later version.
andrewm@0 20
andrewm@0 21 This program is distributed in the hope that it will be useful,
andrewm@0 22 but WITHOUT ANY WARRANTY; without even the implied warranty of
andrewm@0 23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
andrewm@0 24 GNU General Public License for more details.
andrewm@0 25
andrewm@0 26 You should have received a copy of the GNU General Public License
andrewm@0 27 along with this program. If not, see <http://www.gnu.org/licenses/>.
andrewm@0 28 */
andrewm@0 29
andrewm@0 30 #include "PluginProcessor.h"
andrewm@0 31 #include "PluginEditor.h"
andrewm@0 32 #include <math.h>
andrewm@0 33
andrewm@0 34 //==============================================================================
andrewm@0 35 RingModulatorAudioProcessor::RingModulatorAudioProcessor()
andrewm@0 36 {
andrewm@0 37 // Set default values:
andrewm@0 38 carrierFrequency_ = 100.0;
andrewm@0 39 sweepWidth_ = 0.0;
andrewm@0 40 lfoFrequency_ = 1.0;
andrewm@0 41 waveform_ = kWaveformSine;
andrewm@0 42
andrewm@0 43 lfoPhase_ = carrierPhase_ = 0.0;
andrewm@0 44 inverseSampleRate_ = 1.0/44100.0;
andrewm@0 45
andrewm@0 46 lastUIWidth_ = 370;
andrewm@0 47 lastUIHeight_ = 160;
andrewm@0 48 }
andrewm@0 49
andrewm@0 50 RingModulatorAudioProcessor::~RingModulatorAudioProcessor()
andrewm@0 51 {
andrewm@0 52 }
andrewm@0 53
andrewm@0 54 //==============================================================================
andrewm@0 55 const String RingModulatorAudioProcessor::getName() const
andrewm@0 56 {
andrewm@0 57 return JucePlugin_Name;
andrewm@0 58 }
andrewm@0 59
andrewm@0 60 int RingModulatorAudioProcessor::getNumParameters()
andrewm@0 61 {
andrewm@0 62 return kNumParameters;
andrewm@0 63 }
andrewm@0 64
andrewm@0 65 float RingModulatorAudioProcessor::getParameter (int index)
andrewm@0 66 {
andrewm@0 67 // This method will be called by the host, probably on the audio thread, so
andrewm@0 68 // it's absolutely time-critical. Don't use critical sections or anything
andrewm@0 69 // UI-related, or anything at all that may block in any way!
andrewm@0 70 switch (index)
andrewm@0 71 {
andrewm@0 72 case kCarrierFrequencyParam: return carrierFrequency_;
andrewm@0 73 case kSweepWidthParam: return sweepWidth_;
andrewm@0 74 case kLFOFrequencyParam: return lfoFrequency_;
andrewm@0 75 case kWaveformParam: return (float)waveform_;
andrewm@0 76 default: return 0.0f;
andrewm@0 77 }
andrewm@0 78 }
andrewm@0 79
andrewm@0 80 void RingModulatorAudioProcessor::setParameter (int index, float newValue)
andrewm@0 81 {
andrewm@0 82 // This method will be called by the host, probably on the audio thread, so
andrewm@0 83 // it's absolutely time-critical. Don't use critical sections or anything
andrewm@0 84 // UI-related, or anything at all that may block in any way!
andrewm@0 85
andrewm@0 86 switch (index)
andrewm@0 87 {
andrewm@0 88 case kCarrierFrequencyParam:
andrewm@0 89 carrierFrequency_ = newValue;
andrewm@0 90 break;
andrewm@0 91 case kSweepWidthParam:
andrewm@0 92 sweepWidth_ = newValue;
andrewm@0 93 break;
andrewm@0 94 case kLFOFrequencyParam:
andrewm@0 95 lfoFrequency_ = newValue;
andrewm@0 96 break;
andrewm@0 97 case kWaveformParam:
andrewm@0 98 waveform_ = (int)newValue;
andrewm@0 99 break;
andrewm@0 100 default:
andrewm@0 101 break;
andrewm@0 102 }
andrewm@0 103 }
andrewm@0 104
andrewm@0 105 const String RingModulatorAudioProcessor::getParameterName (int index)
andrewm@0 106 {
andrewm@0 107 switch (index)
andrewm@0 108 {
andrewm@0 109 case kCarrierFrequencyParam: return "carrier frequency";
andrewm@0 110 case kSweepWidthParam: return "LFO sweep width";
andrewm@0 111 case kLFOFrequencyParam: return "LFO frequency";
andrewm@0 112 case kWaveformParam: return "LFO waveform";
andrewm@0 113 default: break;
andrewm@0 114 }
andrewm@0 115
andrewm@0 116 return String::empty;
andrewm@0 117 }
andrewm@0 118
andrewm@0 119 const String RingModulatorAudioProcessor::getParameterText (int index)
andrewm@0 120 {
andrewm@0 121 return String (getParameter (index), 2);
andrewm@0 122 }
andrewm@0 123
andrewm@0 124 const String RingModulatorAudioProcessor::getInputChannelName (int channelIndex) const
andrewm@0 125 {
andrewm@0 126 return String (channelIndex + 1);
andrewm@0 127 }
andrewm@0 128
andrewm@0 129 const String RingModulatorAudioProcessor::getOutputChannelName (int channelIndex) const
andrewm@0 130 {
andrewm@0 131 return String (channelIndex + 1);
andrewm@0 132 }
andrewm@0 133
andrewm@0 134 bool RingModulatorAudioProcessor::isInputChannelStereoPair (int index) const
andrewm@0 135 {
andrewm@0 136 return true;
andrewm@0 137 }
andrewm@0 138
andrewm@0 139 bool RingModulatorAudioProcessor::isOutputChannelStereoPair (int index) const
andrewm@0 140 {
andrewm@0 141 return true;
andrewm@0 142 }
andrewm@0 143
andrewm@0 144 bool RingModulatorAudioProcessor::silenceInProducesSilenceOut() const
andrewm@0 145 {
andrewm@0 146 #if JucePlugin_SilenceInProducesSilenceOut
andrewm@0 147 return true;
andrewm@0 148 #else
andrewm@0 149 return false;
andrewm@0 150 #endif
andrewm@0 151 }
andrewm@0 152
andrewm@0 153 double RingModulatorAudioProcessor::getTailLengthSeconds() const
andrewm@0 154 {
andrewm@0 155 return 0.0;
andrewm@0 156 }
andrewm@0 157
andrewm@0 158 bool RingModulatorAudioProcessor::acceptsMidi() const
andrewm@0 159 {
andrewm@0 160 #if JucePlugin_WantsMidiInput
andrewm@0 161 return true;
andrewm@0 162 #else
andrewm@0 163 return false;
andrewm@0 164 #endif
andrewm@0 165 }
andrewm@0 166
andrewm@0 167 bool RingModulatorAudioProcessor::producesMidi() const
andrewm@0 168 {
andrewm@0 169 #if JucePlugin_ProducesMidiOutput
andrewm@0 170 return true;
andrewm@0 171 #else
andrewm@0 172 return false;
andrewm@0 173 #endif
andrewm@0 174 }
andrewm@0 175
andrewm@0 176 int RingModulatorAudioProcessor::getNumPrograms()
andrewm@0 177 {
andrewm@0 178 return 0;
andrewm@0 179 }
andrewm@0 180
andrewm@0 181 int RingModulatorAudioProcessor::getCurrentProgram()
andrewm@0 182 {
andrewm@0 183 return 0;
andrewm@0 184 }
andrewm@0 185
andrewm@0 186 void RingModulatorAudioProcessor::setCurrentProgram (int index)
andrewm@0 187 {
andrewm@0 188 }
andrewm@0 189
andrewm@0 190 const String RingModulatorAudioProcessor::getProgramName (int index)
andrewm@0 191 {
andrewm@0 192 return String::empty;
andrewm@0 193 }
andrewm@0 194
andrewm@0 195 void RingModulatorAudioProcessor::changeProgramName (int index, const String& newName)
andrewm@0 196 {
andrewm@0 197 }
andrewm@0 198
andrewm@0 199 //==============================================================================
andrewm@0 200 void RingModulatorAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
andrewm@0 201 {
andrewm@0 202 // Calculations that happen before play begins. Pretty simple in this effect, just
andrewm@0 203 // reset the previous state.
andrewm@0 204 lfoPhase_ = 0.0;
andrewm@0 205 carrierPhase_ = 0.0;
andrewm@0 206 inverseSampleRate_ = 1.0/sampleRate;
andrewm@0 207 }
andrewm@0 208
andrewm@0 209 void RingModulatorAudioProcessor::releaseResources()
andrewm@0 210 {
andrewm@0 211 // When playback stops, you can use this as an opportunity to free up any
andrewm@0 212 // spare memory, etc.
andrewm@0 213 }
andrewm@0 214
andrewm@0 215 void RingModulatorAudioProcessor::reset()
andrewm@0 216 {
andrewm@0 217 // Use this method as the place to clear any delay lines, buffers, etc, as it
andrewm@0 218 // means there's been a break in the audio's continuity.
andrewm@0 219
andrewm@0 220 lfoPhase_ = 0.0;
andrewm@0 221 carrierPhase_ = 0.0;
andrewm@0 222 }
andrewm@0 223
andrewm@0 224
andrewm@0 225 void RingModulatorAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
andrewm@0 226 {
andrewm@0 227 // Helpful information about this block of samples:
andrewm@0 228 const int numInputChannels = getNumInputChannels(); // How many input channels for our effect?
andrewm@0 229 const int numOutputChannels = getNumOutputChannels(); // How many output channels for our effect?
andrewm@0 230 const int numSamples = buffer.getNumSamples(); // How many samples in the buffer for this block?
andrewm@0 231
andrewm@0 232 int channel;
andrewm@0 233 float cph, lph;
andrewm@0 234
andrewm@0 235 // Go through each channel of audio that's passed in. In this example we apply identical
andrewm@0 236 // effects to each channel, regardless of how many input channels there are. For some effects, like
andrewm@0 237 // a stereo chorus or panner, you might do something different for each channel.
andrewm@0 238
andrewm@0 239 for (channel = 0; channel < numInputChannels; ++channel)
andrewm@0 240 {
andrewm@0 241 // channelData is an array of length numSamples which contains the audio for one channel
b@1 242 float* channelData = buffer.getWritePointer(channel);
andrewm@0 243
andrewm@0 244 // Make a temporary copy of any state variables declared in PluginProcessor.h which need to be
andrewm@0 245 // maintained between calls to processBlock(). Each channel needs to be processed identically
andrewm@0 246 // which means that the activity of processing one channel can't affect the state variable for
andrewm@0 247 // the next channel.
andrewm@0 248
andrewm@0 249 cph = carrierPhase_;
andrewm@0 250 lph = lfoPhase_;
andrewm@0 251
andrewm@0 252 for (int i = 0; i < numSamples; ++i)
andrewm@0 253 {
andrewm@0 254 const float in = channelData[i];
andrewm@0 255
andrewm@0 256 // Ring modulation is easy! Just multiply the waveform by a periodic carrier
andrewm@0 257 channelData[i] = in * sinf(2.0 * M_PI * cph);
andrewm@0 258
andrewm@0 259 // Update the carrier and LFO phases, keeping them in the range 0-1
andrewm@0 260 lph += lfoFrequency_*inverseSampleRate_;
andrewm@0 261 if(lph >= 1.0)
andrewm@0 262 lph -= 1.0;
andrewm@0 263 cph += (carrierFrequency_ + sweepWidth_*lfo(lfoPhase_, waveform_))*inverseSampleRate_;
andrewm@0 264 if(cph >= 1.0)
andrewm@0 265 cph -= 1.0;
andrewm@0 266 }
andrewm@0 267 }
andrewm@0 268
andrewm@0 269 // Having made a local copy of the state variables for each channel, now transfer the result
andrewm@0 270 // back to the main state variable so they will be preserved for the next call of processBlock()
andrewm@0 271
andrewm@0 272 carrierPhase_ = cph;
andrewm@0 273 lfoPhase_ = lph;
andrewm@0 274
andrewm@0 275 // In case we have more outputs than inputs, we'll clear any output
andrewm@0 276 // channels that didn't contain input data, (because these aren't
andrewm@0 277 // guaranteed to be empty - they may contain garbage).
andrewm@0 278 for (int i = numInputChannels; i < numOutputChannels; ++i)
andrewm@0 279 {
andrewm@0 280 buffer.clear (i, 0, buffer.getNumSamples());
andrewm@0 281 }
andrewm@0 282 }
andrewm@0 283
andrewm@0 284 //==============================================================================
andrewm@0 285 bool RingModulatorAudioProcessor::hasEditor() const
andrewm@0 286 {
andrewm@0 287 return true; // (change this to false if you choose to not supply an editor)
andrewm@0 288 }
andrewm@0 289
andrewm@0 290 AudioProcessorEditor* RingModulatorAudioProcessor::createEditor()
andrewm@0 291 {
andrewm@0 292 return new RingModulatorAudioProcessorEditor (this);
andrewm@0 293 }
andrewm@0 294
andrewm@0 295 //==============================================================================
andrewm@0 296 void RingModulatorAudioProcessor::getStateInformation (MemoryBlock& destData)
andrewm@0 297 {
andrewm@0 298 // You should use this method to store your parameters in the memory block.
andrewm@0 299 // You could do that either as raw data, or use the XML or ValueTree classes
andrewm@0 300 // as intermediaries to make it easy to save and load complex data.
andrewm@0 301
andrewm@0 302 // Create an outer XML element..
andrewm@0 303 XmlElement xml("C4DMPLUGINSETTINGS");
andrewm@0 304
andrewm@0 305 // add some attributes to it..
andrewm@0 306 xml.setAttribute("uiWidth", lastUIWidth_);
andrewm@0 307 xml.setAttribute("uiHeight", lastUIHeight_);
andrewm@0 308 xml.setAttribute("carrierFrequency", carrierFrequency_);
andrewm@0 309 xml.setAttribute("sweepWidth", sweepWidth_);
andrewm@0 310 xml.setAttribute("lfoFrequency", lfoFrequency_);
andrewm@0 311 xml.setAttribute("waveform", waveform_);
andrewm@0 312
andrewm@0 313 // then use this helper function to stuff it into the binary blob and return it..
andrewm@0 314 copyXmlToBinary(xml, destData);
andrewm@0 315 }
andrewm@0 316
andrewm@0 317 void RingModulatorAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
andrewm@0 318 {
andrewm@0 319 // You should use this method to restore your parameters from this memory block,
andrewm@0 320 // whose contents will have been created by the getStateInformation() call.
andrewm@0 321
andrewm@0 322 // This getXmlFromBinary() helper function retrieves our XML from the binary blob..
andrewm@0 323 ScopedPointer<XmlElement> xmlState (getXmlFromBinary (data, sizeInBytes));
andrewm@0 324
andrewm@0 325 if(xmlState != 0)
andrewm@0 326 {
andrewm@0 327 // make sure that it's actually our type of XML object..
andrewm@0 328 if(xmlState->hasTagName("C4DMPLUGINSETTINGS"))
andrewm@0 329 {
andrewm@0 330 // ok, now pull out our parameters..
andrewm@0 331 lastUIWidth_ = xmlState->getIntAttribute("uiWidth", lastUIWidth_);
andrewm@0 332 lastUIHeight_ = xmlState->getIntAttribute("uiHeight", lastUIHeight_);
andrewm@0 333
andrewm@0 334 carrierFrequency_ = (float)xmlState->getDoubleAttribute("carrierFrequency", carrierFrequency_);
andrewm@0 335 sweepWidth_ = (float)xmlState->getDoubleAttribute("sweepWidth", sweepWidth_);
andrewm@0 336 lfoFrequency_ = (float)xmlState->getDoubleAttribute("lfoFrequency", lfoFrequency_);
andrewm@0 337 waveform_ = xmlState->getIntAttribute("waveform", waveform_);
andrewm@0 338 }
andrewm@0 339 }
andrewm@0 340 }
andrewm@0 341
andrewm@0 342 //==============================================================================
andrewm@0 343 // Function for calculating LFO waveforms. Phase runs from 0-1, output is scaled
andrewm@0 344 // from -1 to 1 (note: not 0 to 1 as in delay-based effects)
andrewm@0 345 float RingModulatorAudioProcessor::lfo(float phase, int waveform)
andrewm@0 346 {
andrewm@0 347 switch(waveform)
andrewm@0 348 {
andrewm@0 349 case kWaveformTriangle:
andrewm@0 350 if(phase < 0.25f)
andrewm@0 351 return 4.0f*phase;
andrewm@0 352 else if(phase < 0.75f)
andrewm@0 353 return 1.0f - 4.0f*(phase - 0.25f);
andrewm@0 354 else
andrewm@0 355 return -1.0f + 4.0f*(phase - 0.75f);
andrewm@0 356 case kWaveformSquare:
andrewm@0 357 if(phase < 0.5f)
andrewm@0 358 return 1.0f;
andrewm@0 359 else
andrewm@0 360 return -1.0f;
andrewm@0 361 case kWaveformSawtooth:
andrewm@0 362 if(phase < 0.5f)
andrewm@0 363 return 2.0f*phase;
andrewm@0 364 else
andrewm@0 365 return 2.0f*phase - 2.0f;
andrewm@0 366 case kWaveformInverseSawtooth:
andrewm@0 367 if(phase < 0.5f)
andrewm@0 368 return -2.0f*phase;
andrewm@0 369 else
andrewm@0 370 return 2.0f - 2.0f*phase;
andrewm@0 371 case kWaveformSine:
andrewm@0 372 default:
andrewm@0 373 return sinf(2.0 * M_PI * phase);
andrewm@0 374 }
andrewm@0 375 }
andrewm@0 376
andrewm@0 377 //==============================================================================
andrewm@0 378 // This creates new instances of the plugin..
andrewm@0 379 AudioProcessor* JUCE_CALLTYPE createPluginFilter()
andrewm@0 380 {
andrewm@0 381 return new RingModulatorAudioProcessor();
andrewm@0 382 }