-
Notifications
You must be signed in to change notification settings - Fork 10
/
synthvoice.ino
491 lines (438 loc) · 14.9 KB
/
synthvoice.ino
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
#include "synthvoice.h"
void SynthVoice::Init() {
_envMod = 0.5f;
_accentLevel = 0.5f;
_cutoff = 0.2f; // 0..1 normalized freq range. Keep in mind that EnvMod set to max practically floats this range
_filter_freq = linToExp(_cutoff, 0.0f, 1.0f, MIN_CUTOFF_FREQ, MAX_CUTOFF_FREQ);
_reso = 0.4f;
_gain = 0.0f; // values >1 will distort sound
_drive = 0.0f;
_eAmpEnvState = ENV_IDLE;
_eFilterEnvState = ENV_IDLE;
// midiNotes[0] = -1;
// midiNotes[1] = -1;
_midiNote = 69;
_currentStep = 1.0f;
_targetStep = 1.0f;
_tuning = 1.0f;
_pitchbend = 1.0f;
_deltaStep = 0.0f;
_slideMs = 60.0f;
_phaze = 0.0f;
mva1.n = 0 ;
_pan = 0.5;
// parameters of envelopes
_ampEnvPosition = 0.0;
_filterEnvPosition = 0.0;
_ampAttackMs = 0.5;
_ampDecayMs = 1230.0;
_ampReleaseMs = 1.0;
_ampAccentReleaseMs = 50.0f;
// _ampEnvAttackStep = 15.0;
// _ampEnvDecayStep = 1.0;
// _ampEnvReleaseStep = 15.0;
_filterAttackMs = 3.0;
_filterDecayMs = 200.0;
//_filterEnvAttackStep = 15.0;
//_filterEnvDecayStep = 1.0;
Distortion.Init();
Drive.Init();
Filter.Init((float)SAMPLE_RATE);
#if FILTER_TYPE == 2
// Filter.SetMode(TeeBeeFilter::LP_18);
Filter.SetMode(TeeBeeFilter::TB_303);
#endif
highpass1.setMode(OnePoleFilter::HIGHPASS);
highpass1.setCutoff(44.486f);
highpass2.setMode(OnePoleFilter::HIGHPASS);
highpass2.setCutoff(24.167f);
allpass.setMode(OnePoleFilter::ALLPASS);
allpass.setCutoff(14.008f);
ampDeclicker.setMode(BiquadFilter::LOWPASS12);
ampDeclicker.setGain( amp2dB(sqrt(0.5f)) );
ampDeclicker.setFrequency(200.0f);
filtDeclicker.setMode(BiquadFilter::LOWPASS12);
filtDeclicker.setGain( amp2dB(sqrt(0.5f)) );
filtDeclicker.setFrequency(200.0f);
notch.setMode(BiquadFilter::BANDREJECT);
notch.setFrequency(7.5164f);
notch.setBandwidth(4.7f);
}
inline float SynthVoice::getSample() {
float samp = 0.0f, filtEnv = 0.0f, ampEnv = 0.0f, final_cut = 0.0f;
filtEnv = GetFilterEnv();
ampEnv = GetAmpEnv();
if (_eAmpEnvState != ENV_IDLE) {
// samp = (float)((1.0f - _waveMix) * lookupTable(*(tables[_waveBase]), _phaze)) + (float)(_waveMix * lookupTable(*(tables[_waveBase+1]), _phaze)) ; // lookup and blend waveforms
samp = (float)((1.0f - _waveMix) * lookupTable(exp_square_tbl, _phaze)) + (float)(_waveMix * lookupTable(saw_tbl, _phaze)) ; // lookup and blend waveforms
} else {
samp = 0.0f;
}
final_cut = (float)_filter_freq * ( (float)_envMod * ((float)filtEnv - 0.2f) + 1.3f * (float)_accentation + 1.0f );
final_cut = filtDeclicker.getSample( final_cut );
Filter.SetCutoff( final_cut );
samp = highpass1.getSample(samp); // pre-filter highpass, following open303
samp = allpass.getSample(samp); // phase correction, following open303
samp = Filter.Process(samp); // main filter
samp = highpass2.getSample(samp); // post-filtering, following open303
samp = notch.getSample(samp); // post-filtering, following open303
samp = Drive.Process(samp); // overdrive
samp = Distortion.Process(samp); // distortion
samp *= ampEnv; // amp envelope
_compens = _volume * 8.0f * _fx_compens ; // * _flt_compens;
_compens = ampDeclicker.getSample(_compens);
samp *= _compens;
if ((_slide || _portamento) && _deltaStep != 0.0f) { // portamento / slide processing
if (fabs(_effectiveStep - _currentStep) >= fabs(_deltaStep)) {
_currentStep += _deltaStep;
} else {
_currentStep = _effectiveStep;
_deltaStep = 0.0f;
}
}
// Increment and wrap phase
_phaze += _currentStep;
/*
* // this is more accurate classical approach, but it gives quite early audible aliasing when not using band-limited samples
if ( _phaze >= TABLE_SIZE) {
_phaze -= TABLE_SIZE ;
*/
// this is less accurate in terms of pitch, especially at higher notes, but at this price you have quite no aliasing, as phase reset produces no moire
if ( _phaze >= TABLE_SIZE) {
if (_wave_cnt == 3) { // we drop the phase every 4 periods
_wave_cnt = 0;
_phaze = 0.0f; // we reset the phase, so aliasing will be present in the form of lower octave tones which is less annoying
} else {
_wave_cnt++;
_phaze -= TABLE_SIZE ;
}
}
/*
// this is less accurate in terms of pitch, especially at higher notes, but at this price you have quite no aliasing, as phase reset produces no moire
if ( _phaze >= TABLE_SIZE) {
_phaze = 2.0f * (float)( (int)(0.5f * (_phaze - (float)TABLE_SIZE)) );
DEBF("%0.5f\r\n", _phaze);
}*/
//synth_buf[_index][i] = fast_shape(samp); // mono limitter
return samp;
}
inline void SynthVoice::SetCutoff(float lvl) {
_cutoff = lvl;
_filter_freq = knobMap( lvl, MIN_CUTOFF_FREQ, MAX_CUTOFF_FREQ);
#ifdef DEBUG_SYNTH
DEBF("Synth %d cutoff=%0.3f freq=%0.3f\r\n" , _index, _cutoff, _filter_freq);
#endif
}
inline void SynthVoice::PitchBend(int number) {
//
float semi = ((((float)number + 8191.5f) * (float)TWO_DIV_16383 ) - 1.0f ) * 12.0f;
_pitchbend = powf(1.059463f, semi);
_effectiveStep = _targetStep * _tuning * _pitchbend;
DEBUG(_effectiveStep);
}
inline void SynthVoice::ParseCC(uint8_t cc_number , uint8_t cc_value) {
float tmp = 0.0f;
switch (cc_number) {
case CC_303_PORTATIME:
_slideMs = (float)cc_value;
break;
case CC_303_VOLUME:
_volume = (float)cc_value * MIDI_NORM;
break;
case CC_303_PAN:
_pan = (float)cc_value * MIDI_NORM;
break;
case CC_303_PORTAMENTO:
_portamento = (cc_value >= 64);
break;
case CC_303_WAVEFORM:
/*
// actually we can gradually switch between several waveforms, basing on the CC value, blending neighbour two waveforms
_waveBase = (uint8_t)(((float)cc_value * 2.99999f * MIDI_NORM)) ; // 0, 1, 2 range
DEBF("base %d\r\n", _waveBase );
_waveMix = ((float)cc_value - (float)(_waveBase*42.33333f)) * MIDI_NORM * 3.0f;
DEBF("mix %0.5f\r\n", _waveMix );*/
_waveMix = (float)cc_value * MIDI_NORM;
break;
case CC_303_RESO:
_reso = cc_value * MIDI_NORM ;
_flt_compens = one_div( bilinearLookup(norm1_tbl, _cutoff * 127.0f, cc_value ));
SetReso(_reso);
break;
case CC_303_DECAY: // Env release
tmp = (float)cc_value * MIDI_NORM;
_filterDecayMs = knobMap(tmp, 15.0f, 5000.0f);
_ampDecayMs = knobMap(tmp, 15.0f, 7500.0f);
break;
case CC_303_ATTACK: // Env attack
tmp = (float)cc_value * MIDI_NORM;
_filterAttackMs = knobMap(tmp, 3.0f, 500.0f);
_ampAttackMs = knobMap(tmp, 3.0f, 700.0f);
break;
case CC_303_CUTOFF:
_cutoff = (float)cc_value * MIDI_NORM;
_flt_compens = one_div( bilinearLookup(norm1_tbl, cc_value, _reso * 127.0f));
SetCutoff(_cutoff);
break;
case CC_303_DELAY_SEND:
_sendDelay = (float)cc_value * MIDI_NORM;
break;
case CC_303_REVERB_SEND:
_sendReverb = (float)cc_value * MIDI_NORM;
break;
case CC_303_ENVMOD_LVL:
_envMod = (float)cc_value * MIDI_NORM;
break;
case CC_303_ACCENT_LVL:
_accentLevel = (float)cc_value * MIDI_NORM;
break;
case CC_303_DISTORTION:
_gain = (float)cc_value * MIDI_NORM ;
_fx_compens = one_div( bilinearLookup(norm2_tbl, _drive * 127.0f, cc_value));
SetDistortionLevel(_gain);
break;
case CC_303_OVERDRIVE:
_drive = (float)cc_value * MIDI_NORM ;
_fx_compens = one_div( bilinearLookup(norm2_tbl, cc_value, _gain * 127.0f));
SetOverdriveLevel(_drive);
break;
case CC_303_SATURATOR:
_saturator = (float)cc_value * MIDI_NORM;
Filter.SetDrive(_saturator);
break;
case CC_303_TUNING:
_tuning = tuning[cc_value];
_effectiveStep = _targetStep * _tuning * _pitchbend;
break;
}
}
float SynthVoice::GetAmpEnv() {
switch (_eAmpEnvState) {
case ENV_INIT:
_k_acc = (1.0f + 0.3f * _accentation);
_ampEnvPosition = 0.0f;
_ampEnvAttackStep = _msToSteps * one_div( _ampAttackMs + 0.0001f);
_ampEnvDecayStep = _msToSteps * one_div(_ampDecayMs + 0.0001f);
if (_accent) {
//_ampEnvDecayStep *= 5.0f;
_ampEnvReleaseStep = _msToSteps * one_div(_ampAccentReleaseMs + 0.0001f);
} else {
_ampEnvReleaseStep = _msToSteps * one_div(_ampReleaseMs + 0.0001f);
}
_eAmpEnvState = ENV_ATTACK;
_ampEnvVal = (-exp_tbl[ 0 ] + 1.0f) * 0.5f;
break;
case ENV_ATTACK:
_ampEnvPosition += _ampEnvAttackStep;
if (_ampEnvPosition >= TABLE_SIZE) {
_eAmpEnvState = ENV_DECAY;
_ampEnvPosition = 0;
_ampEnvVal = (-exp_tbl[ TABLE_SIZE - 1 ] + 1.0f) * 0.5f * _k_acc;
} else {
_ampEnvVal = (-lookupTable(exp_tbl, _ampEnvPosition ) + 1.0f) * 0.5f * _k_acc;
if (_pass_val > _ampEnvVal) _ampEnvVal = _pass_val;
}
_pass_val = _ampEnvVal;
break;
case ENV_DECAY:
_ampEnvPosition += _ampEnvDecayStep;
if (_ampEnvPosition >= TABLE_SIZE) {
_eAmpEnvState = ENV_SUSTAIN;
_ampEnvPosition = 0;
_ampEnvVal = _sust_level;
} else {
_ampEnvVal = _sust_level + (1.0f - _sust_level) * (lookupTable(exp_tbl, _ampEnvPosition) + 1.0f) * 0.5f * _k_acc;
}
_pass_val = _ampEnvVal;
break;
case ENV_SUSTAIN:
_ampEnvVal = _sust_level; // asuming sustain to be endless
_pass_val = _ampEnvVal;
_ampEnvPosition = 0;
break;
case ENV_RELEASE:
if (_ampEnvPosition >= TABLE_SIZE) {
_eAmpEnvState = ENV_IDLE;
_ampEnvPosition = 0;
_ampEnvVal = 0.0f;
} else {
if (_ampEnvPosition <= _ampEnvReleaseStep) _release_lvl = _pass_val;
_ampEnvVal = _release_lvl * (lookupTable(exp_tbl, _ampEnvPosition) + 1.0f) * 0.5f * _k_acc;
}
_ampEnvPosition += _ampEnvReleaseStep;
_pass_val = _ampEnvVal;
break;
case ENV_IDLE:
_ampEnvVal = 0.0f;
break;
default:
_ampEnvVal = 0.0f;
}
if (_accent) {
}
return _ampEnvVal;
}
inline float SynthVoice::GetFilterEnv() {
switch (_eFilterEnvState) {
case ENV_INIT:
// k_acc = (1.0f + 0.45f * _accentation);
_offset = max(_filterEnvVal, _offset);
_filterEnvPosition = 0.0f;
_filterEnvAttackStep = _msToSteps * one_div(_filterAttackMs + 0.0001f);
_filterEnvDecayStep = _msToSteps * one_div(_filterDecayMs + 0.0001f);
if (_accent) {
// _filterEnvAttackStep *= 1.4f;
_filterEnvDecayStep *= 5.0f;
// _reso += 0.2f;
}
_eFilterEnvState = ENV_ATTACK;
_filterEnvVal = (-exp_tbl[ 0 ] + 1.0f) * 0.5f ;
break;
case ENV_ATTACK:
if (_filterEnvPosition >= (float)TABLE_SIZE) {
_eFilterEnvState = ENV_DECAY;
_filterEnvPosition = 0.0f;
_filterEnvVal = (-exp_tbl[ TABLE_SIZE - 1 ] + 1.0f) * 0.5f ;
} else {
_filterEnvVal = (-lookupTable(exp_tbl, _filterEnvPosition) + 1.0f) * 0.5f ;
}
_filterEnvPosition += _filterEnvAttackStep;
break;
case ENV_DECAY:
if (_filterEnvPosition >= (float)TABLE_SIZE) {
_eFilterEnvState = ENV_IDLE; // Attack-Decay-0 envelope (?)
_filterEnvPosition = 0.0f;
_filterEnvVal = 0.0f;
} else {
_filterEnvVal = (lookupTable(exp_tbl, _filterEnvPosition) + 1.0f) * 0.5f ;
}
_filterEnvPosition += _filterEnvDecayStep;
_offset *= _offset_leak;
break;
case ENV_IDLE:
_filterEnvVal = 0.0f;
_offset *= _offset_leak;
break;
default:
_filterEnvVal = 0.0f;
}
_filterEnvVal += _offset;
return _filterEnvVal ;
}
// The following code initially written by Anton Savov,
// is taken from http://antonsavov.net/cms/projects/303andmidi.html
// Monophonic Voice Allocator (with Accent, suitable for the 303)
// "Newest" note-priority rule
// Modified version, allows multiple Notes with the same pitch
inline void SynthVoice::on_midi_noteON(uint8_t note, uint8_t velocity)
{
mva_note_on(&mva1, note, (velocity >= 80));
bool slide = (mva1.n > 1);
bool accent = (mva1.accents[0]);
note = mva1.notes[0] ;
note_on(note, slide, accent);
}
inline void SynthVoice::on_midi_noteOFF(uint8_t note, uint8_t velocity)
{
if (mva1.n == 0) {
return;
}
uint8_t tmp_note = mva1.notes[0];
uint8_t tmp_accent = mva1.accents[0];
mva_note_off(&mva1, note);
if (mva1.n > 0)
{
if (mva1.notes[0] != tmp_note)
{
bool accent = (mva1.accents[0] );
bool slide = 1;
note = mva1.notes[0];
note_on(note, slide, accent);
}
}
else {
note_off();
}
}
void SynthVoice::mva_note_on(mva_data *p, uint8_t note, uint8_t accent)
{
uint8_t s = 0;
uint8_t i = 0;
// shift all notes back
uint8_t m = p->n + 1;
m = (m > MIDI_MVA_SZ ? MIDI_MVA_SZ : m);
s = m;
i = m;
while (i > 0)
{
--s;
p->notes[i] = p->notes[s];
p->accents[i] = p->accents[s];
i = s;
}
// put the new note first
p->notes[0] = note;
p->accents[0] = accent;
// update the voice counter
p->n = m;
}
void SynthVoice::mva_note_off(mva_data *p, uint8_t note)
{
uint8_t s = 0;
// find if the note is actually in the buffer
uint8_t m = p->n;
uint8_t i = m;
while (i) // count backwards (oldest notes first)
{
--i;
if (note == p->notes[i] )
{
// found it!
if (i < (p->n - 1)) // don't shift if this was the last note..
{
// remove it now.. just shift everything after it
s = i;
while (i < m)
{
++s;
p->notes[i] = p->notes[s];
p->accents[i] = p->accents[s];
i = s;
}
}
// update the voice counter
if (m > 0) {
p->n = m - 1;
}
break;
}
}
}
void SynthVoice::mva_reset(mva_data *p)
{
p->n = 0;
}
void SynthVoice::note_on(uint8_t midiNote, bool slide, bool accent)
{
_accent = accent;
_slide = slide || _portamento;
_targetStep = midi_tbl_steps[midiNote];
_effectiveStep = _targetStep * _tuning * _pitchbend;
if (_slide) {
_deltaStep = (_effectiveStep - _currentStep) * (1000.0f * DIV_SAMPLE_RATE / _slideMs );
} else {
_currentStep = _effectiveStep;
_deltaStep = 0.0f ;
_eAmpEnvState = ENV_INIT;
_eFilterEnvState = ENV_INIT;
_phaze = 0.0f;
}
if (mva1.n == 1) {
if (_accent) _accentation = _accentLevel; else _accentation = 0.0f;
}
}
void SynthVoice::note_off()
{
_eAmpEnvState = ENV_RELEASE;
_ampEnvPosition = 0;
}