-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginProcessor.cpp
More file actions
179 lines (139 loc) · 5.98 KB
/
Copy pathPluginProcessor.cpp
File metadata and controls
179 lines (139 loc) · 5.98 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
#include "PluginProcessor.h"
#include "PluginEditor.h"
FlatLineAudioProcessor::FlatLineAudioProcessor()
#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
),
apvts (*this, nullptr, "PARAMETERS", createParameterLayout())
#endif
{
}
juce::AudioProcessorValueTreeState::ParameterLayout FlatLineAudioProcessor::createParameterLayout()
{
std::vector<std::unique_ptr<juce::RangedAudioParameter>> params;
params.push_back (std::make_unique<juce::AudioParameterFloat>(
juce::ParameterID { "input", 1 }, "Input",
juce::NormalisableRange<float> (0.0f, 4.0f, 0.01f), 1.0f));
auto driveRange = juce::NormalisableRange<float> (0.000001f, 1.0f, 0.000001f);
driveRange.setSkewForCentre (0.001f); // exponential-style feel, matching the JSFX :log intent
params.push_back (std::make_unique<juce::AudioParameterFloat>(
juce::ParameterID { "drive", 1 }, "Drive", driveRange, 0.001f));
params.push_back (std::make_unique<juce::AudioParameterFloat>(
juce::ParameterID { "lookahead", 1 }, "Lookahead",
juce::NormalisableRange<float> (0.0f, 50.0f, 0.1f), 5.0f));
params.push_back (std::make_unique<juce::AudioParameterFloat>(
juce::ParameterID { "output", 1 }, "Output",
juce::NormalisableRange<float> (0.0f, 4.0f, 0.01f), 1.0f));
params.push_back (std::make_unique<juce::AudioParameterBool>(
juce::ParameterID { "delta", 1 }, "Delta", false));
return { params.begin(), params.end() };
}
void FlatLineAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
currentSampleRate = sampleRate;
maxLookaheadSamples = juce::jmax (1, (int) std::ceil (sampleRate * 0.050) + samplesPerBlock + 8);
const int channels = juce::jmax (2, getTotalNumOutputChannels());
lookaheadBuffer.setSize (channels, maxLookaheadSamples);
lookaheadBuffer.clear();
writePos = 0;
a = 1.0f;
b = 1.0f;
}
void FlatLineAudioProcessor::releaseResources()
{
}
#ifndef JucePlugin_PreferredChannelConfigurations
bool FlatLineAudioProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const
{
const auto& mainOut = layouts.getMainOutputChannelSet();
if (mainOut != juce::AudioChannelSet::mono()
&& mainOut != juce::AudioChannelSet::stereo())
return false;
#if ! JucePlugin_IsSynth
if (mainOut != layouts.getMainInputChannelSet())
return false;
#endif
return true;
}
#endif
void FlatLineAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{
juce::ignoreUnused (midiMessages);
juce::ScopedNoDenormals noDenormals;
const int numSamples = buffer.getNumSamples();
const int numChannels = buffer.getNumChannels();
for (int ch = getTotalNumInputChannels(); ch < getTotalNumOutputChannels(); ++ch)
buffer.clear (ch, 0, numSamples);
const float input = apvts.getRawParameterValue ("input")->load();
const float drive = apvts.getRawParameterValue ("drive")->load();
const float lookMs = apvts.getRawParameterValue ("lookahead")->load();
const float output = apvts.getRawParameterValue ("output")->load();
const bool deltaMode = apvts.getRawParameterValue ("delta")->load() > 0.5f;
int lookSamples = (int) std::floor (currentSampleRate * lookMs / 1000.0);
lookSamples = juce::jlimit (0, maxLookaheadSamples - 2, lookSamples);
auto* left = buffer.getWritePointer (0);
auto* right = numChannels > 1 ? buffer.getWritePointer (1) : nullptr;
for (int i = 0; i < numSamples; ++i)
{
const float inL = left[i] * input;
const float inR = right != nullptr ? right[i] * input : inL;
lookaheadBuffer.setSample (0, writePos, inL);
if (lookaheadBuffer.getNumChannels() > 1)
lookaheadBuffer.setSample (1, writePos, inR);
// JSFX detector: abs(((l/2) + (r/2))^2)
const float mid = 0.5f * inL + 0.5f * inR;
const float det = std::abs (mid * mid);
a = juce::jmax (a, eps);
b = juce::jmax (b, eps);
// Your A/B core, ported directly from the ReaJS version.
a = 0.99f * b
+ det * drive / a
+ 0.01f * (a - b);
a = juce::jmax (a, eps);
b = juce::jmax (b, eps);
b = 0.99f * juce::jmax (a, juce::jmax (b / a, b))
+ 0.01f * b * std::abs (a / b);
a = juce::jmax (a, eps);
b = juce::jmax (b, eps);
int readPos = writePos - lookSamples;
if (readPos < 0)
readPos += maxLookaheadSamples;
const float dryL = lookaheadBuffer.getSample (0, readPos);
const float dryR = lookaheadBuffer.getNumChannels() > 1 ? lookaheadBuffer.getSample (1, readPos) : dryL;
const float procL = dryL / b;
const float procR = dryR / b;
const float outL = deltaMode ? (dryL - procL) : procL;
const float outR = deltaMode ? (dryR - procR) : procR;
left[i] = outL * output;
if (right != nullptr)
right[i] = outR * output;
++writePos;
if (writePos >= maxLookaheadSamples)
writePos = 0;
}
}
juce::AudioProcessorEditor* FlatLineAudioProcessor::createEditor()
{
return new FlatLineAudioProcessorEditor (*this);
}
void FlatLineAudioProcessor::getStateInformation (juce::MemoryBlock& destData)
{
if (auto xml = apvts.copyState().createXml())
copyXmlToBinary (*xml, destData);
}
void FlatLineAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
{
if (auto xmlState = getXmlFromBinary (data, sizeInBytes))
if (xmlState->hasTagName (apvts.state.getType()))
apvts.replaceState (juce::ValueTree::fromXml (*xmlState));
}
juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
return new FlatLineAudioProcessor();
}