-
Notifications
You must be signed in to change notification settings - Fork 0
/
voice.h
512 lines (384 loc) · 11.7 KB
/
voice.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
500
501
502
503
504
505
506
507
508
509
510
511
/*
This file is part of Virtual Robot's Pine Synthesizer.
Copyright <2022> <Brian Gunnison>
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include "daisy_pod.h"
#include "daisysp.h"
#include "midimap.h"
using namespace daisy;
using namespace daisysp;
// springvoice is 92% CPU usage constant with moog filter, overload (no midi) if set to 5.
#define SPRING_VOICE_POLYPHONY 4
#define MALLET_VOICE_POLYPHONY 2 // 98% with moog filter
#define OSC_VOICE_POLYPHONY 8 // 18% cpu usage with moog filter
#define HIHAT_VOICE_POLYPHONY 2
#define FORMANT_VOICE_POLYPHONY 8
#define NOISE_VOICE_POLYPHONY 8
// we have to instantiate max
#define MAX_POLYPHONY OSC_VOICE_POLYPHONY
// ADSR settings
#define ADSR_ATTACK_MIN 0.01f
#define ADSR_ATTACK_DEFAULT 0.1f
#define ADSR_ATTACK_MAX 5.0f
#define ADSR_DECAY_MIN 0.01f
#define ADSR_DECAY_DEFAULT 0.5f
#define ADSR_DECAY_MAX 5.0f
#define ADSR_SUSTAIN_MIN 0.01f
#define ADSR_SUSTAIN_DEFAULT 0.0f
#define ADSR_SUSTAIN_MAX 1.0f
#define ADSR_RELEASE_MIN 0.01f
#define ADSR_RELEASE_DEFAULT 0.2f
#define ADSR_RELEASE_MAX 1.0f
typedef struct
{
uint8_t midiNote; // > 0 if playing
float amplitude; // start gain
}Note;
class NullVoice
{
public:
virtual void Init(DaisyPod *phw, float SR);
virtual float Process();
virtual void NoteOn(NoteOnEvent *p);
virtual void NoteOff(NoteOffEvent *p);
virtual void SetFreq(float freq);
virtual void SetCC0(uint8_t value);
virtual void SetCC1(uint8_t value);
virtual void SetCC2(uint8_t value);
virtual void SetCC3(uint8_t value);
virtual void SetCC4(uint8_t value){}
virtual void SetCC5(uint8_t value){}
virtual void ProcessParm0();
virtual void ProcessParm1();
virtual void ProcessParm2();
virtual void ProcessParm3();
virtual void ProcessParm4(){}
virtual void ProcessParm5(){}
virtual void Panic();
protected:
float sampleRate;
uint8_t polyphony;
Note notes[MAX_POLYPHONY];
DaisyPod *hw;
};
class OscVoice : public NullVoice
{
public:
void Init(DaisyPod *phw, float SR) override;
float Process() override;
void NoteOn(NoteOnEvent *p) override;
void NoteOff(NoteOffEvent *p) override;
void SetFreq(float freq) override;
void SetCC0(uint8_t value) override;
void SetCC1(uint8_t value) override;
void SetCC2(uint8_t value) override;
void SetCC3(uint8_t value) override;
void ProcessParm0() override;
void ProcessParm1() override;
void ProcessParm2() override;
void ProcessParm3() override;
void Panic() override;
private:
Oscillator synth[MAX_POLYPHONY];
Adsr adsr[MAX_POLYPHONY];
void StartNote(uint8_t i, NoteOnEvent *p);
bool ADSROn;
void SetADSRAttack(float v);
float ADSRAttack;
void SetADSRDecay(float v);
float ADSRDecay;
void SetADSRSustain(float v);
float ADSRSustain;
void SetADSRRelease(float v);
float ADSRRelease;
Parameter ADSRAttackPotParm; // sets range and plot
Parameter ADSRDecayPotParm; // sets range and plot
Parameter ADSRSustainPotParm; // sets range and plot
Parameter ADSRReleasePotParm; // sets range and plot
};
class SpringVoice : public NullVoice
{
public:
void Init(DaisyPod *phw, float SR) override;
float Process() override;
void NoteOn(NoteOnEvent *p) override;
void NoteOff(NoteOffEvent *p) override;
void SetFreq(float freq) override;
void SetCC0(uint8_t value) override;
void SetCC1(uint8_t value) override;
void SetCC2(uint8_t value) override;
void SetCC3(uint8_t value) override;
void ProcessParm0() override;
void ProcessParm1() override;
void ProcessParm2() override;
void ProcessParm3() override;
void Panic() override;
private:
StringVoice spring[MAX_POLYPHONY];
void StartNote(uint8_t i, NoteOnEvent *p);
void SetDamping(float v);
float damping;
void SetStructure(float v);
float structure;
void SetBrightness(float v);
float brightness;
void SetAccent(float v);
float accent;
Parameter DampingPotParm; // sets range and plot
Parameter StructurePotParm; // sets range and plot
Parameter BrightnessPotParm; // sets range and plot
Parameter AccentPotParm; // sets range and plot
};
class MalletVoice : public NullVoice
{
public:
void Init(DaisyPod *phw, float SR) override;
float Process() override;
virtual void NoteOn(NoteOnEvent *p) override;
virtual void NoteOff(NoteOffEvent *p) override;
virtual void SetFreq(float freq) override;
void SetCC0(uint8_t value) override;
void SetCC1(uint8_t value) override;
void SetCC2(uint8_t value) override;
void SetCC3(uint8_t value) override;
void ProcessParm0() override;
void ProcessParm1() override;
void ProcessParm2() override;
void ProcessParm3() override;
void Panic() override;
private:
ModalVoice mallet[MAX_POLYPHONY];
void StartNote(uint8_t i, NoteOnEvent *p);
void SetDamping(float v);
float damping;
void SetStructure(float v);
float structure;
void SetBrightness(float v);
float brightness;
void SetAccent(float v);
float accent;
Parameter DampingPotParm; // sets range and plot
Parameter StructurePotParm; // sets range and plot
Parameter BrightnessPotParm; // sets range and plot
Parameter AccentPotParm; // sets range and plot
};
class HiHatVoice : public NullVoice
{
public:
void Init(DaisyPod *phw, float SR) override;
float Process() override;
virtual void NoteOn(NoteOnEvent *p) override;
virtual void NoteOff(NoteOffEvent *p) override;
virtual void SetFreq(float freq) override;
void SetCC0(uint8_t value) override;
void SetCC1(uint8_t value) override;
void SetCC2(uint8_t value) override;
void SetCC3(uint8_t value) override;
void ProcessParm0() override;
void ProcessParm1() override;
void ProcessParm2() override;
void ProcessParm3() override;
void Panic() override;
private:
// or <RingModNoise> - This is much more hihat, but much less tonal
HiHat<SquareNoise> hihat[MAX_POLYPHONY];
void StartNote(uint8_t i, NoteOnEvent *p);
void SetAccentCC(uint8_t value);
float accent;
void SetSustainCC(uint8_t value); // bool
bool sustain;
void SetDecayCC(uint8_t value);
float decay;
void SetToneCC(uint8_t value);
float tone;
void SetNoisinessCC(uint8_t value);
float noisiness;
};
class FormantVoice : public NullVoice
{
public:
void Init(DaisyPod *phw, float SR) override;
float Process() override;
virtual void NoteOn(NoteOnEvent *p) override;
virtual void NoteOff(NoteOffEvent *p) override;
virtual void SetFreq(float freq) override;
void SetCC0(uint8_t value) override;
void SetCC1(uint8_t value) override;
void SetCC2(uint8_t value) override;
void SetCC3(uint8_t value) override;
void SetCC4(uint8_t value) override;
void SetCC5(uint8_t value) override;
void ProcessParm0() override;
void ProcessParm1() override;
void ProcessParm2() override;
void ProcessParm3() override;
void Panic() override;
private:
FormantOscillator formant[MAX_POLYPHONY];
Adsr adsr[MAX_POLYPHONY];
void StartNote(uint8_t i, NoteOnEvent *p);
bool ADSROn;
void SetADSRAttack(float v);
float ADSRAttack;
void SetADSRDecay(float v);
float ADSRDecay;
void SetADSRSustain(float v);
float ADSRSustain;
void SetADSRRelease(float v);
float ADSRRelease;
Parameter ADSRAttackPotParm; // sets range and plot
Parameter ADSRDecayPotParm; // sets range and plot
Parameter ADSRSustainPotParm; // sets range and plot
Parameter ADSRReleasePotParm; // sets range and plot
void SetFormantFreqCC(uint8_t value);
float formantFreq;
void SetPhaseShiftCC(uint8_t value);
float phaseShift;
};
// copied from DaisySP
class MyWhiteNoise
{
public:
/** Initializes the WhiteNoise object
*/
void Init(int32_t seed)
{
amp_ = 1.0f;
randseed_ = seed;
}
/** sets the amplitude of the noise output
*/
inline void SetAmp(float a) { amp_ = a; }
/** returns a new sample of noise in the range of -amp_ to amp_
*/
inline float Process()
{
randseed_ *= 16807;
return (randseed_ * coeff_) * amp_;
}
private:
static constexpr float coeff_ = 4.6566129e-010f;
float amp_;
int32_t randseed_;
};
// we get some tonality by setting the filter freq to the note
// noise -> filter -> filter
class NoiseFilter
{
public:
void Init(float sampleRate, int32_t seed);
float Process(float adsrLevel);
void SetAmp(float amp) { wn.SetAmp(amp); }
void SetFreq(float freq);
void SetResonance(float Res);
void SetDrive(float drive);
private:
float sampleRate;
MyWhiteNoise wn;
Svf hpFilter; // set cutoff an octave below note to prevent rumble
static constexpr uint8_t NUM_FILTERS = 3;
Svf filter[NUM_FILTERS];
float resGain; // as the resonance goes up the gain goes down
};
class NoiseVoice : public NullVoice
{
public:
void Init(DaisyPod *phw, float SR) override;
float Process() override;
void NoteOn(NoteOnEvent *p) override;
void NoteOff(NoteOffEvent *p) override;
void SetFreq(float freq) override;
void SetCC0(uint8_t value) override;
void SetCC1(uint8_t value) override;
void SetCC2(uint8_t value) override;
void SetCC3(uint8_t value) override;
void ProcessParm0() override;
void ProcessParm1() override;
void ProcessParm2() override;
void ProcessParm3() override;
void ProcessParm4() override;
void ProcessParm5() override;
void Panic() override;
private:
NoiseFilter noise[MAX_POLYPHONY];
Adsr adsr[MAX_POLYPHONY];
void StartNote(uint8_t i, NoteOnEvent *p);
void SetResonance(float v);
float resonance;
void SetDrive(float v);
float drive;
bool ADSROn;
void SetADSRAttack(float v);
float ADSRAttack;
void SetADSRDecay(float v);
float ADSRDecay;
void SetADSRSustain(float v);
float ADSRSustain;
void SetADSRRelease(float v);
float ADSRRelease;
Parameter ADSRAttackPotParm; // sets range and plot
Parameter ADSRDecayPotParm; // sets range and plot
Parameter ADSRSustainPotParm; // sets range and plot
Parameter ADSRReleasePotParm; // sets range and plot
Parameter ResonancePotParm; // sets range and plot
Parameter DrivePotParm; // sets range and plot
};
// a container for voices
class Voices : public CCMIDIMapable
{
public:
Voices() {}
void Init(DaisyPod *pod, float SR);
// 1/-1 rotates thru voices, 0 does nothing
void Select(int8_t sel);
float Process(void);
void UpdateBackGround(void);
void NoteOn(NoteOnEvent *p);
void NoteOff(NoteOffEvent *p);
void SetFreq(float freq);
void SetCC0(uint8_t value);
void SetCC1(uint8_t value);
void SetCC2(uint8_t value);
void SetCC3(uint8_t value);
void CCProcess(uint8_t ccFuncNumber, uint8_t value);
void ProcessParm0();
void ProcessParm1();
void ProcessParm2();
void ProcessParm3();
private:
DaisyPod *phw;
float sampleRate;
void Panic(void);
uint8_t currentVoiceSelector;
typedef enum
{
SYNTH_VOICE,
SPRING_VOICE,
MALLET_VOICE,
FORMANT_VOICE,
NOISE_VOICE
}VOICE_TYPE;
#define NUM_VOICES 5
void ChangeVoice(uint8_t sel);
OscVoice oscVoice;
SpringVoice springVoice;
MalletVoice malletVoice;
FormantVoice formantVoice;
NoiseVoice noiseVoice;
NullVoice *pvoice;
};