-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynth.h
357 lines (305 loc) · 9.26 KB
/
synth.h
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
#pragma once
/*
* File: synth.h
*
* Simple Synth using Maximilian
*
*/
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <algorithm>
#include <arm_neon.h>
#include "unit.h" // Note: Include common definitions for all units
#include "maximilian.h"
#include "libs/maxiPolyBLEP.h"
// Uncomment this line to use maxiEnvGen class
// #define USE_MAXIENVGEN
// Use this instead of mtof() in maxmilian to allow note number in float
inline double note2freq(float note) {
return (440.f / 32) * powf(2, (note - 9.0) / 12);
}
enum Params {
Note = 0,
AmpDecay,
Wave,
Balance,
// Wave,
Decay,
EnvPitchInt,
Shape,
Gain,
EqGain,
EqCutoff,
EqReso,
EqShape
};
class Synth {
public:
/*===========================================================================*/
/* Public Data Structures/Types. */
/*===========================================================================*/
/*===========================================================================*/
/* Lifecycle Methods. */
/*===========================================================================*/
Synth(void) {}
~Synth(void) {}
inline int8_t Init(const unit_runtime_desc_t * desc) {
// Check compatibility of samplerate with unit, for drumlogue should be 48000
if (desc->samplerate != 48000)
return k_unit_err_samplerate;
// Check compatibility of frame geometry
if (desc->output_channels != 2) // should be stereo output
return k_unit_err_geometry;
maxiSettings::sampleRate = 48000;
// pitchEnvelope_.setupAR()
// Note: if need to allocate some memory can do it here and return k_unit_err_memory if getting allocation errors
return k_unit_err_none;
}
inline void Teardown() {
// Note: cleanup and release resources if any
}
inline void Reset() {
// Note: Reset synth state. I.e.: Clear filter memory, reset oscillator
// phase etc.
// osc.setWaveform(maxiPolyBLEP::Waveform::SINE);
// osc2_.setWaveform(maxiPolyBLEP::Waveform::SAWTOOTH);
gate_ = 0;
}
inline void Resume() {
// Note: Synth will resume and exit suspend state. Usually means the synth
// was selected and the render callback will be called again
}
inline void Suspend() {
// Note: Synth will enter suspend state. Usually means another synth was
// selected and thus the render callback will not be called
}
/*===========================================================================*/
/* Other Public Methods. */
/*===========================================================================*/
fast_inline void Render(float * out, size_t frames) {
float * __restrict out_p = out;
const float * out_e = out_p + (frames << 1); // assuming stereo output
const int trigger = (gate_ > 0);
for (; out_p != out_e; out_p += 2) {
// Envelope
float env = pitchEnvelope_.adsr(1.0, trigger);
float ampEnv = ampEnvelope_.adsr(1.0, trigger);
float sig;
// Oscillator
float pitch1 = note2freq(note_ + egPitch_ * env);
if (wave_ == 3) {
sig = (1.f - balance_) * osc.saw(pitch1) + balance_ * noise.noise();
} else if (wave_ == 2) {
sig = (1.f - balance_) * osc.square(pitch1) + balance_ * noise.noise();
} else if (wave_ == 1) {
sig = (1.f - balance_) * osc.triangle(pitch1) + balance_ * noise.noise();
} else {
sig = (1.f - balance_) * osc.sinewave(pitch1) + balance_ * noise.noise();
}
sig = maxiConvert::dbsToAmp(gain_) * sig;
if (shape_ == 2) {
sig = sat.fastatan(sig);
} else if (shape_ == 1) {
sig = sat.softclip(sig);
} else {
sig = sat.hardclip(sig);
}
// Filter
if (eqShape_ == 2) {
eq.set(maxiBiquad::HIGHSHELF, note2freq(eqCutoff_), eqResonance_, eqGain_);
} else if (eqShape_ == 1) {
eq.set(maxiBiquad::LOWSHELF, note2freq(eqCutoff_), eqResonance_, eqGain_);
} else {
eq.set(maxiBiquad::PEAK, note2freq(eqCutoff_), eqResonance_, eqGain_);
}
sig = eq.play(sig * ampEnv);
// Note: should take advantage of NEON ArmV7 instructions
vst1_f32(out_p, vdup_n_f32(sig));
}
}
inline void setParameter(uint8_t index, int32_t value) {
p_[index] = value;
switch (index) {
case Note:
note_ = value;
break;
case Decay:
pitchEnvelope_.setAttackMS(1.0f);
pitchEnvelope_.setSustain(0.0f);
pitchEnvelope_.setRelease(1.0f);
pitchEnvelope_.setDecay(value + 1);
break;
case AmpDecay:
ampEnvelope_.setAttackMS(1.0f);
ampEnvelope_.setSustain(0.0f);
ampEnvelope_.setRelease(1.0f);
ampEnvelope_.setDecay(value + 1);
break;
case EnvPitchInt:
egPitch_ = 0.48f * value; // 24 semitones / 100%
break;
case Balance:
balance_ = 0.01f * value;
break;
case Gain:
gain_ = value;
break;
case Shape:
shape_ = value;
break;
case Wave:
wave_ = value;
break;
case EqShape:
eqShape_ = value;
break;
case EqCutoff:
eqCutoff_ = 1.27f * value; // -63.5 .. +63.5
break;
case EqReso:
eqResonance_ = powf(2, 1.f / (1<<5) * value); // 2^(-4) .. 2^4
break;
case EqGain:
eqGain_ = value;
break;
default:
break;
}
}
inline int32_t getParameterValue(uint8_t index) const {
return p_[index];
}
inline const char * getParameterStrValue(uint8_t index, int32_t value) const {
(void)value;
switch (index) {
case Shape:
if (value < 3) {
return ShapeStr[value];
} else {
return nullptr;
}
break;
case Wave:
if (value < 4) {
return WaveStr[value];
} else {
return nullptr;
}
break;
case EqShape:
if (value < 3) {
return EqShapeStr[value];
} else {
return nullptr;
}
break;
// Note: String memory must be accessible even after function returned.
// It can be assumed that caller will have copied or used the string
// before the next call to getParameterStrValue
default:
break;
}
return nullptr;
}
inline const uint8_t * getParameterBmpValue(uint8_t index,
int32_t value) const {
(void)value;
switch (index) {
// Note: Bitmap memory must be accessible even after function returned.
// It can be assumed that caller will have copied or used the bitmap
// before the next call to getParameterBmpValue
// Note: Not yet implemented upstream
default:
break;
}
return nullptr;
}
inline void NoteOn(uint8_t note, uint8_t velocity) {
note_ = note;
GateOn(velocity);
}
inline void NoteOff(uint8_t note) {
(void)note;
GateOff();
}
inline void GateOn(uint8_t velocity) {
amp_ = 1. / 127 * velocity;
gate_ += 1;
osc.phaseReset(0.0f);
}
inline void GateOff() {
if (gate_ > 0 ) {
gate_ -= 1;
}
}
inline void AllNoteOff() {}
inline void PitchBend(uint16_t bend) { (void)bend; }
inline void ChannelPressure(uint8_t pressure) { (void)pressure; }
inline void Aftertouch(uint8_t note, uint8_t aftertouch) {
(void)note;
(void)aftertouch;
}
inline void LoadPreset(uint8_t idx) { (void)idx; }
inline uint8_t getPresetIndex() const { return 0; }
/*===========================================================================*/
/* Static Members. */
/*===========================================================================*/
static inline const char * getPresetName(uint8_t idx) {
(void)idx;
// Note: String memory must be accessible even after function returned.
// It can be assumed that caller will have copied or used the string
// before the next call to getPresetName
return nullptr;
}
private:
/*===========================================================================*/
/* Private Member Variables. */
/*===========================================================================*/
std::atomic_uint_fast32_t flags_;
int32_t p_[24];
maxiEnv pitchEnvelope_;
maxiEnv ampEnvelope_;
// maxiEnvGen pitchEnvelope_;
// maxiEnvGen ampEnvelope_;
maxiOsc osc;
// saturation
maxiBiquad eq;
maxiNonlinearity sat;
float gain_;
int wave_;
int shape_;
int eqShape_;
float eqGain_;
double eqCutoff_;
float eqResonance_;
// maxiPolyBLEP osc;
maxiOsc noise;
int32_t note_;
float amp_;
uint32_t gate_;
float egPitch_;
float balance_; // 0 = osc1 only, 1= osc2 only
/*===========================================================================*/
/* Private Methods. */
/*===========================================================================*/
/*===========================================================================*/
/* Constants. */
/*===========================================================================*/
const char *WaveStr[4] = {
"Sine",
"Tri",
"Square",
"Saw"
};
const char *EqShapeStr[3] = {
"Peak",
"LowSh",
"HighSh"
};
const char *ShapeStr[3] = {
"Soft",
"Hard",
"Atan",
};
};