-
Notifications
You must be signed in to change notification settings - Fork 1
/
MIDIKeyboard.sc
286 lines (249 loc) · 6.9 KB
/
MIDIKeyboard.sc
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
// (c) 2006-2010, Thor Magnusson - www.ixi-audio.net
// GNU license - google it.
MIDIKeyboard {
var <>keys;
var trackKey, chosenkey, mouseTracker;
var win, bounds, octaves, startnote;
var downAction, upAction, trackAction;
*new { arg w, bounds, octaves, startnote;
^super.new.initMIDIKeyboard(w, bounds, octaves, startnote);
}
initMIDIKeyboard { arg w, argbounds, argoctaves=3, argstartnote;
var r, pix, pen;
octaves = argoctaves ? 4;
bounds = argbounds ? Rect(20, 10, 364, 60);
if((win= w).isNil, {
win = Window.new("MIDI Keyboard",
Rect(100, 250, bounds.width + 40, bounds.height+30));
win.front
});
mouseTracker = UserView.new(win, bounds); // thanks ron!
bounds = mouseTracker.bounds;
pen = Pen;
startnote = argstartnote ? 48;
trackKey = 0;
//pix = [0, 6, 10, 16, 20, 30, 36, 40, 46, 50, 56, 60];
pix = [ 0, 0.1, 0.17, 0.27, 0.33, 0.5, 0.6, 0.67, 0.77, 0.83, 0.93, 1 ]; // as above but normalized
keys = List.new;
octaves.do({arg j;
12.do({arg i;
if((i == 1) || (i == 3) || (i == 6) || (i == 8) || (i == 10), { // black keys
r = Rect( ( (pix[i]*((bounds.width/octaves) -
(bounds.width/octaves/7))).round(1) + ((bounds.width/octaves)*j)).round(1)+1,
0,
bounds.width/octaves/10,
bounds.height/1.7);
keys.add(MIDIKey.new(startnote+i+(j*12), r, Color.black));
}, { // white keys
r = Rect(((pix[i]*((bounds.width/octaves) -
(bounds.width/octaves/7))).round(1) + ((bounds.width/octaves)*j)).round(1),
0,
bounds.width/octaves/7,
bounds.height);
keys.add(MIDIKey.new(startnote+i+(j*12), r, Color.white));
});
});
});
mouseTracker
.canFocus_(false)
//.relativeOrigin_(false)
.mouseDownAction_({|me, x, y, mod|
chosenkey = this.findNote(x, y);
trackKey = chosenkey;
chosenkey.color = Color.grey;
downAction.value(chosenkey.note);
this.refresh;
})
.mouseMoveAction_({|me, x, y, mod|
chosenkey = this.findNote(x, y);
if(trackKey.note != chosenkey.note, {
trackKey.color = trackKey.scalecolor; // was : type
trackKey = chosenkey;
chosenkey.color = Color.grey;
trackAction.value(chosenkey.note);
this.refresh;
});
})
.mouseUpAction_({|me, x, y, mod|
chosenkey = this.findNote(x, y);
trackKey = chosenkey;
chosenkey.color = chosenkey.scalecolor; // was: type
upAction.value(chosenkey.note);
this.refresh;
})
.drawFunc_({
octaves.do({arg j;
// first draw the white keys
12.do({arg i;
var key;
key = keys[i+(j*12)];
if(key.type == Color.white, {
pen.color = Color.black;
pen.strokeRect(Rect(key.rect.left+0.5, key.rect.top+0.5, key.rect.width+0.5, key.rect.height-0.5));
pen.color = key.color; // white or grey
pen.fillRect(Rect(key.rect.left+0.5, key.rect.top+0.5, key.rect.width+0.5, key.rect.height-0.5));
});
});
// and then draw the black keys on top of the white
12.do({arg i;
var key;
key = keys[i+(j*12)];
if(key.type == Color.black, {
pen.color = Color.black;
pen.strokeRect(Rect(key.rect.left+0.5, key.rect.top+0.5, key.rect.width, key.rect.height));
pen.color = key.color;
pen.fillRect(Rect(key.rect.left+0.5, key.rect.top+0.5, key.rect.width, key.rect.height));
});
})
})
});
}
refresh {
mouseTracker.refresh;
}
keyDown { arg note, color; // midinote
if(note.isArray, {
note.do({arg note;
if(this.inRange(note), {
keys[note - startnote].color = Color.grey;
});
});
}, {
if(this.inRange(note), {
keys[note - startnote].color = Color.grey;
});
});
this.refresh;
}
keyUp { arg note; // midinote
// if(this.inRange(note), {
// keys[note - startnote].color = keys[note - startnote].scalecolor;
// });
if(note.isArray, {
note.do({arg note;
if(this.inRange(note), {
keys[note - startnote].color = keys[note - startnote].scalecolor;
});
});
}, {
if(this.inRange(note), {
keys[note - startnote].color = keys[note - startnote].scalecolor;
});
});
this.refresh;
}
keyDownAction_ { arg func;
downAction = func;
}
keyUpAction_ { arg func;
upAction = func;
}
keyTrackAction_ { arg func;
trackAction = func;
}
showScale {arg argscale, key=startnote, argcolor;
var color, scale, counter, transp;
this.clear; // erase scalecolors (make them their type)
counter = 0;
color = argcolor ? Color.red;
transp = key%12;
scale = argscale + transp + startnote;
keys.do({arg key, i;
key.color = key.type; // back to original color
if(((i-transp)%12 == 0)&&((i-transp)!=0), { counter = 0; scale = scale+12;}); if(key.note == scale[counter], {
counter = counter + 1;
key.color = key.color.blend(color, 0.5);
key.scalecolor = key.color;
key.inscale = true;
});
});
this.refresh;
}
clear {
keys.do({arg key, i;
key.color = key.type; // back to original color
key.scalecolor = key.color;
key.inscale = false;
});
this.refresh;
}
// just for fun
playScale { arg argscale, key=startnote, int=0.5;
var scale = argscale;
SynthDef(\midikeyboardsine, {arg freq, amp = 0.25;
Out.ar(0, (SinOsc.ar(freq,0,amp)*EnvGen.ar(Env.perc, doneAction:2)).dup)
}).load(Server.default);
Task({
scale.mirror.do({arg note;
Synth(\midikeyboardsine, [\freq, (key+note).midicps]);
int.wait;
}); }).start;
}
setColor {arg note, color;
var newcolor = keys[note - startnote].color.blend(color, 0.5);
keys[note - startnote].color = newcolor;
keys[note - startnote].scalecolor = newcolor;
this.refresh;
}
getColor { arg note;
^keys[note - startnote].color;
}
getType { arg note;
^keys[note - startnote].type;
}
removeColor {arg note;
keys[note - startnote].scalecolor = keys[note - startnote].type;
keys[note - startnote].color = keys[note - startnote].type;
this.refresh;
}
inScale {arg key;
^keys[key-startnote].inscale;
}
retNote {arg key;
^keys[key].note;
}
remove {
mouseTracker.remove;
win.refresh;
}
// local function
findNote {arg x, y;
var chosenkeys;
chosenkeys = [];
keys.reverse.do({arg key;
if(key.rect.containsPoint(Point.new(x,y)), {
chosenkeys = chosenkeys.add(key);
});
});
block{|break|
chosenkeys.do({arg key;
if(key.type == Color.black, {
chosenkey = key;
break.value; // the important part
}, {
chosenkey = key;
});
});
};
^chosenkey;
}
// local
inRange {arg note; // check if an input note is in the range of the keyboard
if((note>startnote) && (note<(startnote + (octaves*12))), {^true}, {^false});
}
}
MIDIKey {
var <rect, <>color, <note, <type;
var <>scalecolor, <>inscale;
*new { arg note, rect, type;
^super.new.initMIDIKey(note, rect, type);
}
initMIDIKey {arg argnote, argrect, argtype;
note = argnote;
rect = argrect;
type = argtype;
color = argtype;
scalecolor = color;
inscale = false;
}
}