forked from gregvds/AudioPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginProcessor.cpp
More file actions
404 lines (338 loc) · 16 KB
/
PluginProcessor.cpp
File metadata and controls
404 lines (338 loc) · 16 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
/*
==============================================================================
This file was auto-generated!
It contains the basic framework code for a JUCE plugin processor.
==============================================================================
*/
#include "PluginProcessor.h"
#include "PluginEditor.h"
//==============================================================================
GainSliderAudioProcessor::GainSliderAudioProcessor()
#ifndef JucePlugin_PreferredChannelConfigurations
: AudioProcessor (BusesProperties()
#if ! JucePlugin_IsMidiEffect
#if ! JucePlugin_IsSynth
.withInput ("Input", AudioChannelSet::stereo(), true)
#endif
.withOutput ("Output", AudioChannelSet::stereo(), true)
#endif
),
treeState(*this, nullptr, "PARAMETERS", createParameterLayout())
#endif
{
//treeState.state = ValueTree("savedParameters");
//auto* audioProcessorEditor = createEditor();
}
GainSliderAudioProcessor::~GainSliderAudioProcessor()
{
}
//==============================================================================
AudioProcessorValueTreeState::ParameterLayout GainSliderAudioProcessor::createParameterLayout()
{
// method to stuff parameters into the treeState
std::vector<std::unique_ptr<RangedAudioParameter>> params;
auto delayParams = std::make_unique<AudioParameterFloat> (DELAY_ID, DELAY_NAME, NormalisableRange<float> (200.0f, 320.0f), 270.0f, DELAY_NAME, AudioProcessorParameter::genericParameter, [](float value, int){return String (value, 0);}, nullptr);
auto freqParams = std::make_unique<AudioParameterFloat> (FREQ_ID, FREQ_NAME, NormalisableRange<float> (400.0f, 1000.0f), 700.0f, FREQ_NAME, AudioProcessorParameter::genericParameter, [](float value, int){return String (value, 2);}, nullptr);
auto qParams = std::make_unique<AudioParameterFloat> (Q_ID, Q_NAME, NormalisableRange<float> (0.1f, 0.6f), 0.4f, Q_NAME,
AudioProcessorParameter::genericParameter, [](float value, int){return String (value, 2);}, nullptr);
auto sepParams = std::make_unique<AudioParameterFloat> (SEP_ID, SEP_NAME, NormalisableRange<float> (-6.0f, 0.0f), -4.0f, SEP_NAME,
AudioProcessorParameter::genericParameter, [](float value, int){return String (value, 1);}, nullptr);
auto dGainParams = std::make_unique<AudioParameterFloat> (DGAIN_ID, DGAIN_NAME, NormalisableRange<float> (-24.0f, 0.0f), 0.0f, DGAIN_NAME, AudioProcessorParameter::genericParameter, [](float value, int){return String (value, 1);}, nullptr);
auto xGainParams = std::make_unique<AudioParameterFloat> (XGAIN_ID, XGAIN_NAME, NormalisableRange<float> (-24.0f, 0.0f), 0.0f, XGAIN_NAME, AudioProcessorParameter::genericParameter, [](float value, int){return String (value, 1);}, nullptr);
auto activeParams = std::make_unique<AudioParameterBool> (ACTIVE_ID, ACTIVE_NAME, true);
auto typeParams = std::make_unique<AudioParameterChoice> (TYPE_ID, TYPE_NAME, StringArray {"Shelf", "Pass", "None"}, 0);
params.push_back(std::move(delayParams));
params.push_back(std::move(freqParams));
params.push_back(std::move(qParams));
params.push_back(std::move(sepParams));
params.push_back(std::move(dGainParams));
params.push_back(std::move(xGainParams));
params.push_back(std::move(activeParams));
params.push_back(std::move(typeParams));
return {params.begin(), params.end()};
}
//==============================================================================
const String GainSliderAudioProcessor::getName() const
{
return JucePlugin_Name;
}
bool GainSliderAudioProcessor::acceptsMidi() const
{
#if JucePlugin_WantsMidiInput
return true;
#else
return false;
#endif
}
bool GainSliderAudioProcessor::producesMidi() const
{
#if JucePlugin_ProducesMidiOutput
return true;
#else
return false;
#endif
}
bool GainSliderAudioProcessor::isMidiEffect() const
{
#if JucePlugin_IsMidiEffect
return true;
#else
return false;
#endif
}
double GainSliderAudioProcessor::getTailLengthSeconds() const
{
return 0.0;
}
int GainSliderAudioProcessor::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 GainSliderAudioProcessor::getCurrentProgram()
{
return 0;
}
void GainSliderAudioProcessor::setCurrentProgram (int index)
{
}
const String GainSliderAudioProcessor::getProgramName (int index)
{
return {};
}
void GainSliderAudioProcessor::changeProgramName (int index, const String& newName)
{
}
//==============================================================================
void GainSliderAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
// Use this method as the place to do any pre-playback
// initialisation that you need..
const int numInputChannels = getTotalNumInputChannels();
// Initialisation of the filter buffer.
// It has the same size as the buffer itself.
mFilterBuffer.setSize(numInputChannels, samplesPerBlock, false, true, true);
mFilterBuffer.clear();
// Initialisation of the delay buffer.
// this is for 1 second of delay buffer size.
// This is absolutely enough for our purpose, the psychoacoustic filter
// requiring usually between 200 to 300 microseconds.
const int delayBufferSize = sampleRate + samplesPerBlock;
mDelayBuffer.setSize(numInputChannels, delayBufferSize, false, true, true);
mDelayBuffer.clear();
mSampleRate = sampleRate;
// Initialisation of the spec for the process duplicators of the filters
dsp::ProcessSpec spec;
spec.sampleRate = sampleRate;
spec.maximumBlockSize = samplesPerBlock;
spec.numChannels = getTotalNumOutputChannels();
// Initialisation of the process duplicators
iirLowPassFilterDuplicator.reset();
iirHighPassFilterDuplicator.reset();
updateFilterParameters();
iirLowPassFilterDuplicator.prepare(spec);
iirHighPassFilterDuplicator.prepare(spec);
audioProcessorEditor->visualiser.clear();
// Debug
/*
DBG("delayBufferSize: " << delayBufferSize); //44228
DBG("samplesPerBlock: " << samplesPerBlock); //128
DBG("sampleRate: " << sampleRate); //44100
*/
}
void GainSliderAudioProcessor::releaseResources()
{
// When playback stops, you can use this as an opportunity to free up any
// spare memory, etc.
}
#ifndef JucePlugin_PreferredChannelConfigurations
bool GainSliderAudioProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const
{
#if JucePlugin_IsMidiEffect
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.
if (layouts.getMainOutputChannelSet() != AudioChannelSet::mono()
&& layouts.getMainOutputChannelSet() != 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 GainSliderAudioProcessor::updateFilterParameters ()
{
auto* sliderFreqValue = treeState.getRawParameterValue(FREQ_ID);
//auto* sliderQValue = treeState.getRawParameterValue(Q_ID);
auto* sliderSepValue = treeState.getRawParameterValue(SEP_ID);
auto* filterType = treeState.getRawParameterValue(TYPE_ID);
if (*filterType == 0)
{
*iirLowPassFilterDuplicator.state = *dsp::IIR::Coefficients<float>::makeLowShelf(mSampleRate, *sliderFreqValue, 1.1f, Decibels::decibelsToGain(-1.0f * *sliderSepValue));
*iirHighPassFilterDuplicator.state = *dsp::IIR::Coefficients<float>::makeLowShelf(mSampleRate, *sliderFreqValue, 1.1f, Decibels::decibelsToGain(*sliderSepValue));
}
else if (*filterType == 1)
{
// low pass and highpass
}
else if (*filterType == 2)
{
// No filtering
}
}
void GainSliderAudioProcessor::processBlock (AudioBuffer<float>& buffer, MidiBuffer& midiMessages)
{
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());
//mDelayBuffer.clear(i, 0, mDelayBuffer.getNumSamples());
}
auto* activeState = treeState.getRawParameterValue(ACTIVE_ID);
if (*activeState == true)
{
const int bufferLength = buffer.getNumSamples();
const int delayBufferLength = mDelayBuffer.getNumSamples();
// We copy the buffer into the filter buffer
for (int channel = 0; channel < totalNumInputChannels; ++channel)
{
const float* bufferChannelData = buffer.getReadPointer(channel);
mFilterBuffer.copyFrom(channel, 0, bufferChannelData, bufferLength);
}
// Do the filtering on the filter buffer for the Crossfeed signal
updateFilterParameters();
dsp::AudioBlock<float> block (mFilterBuffer);
iirLowPassFilterDuplicator.process(dsp::ProcessContextReplacing<float> (block));
// Adjust gain on the Filter buffer for separation (and for debug too)
for (int channel = 0; channel < totalNumInputChannels; ++channel)
{
auto* filteredChannelData = mFilterBuffer.getWritePointer (channel);
auto* sepGainValue = treeState.getRawParameterValue(SEP_ID);
auto* xGainValue = treeState.getRawParameterValue(XGAIN_ID);
for (int sample = 0; sample < mFilterBuffer.getNumSamples(); ++sample)
{
filteredChannelData[sample] = mFilterBuffer.getSample(channel, sample) * Decibels::decibelsToGain(2.0 * *sepGainValue) * Decibels::decibelsToGain(*xGainValue);
}
}
// We copy the filter buffer into the delay Buffer
for (int channel = 0; channel < totalNumInputChannels; ++channel)
{
// Here we cross the channels with getTotalNumInputChannels()-1-channel 0 -> 1, 1 -> 0!
const float* filterChannelData = mFilterBuffer.getReadPointer(getTotalNumInputChannels()-1-channel);
fillDelayBuffer(channel, bufferLength, delayBufferLength, filterChannelData);
}
// Do the filtering on the main buffer for the direct signal
updateFilterParameters();
dsp::AudioBlock<float> block2 (buffer);
iirHighPassFilterDuplicator.process(dsp::ProcessContextReplacing<float> (block2));
// Gain adjustment to the main signal (To be removed in the end? Useful to debug)
for (int channel = 0; channel < totalNumInputChannels; ++channel)
{
auto* DirectChannelData = buffer.getWritePointer (channel);
auto* dGainValue = treeState.getRawParameterValue(DGAIN_ID);
for (int sample = 0; sample < buffer.getNumSamples(); ++sample)
{
DirectChannelData[sample] = buffer.getSample(channel, sample) * Decibels::decibelsToGain(*dGainValue);
}
}
// We add the delayBuffer to the main buffer
for (int channel = 0; channel < totalNumInputChannels; ++channel)
{
const float* delayChannelData = mDelayBuffer.getReadPointer(channel);
auto sliderDelayValue = treeState.getRawParameterValue(DELAY_ID);
getFromDelayBuffer(*sliderDelayValue, buffer, channel, bufferLength, delayBufferLength, delayChannelData);
}
//Update write position
mWritePosition += buffer.getNumSamples();
mWritePosition %= mDelayBuffer.getNumSamples();
}
audioProcessorEditor->visualiser.pushBuffer(buffer);
}
//==============================================================================
//==============================================================================
void GainSliderAudioProcessor::fillDelayBuffer(int channel, const int bufferLength, const int delayBufferLength, const float* bufferData)
{
const int bufferRemaining = delayBufferLength - mWritePosition;
//Copy data from main buffer to delay buffer
if (bufferLength <= bufferRemaining)
{
mDelayBuffer.copyFromWithRamp(channel, mWritePosition, bufferData, bufferLength, 1.0, 1.0);
}
else
{
mDelayBuffer.copyFromWithRamp(channel, mWritePosition, bufferData, bufferRemaining, 1.0, 1.0);
mDelayBuffer.copyFromWithRamp(channel, 0, bufferData, bufferLength - bufferRemaining, 1.0, 1.0);
}
}
//==============================================================================
void GainSliderAudioProcessor::getFromDelayBuffer(float delayTimeValue, AudioBuffer<float>& buffer, int channel, const int bufferLength, const int delayBufferLength, const float* delayBufferData)
{
// delay time is in microseconds!
int delaySamples = static_cast<int>(round(mSampleRate * delayTimeValue/1000000.0f));
const int readPosition = static_cast<int>(( delayBufferLength + mWritePosition - delaySamples )) % delayBufferLength;
const int bufferRemaining = delayBufferLength - readPosition;
if (bufferLength <= bufferRemaining)
{
buffer.addFrom(channel, 0, delayBufferData + readPosition, bufferLength);
}
else
{
buffer.addFrom(channel, 0, delayBufferData + readPosition, bufferRemaining);
buffer.addFrom(channel, bufferRemaining, delayBufferData, bufferLength - bufferRemaining);
}
}
//==============================================================================
bool GainSliderAudioProcessor::hasEditor() const
{
return true; // (change this to false if you choose to not supply an editor)
}
AudioProcessorEditor* GainSliderAudioProcessor::createEditor()
{
return new GainSliderAudioProcessorEditor (*this);
}
//==============================================================================
void GainSliderAudioProcessor::getStateInformation (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.
/*
ScopedPointer<XmlElement> xml (treeState.state.createXml());
copyXmlToBinary(*xml, destData);
*/
}
void GainSliderAudioProcessor::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.
/*
ScopedPointer<XmlElement> loadedParameters (getXmlFromBinary(data, sizeInBytes));
if (loadedParameters != nullptr)
{
if (loadedParameters -> hasTagName(treeState.state.getType()))
{
treeState.state = ValueTree::fromXml(*loadedParameters);
}
}
*/
}
//==============================================================================
// This creates new instances of the plugin..
AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
return new GainSliderAudioProcessor();
}