forked from dkmooers/gpu-synth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Synthesis.cpp
493 lines (419 loc) · 13.2 KB
/
Synthesis.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#include "Synthesis.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmain"
#pragma clang diagnostic ignored "-Wint-to-pointer-cast"
#include "IPlug_include_in_plug_src.h"
#pragma clang diagnostic pop
#include "IControl.h"
#include "IPlugMultiTargets_controls.h"
#include "IKeyboardControl.h"
#include "resource.h"
const int kNumPrograms = 5; // number of presets to include (will fill with "Empty" if not enough presets are declared below)
const double parameterStep = 0.001;
enum EParams
{
mB,
mNumPartials,
mStringDetuneRange,
mPartialDetuneRange,
mDamping,
mLinearTerm,
mSquaredTerm,
mCubicTerm,
mBrightnessA,
mBrightnessB,
mPitchBendCoarse,
mPitchBendFine,
//mNoisyTransient,
kNumParams
};
typedef struct {
const char* name;
const int x1;
const int y1;
const int x2;
const int y2;
const double defaultVal;
const double minVal;
const double maxVal;
const double step;
/// also need a shape parameter in here!
} parameterProperties_struct;
const parameterProperties_struct parameterProperties[kNumParams] =
{
{
.name="Inharmonicity",
.x1 = 50,
.y1 = 100,
.x2 = 50+60,
.y2 = 100+90,
.defaultVal = 0.003,
.minVal = 0.0,
.maxVal = 1.0,
.step = 0.0001
},
{
.name="Partials",
.x1 = 150,
.y1 = 50,
.x2 = 150+60,
.y2 = 50+90
},
{
.name="String Detune Range",
.x1 = 250,
.y1 = 100,
.x2 = 250+60,
.y2 = 100+90,
.defaultVal = 0.001,
.minVal = 0.0001,
.maxVal = 0.01,
.step = 0.0001
},
{
.name="Partial Detune Range",
.x1 = 350,
.y1 = 50,
.x2 = 350+60,
.y2 = 50+90,
.defaultVal = 1.0,
.minVal = 0.1,
.maxVal = 2.0,
.step = 0.001
},
{
.name="Damping",
.x1 = 450,
.y1 = 100,
.x2 = 450+60,
.y2 = 100+90,
.defaultVal = 2.5,
.minVal = 0.1,
.maxVal = 10.0,
.step = 0.001
},
{
.name="Linear Term",
.x1 = 50,
.y1 = 250,
.x2 = 50+60,
.y2 = 250+90,
.defaultVal = 0.3,
.minVal = 0.001,
.maxVal = 1.0,
.step = 0.001
},
{
.name="Squared Term",
.x1 = 150,
.y1 = 200,
.x2 = 150+60,
.y2 = 200+90,
.defaultVal = 1.0,
.minVal = 0.001,
.maxVal = 1.0,
.step = 0.001
},
{
.name="Cubic Term",
.x1 = 250,
.y1 = 250,
.x2 = 250+60,
.y2 = 250+90,
.defaultVal = 0.3,
.minVal = 0.001,
.maxVal = 1.0,
.step = 0.001
},
{
.name="Brightness A",
.x1 = 350,
.y1 = 200,
.x2 = 350+60,
.y2 = 200+90,
.defaultVal = 0.4,
.minVal = 0.001,
.maxVal = 1.0,
.step = 0.001
},
{
.name="Brightness B",
.x1 = 450,
.y1 = 250,
.x2 = 450+60,
.y2 = 250+90,
.defaultVal = 0.2,
.minVal = 0.001,
.maxVal = 1.0,
.step = 0.001
},
{
.name="Pitch Bend Coarse",
.x1 = 50,
.y1 = 350,
.x2 = 50+60,
.y2 = 350+90,
.defaultVal = 0.5,
.minVal = 0.001,
.maxVal = 1.0,
.step = 0.001
},
{
.name="Pitch Bend Fine",
.x1 = 150,
.y1 = 350,
.x2 = 150+60,
.y2 = 350+90,
.defaultVal = 0.5,
.minVal = 0.001,
.maxVal = 1.0,
.step = 0.001
}
// {
// .name="Noisy Transient",
// .x1 = 50,
// .y1 = 374,
// .x2 = 50+60,
// .y2 = 374+90
// }
};
enum ELayout
{
kWidth = GUI_WIDTH,
kHeight = GUI_HEIGHT,
kKeybX = 1,
kKeybY = 720
//kFrequencyX = 79,
//kFrequencyY = 62,
//kKnobFrames = 128
};
Synthesis::Synthesis(IPlugInstanceInfo instanceInfo)
: IPLUG_CTOR(kNumParams, kNumPrograms, instanceInfo),
lastVirtualKeyboardNoteNumber(virtualKeyboardMinimumNoteNumber - 1)
{
TRACE;
CreateParams();
CreateGraphics();
CreatePresets();
VoiceManager& voiceManager = VoiceManager::getInstance();
voiceManager.initOpenCL();
//openCLStarted = false;
// connet the noteOn's and noteOff's from mMIDIReceiver to VoiceManager using signals and slots
mMIDIReceiver.noteOn.Connect(&voiceManager, &VoiceManager::onNoteOn);
mMIDIReceiver.noteOff.Connect(&voiceManager, &VoiceManager::onNoteOff);
mMIDIReceiver.sustainChange.Connect(&voiceManager, &VoiceManager::onSustainChange);
mMIDIReceiver.expressionChange.Connect(&voiceManager, &VoiceManager::onExpressionChange);
mMIDIReceiver.modChange.Connect(&voiceManager, &VoiceManager::onModChange);
//mMIDIReceiver.noteOn.Connect(mOscilloscope, &Oscilloscope::updatePixelColors);
}
Synthesis::~Synthesis() {}
void Synthesis::CreateParams() {
for (int i = 0; i < kNumParams; i++) {
IParam* param = GetParam(i);
const parameterProperties_struct& properties = parameterProperties[i];
switch (i) {
// Int parameters:
case mNumPartials:
param->InitInt(properties.name,
10, // default
1, // min
200); // max
break;
// Bool parameters:
// case mNoisyTransient:
// param->InitBool(properties.name, true);
// break;
// Double parameters:
default:
param->InitDouble(properties.name,
properties.defaultVal,
properties.minVal,
properties.maxVal,
parameterStep);
break;
}
}
/// set shape for knobs
GetParam(mB)->SetShape(5);
GetParam(mNumPartials)->SetShape(1);
/// initialize correct default parameter values on load
for (int i = 0; i < kNumParams; i++) {
OnParamChange(i);
}
}
void Synthesis::CreateGraphics() {
IGraphics* pGraphics = MakeGraphics(this, kWidth, kHeight);
pGraphics->AttachPanelBackground(&COLOR_BLACK);
//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);
//mOscilloscope = new Oscilloscope(this, IRECT(0, 300, GUI_WIDTH, GUI_HEIGHT));
// 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: */ 8, &whiteKeyImage, &blackKeyImage, keyCoordinates);
pGraphics->AttachControl(mVirtualKeyboard);
// Knob bitmap
IBitmap knobBitmap = pGraphics->LoadIBitmap(KNOB_ID, KNOB_FN, 101);
for (int i = 0; i < kNumParams; i++) {
const parameterProperties_struct& properties = parameterProperties[i];
IControl* control;
IControl* labelcontrol;
IBitmap* graphic;
IText text = IText(14, &COLOR_WHITE);
IText labeltext = IText(14, &COLOR_WHITE);
switch (i) {
// Knobs:
default:
graphic = &knobBitmap;
control = new IKnobMultiControlText(this, IRECT(properties.x1, properties.y1, properties.x2, properties.y2), i, graphic, &text);
labelcontrol = new ITextControl(this, IRECT(properties.x1, properties.y1-20, properties.x2, properties.y1), &labeltext, properties.name);
break;
}
pGraphics->AttachControl(control);
pGraphics->AttachControl(labelcontrol);
}
// // caption
// IText mBLabel(14, &COLOR_WHITE);
// pGraphics->AttachControl(new ITextControl(this, IRECT(60-12, 154-20, 60+50, 154+20), &mBLabel, "Inharmonicity"));
//
// // TEXT TESTING
//
// IRECT tmpRect4(650, 50, 800, 120);
// IText textProps4(40, &COLOR_WHITE, "Helvetica", IText::kStyleNormal, IText::kAlignCenter, 0, IText::kQualityDefault);
// pGraphics->AttachControl(new ITextControl(this, tmpRect4, &textProps4, "STRING SYNTH"));
//pGraphics->AttachControl(mOscilloscope);
AttachGraphics(pGraphics);
}
///////////////////////////---------////////////////////////////
void Synthesis::ProcessDoubleReplacing(double** inputs, double** outputs, int nFrames)
{
// Mutex is already locked for us.
double *leftOutput = outputs[0];
double *rightOutput = outputs[1];
VoiceManager& voiceManager = VoiceManager::getInstance();
processVirtualKeyboard();
//std::cout << "\nBlock size = " << GetBlockSize();
// blockOfSamples = voiceManager.getBlockOfSamples();
// for (int i = 0; i < BLOCK_SIZE; i++) {
// leftOutput[i] = blockOfSamples[i*NUM_CHANNELS];
// rightOutput[i] = blockOfSamples[i*NUM_CHANNELS + 1];
//// if (i < 512) {
//// printf("\ni = %d, left = %f, right = %f", i, leftOutput[i], rightOutput[i]);
//// }
// //printf("\nleftOutput index = %d, rightOutput index = %d", i*NUM_CHANNELS, i*NUM_CHANNELS+1);
// mOscilloscope->updateLastSample(leftOutput[i], rightOutput[i]);
// mMIDIReceiver.advance();
// }
//std::thread voiceEnergyUpdater(&Synthesis::voiceEnergyUpdaterManager, this);
/// this allows for BLOCK_SIZE to be different (smaller) than actual VST host blockSize, to reduce latency
int numBlocks = nFrames/BLOCK_SIZE;
for (int j = 0; j < numBlocks; j++) {
blockOfSamples = voiceManager.getBlockOfSamples();
for (int i = 0; i < BLOCK_SIZE; i++) {
leftOutput[i + j*BLOCK_SIZE] = clip(blockOfSamples[i*NUM_CHANNELS]);
rightOutput[i + j*BLOCK_SIZE] = clip(blockOfSamples[i*NUM_CHANNELS + 1]);
// add in bandlimited noise
// leftOutput[i + j*BLOCK_SIZE] += 0.003f * (static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/2.0f)) - 1.0f) * blockOfSamples[i*NUM_CHANNELS];
// rightOutput[i + j*BLOCK_SIZE] += 0.003f * (static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/2.0f)) - 1.0f) * blockOfSamples[i*NUM_CHANNELS + 1];
// mOscilloscope->updateLastSample(leftOutput[i], rightOutput[i]);
mMIDIReceiver.advance();
voiceManager.updateVoiceDampingAndEnergy(i);
}
mMIDIReceiver.Flush(nFrames/numBlocks);
voiceManager.setFreeInaudibleVoices();
}
// for (int i = 0; i < nFrames/BLOCK_SIZE; i++) {
// blockOfSamples = voiceManager.getBlockOfSamples();
// for (int j = 0; j < BLOCK_SIZE; j++) {
// leftOutput[i*BLOCK_SIZE + j] = blockOfSamples[i*NUM_CHANNELS];
// rightOutput[i*BLOCK_SIZE + j] = blockOfSamples[i*NUM_CHANNELS + 1];
// mOscilloscope->updateLastSample(leftOutput[i], rightOutput[i]);
// mMIDIReceiver.advance();
// }
// }
//mMIDIReceiver.Flush(nFrames);
}
double Synthesis::clip(double n) {
return std::max(-0.99, std::min(n, 0.99));
}
///////////////////////////---------////////////////////////////
void Synthesis::Reset()
{
TRACE;
IMutexLock lock(this);
double sampleRate = GetSampleRate();
VoiceManager::getInstance().setSampleRate(sampleRate);
}
void Synthesis::OnParamChange(int paramIdx)
{
IMutexLock lock(this);
VoiceManager& voiceManager = VoiceManager::getInstance();
IParam* param = GetParam(paramIdx);
// std::cout << paramIdx << "\n";
// printf("\nparam: %s", paramIdx);
switch(paramIdx) {
// Volume Envelope:
case mB:
voiceManager.updateInharmonicityCoeff(param->Value());
break;
case mNumPartials:
voiceManager.updateNumPartials(param->Value());
break;
case mStringDetuneRange:
voiceManager.updateStringDetuneRange(param->Value());
break;
case mPartialDetuneRange:
voiceManager.updatePartialDetuneRange(param->Value());
break;
case mDamping:
voiceManager.updateDamping(param->Value());
break;
case mLinearTerm:
voiceManager.updateLinearTerm(param->Value());
break;
case mSquaredTerm:
voiceManager.updateSquaredTerm(param->Value());
break;
case mCubicTerm:
voiceManager.updateCubicTerm(param->Value());
break;
case mBrightnessA:
voiceManager.updateBrightnessA(param->Value());
break;
case mBrightnessB:
voiceManager.updateBrightnessB(param->Value());
break;
case mPitchBendCoarse:
voiceManager.updatePitchBendCoarse(param->Value());
break;
case mPitchBendFine:
voiceManager.updatePitchBendFine(param->Value());
break;
default:
break;
}
}
// This function will be called whenever the application receives a MIDI message. We're passing the messages through to our MIDI receiver.
void Synthesis::ProcessMidiMsg(IMidiMsg* pMsg) {
mMIDIReceiver.onMessageReceived(pMsg);
mVirtualKeyboard->SetDirty();
}
void Synthesis::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 pressed 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;
}
void Synthesis::CreatePresets() {
}