-
Notifications
You must be signed in to change notification settings - Fork 3
/
MIDI_x0x.ino
372 lines (263 loc) · 8.17 KB
/
MIDI_x0x.ino
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
#include <MIDI.h>
#include <AH_MCP4922.h>
#include <DS1267.h>
#include <EEPROM.h>
#define GATE_PIN 2
#define GATE_LED A5
#define ENV_MOD_CTRL 1
#define ENV_MOD_PIN 3
#define CUTOFF_CTRL 74
#define CUTOFF_PIN 6
#define SAW_CTRL 70
#define SAW_PIN 5
#define SQR_CTRL 71
#define SQR_PIN 9
#define SLIDE_CTRL 65
#define SLIDE_TIME_CTRL 5
#define SLIDE_IN_PIN 13
#define SLIDE_OUT_PIN 12
#define DECAY_CTRL 72
#define DECAY_PIN 11
#define ACCENT_CTRL 11
#define ACCENT_PIN 10
#define RES_CTRL 2
#define RES_POT_PIN A4
#define ALL_NOTES_OFF 123
#define CTRL_RESET 121
AH_MCP4922 PitchDac(A1,A2,A3,LOW,LOW);
// Velocity gets assigned to the analog cutoff voltage because
// the PWM pins have some latency. Whatever velocity controls cannot
// afford latency since the note is beginning simultaneously.
AH_MCP4922 CutoffDac(A1,A2,A3,HIGH,LOW);
// dual digipot to control x0x resonance programatically
DS1267 ResonancePot(8,7,4);
int liveNoteCount = 0;
int pitchbendOffset = 0;
int baseNoteFrequency;
byte deviceID;
MIDI_CREATE_DEFAULT_INSTANCE();
void handleNoteOn(byte channel, byte pitch, byte velocity)
{
liveNoteCount++;
baseNoteFrequency = (pitch - 12) * 42;
PitchDac.setValue(baseNoteFrequency + pitchbendOffset);
digitalWrite(GATE_PIN, HIGH);
digitalWrite(GATE_LED, HIGH);
}
void handleNoteOff(byte channel, byte pitch, byte velocity)
{
liveNoteCount--;
if (liveNoteCount <= 0) {
digitalWrite(GATE_PIN, LOW);
digitalWrite(GATE_LED, LOW);
}
}
// these hold the current settings
byte envelopeLevel, cutoff, resonance, accent, sawLevel, squareLevel, slide, decay;
// variables for managing resonance pot input value
int resControllerValue, lastResControllerValue, resPotValue;
float resPotRecentAvg = 0;
void handleControlChange(byte channel, byte number, byte value)
{
int scaledValue = int(value) << 1;
switch (number) {
case CUTOFF_CTRL:
CutoffDac.setValue(value << 5);
cutoff = value;
break;
case ENV_MOD_CTRL:
analogWrite(ENV_MOD_PIN, scaledValue);
envelopeLevel = value;
break;
case SAW_CTRL:
analogWrite(SAW_PIN, scaledValue);
sawLevel = value;
break;
case SQR_CTRL:
analogWrite(SQR_PIN, scaledValue);
squareLevel = value;
break;
case DECAY_CTRL:
analogWrite(DECAY_PIN, 255 - scaledValue);
decay = value;
break;
case ACCENT_CTRL:
analogWrite(ACCENT_PIN, scaledValue);
accent = value;
break;
case SLIDE_CTRL:
if (value >= 64) {
digitalWrite(SLIDE_IN_PIN, HIGH);
digitalWrite(SLIDE_OUT_PIN, LOW);
} else {
digitalWrite(SLIDE_IN_PIN, LOW);
digitalWrite(SLIDE_OUT_PIN, HIGH);
}
slide = value;
break;
case RES_CTRL:
ResonancePot.setValue(255-scaledValue, 255-scaledValue, 0);
resonance = value;
break;
case ALL_NOTES_OFF:
liveNoteCount = 0;
handlePitchBend(deviceID, 0);
digitalWrite(GATE_PIN, LOW);
digitalWrite(GATE_LED, LOW);
break;
}
}
void handlePitchBend(byte channel, int bend)
{
pitchbendOffset = bend >> 4;
PitchDac.setValue(baseNoteFrequency + pitchbendOffset);
}
void handleSystemExclusive(byte message[], unsigned size) {
if (message[1] != 0x77) return; // manufacturer ID
if (message[2] != 0x33) return; // model ID
if (message[3] != deviceID) return; // device ID as set with trim pot
switch (message[4]) {
case 0x00:
setMidiChannel(message[5]);
break;
case 0x01:
sendPatchDump();
break;
case 0x02:
receivePatchDump(message);
break;
case 0x11:
saveProgram(message[5]);
default:
break;
}
}
void saveProgram(char progNumber) {
int offset = 8 * progNumber;
EEPROM.update(offset++, envelopeLevel);
EEPROM.update(offset++, resonance);
EEPROM.update(offset++, accent);
EEPROM.update(offset++, slide);
EEPROM.update(offset++, sawLevel);
EEPROM.update(offset++, squareLevel);
EEPROM.update(offset++, decay);
EEPROM.update(offset, cutoff);
}
void handleProgramChange(byte channel, byte number) {
int offset = 8 * number;
envelopeLevel = EEPROM.read(offset++);
resonance = EEPROM.read(offset++);
accent = EEPROM.read(offset++);
slide = EEPROM.read(offset++);
sawLevel = EEPROM.read(offset++);
squareLevel = EEPROM.read(offset++);
decay = EEPROM.read(offset++);
cutoff = EEPROM.read(offset);
}
void sendPatchDump() {
byte sysexArray[] = { 0xf0, 0x77, 0x33, deviceID, 0x02, 0,0,0,0,0,0,0,0, 0xf7 };
int paramByte = 5;
sysexArray[paramByte++] = envelopeLevel;
sysexArray[paramByte++] = resonance;
sysexArray[paramByte++] = accent;
sysexArray[paramByte++] = slide;
sysexArray[paramByte++] = sawLevel;
sysexArray[paramByte++] = squareLevel;
sysexArray[paramByte++] = decay;
sysexArray[paramByte] = cutoff;
MIDI.sendSysEx(14, sysexArray, true);
}
void receivePatchDump(byte message[]) {
int paramByte = 5;
handleControlChange(deviceID, ENV_MOD_CTRL, message[paramByte++]);
handleControlChange(deviceID, RES_CTRL, message[paramByte++]);
handleControlChange(deviceID, ACCENT_CTRL, message[paramByte++]);
handleControlChange(deviceID, SLIDE_CTRL, message[paramByte++]);
handleControlChange(deviceID, SAW_CTRL, message[paramByte++]);
handleControlChange(deviceID, SQR_CTRL, message[paramByte++]);
handleControlChange(deviceID, DECAY_CTRL, message[paramByte++]);
handleControlChange(deviceID, CUTOFF_CTRL, message[paramByte++]);
}
void setMidiChannel(byte newChannel) {
MIDI.begin(newChannel % 17);
playScale(newChannel % 17);
}
// -----------------------------------------------------------------------------
void setup()
{
int channelSpan = 1024 / 16;
int channelInput = analogRead(0);
deviceID = channelInput / channelSpan;
pinMode(GATE_PIN, OUTPUT);
digitalWrite(GATE_PIN, LOW);
pinMode(GATE_LED, OUTPUT);
digitalWrite(GATE_LED, LOW);
digitalWrite(SLIDE_IN_PIN, OUTPUT);
digitalWrite(SLIDE_IN_PIN, LOW);
digitalWrite(SLIDE_OUT_PIN, OUTPUT);
digitalWrite(SLIDE_OUT_PIN, HIGH);
slide = 0;
pinMode(ENV_MOD_PIN, OUTPUT);
digitalWrite(ENV_MOD_PIN, HIGH);
envelopeLevel = 127;
pinMode(SAW_PIN, OUTPUT);
digitalWrite(SAW_PIN, HIGH);
sawLevel = 127;
pinMode(SQR_PIN, OUTPUT);
digitalWrite(SQR_PIN, LOW);
squareLevel = 0;
pinMode(CUTOFF_PIN, OUTPUT);
digitalWrite(CUTOFF_PIN, LOW);
cutoff = 0;
pinMode(DECAY_PIN, OUTPUT);
digitalWrite(DECAY_PIN, LOW);
decay = 0;
pinMode(ACCENT_PIN, OUTPUT);
digitalWrite(ACCENT_PIN, LOW);
accent = 0;
TCCR0B = (TCCR0B & 0b11111000) | 0x01;
TCCR1B = (TCCR1B & 0b11111000) | 0x01;
TCCR2B = (TCCR2B & 0b11111000) | 0x01;
delay(1000);
// init resonance running avg to avoid sending event at startup
resPotRecentAvg = analogRead(RES_POT_PIN);
lastResControllerValue = 127 - ((int)resPotRecentAvg >> 3);
playScale(deviceID);
// calibrate 8V
baseNoteFrequency = (108 - 12) * 42;
PitchDac.setValue(baseNoteFrequency);
// calibrate full cutoff
CutoffDac.setValue(32 * 127);
cutoff = 127;
MIDI.setHandleNoteOn(handleNoteOn);
MIDI.setHandleNoteOff(handleNoteOff);
MIDI.setHandlePitchBend(handlePitchBend);
MIDI.setHandleControlChange(handleControlChange);
MIDI.setHandleSystemExclusive(handleSystemExclusive);
MIDI.setHandleProgramChange(handleProgramChange);
MIDI.begin(deviceID);
MIDI.turnThruOff();
}
void playScale(int channel) {
int note = 60;
for (int i=0; i<channel; i++) {
handleNoteOn(channel, note, 100);
delay(5000);
handleNoteOff(channel, note, 100);
delay(5000);
note++;
}
}
void loop()
{
resPotValue = analogRead(RES_POT_PIN);
resPotRecentAvg = .2 * (float)resPotValue + .8 * resPotRecentAvg;
resControllerValue = 127 - ((int)resPotRecentAvg >> 3);
if (abs(lastResControllerValue - resControllerValue) > 1) {
lastResControllerValue = resControllerValue;
handleControlChange(deviceID, RES_CTRL, resControllerValue);
resonance = resControllerValue;
MIDI.sendControlChange(RES_CTRL, resControllerValue, deviceID);
}
MIDI.read();
}