/
Arduino2OSC.js
1738 lines (1316 loc) · 46.8 KB
/
Arduino2OSC.js
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Arduino2OSC Bridge
Convert Arduino sensor data into OSC messages.
For Max/MSP 4.6.
More info: http://vjacket.com
Copyright 2010 Tyler Freeman
http://odbol.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
autowatch = 1;
outlets = 7;
inlets = 5;
// sorry about the messy code; this used to be another project!
// - Tyler
//for include code at bottom
var includes = "";
/* have to include this at bottom of file since max gives JS errors otherwise!
include("xorax_serialize.js");
eval(includes); // initialise included code
includes = ""; //clear buffer
*/
//these control the lengths of automator recordings
const MAX_AUTOMATOR_REC_BUFFER = 50000;
const MIN_AUTOMATOR_REC_BUFFER = 10; //this is so the user can tap z to stop the animation
const ACCELERATION_THRESHOLD = 0.1; //TODO: adjust!
const ACCEL_MIN_WAND = 114; // the point where the accelerometer is at maxmium negative acceleration
const ACCEL_ZERO_WAND = 131; // the point where the accelerometer is at zero acceleration
const ACCEL_MAX_WAND = 200; // the point where the accelerometer is at maximum positive acceleration
const ACCEL_MIN_NUNCHUK = 108; // the point where the accelerometer is at maxmium negative acceleration
const ACCEL_ZERO_NUNCHUK = 126; // the point where the accelerometer is at zero acceleration
const ACCEL_MAX_NUNCHUK = 150; // the point where the accelerometer is at maximum positive acceleration
const MAX_CUT_INTERVAL = 20; //the maximum amount of time it takes for a cut gesture fade
//enable slow mo for movies
const SLOWEST_MOVIE_RATE = 0.1;
const OUTLET_DROPDOWN = 1;
const OUTLET_OSC = 0;
const OUTLET_SENSOR_UI = 2;
const NUM_DROPDOWNS = 4;
const DROPDOWN_NULL_VAL = "--"
const COL_NAME_ROTATION = 0;
const COL_NAME_X = 1;
const COL_NAME_Y = 2;
//set to true to print debug messages
var isDebug = false;
//isDebug = true;
// global variables for loading presets
var loadPreset1;
var loadAutomator1;
var loadPresetConstructorArray = [];
///// RESOLUME AVENUE OSC /////////
/*
I added the following 'wildcards' for OSC control.
- Use '/activeclip/' to control the currently selected clip. Instead of '/layer#/clip#/'.
- Use '/activelayer/' to control the currently selected layer. Instead of '/layer#/'.
- Use '/activetrack/' to control the currently selected track. Instead of '/track#/'.
http://www.resolume.com/avenue/manual/
http://resolume.com/forum/viewtopic.php?f=5&t=4226&start=40
http://www.resolume.com/forum/viewtopic.php?f=13&t=5632&start=10
*/
var dropdownOpts = [];
var avenueRouteOpts = [];
var avenueVidParams = [
"position/speed ${0}",
"position/direction ${0}",
"position/values ${0}",
"connect 1",
"opacity/values ${0}",
"width/values ${0}",
"height/values ${0}",
"scale/values ${0}",
"positionx/values ${0}",
"positiony/values ${0}",
"rotatex/values ${0}",
"rotatey/values ${0}",
"rotatez/values ${0}",
"anchorx/values ${0}",
"anchory/values ${0}",
"anchorz/values ${0}",
//more not in doc:
"rscale/values ${0}",
"gscale/values ${0}",
"bscale/values ${0}"
];
var avenueAudioParams = [
"pitch/values ${0}",
"position/direction ${0}",
"position/values ${0}"];
var avenueVidOpts = {"video": avenueVidParams,
"clip${0}": avenueVidParams,
"audio": avenueAudioParams};
//control effects per layer: /layer#/link[1-6]/values
//turn off: /layer#/video/effect1/bypassed 0
//all effects: /layer1/video/effect1/param2/values $1
var avenueVidEffectParams = ["opacity/values ${0}"];
//holds the names of each dropdown group column
var dropdownCols = [COL_NAME_ROTATION, COL_NAME_X];
//holds the names of each dropdown group (button names)
var dropdownGroups = [0, 1, 2, 3, 4, 5];
//holds the states of each dropdown group per column.
// key: colName
// value:
// key: groupName
// value: [groupName, dropValue1...dropValueN]
var dropdownStates = {};
//initializes our internal arrays on startup.
function initDropdownValues() {
dropdownStates = {};
for (var i in dropdownCols) {
debugPrint("\ndropdownstates[" + i + "]");
dropdownStates[i] = {};
}
avenueVidEffectParams = ["opacity/values ${0}"];
for (var i = 1; i <= 30; i++)
avenueVidEffectParams.push("param" + i + "/values ${0}");
avenueRouteOpts = [DROPDOWN_NULL_VAL, "composition"];
for (var i = 1; i <= 4; i++) {
avenueRouteOpts.push("layer" + i);
avenueVidOpts["video/effect" + i] = avenueVidEffectParams;
}
avenueRouteOpts.push("activeclip");
for (var i = 1; i <= 6; i++)
avenueVidOpts["link" + i + "/values ${0}"] = null;
dropdownOpts[0] = avenueRouteOpts;
dropdownOpts[1] = avenueVidOpts;
debugPrint("\ninit dropdown values: " + avenueVidEffectParams);
}
//resets the given dropdown group to defaults
function initDropdownGroup(colName, groupName) {
debugPrint("\ninitdropdownGroup(" + colName + ", " + groupName);
//reset internal states
dropdownStates[colName][groupName] = [null, null, null, null];
//clear all and ready to fill
for (var i = 0; i < NUM_DROPDOWNS; i++) {
outlet(OUTLET_DROPDOWN, colName, groupName, i, "clear");
}
//init first dropdown
for (var j = 0; j < dropdownOpts[0].length; j++) {
outlet(OUTLET_DROPDOWN, colName, groupName, 0, "append", dropdownOpts[0][j]);
}
//init rest
var firstIdx = null;
for (var j in dropdownOpts[1]) {
if (firstIdx == null)
firstIdx = j;
outlet(OUTLET_DROPDOWN, colName, groupName, 1, "append", j);
}
/* dbug
for (k in avenueVidEffectParams)
debugPrint("avenueVidEffectParams : " + avenueVidEffectParams[k]);
*/
//init following based on above selected
refreshDropdownGroup([colName, groupName, dropdownOpts[0][0], firstIdx, dropdownOpts[1][firstIdx][0]]);
}
//curValues is an array of the colName, groupName, then the current values of each dropdown in the group.
//DEPRICATED: not true: pass null to skip changing that dropdown's value.
function refreshDropdownGroup(curValues) {
var dIdx = curValues[3];
debugPrint("\nrefreshing " + curValues + " dIdx = " + dIdx);
var colName = curValues[0];
var groupName = curValues[1];
//disable output while we change values so we don't enter infinite recursion!
outlet(OUTLET_DROPDOWN, colName, groupName, "gate", 0);
//add all new values to dropdown.
//also check on some values to see if they are still valid
outlet(OUTLET_DROPDOWN, colName, groupName, 2, "clear");
var firstIdx = null;
var isCurVal4Valid = false;
for (var j in dropdownOpts[1][dIdx]) {
if (firstIdx == null)
firstIdx = j;
if (dropdownOpts[1][dIdx][j] == curValues[4])
isCurVal4Valid = true;
outlet(OUTLET_DROPDOWN, colName, groupName, 2, "append", dropdownOpts[1][dIdx][j]);
}
//replace last curValue with new, valid value, if they changed the second one
if (dropdownStates[colName][groupName][3] != curValues[3]) {
if (!isCurVal4Valid) {
if (firstIdx)
curValues[4] = dropdownOpts[1][dIdx][firstIdx];
else
curValues[4] = null;
}
}
//now set all the dropdowns to their last values
for (var i = 2; i < curValues.length; i++) {
if (curValues[i]) {
debugPrint("\ncurValue[" + i + "] = " + curValues[i]);
outlet(OUTLET_DROPDOWN, colName, groupName, i - 2, "setsymbol", curValues[i]);
}
}
//now save the current values as an internal state (man this is annoying. where's jQuery for Max!?!?!)
dropdownStates[colName][groupName] = curValues;
//make sure the pak list has the latest from ALL dropdowns in group!
for (var i = 0; i < NUM_DROPDOWNS; i++) {
outlet(OUTLET_DROPDOWN, colName, groupName, i, "bang");
}
//enable output again
outlet(OUTLET_DROPDOWN, colName, groupName, "gate", 1);
}
//////////////////////////////
// Arudino Sensor UI states //
//////////////////////////////
const SENSOR_UI_CUTOFF_DROPDOWN = 6;
//holds the states of each sensor UI element per group.
// key: groupName
// value: [groupName, dropValue1...dropValueN]
var sensorUIStates = {};
function initSensorUIValues() {
for (var i in dropdownGroups) {
var state = [];
for (var j = 0; j < 10; j++) {
var val = 0;
if (j == 8)
val = 1;
state.push([i, j, val]);
}
sensorUIStates[i] = state; //[null, null, null, null, null, null, null, null, null, null];
}
}
function initSensorUIValuesFromUI() {
for (var i in dropdownGroups) {
for (var j in sensorUIStates) {
outlet(OUTLET_SENSOR_UI, i, j, "bang");
}
}
}
//curValues is an array of the groupName, then the index and then current value of ONE UI element in the group.
function refreshSensorUIGroup(curValues) {
debugPrint("\nrefreshing sensorUI " + curValues);
if (!curValues)
return;
var groupName = curValues[0];
var routeNum = curValues[1];
//disable output while we change values so we don't enter infinite recursion!
outlet(OUTLET_SENSOR_UI, groupName, "gate", 0);
//add all new values to sensor ui elements.
//for (var i = 1; i < curValues.length; i++) {
var i = 2;
if (curValues[i]) {
debugPrint("\ncurValue[" + i + "] = " + curValues[i]);
//update the UI element's value based on its type
//switch(sensorUITypes[i]) {
// case "slider", "switch":
outlet(OUTLET_SENSOR_UI, groupName, routeNum, curValues[i]);
// break;
// case "dropdown":
// outlet(OUTLET_SENSOR_UI, groupName, i - 1, "set", curValues[i]);
// break;
//}
}
//}
//now save the current values as an internal state (man this is annoying. where's jQuery for Max!?!?!)
sensorUIStates[groupName][routeNum] = curValues;
//enable output again
outlet(OUTLET_SENSOR_UI, groupName, "gate", 1);
}
function saveSensorUIGroup(curValues) {
debugPrint("\nsaving sensorUI " + curValues);
if (!curValues)
return;
var groupName = curValues[0];
var routeNum = curValues[1];
//now save the current values as an internal state (man this is annoying. where's jQuery for Max!?!?!)
sensorUIStates[groupName][routeNum] = curValues;
}
//build a proper OSC path from the internal state of the given dropdown, or returns
//null on error or empty state
//state is an array of values to join into a path.
function getOSCPathFromDropdown(state) {
var oscCmd = "";
for (var j = 2; j < state.length; j++) { //skip the dropdown columnName and groupName
if (state[j] && state[j] != "null" && state[j] != DROPDOWN_NULL_VAL)
oscCmd += "/" + state[j];
else
break;
}
if (oscCmd.length > 1) {
//oscCmd += "/values";
return oscCmd;
}
else
return null;
}
//replaces all occurences of variables "${varName}" in oscPath
//with the respective values in valuesArray and returns a valid OSC command array of [path, value]
function substituteOSC(oscPath, valuesArray) {
if (!oscPath)
return null;
for (var j in valuesArray) {
oscPath = oscPath.replace(new RegExp("\\$\\{" + j + "\\}", "g"), valuesArray[j]);
}
if (oscPath.length > 1) {
//oscCmd += "/values";
//post(oscPath.split(" ").join("|"));
//must convert all string values to floats if possible
var oscPathSplit = oscPath.split(" ");
var oscCmd = [];
for (var i in oscPathSplit) {
var num = parseFloat(oscPathSplit[i]);
if (isNaN(num))
oscCmd.push(oscPathSplit[i]);
else
oscCmd.push(num);
}
return oscCmd;
}
else
return null;
}
//sendAndResetOSCBuffer() sends all collected OSC messages (from sendOSC())
//in a bundle and resets the buffer.
var oscBufferFull = false;
function sendAndResetOSCBuffer(routeNum) {
//debugPrint("\nsendAndResetOSCBuffer(" + routeNum + ", " + oscBufferFull);
if (oscBufferFull)
outlet(routeNum, "bang");
oscBufferFull = false;
}
//sends an OSC message through the given routeNum.
//oscMsg can be a string or an array of strings.
//buffers the messages to send them all in a bundle at the end
function sendOSC(routeNum, oscMsg) {
outlet(routeNum, oscMsg);
//debugPrint("\nsendOSC(" + routeNum + ", " + oscMsg);
oscBufferFull = true;
}
//this is the outlet that feeds back into the wii object
var wiiOutlet = outlets - 1;
var presetLoaderOutlet = outlets - 2;
var clips; //holds an effects array that is being maniuplated
//var effects; //holds effects for a certain clip
var mixer;
//holds an array of PresetQueue objects for each layer.
var presetQueues;
var wand;
var curButtons;
var nunchuk;
var plusDown; //indicates plus is held down
var isCutting = false; //indicates holding C to cut mixer
var testMod = 0; //used for testing parameters autogenerated by viideoifier.py
function debugPrint(msg) {
if (isDebug)
post(msg);
}
//this returns a copy of the object what.
//stolen from: http://www.irt.org/script/879.htm
function cloneObject(what) {
for (i in what) {
if (typeof what[i] == 'object') {
this[i] = new cloneObject(what[i]);
}
else
this[i] = what[i];
}
}
//returns the first key of the given associative array
function getFirstKey(obj) {
for (var j in obj)
return j;
}
//sets respective LEDs to on (1) or off (0).
function setLEDs(one, two, three, four) {
//post("should set LEDS here");
outlet(wiiOutlet, "led", one, two, three, four);
}
//returns a name with a randomized number at the end
function getRandomName() {
var randNum = Math.floor(max.time);
return randNum.toString().slice(-5);
}
//returns a filehandle to an opened file for writing.
//if no filename specified, it makes one up.
function openFile(fileName) {
if (fileName == undefined || fileName == "") {
var randName = getRandomName();
fileName = "auto" + randName + ".js";
}
f = new File(fileName, "write", "TEXT");
post("\nopened file " + f.filename);
return f;
}
function saveToFile(f, txt) {
if (f == null || !f.isopen) {
f = openFile();
}
f.writestring(txt);
return f;
}
function closeFile(f) {
if (f != null) {
f.close();
post("\nclosed file " + f.filename);
}
f = null;
}
//SCALING UTILITIES
//scales a number to fit within the new extrema.
//if hardCutoff is true, it will not exceed the extrema
function scaleRange(num, oldMin, oldMax, newMin, newMax, hardCutoff)
{
var n = ((num - oldMin) / ((oldMax - oldMin) / (newMax - newMin))) + newMin;
if (hardCutoff == true) {
if (n < newMin)
n = newMin;
else if (n > newMax)
n = newMax;
}
// post("\nscaleRange(" + num + " = " + n + " < " + newMax);
return n;
}
//scales a number using different scales depending if num is greater or less than the zero point oldZero.
//if hardCutoff is true, it will not exceed the extrema
function scaleWeightedRange(num, oldMin, oldZero, oldMax, newMin, newMax, hardCutoff) {
var newZero = scaleRange(0.5, 0, 1, newMin, newMax);
if (num > oldZero)
return scaleRange(num, oldZero, oldMax, newZero, newMax, hardCutoff);
else
return scaleRange(num, oldMin, oldZero, newMin, newZero, hardCutoff);
}
function scaleIR(num, min, max) {
/*
range = (num + 1.0) / 2.0;
return (range * (max - min)) + min;*/
return scaleRange(num, -1.0, 1.0, min, max);
}
function scaleIRAngle(num, min, max) {
//post("\nwas:" + (((num + 1.5707965 / 3.141593) * (max - min)) + min));
/*range = (num + 1.5707965) / 3.141593;
return (range * (max - min)) + min;
*/
return scaleRange(num, -3.141593, 3.141593, min, max);
}
function scaleMotion(num, min, max) {
/*range = (num - 60.0) / 130.0;
return (range * (max - min)) + min;*/
return scaleRange(num, 10.0, 130.0, min, max);
}
//this is which clip we are currently manipulating
//it is controlled by the d pad. you can press it once to select,
//or hold one or more directions down to manipulate two at once. NOT
var curClip;
function setCurClip() {
if (curButtons != null) {
if (curButtons.down) {
setLEDs(0,0,1,0);
curClip = getLayerIdxFromDPad(curButtons);
}
if (curButtons.up) {
setLEDs(1,0,0,0);
curClip = getLayerIdxFromDPad(curButtons);
}
if (curButtons.left) {
setLEDs(0,0,0,4);
curClip = getLayerIdxFromDPad(curButtons);
}
if (curButtons.right) {
setLEDs(0,1,0,0);
curClip = getLayerIdxFromDPad(curButtons);
}
}
//post("curClip: " + curClip + "\n");
}
var curEffect;
function setCurEffect() {
curEffect = 0;
if (nunchuk != null) {
curEffect = getLayerIdxFromDPad(nunchuk);
}
}
//returns the layer index for the direction being held down on the given buttons object
function getLayerIdxFromDPad(buttons) {
if (buttons.down) {
return 3;
}
if (buttons.up) {
return 1;
}
if (buttons.left) {
return 4;
}
if (buttons.right) {
return 2;
}
return 0;
}
///////////////////////////////////////////////////////////////////////////
//buttons class
function Buttons(a) {
this.refresh(a);
}
Buttons.prototype.src;
Buttons.prototype.a;
Buttons.prototype.b;
Buttons.prototype.one;
Buttons.prototype.two;
Buttons.prototype.left;
Buttons.prototype.right;
Buttons.prototype.up;
Buttons.prototype.down;
Buttons.prototype.minus;
Buttons.prototype.plus;
Buttons.prototype.home;
Buttons.prototype.refresh = function (a) {
this.src = a; //TODO: remove this and use JSON instead!
var n = a[2];
this.two = n & 1;
this.one = n & 2;
this.b = n & 4;
this.a = n & 8;
this.minus = n & 16;
this.home = n & 128;
this.left = n & 256;
this.right = n & 512;
this.down = n & 1024;
this.up = n & 2048;
this.plus = n & 4096;
};
///////////////////////////////////////////////////////////////////////////
//Automator class
//controlInstance is a control or effect object to automate.
//if oscillate is true, once the recording ends it runs backwards,
//otherwise it will start from the begining.
function Automator(controlInstance, oscillate) {
this.control = controlInstance;
this.commands = new Array();
this.buttons = new Array();
this.curCommand = -1;
this.oscillate = oscillate;
this.reverse = false;
}
Automator.prototype.oscillate;
//indicates if the loop is currently oscillating backwards
Automator.prototype.reverse;
//this is a pointer to the control or effect asscociated with this automator
Automator.prototype.control;
Automator.prototype.isRecording;
//this is an array of recorded input arrays
Automator.prototype.commands;
//this is an array of recorded buttons
Automator.prototype.buttons;
//this is an array of recorded nunchuk buttons
//Automator.prototype.nunchuk;
//index in the command arrays
Automator.prototype.curCommand;
//int keeps track of the frameskip rate (for acceleration of automations)
Automator.prototype.curRate = 1;
//runs recorded commands in a loop
Automator.prototype.run = function(a, b) {
// post("auto.running: " + this.isRecording + " || " + this.commands.length + "\n");
if (this.isRecording || this.commands.length < MIN_AUTOMATOR_REC_BUFFER)
return;
//figure out rate of frameskip (1 is no frames skipped)
//and slowly reduce frameskip to 0.
if (this.curRate > 1) {
this.curRate--;
//debugPrint("\nnew curRate: " + this.curRate);
}
else
this.curRate = 1;
if (this.reverse) {
this.curCommand -= this.curRate;
if (this.curCommand < 1) {
this.reverse = false;
this.curCommand = 0;
}
}
else {
this.curCommand += this.curRate;
if (this.curCommand >= this.commands.length) {
if (this.oscillate) {
this.curCommand = this.commands.length - 2;
this.reverse = true;
}
else
this.curCommand = 0;
}
}
// debugPrint("running " + this.curCommand + " on " + this.buttons[this.curCommand].b + "\n");
this.control.run(this.commands[this.curCommand], this.buttons[this.curCommand]);
}
//records commands for later use.
//takes a (wii input) and b (curButtons) to record.
Automator.prototype.record = function(a, b) {
if (++this.curCommand > MAX_AUTOMATOR_REC_BUFFER) {
post("reached record limit");
this.stopRecording();
return;
}
this.isRecording = true;
this.commands.push(a);
this.buttons.push(new cloneObject(b));
}
Automator.prototype.stopRecording = function() {
post("stopped recording");
this.isRecording = false;
}
//saves current automation to filehandle f
//if f is provided then it will not write a loading function, just the member arrays.
Automator.prototype.save = function(f) {
var isStandAlone = false;
if (!f) {
isStandAlone = true;
f = openFile(this.outletNum + "-" + this.routeNum + "automator" + getRandomName() + ".js");
debugPrint("serializing commands:\n");
if (this.control.file != undefined) {
saveToFile(f, "\n//for effect: " + this.control.file + "\n");
}
saveToFile(f, "\nloadAutomator1 = function(theEffect) {\n");
saveToFile(f, " theEffect.automator = new Automator(theEffect, true);\n");
saveToFile(f, " var buttonsA = theEffect.automator.buttons;\n");
saveToFile(f, " var commandsA = theEffect.automator.commands;\n");
}
//save automator arrays
for (var k = 0; k < this.commands.length; k++) {
saveToFile(f, " commandsA.push(unserialize(" + serialize(this.commands[k]) + "));\n");
}
saveToFile("\n");
for (var k = 0; k < this.buttons.length; k++) {
saveToFile(f, " buttonsA.push(new Buttons(" + this.buttons[k].src + "));\n"); //TODO: write out JSON
}
if (isStandAlone) {
saveToFile(f, " return theEffect.automator;\n};\n\n");
closeFile(f);
}
}
///////////////////////////////////////////////////////////////////////////
//Control class
function Control(outletNum, routeNum) {
this.routeNum = routeNum;
this.outletNum = outletNum;
}
Control.prototype.outletNum;
Control.prototype.routeNum;
//this is a pointer to the automator.
//if it is not null, the automator gets run instead of the run() function.
//in turn it will run this object's run() function, but with pre-recorded
//values instead of live user input.
Control.prototype.automator;
//this is the function to run
Control.prototype.run;
//this should always be run at the begining of the run() function
Control.prototype.run_super = function(a, buttons) {
return;
}
//this triggers one frame of the automator, or whatever other
//behaviour might be happening (accleration, etc).
Control.prototype.runAutomation = function(a, buttons) {
if (this.automator != null) {
this.automator.run(a, buttons);
}
}
//name of the function to create this object. (for writing preset files)
Control.prototype.constructor = "Control";
Control.prototype.isActive = true; //controls are always active (dummy value)
//this saves the effect's parameters as a preset to the filehandle f.
//it handles saving of the automation and other general members.
//it will call saveExtras() which should be overridden to save object-specific members.
//set preserveRouteNum for a control. false for an effect (maybe not necessary??) TODO
Control.prototype.save_super = function (f, preserveRouteNum) {
if (!this.isActive)
return;
var openedFile = false;
if (f == undefined) {
var filename = this.outletNum + "-" + this.routeNum + "control" + getRandomName() + ".js";
post("\nopening preset file: " + filename);
f = openFile(filename);
openedFile = true;
}
//if routeNum is a string, we have to fix it for inclusion in the code.
/*
var routeNumSerialized = parseInt(this.routeNum);
if (!(routeNumSerialized > 0))
routeNumSerialized = "'" + this.routeNum + "'"
*/
post("\nsaving control preset " + this.outletNum + "/" + this.routeNum);
if (this["file"] != undefined) {
saveToFile(f, "\n//for effect file: " + this.file);
}
saveToFile(f, "\n//for control: " + this.outletNum + "/" + this.routeNum + "\n");
saveToFile(f, "\nfunction(outletNum, routeNum) {\n");
if (preserveRouteNum) //control routeNums are special and should be preserved
saveToFile(f, " routeNum = " + serialize(this.routeNum) + "; \n");
saveToFile(f, " var fx = new " + this.constructor + "(outletNum, routeNum);\n");
//saveToFile(f, " fx.;\n");
//save child class members
this.saveExtras(f);
//save automator
if (this.automator != undefined) {
saveToFile(f, " fx.automator = new Automator(fx, true);\n");
saveToFile(f, " var buttonsA = fx.automator.buttons;\n");
saveToFile(f, " var commandsA = fx.automator.commands;\n");
this.automator.save(f);
}
saveToFile(f, "\n return fx;\n}\n");
if (openedFile)
closeFile(f);
return f;
}
Control.prototype.save = function (f) {
this.save_super(f, true);
}
//this saves the effect's parameters as a preset to the filehandle f.
//this is the default and for most effects should be overridden,
//writing members specific to the "fx" object.
Control.prototype.saveExtras = function (f) {
if (f == undefined)
return;
saveToFile(f, "\n \n");
}
Control.prototype.dispose = function () {
debugPrint("\ncontrol dispose");
}
Control.prototype.makeActive = function () {
debugPrint("\ncontrol makeActive");
}
//TRIGGER FUNCTIONS
//triggers a reverse of the automation, if playing.
//this is generic to all controls with an animation.
Control.prototype.trigReverse = function(speed) {
if (this.automator)
this.automator.reverse = !this.automator.reverse;
}
//triggers an accelleration of the automation, if playing.
//speed is an int: 1 is normal.
//this is generic to all controls with an animation.
Control.prototype.trigBoost = function(speed, deceleration) {
if (this.automator) {
this.automator.curRate = Math.round(speed);
//debugPrint("\ncurRate: " + this.automator.curRate);
}
}
///////////////////////////////////////////////////////////////////////////
//effect class
function Effect(fileName, outletNum, routeNum) {
this.file = fileName;
this.routeNum = routeNum;
this.outletNum = outletNum;
//load the effect and get rid of the old one
outlet(this.outletNum, new Array(this.routeNum, "dispose"));
//don't load the effect until they run it.
//outlet(this.outletNum, new Array(this.routeNum, "read", fileName));
this.isActive = false;
}
Effect.prototype.file;
Effect.prototype.outletNum;
Effect.prototype.routeNum;
Effect.prototype.isActive;
//this is a pointer to the automator.
//if it is not null, the automator gets run instead of the run() function.
//in turn it will run this object's run() function, but with pre-recorded
//values instead of live user input.
Effect.prototype.automator;
Effect.prototype.run; //this is the function to run
//this should always be run at the begining of the run() function
//it will only reload an effect if user is trying to use it (i.e. pressing buttons)
Effect.prototype.run_super = function(a, buttons) {
if (!this.isActive && (buttons.a || buttons.b || buttons.one || buttons.two)) {
this.makeActive();
}
}
//turns on an effect
Effect.prototype.makeActive = function() {
outlet(this.outletNum, new Array(this.routeNum, "read", this.file));
this.isActive = true;
}
//this temporarily disables the effect
Effect.prototype.dispose_super = function () {
if (this.isActive) {
this.automator = null; //stop automation from running on a disposed effect
outlet(this.outletNum, new Array(this.routeNum, "dispose"));
this.isActive = false;
}
}
//this temporarily disables the effect
Effect.prototype.dispose = function () {
this.dispose_super();
}