-
Notifications
You must be signed in to change notification settings - Fork 1
/
PancakeHouse.cpp
338 lines (300 loc) · 10.3 KB
/
PancakeHouse.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
#include "PancakeHouse.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmain"
#include "IPlug_include_in_plug_src.h"
#pragma clang diagnostic pop
#include "IControl.h"
#include "IKeyboardControl.h"
#include "resource.h"
#include <math.h>
#include <algorithm>
#include <functional>
const int kNumPrograms = 5;
const double parameterStep = 0.001;
enum EParams
{
// Oscillator Section:
mOsc1Waveform = 0,
mOsc1PitchMod,
mOsc2Waveform,
mOsc2PitchMod,
mOscMix,
// Filter Section:
mFilterMode,
mFilterCutoff,
mFilterResonance,
mFilterLfoAmount,
mFilterEnvAmount,
// LFO:
mLFOWaveform,
mLFOFrequency,
// Volume Envelope
mVolumeEnvAttack,
mVolumeEnvDecay,
mVolumeEnvSustain,
mVolumeEnvRelease,
// Filter envelope:
mFilterEnvAttack,
mFilterEnvDecay,
mFilterEnvSustain,
mFilterEnvRelease,
kNumParams
};
typedef struct {
const char* name;
const int x;
const int y;
const double defaultVal;
const double minVal;
const double maxVal;
} parameterProperties_struct;
parameterProperties_struct parameterProperties[kNumParams] =
{
{ "Osc 1 Waveform", 30, 75, 0, 0, 0 },
{ "Osc 1 Pitch Mod", 85, 75, 0.0, 0.0, 1.0 },
{ "Osc 2 Waveform", 203, 75, 0, 0, 0 },
{ "Osc 2 Pitch Mod", 260, 75, 0.0, 0.0, 1.0 },
{ "Osc Mix", 146, 75, 0.5, 0.0, 1.0 },
{ "Filter Mode", 30, 188, 0, 0, 0 },
{ "Filter Cutoff", 84, 190, 0.99, 0.0, 0.99 },
{ "Filter Resonance", 139, 190, 0.0, 0.0, 1.0 },
{ "Filter LFO Amount", 196, 190, 0.0, 0.0, 1.0 },
{ "Filter Envelope Amount", 251, 190, 0.0, -1.0, 1.0 },
{ "LFO Waveform", 30, 298, 0, 0, 0 },
{ "LFO Frequency", 86, 299, 6.0, 0.01, 30.0 },
{ "Volume Env Attack", 337, 75, 0.01, 0.01, 10.0 },
{ "Volume Env Decay", 393, 75, 0.5, 0.01, 15.0 },
{ "Volume Env Sustain", 448, 75, 0.1, 0.001, 1.0 },
{ "Volume Env Release", 503, 75, 1.0, 0.01, 15.0 },
{ "Filter Env Attack", 338, 190, 0.01, 0.01, 10.0 },
{ "Filter Env Decay", 393, 190, 0.5, 0.01, 15.0 },
{ "Filter Env Sustain", 448, 190, 0.1, 0.001, 1.0 },
{ "Filter Env Release", 503, 190, 1.0, 0.01, 15.0 },
};
enum ELayout
{
kWidth = GUI_WIDTH,
kHeight = GUI_HEIGHT,
kKeybX = 62,
kKeybY = 425
};
PancakeHouse::PancakeHouse(IPlugInstanceInfo instanceInfo)
: IPLUG_CTOR(kNumParams, kNumPrograms, instanceInfo),
lastVirtualKeyboardNoteNumber(virtualKeyboardMinimumNoteNumber - 1)
{
TRACE;
CreateParams();
CreateGraphics();
CreatePresets();
mMIDIReceiver.noteOn.Connect(&voiceManager, &VoiceManager::onNoteOn);
mMIDIReceiver.noteOff.Connect(&voiceManager, &VoiceManager::onNoteOff);
}
PancakeHouse::~PancakeHouse() {}
void PancakeHouse::ProcessDoubleReplacing(double** inputs,
double** outputs,
int nFrames)
{
// Mutex is already locked for us.
double *leftOutput = outputs[0];
double *rightOutput = outputs[1];
processVirtualKeyboard();
//copy left buffer into right buffer
for (int i = 0; i < nFrames; ++i) {
mMIDIReceiver.advance();
leftOutput[i] = rightOutput[i] = voiceManager.nextSample();
}
mMIDIReceiver.Flush(nFrames);
}
void PancakeHouse::Reset()
{
TRACE;
IMutexLock lock(this);
double sampleRate = GetSampleRate();
voiceManager.setSampleRate(sampleRate);
}
void PancakeHouse::OnParamChange(int paramIdx)
{
IMutexLock lock(this);
IParam* param = GetParam(paramIdx);
if (paramIdx == mLFOWaveform) {
voiceManager.setLFOMode(static_cast<Oscillator::OscillatorMode>(param->Int()));
}
else if (paramIdx == mLFOFrequency) {
voiceManager.setLFOFrequency(param->Value());
}
else {
using std::tr1::placeholders::_1;
using std::tr1::bind;
VoiceManager::VoiceChangerFunction changer;
switch (paramIdx) {
case mOsc1Waveform:
changer = bind(&VoiceManager::setOscillatorMode, _1, 1, static_cast<Oscillator::OscillatorMode>(param->Int()));
break;
case mOsc1PitchMod:
changer = bind(&VoiceManager::setOscillatorPitchMod, _1, 1, param->Value());
break;
case mOsc2Waveform:
changer = bind(&VoiceManager::setOscillatorMode, _1, 2, static_cast<Oscillator::OscillatorMode>(param->Int()));
break;
case mOsc2PitchMod:
changer = bind(&VoiceManager::setOscillatorPitchMod, _1, 2, param->Value());
break;
case mOscMix:
changer = bind(&VoiceManager::setOscillatorMix, _1, param->Value());
break;
// Filter Section:
case mFilterMode:
changer = bind(&VoiceManager::setFilterMode, _1, static_cast<Filter::FilterMode>(param->Int()));
break;
case mFilterCutoff:
changer = bind(&VoiceManager::setFilterCutoff, _1, param->Value());
break;
case mFilterResonance:
changer = bind(&VoiceManager::setFilterResonance, _1, param->Value());
break;
case mFilterLfoAmount:
changer = bind(&VoiceManager::setFilterLFOAmount, _1, param->Value());
break;
case mFilterEnvAmount:
changer = bind(&VoiceManager::setFilterEnvAmount, _1, param->Value());
break;
// Volume Envelope
case mVolumeEnvAttack:
changer = bind(&VoiceManager::setVolumeEnvelopeStageValue, _1, EnvelopeGenerator::ENVELOPE_STAGE_ATTACK, param->Value());
break;
case mVolumeEnvDecay:
changer = bind(&VoiceManager::setVolumeEnvelopeStageValue, _1, EnvelopeGenerator::ENVELOPE_STAGE_DECAY, param->Value());
break;
case mVolumeEnvSustain:
changer = bind(&VoiceManager::setFilterEnvelopeStageValue, _1, EnvelopeGenerator::ENVELOPE_STAGE_SUSTAIN, param->Value());
break;
case mVolumeEnvRelease:
changer = bind(&VoiceManager::setFilterEnvelopeStageValue, _1, EnvelopeGenerator::ENVELOPE_STAGE_RELEASE, param->Value());
break;
//Filter Envelope
case mFilterEnvAttack:
changer = bind(&VoiceManager::setFilterEnvelopeStageValue, _1, EnvelopeGenerator::ENVELOPE_STAGE_ATTACK, param->Value());
break;
case mFilterEnvDecay:
changer = bind(&VoiceManager::setFilterEnvelopeStageValue, _1, EnvelopeGenerator::ENVELOPE_STAGE_DECAY, param->Value());
break;
case mFilterEnvSustain:
changer = bind(&VoiceManager::setFilterEnvelopeStageValue, _1, EnvelopeGenerator::ENVELOPE_STAGE_SUSTAIN, param->Value());
break;
case mFilterEnvRelease:
changer = bind(&VoiceManager::setFilterEnvelopeStageValue, _1, EnvelopeGenerator::ENVELOPE_STAGE_RELEASE, param->Value());
break;
}
voiceManager.changeAllVoices(changer);
}
}
void PancakeHouse::CreatePresets() {
}
void PancakeHouse::CreateParams() {
for (int i = 0; i < kNumParams; i++) {
IParam* param = GetParam(i);
const parameterProperties_struct& properties = parameterProperties[i];
switch (i) {
// Enum Parameters:
case mOsc1Waveform:
case mOsc2Waveform:
param->InitEnum(properties.name,
Oscillator::OSCILLATOR_MODE_SAW,
Oscillator::kNumOscillatorModes);
// for VST3
param->SetDisplayText(0, properties.name);
break;
case mLFOWaveform:
param->InitEnum(properties.name,
Oscillator::OSCILLATOR_MODE_TRIANGLE,
Oscillator::kNumOscillatorModes);
// For VST3
param->SetDisplayText(0, properties.name);
break;
case mFilterMode:
param->InitEnum(properties.name,
Filter::FILTER_MODE_LOWPASS,
Filter::knumFilterModes);
break;
//Double Parameters
default:
param->InitDouble(properties.name,
properties.defaultVal,
properties.minVal,
properties.maxVal,
parameterStep);
break;
}
}
GetParam(mFilterCutoff)->SetShape(2);
GetParam(mVolumeEnvAttack)->SetShape(3);
GetParam(mFilterEnvAttack)->SetShape(3);
GetParam(mVolumeEnvDecay)->SetShape(3);
GetParam(mFilterEnvDecay)->SetShape(3);
GetParam(mVolumeEnvSustain)->SetShape(2);
GetParam(mFilterEnvSustain)->SetShape(2);
GetParam(mVolumeEnvRelease)->SetShape(3);
GetParam(mFilterEnvRelease)->SetShape(3);
for (int i = 0; i < kNumParams; i++) {
OnParamChange(i);
}
}
void PancakeHouse::CreateGraphics() {
IGraphics* pGraphics = MakeGraphics(this, kWidth, kHeight);
pGraphics->AttachBackground(BG_ID, BG_FN);
IBitmap whiteKeyImage = pGraphics->LoadIBitmap(WHITE_KEY_ID, WHITE_KEY_FN, 6);
IBitmap blackKeyImage = pGraphics->LoadIBitmap(BLACK_KEY_ID, BLACK_KEY_FN);
// C# D# F# G# A#
int keyCoordinates[12] = { 0, 10, 17, 30, 35, 52, 61, 68, 79, 85, 97, 102 };
mVirtualKeyboard = new IKeyboardControl(this, kKeybX, kKeybY, virtualKeyboardMinimumNoteNumber, /*octaves: */ 4, &whiteKeyImage, &blackKeyImage, keyCoordinates);
pGraphics->AttachControl(mVirtualKeyboard);
IBitmap waveformBitmap = pGraphics->LoadIBitmap(WAVEFORM_ID, WAVEFORM_FN, 4);
IBitmap filterModeBitmap = pGraphics->LoadIBitmap(FILTERMODE_ID, FILTERMODE_FN, 3);
IBitmap knobBitmap = pGraphics->LoadIBitmap(KNOB_ID, KNOB_FN, 127);
for (int i = 0; i < kNumParams; i++) {
const parameterProperties_struct& properties = parameterProperties[i];
IControl* control;
IBitmap* graphic;
switch (i) {
//Switches
case mOsc1Waveform:
case mOsc2Waveform:
case mLFOWaveform:
graphic = &waveformBitmap;
control = new ISwitchControl(this, properties.x, properties.y, i, graphic);
break;
case mFilterMode:
graphic = &filterModeBitmap;
control = new ISwitchControl(this, properties.x, properties.y, i, graphic);
break;
// Knobs
default:
graphic = &knobBitmap;
control = new IKnobMultiControl(this, properties.x, properties.y, i, graphic);
break;
}
pGraphics->AttachControl(control);
}
AttachGraphics(pGraphics);
}
void PancakeHouse::ProcessMidiMsg(IMidiMsg* pMsg) {
mMIDIReceiver.onMessageReceived(pMsg);
mVirtualKeyboard->SetDirty();
}
void PancakeHouse::processVirtualKeyboard() {
IKeyboardControl* virtualKeyboard = (IKeyboardControl*)mVirtualKeyboard;
int virtualKeyboardNoteNumber = virtualKeyboard->GetKey() + virtualKeyboardMinimumNoteNumber;
if (lastVirtualKeyboardNoteNumber >= virtualKeyboardMinimumNoteNumber && virtualKeyboardNoteNumber != lastVirtualKeyboardNoteNumber) {
// The note number has changed from a valid key to something else (valid key or nothing). Release the valid key:
IMidiMsg midiMessage;
midiMessage.MakeNoteOffMsg(lastVirtualKeyboardNoteNumber, 0);
mMIDIReceiver.onMessageReceived(&midiMessage);
}
if (virtualKeyboardNoteNumber >= virtualKeyboardMinimumNoteNumber && virtualKeyboardNoteNumber != lastVirtualKeyboardNoteNumber) {
// A valid key is presesed that wasn't pressed the previous call. Send a "note on" message to the MIDI receiver:
IMidiMsg midiMessage;
midiMessage.MakeNoteOnMsg(virtualKeyboardNoteNumber, virtualKeyboard->GetVelocity(), 0);
mMIDIReceiver.onMessageReceived(&midiMessage);
}
lastVirtualKeyboardNoteNumber = virtualKeyboardNoteNumber;
}