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 Vibrato: frequency modulation using delay lines
|
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 <cstring>
|
andrewm@0
|
33
|
andrewm@0
|
34 #ifdef _MSC_VER
|
andrewm@0
|
35 #define snprintf _snprintf_s //support for pre-2014 versions of Visual Studio
|
andrewm@0
|
36 #endif // _MSC_VER
|
andrewm@0
|
37
|
andrewm@0
|
38 //==============================================================================
|
andrewm@0
|
39 VibratoAudioProcessorEditor::VibratoAudioProcessorEditor (VibratoAudioProcessor* ownerFilter)
|
andrewm@0
|
40 : AudioProcessorEditor (ownerFilter),
|
andrewm@0
|
41 sweepWidthLabel_("", "Sweep Width (sec.):"),
|
andrewm@0
|
42 frequencyLabel_("", "LFO Frequency:"),
|
andrewm@0
|
43 waveformLabel_("", "LFO Waveform:"),
|
andrewm@0
|
44 interpolationLabel_("", "Interpolation Type:"),
|
andrewm@0
|
45 pitchShiftLabel_("", "Maximum Pitch Shift:")
|
andrewm@0
|
46 {
|
andrewm@0
|
47 // Initialise variables
|
andrewm@0
|
48 oldSweepWidth_ = oldFrequency_ = -1.0;
|
andrewm@0
|
49 oldWaveform_ = -1;
|
andrewm@0
|
50
|
andrewm@0
|
51 // Set up the sliders
|
andrewm@0
|
52 addAndMakeVisible (&sweepWidthSlider_);
|
andrewm@0
|
53 sweepWidthSlider_.setSliderStyle (Slider::Rotary);
|
andrewm@0
|
54 sweepWidthSlider_.addListener (this);
|
andrewm@0
|
55 sweepWidthSlider_.setRange (.001, VibratoAudioProcessor::kMaximumSweepWidth, 0.0005);
|
andrewm@0
|
56
|
andrewm@0
|
57 addAndMakeVisible (&frequencySlider_);
|
andrewm@0
|
58 frequencySlider_.setSliderStyle (Slider::Rotary);
|
andrewm@0
|
59 frequencySlider_.addListener (this);
|
andrewm@0
|
60 frequencySlider_.setRange (0.5, 5.0, 0.025);
|
andrewm@0
|
61
|
andrewm@0
|
62 addAndMakeVisible(&waveformComboBox_);
|
andrewm@0
|
63 waveformComboBox_.setEditableText(false);
|
andrewm@0
|
64 waveformComboBox_.setJustificationType(Justification::left);
|
andrewm@0
|
65 waveformComboBox_.addItem("Sine", VibratoAudioProcessor::kWaveformSine);
|
andrewm@0
|
66 waveformComboBox_.addItem("Triangle", VibratoAudioProcessor::kWaveformTriangle);
|
andrewm@0
|
67 waveformComboBox_.addItem("Sawtooth (rising)", VibratoAudioProcessor::kWaveformSawtooth);
|
andrewm@0
|
68 waveformComboBox_.addItem("Sawtooth (falling)", VibratoAudioProcessor::kWaveformInverseSawtooth);
|
andrewm@0
|
69 waveformComboBox_.addListener(this);
|
andrewm@0
|
70
|
andrewm@0
|
71 addAndMakeVisible(&interpolationComboBox_);
|
andrewm@0
|
72 interpolationComboBox_.setEditableText(false);
|
andrewm@0
|
73 interpolationComboBox_.setJustificationType(Justification::left);
|
andrewm@0
|
74 interpolationComboBox_.addItem("None", VibratoAudioProcessor::kInterpolationNearestNeighbour);
|
andrewm@0
|
75 interpolationComboBox_.addItem("Linear", VibratoAudioProcessor::kInterpolationLinear);
|
andrewm@0
|
76 interpolationComboBox_.addItem("Cubic", VibratoAudioProcessor::kInterpolationCubic);
|
andrewm@0
|
77 interpolationComboBox_.addListener(this);
|
andrewm@0
|
78
|
andrewm@0
|
79 // This label is informational and exists apart from other controls
|
andrewm@0
|
80 // The other labels are attached to sliders and combo boxes
|
andrewm@0
|
81 addAndMakeVisible(&pitchShiftLabel_);
|
andrewm@0
|
82 pitchShiftLabel_.setFont(Font (12.0f));
|
andrewm@0
|
83
|
andrewm@0
|
84 sweepWidthLabel_.attachToComponent(&sweepWidthSlider_, false);
|
andrewm@0
|
85 sweepWidthLabel_.setFont(Font (11.0f));
|
andrewm@0
|
86
|
andrewm@0
|
87 frequencyLabel_.attachToComponent(&frequencySlider_, false);
|
andrewm@0
|
88 frequencyLabel_.setFont(Font (11.0f));
|
andrewm@0
|
89
|
andrewm@0
|
90 waveformLabel_.attachToComponent(&waveformComboBox_, false);
|
andrewm@0
|
91 waveformLabel_.setFont(Font (11.0f));
|
andrewm@0
|
92
|
andrewm@0
|
93 interpolationLabel_.attachToComponent(&interpolationComboBox_, false);
|
andrewm@0
|
94 interpolationLabel_.setFont(Font (11.0f));
|
andrewm@0
|
95
|
andrewm@0
|
96 // add the triangular resizer component for the bottom-right of the UI
|
andrewm@0
|
97 addAndMakeVisible(resizer_ = new ResizableCornerComponent (this, &resizeLimits_));
|
andrewm@0
|
98 resizeLimits_.setSizeLimits(370, 160, 600, 300);
|
andrewm@0
|
99
|
andrewm@0
|
100 // set our component's initial size to be the last one that was stored in the filter's settings
|
andrewm@0
|
101 setSize(ownerFilter->lastUIWidth_,
|
andrewm@0
|
102 ownerFilter->lastUIHeight_);
|
andrewm@0
|
103
|
andrewm@0
|
104 startTimer(50);
|
andrewm@0
|
105 }
|
andrewm@0
|
106
|
andrewm@0
|
107 VibratoAudioProcessorEditor::~VibratoAudioProcessorEditor()
|
andrewm@0
|
108 {
|
andrewm@0
|
109 }
|
andrewm@0
|
110
|
andrewm@0
|
111 //==============================================================================
|
andrewm@0
|
112 void VibratoAudioProcessorEditor::paint (Graphics& g)
|
andrewm@0
|
113 {
|
andrewm@0
|
114 g.fillAll (Colours::grey);
|
andrewm@0
|
115 }
|
andrewm@0
|
116
|
andrewm@0
|
117 void VibratoAudioProcessorEditor::resized()
|
andrewm@0
|
118 {
|
andrewm@0
|
119 sweepWidthSlider_.setBounds (20, 20, 150, 40);
|
andrewm@0
|
120 frequencySlider_.setBounds(200, 20, 150, 40);
|
andrewm@0
|
121 pitchShiftLabel_.setBounds(20, 60, 350, 20);
|
andrewm@0
|
122 waveformComboBox_.setBounds(20, 100, 150, 30);
|
andrewm@0
|
123 interpolationComboBox_.setBounds(200, 100, 150, 30);
|
andrewm@0
|
124
|
andrewm@0
|
125 resizer_->setBounds(getWidth() - 16, getHeight() - 16, 16, 16);
|
andrewm@0
|
126
|
andrewm@0
|
127 getProcessor()->lastUIWidth_ = getWidth();
|
andrewm@0
|
128 getProcessor()->lastUIHeight_ = getHeight();
|
andrewm@0
|
129 }
|
andrewm@0
|
130
|
andrewm@0
|
131 //==============================================================================
|
andrewm@0
|
132 // This timer periodically checks whether any of the filter's parameters have changed...
|
andrewm@0
|
133 void VibratoAudioProcessorEditor::timerCallback()
|
andrewm@0
|
134 {
|
andrewm@0
|
135 VibratoAudioProcessor* ourProcessor = getProcessor();
|
andrewm@0
|
136
|
andrewm@0
|
137 sweepWidthSlider_.setValue(ourProcessor->sweepWidth_, dontSendNotification);
|
andrewm@0
|
138 frequencySlider_.setValue(ourProcessor->frequency_, dontSendNotification);
|
b@1
|
139 waveformComboBox_.setSelectedId(ourProcessor->waveform_, dontSendNotification);
|
b@1
|
140 interpolationComboBox_.setSelectedId(ourProcessor->interpolation_, dontSendNotification);
|
andrewm@0
|
141
|
andrewm@0
|
142 // Update the pitch shift label only when something changes to avoid
|
andrewm@0
|
143 // needless calculations
|
andrewm@0
|
144 if(ourProcessor->sweepWidth_ != oldSweepWidth_ ||
|
andrewm@0
|
145 ourProcessor->frequency_ != oldFrequency_ ||
|
andrewm@0
|
146 ourProcessor->waveform_ != oldWaveform_)
|
andrewm@0
|
147 {
|
andrewm@0
|
148 updatePitchShiftLabel();
|
andrewm@0
|
149 oldSweepWidth_ = ourProcessor->sweepWidth_;
|
andrewm@0
|
150 oldFrequency_ = ourProcessor->frequency_;
|
andrewm@0
|
151 oldWaveform_ = ourProcessor->waveform_;
|
andrewm@0
|
152 }
|
andrewm@0
|
153 }
|
andrewm@0
|
154
|
andrewm@0
|
155 // This is our Slider::Listener callback, when the user drags a slider.
|
andrewm@0
|
156 void VibratoAudioProcessorEditor::sliderValueChanged (Slider* slider)
|
andrewm@0
|
157 {
|
andrewm@0
|
158 // It's vital to use setParameterNotifyingHost to change any parameters that are automatable
|
andrewm@0
|
159 // by the host, rather than just modifying them directly, otherwise the host won't know
|
andrewm@0
|
160 // that they've changed.
|
andrewm@0
|
161
|
andrewm@0
|
162 if (slider == &sweepWidthSlider_)
|
andrewm@0
|
163 {
|
andrewm@0
|
164 getProcessor()->setParameterNotifyingHost (VibratoAudioProcessor::kSweepWidthParam,
|
andrewm@0
|
165 (float)sweepWidthSlider_.getValue());
|
andrewm@0
|
166 updatePitchShiftLabel();
|
andrewm@0
|
167 }
|
andrewm@0
|
168 else if (slider == &frequencySlider_)
|
andrewm@0
|
169 {
|
andrewm@0
|
170 getProcessor()->setParameterNotifyingHost (VibratoAudioProcessor::kFrequencyParam,
|
andrewm@0
|
171 (float)frequencySlider_.getValue());
|
andrewm@0
|
172 updatePitchShiftLabel();
|
andrewm@0
|
173 }
|
andrewm@0
|
174 }
|
andrewm@0
|
175
|
andrewm@0
|
176 // Similar callback to sliderValueChanged for ComboBox updates
|
andrewm@0
|
177 void VibratoAudioProcessorEditor::comboBoxChanged (ComboBox *comboBox)
|
andrewm@0
|
178 {
|
andrewm@0
|
179 if(comboBox == &waveformComboBox_)
|
andrewm@0
|
180 {
|
andrewm@0
|
181 getProcessor()->setParameterNotifyingHost (VibratoAudioProcessor::kWaveformParam,
|
andrewm@0
|
182 (float)waveformComboBox_.getSelectedId());
|
andrewm@0
|
183 updatePitchShiftLabel();
|
andrewm@0
|
184 }
|
andrewm@0
|
185 else if(comboBox == &interpolationComboBox_)
|
andrewm@0
|
186 {
|
andrewm@0
|
187 getProcessor()->setParameterNotifyingHost (VibratoAudioProcessor::kInterpolationParam,
|
andrewm@0
|
188 (float)interpolationComboBox_.getSelectedId());
|
andrewm@0
|
189 // This parameter doesn't affect the maximum pitch shift
|
andrewm@0
|
190 }
|
andrewm@0
|
191 }
|
andrewm@0
|
192
|
andrewm@0
|
193 // Update the content of the pitch shift label (whenever controls change)
|
andrewm@0
|
194 void VibratoAudioProcessorEditor::updatePitchShiftLabel()
|
andrewm@0
|
195 {
|
andrewm@0
|
196 VibratoAudioProcessor* ourProcessor = getProcessor();
|
andrewm@0
|
197
|
andrewm@0
|
198 // The amount of pitch shift depends on the derivative of the delay, which
|
andrewm@0
|
199 // is given by: delay = width * f(frequency * t)
|
andrewm@0
|
200 // where f(x) is one of:
|
andrewm@0
|
201 // sine --> 0.5 + 0.5*sin(2*pi*x) --> derivative pi*cos(x)*dx
|
andrewm@0
|
202 // triangle --> {2.0*x or 1.0-(2.0*(x-0.5)) ---> derivative +/- 2.0*dx
|
andrewm@0
|
203 // sawtooth rising --> x --> derivative 1.0*dx
|
andrewm@0
|
204 // sawtooth falling --> 1.0 - x --> derivative -1.0*dx
|
andrewm@0
|
205 // For f(frequency*t), "dx" = frequency
|
andrewm@0
|
206
|
andrewm@0
|
207 float maxSpeed = 1.0, minSpeed = 1.0;
|
andrewm@0
|
208 float maxPitch = 0.0, minPitch = 0.0;
|
andrewm@0
|
209 char str[256];
|
andrewm@0
|
210
|
andrewm@0
|
211 switch(ourProcessor->waveform_)
|
andrewm@0
|
212 {
|
andrewm@0
|
213 case VibratoAudioProcessor::kWaveformSine:
|
andrewm@0
|
214 maxSpeed = 1.0 + M_PI * ourProcessor->frequency_ * ourProcessor->sweepWidth_;
|
andrewm@0
|
215 minSpeed = 1.0 - M_PI * ourProcessor->frequency_ * ourProcessor->sweepWidth_;
|
andrewm@0
|
216 break;
|
andrewm@0
|
217 case VibratoAudioProcessor::kWaveformTriangle:
|
andrewm@0
|
218 maxSpeed = 1.0 + 2.0 * ourProcessor->frequency_ * ourProcessor->sweepWidth_;
|
andrewm@0
|
219 minSpeed = 1.0 - 2.0 * ourProcessor->frequency_ * ourProcessor->sweepWidth_;
|
andrewm@0
|
220 break;
|
andrewm@0
|
221 case VibratoAudioProcessor::kWaveformSawtooth:
|
andrewm@0
|
222 // Standard (rising) sawtooth means delay is increasing --> pitch is lower
|
andrewm@0
|
223 maxSpeed = 1.0;
|
andrewm@0
|
224 minSpeed = 1.0 - ourProcessor->frequency_ * ourProcessor->sweepWidth_;
|
andrewm@0
|
225 break;
|
andrewm@0
|
226 case VibratoAudioProcessor::kWaveformInverseSawtooth:
|
andrewm@0
|
227 // Inverse (falling) sawtooth means delay is decreasing --> pitch is higher
|
andrewm@0
|
228 maxSpeed = 1.0 + ourProcessor->frequency_ * ourProcessor->sweepWidth_;
|
andrewm@0
|
229 minSpeed = 1.0;
|
andrewm@0
|
230 break;
|
andrewm@0
|
231 }
|
andrewm@0
|
232
|
andrewm@0
|
233 // Convert speed to pitch shift --> semitones = 12*log2(speed)
|
andrewm@0
|
234 maxPitch = 12.0*logf(maxSpeed)/logf(2.0);
|
andrewm@0
|
235
|
andrewm@0
|
236 if(minSpeed > 0)
|
andrewm@0
|
237 {
|
andrewm@0
|
238 minPitch = 12.0*logf(minSpeed)/logf(2.0);
|
andrewm@0
|
239 snprintf(str, 256, "Vibrato range: %+.2f to %+.2f semitones (speed %.3f to %.3f)",
|
andrewm@0
|
240 minPitch, maxPitch, minSpeed, maxSpeed);
|
andrewm@0
|
241 }
|
andrewm@0
|
242 else
|
andrewm@0
|
243 {
|
andrewm@0
|
244 snprintf(str, 256, "Vibrato range: --- to %+.2f semitones (speed %.3f to %.3f)",
|
andrewm@0
|
245 maxPitch, minSpeed, maxSpeed);
|
andrewm@0
|
246 }
|
andrewm@0
|
247
|
andrewm@0
|
248 pitchShiftLabel_.setText(str, dontSendNotification);
|
andrewm@0
|
249 } |