-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynthVoice.h
252 lines (238 loc) · 6.09 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
#ifndef SynthVoice_h
#define SynthVoice_h
#include "FloatWaveTableOsc.hpp"
#include "ADSR.h"
#include "LowPass.h"
#include <Arduino.h>
class SynthVoice {
public:
SynthVoice()
{
this->sampleRate = 8000;
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;
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;
lowpass.SetParameters(ffreq, fq);
}
~SynthVoice(void) {
}
void SetSampleRate(float newSampleRate)
{
this->sampleRate = newSampleRate;
}
void MidiNoteOn(uint8_t note, uint8_t vel)
{
float f = pow(2.0,(note*1.0-69.0)/12.0)*440.0;
velocity = vel/128.0;
freq1 = f;
freq2 = f;
osc[0].SetFrequency(freq1,sampleRate);
osc[1].SetFrequency(freq2,sampleRate);
adsr[0].Gate(1);
adsr[1].Gate(1);
}
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 SetOsc1ADSR(byte a, float d,float s,float r)
{
adsr[0].SetADSR(a,d,s,r);
}
void SetOsc2ADSR(byte 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 MidiBend(int bend)
{
AudioNoInterrupts();
float factor = ((bend - 8192.0)/8192.0);
float mul = pow(2.0,(factor*12.0)/12.0);
float bendfreq1 = freq1*mul;
float bendfreq2 = freq2*mul;
osc[0].SetFrequency(bendfreq1,sampleRate);
osc[1].SetFrequency(bendfreq2,sampleRate);
AudioInterrupts();
}
void ControlChange(byte channel, byte control, byte 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;
}
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);
}
void SetOsc2PhaseOffset(uint8_t newphase)
{
osc[1].SetPhaseOffset(newphase/127.0);
}
void MidiOsc1Wave(uint8_t newwave)
{
osc[0].SetWaveTable(newwave);
wt1_idx = newwave;
}
void MidiOsc2Wave(uint8_t newwave)
{
osc[1].SetWaveTable(newwave);
wt2_idx = newwave;
}
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 (lowpass.Process(velocity*adsr[0].Process()*osc[0].Process()*fmod1+velocity*adsr[1].Process()*osc[1].Process()*fmod2));
}
else
{
return lowpass.Process((velocity*adsr[0].Process()*osc[0].Process()*fmod1) + (velocity*adsr[1].Process()*osc[1].Process()*fmod2) + (velocity*(adsr[0].Process()*osc[0].Process()*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;
}
protected:
FloatWaveTableOsc osc[2];
ADSR adsr[2];
float sampleRate;
float freq1;
float freq2;
float velocity;
float modulation;
float pwm;
float fmod1;
float fmod2;
float fmod3;
float ffreq;
float fq;
LowPass lowpass;
uint8_t wt1_idx;
uint8_t wt2_idx;
};
#endif