annotate effects/chorus/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 Chorus: chorus effect based on time-varying delays
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 ChorusAudioProcessor::kMaximumDelay = 0.05;
andrewm@0 35 const float ChorusAudioProcessor::kMaximumSweepWidth = 0.05;
andrewm@0 36
andrewm@0 37 //==============================================================================
andrewm@0 38 ChorusAudioProcessor::ChorusAudioProcessor() : delayBuffer_ (2, 1)
andrewm@0 39 {
andrewm@0 40 // Set default values:
andrewm@0 41 delay_ = .03;
andrewm@0 42 sweepWidth_ = .02;
andrewm@0 43 depth_ = 1.0;
andrewm@0 44 frequency_ = 0.2;
andrewm@0 45 waveform_ = kWaveformSine;
andrewm@0 46 interpolation_ = kInterpolationLinear;
andrewm@0 47 numVoices_ = 2;
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 ChorusAudioProcessor::~ChorusAudioProcessor()
andrewm@0 62 {
andrewm@0 63 }
andrewm@0 64
andrewm@0 65 //==============================================================================
andrewm@0 66 const String ChorusAudioProcessor::getName() const
andrewm@0 67 {
andrewm@0 68 return JucePlugin_Name;
andrewm@0 69 }
andrewm@0 70
andrewm@0 71 int ChorusAudioProcessor::getNumParameters()
andrewm@0 72 {
andrewm@0 73 return kNumParameters;
andrewm@0 74 }
andrewm@0 75
andrewm@0 76 float ChorusAudioProcessor::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 kFrequencyParam: return frequency_;
andrewm@0 87 case kWaveformParam: return (float)waveform_;
andrewm@0 88 case kInterpolationParam: return (float)interpolation_;
andrewm@0 89 case kNumVoicesParam: return (float)numVoices_;
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 ChorusAudioProcessor::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 kFrequencyParam:
andrewm@0 113 frequency_ = newValue;
andrewm@0 114 break;
andrewm@0 115 case kWaveformParam:
andrewm@0 116 waveform_ = (int)newValue;
andrewm@0 117 break;
andrewm@0 118 case kInterpolationParam:
andrewm@0 119 interpolation_ = (int)newValue;
andrewm@0 120 break;
andrewm@0 121 case kNumVoicesParam:
andrewm@0 122 numVoices_ = (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 ChorusAudioProcessor::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 kFrequencyParam: return "frequency";
andrewm@0 140 case kWaveformParam: return "waveform";
andrewm@0 141 case kInterpolationParam: return "interpolation";
andrewm@0 142 case kNumVoicesParam: return "number of voices";
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 ChorusAudioProcessor::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 ChorusAudioProcessor::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 ChorusAudioProcessor::getOutputChannelName (int channelIndex) const
andrewm@0 161 {
andrewm@0 162 return String (channelIndex + 1);
andrewm@0 163 }
andrewm@0 164
andrewm@0 165 bool ChorusAudioProcessor::isInputChannelStereoPair (int index) const
andrewm@0 166 {
andrewm@0 167 return true;
andrewm@0 168 }
andrewm@0 169
andrewm@0 170 bool ChorusAudioProcessor::isOutputChannelStereoPair (int index) const
andrewm@0 171 {
andrewm@0 172 return true;
andrewm@0 173 }
andrewm@0 174
andrewm@0 175 bool ChorusAudioProcessor::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 ChorusAudioProcessor::getTailLengthSeconds() const
andrewm@0 185 {
andrewm@0 186 return 0.0;
andrewm@0 187 }
andrewm@0 188
andrewm@0 189 bool ChorusAudioProcessor::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 ChorusAudioProcessor::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 ChorusAudioProcessor::getNumPrograms()
andrewm@0 208 {
andrewm@0 209 return 0;
andrewm@0 210 }
andrewm@0 211
andrewm@0 212 int ChorusAudioProcessor::getCurrentProgram()
andrewm@0 213 {
andrewm@0 214 return 0;
andrewm@0 215 }
andrewm@0 216
andrewm@0 217 void ChorusAudioProcessor::setCurrentProgram (int index)
andrewm@0 218 {
andrewm@0 219 }
andrewm@0 220
andrewm@0 221 const String ChorusAudioProcessor::getProgramName (int index)
andrewm@0 222 {
andrewm@0 223 return String::empty;
andrewm@0 224 }
andrewm@0 225
andrewm@0 226 void ChorusAudioProcessor::changeProgramName (int index, const String& newName)
andrewm@0 227 {
andrewm@0 228 }
andrewm@0 229
andrewm@0 230 //==============================================================================
andrewm@0 231 void ChorusAudioProcessor::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 ChorusAudioProcessor::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 ChorusAudioProcessor::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 ChorusAudioProcessor::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
andrewm@0 270 // Go through each channel of audio that's passed in. In this example we apply identical
andrewm@0 271 // effects to each channel, regardless of how many input channels there are. For some effects, like
andrewm@0 272 // a stereo chorus or panner, you might do something different for each channel.
andrewm@0 273
andrewm@0 274 for (channel = 0; channel < numInputChannels; ++channel)
andrewm@0 275 {
andrewm@0 276 // channelData is an array of length numSamples which contains the audio for one channel
b@1 277 float* channelData = buffer.getWritePointer(channel);
andrewm@0 278
andrewm@0 279 // delayData is the circular buffer for implementing delay on this channel
b@1 280 float* delayData = delayBuffer_.getWritePointer (jmin (channel, delayBuffer_.getNumChannels() - 1));
andrewm@0 281
andrewm@0 282 // Make a temporary copy of any state variables declared in PluginProcessor.h which need to be
andrewm@0 283 // maintained between calls to processBlock(). Each channel needs to be processed identically
andrewm@0 284 // which means that the activity of processing one channel can't affect the state variable for
andrewm@0 285 // the next channel.
andrewm@0 286
andrewm@0 287 dpw = delayWritePosition_;
andrewm@0 288 ph = lfoPhase_;
andrewm@0 289
andrewm@0 290 for (int i = 0; i < numSamples; ++i)
andrewm@0 291 {
andrewm@0 292 const float in = channelData[i];
andrewm@0 293 float interpolatedSample = 0.0;
andrewm@0 294 float phaseOffset = 0.0;
andrewm@0 295 float weight;
andrewm@0 296
andrewm@0 297 // Chorus can have more than 2 voices (where the original, undelayed signal counts as a voice).
andrewm@0 298 // In this implementation, all voices use the same LFO, but with different phase offsets. It
andrewm@0 299 // is also possible to use different waveforms and different frequencies for each voice.
andrewm@0 300
andrewm@0 301 for(int j = 0; j < numVoices_ - 1; ++j)
andrewm@0 302 {
andrewm@0 303 if(stereo_ != 0 && numVoices_ > 2)
andrewm@0 304 {
andrewm@0 305 // A stereo chorus pans each voice to a different location in the stereo field.
andrewm@0 306 // How this is done depends on the number of voices:
andrewm@0 307 // -- 2 voices: N/A (need at least 2 delayed voices for stereo chorus)
andrewm@0 308 // -- 3 voices: 1 voice left, 1 voice right (0, 1)
andrewm@0 309 // -- 4 voices: 1 voice left, 1 voice centre, 1 voice right (0, 0.5, 1)
andrewm@0 310 // -- 5 voices: 1 voice left, 1 voice left-centre,
andrewm@0 311 // 1 voice right-centre, 1 voice right (0, 0.33, 0.66, 1)
andrewm@0 312
andrewm@0 313 weight = (float)j/(float)(numVoices_ - 2);
andrewm@0 314
andrewm@0 315 // Left and right channels are mirrors of each other in weight
andrewm@0 316 if(channel != 0)
andrewm@0 317 weight = 1.0 - weight;
andrewm@0 318 }
andrewm@0 319 else
andrewm@0 320 weight = 1.0;
andrewm@0 321
andrewm@0 322 // Add the voice to the mix if it has nonzero weight
andrewm@0 323 if(weight != 0.0)
andrewm@0 324 {
andrewm@0 325 // Recalculate the read pointer position with respect to the write pointer. A more efficient
andrewm@0 326 // implementation might increment the read pointer based on the derivative of the LFO without
andrewm@0 327 // running the whole equation again, but this format makes the operation clearer.
andrewm@0 328
andrewm@0 329 currentDelay = delay_ + sweepWidth_*lfo(fmodf(ph + phaseOffset, 1.0f), waveform_);
andrewm@0 330 dpr = fmodf((float)dpw - (float)(currentDelay * getSampleRate()) + (float)delayBufferLength_,
andrewm@0 331 (float)delayBufferLength_);
andrewm@0 332
andrewm@0 333 // In this example, the output is the input plus the contents of the delay buffer (weighted by delayMix)
andrewm@0 334 // The last term implements a tremolo (variable amplitude) on the whole thing.
andrewm@0 335
andrewm@0 336 if(interpolation_ == kInterpolationLinear)
andrewm@0 337 {
andrewm@0 338 // Find the fraction by which the read pointer sits between two
andrewm@0 339 // samples and use this to adjust weights of the samples
andrewm@0 340 float fraction = dpr - floorf(dpr);
andrewm@0 341 int previousSample = (int)floorf(dpr);
andrewm@0 342 int nextSample = (previousSample + 1) % delayBufferLength_;
andrewm@0 343 interpolatedSample = fraction*delayData[nextSample]
andrewm@0 344 + (1.0f-fraction)*delayData[previousSample];
andrewm@0 345 }
andrewm@0 346 else if(interpolation_ == kInterpolationCubic)
andrewm@0 347 {
andrewm@0 348 // Cubic interpolation will produce cleaner results at the expense
andrewm@0 349 // of more computation. This code uses the Catmull-Rom variant of
andrewm@0 350 // cubic interpolation. To reduce the load, calculate a few quantities
andrewm@0 351 // in advance that will be used several times in the equation:
andrewm@0 352
andrewm@0 353 int sample1 = (int)floorf(dpr);
andrewm@0 354 int sample2 = (sample1 + 1) % delayBufferLength_;
andrewm@0 355 int sample3 = (sample2 + 1) % delayBufferLength_;
andrewm@0 356 int sample0 = (sample1 - 1 + delayBufferLength_) % delayBufferLength_;
andrewm@0 357
andrewm@0 358 float fraction = dpr - floorf(dpr);
andrewm@0 359 float frsq = fraction*fraction;
andrewm@0 360
andrewm@0 361 float a0 = -0.5f*delayData[sample0] + 1.5f*delayData[sample1]
andrewm@0 362 - 1.5f*delayData[sample2] + 0.5f*delayData[sample3];
andrewm@0 363 float a1 = delayData[sample0] - 2.5f*delayData[sample1]
andrewm@0 364 + 2.0f*delayData[sample2] - 0.5f*delayData[sample3];
andrewm@0 365 float a2 = -0.5f*delayData[sample0] + 0.5f*delayData[sample2];
andrewm@0 366 float a3 = delayData[sample1];
andrewm@0 367
andrewm@0 368 interpolatedSample = a0*fraction*frsq + a1*frsq + a2*fraction + a3;
andrewm@0 369 }
andrewm@0 370 else // Nearest neighbour interpolation
andrewm@0 371 {
andrewm@0 372 // Find the nearest input sample by rounding the fractional index to the
andrewm@0 373 // nearest integer. It's possible this will round it to the end of the buffer,
andrewm@0 374 // in which case we need to roll it back to the beginning.
andrewm@0 375 int closestSample = (int)floorf(dpr + 0.5f);
andrewm@0 376 if(closestSample == delayBufferLength_)
andrewm@0 377 closestSample = 0;
andrewm@0 378 interpolatedSample = delayData[closestSample];
andrewm@0 379 }
andrewm@0 380
andrewm@0 381 // Store the output sample in the buffer, which starts by containing the input sample
andrewm@0 382 channelData[i] += depth_ * weight * interpolatedSample;
andrewm@0 383 }
andrewm@0 384
andrewm@0 385 // 3-voice chorus uses two voices in quadrature phase (90 degrees apart). Otherwise,
andrewm@0 386 // spread the voice phases evenly around the unit circle. (For 2-voice chorus, this
andrewm@0 387 // code doesn't matter since the loop only runs once.)
andrewm@0 388 if(numVoices_ < 3)
andrewm@0 389 phaseOffset += 0.25f;
andrewm@0 390 else
andrewm@0 391 phaseOffset += 1.0f / (float)(numVoices_ - 1);
andrewm@0 392 }
andrewm@0 393
andrewm@0 394 // Store the current input in the delay buffer (no feedback in a chorus, unlike a flanger).
andrewm@0 395 delayData[dpw] = in;
andrewm@0 396
andrewm@0 397 // Increment the write pointer at a constant rate. The read pointer will move at different
andrewm@0 398 // rates depending on the settings of the LFO, the delay and the sweep width.
andrewm@0 399
andrewm@0 400 if (++dpw >= delayBufferLength_)
andrewm@0 401 dpw = 0;
andrewm@0 402
andrewm@0 403 // Update the LFO phase, keeping it in the range 0-1
andrewm@0 404 ph += frequency_*inverseSampleRate_;
andrewm@0 405 if(ph >= 1.0)
andrewm@0 406 ph -= 1.0;
andrewm@0 407 }
andrewm@0 408 }
andrewm@0 409
andrewm@0 410 // Having made a local copy of the state variables for each channel, now transfer the result
andrewm@0 411 // back to the main state variable so they will be preserved for the next call of processBlock()
andrewm@0 412
andrewm@0 413 delayWritePosition_ = dpw;
andrewm@0 414 lfoPhase_ = ph;
andrewm@0 415
andrewm@0 416 // In case we have more outputs than inputs, we'll clear any output
andrewm@0 417 // channels that didn't contain input data, (because these aren't
andrewm@0 418 // guaranteed to be empty - they may contain garbage).
andrewm@0 419 for (int i = numInputChannels; i < numOutputChannels; ++i)
andrewm@0 420 {
andrewm@0 421 buffer.clear (i, 0, buffer.getNumSamples());
andrewm@0 422 }
andrewm@0 423 }
andrewm@0 424
andrewm@0 425 //==============================================================================
andrewm@0 426 bool ChorusAudioProcessor::hasEditor() const
andrewm@0 427 {
andrewm@0 428 return true; // (change this to false if you choose to not supply an editor)
andrewm@0 429 }
andrewm@0 430
andrewm@0 431 AudioProcessorEditor* ChorusAudioProcessor::createEditor()
andrewm@0 432 {
andrewm@0 433 return new ChorusAudioProcessorEditor (this);
andrewm@0 434 }
andrewm@0 435
andrewm@0 436 //==============================================================================
andrewm@0 437 void ChorusAudioProcessor::getStateInformation (MemoryBlock& destData)
andrewm@0 438 {
andrewm@0 439 // You should use this method to store your parameters in the memory block.
andrewm@0 440 // You could do that either as raw data, or use the XML or ValueTree classes
andrewm@0 441 // as intermediaries to make it easy to save and load complex data.
andrewm@0 442
andrewm@0 443 // Create an outer XML element..
andrewm@0 444 XmlElement xml("C4DMPLUGINSETTINGS");
andrewm@0 445
andrewm@0 446 // add some attributes to it..
andrewm@0 447 xml.setAttribute("uiWidth", lastUIWidth_);
andrewm@0 448 xml.setAttribute("uiHeight", lastUIHeight_);
andrewm@0 449 xml.setAttribute("delay", delay_);
andrewm@0 450 xml.setAttribute("sweepWidth", sweepWidth_);
andrewm@0 451 xml.setAttribute("depth", depth_);
andrewm@0 452 xml.setAttribute("frequency", frequency_);
andrewm@0 453 xml.setAttribute("waveform", waveform_);
andrewm@0 454 xml.setAttribute("interpolation", interpolation_);
andrewm@0 455 xml.setAttribute("numVoices", numVoices_);
andrewm@0 456 xml.setAttribute("stereo", stereo_);
andrewm@0 457
andrewm@0 458 // then use this helper function to stuff it into the binary blob and return it..
andrewm@0 459 copyXmlToBinary(xml, destData);
andrewm@0 460 }
andrewm@0 461
andrewm@0 462 void ChorusAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
andrewm@0 463 {
andrewm@0 464 // You should use this method to restore your parameters from this memory block,
andrewm@0 465 // whose contents will have been created by the getStateInformation() call.
andrewm@0 466
andrewm@0 467 // This getXmlFromBinary() helper function retrieves our XML from the binary blob..
andrewm@0 468 ScopedPointer<XmlElement> xmlState (getXmlFromBinary (data, sizeInBytes));
andrewm@0 469
andrewm@0 470 if(xmlState != 0)
andrewm@0 471 {
andrewm@0 472 // make sure that it's actually our type of XML object..
andrewm@0 473 if(xmlState->hasTagName("C4DMPLUGINSETTINGS"))
andrewm@0 474 {
andrewm@0 475 // ok, now pull out our parameters..
andrewm@0 476 lastUIWidth_ = xmlState->getIntAttribute("uiWidth", lastUIWidth_);
andrewm@0 477 lastUIHeight_ = xmlState->getIntAttribute("uiHeight", lastUIHeight_);
andrewm@0 478
andrewm@0 479 delay_ = (float)xmlState->getDoubleAttribute("delay", delay_);
andrewm@0 480 sweepWidth_ = (float)xmlState->getDoubleAttribute("sweepWidth", sweepWidth_);
andrewm@0 481 depth_ = (float)xmlState->getDoubleAttribute("depth", depth_);
andrewm@0 482 frequency_ = (float)xmlState->getDoubleAttribute("frequency", frequency_);
andrewm@0 483 waveform_ = xmlState->getIntAttribute("waveform", waveform_);
andrewm@0 484 interpolation_ = xmlState->getIntAttribute("interpolation", interpolation_);
andrewm@0 485 numVoices_ = xmlState->getIntAttribute("numVoices", numVoices_);
andrewm@0 486 stereo_ = xmlState->getIntAttribute("stereo", stereo_);
andrewm@0 487 }
andrewm@0 488 }
andrewm@0 489 }
andrewm@0 490
andrewm@0 491 //==============================================================================
andrewm@0 492 // Function for calculating LFO waveforms. Phase runs from 0-1, output is scaled
andrewm@0 493 // from 0 to 1 (note: not -1 to 1 as would be typical of sine).
andrewm@0 494 float ChorusAudioProcessor::lfo(float phase, int waveform)
andrewm@0 495 {
andrewm@0 496 switch(waveform)
andrewm@0 497 {
andrewm@0 498 case kWaveformTriangle:
andrewm@0 499 if(phase < 0.25f)
andrewm@0 500 return 0.5f + 2.0f*phase;
andrewm@0 501 else if(phase < 0.75f)
andrewm@0 502 return 1.0f - 2.0f*(phase - 0.25f);
andrewm@0 503 else
andrewm@0 504 return 2.0f*(phase-0.75f);
andrewm@0 505 case kWaveformSquare:
andrewm@0 506 if(phase < 0.5f)
andrewm@0 507 return 1.0f;
andrewm@0 508 else
andrewm@0 509 return 0.0f;
andrewm@0 510 case kWaveformSawtooth:
andrewm@0 511 if(phase < 0.5f)
andrewm@0 512 return 0.5f + phase;
andrewm@0 513 else
andrewm@0 514 return phase - 0.5f;
andrewm@0 515 case kWaveformSine:
andrewm@0 516 default:
andrewm@0 517 return 0.5f + 0.5f*sinf(2.0 * M_PI * phase);
andrewm@0 518 }
andrewm@0 519 }
andrewm@0 520
andrewm@0 521 //==============================================================================
andrewm@0 522 // This creates new instances of the plugin..
andrewm@0 523 AudioProcessor* JUCE_CALLTYPE createPluginFilter()
andrewm@0 524 {
andrewm@0 525 return new ChorusAudioProcessor();
andrewm@0 526 }