-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSynthVoice.h
499 lines (454 loc) · 11.4 KB
/
SynthVoice.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
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
494
495
496
497
498
499
#ifndef SynthVoice_h
#define SynthVoice_h
#include "FloatWaveTableOsc.h"
#include "ADSR.h"
#include "LowPass.h"
class SynthVoice {
public:
SynthVoice()
{
this->sampleRate = 8000;
this->modulation = 0;
this->pwm = 0.5;
this->vol1 = 1.0;
this->vol2 = 1.0;
this->fmod1 = 1.0;
this->fmod2 = 1.0;
this->fmod3 = 0.0;
this->ffreq = 1.0;
this->fq = 0.1;
this->wt1_idx = 0;
this->wt2_idx = 0;
lowpass.SetParameters(ffreq, fq);
}
SynthVoice(float sampleRate) {
this->sampleRate = sampleRate;
this->modulation = 0;
this->pwm = 0.5;
this->fmod1 = 1.0;
this->fmod2 = 1.0;
this->fmod3 = 0.0;
this->ffreq = 1.0;
this->fq = 0.1;
wt1_idx = 0;
wt2_idx = 0;
lowpass.SetParameters(ffreq, fq);
}
~SynthVoice(void) {
}
void SetSampleRate(float newSampleRate)
{
this->sampleRate = newSampleRate;
}
void MidiNoteOn(int note, int vel)
{
//float f = pow(octave_factor,(note*1.0-69.0)/tet)*tune;
float f = pow(octave_factor,(note*1.0-(tet*5.75))/tet)*tune;
velocity = vel/127.0;
freq1 = f;
freq2 = f;
osc[0].SetFrequency(freq1,sampleRate);
osc[1].SetFrequency(freq2,sampleRate);
adsr[0].Gate(1);
adsr[1].Gate(1);
osc[0].ResetPhase();
osc[1].ResetPhase();
}
void MidiChangeNote(int note, int vel)
{
//float f = pow(octave_factor,(newNote*1.0-69.0)/tet)*tune;
float f = pow(octave_factor,(note*1.0-(tet*5.75))/tet)*tune;
velocity = vel/127.0;
freq1 = f;
freq2 = f;
osc[0].SetFrequency(freq1,sampleRate);
osc[1].SetFrequency(freq2,sampleRate);
}
void Lfo1SetFrequency(float frequency)
{
lfo[0].SetFrequency(frequency,sampleRate/32);
}
void Lfo2SetFrequency(float frequency)
{
lfo[1].SetFrequency(frequency,sampleRate/32);
}
void MidiNoteOff()
{
adsr[0].Gate(0);
adsr[1].Gate(0);
}
void AddOsc1WaveTable(int len, float *waveTableIn)
{
osc[0].AddWaveTable(len,waveTableIn);
}
void AddOsc1SharedWaveTable(int len, float *waveTableIn)
{
osc[0].AddSharedWaveTable(len, waveTableIn);
}
void AddOsc2WaveTable(int len, float *waveTableIn)
{
osc[1].AddWaveTable(len,waveTableIn);
}
void AddOsc2SharedWaveTable(int len, float *waveTableIn)
{
osc[1].AddSharedWaveTable(len, waveTableIn);
}
void AddLfo1SharedWaveTable(int len, float *waveTableIn)
{
lfo[0].AddSharedWaveTable(len,waveTableIn);
}
void AddLfo2SharedWaveTable(int len, float *waveTableIn)
{
lfo[1].AddSharedWaveTable(len,waveTableIn);
}
void SetOsc1ADSR(uint8_t a, float d,float s,float r)
{
adsr[0].SetADSR(a,d,s,r);
}
void SetOsc2ADSR(uint8_t a, float d, float s, float r)
{
adsr[1].SetADSR(a,d,s,r);
}
void SetFmod1(uint8_t fmod)
{
this->fmod1 = fmod/64;
}
void SetFmod2(uint8_t fmod)
{
this->fmod2 = fmod/64;
}
void SetFmod3(uint8_t fmod)
{
this->fmod3 = fmod/64;
}
void noteSpread( int note, int spread)
{
spread = spread-63;
float factor = pow(2.0,1.0/12.0)/100.0;
float bendfreq1 = freq1+freq1*factor*((float)spread/63.0);
float bendfreq2 = freq2-freq2*factor*((float)spread/63.0);
osc[0].SetFrequency(bendfreq1,sampleRate);
osc[1].SetFrequency(bendfreq2,sampleRate);
}
void SetOctaveFactor(float factor)
{
this->octave_factor = factor;
}
void MidiBend(int bend)
{
//AudioNoInterrupts();
float factor = ((bend - 8192.0)/8192.0);
float mul = pow(octave_factor,(factor*tet)/tet);
float bendfreq1 = freq1*mul;
float bendfreq2 = freq2*mul;
osc[0].SetFrequency(bendfreq1,sampleRate);
osc[1].SetFrequency(bendfreq2,sampleRate);
//AudioInterrupts();
}
void ControlChange(uint8_t channel, uint8_t control, uint8_t value)
{
//AudioNoInterrupts();
switch(control)
{
case 1: //Modulation
MidiMod(value);
break;
case 16: //Osc1Wave
MidiOsc1Wave(value);
case 17: //Osc2Wave
MidiOsc2Wave(value);
break;
case 18: //osc1 attack
adsr[0].SetAttackMidi(value);
break;
case 19: //osc1 decay
adsr[0].SetDecayMidi(value);
break;
case 20: //osc1 sustain
adsr[0].SetSustainMidi(value);
break;
case 21: //osc1 release
adsr[0].SetReleaseMidi(value);
break;
case 22: //osc2 attack
adsr[1].SetAttackMidi(value);
break;
case 23: //osc2 decay
adsr[1].SetDecayMidi(value);
break;
case 24: //osc2 sustain
adsr[1].SetSustainMidi(value);
break;
case 25: //osc2 release
adsr[1].SetReleaseMidi(value);
break;
case 26: //filter freq
lowpass.SetParameters(value*(1.0/127.0),lowpass.GetRes());
break;
case 27: //filter res
lowpass.SetParameters(lowpass.GetFreq(),value*(1.0/127.0));
break;
case 28: //PWM
MidiPwm(value);
break;
case 71: //OSC 1 waveform
if (value>=0 && value <= 64)
{
int wave = getMidiOsc1Wave();
int v = (int)value;
int newval = wave + v;
if (newval > 255)
{
newval = newval-255;
}
MidiOsc1Wave(newval);
} else if (value > 64 && value <=127)
{
int wave = getMidiOsc1Wave();
int v = (int)value;
int newval = wave - (128-v);
if (newval < 0)
{
newval = 256+ newval;
}
MidiOsc1Wave(newval);
}
break;
case 72: //OSC 1 volume
if (value < 64)
{
int newval = GetOsc1Volume() + value;
if (newval > 127)
{
newval = 127;
}
SetOsc1Volume(newval);
}
else if (value > 64)
{
int newval = GetOsc1Volume() - (128 - (int)value);
if (newval < 0)
{
newval = 0;
}
SetOsc1Volume(newval);
}
break;
case 73: //OSC 1 phase offset
if (value < 64)
{
int newval = GetOsc1PhaseOffset() + value;
if (newval > 127)
{
newval = 127;
}
SetOsc1PhaseOffset(newval);
}
else if (value > 64)
{
int newval = GetOsc1PhaseOffset() - (128 - (int)value);
if (newval < 0)
{
newval = 0;
}
SetOsc1PhaseOffset(newval);
}
break;
case 75: //OSC 2 waveform
if (value < 64)
{
int newval = getMidiOsc2Wave() + value;
if (newval > 255)
{
newval = newval - 255;
}
MidiOsc2Wave(newval);
}
else if (value > 64)
{
int newval = getMidiOsc2Wave() - (128-value);
if (newval < 0)
{
newval = 256 + newval;
}
MidiOsc2Wave(newval);
}
break;
case 76: //OSC 2 volume
if (value < 64)
{
int newval = GetOsc2Volume() + value;
if (newval > 127)
{
newval = 127;
}
SetOsc2Volume(newval);
}
else if (value > 64)
{
int newval = GetOsc2Volume() - (128 - value);
if (newval < 0)
{
newval = 0;
}
SetOsc2Volume(newval);
}
break;
case 77: //OSC 2 phase
if (value < 64)
{
int newval = GetOsc2PhaseOffset() + value;
if (newval > 127)
{
newval = 127;
}
SetOsc2PhaseOffset(newval);
}
else if (value > 64)
{
int newval = GetOsc2PhaseOffset() - (128 - value);
if (newval < 0)
{
newval = 0;
}
SetOsc2PhaseOffset(newval);
}
break;
}
//AudioInterrupts();
}
void MidiMod(uint8_t newmod)
{
modulation = newmod/127.0;
fmod1 = 1.0-0.1*modulation/127.0;
fmod2 = 1.0-0.1*modulation/127.0;
fmod3 = 0.5*modulation;
}
void MidiPwm(uint8_t newmod)
{
pwm = newmod/127.0;
if(newmod == 0)
{
osc[0].SetPhaseOffset(0);
osc[1].SetPhaseOffset(0);
}
else
{
osc[0].SetPhaseOffset(pwm);
osc[1].SetPhaseOffset(pwm);
}
}
int GetOsc1WaveTableCount()
{
return osc[0].GetWaveTableCount();
}
int GetOsc2WaveTableCount()
{
return osc[1].GetWaveTableCount();
}
void SetOsc1PhaseOffset(uint8_t newphase)
{
osc[0].SetPhaseOffset(newphase/127.0);
}
uint8_t GetOsc1PhaseOffset()
{
return (uint8_t)(osc[0].GetPhaseOffset() * 127.0);
}
void SetOsc2PhaseOffset(uint8_t newphase)
{
osc[1].SetPhaseOffset(newphase/127.0);
}
uint8_t GetOsc2PhaseOffset()
{
return (uint8_t)(osc[1].GetPhaseOffset() * 127.0);
}
void SetOsc1Volume(uint8_t volume)
{
vol1 = ((float)volume)/127.0;
}
uint8_t GetOsc1Volume()
{
return (uint8_t)(vol1*127.0);
}
void SetOsc2Volume(uint8_t volume)
{
vol2 = ((float)volume)/127.0;
}
uint8_t GetOsc2Volume()
{
return (uint8_t)(vol2*127.0);
}
void MidiOsc1Wave(uint8_t newwave)
{
osc[0].SetWaveTable(newwave);
wt1_idx = newwave;
}
int getMidiOsc1Wave()
{
return wt1_idx;
}
void MidiOsc2Wave(uint8_t newwave)
{
osc[1].SetWaveTable(newwave);
wt2_idx = newwave;
}
int getMidiOsc2Wave()
{
return wt2_idx;
}
void SetFilterParameters(uint8_t filter_freq, uint8_t filter_q)
{
lowpass.SetParameters(filter_freq/127.0,filter_q/127.0);
}
float Process()
{
if(modulation<=0.01)
{
return 0.5*(lowpass.Process(velocity*adsr[0].Process()*vol1*osc[0].Process()*fmod1+velocity*adsr[1].Process()*vol2*osc[1].Process()*fmod2));
}
else
{
return 0.5*lowpass.Process((velocity*adsr[0].Process()*vol1*osc[0].Process()*fmod1) + (velocity*adsr[1].Process()*vol2*osc[1].Process()*fmod2) + (velocity*(adsr[0].Process()*vol1*osc[0].Process()*vol2*osc[1].Process()*fmod3)));
}
}
bool IsPlaying()
{
if(adsr[0].GetState()==ADSR::envState::env_idle && adsr[0].GetState()==ADSR::envState::env_idle)
{
return false;
}
return true;
}
void SetTet(float tet)
{
this->tet = tet;
}
void SetTune(float tune)
{
this->tune = tune;
}
protected:
FloatWaveTableOsc osc[2];
ADSR adsr[2];
FloatWaveTableOsc lfo[2];
float vol1;
float vol2;
float sampleRate;
float freq1;
float freq2;
float velocity;
float modulation;
float pwm;
float fmod1;
float fmod2;
float fmod3;
float ffreq;
float fq;
float octave_factor=2.0;
LowPass lowpass;
uint8_t wt1_idx;
uint8_t wt2_idx;
float tet = 12.0;
float tune = 440.0;
};
#endif