annotate effects/delay/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 Delay: basic delay effect with feedback
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 DelayAudioProcessor::DelayAudioProcessor() : delayBuffer_ (2, 1)
andrewm@0 35 {
andrewm@0 36 // Set default values:
andrewm@0 37 delayLength_ = 0.5;
andrewm@0 38 dryMix_ = 1.0;
andrewm@0 39 wetMix_ = 0.5;
andrewm@0 40 feedback_ = 0.75;
andrewm@0 41 delayBufferLength_ = 1;
andrewm@0 42
andrewm@0 43 // Start the circular buffer pointers at the beginning
andrewm@0 44 delayReadPosition_ = 0;
andrewm@0 45 delayWritePosition_ = 0;
andrewm@0 46
andrewm@0 47 lastUIWidth_ = 370;
andrewm@0 48 lastUIHeight_ = 140;
andrewm@0 49 }
andrewm@0 50
andrewm@0 51 DelayAudioProcessor::~DelayAudioProcessor()
andrewm@0 52 {
andrewm@0 53 }
andrewm@0 54
andrewm@0 55 //==============================================================================
andrewm@0 56 const String DelayAudioProcessor::getName() const
andrewm@0 57 {
andrewm@0 58 return JucePlugin_Name;
andrewm@0 59 }
andrewm@0 60
andrewm@0 61 int DelayAudioProcessor::getNumParameters()
andrewm@0 62 {
andrewm@0 63 return kNumParameters;
andrewm@0 64 }
andrewm@0 65
andrewm@0 66 float DelayAudioProcessor::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 kDryMixParam: return dryMix_;
andrewm@0 74 case kWetMixParam: return wetMix_;
andrewm@0 75 case kFeedbackParam: return feedback_;
andrewm@0 76 case kDelayLengthParam:return delayLength_;
andrewm@0 77 default: return 0.0f;
andrewm@0 78 }
andrewm@0 79 }
andrewm@0 80
andrewm@0 81 void DelayAudioProcessor::setParameter (int index, float newValue)
andrewm@0 82 {
andrewm@0 83 // This method will be called by the host, probably on the audio thread, so
andrewm@0 84 // it's absolutely time-critical. Don't use critical sections or anything
andrewm@0 85 // UI-related, or anything at all that may block in any way!
andrewm@0 86 switch (index)
andrewm@0 87 {
andrewm@0 88 case kDryMixParam:
andrewm@0 89 dryMix_ = newValue;
andrewm@0 90 break;
andrewm@0 91 case kWetMixParam:
andrewm@0 92 wetMix_ = newValue;
andrewm@0 93 break;
andrewm@0 94 case kFeedbackParam:
andrewm@0 95 feedback_ = newValue;
andrewm@0 96 break;
andrewm@0 97 case kDelayLengthParam:
andrewm@0 98 delayLength_ = newValue;
andrewm@0 99 delayReadPosition_ = (int)(delayWritePosition_ - (delayLength_ * getSampleRate())
andrewm@0 100 + delayBufferLength_) % delayBufferLength_;
andrewm@0 101 break;
andrewm@0 102 default:
andrewm@0 103 break;
andrewm@0 104 }
andrewm@0 105 }
andrewm@0 106
andrewm@0 107 const String DelayAudioProcessor::getParameterName (int index)
andrewm@0 108 {
andrewm@0 109 switch (index)
andrewm@0 110 {
andrewm@0 111 case kDryMixParam: return "dry mix";
andrewm@0 112 case kWetMixParam: return "wet mix";
andrewm@0 113 case kFeedbackParam: return "feedback";
andrewm@0 114 case kDelayLengthParam:return "delay";
andrewm@0 115 default: break;
andrewm@0 116 }
andrewm@0 117
andrewm@0 118 return String::empty;
andrewm@0 119 }
andrewm@0 120
andrewm@0 121 const String DelayAudioProcessor::getParameterText (int index)
andrewm@0 122 {
andrewm@0 123 return String (getParameter (index), 2);
andrewm@0 124 }
andrewm@0 125
andrewm@0 126 const String DelayAudioProcessor::getInputChannelName (int channelIndex) const
andrewm@0 127 {
andrewm@0 128 return String (channelIndex + 1);
andrewm@0 129 }
andrewm@0 130
andrewm@0 131 const String DelayAudioProcessor::getOutputChannelName (int channelIndex) const
andrewm@0 132 {
andrewm@0 133 return String (channelIndex + 1);
andrewm@0 134 }
andrewm@0 135
andrewm@0 136 bool DelayAudioProcessor::isInputChannelStereoPair (int index) const
andrewm@0 137 {
andrewm@0 138 return true;
andrewm@0 139 }
andrewm@0 140
andrewm@0 141 bool DelayAudioProcessor::isOutputChannelStereoPair (int index) const
andrewm@0 142 {
andrewm@0 143 return true;
andrewm@0 144 }
andrewm@0 145
andrewm@0 146 bool DelayAudioProcessor::silenceInProducesSilenceOut() const
andrewm@0 147 {
andrewm@0 148 #if JucePlugin_SilenceInProducesSilenceOut
andrewm@0 149 return true;
andrewm@0 150 #else
andrewm@0 151 return false;
andrewm@0 152 #endif
andrewm@0 153 }
andrewm@0 154 double DelayAudioProcessor::getTailLengthSeconds() const
andrewm@0 155 {
andrewm@0 156 return 0.0;
andrewm@0 157 }
andrewm@0 158 bool DelayAudioProcessor::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 DelayAudioProcessor::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 DelayAudioProcessor::getNumPrograms()
andrewm@0 177 {
andrewm@0 178 return 0;
andrewm@0 179 }
andrewm@0 180
andrewm@0 181 int DelayAudioProcessor::getCurrentProgram()
andrewm@0 182 {
andrewm@0 183 return 0;
andrewm@0 184 }
andrewm@0 185
andrewm@0 186 void DelayAudioProcessor::setCurrentProgram (int index)
andrewm@0 187 {
andrewm@0 188 }
andrewm@0 189
andrewm@0 190 const String DelayAudioProcessor::getProgramName (int index)
andrewm@0 191 {
andrewm@0 192 return String::empty;
andrewm@0 193 }
andrewm@0 194
andrewm@0 195 void DelayAudioProcessor::changeProgramName (int index, const String& newName)
andrewm@0 196 {
andrewm@0 197 }
andrewm@0 198
andrewm@0 199 //==============================================================================
andrewm@0 200 void DelayAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
andrewm@0 201 {
andrewm@0 202 // Allocate and zero the delay buffer (size will depend on current sample rate)
andrewm@0 203 // Sanity check the result so we don't end up with any zero-length calculations
andrewm@0 204 delayBufferLength_ = (int)(2.0*sampleRate);
andrewm@0 205 if(delayBufferLength_ < 1)
andrewm@0 206 delayBufferLength_ = 1;
andrewm@0 207 delayBuffer_.setSize(2, delayBufferLength_);
andrewm@0 208 delayBuffer_.clear();
andrewm@0 209
andrewm@0 210 // This method gives us the sample rate. Use this to figure out what the delay position
andrewm@0 211 // offset should be (since it is specified in seconds, and we need to convert it to a number
andrewm@0 212 // of samples)
andrewm@0 213 delayReadPosition_ = (int)(delayWritePosition_ - (delayLength_ * getSampleRate())
andrewm@0 214 + delayBufferLength_) % delayBufferLength_;
andrewm@0 215 }
andrewm@0 216
andrewm@0 217 void DelayAudioProcessor::releaseResources()
andrewm@0 218 {
andrewm@0 219 // When playback stops, you can use this as an opportunity to free up any
andrewm@0 220 // spare memory, etc.
andrewm@0 221
andrewm@0 222 // The delay buffer will stay in memory until the effect is unloaded.
andrewm@0 223 }
andrewm@0 224
andrewm@0 225 void DelayAudioProcessor::reset()
andrewm@0 226 {
andrewm@0 227 // Use this method as the place to clear any delay lines, buffers, etc, as it
andrewm@0 228 // means there's been a break in the audio's continuity.
andrewm@0 229
andrewm@0 230 delayBuffer_.clear();
andrewm@0 231 }
andrewm@0 232
andrewm@0 233
andrewm@0 234 void DelayAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
andrewm@0 235 {
andrewm@0 236 // Helpful information about this block of samples:
andrewm@0 237 const int numInputChannels = getNumInputChannels(); // How many input channels for our effect?
andrewm@0 238 const int numOutputChannels = getNumOutputChannels(); // How many output channels for our effect?
andrewm@0 239 const int numSamples = buffer.getNumSamples(); // How many samples in the buffer for this block?
andrewm@0 240
andrewm@0 241 int channel, dpr, dpw; // dpr = delay read pointer; dpw = delay write pointer
andrewm@0 242
andrewm@0 243 // Go through each channel of audio that's passed in. In this example we apply identical
andrewm@0 244 // effects to each channel, regardless of how many input channels there are. For some effects, like
andrewm@0 245 // a stereo chorus or panner, you might do something different for each channel.
andrewm@0 246
andrewm@0 247 for (channel = 0; channel < numInputChannels; ++channel)
andrewm@0 248 {
andrewm@0 249 // channelData is an array of length numSamples which contains the audio for one channel
b@1 250 float* channelData = buffer.getWritePointer(channel);
andrewm@0 251
andrewm@0 252 // delayData is the circular buffer for implementing delay on this channel
b@1 253 float* delayData = delayBuffer_.getWritePointer (jmin (channel, delayBuffer_.getNumChannels() - 1));
andrewm@0 254
andrewm@0 255 // Make a temporary copy of any state variables declared in PluginProcessor.h which need to be
andrewm@0 256 // maintained between calls to processBlock(). Each channel needs to be processed identically
andrewm@0 257 // which means that the activity of processing one channel can't affect the state variable for
andrewm@0 258 // the next channel.
andrewm@0 259
andrewm@0 260 dpr = delayReadPosition_;
andrewm@0 261 dpw = delayWritePosition_;
andrewm@0 262
andrewm@0 263 for (int i = 0; i < numSamples; ++i)
andrewm@0 264 {
andrewm@0 265 const float in = channelData[i];
andrewm@0 266 float out = 0.0;
andrewm@0 267
andrewm@0 268 // In this example, the output is the input plus the contents of the delay buffer (weighted by delayMix)
andrewm@0 269 // The last term implements a tremolo (variable amplitude) on the whole thing.
andrewm@0 270
andrewm@0 271 out = (dryMix_ * in + wetMix_ * delayData[dpr]);
andrewm@0 272
andrewm@0 273 // Store the current information in the delay buffer. delayData[dpr] is the delay sample we just read,
andrewm@0 274 // i.e. what came out of the buffer. delayData[dpw] is what we write to the buffer, i.e. what goes in
andrewm@0 275
andrewm@0 276 delayData[dpw] = in + (delayData[dpr] * feedback_);
andrewm@0 277
andrewm@0 278 if (++dpr >= delayBufferLength_)
andrewm@0 279 dpr = 0;
andrewm@0 280 if (++dpw >= delayBufferLength_)
andrewm@0 281 dpw = 0;
andrewm@0 282
andrewm@0 283 // Store the output sample in the buffer, replacing the input
andrewm@0 284 channelData[i] = out;
andrewm@0 285 }
andrewm@0 286 }
andrewm@0 287
andrewm@0 288 // Having made a local copy of the state variables for each channel, now transfer the result
andrewm@0 289 // back to the main state variable so they will be preserved for the next call of processBlock()
andrewm@0 290
andrewm@0 291 delayReadPosition_ = dpr;
andrewm@0 292 delayWritePosition_ = dpw;
andrewm@0 293
andrewm@0 294 // In case we have more outputs than inputs, we'll clear any output
andrewm@0 295 // channels that didn't contain input data, (because these aren't
andrewm@0 296 // guaranteed to be empty - they may contain garbage).
andrewm@0 297 for (int i = numInputChannels; i < numOutputChannels; ++i)
andrewm@0 298 {
andrewm@0 299 buffer.clear (i, 0, buffer.getNumSamples());
andrewm@0 300 }
andrewm@0 301 }
andrewm@0 302
andrewm@0 303 //==============================================================================
andrewm@0 304 bool DelayAudioProcessor::hasEditor() const
andrewm@0 305 {
andrewm@0 306 return true; // (change this to false if you choose to not supply an editor)
andrewm@0 307 }
andrewm@0 308
andrewm@0 309 AudioProcessorEditor* DelayAudioProcessor::createEditor()
andrewm@0 310 {
andrewm@0 311 return new DelayAudioProcessorEditor (this);
andrewm@0 312 }
andrewm@0 313
andrewm@0 314 //==============================================================================
andrewm@0 315 void DelayAudioProcessor::getStateInformation (MemoryBlock& destData)
andrewm@0 316 {
andrewm@0 317 // You should use this method to store your parameters in the memory block.
andrewm@0 318 // You could do that either as raw data, or use the XML or ValueTree classes
andrewm@0 319 // as intermediaries to make it easy to save and load complex data.
andrewm@0 320
andrewm@0 321 // Create an outer XML element..
andrewm@0 322 XmlElement xml("C4DMPLUGINSETTINGS");
andrewm@0 323
andrewm@0 324 // add some attributes to it..
andrewm@0 325 xml.setAttribute("uiWidth", lastUIWidth_);
andrewm@0 326 xml.setAttribute("uiHeight", lastUIHeight_);
andrewm@0 327 xml.setAttribute("delayLength", delayLength_);
andrewm@0 328 xml.setAttribute("feedback", feedback_);
andrewm@0 329 xml.setAttribute("dryMix", dryMix_);
andrewm@0 330 xml.setAttribute("wetMix", wetMix_);
andrewm@0 331
andrewm@0 332 // then use this helper function to stuff it into the binary blob and return it..
andrewm@0 333 copyXmlToBinary(xml, destData);
andrewm@0 334 }
andrewm@0 335
andrewm@0 336 void DelayAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
andrewm@0 337 {
andrewm@0 338 // You should use this method to restore your parameters from this memory block,
andrewm@0 339 // whose contents will have been created by the getStateInformation() call.
andrewm@0 340
andrewm@0 341 // This getXmlFromBinary() helper function retrieves our XML from the binary blob..
andrewm@0 342 ScopedPointer<XmlElement> xmlState (getXmlFromBinary (data, sizeInBytes));
andrewm@0 343
andrewm@0 344 if(xmlState != 0)
andrewm@0 345 {
andrewm@0 346 // make sure that it's actually our type of XML object..
andrewm@0 347 if(xmlState->hasTagName("C4DMPLUGINSETTINGS"))
andrewm@0 348 {
andrewm@0 349 // ok, now pull out our parameters..
andrewm@0 350 lastUIWidth_ = xmlState->getIntAttribute("uiWidth", lastUIWidth_);
andrewm@0 351 lastUIHeight_ = xmlState->getIntAttribute("uiHeight", lastUIHeight_);
andrewm@0 352
andrewm@0 353 delayLength_ = (float)xmlState->getDoubleAttribute("delayLength", delayLength_);
andrewm@0 354 feedback_ = (float)xmlState->getDoubleAttribute("feedback", feedback_);
andrewm@0 355 dryMix_ = (float)xmlState->getDoubleAttribute("dryMix", dryMix_);
andrewm@0 356 wetMix_ = (float)xmlState->getDoubleAttribute("wetMix", wetMix_);
andrewm@0 357 }
andrewm@0 358 }
andrewm@0 359 }
andrewm@0 360
andrewm@0 361 //==============================================================================
andrewm@0 362 // This creates new instances of the plugin..
andrewm@0 363 AudioProcessor* JUCE_CALLTYPE createPluginFilter()
andrewm@0 364 {
andrewm@0 365 return new DelayAudioProcessor();
andrewm@0 366 }