-
Notifications
You must be signed in to change notification settings - Fork 0
/
PianoRoll.sc
620 lines (613 loc) · 16 KB
/
PianoRoll.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
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/*
PianoRoll - a piano roll for SuperCollider!
TODO:
* make sure ySpec can be \freq, \instrument, etc.
*/
PianoRoll : SCViewHolder {
var <bounds;
var <translate;
var <grid=true;
var <>selectedStrokeColor;
var <>highlightedStrokeColor;
var <ySpec=\midinote;
var <vSize = 20; // size of each row in pixels
var <vquant;
var <beatSize = 80; // size of each beat in pixels
var <beatquant = 0.25; // x quantizing
var <sidebarWidth = 40, <topbarHeight = 20;
var <>selected; // list of selected notes
var <>highlighted; // list of highlighted notes
var clickedOn, mouseDownPoint, mouseDownButton, dragAction=nil; // mouse actions context info
var selectionRect, ghosts;
var tview, uv;
var <sequence;
var newNoteSustain=\last;
var <>looping=false;
*initClass {
Spec.add(\midinote, [0, 127, \lin, 1, 60]); // i have no idea why the default step for \midinote is 0...
}
*new {
| parent bounds sequence |
^super.new.init(parent, bounds, sequence);
}
init {
| parent argbounds argsequence |
var keymap;
bounds = argbounds ?? {Rect(0, 0, 500, 500)};
bounds = bounds.asRect;
sequence = argsequence ?? { Sequence.new };
vquant = 1; // vertical quant - remove this? - FIX
ySpec = ySpec.asSpec;//ControlSpec(0, 127, \lin, 1, 64);
translate = Point(0, 0);
selected = List.new;
selectedStrokeColor = Color.blue;
highlighted = List.new;
highlightedStrokeColor = Color.green;
uv = UserView(parent, bounds).background_(Color.black);
tview = UserView(parent, bounds).acceptsMouse_(false);
view = UserView(parent, bounds).layout_(StackLayout(tview, uv).mode_(\stackAll));
tview.drawFunc_({
| view |
Pen.translate(sidebarWidth+translate.x, topbarHeight+translate.y);
if(selectionRect.notNil, {
Pen.fillColor_(Color.gray(0.5, 0.2));
Pen.strokeColor_(Color.gray(0.5, 0.4));
Pen.addRect(selectionRect);
Pen.draw(3);
});
if(ghosts.notNil, {
Pen.color_(Color.red(1, 0.5));
ghosts.do({
| note n |
if(note.notNil, {
var rect = this.noteToRect(note);
Pen.addRect(rect);
Pen.draw(3);
});
});
});
});
uv.drawFunc_({
| view |
if(grid, { // draw grid lines
var numlines = ((view.bounds.width)/(beatSize*beatquant));
numlines.floor.do({ // vertical lines
| n |
var xpos = sidebarWidth+(n*(beatSize*beatquant));
if(((n*(beatSize*beatquant))-translate.x)%beatSize == 0, {
Pen.color_(Color.gray(1, 0.5));
}, {
Pen.color_(Color.gray(1, 0.125));
});
Pen.line(Point(xpos, translate.y.max(0)), Point(xpos, view.bounds.height));
Pen.stroke;
});
Pen.color_(Color.gray(1, 0.125));
((view.bounds.height-(translate.y).min(0))/vSize).floor.do { // horizontal lines
| n |
var ypos = translate.y+((1+n)*vSize);
Pen.line(Point(translate.x.max(0), ypos), Point(view.bounds.width, ypos));
};
Pen.stroke;
});
Pen.translate(sidebarWidth+translate.x, topbarHeight+translate.y);
sequence.rawList.do {
| note n |
if(note.notNil, {
var rect = this.noteToRect(note);
Pen.color_(case(
{ this.isSelected(note) }, {
selectedStrokeColor;
},
{ this.isHighlighted(note) }, {
highlightedStrokeColor;
},
true, {
Color.red;
},
));
Pen.addRect(rect);
Pen.draw(3);
Pen.color_(Color.black);
Pen.stringCenteredIn(note[\midinote].asString, rect);
Pen.stroke;
});
};
Pen.translate((sidebarWidth+translate.x).neg, (topbarHeight+translate.y).neg);
if(topbarHeight > 0, {
var hOffset = (translate.x.abs/beatSize).max(0).floor;
Pen.color_(Color.red(0.5));
Pen.addRect(Rect(0, 0, view.bounds.width, topbarHeight));
Pen.fill;
(view.bounds.width/beatSize).ceil.do({
| n |
var xpos = ((n+hOffset)*beatSize)+sidebarWidth+translate.x;
var num = (n+hOffset);
Pen.color_(if(num < this.sequence.dur, Color.black, Color.grey(0.5))); // show dur of sequence
Pen.stringCenteredIn(num.asString, Rect(xpos, 0, beatSize, topbarHeight));
});
Pen.stroke;
});
if(sidebarWidth > 0, {
var vOffset = (translate.y/vSize).min(0).floor.abs;
var sArray = (ySpec.minval, if(ySpec.step==0, ySpec.guessNumberStep, ySpec.step) .. ySpec.maxval);
Pen.color_(Color.red(0.5));
Pen.addRect(Rect(0, 0, sidebarWidth, view.bounds.height));
Pen.fill;
Pen.color_(Color.black);
(view.bounds.height/vSize).floor.do({ // numbers
| n |
var ypos = (n*vSize)+topbarHeight;
Pen.stringCenteredIn((ySpec.maxval-(vOffset+n)).asString, Rect(0, ypos, sidebarWidth, vSize));
});
Pen.stroke;
});
});
keymap = Keymap((
\space: {
| this |
this.playPause;
},
'S-space': {
| this |
this.playPause(loop:true);
},
\delete: {
| this |
var csel = this.selected.deepCopy;
csel.do({
| item |
this.delete(item);
});
this.deselectAll;
},
\enter: {
| this |
this.edit(if(this.selected.size==0, nil, this.selected));
},
['C-h', '?']: {
| this |
this.help;
},
));
uv.keyDownAction_({
| view char modifiers unicode keycode |
keymap.keyDown(Keymap.stringifyKey(modifiers, keycode)).value(this);
});
uv.mouseDownAction_({
| view x y modifiers buttonNumber clickCount |
case(
{ x <= sidebarWidth and: { y <= topbarHeight } }, { // clicked in the top left bit
if(clickCount == 2, {
"EVENT EDIT".postln; // FIX
});
},
{ x <= sidebarWidth }, { // clicked on the sidebar
"sidebar".postln;
},
{ y <= topbarHeight }, { // clicked on the top bar
if(buttonNumber == 1, { // right click to set dur...
var beat = this.pointToBeat((x@y));
this.sequence.dur_(if(beat < 1, nil, beat.round));
});
},
true, { // clicked in the note area
clickedOn = this.getAtPoint(x@y);
mouseDownPoint = x@y;
mouseDownButton = buttonNumber;
switch(buttonNumber,
0, {
switch(clickCount,
1, { // left click to select notes.
if(clickedOn.size > 0, { // clicked on something
var nrect = this.noteToRect(clickedOn[0]);
dragAction = nil;
if(( buttonNumber == 0 ) and: { (this.untranslatePoint(nrect.rightBottom) - (x@y)).x < 5 }, {
dragAction = \resize;
});
});
if(modifiers.isShift, {
clickedOn.do({
| item |
if(this.isSelected(item), {
this.deselect(item);
}, {
this.select(item);
});
});
}, {
if(clickedOn.size == 0, {
this.deselectAll;
}, {
if(this.isSelected(clickedOn[0]).not, {
this.deselectAll;
});
if(selected.size == 0, {
clickedOn.do({
| item |
this.select(item);
});
});
});
});
},
2, { // double left-click to make a note
if((clickedOn.size > 0) and: { selected.size > 0 }, {
Synth(\snare1);
// edit the note's properties directly - FIX
}, {
this.add((
midinote: (this.pointToY(x@y)+(vquant/2)).round(vquant),
sustain: this.newNoteSustain,
beat: (this.pointToBeat(x@y)-(beatquant/2)).round(beatquant),
));
});
},
);
},
1, {
this.getAtPoint(x@y).do({
| item |
this.delete(item);
});
},
);
},
);
this.refresh;
});
uv.mouseMoveAction_({
| uvw x y modifiers |
switch(mouseDownButton,
0, {
if(clickedOn.size == 0, { // draw the selection rect if we didn't click on anything.
var untranslatedRect = Rect.fromPoints(mouseDownPoint, x@y);
selectionRect = Rect.fromPoints(
this.translatePoint(mouseDownPoint),
this.translatePoint(x@y),
);
}, { // if we clicked on something, move or resize all selected notes
if(dragAction == \resize, { // FIX: just find the % of the resized note and apply it to all notes \sustain and \beat
var drag, c_beats, start, end, oldsize, newsize;
drag = ((x - mouseDownPoint.x)/beatSize).round(beatquant);
c_beats = selected.collect(_[\beat]);
start = c_beats.reduce(\min);
end = selected.sortBy(\beat);
end = end.reject({
| e |
e[\beat] < end.last[\beat];
});
end = end.collect(_[\sustain]).reduce(\max) + end[0][\beat];
oldsize = end - start;
newsize = (end + drag) - start;
ghosts = selected.deepCopy;
ghosts.do({
| item |
item[\sustain] = (item[\sustain] * (newsize/oldsize));
item[\beat] = ((item[\beat]-start)*(newsize/oldsize))+start;
});
}, {
ghosts = selected.deepCopy;
ghosts.do({
| item |
var diff = ((x@y) - mouseDownPoint);
item[\beat] = item[\beat] + (diff.x/beatSize).round(beatquant);
item[\midinote] = item[\midinote]?60 - (diff.y/vSize).round;
});
});
});
tview.refresh;
},
1, {
this.getAtPoint(x@y).do({
| item |
this.delete(item);
});
uv.refresh;
},
);
});
uv.mouseUpAction_({
| uvw x y modifiers |
if(selectionRect.notNil, { // select items in the rect on mouse up
var untranslatedRect = Rect.fromPoints(
this.untranslatePoint(selectionRect.leftTop),
this.untranslatePoint(selectionRect.rightBottom),
);
var items = this.getInRect(untranslatedRect);
items.do({
| item |
this.select(item);
});
selectionRect = nil;
});
if(ghosts.notNil, {
var os = selected.deepCopy;
var gh = ghosts.deepCopy;
if(modifiers.isAlt.not, {
os.do({
| item |
this.delete(item);
});
this.deselectAll;
});
gh.do({
| item |
this.add(item);
this.select(item);
});
ghosts = nil;
});
this.refresh;
});
uv.mouseWheelAction_({
| view x y modifiers xDelta yDelta |
var newtrans = this.translate;
newtrans.x = (newtrans.x + (xDelta.sign*(beatSize*beatquant)));
newtrans.y = (newtrans.y + (yDelta.sign*vSize));
this.translate_(newtrans);
});
this.centerOn(\midinote.asSpec.default);
}
bounds_ {
| bounds |
bounds = bounds;
view.bounds = bounds;
this.refresh;
}
newNoteSustain {
if(newNoteSustain==\last, {
if(this.sequence.list.size == 0, {
^1;
}, {
^this.sequence.rawList.last[\sustain];
});
}, {
^newNoteSustain;
});
}
newNoteSustain_ {
| sustain |
newNoteSustain = if(sustain.isKindOf(Number) or: { sustain == \last }, sustain, 1);
}
add {
| note |
sequence.add(note);
this.refresh;
}
delete {
| obj |
this.deselect(obj);
sequence.remove(obj);
this.refresh;
}
del {
| obj |
^this.delete(obj);
}
edit {
| obj |
if(obj.isNil, {
// edit the sequence
}, {
// edit the selected notes
});
}
sequence_ {
| seq |
sequence = seq ?? { Sequence.new; };
this.refresh;
}
getInRect { // returns the objects that are contained inside the provided Rect. don't provide a translated rect.
| rect |
var topYVal = this.pointToY(rect.top), botYVal = this.pointToY(rect.top+rect.height);
^sequence.eventsIn(this.pointToBeat(rect.left-this.translate.x), this.pointToBeat(rect.left+rect.width-this.translate.x))
.select({
| event |
((event[\midinote]?60 >= botYVal) and: { event[\midinote]?60 <= topYVal });
});
}
getAtPoint { // returns the objects that contain the provided Point.
| point |
var beat = this.pointToBeat(point);
var yVal = this.pointToY(point).ceil;
// point.postcs;
^sequence.rawList.select({
| event |
var end = event[\beat] + event[\sustain];
((event[\beat] <= beat) and: { beat < end } and: { event[\midinote]?60 == yVal });
});
}
isSelected {
| obj |
selected.do({
| item |
if(item.noteCompare(obj), {
^true;
});
});
^false;
}
select {
| obj |
if(this.isSelected(obj).not, { // FIX: don't select things that don't exist
selected = selected.add(obj);
});
}
deselect {
| obj |
selected = selected.reject(_.noteCompare(obj));
}
deselectAll {
selected = [];
}
isHighlighted {
| obj |
highlighted.do({
| item |
if(item.noteCompare(obj), {
^true;
});
});
^false;
}
highlight {
| obj |
if(this.isHighlighted(obj).not, { // FIX: don't select things that don't exist
highlighted = highlighted.add(obj);
});
this.refresh;
}
unhighlight {
| obj |
highlighted = highlighted.reject(_.noteCompare(obj));
this.refresh;
}
unhighlightAll {
highlighted = [];
}
notePlay {
| event name beat |
if(event.notNil, {
{this.highlight(event);}.defer(0);
{this.unhighlight(event)}.defer(event[\dur].beatstime);
});
}
pointToBeat { // return the "beat" at the point. must be a non-translated point. - FIX
| point | // should also accept numbers instead of just Points.
var ix = if(point.isKindOf(Point), { point.x }, point) - sidebarWidth;
^(ix/beatSize);
}
pointToY { // return the y value at the point. must be a non-translated point. - FIX
| point | // should also accept numbers instead of just Points.
var iy = if(point.isKindOf(Point), { point.y }, point) - topbarHeight;
^(ySpec.maxval-((iy-translate.y)/vSize));
}
beatToX { // return the (non-translated) x-position of the provided beat.
| beat |
^(beat*(beatSize));
}
noteToY { // return the (non-translated) (top) y-position of the provided 'midinote'.
| note |
^(vSize*(ySpec.maxval-note));
}
noteToRect {
| note |
^Rect(
this.beatToX(note[\beat]),
this.noteToY(note.use({ \midinote.envirGet.value; })?60),
(note.use({\sustain.envirGet.value;}))*beatSize, // FIX: use event.use{ \sustain.envirGet } or w/e instead
vSize
);
}
translate_ { // translation of the piano roll grid.
| point |
translate = Point(
point.x.min(0),
point.y.round(vSize).min(0).max(-1*((vSize*(ySpec.range+1))-(view.bounds.height-topbarHeight))),
);
this.refresh;
}
translatePoint { // translates a point on the view to a point in the piano roll (i.e. 0,0 on piano roll is actually 40,-940 on the whole view)
| point |
^Point(point.x-translate.x-sidebarWidth, point.y-translate.y-topbarHeight);
}
untranslatePoint {
| point |
^Point(point.x+translate.x+sidebarWidth, point.y+translate.y+topbarHeight);
}
grid_ {
| bool |
grid = bool;
this.refresh;
}
sidebarWidth_ {
| width |
sidebarWidth = width;
this.refresh;
}
ySpec_ {
| spec |
ySpec = spec.asSpec;
this.refresh;
}
vSize_ {
| size |
vSize = size;
this.refresh;
}
beatSize_ {
| size |
beatSize = size;
this.refresh;
}
beatquant_ {
| quant |
beatquant = quant;
this.refresh;
}
centerOn { // FIX
| num |
var numRows = view.bounds.height/vSize;
this.topOn(num+(numRows/2));
}
topOn { // make the number the top row in the view
| num |
this.translate = Point(this.translate.x, -1*vSize*(ySpec.maxval-num));
}
isPlaying {
^Pdef(\__pianoRoll).isPlaying;
}
play {
| start end loop=false |
Pdef(\__pianoRoll, PnC(Plazy({
var pattern = this.sequence.asPattern;
var pbeats = this.sequence.dur;
Pfwd(Psync(pattern, pbeats, pbeats), Message(this, \notePlay), \ );
}), {true.yield;loop{this.looping.yield;}}.asRoutine));
if(loop, {
this.looping = true;
}, {
this.looping = false;
});
Pdef(\__pianoRoll).play;
}
stop {
this.looping = false;
Pdef(\__pianoRoll).stop;
}
playPause {
| start end loop=false |
if(this.isPlaying, {
this.stop;
}, {
\play.postln;
this.play(start, end, loop);
});
}
refresh {
view.refresh;
}
help {
[
"C-e -- Zoom to selection", // FIX
"C-1 -- Zoom in", // FIX
"C-3 -- Zoom out", // FIX
"space -- Play",
"S-space -- Loop play", // FIX
"delete -- Delete selected notes",
"enter -- Edit selected notes or Sequence properties.", // FIX
"C-c C-s -- CmdPeriod", // FIX
].do({
| msg |
this.message(msg);
});
}
}
+ Event {
noteCompare { // compare two events by their \beat and \midinote keys.
// FIX - will need to consider other stuff instead of \midinote when other ySpecs are supported by PianoRoll.
| event |
^(event[\beat] == this[\beat] and: { event[\midinote] == this[\midinote] });
}
}