-
Notifications
You must be signed in to change notification settings - Fork 0
/
Voice.h
270 lines (234 loc) · 10 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
#ifndef Voice_h_
#define Voice_h_
#include "Program.h"
class Voice{
private:
AudioSynthWaveformModulated *modulator; //xy=393,306
AudioEffectWaveshaper *modulatorWaveshape;
AudioEffectEnvelope *modulatorEnvelope; //xy=576,305
AudioSynthWaveformModulated *carrier;
AudioEffectWaveshaper *carrierWaveshape;
AudioEffectEnvelope *carrierEnvelope; //xy=872,301
AudioFilterStateVariable *filter;
AudioEffectEnvelope *filterEnvelope;
AudioMixer4 *filterMixer;
bool isPlaying;
//The program used by this voice
Program *program;
void Setup(AudioSynthWaveformModulated *modulator,
AudioEffectWaveshaper *modulatorWaveshape,
AudioEffectEnvelope *modulatorEnvelope,
AudioSynthWaveformModulated *carrier,
AudioEffectWaveshaper *carrierWaveshape,
AudioEffectEnvelope *carrierEnvelope,
AudioFilterStateVariable *filter,
AudioEffectEnvelope *filterEnvelope,
AudioMixer4 *filterMixer,
Program *program);
public:
byte pitch;
byte channel;
Voice();
Voice(AudioSynthWaveformModulated *modulator,
AudioEffectWaveshaper *modulatorWaveshape,
AudioEffectEnvelope *modulatorEnvelope,
AudioSynthWaveformModulated *carrier,
AudioEffectWaveshaper *carrierWaveshape,
AudioEffectEnvelope *carrierEnvelope,
AudioFilterStateVariable *filter,
AudioEffectEnvelope *filterEnvelope,
AudioMixer4 *filterMixer,
Program *program);
void SetProgram(Program *program);
void NoteOn(byte channel, byte pitch, float level);
void NoteOff();
bool IsPlaying();
};
Voice::Voice(){
}
Voice::Voice(AudioSynthWaveformModulated *modulator,
AudioEffectWaveshaper *modulatorWaveshape,
AudioEffectEnvelope *modulatorEnvelope,
AudioSynthWaveformModulated *carrier,
AudioEffectWaveshaper *carrierWaveshape,
AudioEffectEnvelope *carrierEnvelope,
AudioFilterStateVariable *filter,
AudioEffectEnvelope *filterEnvelope,
AudioMixer4 *filterMixer,
Program *program)
{
Setup(modulator, modulatorWaveshape, modulatorEnvelope, carrier, carrierWaveshape, carrierEnvelope, filter, filterEnvelope, filterMixer, program);
}
void Voice::Setup(AudioSynthWaveformModulated *modulator,
AudioEffectWaveshaper *modulatorWaveshape,
AudioEffectEnvelope *modulatorEnvelope,
AudioSynthWaveformModulated *carrier,
AudioEffectWaveshaper *carrierWaveshape,
AudioEffectEnvelope *carrierEnvelope,
AudioFilterStateVariable *filter,
AudioEffectEnvelope *filterEnvelope,
AudioMixer4 *filterMixer,
Program *program){
this->modulator = modulator;
this->modulatorWaveshape = modulatorWaveshape;
this->modulatorEnvelope = modulatorEnvelope;
this->carrier = carrier;
this->carrierWaveshape = carrierWaveshape;
this->carrierEnvelope = carrierEnvelope;
this->filter = filter;
this->filterEnvelope = filterEnvelope;
this->filterMixer = filterMixer;
//Setup default filter mixer
(*(this->filterMixer)).gain(0, 1);
(*(this->filterMixer)).gain(1, 1);
(*(this->filterMixer)).gain(2, 1);
(*(this->filterMixer)).gain(3, 0);
this->program = program;
//(*(this->carrier)).frequencyModulation(10);
//(*(this->modulator)).frequencyModulation(10);
//~OPL3/~DX7 style. (Yamaha marketing used FM, but implementation was actually PM)
(*(this->modulator)).phaseModulation(900);
(*(this->carrier)).phaseModulation(900);
//set modulator envelope
(*(this->modulatorEnvelope)).attack(2500);
(*(this->modulatorEnvelope)).hold(0);
(*(this->modulatorEnvelope)).decay(1);
(*(this->modulatorEnvelope)).sustain(1);
(*(this->modulatorEnvelope)).release(120);
//set carrier envelope
(*(this->carrierEnvelope)).attack(10);
(*(this->carrierEnvelope)).hold(0);
(*(this->carrierEnvelope)).decay(50);
(*(this->carrierEnvelope)).sustain(1);
(*(this->carrierEnvelope)).release(120);
//Modulator sine * 2, Carrier sine * 1 -> ?Glockenspiel?Harp? (Zelda fairy). :)
/*
//set modulator envelope
(*(this->modulatorEnvelope)).attack(25);
(*(this->modulatorEnvelope)).hold(0);
(*(this->modulatorEnvelope)).decay(250);
(*(this->modulatorEnvelope)).sustain(0.3);
(*(this->modulatorEnvelope)).release(700);
//set carrier envelope
(*(this->carrierEnvelope)).attack(2);
(*(this->carrierEnvelope)).hold(0);
(*(this->carrierEnvelope)).decay(500);
(*(this->carrierEnvelope)).sustain(0.6);
(*(this->carrierEnvelope)).release(700);
*/
//Modulator triangle * 2 (or 0.5), Carrier sine * 1 -> very lush
/*
//set modulator envelope
(*(this->modulatorEnvelope)).attack(250);
(*(this->modulatorEnvelope)).hold(0);
(*(this->modulatorEnvelope)).decay(1500);
(*(this->modulatorEnvelope)).sustain(0);
(*(this->modulatorEnvelope)).release(1);
//set carrier envelope
(*(this->carrierEnvelope)).attack(10);
(*(this->carrierEnvelope)).hold(0);
(*(this->carrierEnvelope)).decay(1500);
(*(this->carrierEnvelope)).sustain(0);
(*(this->carrierEnvelope)).release(1);
*/
//Modulator sine * 2, carrier sine * 1 -> lush
/*
//set modulator envelope
(*(this->modulatorEnvelope)).attack(50);
(*(this->modulatorEnvelope)).hold(0);
(*(this->modulatorEnvelope)).decay(50);
(*(this->modulatorEnvelope)).sustain(0.25);
(*(this->modulatorEnvelope)).release(10);
//set carrier envelope
(*(this->carrierEnvelope)).attack(10);
(*(this->carrierEnvelope)).hold(0);
(*(this->carrierEnvelope)).decay(50);
(*(this->carrierEnvelope)).sustain(0.75);
(*(this->carrierEnvelope)).release(150);
*/
//Modlator sine * 0.5, carrier square * 1 -> such phatness, much wow!
/*
//set modulator envelope
(*(this->modulatorEnvelope)).attack(100);
(*(this->modulatorEnvelope)).hold(0);
(*(this->modulatorEnvelope)).decay(100);
(*(this->modulatorEnvelope)).sustain(0.5);
(*(this->modulatorEnvelope)).release(10);
//set carrier envelope
(*(this->carrierEnvelope)).attack(10);
(*(this->carrierEnvelope)).hold(0);
(*(this->carrierEnvelope)).decay(50);
(*(this->carrierEnvelope)).sustain(1);
(*(this->carrierEnvelope)).release(50);
*/
}
void Voice::SetProgram(Program* program){
this->program = program;
}
void Voice::NoteOn(byte channel, byte pitch, float level){
this->pitch = pitch;
this->channel = channel;
AudioNoInterrupts();
//Modulator
(*(this->modulator)).begin((*(this->program)).GetOperatorVolume(0), midiNoteToFreq[pitch] * (*(this->program)).GetFreqMul(0) + (*(this->program)).GetFreqAdd(0), (*(this->program)).GetWaveform(0));
(*(this->modulatorWaveshape)).shape((*(this->program)).GetWaveshape(0),WAVESHAPE_SAMPLE_SIZE);
//(*(this->modulator)).phaseModulation(1440);
//(*(this->modulator)).phase(0);
//set modulator envelope
float *modulatorVolumeEnvelopeSettings = (*(this->program)).GetVolumeDAHDSR(0);
(*(this->modulatorEnvelope)).delay(*(modulatorVolumeEnvelopeSettings+0));
(*(this->modulatorEnvelope)).attack(*(modulatorVolumeEnvelopeSettings+1));
(*(this->modulatorEnvelope)).hold(*(modulatorVolumeEnvelopeSettings+2));
(*(this->modulatorEnvelope)).decay(*(modulatorVolumeEnvelopeSettings+3));
(*(this->modulatorEnvelope)).sustain(*(modulatorVolumeEnvelopeSettings+4));
(*(this->modulatorEnvelope)).release(*(modulatorVolumeEnvelopeSettings+5));
(*(this->modulatorEnvelope)).noteOn();
//Carrier
(*(this->carrier)).begin((*(this->program)).GetOperatorVolume(1) * level, midiNoteToFreq[pitch] * (*(this->program)).GetFreqMul(1) + (*(this->program)).GetFreqAdd(1), (*(this->program)).GetWaveform(1));
(*(this->carrierWaveshape)).shape((*(this->program)).GetWaveshape(1),WAVESHAPE_SAMPLE_SIZE);
//(*(this->carrier)).phaseModulation(1440);
//(*(this->carrier)).phaseModulation(180);
//(*(this->carrier)).frequencyModulation(1);
//set carrier envelope
float *carrierVolumeEnvelopeSettings = (*(this->program)).GetVolumeDAHDSR(1);
(*(this->carrierEnvelope)).delay(*(carrierVolumeEnvelopeSettings+0));
(*(this->carrierEnvelope)).attack(*(carrierVolumeEnvelopeSettings+1));
(*(this->carrierEnvelope)).hold(*(carrierVolumeEnvelopeSettings+2));
(*(this->carrierEnvelope)).decay(*(carrierVolumeEnvelopeSettings+3));
(*(this->carrierEnvelope)).sustain(*(carrierVolumeEnvelopeSettings+4));
(*(this->carrierEnvelope)).release(*(carrierVolumeEnvelopeSettings+5));
(*(this->carrierEnvelope)).noteOn();
//Filter
(*(this->filter)).frequency( (*(this->program)).GetFilterCutoff() );
(*(this->filter)).resonance( (*(this->program)).GetFilterResonance() );
(*(this->filter)).octaveControl( (*(this->program)).GetFilterOctaveControl() );
float *filterVolumeEnvelopeSettings = (*(this->program)).GetFilterDAHDSR();
(*(this->filterEnvelope)).delay(*(filterVolumeEnvelopeSettings+0));
(*(this->filterEnvelope)).attack(*(filterVolumeEnvelopeSettings+1));
(*(this->filterEnvelope)).hold(*(filterVolumeEnvelopeSettings+2));
(*(this->filterEnvelope)).decay(*(filterVolumeEnvelopeSettings+3));
(*(this->filterEnvelope)).sustain(*(filterVolumeEnvelopeSettings+4));
(*(this->filterEnvelope)).release(*(filterVolumeEnvelopeSettings+5));
(*(this->filterEnvelope)).noteOn();
(*(this->filterMixer)).gain(0, (*(this->program)).GetFilterLowpassVolume() );
(*(this->filterMixer)).gain(1, (*(this->program)).GetFilterBandpassVolume() );
(*(this->filterMixer)).gain(2, (*(this->program)).GetFilterHighpassVolume() );
(*(this->filterMixer)).gain(3, 0 );
this->isPlaying = true;
AudioInterrupts();
}
void Voice::NoteOff(){
(*(this->modulatorEnvelope)).noteOff();
(*(this->carrierEnvelope)).noteOff();
(*(this->filterEnvelope)).noteOff();
this->isPlaying = false;
}
bool Voice::IsPlaying(){
//Check if isPlaying is false AND carrier envelope is finished/idling (0) or in delay phase (1)
if((this->isPlaying == false) && ((*(this->carrierEnvelope)).isPlaying() == false)){
return false;
} else {
return true;
}
}
#endif