andrewm@0: /* andrewm@0: This code accompanies the textbook: andrewm@0: andrewm@0: Digital Audio Effects: Theory, Implementation and Application andrewm@0: Joshua D. Reiss and Andrew P. McPherson andrewm@0: andrewm@0: --- andrewm@0: andrewm@0: Vibrato: frequency modulation using delay lines andrewm@0: See textbook Chapter 2: Delay Line Effects andrewm@0: andrewm@0: Code by Andrew McPherson, Brecht De Man and Joshua Reiss andrewm@0: andrewm@0: --- andrewm@0: andrewm@0: This program is free software: you can redistribute it and/or modify andrewm@0: it under the terms of the GNU General Public License as published by andrewm@0: the Free Software Foundation, either version 3 of the License, or andrewm@0: (at your option) any later version. andrewm@0: andrewm@0: This program is distributed in the hope that it will be useful, andrewm@0: but WITHOUT ANY WARRANTY; without even the implied warranty of andrewm@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrewm@0: GNU General Public License for more details. andrewm@0: andrewm@0: You should have received a copy of the GNU General Public License andrewm@0: along with this program. If not, see . andrewm@0: */ andrewm@0: andrewm@0: #include "PluginProcessor.h" andrewm@0: #include "PluginEditor.h" andrewm@0: #include andrewm@0: andrewm@0: #ifdef _MSC_VER andrewm@0: #define snprintf _snprintf_s //support for pre-2014 versions of Visual Studio andrewm@0: #endif // _MSC_VER andrewm@0: andrewm@0: //============================================================================== andrewm@0: VibratoAudioProcessorEditor::VibratoAudioProcessorEditor (VibratoAudioProcessor* ownerFilter) andrewm@0: : AudioProcessorEditor (ownerFilter), andrewm@0: sweepWidthLabel_("", "Sweep Width (sec.):"), andrewm@0: frequencyLabel_("", "LFO Frequency:"), andrewm@0: waveformLabel_("", "LFO Waveform:"), andrewm@0: interpolationLabel_("", "Interpolation Type:"), andrewm@0: pitchShiftLabel_("", "Maximum Pitch Shift:") andrewm@0: { andrewm@0: // Initialise variables andrewm@0: oldSweepWidth_ = oldFrequency_ = -1.0; andrewm@0: oldWaveform_ = -1; andrewm@0: andrewm@0: // Set up the sliders andrewm@0: addAndMakeVisible (&sweepWidthSlider_); andrewm@0: sweepWidthSlider_.setSliderStyle (Slider::Rotary); andrewm@0: sweepWidthSlider_.addListener (this); andrewm@0: sweepWidthSlider_.setRange (.001, VibratoAudioProcessor::kMaximumSweepWidth, 0.0005); andrewm@0: andrewm@0: addAndMakeVisible (&frequencySlider_); andrewm@0: frequencySlider_.setSliderStyle (Slider::Rotary); andrewm@0: frequencySlider_.addListener (this); andrewm@0: frequencySlider_.setRange (0.5, 5.0, 0.025); andrewm@0: andrewm@0: addAndMakeVisible(&waveformComboBox_); andrewm@0: waveformComboBox_.setEditableText(false); andrewm@0: waveformComboBox_.setJustificationType(Justification::left); andrewm@0: waveformComboBox_.addItem("Sine", VibratoAudioProcessor::kWaveformSine); andrewm@0: waveformComboBox_.addItem("Triangle", VibratoAudioProcessor::kWaveformTriangle); andrewm@0: waveformComboBox_.addItem("Sawtooth (rising)", VibratoAudioProcessor::kWaveformSawtooth); andrewm@0: waveformComboBox_.addItem("Sawtooth (falling)", VibratoAudioProcessor::kWaveformInverseSawtooth); andrewm@0: waveformComboBox_.addListener(this); andrewm@0: andrewm@0: addAndMakeVisible(&interpolationComboBox_); andrewm@0: interpolationComboBox_.setEditableText(false); andrewm@0: interpolationComboBox_.setJustificationType(Justification::left); andrewm@0: interpolationComboBox_.addItem("None", VibratoAudioProcessor::kInterpolationNearestNeighbour); andrewm@0: interpolationComboBox_.addItem("Linear", VibratoAudioProcessor::kInterpolationLinear); andrewm@0: interpolationComboBox_.addItem("Cubic", VibratoAudioProcessor::kInterpolationCubic); andrewm@0: interpolationComboBox_.addListener(this); andrewm@0: andrewm@0: // This label is informational and exists apart from other controls andrewm@0: // The other labels are attached to sliders and combo boxes andrewm@0: addAndMakeVisible(&pitchShiftLabel_); andrewm@0: pitchShiftLabel_.setFont(Font (12.0f)); andrewm@0: andrewm@0: sweepWidthLabel_.attachToComponent(&sweepWidthSlider_, false); andrewm@0: sweepWidthLabel_.setFont(Font (11.0f)); andrewm@0: andrewm@0: frequencyLabel_.attachToComponent(&frequencySlider_, false); andrewm@0: frequencyLabel_.setFont(Font (11.0f)); andrewm@0: andrewm@0: waveformLabel_.attachToComponent(&waveformComboBox_, false); andrewm@0: waveformLabel_.setFont(Font (11.0f)); andrewm@0: andrewm@0: interpolationLabel_.attachToComponent(&interpolationComboBox_, false); andrewm@0: interpolationLabel_.setFont(Font (11.0f)); andrewm@0: andrewm@0: // add the triangular resizer component for the bottom-right of the UI andrewm@0: addAndMakeVisible(resizer_ = new ResizableCornerComponent (this, &resizeLimits_)); andrewm@0: resizeLimits_.setSizeLimits(370, 160, 600, 300); andrewm@0: andrewm@0: // set our component's initial size to be the last one that was stored in the filter's settings andrewm@0: setSize(ownerFilter->lastUIWidth_, andrewm@0: ownerFilter->lastUIHeight_); andrewm@0: andrewm@0: startTimer(50); andrewm@0: } andrewm@0: andrewm@0: VibratoAudioProcessorEditor::~VibratoAudioProcessorEditor() andrewm@0: { andrewm@0: } andrewm@0: andrewm@0: //============================================================================== andrewm@0: void VibratoAudioProcessorEditor::paint (Graphics& g) andrewm@0: { andrewm@0: g.fillAll (Colours::grey); andrewm@0: } andrewm@0: andrewm@0: void VibratoAudioProcessorEditor::resized() andrewm@0: { andrewm@0: sweepWidthSlider_.setBounds (20, 20, 150, 40); andrewm@0: frequencySlider_.setBounds(200, 20, 150, 40); andrewm@0: pitchShiftLabel_.setBounds(20, 60, 350, 20); andrewm@0: waveformComboBox_.setBounds(20, 100, 150, 30); andrewm@0: interpolationComboBox_.setBounds(200, 100, 150, 30); andrewm@0: andrewm@0: resizer_->setBounds(getWidth() - 16, getHeight() - 16, 16, 16); andrewm@0: andrewm@0: getProcessor()->lastUIWidth_ = getWidth(); andrewm@0: getProcessor()->lastUIHeight_ = getHeight(); andrewm@0: } andrewm@0: andrewm@0: //============================================================================== andrewm@0: // This timer periodically checks whether any of the filter's parameters have changed... andrewm@0: void VibratoAudioProcessorEditor::timerCallback() andrewm@0: { andrewm@0: VibratoAudioProcessor* ourProcessor = getProcessor(); andrewm@0: andrewm@0: sweepWidthSlider_.setValue(ourProcessor->sweepWidth_, dontSendNotification); andrewm@0: frequencySlider_.setValue(ourProcessor->frequency_, dontSendNotification); b@1: waveformComboBox_.setSelectedId(ourProcessor->waveform_, dontSendNotification); b@1: interpolationComboBox_.setSelectedId(ourProcessor->interpolation_, dontSendNotification); andrewm@0: andrewm@0: // Update the pitch shift label only when something changes to avoid andrewm@0: // needless calculations andrewm@0: if(ourProcessor->sweepWidth_ != oldSweepWidth_ || andrewm@0: ourProcessor->frequency_ != oldFrequency_ || andrewm@0: ourProcessor->waveform_ != oldWaveform_) andrewm@0: { andrewm@0: updatePitchShiftLabel(); andrewm@0: oldSweepWidth_ = ourProcessor->sweepWidth_; andrewm@0: oldFrequency_ = ourProcessor->frequency_; andrewm@0: oldWaveform_ = ourProcessor->waveform_; andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: // This is our Slider::Listener callback, when the user drags a slider. andrewm@0: void VibratoAudioProcessorEditor::sliderValueChanged (Slider* slider) andrewm@0: { andrewm@0: // It's vital to use setParameterNotifyingHost to change any parameters that are automatable andrewm@0: // by the host, rather than just modifying them directly, otherwise the host won't know andrewm@0: // that they've changed. andrewm@0: andrewm@0: if (slider == &sweepWidthSlider_) andrewm@0: { andrewm@0: getProcessor()->setParameterNotifyingHost (VibratoAudioProcessor::kSweepWidthParam, andrewm@0: (float)sweepWidthSlider_.getValue()); andrewm@0: updatePitchShiftLabel(); andrewm@0: } andrewm@0: else if (slider == &frequencySlider_) andrewm@0: { andrewm@0: getProcessor()->setParameterNotifyingHost (VibratoAudioProcessor::kFrequencyParam, andrewm@0: (float)frequencySlider_.getValue()); andrewm@0: updatePitchShiftLabel(); andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: // Similar callback to sliderValueChanged for ComboBox updates andrewm@0: void VibratoAudioProcessorEditor::comboBoxChanged (ComboBox *comboBox) andrewm@0: { andrewm@0: if(comboBox == &waveformComboBox_) andrewm@0: { andrewm@0: getProcessor()->setParameterNotifyingHost (VibratoAudioProcessor::kWaveformParam, andrewm@0: (float)waveformComboBox_.getSelectedId()); andrewm@0: updatePitchShiftLabel(); andrewm@0: } andrewm@0: else if(comboBox == &interpolationComboBox_) andrewm@0: { andrewm@0: getProcessor()->setParameterNotifyingHost (VibratoAudioProcessor::kInterpolationParam, andrewm@0: (float)interpolationComboBox_.getSelectedId()); andrewm@0: // This parameter doesn't affect the maximum pitch shift andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: // Update the content of the pitch shift label (whenever controls change) andrewm@0: void VibratoAudioProcessorEditor::updatePitchShiftLabel() andrewm@0: { andrewm@0: VibratoAudioProcessor* ourProcessor = getProcessor(); andrewm@0: andrewm@0: // The amount of pitch shift depends on the derivative of the delay, which andrewm@0: // is given by: delay = width * f(frequency * t) andrewm@0: // where f(x) is one of: andrewm@0: // sine --> 0.5 + 0.5*sin(2*pi*x) --> derivative pi*cos(x)*dx andrewm@0: // triangle --> {2.0*x or 1.0-(2.0*(x-0.5)) ---> derivative +/- 2.0*dx andrewm@0: // sawtooth rising --> x --> derivative 1.0*dx andrewm@0: // sawtooth falling --> 1.0 - x --> derivative -1.0*dx andrewm@0: // For f(frequency*t), "dx" = frequency andrewm@0: andrewm@0: float maxSpeed = 1.0, minSpeed = 1.0; andrewm@0: float maxPitch = 0.0, minPitch = 0.0; andrewm@0: char str[256]; andrewm@0: andrewm@0: switch(ourProcessor->waveform_) andrewm@0: { andrewm@0: case VibratoAudioProcessor::kWaveformSine: andrewm@0: maxSpeed = 1.0 + M_PI * ourProcessor->frequency_ * ourProcessor->sweepWidth_; andrewm@0: minSpeed = 1.0 - M_PI * ourProcessor->frequency_ * ourProcessor->sweepWidth_; andrewm@0: break; andrewm@0: case VibratoAudioProcessor::kWaveformTriangle: andrewm@0: maxSpeed = 1.0 + 2.0 * ourProcessor->frequency_ * ourProcessor->sweepWidth_; andrewm@0: minSpeed = 1.0 - 2.0 * ourProcessor->frequency_ * ourProcessor->sweepWidth_; andrewm@0: break; andrewm@0: case VibratoAudioProcessor::kWaveformSawtooth: andrewm@0: // Standard (rising) sawtooth means delay is increasing --> pitch is lower andrewm@0: maxSpeed = 1.0; andrewm@0: minSpeed = 1.0 - ourProcessor->frequency_ * ourProcessor->sweepWidth_; andrewm@0: break; andrewm@0: case VibratoAudioProcessor::kWaveformInverseSawtooth: andrewm@0: // Inverse (falling) sawtooth means delay is decreasing --> pitch is higher andrewm@0: maxSpeed = 1.0 + ourProcessor->frequency_ * ourProcessor->sweepWidth_; andrewm@0: minSpeed = 1.0; andrewm@0: break; andrewm@0: } andrewm@0: andrewm@0: // Convert speed to pitch shift --> semitones = 12*log2(speed) andrewm@0: maxPitch = 12.0*logf(maxSpeed)/logf(2.0); andrewm@0: andrewm@0: if(minSpeed > 0) andrewm@0: { andrewm@0: minPitch = 12.0*logf(minSpeed)/logf(2.0); andrewm@0: snprintf(str, 256, "Vibrato range: %+.2f to %+.2f semitones (speed %.3f to %.3f)", andrewm@0: minPitch, maxPitch, minSpeed, maxSpeed); andrewm@0: } andrewm@0: else andrewm@0: { andrewm@0: snprintf(str, 256, "Vibrato range: --- to %+.2f semitones (speed %.3f to %.3f)", andrewm@0: maxPitch, minSpeed, maxSpeed); andrewm@0: } andrewm@0: andrewm@0: pitchShiftLabel_.setText(str, dontSendNotification); andrewm@0: }