annotate effects/vibrato/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 Vibrato: frequency modulation using delay lines
andrewm@0 10 See textbook Chapter 2: Delay Line Effects
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 const float VibratoAudioProcessor::kMaximumSweepWidth = 0.05;
andrewm@0 35
andrewm@0 36 //==============================================================================
andrewm@0 37 VibratoAudioProcessor::VibratoAudioProcessor() : delayBuffer_ (2, 1)
andrewm@0 38 {
andrewm@0 39 // Set default values:
andrewm@0 40 sweepWidth_ = .01;
andrewm@0 41 frequency_ = 2.0;
andrewm@0 42 waveform_ = kWaveformSine;
andrewm@0 43 interpolation_ = kInterpolationLinear;
andrewm@0 44
andrewm@0 45 delayBufferLength_ = 1;
andrewm@0 46 lfoPhase_ = 0.0;
andrewm@0 47 inverseSampleRate_ = 1.0/44100.0;
andrewm@0 48
andrewm@0 49 // Start the circular buffer pointer at the beginning
andrewm@0 50 delayWritePosition_ = 0;
andrewm@0 51
andrewm@0 52 lastUIWidth_ = 370;
andrewm@0 53 lastUIHeight_ = 160;
andrewm@0 54 }
andrewm@0 55
andrewm@0 56 VibratoAudioProcessor::~VibratoAudioProcessor()
andrewm@0 57 {
andrewm@0 58 }
andrewm@0 59
andrewm@0 60 //==============================================================================
andrewm@0 61 const String VibratoAudioProcessor::getName() const
andrewm@0 62 {
andrewm@0 63 return JucePlugin_Name;
andrewm@0 64 }
andrewm@0 65
andrewm@0 66 int VibratoAudioProcessor::getNumParameters()
andrewm@0 67 {
andrewm@0 68 return kNumParameters;
andrewm@0 69 }
andrewm@0 70
andrewm@0 71 float VibratoAudioProcessor::getParameter (int index)
andrewm@0 72 {
andrewm@0 73 // This method will be called by the host, probably on the audio thread, so
andrewm@0 74 // it's absolutely time-critical. Don't use critical sections or anything
andrewm@0 75 // UI-related, or anything at all that may block in any way!
andrewm@0 76 switch (index)
andrewm@0 77 {
andrewm@0 78 case kSweepWidthParam: return sweepWidth_;
andrewm@0 79 case kFrequencyParam: return frequency_;
andrewm@0 80 case kWaveformParam: return (float)waveform_;
andrewm@0 81 case kInterpolationParam: return (float)interpolation_;
andrewm@0 82 default: return 0.0f;
andrewm@0 83 }
andrewm@0 84 }
andrewm@0 85
andrewm@0 86 void VibratoAudioProcessor::setParameter (int index, float newValue)
andrewm@0 87 {
andrewm@0 88 // This method will be called by the host, probably on the audio thread, so
andrewm@0 89 // it's absolutely time-critical. Don't use critical sections or anything
andrewm@0 90 // UI-related, or anything at all that may block in any way!
andrewm@0 91
andrewm@0 92 switch (index)
andrewm@0 93 {
andrewm@0 94 case kSweepWidthParam:
andrewm@0 95 sweepWidth_ = newValue;
andrewm@0 96 break;
andrewm@0 97 case kFrequencyParam:
andrewm@0 98 frequency_ = newValue;
andrewm@0 99 break;
andrewm@0 100 case kWaveformParam:
andrewm@0 101 waveform_ = (int)newValue;
andrewm@0 102 break;
andrewm@0 103 case kInterpolationParam:
andrewm@0 104 interpolation_ = (int)newValue;
andrewm@0 105 break;
andrewm@0 106 default:
andrewm@0 107 break;
andrewm@0 108 }
andrewm@0 109 }
andrewm@0 110
andrewm@0 111 const String VibratoAudioProcessor::getParameterName (int index)
andrewm@0 112 {
andrewm@0 113 switch (index)
andrewm@0 114 {
andrewm@0 115 case kSweepWidthParam: return "sweep width";
andrewm@0 116 case kFrequencyParam: return "frequency";
andrewm@0 117 case kWaveformParam: return "waveform";
andrewm@0 118 case kInterpolationParam: return "interpolation";
andrewm@0 119 default: break;
andrewm@0 120 }
andrewm@0 121
andrewm@0 122 return String::empty;
andrewm@0 123 }
andrewm@0 124
andrewm@0 125 const String VibratoAudioProcessor::getParameterText (int index)
andrewm@0 126 {
andrewm@0 127 return String (getParameter (index), 2);
andrewm@0 128 }
andrewm@0 129
andrewm@0 130 const String VibratoAudioProcessor::getInputChannelName (int channelIndex) const
andrewm@0 131 {
andrewm@0 132 return String (channelIndex + 1);
andrewm@0 133 }
andrewm@0 134
andrewm@0 135 const String VibratoAudioProcessor::getOutputChannelName (int channelIndex) const
andrewm@0 136 {
andrewm@0 137 return String (channelIndex + 1);
andrewm@0 138 }
andrewm@0 139
andrewm@0 140 bool VibratoAudioProcessor::isInputChannelStereoPair (int index) const
andrewm@0 141 {
andrewm@0 142 return true;
andrewm@0 143 }
andrewm@0 144
andrewm@0 145 bool VibratoAudioProcessor::isOutputChannelStereoPair (int index) const
andrewm@0 146 {
andrewm@0 147 return true;
andrewm@0 148 }
andrewm@0 149
andrewm@0 150 bool VibratoAudioProcessor::silenceInProducesSilenceOut() const
andrewm@0 151 {
andrewm@0 152 #if JucePlugin_SilenceInProducesSilenceOut
andrewm@0 153 return true;
andrewm@0 154 #else
andrewm@0 155 return false;
andrewm@0 156 #endif
andrewm@0 157 }
andrewm@0 158
andrewm@0 159 double VibratoAudioProcessor::getTailLengthSeconds() const
andrewm@0 160 {
andrewm@0 161 return 0.0;
andrewm@0 162 }
andrewm@0 163
andrewm@0 164 bool VibratoAudioProcessor::acceptsMidi() const
andrewm@0 165 {
andrewm@0 166 #if JucePlugin_WantsMidiInput
andrewm@0 167 return true;
andrewm@0 168 #else
andrewm@0 169 return false;
andrewm@0 170 #endif
andrewm@0 171 }
andrewm@0 172
andrewm@0 173 bool VibratoAudioProcessor::producesMidi() const
andrewm@0 174 {
andrewm@0 175 #if JucePlugin_ProducesMidiOutput
andrewm@0 176 return true;
andrewm@0 177 #else
andrewm@0 178 return false;
andrewm@0 179 #endif
andrewm@0 180 }
andrewm@0 181
andrewm@0 182 int VibratoAudioProcessor::getNumPrograms()
andrewm@0 183 {
andrewm@0 184 return 0;
andrewm@0 185 }
andrewm@0 186
andrewm@0 187 int VibratoAudioProcessor::getCurrentProgram()
andrewm@0 188 {
andrewm@0 189 return 0;
andrewm@0 190 }
andrewm@0 191
andrewm@0 192 void VibratoAudioProcessor::setCurrentProgram (int index)
andrewm@0 193 {
andrewm@0 194 }
andrewm@0 195
andrewm@0 196 const String VibratoAudioProcessor::getProgramName (int index)
andrewm@0 197 {
andrewm@0 198 return String::empty;
andrewm@0 199 }
andrewm@0 200
andrewm@0 201 void VibratoAudioProcessor::changeProgramName (int index, const String& newName)
andrewm@0 202 {
andrewm@0 203 }
andrewm@0 204
andrewm@0 205 //==============================================================================
andrewm@0 206 void VibratoAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
andrewm@0 207 {
andrewm@0 208 // Allocate and zero the delay buffer (size will depend on current sample rate)
andrewm@0 209 // Add 3 extra samples to allow cubic interpolation even at maximum delay
andrewm@0 210 delayBufferLength_ = (int)(kMaximumSweepWidth*sampleRate) + 3;
andrewm@0 211 delayBuffer_.setSize(2, delayBufferLength_);
andrewm@0 212 delayBuffer_.clear();
andrewm@0 213 lfoPhase_ = 0.0;
andrewm@0 214
andrewm@0 215 inverseSampleRate_ = 1.0/sampleRate;
andrewm@0 216 }
andrewm@0 217
andrewm@0 218 void VibratoAudioProcessor::releaseResources()
andrewm@0 219 {
andrewm@0 220 // When playback stops, you can use this as an opportunity to free up any
andrewm@0 221 // spare memory, etc.
andrewm@0 222
andrewm@0 223 // The delay buffer will stay in memory until the effect is unloaded.
andrewm@0 224 }
andrewm@0 225
andrewm@0 226 void VibratoAudioProcessor::reset()
andrewm@0 227 {
andrewm@0 228 // Use this method as the place to clear any delay lines, buffers, etc, as it
andrewm@0 229 // means there's been a break in the audio's continuity.
andrewm@0 230
andrewm@0 231 delayBuffer_.clear();
andrewm@0 232 }
andrewm@0 233
andrewm@0 234
andrewm@0 235 void VibratoAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
andrewm@0 236 {
andrewm@0 237 // Helpful information about this block of samples:
andrewm@0 238 const int numInputChannels = getNumInputChannels(); // How many input channels for our effect?
andrewm@0 239 const int numOutputChannels = getNumOutputChannels(); // How many output channels for our effect?
andrewm@0 240 const int numSamples = buffer.getNumSamples(); // How many samples in the buffer for this block?
andrewm@0 241
andrewm@0 242 int channel, dpw; // dpr = delay read pointer; dpw = delay write pointer
andrewm@0 243 float dpr, currentDelay, ph;
andrewm@0 244
andrewm@0 245 // Go through each channel of audio that's passed in. In this example we apply identical
andrewm@0 246 // effects to each channel, regardless of how many input channels there are. For some effects, like
andrewm@0 247 // a stereo chorus or panner, you might do something different for each channel.
andrewm@0 248
andrewm@0 249 for (channel = 0; channel < numInputChannels; ++channel)
andrewm@0 250 {
andrewm@0 251 // channelData is an array of length numSamples which contains the audio for one channel
b@1 252 float* channelData = buffer.getWritePointer(channel);
andrewm@0 253
andrewm@0 254 // delayData is the circular buffer for implementing delay on this channel
b@1 255 float* delayData = delayBuffer_.getWritePointer (jmin (channel, delayBuffer_.getNumChannels() - 1));
andrewm@0 256
andrewm@0 257 // Make a temporary copy of any state variables declared in PluginProcessor.h which need to be
andrewm@0 258 // maintained between calls to processBlock(). Each channel needs to be processed identically
andrewm@0 259 // which means that the activity of processing one channel can't affect the state variable for
andrewm@0 260 // the next channel.
andrewm@0 261
andrewm@0 262 dpw = delayWritePosition_;
andrewm@0 263 ph = lfoPhase_;
andrewm@0 264
andrewm@0 265 for (int i = 0; i < numSamples; ++i)
andrewm@0 266 {
andrewm@0 267 const float in = channelData[i];
andrewm@0 268 float interpolatedSample = 0.0;
andrewm@0 269
andrewm@0 270 // Recalculate the read pointer position with respect to the write pointer. A more efficient
andrewm@0 271 // implementation might increment the read pointer based on the derivative of the LFO without
andrewm@0 272 // running the whole equation again, but this format makes the operation clearer.
andrewm@0 273
andrewm@0 274 currentDelay = sweepWidth_*lfo(ph, waveform_);
andrewm@0 275
andrewm@0 276 // Subtract 3 samples to the delay pointer to make sure we have enough previously written
andrewm@0 277 // samples to interpolate with
andrewm@0 278 dpr = fmodf((float)dpw - (float)(currentDelay * getSampleRate()) + (float)delayBufferLength_ - 3.0,
andrewm@0 279 (float)delayBufferLength_);
andrewm@0 280
andrewm@0 281 // In this example, the output is the input plus the contents of the delay buffer (weighted by delayMix)
andrewm@0 282 // The last term implements a tremolo (variable amplitude) on the whole thing.
andrewm@0 283
andrewm@0 284 if(interpolation_ == kInterpolationLinear)
andrewm@0 285 {
andrewm@0 286 // Find the fraction by which the read pointer sits between two
andrewm@0 287 // samples and use this to adjust weights of the samples
andrewm@0 288 float fraction = dpr - floorf(dpr);
andrewm@0 289 int previousSample = (int)floorf(dpr);
andrewm@0 290 int nextSample = (previousSample + 1) % delayBufferLength_;
andrewm@0 291 interpolatedSample = fraction*delayData[nextSample]
andrewm@0 292 + (1.0f-fraction)*delayData[previousSample];
andrewm@0 293 }
andrewm@0 294 else if(interpolation_ == kInterpolationCubic)
andrewm@0 295 {
andrewm@0 296 // Cubic interpolation will produce cleaner results at the expense
andrewm@0 297 // of more computation. This code uses the Catmull-Rom variant of
andrewm@0 298 // cubic interpolation. To reduce the load, calculate a few quantities
andrewm@0 299 // in advance that will be used several times in the equation:
andrewm@0 300
andrewm@0 301 int sample1 = (int)floorf(dpr);
andrewm@0 302 int sample2 = (sample1 + 1) % delayBufferLength_;
andrewm@0 303 int sample3 = (sample2 + 1) % delayBufferLength_;
andrewm@0 304 int sample0 = (sample1 - 1 + delayBufferLength_) % delayBufferLength_;
andrewm@0 305
andrewm@0 306 float fraction = dpr - floorf(dpr);
andrewm@0 307 float frsq = fraction*fraction;
andrewm@0 308
andrewm@0 309 float a0 = -0.5f*delayData[sample0] + 1.5f*delayData[sample1]
andrewm@0 310 - 1.5f*delayData[sample2] + 0.5f*delayData[sample3];
andrewm@0 311 float a1 = delayData[sample0] - 2.5f*delayData[sample1]
andrewm@0 312 + 2.0f*delayData[sample2] - 0.5f*delayData[sample3];
andrewm@0 313 float a2 = -0.5f*delayData[sample0] + 0.5f*delayData[sample2];
andrewm@0 314 float a3 = delayData[sample1];
andrewm@0 315
andrewm@0 316 interpolatedSample = a0*fraction*frsq + a1*frsq + a2*fraction + a3;
andrewm@0 317 }
andrewm@0 318 else // Nearest neighbour interpolation
andrewm@0 319 {
andrewm@0 320 // Find the nearest input sample by rounding the fractional index to the
andrewm@0 321 // nearest integer. It's possible this will round it to the end of the buffer,
andrewm@0 322 // in which case we need to roll it back to the beginning.
andrewm@0 323 int closestSample = (int)floorf(dpr + 0.5);
andrewm@0 324 if(closestSample == delayBufferLength_)
andrewm@0 325 closestSample = 0;
andrewm@0 326 interpolatedSample = delayData[closestSample];
andrewm@0 327 }
andrewm@0 328
andrewm@0 329 // Store the current information in the delay buffer. With feedback, what we read is
andrewm@0 330 // included in what gets stored in the buffer, otherwise it's just a simple delay line
andrewm@0 331 // of the input signal.
andrewm@0 332
andrewm@0 333 delayData[dpw] = in;
andrewm@0 334
andrewm@0 335 // Increment the write pointer at a constant rate. The read pointer will move at different
andrewm@0 336 // rates depending on the settings of the LFO, the delay and the sweep width.
andrewm@0 337
andrewm@0 338 if (++dpw >= delayBufferLength_)
andrewm@0 339 dpw = 0;
andrewm@0 340
andrewm@0 341 // Store the output sample in the buffer, replacing the input. In the vibrato effect,
andrewm@0 342 // the delaye sample is the only component of the output (no mixing with the dry signal)
andrewm@0 343 channelData[i] = interpolatedSample;
andrewm@0 344
andrewm@0 345 // Update the LFO phase, keeping it in the range 0-1
andrewm@0 346 ph += frequency_*inverseSampleRate_;
andrewm@0 347 if(ph >= 1.0)
andrewm@0 348 ph -= 1.0;
andrewm@0 349 }
andrewm@0 350 }
andrewm@0 351
andrewm@0 352 // Having made a local copy of the state variables for each channel, now transfer the result
andrewm@0 353 // back to the main state variable so they will be preserved for the next call of processBlock()
andrewm@0 354
andrewm@0 355 delayWritePosition_ = dpw;
andrewm@0 356 lfoPhase_ = ph;
andrewm@0 357
andrewm@0 358 // In case we have more outputs than inputs, we'll clear any output
andrewm@0 359 // channels that didn't contain input data, (because these aren't
andrewm@0 360 // guaranteed to be empty - they may contain garbage).
andrewm@0 361 for (int i = numInputChannels; i < numOutputChannels; ++i)
andrewm@0 362 {
andrewm@0 363 buffer.clear (i, 0, buffer.getNumSamples());
andrewm@0 364 }
andrewm@0 365 }
andrewm@0 366
andrewm@0 367 //==============================================================================
andrewm@0 368 bool VibratoAudioProcessor::hasEditor() const
andrewm@0 369 {
andrewm@0 370 return true; // (change this to false if you choose to not supply an editor)
andrewm@0 371 }
andrewm@0 372
andrewm@0 373 AudioProcessorEditor* VibratoAudioProcessor::createEditor()
andrewm@0 374 {
andrewm@0 375 return new VibratoAudioProcessorEditor (this);
andrewm@0 376 }
andrewm@0 377
andrewm@0 378 //==============================================================================
andrewm@0 379 void VibratoAudioProcessor::getStateInformation (MemoryBlock& destData)
andrewm@0 380 {
andrewm@0 381 // You should use this method to store your parameters in the memory block.
andrewm@0 382 // You could do that either as raw data, or use the XML or ValueTree classes
andrewm@0 383 // as intermediaries to make it easy to save and load complex data.
andrewm@0 384
andrewm@0 385 // Create an outer XML element..
andrewm@0 386 XmlElement xml("C4DMPLUGINSETTINGS");
andrewm@0 387
andrewm@0 388 // add some attributes to it..
andrewm@0 389 xml.setAttribute("uiWidth", lastUIWidth_);
andrewm@0 390 xml.setAttribute("uiHeight", lastUIHeight_);
andrewm@0 391 xml.setAttribute("sweepWidth", sweepWidth_);
andrewm@0 392 xml.setAttribute("frequency", frequency_);
andrewm@0 393 xml.setAttribute("waveform", waveform_);
andrewm@0 394 xml.setAttribute("interpolation", interpolation_);
andrewm@0 395
andrewm@0 396 // then use this helper function to stuff it into the binary blob and return it..
andrewm@0 397 copyXmlToBinary(xml, destData);
andrewm@0 398 }
andrewm@0 399
andrewm@0 400 void VibratoAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
andrewm@0 401 {
andrewm@0 402 // You should use this method to restore your parameters from this memory block,
andrewm@0 403 // whose contents will have been created by the getStateInformation() call.
andrewm@0 404
andrewm@0 405 // This getXmlFromBinary() helper function retrieves our XML from the binary blob..
andrewm@0 406 ScopedPointer<XmlElement> xmlState (getXmlFromBinary (data, sizeInBytes));
andrewm@0 407
andrewm@0 408 if(xmlState != 0)
andrewm@0 409 {
andrewm@0 410 // make sure that it's actually our type of XML object..
andrewm@0 411 if(xmlState->hasTagName("C4DMPLUGINSETTINGS"))
andrewm@0 412 {
andrewm@0 413 // ok, now pull out our parameters..
andrewm@0 414 lastUIWidth_ = xmlState->getIntAttribute("uiWidth", lastUIWidth_);
andrewm@0 415 lastUIHeight_ = xmlState->getIntAttribute("uiHeight", lastUIHeight_);
andrewm@0 416
andrewm@0 417 sweepWidth_ = (float)xmlState->getDoubleAttribute("sweepWidth", sweepWidth_);
andrewm@0 418 frequency_ = (float)xmlState->getDoubleAttribute("frequency", frequency_);
andrewm@0 419 waveform_ = xmlState->getIntAttribute("waveform", waveform_);
andrewm@0 420 interpolation_ = xmlState->getIntAttribute("interpolation", interpolation_);
andrewm@0 421 }
andrewm@0 422 }
andrewm@0 423 }
andrewm@0 424
andrewm@0 425 //==============================================================================
andrewm@0 426 // Function for calculating LFO waveforms. Phase runs from 0-1, output is scaled
andrewm@0 427 // from 0 to 1 (note: not -1 to 1 as would be typical of sine).
andrewm@0 428 float VibratoAudioProcessor::lfo(float phase, int waveform)
andrewm@0 429 {
andrewm@0 430 switch(waveform)
andrewm@0 431 {
andrewm@0 432 case kWaveformTriangle:
andrewm@0 433 if(phase < 0.25f)
andrewm@0 434 return 0.5f + 2.0f*phase;
andrewm@0 435 else if(phase < 0.75f)
andrewm@0 436 return 1.0f - 2.0f*(phase - 0.25f);
andrewm@0 437 else
andrewm@0 438 return 2.0f*(phase-0.75f);
andrewm@0 439 case kWaveformSquare:
andrewm@0 440 if(phase < 0.5f)
andrewm@0 441 return 1.0f;
andrewm@0 442 else
andrewm@0 443 return 0.0f;
andrewm@0 444 case kWaveformSawtooth:
andrewm@0 445 if(phase < 0.5f)
andrewm@0 446 return 0.5f + phase;
andrewm@0 447 else
andrewm@0 448 return phase - 0.5f;
andrewm@0 449 case kWaveformInverseSawtooth:
andrewm@0 450 if(phase < 0.5f)
andrewm@0 451 return 0.5f - phase;
andrewm@0 452 else
andrewm@0 453 return 1.5f - phase;
andrewm@0 454 case kWaveformSine:
andrewm@0 455 default:
andrewm@0 456 return 0.5f + 0.5f*sinf(2.0 * M_PI * phase);
andrewm@0 457 }
andrewm@0 458 }
andrewm@0 459
andrewm@0 460 //==============================================================================
andrewm@0 461 // This creates new instances of the plugin..
andrewm@0 462 AudioProcessor* JUCE_CALLTYPE createPluginFilter()
andrewm@0 463 {
andrewm@0 464 return new VibratoAudioProcessor();
andrewm@0 465 }