-
Notifications
You must be signed in to change notification settings - Fork 0
/
VSynthTools.pde
236 lines (169 loc) · 4.35 KB
/
VSynthTools.pde
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
// VSynthTools
// v0.12
// July 2014
// Basel School of Design
// General ---------------
void initVSynth(){
initMidi("SLIDER/KNOB"); // default for NanoControl
initLfos();
}
void initVSynth(String port){
initMidi(port);
initLfos();
}
void updateVSynth(){
updateMidi();
updateLfos();
}
// MIDI ---------------
import themidibus.*;
MidiBus bus;
MidiBuffer midi;
void initMidi(String inputPort) {
midi = new MidiBuffer();
MidiBus.list();
bus = new MidiBus(this, inputPort, -1);
}
void updateMidi(){
midi.update();
}
class MidiBuffer{
int ccAmount = 128;
float defaultValue = 1;
boolean useKnobsForEasing = true;
float[] direct; // last read value
float[] eased; // current animated value
float[] easeRate; // easing speed
MidiBuffer() {
// initialize buffers
direct = new float[ccAmount];
for( int i = 0; i < ccAmount; i++ ){
direct[i] = defaultValue;
}
eased = new float[ccAmount];
for( int i = 0; i < ccAmount; i++ ){
eased[i] = defaultValue;
}
easeRate = new float[ccAmount];
for( int i = 0; i < ccAmount; i++ ){
easeRate[i] = defaultValue; // default with a very fast ease rate
}
}
void update() {
for( int i = 0; i < ccAmount; i++ ){
float delta = direct[i] - eased[i];
eased[i] += delta * easeRate[i];
}
if(useKnobsForEasing) {
for(int i = 0; i < 8; i++) {
easeRate[i] = value( i+16, 0.001, 1, 3 ); // 0-7
easeRate[i+32] = value( i+16, 0.001, 1, 3 ); // 32-39
easeRate[i+48] = value( i+16, 0.001, 1, 3 ); // 48-55
easeRate[i+71] = value( i+16, 0.001, 1, 3 ); // 64-71
}
}
}
float value(int index) {
return value(index, 0, 1, 1);
}
float value(int index, float a, float b) {
return value(index, a, b, 1);
}
float value(int index, float a, float b, float linearity) {
return map(pow(direct[index], linearity), 0, 1, a, b);
}
float eased(int index) {
return eased(index, 0, 1, 1);
}
float eased(int index, float a, float b) {
return map(eased[index], 0, 1, a, b);
}
float eased(int index, float a, float b, float linearity) {
return map(pow(eased[index], linearity), 0, 1, a, b);
}
}
public void controllerChange(int channel, int number, int value) {
if(channel != 0) {
println("Wrong Midi Channel, should be 1");
} else {
println("Number: " + number + ", Value: " + value);
midi.direct[number] = map(value, 0, 127, 0, 1); // store and normalize incoming value
}
}
// LFOBuffer ---------------
LFOBuffer lfo;
void initLfos(){
lfo = new LFOBuffer();
}
void updateLfos(){
lfo.update();
}
class LFOBuffer {
ArrayList<LFO> lfos;
int amountLFOs = 8;
LFOBuffer() {
lfos = new ArrayList<LFO>(); // create 8 LFOs with frequency 1Hz
for(int i = 0; i < amountLFOs; i++) {
lfos.add(new LFO(1));
}
}
void update(){
for(int i = 0; i < lfos.size(); i++){
lfos.get(i).update();
}
}
void setFrequency(int num, float freq){
lfos.get(num).setFrequency(freq);
}
void setPhaseInvert(int num, boolean phase) {
lfos.get(num).setPhaseInvert(phase);
}
float value(int num){
return lfos.get(num).value(0, 1);
}
float value(int num, float a, float b){
return lfos.get(num).value(a, b, 0);
}
float value(int num, float a, float b, float phase ){
return lfos.get(num).value(a,b,phase);
}
}
// LFO ---------------
class LFO {
boolean phaseInvert = false;
int timer = 0;
int lastTime = 0;
float freq = 1; // Hertz
LFO(float f) {
this.setFrequency(f);
}
void update(){
float diffTime = millis() - lastTime; // time passed
timer += diffTime * freq; // actual animation
timer %= 1000; // wrap around
lastTime = millis();
}
void setFrequency(float f){
this.freq = f;
}
void setPhaseInvert(boolean invert) {
phaseInvert = invert;
}
float value(){
return value(0, 1);
}
float value(float a, float b){
return value(a, b, 0);
}
float value(float a, float b, float phase ){
if(phaseInvert) {
return map(cos( map(timer+phase, 0, 1000, 0, TWO_PI ) ), -1, 1, a, b);
} else {
return map(sin( map(timer+phase, 0, 1000, 0, TWO_PI ) ), -1, 1, a, b);
}
}
void reset() {
timer = 0;
}
}