-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginProcessor.cpp
365 lines (325 loc) · 13.9 KB
/
PluginProcessor.cpp
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
/*
==============================================================================
This file contains the basic framework code for a JUCE plugin processor.
==============================================================================
*/
#include "PluginProcessor.h"
#include "PluginEditor.h"
juce::AudioProcessorValueTreeState::ParameterLayout createLayout(int numOperators)
{
juce::AudioProcessorValueTreeState::ParameterLayout layout;
for(int i = 0; i < numOperators; ++i)
{
juce::String iStr = juce::String(i);
//strings and parameters for the operator
auto ratioId = "ratioParam" + iStr;
auto ratioName = "Operator " + iStr + " ratio";
auto levelId = "levelParam" + iStr;
auto levelName = "Operator " + iStr + " level";
auto indexId = "indexParam" + iStr;
auto indexName = "Operator " + iStr + " Mod Index";
auto outputId = "audibleParam" + iStr;
auto outputName = "Operator " + iStr + " audible";
auto panId = "panParam" + iStr;
auto panName = "Operator " + iStr + " pan";
juce::NormalisableRange<float> ratioRange(0.0f, 10.0f, 0.01f, 0.5f);
ratioRange.setSkewForCentre(1.0f);
layout.add(std::make_unique<juce::AudioParameterFloat>(ratioId, ratioName, ratioRange, 1.0f));
layout.add(std::make_unique<juce::AudioParameterFloat>(levelId, levelName, 0.0f, 1.0f, 1.0f));
layout.add(std::make_unique<juce::AudioParameterFloat>(indexId, indexName, 0.0f, 500.0f, 0.0f));
layout.add(std::make_unique<juce::AudioParameterFloat>(panId, panName, -1.0f, 1.0f, 0.0f));
layout.add(std::make_unique<juce::AudioParameterBool>(outputId, outputName, false));
//and for the envelope
auto delayId = "delayParam" + iStr;
auto delayName = "Operator " + iStr + " delay";
auto attackId = "attackParam" + iStr;
auto attackName = "Operator " + iStr + " attack";
auto holdId = "holdParam" + iStr;
auto holdName = "Operator " + iStr + " hold";
auto decayId = "decayParam" + iStr;
auto decayName = "Operator " + iStr + " decay";
auto sustainId = "sustainParam" + iStr;
auto sustainName = "Operator " + iStr + " sustain";
auto releaseId = "releaseParam" + iStr;
auto releaseName = "Operator " + iStr + " release";
juce::NormalisableRange<float> timeRange1(1.0f, 20000.0f, 0.5f, 0.5f);
timeRange1.setSkewForCentre(1000.0f);
juce::NormalisableRange<float> timeRange2(1.0f, 20000.0f, 0.5f, 0.5f);
timeRange2.setSkewForCentre(5000.0f);
layout.add(std::make_unique<juce::AudioParameterFloat>(delayId, delayName, timeRange2, 0.0f));
layout.add(std::make_unique<juce::AudioParameterFloat>(attackId, attackName, timeRange1, 20.0f));
layout.add(std::make_unique<juce::AudioParameterFloat>(holdId, holdName, timeRange1, 0.0f));
layout.add(std::make_unique<juce::AudioParameterFloat>(decayId, decayName, timeRange2, 100.0f));
layout.add(std::make_unique<juce::AudioParameterFloat>(sustainId, sustainName, 0.0f, 1.0f , 0.6f));
layout.add(std::make_unique<juce::AudioParameterFloat>(releaseId, releaseName, timeRange2, 40.0f));
//operator wave types
juce::StringArray waveTypes;
waveTypes.add("Sine");
waveTypes.add("Square");
waveTypes.add("Saw");
waveTypes.add("Triangle");
auto waveId = "waveParam" + iStr;
auto waveName = "Operator " + iStr + " wave";
layout.add(std::make_unique<juce::AudioParameterChoice>(waveId, waveName, waveTypes, 0));
//bools for modulation paths
for(int n = 0; n < numOperators; ++n)
{
juce::String nStr = juce::String(n);
auto modId = iStr + "to" + nStr + "Param";
auto modName = "Operator " + iStr + " to " + nStr;
layout.add(std::make_unique<juce::AudioParameterBool>(modId, modName, false));
}
}
for(int i = 0; i < 4; ++i)
{
auto iStr = juce::String(i);
auto rateId = "lfoRateParam" + iStr;
auto rateName = "LFO " + iStr + " Rate";
auto levelId = "lfoLevelParam" + iStr;
auto levelName = "LFO " + iStr + " Level";
auto waveId = "lfoWaveParam" + iStr;
auto waveName = "LFO " + iStr + " Waveform";
auto targetId = "lfoTargetParam" + iStr;
auto targetName = "LFO " + iStr + " Target";
auto modeId = "lfoRatioModeParam" + iStr;
auto modeName = "LFO " + iStr + "ratio modulation mode";
layout.add(std::make_unique<juce::AudioParameterFloat>(rateId, rateName, 0.0f, 20.0f , 1.0f));
layout.add(std::make_unique<juce::AudioParameterFloat>(levelId, levelName, 0.0f, 1.0f , 1.0f));
juce::StringArray targets;
targets.add("No target");
for(int n = 0; n < numOperators; ++n)
{
auto nStr = juce::String(n + 1);
auto opLevel = "Operator " + nStr + " level";
auto opRatio = "Operator " + nStr + "level";
targets.add(opRatio);
targets.add(opLevel);
}
juce::StringArray waveTypes;
waveTypes.add("Sine");
waveTypes.add("Triangle");
waveTypes.add("Square");
waveTypes.add("Saw");
waveTypes.add("Random");
juce::StringArray ratioModes;
ratioModes.add("Up");
ratioModes.add("Both");
ratioModes.add("Down");
layout.add(std::make_unique<juce::AudioParameterChoice>(modeId, modeName, ratioModes, 0));
layout.add(std::make_unique<juce::AudioParameterChoice>(targetId, targetName, targets, 0));
layout.add(std::make_unique<juce::AudioParameterChoice>(waveId, waveName, waveTypes, 0));
}
auto cutoffId = "cutoffParam";
auto cutoffName = "Filter Cutoff";
auto resonanceId = "resonanceParam";
auto resonanceName = "Filter Resonance";
auto typeId = "filterTypeParam";
auto typeName = "Filter Type";
auto toggleId = "filterToggleParam";
auto toggleName = "Bypass Filter";
juce::NormalisableRange<float> cutoffRange(1.0f, 15000.0f, 0.5f, 0.5f);
cutoffRange.setSkewForCentre(700.0f);
juce::NormalisableRange<float> resonanceRange(0.0f, 20.0f, 0.01f, 0.01f);
resonanceRange.setSkewForCentre(2.0f);
layout.add(std::make_unique<juce::AudioParameterFloat>(cutoffId, cutoffName, cutoffRange, 2500.0f));
layout.add(std::make_unique<juce::AudioParameterFloat>(resonanceId, resonanceName, resonanceRange, 1.0f));
layout.add(std::make_unique<juce::AudioParameterBool>(typeId, typeName, false));
layout.add(std::make_unique<juce::AudioParameterBool>(toggleId, toggleName, false));
return layout;
}
//==============================================================================
HexFmAudioProcessor::HexFmAudioProcessor()
#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
),
tree(*this, nullptr, "synthParams", createLayout(numOperators)),
synth(6, 4, 6, &tree),
fixedFilter(juce::dsp::IIR::Coefficients<float>::makeHighShelf(44100.0f, 44100.0f * 0.35f, 1.0f, 0.00001f)),
synthFilter(juce::dsp::IIR::Coefficients<float>::makeLowPass(44100.0f, 2500.0f, 1.0f))
#endif
{
for(int i = 0; i < 4; ++i)
{
auto iStr = juce::String(i);
lfoRateIds.push_back("lfoRateParam" + iStr);
lfoWaveIds.push_back("lfoWaveParam" + iStr);
lfoTargetIds.push_back("lfoTargetParam" + iStr);
lfoLevelIds.push_back("lfoLevelParam" + iStr);
lfoRatioModeIds.push_back("lfoRatioModeParam" + iStr);
}
for(int i = 0; i < numOperators; ++i)
{
auto iStr = juce::String(i);
ratioIds.push_back("ratioParam" + iStr);
levelIds.push_back("levelParam" + iStr);
modIndexIds.push_back("indexParam" + iStr);
audibleIds.push_back("audibleParam" + iStr);
panIds.push_back("panParam" + iStr);
waveIds.push_back("waveParam" + iStr);
delayIds.push_back("delayParam" + iStr);
attackIds.push_back("attackParam" + iStr);
holdIds.push_back("holdParam" + iStr);
decayIds.push_back("decayParam" + iStr);
sustainIds.push_back("sustainParam" + iStr);
releaseIds.push_back("releaseParam" + iStr);
std::vector<juce::String> routing;
for(int n = 0; n < numOperators; ++n)
{
auto nStr = juce::String(n);
routing.push_back(iStr + "to" + nStr + "Param");
}
routingIds.push_back(routing);
}
}
//======================================================================
HexFmAudioProcessor::~HexFmAudioProcessor()
{
}
//==============================================================================
const juce::String HexFmAudioProcessor::getName() const
{
return JucePlugin_Name;
}
bool HexFmAudioProcessor::acceptsMidi() const
{
#if JucePlugin_WantsMidiInput
return true;
#else
return false;
#endif
}
bool HexFmAudioProcessor::producesMidi() const
{
#if JucePlugin_ProducesMidiOutput
return true;
#else
return false;
#endif
}
bool HexFmAudioProcessor::isMidiEffect() const
{
#if JucePlugin_IsMidiEffect
return true;
#else
return false;
#endif
}
double HexFmAudioProcessor::getTailLengthSeconds() const
{
return 0.0;
}
int HexFmAudioProcessor::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 HexFmAudioProcessor::getCurrentProgram()
{
return 0;
}
void HexFmAudioProcessor::setCurrentProgram (int index)
{
}
const juce::String HexFmAudioProcessor::getProgramName (int index)
{
return {};
}
void HexFmAudioProcessor::changeProgramName (int index, const juce::String& newName)
{
}
//==============================================================================
void HexFmAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
juce::ignoreUnused(samplesPerBlock);
lastSampleRate = sampleRate;
synth.setCurrentPlaybackSampleRate(sampleRate);
maxiSettings::setup((int)sampleRate, 2, samplesPerBlock);
for(juce::SynthesiserVoice* i : *synth.voiceArray())
{
auto* voice = dynamic_cast<FmVoice*>(i);
voice->setSampleRate(sampleRate);
}
juce::dsp::ProcessSpec spec;
spec.sampleRate = sampleRate;
spec.maximumBlockSize = samplesPerBlock;
spec.numChannels = getTotalNumOutputChannels();
fixedFilter.prepare(spec);
fixedFilter.reset();
synthFilter.prepare(spec);
synthFilter.reset();
}
void HexFmAudioProcessor::releaseResources()
{
// When playback stops, you can use this as an opportunity to free up any
// spare memory, etc.
}
#ifndef JucePlugin_PreferredChannelConfigurations
bool HexFmAudioProcessor::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.
if (layouts.getMainOutputChannelSet() != juce::AudioChannelSet::mono()
&& 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 HexFmAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{
buffer.clear();
synth.updateParams();
synth.renderNextBlock(buffer, midiMessages, 0, buffer.getNumSamples());
juce::dsp::AudioBlock<float> block(buffer);
updateFilter();
if(isFilterOn)
{
synthFilter.process(juce::dsp::ProcessContextReplacing<float>(block));
}
fixedFilter.process(juce::dsp::ProcessContextReplacing<float>(block));
}
//==============================================================================
bool HexFmAudioProcessor::hasEditor() const
{
return true; // (change this to false if you choose to not supply an editor)
}
juce::AudioProcessorEditor* HexFmAudioProcessor::createEditor()
{
return new HexFmAudioProcessorEditor (*this);
}
//==============================================================================
void HexFmAudioProcessor::getStateInformation (juce::MemoryBlock& destData)
{
auto state = tree.copyState();
std::unique_ptr<juce::XmlElement> xml (state.createXml());
copyXmlToBinary(*xml, destData);
}
void HexFmAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
{
std::unique_ptr<juce::XmlElement> xmlState (getXmlFromBinary (data, sizeInBytes));
if (xmlState.get() != nullptr)
if (xmlState->hasTagName (tree.state.getType()))
tree.replaceState (juce::ValueTree::fromXml (*xmlState));
}
//==============================================================================
//==============================================================================
// This creates new instances of the plugin..
juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
return new HexFmAudioProcessor();
}