annotate effects/pingpongdelay/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 Ping-Pong Delay: stereo delay alternating between channels
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
andrewm@0 33 //==============================================================================
andrewm@0 34 PingPongDelayAudioProcessor::PingPongDelayAudioProcessor() : delayBuffer_ (2, 1)
andrewm@0 35 {
andrewm@0 36 // Set default values:
andrewm@0 37 delayLengthLeft_ = delayLengthRight_ = 0.5;
andrewm@0 38 wetMix_ = 0.5;
andrewm@0 39 feedback_ = 0.75;
andrewm@0 40 delayBufferLength_ = 1;
andrewm@0 41 reverseChannels_ = false;
andrewm@0 42
andrewm@0 43 // Start the circular buffer pointers at the beginning
andrewm@0 44 delayReadPositionLeft_ = delayReadPositionRight_ = 0;
andrewm@0 45 delayWritePosition_ = 0;
andrewm@0 46
andrewm@0 47 lastUIWidth_ = 500;
andrewm@0 48 lastUIHeight_ = 140;
andrewm@0 49 }
andrewm@0 50
andrewm@0 51 PingPongDelayAudioProcessor::~PingPongDelayAudioProcessor()
andrewm@0 52 {
andrewm@0 53 }
andrewm@0 54
andrewm@0 55 //==============================================================================
andrewm@0 56 const String PingPongDelayAudioProcessor::getName() const
andrewm@0 57 {
andrewm@0 58 return JucePlugin_Name;
andrewm@0 59 }
andrewm@0 60
andrewm@0 61 int PingPongDelayAudioProcessor::getNumParameters()
andrewm@0 62 {
andrewm@0 63 return kNumParameters;
andrewm@0 64 }
andrewm@0 65
andrewm@0 66 float PingPongDelayAudioProcessor::getParameter (int index)
andrewm@0 67 {
andrewm@0 68 // This method will be called by the host, probably on the audio thread, so
andrewm@0 69 // it's absolutely time-critical. Don't use critical sections or anything
andrewm@0 70 // UI-related, or anything at all that may block in any way!
andrewm@0 71 switch (index)
andrewm@0 72 {
andrewm@0 73 case kWetMixParam: return wetMix_;
andrewm@0 74 case kFeedbackParam: return feedback_;
andrewm@0 75 case kDelayLengthLeftParam: return delayLengthLeft_;
andrewm@0 76 case kDelayLengthRightParam: return delayLengthRight_;
andrewm@0 77 case kReverseChannelsParam: return (reverseChannels_ ? 1.0f : 0.0f);
andrewm@0 78 default: return 0.0f;
andrewm@0 79 }
andrewm@0 80 }
andrewm@0 81
andrewm@0 82 void PingPongDelayAudioProcessor::setParameter (int index, float newValue)
andrewm@0 83 {
andrewm@0 84 // This method will be called by the host, probably on the audio thread, so
andrewm@0 85 // it's absolutely time-critical. Don't use critical sections or anything
andrewm@0 86 // UI-related, or anything at all that may block in any way!
andrewm@0 87 switch (index)
andrewm@0 88 {
andrewm@0 89 case kWetMixParam:
andrewm@0 90 wetMix_ = newValue;
andrewm@0 91 break;
andrewm@0 92 case kFeedbackParam:
andrewm@0 93 feedback_ = newValue;
andrewm@0 94 break;
andrewm@0 95 case kDelayLengthLeftParam:
andrewm@0 96 delayLengthLeft_ = newValue;
andrewm@0 97 delayReadPositionLeft_ = (int)(delayWritePosition_ - (delayLengthLeft_ * getSampleRate())
andrewm@0 98 + delayBufferLength_) % delayBufferLength_;
andrewm@0 99 break;
andrewm@0 100 case kDelayLengthRightParam:
andrewm@0 101 delayLengthRight_ = newValue;
andrewm@0 102 delayReadPositionRight_ = (int)(delayWritePosition_ - (delayLengthRight_ * getSampleRate())
andrewm@0 103 + delayBufferLength_) % delayBufferLength_;
andrewm@0 104 break;
andrewm@0 105 case kReverseChannelsParam:
andrewm@0 106 reverseChannels_ = (newValue != 0.0f);
andrewm@0 107 break;
andrewm@0 108 default:
andrewm@0 109 break;
andrewm@0 110 }
andrewm@0 111 }
andrewm@0 112
andrewm@0 113 const String PingPongDelayAudioProcessor::getParameterName (int index)
andrewm@0 114 {
andrewm@0 115 switch (index)
andrewm@0 116 {
andrewm@0 117 case kWetMixParam: return "wet mix";
andrewm@0 118 case kFeedbackParam: return "feedback";
andrewm@0 119 case kDelayLengthLeftParam: return "delay left";
andrewm@0 120 case kDelayLengthRightParam: return "delay right";
andrewm@0 121 case kReverseChannelsParam: return "reverse channels";
andrewm@0 122 default: break;
andrewm@0 123 }
andrewm@0 124
andrewm@0 125 return String::empty;
andrewm@0 126 }
andrewm@0 127
andrewm@0 128 const String PingPongDelayAudioProcessor::getParameterText (int index)
andrewm@0 129 {
andrewm@0 130 return String (getParameter (index), 2);
andrewm@0 131 }
andrewm@0 132
andrewm@0 133 const String PingPongDelayAudioProcessor::getInputChannelName (int channelIndex) const
andrewm@0 134 {
andrewm@0 135 return String (channelIndex + 1);
andrewm@0 136 }
andrewm@0 137
andrewm@0 138 const String PingPongDelayAudioProcessor::getOutputChannelName (int channelIndex) const
andrewm@0 139 {
andrewm@0 140 return String (channelIndex + 1);
andrewm@0 141 }
andrewm@0 142
andrewm@0 143 bool PingPongDelayAudioProcessor::isInputChannelStereoPair (int index) const
andrewm@0 144 {
andrewm@0 145 return true;
andrewm@0 146 }
andrewm@0 147
andrewm@0 148 bool PingPongDelayAudioProcessor::isOutputChannelStereoPair (int index) const
andrewm@0 149 {
andrewm@0 150 return true;
andrewm@0 151 }
andrewm@0 152
andrewm@0 153 bool PingPongDelayAudioProcessor::silenceInProducesSilenceOut() const
andrewm@0 154 {
andrewm@0 155 #if JucePlugin_SilenceInProducesSilenceOut
andrewm@0 156 return true;
andrewm@0 157 #else
andrewm@0 158 return false;
andrewm@0 159 #endif
andrewm@0 160 }
andrewm@0 161
andrewm@0 162 double PingPongDelayAudioProcessor::getTailLengthSeconds() const
andrewm@0 163 {
andrewm@0 164 return 0.0;
andrewm@0 165 }
andrewm@0 166
andrewm@0 167 bool PingPongDelayAudioProcessor::acceptsMidi() const
andrewm@0 168 {
andrewm@0 169 #if JucePlugin_WantsMidiInput
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 bool PingPongDelayAudioProcessor::producesMidi() const
andrewm@0 177 {
andrewm@0 178 #if JucePlugin_ProducesMidiOutput
andrewm@0 179 return true;
andrewm@0 180 #else
andrewm@0 181 return false;
andrewm@0 182 #endif
andrewm@0 183 }
andrewm@0 184
andrewm@0 185 int PingPongDelayAudioProcessor::getNumPrograms()
andrewm@0 186 {
andrewm@0 187 return 0;
andrewm@0 188 }
andrewm@0 189
andrewm@0 190 int PingPongDelayAudioProcessor::getCurrentProgram()
andrewm@0 191 {
andrewm@0 192 return 0;
andrewm@0 193 }
andrewm@0 194
andrewm@0 195 void PingPongDelayAudioProcessor::setCurrentProgram (int index)
andrewm@0 196 {
andrewm@0 197 }
andrewm@0 198
andrewm@0 199 const String PingPongDelayAudioProcessor::getProgramName (int index)
andrewm@0 200 {
andrewm@0 201 return String::empty;
andrewm@0 202 }
andrewm@0 203
andrewm@0 204 void PingPongDelayAudioProcessor::changeProgramName (int index, const String& newName)
andrewm@0 205 {
andrewm@0 206 }
andrewm@0 207
andrewm@0 208 //==============================================================================
andrewm@0 209 void PingPongDelayAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
andrewm@0 210 {
andrewm@0 211 // Allocate and zero the delay buffer (size will depend on current sample rate)
andrewm@0 212 // Sanity check the result so we don't end up with any zero-length calculations
andrewm@0 213 delayBufferLength_ = (int)(2.0*sampleRate);
andrewm@0 214 if(delayBufferLength_ < 1)
andrewm@0 215 delayBufferLength_ = 1;
andrewm@0 216 delayBuffer_.setSize(2, delayBufferLength_);
andrewm@0 217 delayBuffer_.clear();
andrewm@0 218
andrewm@0 219 // This method gives us the sample rate. Use this to figure out what the delay position
andrewm@0 220 // offset should be (since it is specified in seconds, and we need to convert it to a number
andrewm@0 221 // of samples)
andrewm@0 222 delayReadPositionLeft_ = (int)(delayWritePosition_ - (delayLengthLeft_ * getSampleRate())
andrewm@0 223 + delayBufferLength_) % delayBufferLength_;
andrewm@0 224 delayReadPositionRight_ = (int)(delayWritePosition_ - (delayLengthRight_ * getSampleRate())
andrewm@0 225 + delayBufferLength_) % delayBufferLength_;
andrewm@0 226 }
andrewm@0 227
andrewm@0 228 void PingPongDelayAudioProcessor::releaseResources()
andrewm@0 229 {
andrewm@0 230 // When playback stops, you can use this as an opportunity to free up any
andrewm@0 231 // spare memory, etc.
andrewm@0 232
andrewm@0 233 // The delay buffer will stay in memory until the effect is unloaded.
andrewm@0 234 }
andrewm@0 235
andrewm@0 236 void PingPongDelayAudioProcessor::reset()
andrewm@0 237 {
andrewm@0 238 // Use this method as the place to clear any delay lines, buffers, etc, as it
andrewm@0 239 // means there's been a break in the audio's continuity.
andrewm@0 240
andrewm@0 241 delayBuffer_.clear();
andrewm@0 242 }
andrewm@0 243
andrewm@0 244
andrewm@0 245 void PingPongDelayAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
andrewm@0 246 {
andrewm@0 247 // Helpful information about this block of samples:
andrewm@0 248 const int numInputChannels = getNumInputChannels(); // How many input channels for our effect?
andrewm@0 249 const int numOutputChannels = getNumOutputChannels(); // How many output channels for our effect?
andrewm@0 250 const int numSamples = buffer.getNumSamples(); // How many samples in the buffer for this block?
andrewm@0 251
andrewm@0 252 // This shouldn't happen, but we need a sanity check: this effect only makes sense
andrewm@0 253 // if there are at least 2 channels to work with (and in this case only 2...)
andrewm@0 254 if(buffer.getNumChannels() < 2)
andrewm@0 255 return;
andrewm@0 256
andrewm@0 257 // If there is one input only, the second channel may not contain anything useful.
andrewm@0 258 // start with a blank buffer in this case
andrewm@0 259 if(numInputChannels < 2)
andrewm@0 260 buffer.clear(1, 0, numSamples);
andrewm@0 261
andrewm@0 262 // channelDataL and channelDataR are arrays of length numSamples which contain
andrewm@0 263 // the audio for one channel
b@1 264 float *channelDataL = buffer.getWritePointer(0);
b@1 265 float *channelDataR = buffer.getWritePointer(1);
andrewm@0 266
andrewm@0 267 // delayDataL and delayDataR are the circular buffers for implementing delay
b@1 268 float* delayDataL = delayBuffer_.getWritePointer(0);
b@1 269 float* delayDataR = delayBuffer_.getWritePointer(1);
andrewm@0 270
andrewm@0 271 for (int i = 0; i < numSamples; ++i)
andrewm@0 272 {
andrewm@0 273 const float inL = channelDataL[i];
andrewm@0 274 const float inR = channelDataR[i];
andrewm@0 275 float outL, outR;
andrewm@0 276
andrewm@0 277 if(reverseChannels_)
andrewm@0 278 {
andrewm@0 279 outL = (inL + wetMix_ * delayDataR[delayReadPositionLeft_]);
andrewm@0 280 outR = (inR + wetMix_ * delayDataL[delayReadPositionRight_]);
andrewm@0 281 }
andrewm@0 282 else
andrewm@0 283 {
andrewm@0 284 outL = (inL + wetMix_ * delayDataL[delayReadPositionLeft_]);
andrewm@0 285 outR = (inR + wetMix_ * delayDataR[delayReadPositionRight_]);
andrewm@0 286 }
andrewm@0 287
andrewm@0 288 // Store the output of one delay buffer into the other, producing
andrewm@0 289 // the ping-pong effect
andrewm@0 290 delayDataR[delayWritePosition_] = inR + (delayDataL[delayReadPositionLeft_] * feedback_);
andrewm@0 291 delayDataL[delayWritePosition_] = inL + (delayDataR[delayReadPositionRight_] * feedback_);
andrewm@0 292
andrewm@0 293 if (++delayReadPositionLeft_ >= delayBufferLength_)
andrewm@0 294 delayReadPositionLeft_ = 0;
andrewm@0 295 if (++delayReadPositionRight_ >= delayBufferLength_)
andrewm@0 296 delayReadPositionRight_ = 0;
andrewm@0 297 if (++delayWritePosition_ >= delayBufferLength_)
andrewm@0 298 delayWritePosition_ = 0;
andrewm@0 299
andrewm@0 300 // Store the output samples in the buffer, replacing the input
andrewm@0 301 channelDataL[i] = outL;
andrewm@0 302 channelDataR[i] = outR;
andrewm@0 303 }
andrewm@0 304
andrewm@0 305 // Clear any channels above 2 (stereo)
andrewm@0 306 for (int i = 2; i < numOutputChannels; ++i)
andrewm@0 307 {
andrewm@0 308 buffer.clear (i, 0, buffer.getNumSamples());
andrewm@0 309 }
andrewm@0 310 }
andrewm@0 311
andrewm@0 312 //==============================================================================
andrewm@0 313 bool PingPongDelayAudioProcessor::hasEditor() const
andrewm@0 314 {
andrewm@0 315 return true; // (change this to false if you choose to not supply an editor)
andrewm@0 316 }
andrewm@0 317
andrewm@0 318 AudioProcessorEditor* PingPongDelayAudioProcessor::createEditor()
andrewm@0 319 {
andrewm@0 320 return new PingPongDelayAudioProcessorEditor (this);
andrewm@0 321 }
andrewm@0 322
andrewm@0 323 //==============================================================================
andrewm@0 324 void PingPongDelayAudioProcessor::getStateInformation (MemoryBlock& destData)
andrewm@0 325 {
andrewm@0 326 // You should use this method to store your parameters in the memory block.
andrewm@0 327 // You could do that either as raw data, or use the XML or ValueTree classes
andrewm@0 328 // as intermediaries to make it easy to save and load complex data.
andrewm@0 329
andrewm@0 330 // Create an outer XML element..
andrewm@0 331 XmlElement xml("C4DMPLUGINSETTINGS");
andrewm@0 332
andrewm@0 333 // add some attributes to it..
andrewm@0 334 xml.setAttribute("uiWidth", lastUIWidth_);
andrewm@0 335 xml.setAttribute("uiHeight", lastUIHeight_);
andrewm@0 336 xml.setAttribute("delayLengthLeft", delayLengthLeft_);
andrewm@0 337 xml.setAttribute("delayLengthRight", delayLengthRight_);
andrewm@0 338 xml.setAttribute("feedback", feedback_);
andrewm@0 339 xml.setAttribute("wetMix", wetMix_);
andrewm@0 340
andrewm@0 341 // then use this helper function to stuff it into the binary blob and return it..
andrewm@0 342 copyXmlToBinary(xml, destData);
andrewm@0 343 }
andrewm@0 344
andrewm@0 345 void PingPongDelayAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
andrewm@0 346 {
andrewm@0 347 // You should use this method to restore your parameters from this memory block,
andrewm@0 348 // whose contents will have been created by the getStateInformation() call.
andrewm@0 349
andrewm@0 350 // This getXmlFromBinary() helper function retrieves our XML from the binary blob..
andrewm@0 351 ScopedPointer<XmlElement> xmlState (getXmlFromBinary (data, sizeInBytes));
andrewm@0 352
andrewm@0 353 if(xmlState != 0)
andrewm@0 354 {
andrewm@0 355 // make sure that it's actually our type of XML object..
andrewm@0 356 if(xmlState->hasTagName("C4DMPLUGINSETTINGS"))
andrewm@0 357 {
andrewm@0 358 // ok, now pull out our parameters..
andrewm@0 359 lastUIWidth_ = xmlState->getIntAttribute("uiWidth", lastUIWidth_);
andrewm@0 360 lastUIHeight_ = xmlState->getIntAttribute("uiHeight", lastUIHeight_);
andrewm@0 361
andrewm@0 362 delayLengthLeft_ = (float)xmlState->getDoubleAttribute("delayLengthLeft", delayLengthLeft_);
andrewm@0 363 delayLengthRight_ = (float)xmlState->getDoubleAttribute("delayLengthRight", delayLengthRight_);
andrewm@0 364 feedback_ = (float)xmlState->getDoubleAttribute("feedback", feedback_);
andrewm@0 365 wetMix_ = (float)xmlState->getDoubleAttribute("wetMix", wetMix_);
andrewm@0 366 }
andrewm@0 367 }
andrewm@0 368 }
andrewm@0 369
andrewm@0 370 //==============================================================================
andrewm@0 371 // This creates new instances of the plugin..
andrewm@0 372 AudioProcessor* JUCE_CALLTYPE createPluginFilter()
andrewm@0 373 {
andrewm@0 374 return new PingPongDelayAudioProcessor();
andrewm@0 375 }