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