-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiano_keyboard_grid.littlefoot
489 lines (420 loc) · 12.8 KB
/
piano_keyboard_grid.littlefoot
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
Piano grid with root/octave controls
Donya Quick
Screen 0: piano grid
Screen 1: control screen
Controls:
- green button = reset root & octave
- orange/lightblue arrows = change root
- reed/blue arrows = change ocatve
- lower left corner button = select scale lighting (chromatic, major highlighted, minor highlighted)
*/
/*
<metadata description="Piano grid interface" details="Piano grid interface" target="Lightpad" tags="Controller;MIDI" canEmbedModes="false">
<variables>
<variable name="channel" displayName="MIDI Channel" type="int" min="1" max="16" value="1" displayMode="stepper" tooltip="The MIDI channel that values are sent on" />
<variable name="velocityBoost" displayName="Minimum Velocity" type="int" min="0" max="127" value="70" displayMode="stepper" tooltip="Minimum velocity (for when constant velocit option is unchecked)" />
<variable name="constVel" displayName = "Constant Velocity" type="bool" value="false" displayMode="checkbox" tooltip="Set constant velocity mode" />
<variable name="constVelVal" displayName="Constant Velocity value" type="int" min="0" max="127" value="120" displayMode="stepper" tooltip="Constant velocity value" />
<variable name="scale" displayName="scale" type="option" value="None" options="None;Major;Minor;" />
</variables>
</metadata>
*/
int screen; // current screen
int oct; // current octave
int root; // current root offset
// we will allow up to 10 simultaneous touches (one per finger)
int p1; // pitch for touch 1
int p2; // pitch for touch 2
int p3; // etc.
int p4;
int p5;
int p6;
int p7;
int p8;
int p9;
int p10;
// API function called on start
void initialise()
{
oct = 5; // this will mean lowest C is pitch number 60, middle C
// ensure that no touch points have pitches assigned
p1 = -1;
p2 = -1;
p3 = -1;
p4 = -1;
p5 = -1;
p6 = -1;
p7 = -1;
p8 = -1;
p9 = -1;
p10 = -1;
for (int i = 0; i < 32; ++i)
setLocalConfigActiveState (i, false, false);
}
// record the current touch index's pitch
void recordKeyPress(int index, int p) {
int pitch = min(127,max(0,p));
if (index==1)
p1 = pitch;
else if (index==2)
p2 = pitch;
if (index==3)
p3 = pitch;
else if (index==4)
p4 = pitch;
else if (index==5)
p5 = pitch;
else if (index==6)
p6 = pitch;
else if (index==7)
p7 = pitch;
else if (index==8)
p8 = pitch;
else if (index==9)
p9 = pitch;
else if (index==10)
p10 = pitch;
}
// record a release of the current touch index
void recordKeyRelease(int index) {
if (index==1)
p1 = -1;
else if (index==2)
p2 = -1;
if (index==3)
p3 = -1;
else if (index==4)
p4 = -1;
else if (index==5)
p5 = -1;
else if (index==6)
p6 = -1;
else if (index==7)
p7 = -1;
else if (index==8)
p8 = -1;
else if (index==9)
p9 = -1;
else if (index==10)
p10 = -1;
}
// For any keys being pressed, make them brighter.
// All touch indices must be checked to do this.
void colorKeysOn() {
colorPitchKeyOn(p1);
colorPitchKeyOn(p2);
colorPitchKeyOn(p3);
colorPitchKeyOn(p4);
colorPitchKeyOn(p5);
colorPitchKeyOn(p6);
colorPitchKeyOn(p7);
colorPitchKeyOn(p8);
colorPitchKeyOn(p9);
colorPitchKeyOn(p10);
}
void colorPitchKeyOn(int pitch) {
int whiteKeyColor = 0xFFFFFF;
int blackKeyColor = 0x00FFFF;
int p = pitch - root - oct*12; // will give us 0 to 15
if (p>=0 && p<4){
p = p%4; // we know the row, so find the column
if (p==0) {
fillRect(whiteKeyColor, 0, 12, 3, 3);
} else if (p==1) {
fillRect(blackKeyColor, 4, 12, 3, 3);
} else if (p==2) {
fillRect(whiteKeyColor, 8, 12, 3, 3);
} else {
fillRect(blackKeyColor, 12, 12, 3, 3);
}
} else if (p>=0 && p<8){
p = p%4; // we know the row, so find the column
if (p==0) {
fillRect(blackKeyColor, 0, 8, 3, 3);
} else if (p==1) {
fillRect(whiteKeyColor, 4, 8, 3, 3);
} else if (p==2) {
fillRect(blackKeyColor, 8, 8, 3, 3);
} else {
fillRect(whiteKeyColor, 12, 8, 3, 3);
}
} else if (p>=0 && p<12){
p = p%4; // we know the row, so find the column
if (p==0) {
fillRect(whiteKeyColor, 0, 4, 3, 3);
} else if (p==1) {
fillRect(whiteKeyColor, 4, 4, 3, 3);
} else if (p==2) {
fillRect(blackKeyColor, 8, 4, 3, 3);
} else {
fillRect(whiteKeyColor, 12, 4, 3, 3);
}
} else if (p>=0 && p<16) {
p = p%4; // we know the row, so find the column
if (p==0) {
fillRect(whiteKeyColor, 0, 0, 3, 3);
} else if (p==1) {
fillRect(blackKeyColor, 4, 0, 3, 3);
} else if (p==2) {
fillRect(whiteKeyColor, 8, 0, 3, 3);
} else {
fillRect(blackKeyColor, 12, 0, 3, 3);
}
}
}
// resets root and octave
void reset() {
fillRect(0xFFFFFF, 0,0,15,15);
fillRect(0x00FF00, 6,0,3,3);
root = 0;
oct = 5;
}
// get the pitch corresponding to a particular touch index
int getPitch(int index) {
if (index==1)
return p1;
else if (index==2)
return p2;
else if (index==3)
return p3;
else if (index==4)
return p4;
else if (index==5)
return p5;
else if (index==6)
return p6;
else if (index==7)
return p7;
else if (index==8)
return p8;
else if (index==9)
return p9;
else if (index==10)
return p10;
else
return -1;
}
// Handle a touch on the piano screen
void handleGridTouchPiano(int index, float xRaw, float yRaw) {
int x = int(14*xRaw/2);
int y = int(14*yRaw/2);
bool keyPressed = true;
if (y<4) { // top row: G#, A, A#, B / 8, 9, 10, 11
if (x<4) {
recordKeyPress(index,oct*12+root+12);
} else if (x<8)
recordKeyPress(index,oct*12+root+13);
else if (x<12)
recordKeyPress(index,oct*12+root+14);
else
recordKeyPress(index,oct*12+root+15);
} else if (y<8) { // 2nd row: E, F, F#, G / 4, 5, 6, 7
if (x<4) {
recordKeyPress(index,oct*12+root+8);
} else if (x<8)
recordKeyPress(index,oct*12+root+9);
else if (x<12)
recordKeyPress(index,oct*12+root+10);
else
recordKeyPress(index,oct*12+root+11);
} else if (y<12) { // 3rd row: C, C#, D, D# / 0, 1, 2, 3
if (x<4)
recordKeyPress(index,oct*12+root+4);
else if (x<8)
recordKeyPress(index,oct*12+root+5);
else if (x<12)
recordKeyPress(index,oct*12+root+6);
else
recordKeyPress(index,oct*12+root+7);
} else { // bottom row: octave controls
if (x<4)
recordKeyPress(index,oct*12+root);
else if (x<8)
recordKeyPress(index,oct*12+root+1);
else if (x<12)
recordKeyPress(index,oct*12+root+2);
else
recordKeyPress(index,oct*12+root+3);
}
}
void handleGridTouchControls(float xRaw, float yRaw) {
int x = int(14*xRaw/2);
int y = int(14*yRaw/2);
if (y<4) { // top row
if (x>5 && x<9) { // reset button
reset();
}
} else if (y<8) { // root controls row
if (x<4) // root down
root = max(0, root-1);
else if (x>10) // root up
root = min(11, root+1);
} else if (y<12) { // octave controls
if (x<4) // octave down
oct = max(0, oct-1);
else if (x>10) // ocatve up
oct = min(10, oct+1);
} else if (y>11 && x<3) {
scale = (scale+1) % 3;
}
}
// convert a touch velocity to MIDI 0-127 including the boost setting
int toVelocity(float vz) {
if (constVel)
return constVelVal
else
return min(127,int(vz*127+velocityBoost));
}
// API function called at the onset of a touch series
void touchStart (int index, float x, float y, float z, float vz)
{
if (screen==0) {
handleGridTouchPiano(index,x,y);
int p = getPitch(index);
if (p>=0)
sendNoteOn(channel-1, p , toVelocity(vz));
} else {
handleGridTouchControls(x,y);
}
}
// API function called whenever the touch moves
void touchMove (int index, float x, float y, float z, float vz)
{
addPressurePoint (0x005555, x, y, z * 10.0);
}
// API function called whenever the touch series ends
void touchEnd (int index, float x, float y, float z, float vz)
{
if(screen==0) {
int p = getPitch(index);
recordKeyRelease(index);
if (p>=0)
sendNoteOff(channel-1, p, toVelocity(vz));
}
}
// API function called on each update
void repaint()
{
if (screen==0) {
paintScreen0();
colorKeysOn();
} else {
paintScreen1();
}
}
void paintScreen0() {
blendRect(0x20000000, 0, 0, 15, 15);
int whiteKeyColor = 0x555555;
int whiteKeyColorOff = 0x050505;
int blackKeyColor = 0x0000FF;
int blackKeyColorOff = 0x000015;
drawPressureMap();
fadePressureMap();
fillRect(whiteKeyColor, 0, 0, 3, 3); // C
if (scale==0 || scale==2) {
fillRect(blackKeyColor, 0, 4, 3, 3); // Ab
} else {
fillRect(blackKeyColorOff, 0, 4, 3, 3); // Ab
}
if (scale==0 || scale==1) {
fillRect(whiteKeyColor, 0, 8, 3, 3); // E
} else {
fillRect(whiteKeyColorOff, 0, 8, 3, 3); // E
}
fillRect(whiteKeyColor, 0, 12, 3, 3); // C
if (scale==0) {
fillRect(blackKeyColor, 4, 0, 3, 3); // C#
} else {
fillRect(blackKeyColorOff, 4, 0, 3, 3); // C#
}
if (scale==0 || scale==1) {
fillRect(whiteKeyColor, 4, 4, 3, 3); // A
} else {
fillRect(whiteKeyColorOff, 4, 4, 3, 3); // A
}
fillRect(whiteKeyColor, 4, 8, 3, 3); // F
if (scale==0) {
fillRect(blackKeyColor, 4, 12, 3, 3); // C#
} else {
fillRect(blackKeyColorOff, 4, 12, 3, 3); // C#
}
fillRect(whiteKeyColor, 8, 0, 3, 3); // D
if (scale==0 || scale==2) {
fillRect(blackKeyColor, 8, 4, 3, 3); // Bb
} else {
fillRect(blackKeyColorOff, 8, 4, 3, 3); // Bb
}
if (scale==0) {
fillRect(blackKeyColor, 8, 8, 3, 3); // F#
} else {
fillRect(blackKeyColorOff, 8, 8, 3, 3); // F#
}
fillRect(whiteKeyColor, 8, 12, 3, 3); // D
if (scale==0 || scale==2) {
fillRect(blackKeyColor, 12, 0, 3, 3); // Eb
} else {
fillRect(blackKeyColorOff, 12, 0, 3, 3); // Eb
}
if (scale==0 || scale==1) {
fillRect(whiteKeyColor, 12, 4, 3, 3); // B
} else {
fillRect(whiteKeyColorOff, 12, 4, 3, 3); // B
}
fillRect(whiteKeyColor, 12, 8, 3, 3); // G
if (scale==0 || scale==2) {
fillRect(blackKeyColor, 12, 12, 3, 3); // Eb
} else {
fillRect(blackKeyColorOff, 12, 12, 3, 3); // Eb
}
}
void paintScreen1() {
blendRect(0x20000000, 0, 0, 15, 15);
int whiteKeyColor = 0x555555;
int blackKeyColor = 0x5555FF;
drawPressureMap();
fadePressureMap();
fillRect(0x00FF00,6,0,3,3); // reset button
// octave buttons
fillRect(0xFF0000, 1, 8, 2, 3); // octave down
fillRect(0xFF0000, 0, 9, 1, 1);
fillRect(0x0000FF, 12, 8, 2, 3); // octave up
fillRect(0x0000FF, 14, 9, 1, 1);
// root buttons
fillRect(0xFF5500, 1, 4, 2, 3); // root down
fillRect(0xFF5500, 0, 5, 1, 1);
fillRect(0x0055FF, 12, 4, 2, 3); // root up
fillRect(0x0055FF, 14, 5, 1, 1);
// octave & root markers
if (oct+2<7) {
fillRect(0xFF5000, oct+2, 11, 4-oct+2, 1); // octave marker
} else {
fillRect(0xFF5000, 7, 11, oct+2-6, 1); // octave marker
}
fillRect(0xFFFF00, oct+2, 11, 1, 1); // octave marker
fillRect(0x00055C, 1, 3, root+1, 1); // root marker
fillRect(0x00FFFF, root+1, 3, 1, 1); // root marker
if(scale==0) {
fillRect(0xFFFFFF, 0,12,3,3);
} else if(scale==1) {
fillRect(0xFFFF00, 0,12,3,3);
} else { //scale is 2
fillRect(0x0000FF, 0, 12, 3, 3);
}
}
void emptyRect(int edgeColor, int x, int y, int w, int h) {
fillRect(edgeColor,x-1,y-1,w+1,1);
fillRect(edgeColor,x-1,y-1,1,h+1);
fillRect(edgeColor,x-1,y+h-1,w+1,1);
fillRect(edgeColor,x+w-1,y-1,1,h+1);
}
void emptyRectBlend(int edgeColor, int x, int y, int w, int h) {
blendRect(edgeColor,x-1,y-1,w+1,1);
blendRect(edgeColor,x-1,y-1,1,h+1);
blendRect(edgeColor,x-1,y+h-1,w+1,1);
blendRect(edgeColor,x+w-1,y-1,1,h+1);
}
// API function called when the side button is pushed
void handleButtonDown (int index) {
screen = (screen+1)%2;
}