-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAutomationClient.sc
More file actions
708 lines (589 loc) · 18.3 KB
/
AutomationClient.sc
File metadata and controls
708 lines (589 loc) · 18.3 KB
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
AutomationClient is part of Automation, a SuperCollider Quark.
(c) 2009 Neels J. Hofmeyr <neels@hofmeyr.de>, GNU GPL v3 or later.
AutomationClient acts as a docking node for the Automation class.
Please first see Automation.
This class can dock to any GUI element like a slider or a button, or
anything else that satisfies below criteria, and make it subject to
Automation's control:
Live changes can be recorded, saved, loaded and replayed.
These are the criteria for a controllable thing, e.g. a GUI element:
controllableThing.value, controllableThing.value_
getter and setter of a member variable of any kind.
controllableThing.action, controllableThing.action_
getter and setter for a function variable. That function is called
whenever a new value needs to be propagated. This should be setup
before docking, the function gets sucked into AutomationClient.
and that by calling value_, action is not run implicitly.
Note: by docking a GUI element, the GUI element's action is replaced by
AutomationClient.internalAction, and its previous action is put in the
AutomationClient instance. If you want to change the action *after*
docking, you need to set the action in the AutomationClient instance, not
in the GUI element.
*/
/* Detect the type of a value of a GUI element and call appropriate typed file
* store/read functions. */
AutomationKind {
classvar <>valueKinds = nil;
*registerKinds{
valueKinds = List.new;
valueKinds.add(AutomationKindFloat());
valueKinds.add(AutomationKindBoolean());
valueKinds.add(AutomationKindInt());
valueKinds.add(AutomationKindString());
}
*get{|val|
var valueKind = nil;
if (valueKinds == nil) {
AutomationKind.registerKinds;
};
block{|break|
valueKinds.do{|kind|
if (kind.matches(val)) {
valueKind = kind;
break.value;
};
};
};
^valueKind;
}
putItem {|file, pos, val|
"invalid AutomationKind, nothing written".postln;
}
getItem {|file|
"invalid AutomationKind, nothing read".postln;
file.seek(0, 2);
^nil;
}
matches {|val|
^false;
}
}
AutomationKindFloat : AutomationKind {
putItem {|file, pos, val|
file.putItemFloat(pos, val);
}
getItem {|file|
^file.getItemFloat;
}
matches {|val|
^(val.isKindOf(Float));
}
}
AutomationKindInt : AutomationKindFloat {
matches {|val|
^(val.isKindOf(Integer));
}
}
AutomationKindBoolean : AutomationKind {
*asFloat {|val|
if (val){
^1.0;
}
^0.0;
}
*fromFloat {|val|
^val.asBoolean;
}
putItem {|file, pos, val|
file.putItemFloat(pos, AutomationKindBoolean.asFloat(val));
}
getItem {|file|
var item = file.getItemFloat;
item[1] = AutomationKindBoolean.fromFloat(item[1]);
^item;
}
matches {|val|
^(val.isKindOf(Boolean));
}
}
AutomationKindString : AutomationKind {
putItem {|file, pos, val|
file.putItemString(pos, val);
}
getItem {|file|
^file.getItemString;
}
matches {|val|
^(val.isKindOf(String));
}
}
AutomationFileBase {
var file = nil,
<controlledElement = "nil", <controlledValueKind = "nil";
classvar fileKinds = nil;
*registerKinds {
fileKinds = List.new;
fileKinds.add(AutomationFileText);
fileKinds.add(AutomationFileDouble);
}
*detect {|file|
if (fileKinds == nil) {
AutomationFileBase.registerKinds;
};
fileKinds.do{|kind|
if (kind.matches(file)) {
^kind.new(file);
};
};
^nil;
}
*matches {
^false;
}
forElement {|element|
if (element == nil) {
controlledElement = "nil";
controlledValueKind = "nil";
}{
controlledElement = "" ++ element.class
++ "@" ++ element.absoluteBounds.left
++ "x" ++ element.absoluteBounds.top;
controlledValueKind = "" ++ element.value.class;
};
}
startPut {
}
startGet {
}
putItemFloat {|pos, val|
"Float not implemented for this file type".postln;
}
getItemFloat {
"Float not implemented for this file type".postln;
file.seek(0, 2);
^nil;
}
putItemString {|pos, val|
"String not implemented for this file type".postln;
}
getItemString {
"String not implemented for this file type".postln;
file.seek(0, 2);
^nil;
}
hasMore {
^(file.pos < file.length);
}
close {
file.close;
}
}
/* Store just a stream of position and value doubles. Incapable of values that
* cannot be represented by a single floating point number. It raw data without
* header or type information, hence it always matches. */
AutomationFileDouble : AutomationFileBase {
*new {|ifile|
^super.new.constructor(ifile);
}
constructor {|ifile|
file = ifile;
}
*matches {|file|
^true;
}
putItemFloat {|pos, val|
file.putDouble(pos);
file.putDouble(val);
}
getItemFloat {
var pos, val;
pos = file.getDouble;
val = file.getDouble;
^[pos, val];
}
}
AutomationFileText : AutomationFileBase {
classvar expectFirstLine = "Automation txt v1";
*new {|ifile|
^super.new.constructor(ifile);
}
constructor{|ifile|
file = ifile;
}
*getFirstLine {|file|
var firstLine;
firstLine = file.getLine(expectFirstLine.size + 10);
^firstLine;
}
*matches {|file|
var l, m;
l = AutomationFileText.getFirstLine(file);
m = false;
if (l == expectFirstLine) {
m = true;
};
file.seek(0, 0);
^m;
}
startPut {
file.write(expectFirstLine ++ "\n");
file.write("K" + controlledElement + controlledValueKind ++ "\n");
}
startGet {
if (AutomationFileText.getFirstLine(file) != expectFirstLine) {
file.seek(0, 2);
};
}
putItemFloat {|pos, val|
file.write("f" + pos + val ++ "\n")
}
*parseFloat {|line|
^line.asFloat;
}
*parseString {|line|
^line;
}
parseKind {|tokens|
controlledElement = tokens[1];
controlledValueKind = tokens[2];
}
getItem {
var l, v, pos, valAt;
{
l = file.getLine(4096);
v = l.split($ );
case {v[0] == "K"}{
this.parseKind(v);
}
{v[0] == "f"}
{
pos = AutomationFileText.parseFloat(v[1]);
valAt = v[0].size + 1 + v[1].size + 1;
^[pos, AutomationFileText.parseFloat(l.copyToEnd(valAt))];
}
{v[0] == "s"}
{
pos = AutomationFileText.parseFloat(v[1]);
valAt = v[0].size + 1 + v[1].size + 1;
^[pos, AutomationFileText.parseString(l.copyToEnd(valAt))];
}
{ true }
{
Error("Unknown value type:" + v[0]).throw;
};
}.loop;
}
getItemFloat {
var item, val;
item = this.getItem;
val = item[1];
if (val.isKindOf(Float)) {
^item;
}{
if (val.isKindOf(String)) {
try {
^[item[0], val.toFloat];
}{|error|
("cannot parse" + val + "as number").postln;
^[item[0], 0]
};
}
};
Error("Unknown item type:" + val.class).throw;
}
putItemString {|pos, val|
file.write("s" + pos + val.replace("\n", "\\n") ++ "\n");
}
getItemString {
var item, val;
item = this.getItem;
val = item[1];
if (val.isKindOf(String)) {
^item;
}{
^[item[0], ""+val];
};
}
}
AutomationClient {
var <>automation = nil,
<>name = nil,
<>action = nil,
values = nil,
playCursor = -1, recordCursor = -1,
controllableThing = nil,
<valueKind = nil;
*new {|controllableThing, automation, name|
^super.new.constructor(controllableThing, automation, name);
}
constructor {|icontrollableThing, iautomation, iname|
controllableThing = icontrollableThing;
automation = iautomation;
name = iname;
values = List.new;
valueKind = AutomationKind.get(controllableThing.value);
if (valueKind == nil) {
("Unknown GUI value kind:" + controllableThing.value.class
+ "for" + controllableThing.class).postln;
};
action = controllableThing.action;
controllableThing.action = {|view| this.internalAction(view); };
automation.addClient(this);
}
stopRecording {
recordCursor = -1;
}
seek { |seconds|
this.stopRecording;
// optimize a rewind
if (seconds <= 0){
playCursor = -1;
};
this.bang(seconds);
}
value {
^controllableThing.value;
}
value_ {|val|
controllableThing.value_(val);
this.internalAction;
^val;
}
save {|dir|
var filename, f, file, backupname, item;
// add a trailing slash.
// TODO: only works on systems with a '/' file separator.
filename = dir;
if (filename.size > 0){
if (filename.at( filename.size - 1 ) != $/) {
filename = filename ++ $/;
};
};
// add my name to the dir with the trailing slash, as a filename
filename = filename ++ name;
// backup existing file?
if (File.exists(filename)) {
backupname = filename ++ ".backup_" ++ Date.getDate.stamp;
while({File.exists(backupname)},{
backupname = filename ++ "_" ++ 999.rand;
});
("mv" + filename + backupname).systemCmd;
};
// now write it out.
f = File(filename, "wb");
if (f.isOpen.not) {
("Automation: FAILED to open `" ++ filename ++ "'").postln;
^false;
};
file = AutomationFileText(f);
file.forElement(controllableThing);
file.startPut;
values.do{|row|
valueKind.putItem(file, row[0], row[1]);
};
file.close;
if (automation.verbose){
("Automation: Saved" + values.size + "values to `" ++ filename ++ "'").postln;
};
}
load {|dir|
var filename, file, pos, val;
// make sure we're not directly overwriting loaded values.
this.stopRecording;
// add a trailing slash.
// TODO: only works on systems with a '/' file separator.
filename = dir;
if (filename.size > 0){
if (filename.at( filename.size - 1 ) != $/) {
filename = filename ++ "/";
};
};
// add my name to the dir with the trailing slash, as a filename
filename = filename ++ name;
// read it in
file = File(filename, "rb");
if (file.isOpen.not) {
("Automation: FAILED to open `" ++ filename ++ "'").postln;
^false;
};
file = AutomationFileBase.detect(file);
if (file == nil) {
(""++filename++": unknown file type").postln;
^false;
};
try {
file.startGet;
values.free;
values = List.new;
// a double is 8 bytes, and there's two doubles per value
// (time and value).
block{|break|
{
if (file.hasMore.not) {
break.value;
};
values.add(valueKind.getItem(file));
}.loop;
};
file.close;
if (values.size < 1){
("Automation: NO VALUES in `" ++ filename ++ "'").postln;
^false;
};
if (automation.verbose){
("Automation:" + file.class + "loaded"
+ values.size + "values from `" ++ filename ++ "'"
+ file.controlledElement + file.controlledValueKind).postln
};
^true;
}{|error|
(""+filename++" ("++file.class++"): error: "++error.what).postln;
}
}
// Evaluate whether the internal event cursors should move and
// update the client gui accordingly.
// Return the absolute time of the next upcoming value.
bang{|nowtime|
var val;
// adjust playCursor position
if (playCursor > values.size){
playCursor = values.size - 1;
}{
if (playCursor < 0){
playCursor = -1;
};
};
// move backward?
while({ if (playCursor > 0) {
(nowtime < values[playCursor][0])
}{
false
};
}, {
playCursor = playCursor - 1;
});
// move forward?
while({ if ((playCursor + 1) < values.size) {
(nowtime >= values[playCursor + 1][0])
}{
false
}
}, {
playCursor = playCursor + 1;
});
if (recordCursor < 0) {
// we're in play mode. Set "slider"'s value.
if (playCursor >= 0){
val = values[playCursor][1];
automation.defer{
if (val != controllableThing.value){
controllableThing.value_(val);
action.value(controllableThing);
};
};
};
}{
// we're in recording mode.
// remove upcoming saved values that are after the
// recording cursor and pass "now".
if (playCursor > recordCursor) {
(playCursor - recordCursor).do{
values.removeAt(playCursor);
playCursor = playCursor - 1;
};
};
};
// return the time of the next value coming up after this one.
if ((playCursor + 1) >= values.size){
^inf;
}{
^values[playCursor+1][0];
};
}
// record a given time and value. You may pass a cursor at which
// to continue recording (for internal calls).
// Returns the new cursor index.
record { |time, val, cursor=(-1)|
var cursorTime, startedRecording;
startedRecording = false;
// if not recording yet, accurately determine the position.
if (cursor < 0){
startedRecording = true;
cursor = playCursor;
if ((cursor < 0) || (cursor >= values.size)){
cursor = -1;
};
// move backward?
while({ if (cursor >= 0){
(time < values[cursor][0])
}{ false }
}, {
cursor = cursor - 1;
});
// move forward?
while({ if ((cursor + 1) < values.size){
(time >= values[cursor + 1][0])
}{ false }
}, {
cursor = cursor + 1;
});
};
// record this value. But where to put it?
if (cursor < 0) {
// we're supposed to insert the item at the start.
cursorTime = -inf; // make the next condition pass
cursor = -1; // make sure it gets added at index 0
}{
// let's see when a new value would count as a separate
// time step (at least minTimeStep later).
cursorTime = values[cursor][0] + this.automation.minTimeStep;
};
if (time > cursorTime){
// the new value is well later than the current one.
// add after the current one.
cursor = cursor + 1;
values.insert(cursor, [time, val]);
// we've inserted a value, keep the playCursor stationary.
if (playCursor >= cursor){ playCursor = playCursor + 1; };
}{
// the new value's time is very close to the current one. Replace!
// Do not change the time though to avoid dragging this
// value along in case of rapidly incoming updates.
values[cursor][1] = val;
};
^cursor;
}
// records the GUI's current value at the given time.
// You may have to wrap this in a defer{ ... } (Mac).
snapshot {|now|
var cursor = recordCursor;
cursor = this.record(now, controllableThing.value);
if ((recordCursor >= 0) &&
(cursor > recordCursor) &&
(cursor <= playCursor)){
// oh no, we're interfering with the recording
// process. Let's fix it.
if (cursor > (recordCursor + 1)){
(cursor - (recordCursor + 1)).do{
values.removeAt(recordCursor);
playCursor = playCursor - 1;
};
};
recordCursor = recordCursor + 1;
};
}
// this is set up to be called upon an action by
// the GUI element (e.g. slider)
internalAction {|view|
var now, val, startedRecording;
// avoid negative values, these only show up when playLatency
// results in a negative now.
now = max(automation.now, 0.0);
val = controllableThing.value;
// call the action set by the user
action.value(view);
// now do the automation action
if (automation.doRecord){
startedRecording = (recordCursor < 0);
recordCursor = this.record(now, val, recordCursor);
if (startedRecording){
automation.clientStartsRecordingMsg;
};
}{
// Control's recording button is not pressed.
// Make sure recording is disabled.
recordCursor = -1;
};
}
}