-
Notifications
You must be signed in to change notification settings - Fork 0
/
thethe.ino
266 lines (230 loc) · 9.37 KB
/
thethe.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
#include <MIDI.h>
#include <NewPing.h>
#define MAX_SENSORS 2
#define TRIGGER1_PIN 2
#define ECHO1_PIN 3
#define TRIGGER2_PIN 4
#define ECHO2_PIN 5
#define PINGS 3
#define MAX_DISTANCE_CM 55
#define DEAD_ZONE_CM 5
#define HIT_VELOCITY_THRESHOLD 20
#define HIT_TIME_THRESHOLD 120
// Sensor Modes
#define NOTE 0
#define CC 1
#define BEND 2
#define HIT 3
#define DISABLED 99
// Scales/Pitch class sets
#define CHROMATIC 0
#define MAJOR 1
#define MINOR 2
#define HARMONIC_MINOR 3
#define MAJOR_PENTATONIC 4
#define MINOR_PENTATONIC 5
#define WHOLE_TONE 6
#define GM_BASSDRUM_SNARE 70
#define ONE_FOUR_FIVE 71
// CC
#define CC_CENTER 64
#define CC_NO_RESET -1
// GM MIDI Drum Kit instruments
#define BASS_DRUM 36
#define SNARE 38
#define CLOSED_HI_HAT 42
#define OPEN_HI_HAT 46
#define CRASH_1 49
// Configuration
int CHROMATIC_INTERVALS[] = {1};
int MAJOR_INTERVALS[] = {2, 2, 1, 2, 2, 2, 1};
int MINOR_INTERVALS[] = {2, 1, 2, 2, 1, 2, 2};
int HARMONIC_MINOR_INTERVALS[] = {2, 1, 2, 2, 1, 3, 1};
int MAJOR_PENTATONIC_INTERVALS[] = {2, 2, 3, 2, 3};
int MINOR_PENTATONIC_INTERVALS[] = {3, 2, 2, 3, 2};
int WHOLE_TONE_INTERVALS[] = {2, 2, 2, 2, 2, 2};
// General parameters
boolean activated[MAX_SENSORS] = {true, true};
boolean reversed[MAX_SENSORS] = {false, false};
int steps[MAX_SENSORS] = {6, 3}; // How many divisions on the measureable space (MAX_DISTANCE_CM)
int channel[MAX_SENSORS] = {1, 10};
int mode[MAX_SENSORS] = {NOTE, HIT};
// CC mode-related parameters
int cc[MAX_SENSORS] = {1, 1};
int ccNoReadingValue[MAX_SENSORS] = {CC_CENTER, CC_NO_RESET};
// NOTE mode-related parameters
int root[MAX_SENSORS] = {60, 36};
int scale[MAX_SENSORS] = {MINOR_PENTATONIC, ONE_FOUR_FIVE};
boolean latch[MAX_SENSORS] = {true, true};
boolean noReadingEndsLatch[MAX_SENSORS] = {true, true}; // 'noReadingEndsLatch = false': 100% true latching
int velocity[MAX_SENSORS] = {100, 100};
int fixedDuration[MAX_SENSORS] = {250, -1}; // For non-latching option, HIT mode, etc.
int fixedPitch[MAX_SENSORS] = {-1, OPEN_HI_HAT}; // For HIT operation mode
// Tracking & State
int notes[MAX_SENSORS][128];
int last[MAX_SENSORS] = { -1, -1};
unsigned long times[MAX_SENSORS] = {millis(), millis()};
// Program
MIDI_CREATE_DEFAULT_INSTANCE();
void setup() {
for (int s = 0; s < MAX_SENSORS; s++)
defineScaleNotes(s);
MIDI.begin();
Serial.begin(115200);
}
NewPing S[] = {NewPing (TRIGGER1_PIN, ECHO1_PIN, MAX_DISTANCE_CM + DEAD_ZONE_CM), NewPing (TRIGGER2_PIN, ECHO2_PIN, MAX_DISTANCE_CM + DEAD_ZONE_CM)};
void loop() {
int value;
for (int s = 0; s < MAX_SENSORS; s++) {
if (!activated[s])
continue;
value = -1;
switch (mode[s]) {
case NOTE:
value = readNote(s);
if (value > 0) {
if (last[s] != value) {
if (latch[s] && last[s] != -1)
MIDI.sendNoteOn(last[s], 0, channel[s]);
last[s] = value;
//velocity[s] = ???; // read from another sensor!
MIDI.sendNoteOn(last[s], velocity[s], channel[s]);
if (!latch[s]) {
if (fixedDuration[s] > 0)
delay(fixedDuration[s]);
MIDI.sendNoteOn(last[s], 0, channel[s]);
}
}
} else {
if (latch[s] && noReadingEndsLatch[s] && last[s] != -1) {
MIDI.sendNoteOn(last[s], 0, channel[s]);
last[s] = -1;
}
}
break;
case CC:
value = readCC(s);
if (value > 0) {
if (last[s] != value) {
MIDI.sendControlChange(cc[s], value, channel[s]);
last[s] = value;
}
} else {
if (ccNoReadingValue[s] != CC_NO_RESET) {
MIDI.sendControlChange(cc[s], ccNoReadingValue[s], channel[s]);
last[s] = ccNoReadingValue[s];
}
}
break;
case BEND:
value = readPitchBend(s);
if (last[s] != value) {
MIDI.sendPitchBend(value, channel[s]);
last[s] = value;
}
break;
case HIT:
if (fixedPitch[s] != -1) {
value = readVelocity(s);
unsigned long now = millis();
if ((now - times[s]) > HIT_TIME_THRESHOLD) {
if (abs(last[s] - value) > HIT_VELOCITY_THRESHOLD) {
MIDI.sendNoteOn(fixedPitch[s], value, channel[s]); // Debounce needed?
if (fixedDuration[s] > 0) {
delay(fixedDuration[s]);
MIDI.sendNoteOff(fixedPitch[s], 0, channel[s]);
}
last[s] = value;
times[s] = now;
}
}
}
break;
}
}
}
int readNote(int sensor) {
int distance = readDistanceCM(sensor);
if (distance <= DEAD_ZONE_CM || distance >= MAX_DISTANCE_CM + DEAD_ZONE_CM) return 0;
int netDistance = distance - DEAD_ZONE_CM;
return notes[sensor][
reversed[sensor] ?
map(netDistance, 0, MAX_DISTANCE_CM, steps[sensor], 0) - 1 :
map(netDistance, 0, MAX_DISTANCE_CM, 0, steps[sensor])
];
}
int readCC(int sensor) {
int distance = readDistanceCM(sensor);
if (distance <= DEAD_ZONE_CM) return 0;
int netDistance = distance - DEAD_ZONE_CM;
return reversed[sensor] ?
map(netDistance, 0, MAX_DISTANCE_CM, 127, 0) :
map(netDistance, 0, MAX_DISTANCE_CM, 0, 127);
}
int readPitchBend(int sensor) {
int distance = readDistanceCM(sensor);
if (distance <= DEAD_ZONE_CM) return 0;
int netDistance = distance - DEAD_ZONE_CM;
return reversed[sensor] ?
map(netDistance, 0, MAX_DISTANCE_CM, 8191, -8192) :
map(netDistance, 0, MAX_DISTANCE_CM, -8192, 8191);
}
int readVelocity(int sensor) {
int distance = readDistanceCM(sensor);
if (distance <= DEAD_ZONE_CM) return 0;
int netDistance = distance - DEAD_ZONE_CM;
return reversed[sensor] ?
map(netDistance, 0, MAX_DISTANCE_CM, 127, 0) :
map(netDistance, 0, MAX_DISTANCE_CM, 0, 127);
}
int readDistanceCM(int sensor) {
return S[sensor].convert_cm(S[sensor].ping_median(PINGS));
}
void defineScaleNotes(int sensor) {
notes[sensor][0] = root[sensor];
int numberOfIntervals = 0;
int intervals[12];
switch (scale[sensor]) {
case CHROMATIC:
numberOfIntervals = 1;
memcpy(intervals, CHROMATIC_INTERVALS, sizeof(CHROMATIC_INTERVALS));
break;
case MAJOR:
numberOfIntervals = 7;
memcpy(intervals, MAJOR_INTERVALS, sizeof(MAJOR_INTERVALS));
break;
case MINOR:
numberOfIntervals = 7;
memcpy(intervals, MINOR_INTERVALS, sizeof(MINOR_INTERVALS));
break;
case HARMONIC_MINOR:
numberOfIntervals = 7;
memcpy(intervals, HARMONIC_MINOR_INTERVALS, sizeof(HARMONIC_MINOR_INTERVALS));
break;
case MAJOR_PENTATONIC:
numberOfIntervals = 5;
memcpy(intervals, MAJOR_PENTATONIC_INTERVALS, sizeof(MAJOR_PENTATONIC_INTERVALS));
break;
case MINOR_PENTATONIC:
numberOfIntervals = 5;
memcpy(intervals, MINOR_PENTATONIC_INTERVALS, sizeof(MINOR_PENTATONIC_INTERVALS));
break;
case WHOLE_TONE:
numberOfIntervals = 6;
memcpy(intervals, WHOLE_TONE_INTERVALS, sizeof(WHOLE_TONE_INTERVALS));
break;
case GM_BASSDRUM_SNARE:
steps[sensor] = 2;
notes[sensor][0] = BASS_DRUM;
notes[sensor][1] = SNARE;
return;
case ONE_FOUR_FIVE:
steps[sensor] = 3;
notes[sensor][0] = root[sensor]; // root
notes[sensor][1] = root[sensor] + 5; // perfect fourth
notes[sensor][2] = root[sensor] + 7; // perfect fifth;
return;
}
for (int i = 1; i < steps[sensor]; i++)
notes[sensor][i] = notes[sensor][i - 1] + intervals[(i - 1) % numberOfIntervals];
}