-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginProcessor.cpp
More file actions
415 lines (332 loc) · 13.3 KB
/
PluginProcessor.cpp
File metadata and controls
415 lines (332 loc) · 13.3 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
/*
==============================================================================
This file contains the basic framework code for a JUCE plugin processor.
==============================================================================
*/
#include "PluginProcessor.h"
#include "PluginEditor.h"
//==============================================================================
GuideLinesCompAudioProcessor::GuideLinesCompAudioProcessor() : AudioProcessor(
BusesProperties()
.withInput("Input", juce::AudioChannelSet::stereo(), true)
.withOutput("Output", juce::AudioChannelSet::stereo(), true)
),
params(apvts)
{
lowCutFilter.setType(juce::dsp::StateVariableTPTFilterType::highpass);
apvts.state.setProperty(Service::PresetManager::presetNameProperty, "", nullptr);
apvts.state.setProperty("version", ProjectInfo::versionString, nullptr);
presetManager = std::make_unique<Service::PresetManager>(apvts);
}
GuideLinesCompAudioProcessor::~GuideLinesCompAudioProcessor()
{
}
//==============================================================================
const juce::String GuideLinesCompAudioProcessor::getName() const
{
return JucePlugin_Name;
}
bool GuideLinesCompAudioProcessor::acceptsMidi() const
{
#if JucePlugin_WantsMidiInput
return true;
#else
return false;
#endif
}
bool GuideLinesCompAudioProcessor::producesMidi() const
{
#if JucePlugin_ProducesMidiOutput
return true;
#else
return false;
#endif
}
bool GuideLinesCompAudioProcessor::isMidiEffect() const
{
#if JucePlugin_IsMidiEffect
return true;
#else
return false;
#endif
}
double GuideLinesCompAudioProcessor::getTailLengthSeconds() const
{
return 0.0;
}
int GuideLinesCompAudioProcessor::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 GuideLinesCompAudioProcessor::getCurrentProgram()
{
return 0;
}
void GuideLinesCompAudioProcessor::setCurrentProgram(int index)
{
}
const juce::String GuideLinesCompAudioProcessor::getProgramName(int index)
{
return {};
}
void GuideLinesCompAudioProcessor::changeProgramName(int index, const juce::String& newName)
{
}
//==============================================================================
void GuideLinesCompAudioProcessor::prepareToPlay(double sampleRate, int samplesPerBlock)
{
params.prepareToPlay(sampleRate);
params.reset();
juce::dsp::ProcessSpec spec;
spec.sampleRate = sampleRate;
spec.maximumBlockSize = juce::uint32(samplesPerBlock);
spec.numChannels = 2;
peakOutputLevelLeft.prepare(sampleRate, 0.05);
peakOutputLevelRight.prepare(sampleRate, 0.05);
lowCutFilter.prepare(spec);
lowCutFilter.reset();
compA.prepare(spec);
compA.reset();
compB.prepare(spec);
compB.reset();
lastLowCut = -1.f;
}
void GuideLinesCompAudioProcessor::releaseResources()
{
// When playback stops, you can use this as an opportunity to free up any
// spare memory, etc.
}
#ifndef JucePlugin_PreferredChannelConfigurations
bool GuideLinesCompAudioProcessor::isBusesLayoutSupported(const BusesLayout& layouts) const
{
const auto mono = juce::AudioChannelSet::mono();
const auto stereo = juce::AudioChannelSet::stereo();
const auto in = layouts.getMainInputChannelSet();
const auto out = layouts.getMainOutputChannelSet();
DBG("isBusesLayoutSupported, in: " << in.getDescription() << ", out: " << out.getDescription());
if ((in == mono && out == mono) ||
(in == mono && out == stereo) ||
(in == stereo && out == stereo))
return true;
return false;
}
#endif
void GuideLinesCompAudioProcessor::processBlock(juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{
initializeProcessing(buffer);
updateBypassState();
params.update();
params.smoothen();
updateLowCutFilter();
updateMappedCompressorParameters();
peakOutputLevelLeft.reset();
peakOutputLevelRight.reset();
// Route input/output
juce::AudioBuffer<float> mainInput = getBusBuffer(buffer, true, 0);
juce::AudioBuffer<float> mainOutput = getBusBuffer(buffer, false, 0);
const int numInputChannels = mainInput.getNumChannels();
const int numOutputChannels = mainOutput.getNumChannels();
const int numSamples = buffer.getNumSamples();
for (int ch = numInputChannels; ch < numOutputChannels; ++ch)
mainOutput.clear(ch, 0, numSamples);
for (int ch = 0; ch < juce::jmin(numInputChannels, numOutputChannels); ++ch)
mainOutput.copyFrom(ch, 0, mainInput, ch, 0, numSamples);
juce::dsp::AudioBlock<float> block(mainOutput);
juce::dsp::ProcessContextReplacing<float> ctx(block);
mainOutput.applyGain(compressInputGainSmoother.getNextValue());
peakInputLevelForKnob.store(juce::jmax(peakInputLevelLeft.getPeak(), peakInputLevelRight.getPeak()));
// --- Measure & compute input RMS + peak BEFORE processing
updateRMSLevels(mainOutput, rmsInputLevelLeft, rmsInputLevelRight);
updatePeakLevels(mainOutput, peakInputLevelLeft, peakInputLevelRight);
rmsInputLevelLeft.computeRMS();
rmsInputLevelRight.computeRMS();
lowCutFilter.process(ctx);
compA.processCompression(ctx);
// --- Measure & compute interstage RMS
updateRMSLevels(mainOutput, rmsCompAOutputLeft, rmsCompAOutputRight);
rmsCompAOutputLeft.computeRMS();
rmsCompAOutputRight.computeRMS();
compB.processCompression(ctx);
updateRMSLevels(mainOutput, rmsCompBOutputLeft, rmsCompBOutputRight);
rmsCompBOutputLeft.computeRMS();
rmsCompBOutputRight.computeRMS();
outputGainProcessor.setGainLinear(params.outputGain);
outputGainProcessor.process(ctx);
// --- Measure & compute output RMS + peak AFTER all processing
updateRMSLevels(mainOutput, rmsOutputLevelLeft, rmsOutputLevelRight);
updatePeakLevels(mainOutput, peakOutputLevelLeft, peakOutputLevelRight);
rmsOutputLevelLeft.computeRMS();
rmsOutputLevelRight.computeRMS();
// --- Compute gain reduction using the stored values
const float rmsInputL = rmsInputLevelLeft.getValue();
const float rmsInputR = rmsInputLevelRight.getValue();
const float rmsCompAOutL = rmsCompAOutputLeft.getValue();
const float rmsCompAOutR = rmsCompAOutputRight.getValue();
const float rmsCompBOutL = rmsCompBOutputLeft.getValue();
const float rmsCompBOutR = rmsCompBOutputRight.getValue();
float totalGR_L = juce::Decibels::gainToDecibels(rmsInputL) - juce::Decibels::gainToDecibels(rmsCompBOutL);
float totalGR_R = juce::Decibels::gainToDecibels(rmsInputR) - juce::Decibels::gainToDecibels(rmsCompBOutR);
float grL = 1.0f;
if (rmsInputL > 0.0f && rmsCompBOutL > 0.0f)
grL = rmsCompBOutL / rmsInputL;
float grR = 1.0f;
if (rmsInputR > 0.0f && rmsCompBOutR > 0.0f)
grR = rmsCompBOutR / rmsInputR;
rmsTotalGainReductionLeft.update(grL);
rmsTotalGainReductionRight.update(grR);
rmsTotalGainReductionLeft.computeRMS();
rmsTotalGainReductionRight.computeRMS();
float rmsGainReductionDbL = juce::Decibels::gainToDecibels(rmsTotalGainReductionLeft.getValue());
float rmsGainReductionDbR = juce::Decibels::gainToDecibels(rmsTotalGainReductionRight.getValue());
compAGainReductionDbLeft.store(
juce::Decibels::gainToDecibels(rmsInputL) - juce::Decibels::gainToDecibels(rmsCompAOutL));
compAGainReductionDbRight.store(
juce::Decibels::gainToDecibels(rmsInputR) - juce::Decibels::gainToDecibels(rmsCompAOutR));
compBGainReductionDbLeft.store(
juce::Decibels::gainToDecibels(rmsCompAOutL) - juce::Decibels::gainToDecibels(rmsCompBOutL));
compBGainReductionDbRight.store(
juce::Decibels::gainToDecibels(rmsCompAOutR) - juce::Decibels::gainToDecibels(rmsCompBOutR));
float compAMax = juce::jmax(compAGainReductionDbLeft.load(), compAGainReductionDbRight.load());
float compBMax = juce::jmax(compBGainReductionDbLeft.load(), compBGainReductionDbRight.load());
compressionAmountForKnob.store(juce::jmax(compAMax, compBMax));
peakOutputLevelForKnob.store(juce::jmax(peakOutputLevelLeft.getPeak(), peakOutputLevelRight.getPeak()));
#if JUCE_DEBUG
protectYourEars(buffer);
#endif
}
//==============================================================================
bool GuideLinesCompAudioProcessor::hasEditor() const
{
return true; // (change this to false if you choose to not supply an editor)
}
juce::AudioProcessorEditor* GuideLinesCompAudioProcessor::createEditor()
{
return new GuideLinesCompAudioProcessorEditor(*this);
}
//==============================================================================
void GuideLinesCompAudioProcessor::getStateInformation(juce::MemoryBlock& destData)
{
juce::MemoryOutputStream mos(destData, true);
apvts.state.writeToStream(mos);
}
void GuideLinesCompAudioProcessor::setStateInformation(const void* data, int sizeInBytes)
{
auto tree = juce::ValueTree::readFromData(data, sizeInBytes);
if (tree.isValid())
{
apvts.replaceState(tree);
}
}
//==============================================================================
// This creates new instances of the plugin..
juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
return new GuideLinesCompAudioProcessor();
}
juce::AudioProcessorParameter* GuideLinesCompAudioProcessor::getBypassParameter() const
{
return params.bypassParam;
}
void GuideLinesCompAudioProcessor::initializeProcessing(juce::AudioBuffer<float>& buffer)
{
juce::ScopedNoDenormals noDenormals;
int totalNumInputChannels = getTotalNumInputChannels();
int totalNumOutputChannels = getTotalNumOutputChannels();
for (int i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
buffer.clear(i, 0, buffer.getNumSamples());
params.update();
}
void GuideLinesCompAudioProcessor::updateBypassState()
{
if (params.bypassed && bypassFade >= 1.0f)
return;
if (params.bypassed && bypassFade < 1.0f)
bypassFade += bypassFadeInc;
else if (!params.bypassed && bypassFade > 0.0f)
bypassFade -= bypassFadeInc;
}
void GuideLinesCompAudioProcessor::updateLowCutFilter()
{
if (params.lowCut != lastLowCut)
{
lowCutFilter.setCutoffFrequency(params.lowCut);
lastLowCut = params.lowCut;
}
}
void GuideLinesCompAudioProcessor::updateMappedCompressorParameters()
{
//--- Raw parameter inputs ---
float controlValue = juce::jlimit(1.0f, 100.0f, params.control);
float compressValue = juce::jlimit(1.0f, 100.0f, params.compression);
//--- Normalized values ---
float normCompress = compressValue / 100.0f;
float normControl = controlValue / 100.0f;
//--- Input gain (from compression value) ---
float inputGainDb = juce::jmap(normCompress, -3.0f, 12.0f);
float inputGainLin = juce::Decibels::decibelsToGain(inputGainDb);
compressInputGainSmoother.setTargetValue(inputGainLin);
//--- Compressor envelope shaping (from control value) ---
float mappedAttack = juce::mapToLog10(normControl, 60.0f, 1.0f);
float mappedRelease = juce::jmap(controlValue, 0.0f, 100.0f, 55.0f, 100.0f);
float mappedThreshold = juce::jmap(controlValue, 0.0f, 100.0f, -12.0f, -24.0f);
//--- Ratio scaling (from compression value) ---
float mappedRatio = juce::jmap(compressValue, 0.0f, 100.0f, 2.0f, 10.0f);
//--- Apply smoothed values ---
controlAttackASmoother.setTargetValue(mappedAttack);
controlReleaseASmoother.setTargetValue(mappedRelease);
controlThresholdASmoother.setTargetValue(mappedThreshold);
compressRatioASmoother.setTargetValue(mappedRatio);
//--- Update visible state ---
controlAttackA = mappedAttack;
controlReleaseA = mappedRelease;
compressThresholdA = mappedThreshold;
compressRatioA = mappedRatio;
//--- Input to compressor ---
compA.updateCompressorSettings(
controlAttackASmoother.getNextValue(),
controlReleaseASmoother.getNextValue(),
compressRatioASmoother.getNextValue(),
controlThresholdASmoother.getNextValue());
}
void GuideLinesCompAudioProcessor::updateRMSLevels(const juce::AudioBuffer<float>& buffer,
RmsMeasurement& rmsLevelLeft,
RmsMeasurement& rmsLevelRight)
{
const int numChannels = buffer.getNumChannels();
const int numSamples = buffer.getNumSamples();
for (int ch = 0; ch < numChannels; ++ch)
{
const float* data = buffer.getReadPointer(ch);
for (int i = 0; i < numSamples; ++i)
{
float sample = data[i];
if (ch == 0)
rmsLevelLeft.update(sample);
else if (ch == 1)
rmsLevelRight.update(sample);
}
}
}
void GuideLinesCompAudioProcessor::updatePeakLevels(
const juce::AudioBuffer<float>& buffer,
Measurement& peakLevelLeft,
Measurement& peakLevelRight)
{
const int numChannels = buffer.getNumChannels();
const int numSamples = buffer.getNumSamples();
for (int ch = 0; ch < numChannels; ++ch)
{
const float* data = buffer.getReadPointer(ch);
for (int i = 0; i < numSamples; ++i)
{
const float absSample = std::fabs(data[i]);
if (ch == 0)
peakLevelLeft.updateIfGreater(absSample);
else if (ch == 1)
peakLevelRight.updateIfGreater(absSample);
}
}
peakLevelLeft.updateSmoothed();
peakLevelRight.updateSmoothed();
}