-
Notifications
You must be signed in to change notification settings - Fork 27
/
PluginProcessor.cpp
328 lines (280 loc) · 12.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
/*
==============================================================================
This file was auto-generated!
It contains the basic framework code for a JUCE plugin processor.
==============================================================================
*/
#include "PluginProcessor.h"
#include "PluginEditor.h"
#include "MidiConstants.h"
#include "Util.h"
#include "GuiConstants.h"
using namespace std;
using Parameter = AudioProcessorValueTreeState::Parameter;
AudioProcessor* JUCE_CALLTYPE createPluginFilter();
//==============================================================================
JuicySFAudioProcessor::JuicySFAudioProcessor()
: AudioProcessor{getBusesProperties()}
, valueTreeState{
*this,
nullptr,
"MYPLUGINSETTINGS",
createParameterLayout()}
, fluidSynthModel{valueTreeState}
{
MemoryBlock bookmarkBuffer;
valueTreeState.state.appendChild({ "uiState", {
{ "width", GuiConstants::minWidth },
{ "height", GuiConstants::minHeight }
}, {} }, nullptr);
valueTreeState.state.appendChild({ "soundFont", {
{ "path", "" },
{ "bookmark", std::move(bookmarkBuffer) },
}, {} }, nullptr);
// no properties, no subtrees (yet)
valueTreeState.state.appendChild({ "banks", {}, {} }, nullptr);
initialiseSynth();
}
AudioProcessorValueTreeState::ParameterLayout JuicySFAudioProcessor::createParameterLayout() {
// https://stackoverflow.com/a/8469002/5257399
unique_ptr<AudioParameterInt> params[] {
// SoundFont 2.4 spec section 7.2: zero through 127, or 128.
make_unique<AudioParameterInt>("bank", "which bank is selected in the soundfont", MidiConstants::midiMinValue, 128, MidiConstants::midiMinValue, "Bank" ),
// note: banks may be sparse, and lack a 0th preset. so defend against this.
make_unique<AudioParameterInt>("preset", "which patch (aka patch, program, instrument) is selected in the soundfont", MidiConstants::midiMinValue, MidiConstants::midiMaxValue, MidiConstants::midiMinValue, "Preset" ),
make_unique<AudioParameterInt>("attack", "volume envelope attack time", MidiConstants::midiMinValue, MidiConstants::midiMaxValue, MidiConstants::midiMinValue, "A" ),
make_unique<AudioParameterInt>("decay", "volume envelope sustain attentuation", MidiConstants::midiMinValue, MidiConstants::midiMaxValue, MidiConstants::midiMinValue, "D" ),
make_unique<AudioParameterInt>("sustain", "volume envelope decay time", MidiConstants::midiMinValue, MidiConstants::midiMaxValue, MidiConstants::midiMinValue, "S" ),
make_unique<AudioParameterInt>("release", "volume envelope release time", MidiConstants::midiMinValue, MidiConstants::midiMaxValue, MidiConstants::midiMinValue, "R" ),
make_unique<AudioParameterInt>("filterCutOff", "low-pass filter cut-off frequency", MidiConstants::midiMinValue, MidiConstants::midiMaxValue, MidiConstants::midiMinValue, "Cut" ),
make_unique<AudioParameterInt>("filterResonance", "low-pass filter resonance attentuation", MidiConstants::midiMinValue, MidiConstants::midiMaxValue, MidiConstants::midiMinValue, "Res" ),
};
return {
make_move_iterator(begin(params)),
make_move_iterator(end(params))
};
}
JuicySFAudioProcessor::~JuicySFAudioProcessor()
{
}
void JuicySFAudioProcessor::initialiseSynth() {
fluidSynthModel.initialise();
}
//==============================================================================
const String JuicySFAudioProcessor::getName() const
{
return JucePlugin_Name;
}
bool JuicySFAudioProcessor::acceptsMidi() const
{
#if JucePlugin_WantsMidiInput
return true;
#else
return false;
#endif
}
bool JuicySFAudioProcessor::producesMidi() const
{
#if JucePlugin_ProducesMidiOutput
return true;
#else
return false;
#endif
}
double JuicySFAudioProcessor::getTailLengthSeconds() const
{
return 0.0;
}
int JuicySFAudioProcessor::getNumPrograms()
{
return fluidSynthModel.getNumPrograms(); // 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 JuicySFAudioProcessor::getCurrentProgram()
{
return fluidSynthModel.getCurrentProgram();
}
void JuicySFAudioProcessor::setCurrentProgram(int index)
{
fluidSynthModel.setCurrentProgram(index);
}
const String JuicySFAudioProcessor::getProgramName(int index)
{
return fluidSynthModel.getProgramName(index);
}
void JuicySFAudioProcessor::changeProgramName (int index, const String& newName)
{
}
//==============================================================================
void JuicySFAudioProcessor::prepareToPlay (double sampleRate, int /*samplesPerBlock*/)
{
// Use this method as the place to do any pre-playback
// initialisation that you need..
synth.setCurrentPlaybackSampleRate (sampleRate);
keyboardState.reset();
fluidSynthModel.setSampleRate(static_cast<float>(sampleRate));
reset();
}
void JuicySFAudioProcessor::releaseResources()
{
// When playback stops, you can use this as an opportunity to free up any
// spare memory, etc.
keyboardState.reset();
}
bool JuicySFAudioProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const
{
// Only mono/stereo and input/output must have same layout
const AudioChannelSet& mainOutput = layouts.getMainOutputChannelSet();
const AudioChannelSet& mainInput = layouts.getMainInputChannelSet();
// input and output layout must either be the same or the input must be disabled altogether
if (! mainInput.isDisabled() && mainInput != mainOutput)
return false;
// do not allow disabling the main buses
if (mainOutput.isDisabled())
return false;
// only allow stereo and mono
return mainOutput.size() <= 2;
}
AudioProcessor::BusesProperties JuicySFAudioProcessor::getBusesProperties() {
return BusesProperties()
.withOutput ("Output", AudioChannelSet::stereo(), true);
}
void JuicySFAudioProcessor::processBlock(AudioBuffer<float>& buffer, MidiBuffer& midiMessages) {
jassert (!isUsingDoublePrecision());
// 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 (int i = getTotalNumInputChannels(); i < getTotalNumOutputChannels(); ++i)
buffer.clear (i, 0, buffer.getNumSamples());
// Now pass any incoming midi messages to our keyboard state object, and let it
// add messages to the buffer if the user is clicking on the on-screen keys
keyboardState.processNextMidiBuffer(midiMessages, 0, buffer.getNumSamples(), true);
fluidSynthModel.processBlock(buffer, midiMessages);
// and now get our synth to process these midi events and generate its output.
// synth.renderNextBlock(buffer, midiMessages, 0, numSamples);
// (see juce_VST3_Wrapper.cpp for the assertion this would trip otherwise)
// we are !JucePlugin_ProducesMidiOutput, so clear remaining MIDI messages from our buffer
midiMessages.clear();
}
//==============================================================================
bool JuicySFAudioProcessor::hasEditor() const
{
return true; // (change this to false if you choose to not supply an editor)
}
AudioProcessorEditor* JuicySFAudioProcessor::createEditor()
{
// grab a raw pointer to it for our own use
return /*pluginEditor = */new JuicySFAudioProcessorEditor (*this, valueTreeState);
}
//==============================================================================
void JuicySFAudioProcessor::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.
// Create an outer XML element..
XmlElement xml{"MYPLUGINSETTINGS"};
// Store the values of all our parameters, using their param ID as the XML attribute
XmlElement* params{xml.createNewChildElement("params")};
for (auto* param : getParameters()) {
if (auto* p = dynamic_cast<AudioProcessorParameterWithID*> (param)) {
params->setAttribute(p->paramID, p->getValue());
}
}
{
ValueTree tree{valueTreeState.state.getChildWithName("uiState")};
XmlElement* newElement{xml.createNewChildElement("uiState")};
{
double value{tree.getProperty("width", GuiConstants::minWidth)};
newElement->setAttribute("width", value);
}
{
double value{tree.getProperty("height", GuiConstants::minHeight)};
newElement->setAttribute("height", value);
}
}
{
ValueTree tree{valueTreeState.state.getChildWithName("soundFont")};
XmlElement* newElement{xml.createNewChildElement("soundFont")};
{
String value = tree.getProperty("path", "");
newElement->setAttribute("path", value);
}
{
MemoryBlock buffer;
var value = tree.getProperty("bookmark", buffer);
jassert(value.isBinaryData());
newElement->setAttribute("bookmark", value.getBinaryData()->toBase64Encoding());
}
}
DEBUG_PRINT(xml.createDocument("",false,false));
copyXmlToBinary(xml, destData);
}
void JuicySFAudioProcessor::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.
// This getXmlFromBinary() helper function retrieves our XML from the binary blob..
shared_ptr<XmlElement> xmlState{getXmlFromBinary(data, sizeInBytes)};
DEBUG_PRINT(xmlState->createDocument("",false,false));
if (xmlState.get() != nullptr) {
// make sure that it's actually our type of XML object..
if (xmlState->hasTagName(valueTreeState.state.getType())) {
{
XmlElement* xmlElement{xmlState->getChildByName("soundFont")};
if (xmlElement) {
ValueTree tree{valueTreeState.state.getChildWithName("soundFont")};
{
Value value{tree.getPropertyAsValue("path", nullptr)};
value = xmlElement->getStringAttribute("path", value.getValue());
}
{
Value value{tree.getPropertyAsValue("bookmark", nullptr)};
jassert(value.getValue().isBinaryData());
MemoryBlock buffer;
buffer.fromBase64Encoding(xmlElement->getStringAttribute("bookmark", value.getValue()));
value = buffer;
}
}
}
{
ValueTree tree{valueTreeState.state.getChildWithName("uiState")};
XmlElement* xmlElement{xmlState->getChildByName("uiState")};
if (xmlElement) {
{
Value value{tree.getPropertyAsValue("width", nullptr)};
value = xmlElement->getIntAttribute("width", value.getValue());
}
{
Value value{tree.getPropertyAsValue("height", nullptr)};
value = xmlElement->getIntAttribute("height", value.getValue());
}
}
}
XmlElement* params{xmlState->getChildByName("params")};
if (params) {
for (auto* param : getParameters()) {
if (auto* p = dynamic_cast<AudioProcessorParameterWithID*>(param)) {
p->setValueNotifyingHost(static_cast<float>(params->getDoubleAttribute(p->paramID, p->getValue())));
}
}
}
}
}
}
// FluidSynth only supports float in its process function, so that's all we can support.
bool JuicySFAudioProcessor::supportsDoublePrecisionProcessing() const {
return false;
}
FluidSynthModel& JuicySFAudioProcessor::getFluidSynthModel() {
return fluidSynthModel;
}
//==============================================================================
// This creates new instances of the plugin..
AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
return new JuicySFAudioProcessor();
}