-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion1
276 lines (221 loc) · 7.98 KB
/
version1
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
#include <SPI.h>
#include <SD.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <MIDI.h>
// ------------------------------------------------------
// GUItool: begin automatically generated code
AudioSynthWaveform waveformLfo1;
AudioSynthWaveform waveformLfo2;
AudioSynthWaveform waveformOsc1;
AudioSynthWaveform waveformOsc2;
AudioEffectMultiply multiply1; //xy=397.407413482666,450.7407455444336
AudioMixer4 oscMixer; //xy=517.2222137451172,151.72222137451172
AudioMixer4 lfoMixer; //xy=588.8888893127441,416.3888854980469
AudioFilterStateVariable filter; //filter //xy=779.9074230194092,264.9073944091797
AudioOutputI2S i2s1; //audio out //xy=1300.888916015625,362.8888854980469
AudioConnection patchCord1(waveformLfo1, 0, multiply1, 0);
AudioConnection patchCord2(waveformLfo2, 0, multiply1, 1);
AudioConnection patchCord3(waveformOsc1, 0, oscMixer, 0);
AudioConnection patchCord4(waveformOsc2, 0, oscMixer, 1);
AudioConnection patchCord5(multiply1, 0, lfoMixer, 0);
AudioConnection patchCord6(oscMixer, 0, filter, 0);
AudioConnection patchCord7(lfoMixer, 0, filter, 1);
AudioConnection patchCord8(filter, 1, i2s1, 0);
AudioControlSGTL5000 audioShield; //xy=152.25,82.25
// GUItool: end automatically generated code
//----------------------------------------------------------------------
// Constants
// midi contolchange numbers
const int osc1CC = 22;
const int osc2CC = 23;
const int oscMixCC = 24;
const int lfo1CC = 25;
const int lfo2CC = 26;
const int shapeCC = 27;
const int pulseCC = 28;
const int filterFrequencyCC = 29;
const int filterResonanceCC = 30;
const int volumeCC = 31;
const int osc1WaveformCC = 14;
const int osc2WaveformCC = 15;
// osc & lfo settings
const int oscMaxFrequency = 375;
const int maxLfo1Speed = 20;
const int maxLfo2Speed = 16;
const int lfo2Base = 2;
// filter settings
const int maxFilterResonance = 5;
const int maxFilterFrequency = oscMaxFrequency + 500;
const float minFilterResonance = 0.7;
// lfo waveforms
const int waveformLfoSawtooth = WAVEFORM_SAWTOOTH;
const int waveformLfoSquare = WAVEFORM_SQUARE;
//osc waveforms
int oscWaveformSquare = WAVEFORM_SQUARE;
int oscWaveformTriangle = WAVEFORM_TRIANGLE;
//----------------------------------------------------------------------
// variables
int oscMix = 0;
int lfo1Speed = 0;
int lfo2Speed = 2;
int shape = 0;
int pulse = 0;
int filterFrequency =0;
int filterResonance = 0;
int osc1Frequency = 0;
int osc2Frequency = 0;
int osc1Waveform = WAVEFORM_SQUARE;
int osc2Waveform = WAVEFORM_TRIANGLE;
boolean isOsc1WaveformSquare = true;
boolean isOsc2WaveFormTriangle = true;
float outPutVolume = 0.5;
// ------------------------------------------------------
// init audio, midi and other stuff
void setup() {
//set upp midi handles
usbMIDI.setHandleControlChange(OnControlChange);
//output serial stuff ad 96k
Serial.begin(9600);
// Init audio stufff
initAudioStuff();
// init everything else
initWaveformsAndOtherThings();
}
// ------------------------------------------------------
// Read midi cc data
void OnControlChange(byte channel, byte control, byte value){
int newIntVal, tempVal = 0;
float newFloatVal = 0;
switch (control) {
case osc1CC: //osc1 freq
newIntVal = map(value, 0, 127,0, oscMaxFrequency);
waveformOsc1.frequency(newIntVal);
break;
case osc2CC: //osc2 freq
newIntVal = map(value, 0, 127,0, oscMaxFrequency);
waveformOsc2.frequency(newIntVal);
break;
case oscMixCC: //osc mix
newFloatVal = mapfloat(value, 0, 127,0, 1);
oscMixer.gain(0, newFloatVal);
oscMixer.gain(1, 1-newFloatVal);
break;
case lfo1CC: // lfo 1 speed
newIntVal = map(value, 0, 127,0, maxLfo1Speed);
waveformLfo1.frequency(newIntVal);
break;
case lfo2CC: // lfo 2 speed - 2, 4, 8, 16
tempVal = map(value, 0, 127,0, maxLfo1Speed);
newIntVal = map(value, 0, 127,1, maxLfo2Speed);
waveformLfo2.frequency(pow(lfo2Base,newIntVal) * tempVal);
break;
case shapeCC: //lfo effect on filter
newFloatVal = mapfloat(value, 0, 127,0, 1);
lfoMixer.gain(0, newFloatVal);
break;
case pulseCC: // lfo depth of modulation of filter
newFloatVal = mapfloat(value, 0, 127, 0, 1);
waveformLfo2.amplitude(newFloatVal);
break;
case filterFrequencyCC: // filter frequency
newIntVal = map(value, 0, 127, 0, maxFilterFrequency );
filter.frequency(newIntVal);
break;
case filterResonanceCC: // filter resonance
newFloatVal = mapfloat(value, 0, 127, minFilterResonance, maxFilterResonance );
filter.resonance(newFloatVal);
break;
case volumeCC: // set audioSheield volume
newFloatVal = mapfloat(value, 0, 127, 0, volumeCC );
audioShield.volume(newFloatVal);
break;
case osc1WaveformCC: // change osc 1 waveform
newIntVal = map(value, 0, 127, 0, 1 );
if(newIntVal == 0) {
if(isOsc1WaveformSquare) {
//change to trigangle
waveformOsc1.begin(WAVEFORM_TRIANGLE);
isOsc1WaveformSquare = false;
} else {
// do nothing - already set to square
}
}
break;
case osc2WaveformCC: // change osc2 waveform
newIntVal = map(value, 0, 127, 0, 1 );
if(newIntVal == 0) {
if(isOsc2WaveFormTriangle) {
//change to trigangle
waveformOsc2.begin(WAVEFORM_SQUARE);
isOsc2WaveFormTriangle = false;
} else {
// do nothing - already set to triangle
}
}
break;
}
}
// ------------------------------------------------------
// Main
void loop() {
usbMIDI.read();
}
// ------------------------------------------------------
// Helpers
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
// ------------------------------------------------------
// Inits
void initAudioStuff() {
AudioNoInterrupts();
AudioMemory(18); // audio needs memory
audioShield.enable();
audioShield.volume(outPutVolume);
AudioProcessorUsageMaxReset();
AudioMemoryUsageMaxReset();
AudioInterrupts();
}
void initWaveformsAndOtherThings() {
// init waveform for osc 1
waveformOsc1.pulseWidth(0);
waveformOsc1.begin(1, osc1Frequency, osc1Waveform);
// init waveform for osc 2
waveformOsc2.pulseWidth(0);
waveformOsc2.begin(1, osc2Frequency, osc2Waveform);
// init lfo waveforms
waveformLfo1.pulseWidth(0);
waveformLfo1.begin(1, lfo1Speed , waveformLfoSawtooth); //saw lfo
// init sub lfo
waveformLfo2.pulseWidth(0);
waveformLfo2.begin(1, lfo2Speed , waveformLfoSquare); // square lfo
// set mixer
oscMixer.gain(0, 0.5);
oscMixer.gain(1, 0.5);
// set mix for lfo
lfoMixer.gain(0, 0);
//filter things
filter.frequency(filterFrequency);
filter.resonance(filterResonance);
}
//----------------------------------------------------------------------
// Read the analog sensors
//
// int analogVal1 = analogRead(analogInPin1); // osc freq
// int analogVal2 = analogRead(analogInPin2); // osc freq 2
// int analogVal3 = analogRead(analogInPin3); // ??
// int analogVal4 = analogRead(analogInPin4); // ??
// int analogVal4 = analogRead(analogInPin4); // ??
// int analogVal4 = analogRead(analogInPin4); // ??
//----------------------------------------------------------------------
// Read the analog sensors
// Map to freq range
// int freq1 = map(analogVal1 , 0, 4096, 10, 370 );
// int freq2 = map(analogVal2 , 0, 4096, 10, 370 );
//
// float oscMixer = mapfloat(analogVal3 , 0, 4096, 0, 10);
// int lfo2Speed = map(analogVal3, 0, 4096, 0, 24);
// int lfo2Speed = map(analogVal3, 0, 4096, 0, 24);