-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynthesizer.cpp
More file actions
332 lines (286 loc) · 6.54 KB
/
Synthesizer.cpp
File metadata and controls
332 lines (286 loc) · 6.54 KB
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
/*
* Synthesizer.cpp
*
* Created on: 04.03.2015
* Author: mem
*/
#include "Synthesizer.h"
Synthesizer::Synthesizer()
{
preset = new synth_preset_t;
config = new synth_configuration_t;
osc[0] = &osc1;
osc[1] = &osc2;
osc[2] = &osc3;
currentFreq = 300;
freqOffset = 0;
// make connections
osc12sum = new AudioConnection(osc1, 0, oscSum, 0);
osc22sum = new AudioConnection(osc2, 0, oscSum, 1);
osc32sum = new AudioConnection(osc3, 0, oscSum, 2);
oscSum2flt1 = new AudioConnection(oscSum, 0, flt1, 0);
oscSum2serParMixer = new AudioConnection(oscSum, 0, serParMixer, 0);
flt12serParMixer = new AudioConnection(flt1, 1, serParMixer, 1);
flt12fltSum = new AudioConnection(flt1, 1, fltSum, 0);
serParMixer2flt2 = new AudioConnection(serParMixer, 0, flt2, 0);
flt22fltSum = new AudioConnection(flt2, 0, fltSum, 1);
fltSum2fltDryWet = new AudioConnection(fltSum, 0, fltDryWet, 0);
oscSum2fltDryWet = new AudioConnection(oscSum, 0, fltDryWet, 1);
fltDryWet2outL = new AudioConnection(fltDryWet, 0, out, 0);
fltDryWet2outR = new AudioConnection(fltDryWet, 0, out, 1);
setFilterSerParCrossfade(1.f);
AudioMemory(20);
}
Synthesizer::~Synthesizer()
{
}
void Synthesizer::setEnabled(bool en)
{
config->enabled = en;
if( en )
enable(config->masterGain);
else
gotoSleep();
/*if(! en)
{
AudioNoInterrupts();
//audioShield.muteHeadphone();
//audioShield.volume(0.f);
AudioInterrupts();
}*/
}
void Synthesizer::enable(float gain)
{
audioShield.enable();
audioShield.volume(gain);
AudioProcessorUsageMaxReset();
AudioMemoryUsageMaxReset();
}
void Synthesizer::gotoSleep()
{
audioShield.volume(0.0);
audioShield.disable();
}
void Synthesizer::disconnect()
{
//TODO: uncomment this!!!
AudioNoInterrupts();
//osc12sum->disconnect();
AudioInterrupts();
}
void Synthesizer::reconnect()
{
//TODO: uncomment this!!!
AudioNoInterrupts();
//osc12sum->connect(osc1, 0, oscSum, 0);
AudioInterrupts();
}
void Synthesizer::setMasterGain(float g)
{
audioShield.volume(g);
config->masterGain = g;
}
void Synthesizer::setBaseFrequency(float f)
{
config->baseFreq = f;
updateFrequency();
}
void Synthesizer::setSemiRange(uint8_t range)
{
config->semiRange = range;
updateFrequency();
}
void Synthesizer::setOffset(control_type_e control, float o)
{
if( ! config->enabled )
return;
switch( control )
{
case CT_PITCH:
{
float offset = o * config->semiRange;
int semis = (int) offset;
int cents = (int) (offset - semis) * 100;
float freqOffset = config->baseFreq;
if( semis < 0 )
{
semis *= -1;
cents *= -1;
if( semis > 0 ) freqOffset *= semif_minus[semis - 1];
if( cents > 0 ) freqOffset *= centf_minus[cents - 1];
}
else
{
if( semis > 0 ) freqOffset *= semif_plus[semis - 1];
if( cents > 0 ) freqOffset *= centf_plus[cents - 1];
}
updateFrequency();
break;
}
case CT_VOLUME:
{
break;
}
}
}
void Synthesizer::updateFrequency()
{
AudioNoInterrupts();
currentFreq = config->baseFreq + freqOffset;
for(int i = 0; i < 3; i++)
{
osc[i]->frequency(getOSCFrequency(i, currentFreq));
}
AudioInterrupts();
}
void Synthesizer::setPreset(synth_preset_t *p)
{
// FILTER SECTION
setFilterDryWet(p->filter.dryWet);
setFilterSerParCrossfade(p->filter.serParCF);
// OSC SECTION
for(int i = 0; i < 3; i++)
{
setOSCEnabled(i, p->osc[i].enabled);
setOSCAmplitude(i, p->osc[i].amplitude);
setOSCWaveform(i, p->osc[i].waveform);
setOSCWavetablePosition(i, p->osc[i].wavetable_position);
setOSCDutycycle(i, p->osc[i].duty);
setOSCSemitones(i, p->osc[i].semitones);
setOSCCents(i, p->osc[i].cents);
}
// Filter section
preset = p;
}
void Synthesizer::setConfiguration(synth_configuration_t *config)
{
//this->gotoSleep();
this->config = config;
this->setPreset(&config->preset);
if(config->enabled)
{
enable(config->masterGain);
}
}
void Synthesizer::noteOn()
{
AudioNoInterrupts();
for(int i = 0; i < 3; i++)
{
osc[i]->begin(preset->osc[i].amplitude, getOSCFrequency(i, currentFreq), preset->osc[i].waveform);
}
AudioInterrupts();
}
float Synthesizer::getOSCFrequency(int i, float baseFreq)
{
int semi,cent;
float f;
semi = preset->osc[i].semitones;
cent = preset->osc[i].cents;
if(semi == 0)
{
f = baseFreq;
}
else if(semi > 0)
{
f = baseFreq * semif_plus[semi-1];
}
else
{
semi *= -1;
f = baseFreq * semif_minus[semi-1];
}
if(cent > 0)
{
f *= centf_plus[cent-1];
}
else if(cent < 0)
{
cent *= -1;
f *= centf_minus[cent-1];
}
return f;
}
void Synthesizer::noteOff()
{
AudioNoInterrupts();
for(int i = 0; i < 3; i++)
{
osc[i]->amplitude(0);
}
AudioInterrupts();
}
// OSC
void Synthesizer::setOSCEnabled(int index, bool en)
{
preset->osc[index].enabled = en;
osc[index]->setEnabled(en);
}
void Synthesizer::setOSCAmplitude(int index, float amp)
{
preset->osc[index].amplitude = amp;
osc[index]->amplitude(amp);
}
void Synthesizer::setOSCCents(int index, int8_t i)
{
preset->osc[index].cents = i;
osc[index]->frequency(getOSCFrequency(index, currentFreq));
}
void Synthesizer::setOSCDutycycle(int index, float f)
{
preset->osc[index].duty = f;
osc[index]->pulseWidth(f);
}
void Synthesizer::setOSCSemitones(int index, int s)
{
preset->osc[index].semitones = s;
osc[index]->frequency(getOSCFrequency(index, currentFreq));
}
void Synthesizer::setOSCWaveform(int index, int wave)
{
preset->osc[index].waveform = wave;
noteOn();
}
void Synthesizer::setOSCWavetablePosition(int index, float wtpos)
{
preset->osc[index].wavetable_position = wtpos;
osc[index]->phase((float) wtpos * 3.6f);
}
// FILTER
void Synthesizer::setFilterSerParCrossfade(float f)
{
preset->filter.serParCF = f;
float coeff = 1.f - f;
serParMixer.gain(0, f);
serParMixer.gain(1, coeff);
fltSum.gain(0, f);
}
void Synthesizer::setFilterDryWet(float f)
{
preset->filter.dryWet = f;
float coeff = 1.f - f;
fltDryWet.gain(0, f);
fltDryWet.gain(1, coeff);
}
void Synthesizer::setFilter1Enabled(bool en)
{
preset->filter.flt1.enabled = en;
flt1.setEnabled(en);
}
void Synthesizer::setFilter1Freq(float f)
{
preset->filter.flt1.frequency = f;
flt1.frequency(f);
}
void Synthesizer::setFilter1Reso(float f)
{
preset->filter.flt1.resonance = f;
flt1.resonance(f);
}
void Synthesizer::setFilter2Freq(float f)
{
}
OSCWaveform * Synthesizer::getOSC(int index)
{
return osc[index];
}