-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_processor.hpp
244 lines (214 loc) · 6.52 KB
/
plugin_processor.hpp
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
#pragma once
#include <juce_analytics/juce_analytics.h>
#include <juce_audio_basics/juce_audio_basics.h>
#include <juce_audio_devices/juce_audio_devices.h>
#include <juce_audio_formats/juce_audio_formats.h>
#include <juce_audio_plugin_client/juce_audio_plugin_client.h>
#include <juce_audio_processors/juce_audio_processors.h>
#include <juce_audio_utils/juce_audio_utils.h>
#include <juce_core/juce_core.h>
#include <juce_data_structures/juce_data_structures.h>
#include <juce_dsp/juce_dsp.h>
#include <juce_events/juce_events.h>
#include <juce_graphics/juce_graphics.h>
#include <juce_gui_basics/juce_gui_basics.h>
#include <juce_gui_extra/juce_gui_extra.h>
#include <juce_audio_processors/juce_audio_processors.h>
// #include "custom_editor.hpp"
using Filter = juce::dsp::IIR::Filter<float>;
using Coefficients = Filter::CoefficientsPtr;
using CutFilter = juce::dsp::ProcessorChain<Filter, Filter, Filter, Filter>;
using MonoChain = juce::dsp::ProcessorChain<CutFilter, Filter, CutFilter>;
enum ChainPositions
{
LowCut,
Peak,
HighCut
};
enum Channel
{
Left, // i.e. 0
Right // i.e.1
};
template <typename T>
struct Fifo
{
void prepare(int numChannels, int numSamples)
{
static_assert(std::is_same_v<T, juce::AudioBuffer<float>>,
"prepare(numChannels, numSamples) should only be used when the Fifo is holding juce::AudioBuffer<float>");
for (auto &buffer : buffers)
{
buffer.setSize(numChannels,
numSamples,
false, // clear everything?
true, // including the extra space?
true); // avoid reallocating if you can?
buffer.clear();
}
}
void prepare(size_t numElements)
{
static_assert(std::is_same_v<T, std::vector<float>>,
"prepare(numElements) should only be used when the Fifo is holding std::vector<float>");
for (auto &buffer : buffers)
{
buffer.clear();
buffer.resize(numElements, 0);
}
}
bool push(const T &t)
{
auto write = fifo.write(1);
if (write.blockSize1 > 0)
{
buffers[write.startIndex1] = t;
return true;
}
return false;
}
bool pull(T &t)
{
auto read = fifo.read(1);
if (read.blockSize1 > 0)
{
t = buffers[read.startIndex1];
return true;
}
return false;
}
int getNumAvailableForReading() const
{
return fifo.getNumReady();
}
private:
static constexpr int Capacity = 30;
std::array<T, Capacity> buffers;
juce::AbstractFifo fifo{Capacity};
};
template <typename BlockType>
struct SingleChannelSampleFifo
{
SingleChannelSampleFifo(Channel ch) : channelToUse(ch)
{
prepared.set(false);
}
void update(const BlockType &buffer)
{
jassert(prepared.get());
jassert(buffer.getNumChannels() > channelToUse);
auto *channelPtr = buffer.getReadPointer(channelToUse);
for (int i = 0; i < buffer.getNumSamples(); ++i)
{
pushNextSampleIntoFifo(channelPtr[i]);
}
}
void prepare(int bufferSize)
{
prepared.set(false);
size.set(bufferSize);
bufferToFill.setSize(1, // channel
bufferSize, // num samples
false, // keepExistingContent
true, // clear extra space
true); // avoid reallocating
audioBufferFifo.prepare(1, bufferSize);
fifoIndex = 0;
prepared.set(true);
}
//==============================================================================
int getNumCompleteBuffersAvailable() const
{
return audioBufferFifo.getNumAvailableForReading();
}
bool isPrepared() const
{
return prepared.get();
}
int getSize() const
{
return size.get();
}
//==============================================================================
bool getAudioBuffer(BlockType &buf)
{
return audioBufferFifo.pull(buf);
}
private:
Channel channelToUse;
int fifoIndex = 0;
Fifo<BlockType> audioBufferFifo;
BlockType bufferToFill;
juce::Atomic<bool> prepared = false;
juce::Atomic<int> size = 0;
void pushNextSampleIntoFifo(float sample)
{
if (fifoIndex == bufferToFill.getNumSamples())
{
auto ok = audioBufferFifo.push(bufferToFill);
juce::ignoreUnused(ok);
fifoIndex = 0;
}
bufferToFill.setSample(0, fifoIndex, sample);
++fifoIndex;
}
};
enum Slope
{
Slope_12,
Slope_24,
Slope_36,
Slope_48
};
struct ChainSettings
{
float lowCutFreq{0}, highCutFreq{0};
Slope lowCutSlope{Slope::Slope_12}, highCutSlope{Slope::Slope_12};
bool lowCutBypassed{false}, highCutBypassed{false};
};
void updateCoefficients(Coefficients &old, const Coefficients &replacements)
{
*old = *replacements;
};
template <int Index, typename ChainType, typename CoefficientType>
void update(ChainType &chain, const CoefficientType &coefficients)
{
updateCoefficients(chain.template get<Index>().coefficients, coefficients[Index]);
chain.template setBypassed<Index>(false);
}
template <typename ChainType, typename CoefficientType>
void updateCutFilter(ChainType &leftLowCut, const CoefficientType &cutCoefficients, const Slope &lowCutSlope)
{
// activate
leftLowCut.template setBypassed<0>(true);
leftLowCut.template setBypassed<1>(true);
leftLowCut.template setBypassed<2>(true);
leftLowCut.template setBypassed<3>(true);
switch (lowCutSlope)
{
case Slope_48:
{
update<3>(leftLowCut, cutCoefficients);
}
case Slope_36:
{
update<2>(leftLowCut, cutCoefficients);
}
case Slope_24:
{
update<1>(leftLowCut, cutCoefficients);
}
case Slope_12:
{
update<0>(leftLowCut, cutCoefficients);
}
}
}
auto makeLowCutFilter(const ChainSettings &chainSettings, double sampleRate)
{
return juce::dsp::FilterDesign<float>::designIIRHighpassHighOrderButterworthMethod(chainSettings.lowCutFreq, sampleRate, 2 * (chainSettings.lowCutSlope + 1));
}
auto makeHighCutFilter(const ChainSettings &chainSettings, double sampleRate)
{
return juce::dsp::FilterDesign<float>::designIIRLowpassHighOrderButterworthMethod(chainSettings.highCutFreq, sampleRate, 2 * (chainSettings.highCutSlope + 1));
}