-
Notifications
You must be signed in to change notification settings - Fork 5
/
DrMixAISynth.cpp
304 lines (240 loc) · 7.79 KB
/
DrMixAISynth.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
#include "DrMixAISynth.h"
#include "IPlug/IPlug_include_in_plug_src.h"
#include "WDL/denormal.h"
#include <string.h>
class IKnobCustomControl: public IKnobMultiControl
{
public:
IKnobCustomControl(IPlugBase *pPlug, int x, int y, int paramIdx = -1, const IBitmap *pBitmap = NULL)
: IKnobMultiControl(pPlug, x, y, paramIdx, pBitmap) {}
void PromptUserInput()
{
mPlug->GetGUI()->PromptUserInput(this, mPlug->GetParam(mParamIdx), &mRECT, IGraphics::kPromptCustomWidth);
Redraw();
}
};
DrMixAISynth::DrMixAISynth(void *instance):
IPLUG_CTOR(kNumParams, 1, instance),
m_synth(new SawtoothSynth()),
m_note_on(-1)
{
// Plugin parameters
IBoolParam *pBypassParam = new IBoolParam("Bypass", false);
pBypassParam->SetGlobal(true);
AddParam(kParamBypass, pBypassParam);
AddParam(kParamEnvelope, new IBoolParam("Envelope", false));
AddParam(kParamAttackTime, new IDoubleExpParam(3, "Attack", 100, 1, 5000, 0, "ms"));
AddParam(kParamDecayTime, new IDoubleExpParam(3, "Decay", 200, 1, 5000, 0, "ms"));
AddParam(kParamSustainLevel, new IDoubleParam("Sustain", -6.0, -72.0, 0.0, 1, "dB"));
AddParam(kParamReleaseTime, new IDoubleExpParam(3, "Release", 300, 1, 5000, 0, "ms"));
AddParam(kParamCutoffFrequency, new IDoubleExpParam(6, "Cutoff", 20000, 20, 20000, 0, "Hz"));
AddParam(kParamResonance, new IDoubleParam("Resonance", 0.5, 0.5, 4.0, 1));
AddParam(kParamLFOFrequency, new IDoubleExpParam(3, "LFO Rate", 2, 0.1, 10, 2, "Hz"));
AddParam(kParamLFOAmplitude, new IDoubleParam("LFO Depth", 0, 0, 1000, 0, "Hz"));
MakeDefaultPreset("Default");
// GUI
IGraphics *pGraphics = MakeGraphics(this, 1200, 680);
pGraphics->SetDefaultScale(IGraphics::kScaleHalf);
pGraphics->EnableTooltips(true);
pGraphics->HandleMouseWheel(IGraphics::kMouseWheelModKey);
IBitmap background(BACKGROUND_PNG_ID, pGraphics->Width(), pGraphics->Height());
pGraphics->AttachBackground(new IBackgroundControl(this, &background));
// Envelope switch/knobs
IBitmap switchBitmap(SWITCH_PNG_ID, 90, 32);
IControl *pSwitchControl = new ISwitchControl(this, 62, 72, kParamEnvelope, &switchBitmap);
pSwitchControl->SetTooltip("Envelope On/Off");
pGraphics->AttachControl(pSwitchControl);
IBitmap knobBitmap(KNOB_PNG_ID, 88, 88, 129);
IControl *pKnobControl;
pKnobControl = new IKnobCustomControl(this, 62, 150, kParamAttackTime, &knobBitmap);
pKnobControl->SetTooltip("Attack Time");
pGraphics->AttachControl(pKnobControl);
pKnobControl = new IKnobCustomControl(this, 62, 280, kParamDecayTime, &knobBitmap);
pKnobControl->SetTooltip("Decay Time");
pGraphics->AttachControl(pKnobControl);
pKnobControl = new IKnobCustomControl(this, 62, 410, kParamSustainLevel, &knobBitmap);
pKnobControl->SetTooltip("Sustain Level");
pGraphics->AttachControl(pKnobControl);
pKnobControl = new IKnobCustomControl(this, 62, 540, kParamReleaseTime, &knobBitmap);
pKnobControl->SetTooltip("Release Time");
pGraphics->AttachControl(pKnobControl);
// Filter knobs
pKnobControl = new IKnobCustomControl(this, 214, 108, kParamCutoffFrequency, &knobBitmap);
pKnobControl->SetTooltip("Cutoff Frequency");
pGraphics->AttachControl(pKnobControl);
pKnobControl = new IKnobCustomControl(this, 214, 238, kParamResonance, &knobBitmap);
pKnobControl->SetTooltip("Resonance");
pGraphics->AttachControl(pKnobControl);
pKnobControl = new IKnobCustomControl(this, 214, 368, kParamLFOFrequency, &knobBitmap);
pKnobControl->SetTooltip("LFO Rate");
pGraphics->AttachControl(pKnobControl);
pKnobControl = new IKnobCustomControl(this, 214, 498, kParamLFOAmplitude, &knobBitmap);
pKnobControl->SetTooltip("LFO Depth");
pGraphics->AttachControl(pKnobControl);
AttachGraphics(pGraphics);
}
void DrMixAISynth::SetSampleRate(double rate)
{
IPlug::SetSampleRate(rate);
m_synth->SetSampleRate(rate);
}
void DrMixAISynth::SetBlockSize(int size)
{
IPlug::SetBlockSize(size);
m_midi_queue.Resize(GetBlockSize(), false);
}
void DrMixAISynth::OnParamChange(int index)
{
switch (index)
{
case kParamEnvelope:
{
bool enable = GetParam<IBoolParam>(index)->Bool();
bool gate = m_note_on >= 0;
BypassEnvelope(!enable, gate);
break;
}
case kParamAttackTime:
{
double attack = GetParam<IDoubleExpParam>(index)->Value() * 0.001;
SetAttackTime(attack);
break;
}
case kParamDecayTime:
{
double decay = GetParam<IDoubleExpParam>(index)->Value() * 0.001;
SetDecayTime(decay);
break;
}
case kParamSustainLevel:
{
double sustain = GetParam<IDoubleParam>(index)->DBToAmp();
SetSustainLevel(sustain);
break;
}
case kParamReleaseTime:
{
double release = GetParam<IDoubleExpParam>(index)->Value() * 0.001;
SetReleaseTime(release);
break;
}
case kParamCutoffFrequency:
{
double cutoff = GetParam<IDoubleExpParam>(index)->Value();
SetCutoffFrequency(cutoff);
break;
}
case kParamResonance:
{
double resonance = GetParam<IDoubleParam>(index)->Value();
SetResonance(resonance);
break;
}
case kParamLFOFrequency:
{
double rate = GetParam<IDoubleExpParam>(index)->Value();
SetLFOFrequency(rate);
break;
}
case kParamLFOAmplitude:
{
double depth = GetParam<IDoubleParam>(index)->Value();
SetLFOAmplitude(depth);
break;
}
}
}
void DrMixAISynth::Reset()
{
m_synth->Reset();
}
void DrMixAISynth::ProcessMidiMsg(const IMidiMsg *msg)
{
m_midi_queue.Add(msg);
}
void DrMixAISynth::ProcessMidiQueue(const IMidiMsg *msg)
{
switch (msg->mStatus >> 4)
{
case IMidiMsg::kNoteOn:
if (msg->mData2)
{
int note = msg->mData1;
double freq = pow(2, (double)(note - 69) / 12) * 440;
SetFrequency(freq);
m_note_on = note;
m_synth->Attack();
break;
}
case IMidiMsg::kNoteOff:
{
int note = msg->mData1;
if (note == m_note_on) m_note_on = -1;
break;
}
case IMidiMsg::kControlChange:
{
int cc = msg->mData1;
if (cc == IMidiMsg::kAllNotesOff) m_note_on = -1;
break;
}
}
}
void DrMixAISynth::ProcessDoubleReplacing(const double *const *inputs, double *const *outputs, int samples)
{
#ifdef WDL_DENORMAL_FTZMODE
WDL_denormal_ftz_scope denormalFtz;
#endif
bool pluginIsBypassed = IsBypassed() || GetParam<IBoolParam>(kParamBypass)->Bool();
bool envelopIsEnabled = !m_synth->EnvelopeIsBypassed();
for (int offset = 0; offset < samples;)
{
int next;
for (;;)
{
if (m_midi_queue.Empty())
{
next = samples;
break;
}
const IMidiMsg* msg = m_midi_queue.Peek();
next = msg->mOffset;
if (next > offset) break;
ProcessMidiQueue(msg);
m_midi_queue.Remove();
}
int block = next - offset;
bool gate = !pluginIsBypassed && (m_note_on >= 0 || envelopIsEnabled);
Process(&outputs[0][offset], block, gate);
offset = next;
}
memcpy(outputs[1], outputs[0], samples * sizeof(double));
m_midi_queue.Flush(samples);
}
bool DrMixAISynth::OnGUIRescale(int wantScale)
{
// Load image set depending on host GUI DPI.
IGraphics* pGraphics = GetGUI();
pGraphics->Rescale(wantScale);
typedef IGraphics::BitmapResource Resource;
// Half scale image set.
static const Resource halfScaleImageSet[] =
{
Resource(IPLUG_RESOURCE(BACKGROUND_PNG)),
Resource(IPLUG_RESOURCE(SWITCH_PNG)),
Resource(IPLUG_RESOURCE(KNOB_PNG)),
Resource()
};
// Full scale image set.
static const Resource fullScaleImageSet[] =
{
Resource(IPLUG_RESOURCE(BACKGROUND_2X_PNG)),
Resource(IPLUG_RESOURCE(SWITCH_2X_PNG)),
Resource(IPLUG_RESOURCE(KNOB_2X_PNG)),
Resource()
};
const Resource* pResources = halfScaleImageSet;
if (wantScale == IGraphics::kScaleFull) pResources = fullScaleImageSet;
pGraphics->LoadBitmapResources(pResources);
return true;
}