-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginProcessor.cpp
More file actions
469 lines (375 loc) · 19.6 KB
/
PluginProcessor.cpp
File metadata and controls
469 lines (375 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
==============================================================================
This file contains the basic framework code for a JUCE plugin processor.
==============================================================================
*/
#include "PluginProcessor.h"
#include "PluginEditor.h"
#include "GUI/Utilities.h"
//==============================================================================
SimpleEQAudioProcessor::SimpleEQAudioProcessor()
#ifndef JucePlugin_PreferredChannelConfigurations
: AudioProcessor (BusesProperties()
#if ! JucePlugin_IsMidiEffect
#if ! JucePlugin_IsSynth
.withInput ("Input", juce::AudioChannelSet::stereo(), true)
#endif
.withOutput ("Output", juce::AudioChannelSet::stereo(), true)
#endif
)
#endif
{
}
SimpleEQAudioProcessor::~SimpleEQAudioProcessor()
{
}
//==============================================================================
const juce::String SimpleEQAudioProcessor::getName() const
{
return JucePlugin_Name;
}
bool SimpleEQAudioProcessor::acceptsMidi() const
{
#if JucePlugin_WantsMidiInput
return true;
#else
return false;
#endif
}
bool SimpleEQAudioProcessor::producesMidi() const
{
#if JucePlugin_ProducesMidiOutput
return true;
#else
return false;
#endif
}
bool SimpleEQAudioProcessor::isMidiEffect() const
{
#if JucePlugin_IsMidiEffect
return true;
#else
return false;
#endif
}
double SimpleEQAudioProcessor::getTailLengthSeconds() const
{
return 0.0;
}
int SimpleEQAudioProcessor::getNumPrograms()
{
return 1; // NB: some hosts don't cope very well if you tell them there are 0 programs,
// so this should be at least 1, even if you're not really implementing programs.
}
int SimpleEQAudioProcessor::getCurrentProgram()
{
return 0;
}
void SimpleEQAudioProcessor::setCurrentProgram (int index)
{
}
const juce::String SimpleEQAudioProcessor::getProgramName (int index)
{
return {};
}
void SimpleEQAudioProcessor::changeProgramName (int index, const juce::String& newName)
{
}
//==============================================================================
void SimpleEQAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
// Use this method as the place to do any pre-playback
// initialisation that you need..
juce::dsp::ProcessSpec spec;
spec.maximumBlockSize = samplesPerBlock;
spec.numChannels = 1;
spec.sampleRate = sampleRate;
leftChain.prepare(spec);
rightChain.prepare(spec);
updateFilters();
leftChannelFifo.prepare(samplesPerBlock);
rightChannelFifo.prepare(samplesPerBlock);
osc.initialise([](float x) { return std::sin(x); });
spec.numChannels = getTotalNumOutputChannels();
osc.prepare(spec);
osc.setFrequency(440);
}
void SimpleEQAudioProcessor::releaseResources()
{
// When playback stops, you can use this as an opportunity to free up any
// spare memory, etc.
}
#ifndef JucePlugin_PreferredChannelConfigurations
bool SimpleEQAudioProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const
{
#if JucePlugin_IsMidiEffect
juce::ignoreUnused (layouts);
return true;
#else
// This is the place where you check if the layout is supported.
// In this template code we only support mono or stereo.
// Some plugin hosts, such as certain GarageBand versions, will only
// load plugins that support stereo bus layouts.
if (//layouts.getMainOutputChannelSet() != juce::AudioChannelSet::stereo()
layouts.getMainOutputChannelSet() != juce::AudioChannelSet::stereo())
return false;
// This checks if the input layout matches the output layout
#if ! JucePlugin_IsSynth
if (layouts.getMainOutputChannelSet() != layouts.getMainInputChannelSet())
return false;
#endif
return true;
#endif
}
#endif
void SimpleEQAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{
juce::ScopedNoDenormals noDenormals;
auto totalNumInputChannels = getTotalNumInputChannels();
auto totalNumOutputChannels = getTotalNumOutputChannels();
// In case we have more outputs than inputs, this code clears any output
// channels that didn't contain input data, (because these aren't
// guaranteed to be empty - they may contain garbage).
// This is here to avoid people getting screaming feedback
// when they first compile a plugin, but obviously you don't need to keep
// this code if your algorithm always overwrites all the output channels.
for (auto i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
buffer.clear (i, 0, buffer.getNumSamples());
updateFilters();
juce::dsp::AudioBlock<float> block(buffer);
// buffer.clear();
//
// for( int i = 0; i < buffer.getNumSamples(); ++i )
// {
// buffer.setSample(0, i, osc.processSample(0));
// }
//
// juce::dsp::ProcessContextReplacing<float> stereoContext(block);
// osc.process(stereoContext);
auto leftBlock = block.getSingleChannelBlock(0);
auto rightBlock = block.getSingleChannelBlock(1);
juce::dsp::ProcessContextReplacing<float> leftContext(leftBlock);
juce::dsp::ProcessContextReplacing<float> rightContext(rightBlock);
leftChain.process(leftContext);
rightChain.process(rightContext);
leftChannelFifo.update(buffer);
rightChannelFifo.update(buffer);
}
//==============================================================================
bool SimpleEQAudioProcessor::hasEditor() const
{
return true; // (change this to false if you choose to not supply an editor)
}
juce::AudioProcessorEditor* SimpleEQAudioProcessor::createEditor()
{
return new SimpleEQAudioProcessorEditor (*this);
// return new juce::GenericAudioProcessorEditor(*this);
}
//==============================================================================
void SimpleEQAudioProcessor::getStateInformation (juce::MemoryBlock& destData)
{
// You should use this method to store your parameters in the memory block.
// You could do that either as raw data, or use the XML or ValueTree classes
// as intermediaries to make it easy to save and load complex data.
juce::MemoryOutputStream mos(destData, true);
apvts.state.writeToStream(mos);
}
void SimpleEQAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
{
// You should use this method to restore your parameters from this memory block,
// whose contents will have been created by the getStateInformation() call.
auto tree = juce::ValueTree::readFromData(data, sizeInBytes);
if( tree.isValid() )
{
apvts.replaceState(tree);
updateFilters();
}
}
ChainSettings getChainSettings(juce::AudioProcessorValueTreeState& apvts)
{
ChainSettings settings;
settings.lowCutFreq = apvts.getRawParameterValue("LowCut Freq")->load();
settings.highCutFreq = apvts.getRawParameterValue("HighCut Freq")->load();
settings.peak1Freq = apvts.getRawParameterValue("Peak1 Freq")->load();
settings.peak2Freq = apvts.getRawParameterValue("Peak2 Freq")->load();
settings.peak3Freq = apvts.getRawParameterValue("Peak3 Freq")->load();
settings.peak1GainInDecibels = apvts.getRawParameterValue("Peak1 Gain")->load();
settings.peak2GainInDecibels = apvts.getRawParameterValue("Peak2 Gain")->load();
settings.peak3GainInDecibels = apvts.getRawParameterValue("Peak3 Gain")->load();
settings.peak1Quality = apvts.getRawParameterValue("Peak1 Quality")->load();
settings.peak2Quality = apvts.getRawParameterValue("Peak2 Quality")->load();
settings.peak3Quality = apvts.getRawParameterValue("Peak3 Quality")->load();
settings.lowCutSlope = static_cast<Slope>(apvts.getRawParameterValue("LowCut Slope")->load());
settings.highCutSlope = static_cast<Slope>(apvts.getRawParameterValue("HighCut Slope")->load());
settings.lowCutBypassed = apvts.getRawParameterValue("LowCut Bypassed")->load() > 0.5f;
settings.peak1Bypassed = apvts.getRawParameterValue("Peak1 Bypassed")->load() > 0.5f;
settings.peak2Bypassed = apvts.getRawParameterValue("Peak2 Bypassed")->load() > 0.5f;
settings.peak3Bypassed = apvts.getRawParameterValue("Peak3 Bypassed")->load() > 0.5f;
settings.highCutBypassed = apvts.getRawParameterValue("HighCut Bypassed")->load() > 0.5f;
return settings;
}
//Coefficients makePeakFilter(const ChainSettings& chainSettings, double sampleRate)
//{
// return juce::dsp::IIR::Coefficients<float>::makePeakFilter(sampleRate,
// chainSettings.peak1Freq,
// chainSettings.peak1Quality,
// juce::Decibels::decibelsToGain(chainSettings.peak1GainInDecibels));
//}
Coefficients makePeakFilter(float freq, float quality, float decibels, double sampleRate)
{
return juce::dsp::IIR::Coefficients<float>::makePeakFilter(sampleRate,
freq,
quality,
juce::Decibels::decibelsToGain(decibels));
}
void SimpleEQAudioProcessor::updatePeakFilter(const ChainSettings &chainSettings)
{
auto sampleRate = getSampleRate();
auto peak1Coefficients = makePeakFilter(chainSettings.peak1Freq,
chainSettings.peak1Quality,
chainSettings.peak1GainInDecibels,
sampleRate);
auto peak2Coefficients = makePeakFilter(chainSettings.peak2Freq,
chainSettings.peak2Quality,
chainSettings.peak2GainInDecibels,
sampleRate);
auto peak3Coefficients = makePeakFilter(chainSettings.peak3Freq,
chainSettings.peak3Quality,
chainSettings.peak3GainInDecibels,
sampleRate);
leftChain.setBypassed<ChainPositions::Peak1>(chainSettings.peak1Bypassed);
rightChain.setBypassed<ChainPositions::Peak1>(chainSettings.peak1Bypassed);
leftChain.setBypassed<ChainPositions::Peak2>(chainSettings.peak2Bypassed);
rightChain.setBypassed<ChainPositions::Peak2>(chainSettings.peak2Bypassed);
leftChain.setBypassed<ChainPositions::Peak3>(chainSettings.peak3Bypassed);
rightChain.setBypassed<ChainPositions::Peak3>(chainSettings.peak3Bypassed);
updateCoefficients(leftChain.get<ChainPositions::Peak1>().coefficients, peak1Coefficients);
updateCoefficients(rightChain.get<ChainPositions::Peak1>().coefficients, peak1Coefficients);
updateCoefficients(leftChain.get<ChainPositions::Peak2>().coefficients, peak2Coefficients);
updateCoefficients(rightChain.get<ChainPositions::Peak2>().coefficients, peak2Coefficients);
updateCoefficients(leftChain.get<ChainPositions::Peak3>().coefficients, peak3Coefficients);
updateCoefficients(rightChain.get<ChainPositions::Peak3>().coefficients, peak3Coefficients);
}
void updateCoefficients(Coefficients &old, const Coefficients &replacements)
{
*old = *replacements;
}
void SimpleEQAudioProcessor::updateLowCutFilters(const ChainSettings &chainSettings)
{
auto cutCoefficients = makeLowCutFilter(chainSettings, getSampleRate());
auto& leftLowCut = leftChain.get<ChainPositions::LowCut>();
auto& rightLowCut = rightChain.get<ChainPositions::LowCut>();
leftChain.setBypassed<ChainPositions::LowCut>(chainSettings.lowCutBypassed);
rightChain.setBypassed<ChainPositions::LowCut>(chainSettings.lowCutBypassed);
updateCutFilter(rightLowCut, cutCoefficients, chainSettings.lowCutSlope);
updateCutFilter(leftLowCut, cutCoefficients, chainSettings.lowCutSlope);
}
void SimpleEQAudioProcessor::updateHighCutFilters(const ChainSettings &chainSettings)
{
auto highCutCoefficients = makeHighCutFilter(chainSettings, getSampleRate());
auto& leftHighCut = leftChain.get<ChainPositions::HighCut>();
auto& rightHighCut = rightChain.get<ChainPositions::HighCut>();
leftChain.setBypassed<ChainPositions::HighCut>(chainSettings.highCutBypassed);
rightChain.setBypassed<ChainPositions::HighCut>(chainSettings.highCutBypassed);
updateCutFilter(leftHighCut, highCutCoefficients, chainSettings.highCutSlope);
updateCutFilter(rightHighCut, highCutCoefficients, chainSettings.highCutSlope);
}
void SimpleEQAudioProcessor::updateFilters()
{
auto chainSettings = getChainSettings(apvts);
updateLowCutFilters(chainSettings);
updatePeakFilter(chainSettings);
updateHighCutFilters(chainSettings);
}
juce::AudioProcessorValueTreeState::ParameterLayout SimpleEQAudioProcessor::createParameterLayout()
{
juce::AudioProcessorValueTreeState::ParameterLayout layout;
auto freqRange = juce::NormalisableRange<float>(20.f, 20000.f, 1.f, 0.25f);
auto gainRange = juce::NormalisableRange<float>(-24.f, 24.f, 0.5f, 1.f);
auto qualityRange = juce::NormalisableRange<float>(0.1f, 10.f, 0.05f, 1.f);
layout.add(std::make_unique<juce::AudioParameterFloat>(juce::ParameterID("LowCut Freq", 1),
"LowCut Freq",
freqRange,
20.f));
layout.add(std::make_unique<juce::AudioParameterFloat>(juce::ParameterID("HighCut Freq", 1),
"HighCut Freq",
freqRange,
20000.f));
layout.add(std::make_unique<juce::AudioParameterFloat>(juce::ParameterID("Peak1 Freq", 1),
"Peak 1 Freq",
freqRange,
500.f));
layout.add(std::make_unique<juce::AudioParameterFloat>(juce::ParameterID("Peak1 Gain", 1),
"Peak 1 Gain",
gainRange,
0.0f));
layout.add(std::make_unique<juce::AudioParameterFloat>(juce::ParameterID("Peak1 Quality", 1),
"Peak 1 Quality",
qualityRange,
1.f));
layout.add(std::make_unique<juce::AudioParameterFloat>(juce::ParameterID("Peak2 Freq", 1),
"Peak 2 Freq",
freqRange,
1000.f));
layout.add(std::make_unique<juce::AudioParameterFloat>(juce::ParameterID("Peak2 Gain", 1),
"Peak 2 Gain",
gainRange,
0.0f));
layout.add(std::make_unique<juce::AudioParameterFloat>(juce::ParameterID("Peak2 Quality", 1),
"Peak 2 Quality",
qualityRange,
1.f));
layout.add(std::make_unique<juce::AudioParameterFloat>(juce::ParameterID("Peak3 Freq", 1),
"Peak 3 Freq",
freqRange,
3000.f));
layout.add(std::make_unique<juce::AudioParameterFloat>(juce::ParameterID("Peak3 Gain", 1),
"Peak 3 Gain",
gainRange,
0.0f));
layout.add(std::make_unique<juce::AudioParameterFloat>(juce::ParameterID("Peak3 Quality", 1),
"Peak 3 Quality",
qualityRange,
1.f));
juce::StringArray stringArray;
for( int i = 0; i < 4; ++i )
{
juce::String str;
str << (12 + i*12);
str << " db/Oct";
stringArray.add(str);
}
layout.add(std::make_unique<juce::AudioParameterChoice>(juce::ParameterID("LowCut Slope", 1),
"LowCut Slope",
stringArray,
0));
layout.add(std::make_unique<juce::AudioParameterChoice>(juce::ParameterID("HighCut Slope", 1),
"HighCut Slope",
stringArray,
0));
layout.add(std::make_unique<juce::AudioParameterBool>(juce::ParameterID("LowCut Bypassed", 1),
"LowCut Bypassed",
false));
layout.add(std::make_unique<juce::AudioParameterBool>(juce::ParameterID("Peak1 Bypassed", 1),
"Peak 1 Bypassed",
false));
layout.add(std::make_unique<juce::AudioParameterBool>(juce::ParameterID("Peak2 Bypassed", 1),
"Peak 2 Bypassed",
false));
layout.add(std::make_unique<juce::AudioParameterBool>(juce::ParameterID("Peak3 Bypassed", 1),
"Peak 3 Bypassed",
false));
layout.add(std::make_unique<juce::AudioParameterBool>(juce::ParameterID("HighCut Bypassed", 1),
"HighCut Bypassed",
false));
layout.add(std::make_unique<juce::AudioParameterBool>(juce::ParameterID("Analyzer Enabled", 1),
"Analyzer Enabled",
true));
return layout;
}
//==============================================================================
// This creates new instances of the plugin..
juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
return new SimpleEQAudioProcessor();
}