annotate effects/tremolo/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 Tremolo: amplitude modulation using a low-frequency oscillator
andrewm@0 10 See textbook Chapter 5: Amplitude Modulation
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 //==============================================================================
andrewm@0 35 TremoloAudioProcessor::TremoloAudioProcessor()
andrewm@0 36 {
andrewm@0 37 // Set default values:
andrewm@0 38 frequency_ = 2.0;
andrewm@0 39 depth_ = 1.0;
andrewm@0 40 waveform_ = kWaveformSine;
andrewm@0 41
andrewm@0 42 lfoPhase_ = 0.0;
andrewm@0 43 inverseSampleRate_ = 1.0/44100.0;
andrewm@0 44
andrewm@0 45 lastUIWidth_ = 370;
andrewm@0 46 lastUIHeight_ = 140;
andrewm@0 47 }
andrewm@0 48
andrewm@0 49 TremoloAudioProcessor::~TremoloAudioProcessor()
andrewm@0 50 {
andrewm@0 51 }
andrewm@0 52
andrewm@0 53 //==============================================================================
andrewm@0 54 const String TremoloAudioProcessor::getName() const
andrewm@0 55 {
andrewm@0 56 return JucePlugin_Name;
andrewm@0 57 }
andrewm@0 58
andrewm@0 59 int TremoloAudioProcessor::getNumParameters()
andrewm@0 60 {
andrewm@0 61 return kNumParameters;
andrewm@0 62 }
andrewm@0 63
andrewm@0 64 float TremoloAudioProcessor::getParameter (int index)
andrewm@0 65 {
andrewm@0 66 // This method will be called by the host, probably on the audio thread, so
andrewm@0 67 // it's absolutely time-critical. Don't use critical sections or anything
andrewm@0 68 // UI-related, or anything at all that may block in any way!
andrewm@0 69 switch (index)
andrewm@0 70 {
andrewm@0 71 case kFrequencyParam: return frequency_;
andrewm@0 72 case kDepthParam: return depth_;
andrewm@0 73 case kWaveformParam: return (float)waveform_;
andrewm@0 74 default: return 0.0f;
andrewm@0 75 }
andrewm@0 76 }
andrewm@0 77
andrewm@0 78 void TremoloAudioProcessor::setParameter (int index, float newValue)
andrewm@0 79 {
andrewm@0 80 // This method will be called by the host, probably on the audio thread, so
andrewm@0 81 // it's absolutely time-critical. Don't use critical sections or anything
andrewm@0 82 // UI-related, or anything at all that may block in any way!
andrewm@0 83
andrewm@0 84 switch (index)
andrewm@0 85 {
andrewm@0 86 case kFrequencyParam:
andrewm@0 87 frequency_ = newValue;
andrewm@0 88 break;
andrewm@0 89 case kDepthParam:
andrewm@0 90 depth_ = newValue;
andrewm@0 91 break;
andrewm@0 92 case kWaveformParam:
andrewm@0 93 waveform_ = (int)newValue;
andrewm@0 94 break;
andrewm@0 95 default:
andrewm@0 96 break;
andrewm@0 97 }
andrewm@0 98 }
andrewm@0 99
andrewm@0 100 const String TremoloAudioProcessor::getParameterName (int index)
andrewm@0 101 {
andrewm@0 102 switch (index)
andrewm@0 103 {
andrewm@0 104 case kFrequencyParam: return "frequency";
andrewm@0 105 case kDepthParam: return "depth";
andrewm@0 106 case kWaveformParam: return "waveform";
andrewm@0 107 default: break;
andrewm@0 108 }
andrewm@0 109
andrewm@0 110 return String::empty;
andrewm@0 111 }
andrewm@0 112
andrewm@0 113 const String TremoloAudioProcessor::getParameterText (int index)
andrewm@0 114 {
andrewm@0 115 return String (getParameter (index), 2);
andrewm@0 116 }
andrewm@0 117
andrewm@0 118 const String TremoloAudioProcessor::getInputChannelName (int channelIndex) const
andrewm@0 119 {
andrewm@0 120 return String (channelIndex + 1);
andrewm@0 121 }
andrewm@0 122
andrewm@0 123 const String TremoloAudioProcessor::getOutputChannelName (int channelIndex) const
andrewm@0 124 {
andrewm@0 125 return String (channelIndex + 1);
andrewm@0 126 }
andrewm@0 127
andrewm@0 128 bool TremoloAudioProcessor::isInputChannelStereoPair (int index) const
andrewm@0 129 {
andrewm@0 130 return true;
andrewm@0 131 }
andrewm@0 132
andrewm@0 133 bool TremoloAudioProcessor::isOutputChannelStereoPair (int index) const
andrewm@0 134 {
andrewm@0 135 return true;
andrewm@0 136 }
andrewm@0 137
andrewm@0 138 bool TremoloAudioProcessor::silenceInProducesSilenceOut() const
andrewm@0 139 {
andrewm@0 140 #if JucePlugin_SilenceInProducesSilenceOut
andrewm@0 141 return true;
andrewm@0 142 #else
andrewm@0 143 return false;
andrewm@0 144 #endif
andrewm@0 145 }
andrewm@0 146
andrewm@0 147 double TremoloAudioProcessor::getTailLengthSeconds() const
andrewm@0 148 {
andrewm@0 149 return 0.0;
andrewm@0 150 }
andrewm@0 151
andrewm@0 152 bool TremoloAudioProcessor::acceptsMidi() const
andrewm@0 153 {
andrewm@0 154 #if JucePlugin_WantsMidiInput
andrewm@0 155 return true;
andrewm@0 156 #else
andrewm@0 157 return false;
andrewm@0 158 #endif
andrewm@0 159 }
andrewm@0 160
andrewm@0 161 bool TremoloAudioProcessor::producesMidi() const
andrewm@0 162 {
andrewm@0 163 #if JucePlugin_ProducesMidiOutput
andrewm@0 164 return true;
andrewm@0 165 #else
andrewm@0 166 return false;
andrewm@0 167 #endif
andrewm@0 168 }
andrewm@0 169
andrewm@0 170 int TremoloAudioProcessor::getNumPrograms()
andrewm@0 171 {
andrewm@0 172 return 0;
andrewm@0 173 }
andrewm@0 174
andrewm@0 175 int TremoloAudioProcessor::getCurrentProgram()
andrewm@0 176 {
andrewm@0 177 return 0;
andrewm@0 178 }
andrewm@0 179
andrewm@0 180 void TremoloAudioProcessor::setCurrentProgram (int index)
andrewm@0 181 {
andrewm@0 182 }
andrewm@0 183
andrewm@0 184 const String TremoloAudioProcessor::getProgramName (int index)
andrewm@0 185 {
andrewm@0 186 return String::empty;
andrewm@0 187 }
andrewm@0 188
andrewm@0 189 void TremoloAudioProcessor::changeProgramName (int index, const String& newName)
andrewm@0 190 {
andrewm@0 191 }
andrewm@0 192
andrewm@0 193 //==============================================================================
andrewm@0 194 void TremoloAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
andrewm@0 195 {
andrewm@0 196 // Calculations that happen before play begins. Pretty simple in this effect, just
andrewm@0 197 // reset the previous state.
andrewm@0 198 lfoPhase_ = 0.0;
andrewm@0 199 inverseSampleRate_ = 1.0/sampleRate;
andrewm@0 200 }
andrewm@0 201
andrewm@0 202 void TremoloAudioProcessor::releaseResources()
andrewm@0 203 {
andrewm@0 204 // When playback stops, you can use this as an opportunity to free up any
andrewm@0 205 // spare memory, etc.
andrewm@0 206 }
andrewm@0 207
andrewm@0 208 void TremoloAudioProcessor::reset()
andrewm@0 209 {
andrewm@0 210 // Use this method as the place to clear any delay lines, buffers, etc, as it
andrewm@0 211 // means there's been a break in the audio's continuity.
andrewm@0 212
andrewm@0 213 lfoPhase_ = 0.0;
andrewm@0 214 }
andrewm@0 215
andrewm@0 216
andrewm@0 217 void TremoloAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
andrewm@0 218 {
andrewm@0 219 // Helpful information about this block of samples:
andrewm@0 220 const int numInputChannels = getNumInputChannels(); // How many input channels for our effect?
andrewm@0 221 const int numOutputChannels = getNumOutputChannels(); // How many output channels for our effect?
andrewm@0 222 const int numSamples = buffer.getNumSamples(); // How many samples in the buffer for this block?
andrewm@0 223
andrewm@0 224 int channel;
andrewm@0 225 float ph;
andrewm@0 226
andrewm@0 227 // Go through each channel of audio that's passed in. In this example we apply identical
andrewm@0 228 // effects to each channel, regardless of how many input channels there are. For some effects, like
andrewm@0 229 // a stereo chorus or panner, you might do something different for each channel.
andrewm@0 230
andrewm@0 231 for (channel = 0; channel < numInputChannels; ++channel)
andrewm@0 232 {
andrewm@0 233 // channelData is an array of length numSamples which contains the audio for one channel
b@1 234 float* channelData = buffer.getWritePointer(channel);
andrewm@0 235
andrewm@0 236 // Make a temporary copy of any state variables declared in PluginProcessor.h which need to be
andrewm@0 237 // maintained between calls to processBlock(). Each channel needs to be processed identically
andrewm@0 238 // which means that the activity of processing one channel can't affect the state variable for
andrewm@0 239 // the next channel.
andrewm@0 240
andrewm@0 241 ph = lfoPhase_;
andrewm@0 242
andrewm@0 243 for (int i = 0; i < numSamples; ++i)
andrewm@0 244 {
andrewm@0 245 const float in = channelData[i];
andrewm@0 246
andrewm@0 247 // Ring modulation is easy! Just multiply the waveform by a periodic carrier
andrewm@0 248 channelData[i] = in * (1.0f - depth_*lfo(ph, waveform_));
andrewm@0 249
andrewm@0 250 // Update the carrier and LFO phases, keeping them in the range 0-1
andrewm@0 251 ph += frequency_*inverseSampleRate_;
andrewm@0 252 if(ph >= 1.0)
andrewm@0 253 ph -= 1.0;
andrewm@0 254 }
andrewm@0 255 }
andrewm@0 256
andrewm@0 257 // Having made a local copy of the state variables for each channel, now transfer the result
andrewm@0 258 // back to the main state variable so they will be preserved for the next call of processBlock()
andrewm@0 259
andrewm@0 260 lfoPhase_ = ph;
andrewm@0 261
andrewm@0 262 // In case we have more outputs than inputs, we'll clear any output
andrewm@0 263 // channels that didn't contain input data, (because these aren't
andrewm@0 264 // guaranteed to be empty - they may contain garbage).
andrewm@0 265 for (int i = numInputChannels; i < numOutputChannels; ++i)
andrewm@0 266 {
andrewm@0 267 buffer.clear (i, 0, buffer.getNumSamples());
andrewm@0 268 }
andrewm@0 269 }
andrewm@0 270
andrewm@0 271 //==============================================================================
andrewm@0 272 bool TremoloAudioProcessor::hasEditor() const
andrewm@0 273 {
andrewm@0 274 return true; // (change this to false if you choose to not supply an editor)
andrewm@0 275 }
andrewm@0 276
andrewm@0 277 AudioProcessorEditor* TremoloAudioProcessor::createEditor()
andrewm@0 278 {
andrewm@0 279 return new TremoloAudioProcessorEditor (this);
andrewm@0 280 }
andrewm@0 281
andrewm@0 282 //==============================================================================
andrewm@0 283 void TremoloAudioProcessor::getStateInformation (MemoryBlock& destData)
andrewm@0 284 {
andrewm@0 285 // You should use this method to store your parameters in the memory block.
andrewm@0 286 // You could do that either as raw data, or use the XML or ValueTree classes
andrewm@0 287 // as intermediaries to make it easy to save and load complex data.
andrewm@0 288
andrewm@0 289 // Create an outer XML element..
andrewm@0 290 XmlElement xml("C4DMPLUGINSETTINGS");
andrewm@0 291
andrewm@0 292 // add some attributes to it..
andrewm@0 293 xml.setAttribute("uiWidth", lastUIWidth_);
andrewm@0 294 xml.setAttribute("uiHeight", lastUIHeight_);
andrewm@0 295 xml.setAttribute("frequency", frequency_);
andrewm@0 296 xml.setAttribute("depth", depth_);
andrewm@0 297 xml.setAttribute("waveform", waveform_);
andrewm@0 298
andrewm@0 299 // then use this helper function to stuff it into the binary blob and return it..
andrewm@0 300 copyXmlToBinary(xml, destData);
andrewm@0 301 }
andrewm@0 302
andrewm@0 303 void TremoloAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
andrewm@0 304 {
andrewm@0 305 // You should use this method to restore your parameters from this memory block,
andrewm@0 306 // whose contents will have been created by the getStateInformation() call.
andrewm@0 307
andrewm@0 308 // This getXmlFromBinary() helper function retrieves our XML from the binary blob..
andrewm@0 309 ScopedPointer<XmlElement> xmlState (getXmlFromBinary (data, sizeInBytes));
andrewm@0 310
andrewm@0 311 if(xmlState != 0)
andrewm@0 312 {
andrewm@0 313 // make sure that it's actually our type of XML object..
andrewm@0 314 if(xmlState->hasTagName("C4DMPLUGINSETTINGS"))
andrewm@0 315 {
andrewm@0 316 // ok, now pull out our parameters..
andrewm@0 317 lastUIWidth_ = xmlState->getIntAttribute("uiWidth", lastUIWidth_);
andrewm@0 318 lastUIHeight_ = xmlState->getIntAttribute("uiHeight", lastUIHeight_);
andrewm@0 319
andrewm@0 320 depth_ = (float)xmlState->getDoubleAttribute("depth", depth_);
andrewm@0 321 frequency_ = (float)xmlState->getDoubleAttribute("frequency", frequency_);
andrewm@0 322 waveform_ = xmlState->getIntAttribute("waveform", waveform_);
andrewm@0 323 }
andrewm@0 324 }
andrewm@0 325 }
andrewm@0 326
andrewm@0 327 //==============================================================================
andrewm@0 328 // Function for calculating LFO waveforms. Phase runs from 0-1, output is scaled
andrewm@0 329 // from 0 to 1 (note: not -1 to 1 as would be typical of sine).
andrewm@0 330 float TremoloAudioProcessor::lfo(float phase, int waveform)
andrewm@0 331 {
andrewm@0 332 switch(waveform)
andrewm@0 333 {
andrewm@0 334 case kWaveformTriangle:
andrewm@0 335 if(phase < 0.25f)
andrewm@0 336 return 0.5f + 2.0f*phase;
andrewm@0 337 else if(phase < 0.75f)
andrewm@0 338 return 1.0f - 2.0f*(phase - 0.25f);
andrewm@0 339 else
andrewm@0 340 return 2.0f*(phase-0.75f);
andrewm@0 341 case kWaveformSquare:
andrewm@0 342 if(phase < 0.5f)
andrewm@0 343 return 1.0f;
andrewm@0 344 else
andrewm@0 345 return 0.0f;
andrewm@0 346 case kWaveformSquareSlopedEdges:
andrewm@0 347 if(phase < 0.48f)
andrewm@0 348 return 1.0f;
andrewm@0 349 else if(phase < 0.5f)
andrewm@0 350 return 1.0f - 50.0f*(phase - 0.48f);
andrewm@0 351 else if(phase < 0.98f)
andrewm@0 352 return 0.0f;
andrewm@0 353 else
andrewm@0 354 return 50.0f*(phase - 0.98f);
andrewm@0 355 case kWaveformSine:
andrewm@0 356 default:
andrewm@0 357 return 0.5f + 0.5f*sinf(2.0 * M_PI * phase);
andrewm@0 358 }
andrewm@0 359 }
andrewm@0 360
andrewm@0 361
andrewm@0 362 //==============================================================================
andrewm@0 363 // This creates new instances of the plugin..
andrewm@0 364 AudioProcessor* JUCE_CALLTYPE createPluginFilter()
andrewm@0 365 {
andrewm@0 366 return new TremoloAudioProcessor();
andrewm@0 367 }