-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction_classes.js
More file actions
1681 lines (1510 loc) · 51.1 KB
/
function_classes.js
File metadata and controls
1681 lines (1510 loc) · 51.1 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
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
// FUNCTIONS
var BATTERY_LEVEL=24000
class BatteryLevelClass extends FUNCTION {
constructor() {
super("Returns 0-32768 based on battery level.", []);
}
run(blade) {}
getInteger(led) { return 32768 - ((millis() * 3) & 0x7fff); }
};
function BatteryLevel() {
return new BatteryLevelClass();
}
class VolumeLevelClass extends FUNCTION {
constructor() {
super("Returns 0-32768 based on volume level.", []);
}
run(blade) {}
// getInteger(led) { return 0 + ((millis() * 7) & 0x7fff); }
getInteger(led) { return 0 + ((millis() / 500) % 11) * 32767 / 10; }
};
function VolumeLevel() {
return new VolumeLevelClass();
}
class BumpClass extends FUNCTION {
constructor() {
super("Function returning a bump shape", arguments);
this.add_arg("BUMP_POSITION", "FUNCTION", "0=bump at hilt, 32768=bump at tip");
this.add_arg("BUMP_WIDTH_FRACTION", "FUNCTION", "bump width", Int(16384));
}
run(blade) {
this.BUMP_POSITION.run(blade);
this.BUMP_WIDTH_FRACTION.run(blade);
var fraction = this.BUMP_WIDTH_FRACTION.getInteger(0);
if (fraction == 0) {
this.mult = 1;
this.location = -10000;
return;
}
this.mult = 32 * 2.0 * 128 * 32768 / fraction / blade.num_leds();
this.location = (this.BUMP_POSITION.getInteger(0) * blade.num_leds() * this.mult) / 32768;
}
getInteger(led) {
var dist = Math.abs(led * this.mult - this.location);
var p = dist >> 7;
if (p >= 32) return 0;
var m = dist & 0x3f;
return blast_hump[p] * (128 - m) + blast_hump[p+1] * m;
}
};
function Bump(P, F) {
return new BumpClass(P, F);
}
//////////// indents and line returns PR ///////////
class ChangeSlowlyClass extends FUNCTION {
constructor(F, SPEED) {
super("Changes F by no more than SPEED values per second.", arguments);
this.add_arg("F", "FUNCTION", "Function to moderate");
this.add_arg("SPEED", "FUNCTION", "maximum change speed");
this.last_micros = micros();
this.value = 0;
}
run(blade) {
super.run(blade);
var now = micros();
var delta = now - this.last_micros;
if (delta > 1000000) delta = 1;
this.last_micros = now;
delta *= this.SPEED.getInteger(0);
delta /= 1000000;
var target = this.F.getInteger(0);
if (delta > Math.abs(this.value - target)) {
this.value = target;
} else if (this.value < target) {
this.value += delta;
} else {
this.value -= delta;
}
}
getInteger(led) { return this.value; }
}
function ChangeSlowly(F, SPEED) {
return new ChangeSlowlyClass(F, SPEED);
}
class IfonClass extends FUNCTION {
constructor(A, B) {
super("A if on, B if off.", arguments);
this.add_arg("A", "FUNCTION", "A");
this.add_arg("B", "FUNCTION", "B");
}
run(blade) {
this.A.run(blade);
this.B.run(blade);
this.on = blade.is_on();
}
getInteger(led) {
if (this.on) return this.A.getInteger(led);
return this.B.getInteger(led);
}
};
function Ifon(A, B) { return new IfonClass(A, B); }
class InOutFuncXClass extends FUNCTION {
constructor(OUT_MILLIS, IN_MILLIS) {
super("0 when off, 32768 when on, OUT_MILLIS/IN_MILLIS determines speed in between.", arguments);
this.add_arg("OUT_MILLIS", "FUNCTION", "millis to ramp up");
this.add_arg("IN_MILLIS", "FUNCTION", "millis to ramp down");
this.last_micros = 0;
this.extension = 0.0;
}
run(blade) {
super.run(blade);
var now = micros();
var delta = now - this.last_micros;
if (delta < 0 || delta > 1000000) delta = 1;
this.last_micros = now;
if (blade.is_on()) {
if (this.extension == 0.0) {
this.extension = 0.00001;
} else {
this.extension += delta / (this.OUT_MILLIS.getInteger(0) * 1000.0);
this.extension = Math.min(this.extension, 1.0);
}
} else {
this.extension -= delta / (this.IN_MILLIS.getInteger(0) * 1000.0);
this.extension = Math.max(this.extension, 0.0);
}
this.ret = this.extension * 32768;
}
getInteger(led) { return this.ret; }
argify(status) {
state.int_argument = IGNITION_TIME_ARG;
this.OUT_MILLIS = this.OUT_MILLIS.argify(status);
state.int_argument = RETRACTION_TIME_ARG;
this.IN_MILLIS = this.IN_MILLIS.argify(status);
return this;
}
};
function InOutFuncX(O, I) {
return new InOutFuncXClass(O, I);
}
function InOutFunc(O, I) {
return InOutFuncX(Int(O), Int(I));
}
// TODO: InOutFuncTD
class IntClass extends FUNCTION {
constructor(N) {
super("Constant integer function", arguments);
this.add_arg("N","INT","number to return.");
}
getInteger(led) { return this.N; }
pp() {
if (pp_is_url) {
if (this.super_short_desc) return "$";
return this.gencomment() + "Int<" + this.N + ">";
}
return this.PPshort("Int<" + this.N +">", "VALUE");
}
argify(state) {
if (state.int_argument) {
ret = IntArg_(ArgumentName(state.int_argument), this.N);
state.int_argument = false;
return ret;
} else {
return this;
}
}
};
function Int(n) { return new IntClass(Math.round(n)); }
class IntArgClass extends FUNCTION {
constructor(ARG, N) {
super("Dynamic Integer argument", arguments);
this.add_arg("ARG","ArgumentName","argument number.");
this.add_arg("DEFAULT","INT","default.");
}
run(blade) {
super.run(blade);
this.value = parseInt(getARG(this.ARG, "" + this.DEFAULT));
}
getInteger(led) { return this.value; }
argify(state) {
if (state.int_argument == this.ARG) {
state.int_argument = false;
}
return this;
}
};
function IntArg_(ARG, N) {
return new IntArgClass(ARG, N);
}
class RgbArgClass extends STYLE {
constructor(ARG, N) {
super("Dynamic Color argument", arguments);
this.add_arg("ARG","ArgumentName","number to return.");
this.add_arg("DEFAULT","COLOR","default.");
}
run(blade) {
super.run(blade);
var d = Math.round(this.DEFAULT.r * 65535) + "," + Math.round(this.DEFAULT.g * 65535)+ "," +Math.round(this.DEFAULT.b * 65535);
var v = getARG(this.ARG, d).split(",");
this.value = Rgb16(parseInt(v[0]), parseInt(v[1]), parseInt(v[2]));
}
getColor(led) { return this.value; }
argify(state) {
if (state.color_argument == this.ARG) {
state.color_argument = false;
}
return this;
}
};
function RgbArg_(ARG, COLOR) {
return new RgbArgClass(ARG, COLOR);
}
class ScaleClass extends FUNCTION {
constructor(F, A, B) {
super("Changes values in range 0-32768 to A-B.", arguments);
this.add_arg("F","FUNCTION","input");
this.add_arg("A","FUNCTION","lower output limit");
this.add_arg("B","FUNCTION","upper output limit");
}
run(blade) {
super.run(blade);
var a = this.A.getInteger(0);
var b = this.B.getInteger(0);
this.mul = (b - a);
this.add = a;
}
getInteger(led) {
return (this.F.getInteger(led) * this.mul >> 15) + this.add;
}
};
function Scale(F, A, B) { return new ScaleClass(F, A, B); }
class InvertFClass extends MACRO {
constructor(F) {
super("Invert input function", arguments);
this.add_arg("F", "FUNCTION", "Function to invert.");
this.SetExpansion(Scale(this.F, Int(32768), Int(0)));
}
};
function InvertF(F) { return new InvertFClass(F); }
class SinClass extends FUNCTION {
constructor(RPM, LOW, HIGH) {
super("Pulses between LOW and HIGH RPM times per minute.", arguments);
this.add_arg("RPM", "FUNCTION", "Revolutions per minute");
this.add_arg("HIGH", "FUNCTION", "upper output limit", Int(32768));
this.add_arg("LOW", "FUNCTION", "lower output limit", Int(0));
this.pos = 0.0;
this.last_micros = 0;
}
run(blade) {
super.run(blade);
var now = micros();
var delta = now - this.last_micros;
this.last_micros = now;
this.pos = fract(this.pos + delta / 60000000.0 * this.RPM.getInteger(0));
var high = this.HIGH.getInteger(0);
var low = this.LOW.getInteger(0);
var tmp = Math.sin(this.pos * Math.PI * 2.0) / 2.0;
this.value = Math.floor( (tmp + 0.5) * (high - low) + low );
}
getInteger(led) { return this.value; }
};
function Sin(RPM, LOW, HIGH) { return new SinClass(RPM, LOW, HIGH); }
class SawClass extends FUNCTION {
constructor(RPM, LOW, HIGH) {
super("Pulses between LOW and HIGH RPM times per minute.", arguments);
this.add_arg("RPM", "FUNCTION", "Revolutions per minute");
this.add_arg("HIGH", "FUNCTION", "upper output limit", Int(32768));
this.add_arg("LOW", "FUNCTION", "lower output limit", Int(0));
this.pos = 0.0;
this.last_micros = 0;
}
run(blade) {
super.run(blade);
var now = micros();
var delta = now - this.last_micros;
this.last_micros = now;
this.pos = fract(this.pos + delta / 60000000.0 * this.RPM.getInteger(0));
var high = this.HIGH.getInteger(0);
var low = this.LOW.getInteger(0);
this.value = Math.floor(low + this.pos * (high - low));
}
getInteger(led) { return this.value; }
};
function Saw(RPM, LOW, HIGH) { return new SawClass(RPM, LOW, HIGH); }
const TRIGGER_DELAY = 0;
const TRIGGER_ATTACK = 1;
const TRIGGER_SUSTAIN = 2;
const TRIGGER_RELEASE = 3;
const TRIGGER_OFF = 4;
class TriggerClass extends FUNCTION {
constructor(EFFECT, FADE_IN_MILLIS, SUSTAIN_MILLIS, FADE_OUT_MILLIS, DELAY_MILLIS) {
super("When EFFECT occurs, DELAY_MILLIS controls a pause, then we ramp up from 0 to 32768, stay there for SUSTAIN_MILLIS, then ramp down again.", arguments);
this.add_arg("EFFECT", "EFFECT", "Trigger event");
this.add_arg("FADE_IN_MILLIS", "FUNCTION", "How long it takes to ramp to 32768");
this.add_arg("SUSTAIN_MILLIS", "FUNCTION", "Stay at 32768 for this long.");
this.add_arg("FADE_OUT_MILLIS", "FUNCTION", "How long it takes to ramp back down to zero.");
this.add_arg("DELAY_MILLIS", "FUNCTION", "How long to delay before trigger starts.", Int(0));
this.trigger_state = TRIGGER_OFF;
console.log("EFFECT INIT");
this.effect = new OneshotEffectDetector(this.EFFECT);
this.start_time = 0;
}
run(blade) {
super.run(blade);
if (this.effect.Detect(blade)) {
this.start_time = micros();
this.trigger_state = TRIGGER_DELAY;
}
if (this.trigger_state == this.TRIGGER_OFF) {
this.value = 0;
return;
}
var t = micros() - this.start_time;
while (true) {
var micros_for_state = this.get_millis_for_state() * 1000;
if (t < micros_for_state) {
switch (this.trigger_state) {
case TRIGGER_DELAY:
this.value = 0;
return;
case TRIGGER_ATTACK:
this.value = t * 32768.0 / micros_for_state;
return;
case TRIGGER_SUSTAIN:
this.value = 32768;
return;
case TRIGGER_RELEASE:
this.value = 32768 - t * 32768 / micros_for_state;
return;
case TRIGGER_OFF:
this.value = 0;
return;
}
}
if (this.TRIGGER_STATE >= 4) throw "Weird state?";
this.trigger_state++;
t -= micros_for_state;
this.start_time += micros_for_state;
}
}
get_millis_for_state() {
switch (this.trigger_state) {
case TRIGGER_DELAY: return this.DELAY_MILLIS.getInteger(0);
case TRIGGER_ATTACK: return this.FADE_IN_MILLIS.getInteger(0);
case TRIGGER_SUSTAIN: return this.SUSTAIN_MILLIS.getInteger(0);
case TRIGGER_RELEASE: return this.FADE_OUT_MILLIS.getInteger(0);
case TRIGGER_OFF:
}
return 10000000;
}
getInteger(led) { return this.value; }
IS_RUNNING() {
return this.trigger_state != TRIGGER_OFF;
}
};
function Trigger(EFFECT, FADE_IN_MILLIS, SUSTAIN_MILLIS, FADE_OUT_MILLIS, DELAY_MILLIS) {
return new TriggerClass(EFFECT, FADE_IN_MILLIS, SUSTAIN_MILLIS, FADE_OUT_MILLIS, DELAY_MILLIS);
}
class SmoothStepClass extends FUNCTION {
constructor(POS, WIDTH) {
super("SmoothStep function", arguments);
this.add_arg("POS", "FUNCTION", "Position 0=hilt, 32768=tip");
this.add_arg("WIDTH", "FUNCTION", "Step width 32768=length of blade");
}
run(blade) {
super.run(blade);
var width=this.WIDTH.getInteger(0);
if (width == 0) {
this.mult = 32768;
} else {
this.mult = 32768 * 32768 / width / blade.num_leds();
}
this.location = blade.num_leds() * this.mult * (this.POS.getInteger(0) - width/2) / 32768;
}
getInteger(led) {
var x = led * this.mult - this.location;
if (x < 0) return 0;
if (x > 32768) return 32768;
return (((x * x) >> 14) * ((3<<14) - x)) >> 15;
}
};
function SmoothStep(POS, WIDTH) { return new SmoothStepClass(POS, WIDTH); }
class RampFClass extends FUNCTION {
constructor() {
super("0 at base, 32768 at tip", arguments);
}
run(blade) {
this.num_leds = blade.num_leds();
}
getInteger(led) {
return led * 32768 / this.num_leds;
}
}
function RampF() {
return new RampFClass();
}
class MultClass extends FUNCTION {
constructor(ARGS) {
super("Multiply values. Uses fixed point 16.15 multiplication", ARGS);
this.FUNCTIONS = Array.from(ARGS);
for (var i = 1; i < this.FUNCTIONS.length + 1; i++)
this.add_arg("FUNCTION" + i, "FUNCTION", "COLOR " + i);
}
getInteger(led) {
var ret = this.FUNCTIONS[0].getInteger(led);
for (var i = 1; i < this.FUNCTIONS.length; i++) {
ret = (ret * this.FUNCTIONS[i].getInteger(led)) >> 15;
}
return ret;
}
}
function Mult(ARGS) {
return new MultClass(Array.from(arguments));
}
class PercentageClass extends MACRO {
constructor(F, P) {
super("Returns P % of F.", arguments);
this.add_arg("F","FUNCTION","F");
this.add_arg("P","INT", "Percent")
this.SetExpansion(Mult(this.F.DOCOPY(), Int(this.P * 32768 / 100)));
}
}
function Percentage(F, P) {
return new PercentageClass(F, P);
}
class NoisySoundLevelClass extends FUNCTION {
constructor() {
super("Noisy sound level.", arguments);
}
run(blade) {
this.var_ = (Math.random() * Math.random()) * 32768;
}
getInteger(led) { return this.var_; }
};
function NoisySoundLevel() { return new NoisySoundLevelClass(); }
class NoisySoundLevelCompatClass extends FUNCTION {
constructor() {
super("Noisy sound level.", arguments);
}
run(blade) {
this.var_ = clamp((Math.random() * Math.random()) * 32768 * 2, 0, 32768);
}
getInteger(led) { return this.var_; }
};
function NoisySoundLevelCompat() { return new NoisySoundLevelCompatClass(); }
class SmoothSoundLevelClass extends FUNCTION {
constructor() {
super("Noisy sound level.", arguments);
this.var_ = 0.0;
}
run(blade) {
var v = Math.random() * 20000.0;
v *= v;
this.var_ = (this.var_ + v) / 100.0 ;
}
getInteger(led) { return this.var_; }
};
function SmoothSoundLevel() { return new SmoothSoundLevelClass(); }
class WavLenClass extends FUNCTION {
constructor() {
super("Length of associated wav file in MS", arguments);
this.add_arg("EFFECT", "EFFECT", "Which effect to get the length of.", EFFECT(EFFECT_NONE));
}
//////////////// WAVLEN PR /////////////////
// WavLen value can be set in settings panel
setLength(value) {
this.wavlenValue = value;
console.log("Updated WavLen: ", this.wavlenValue);
}
getInteger(led) {
const effectArg = this.EFFECT?.value;
const effectName = EFFECT_SOUND_MAP[effectArg];
// Build durations array from loaded buffers
const durations = pickLoopBuffers(effectName)
.map(b => b?.duration ? Math.round(b.duration * 1000) : null);
// Prevent lastPlayedSoundIndex[effectName] from being undefined:
// This keeps EFFECTS from missing the sound duration when loading the default font with Sound OFF.
// Defaults to first buffer so WavLen uses the real duration even before any sound is actually played.
const rawIdx = lastPlayedSoundIndex[effectName];
const idx = (typeof rawIdx === 'number' && rawIdx >= 0 && rawIdx < durations.length)
? rawIdx
: 0;
let result;
if (
useFontWavLenState.get() &&
effectName &&
Array.isArray(durations) &&
durations[idx] != null
) {
result = durations[idx];
} else {
result = myWavLen.wavlenValue;
}
return result;
}
};
function WavLen(EFFECT) { return new WavLenClass(EFFECT); }
class SwingSpeedXClass extends FUNCTION {
constructor() {
super("Swing Speed", arguments);
this.add_arg("MAX", "FUNCTION", "What swing speed returns 32768.");
this.var_ = 0.0;
}
run(blade) {
super.run(blade);
var speed = get_swing_speed();
var v = speed / this.MAX.getInteger(0);
this.var_ = clamp(v * 32768, 0, 32768);
}
getInteger(led) { return this.var_; }
};
function SwingSpeedX(MAX) { return new SwingSpeedXClass(MAX); }
class SwingSpeedClass extends MACRO {
constructor() {
super("Swing Speed", arguments);
this.add_arg("MAX", "INT", "What swing speed returns 32768.");
this.SetExpansion(SwingSpeedX(Int(this.MAX)));
}
};
function SwingSpeed(MAX) { return new SwingSpeedClass(MAX); }
class ClashImpactFXClass extends FUNCTION {
constructor(MIN, MAX) {
super("Returns clash strength.", arguments);
this.add_arg("MIN", "FUNCTION", "Minimum, translates to zero", Int(200));
this.add_arg("MAX", "FUNCTION", "Maximum, translates to 32768", Int(1600));
this.value = 0;
}
run(blade) {
super.run(blade);
current_clash_strength = max(current_clash_strength, random(current_clash_value));
current_clash_value -= random(random(current_clash_value));
this.value = clamp((current_clash_strength - this.MIN.getInteger(0)) * 32768 / this.MAX.getInteger(0), 0, 32768);
}
getInteger(led) {
return this.value;
}
};
function ClashImpactFX(MIN, MAX) {
return new ClashImpactFXClass(MIN, MAX);
}
class ClashImpactFClass extends MACRO {
constructor(MIN, MAX) {
super("Returns clash strength.", arguments);
this.add_arg("MIN", "INT", "Minimum, translates to zero", 200);
this.add_arg("MAX", "INT", "Maximum, translates to 32768", 1600);
this.SetExpansion(ClashImpactFX(Int(this.MIN), Int(this.MAX)));
}
}
function ClashImpactF(MIN, MAX) {
return new ClashImpactFClass(MIN, MAX);
}
class SwingAccelerationXClass extends FUNCTION {
constructor() {
super("Swing Acceleration", arguments);
this.add_arg("MAX", "FUNCTION", "What swing speed returns 32768.", Int(130));
this.var_ = 0.0;
}
run(blade) {
super.run(blade);
var accel = get_swing_accel();
var v = accel / this.MAX.getInteger(0);
this.var_ = clamp(v * 32768, 0, 32768);
}
getInteger(led) { return this.var_; }
};
function SwingAccelerationX(MAX) { return new SwingAccelerationXClass(MAX); }
class SwingAccelerationClass extends MACRO {
constructor() {
super("Swing Speed", arguments);
this.add_arg("MAX", "INT", "What swing speed returns 32768.", 130);
this.SetExpansion(SwingAccelerationX(Int(this.MAX)));
}
};
function SwingAcceleration(MAX) { return new SwingAccelerationClass(MAX); }
class LayerFunctionsClass extends FUNCTION {
constructor(ARGS) {
super("Mix functions", ARGS);
this.LAYERS = Array.from(ARGS);
for (var i = 1; i < this.LAYERS.length + 1; i++)
this.add_arg("FUNCTION" + i, "FUNCTION", "FUNCTION " + i);
}
getInteger(led) {
var ret = 0;
for (var i = 0; i < this.LAYERS.length; i++) {
ret = 32768 - ((((32768 - ret) * (32768 - this.LAYERS[i].getInteger(led)))) >> 15);
}
return ret;
}
};
function LayerFunctions(Layer1, Layer2) {
return new LayerFunctionsClass(Array.from(arguments));
}
class SlowNoiseClass extends FUNCTION {
constructor(SPEED) {
super("Returns a value between 0 and 32768, which slowly changes up and down randomly.", Array.from(arguments));
this.add_arg("SPEED", "FUNCTION", "Change speed");
this.value = random(32768);
}
run(blade) {
super.run(blade);
var now = millis();
var delta = now - this.last_millis;
this.last_millis = now;
if (delta > 100) delta = 1;
var speed = this.SPEED.getInteger(0);
// console.log("DELTA = " + delta + " SPEED = " + speed + " VALUE="+this.value);
while (delta > 0) {
this.value = clamp(this.value + random(speed * 2 + 1) - speed, 0, 32768);
delta--;
}
}
getInteger(led) { return this.value; }
};
function SlowNoise(SPEED) {
return new SlowNoiseClass(SPEED);
}
class IsLessThanClass extends FUNCTION {
constructor(F, V) {
super("Returns 32768 if F < V, otherwise 0.", arguments);
this.add_arg("F", "FUNCTION", "F");
this.add_arg("V", "FUNCTION", "V");
}
getInteger(led) {
return this.F.getInteger(led) < this.V.getInteger(led) ? 32768 : 0;
}
};
function IsLessThan(F, V) {
return new IsLessThanClass(F, V);
}
class IsGreaterThanClass extends MACRO {
constructor(F, V) {
super("Returns 32768 if F > V, otherwise 0.", arguments);
this.add_arg("F", "FUNCTION", "F");
this.add_arg("V", "FUNCTION", "V");
this.SetExpansion(IsLessThan(V.DOCOPY(), F.DOCOPY()));
}
};
function IsGreaterThan(F, V) {
return new IsGreaterThanClass(F, V);
}
class IsBetweenClass extends FUNCTION {
constructor(F, BOTTOM, TOP) {
super("Returns 32768 if BOTTOM < F < TOP, otherwise 0.", arguments);
this.add_arg("F", "FUNCTION", "F");
this.add_arg("BOTTOM", "FUNCTION", "BOTTOM");
this.add_arg("TOP", "FUNCTION", "TOP");
}
getInteger(led) {
var f = this.F.getInteger(led);
return this.BOTTOM.getInteger(led) < f && f < this.TOP.getInteger(led) ? 32768 : 0;
}
};
function IsBetween(F, BOTTOM, TOP) {
return new IsBetweenClass(F, BOTTOM, TOP);
}
class ClampFXClass extends FUNCTION {
constructor(F, MIN, MAX) {
super("Returns F, clamped to MIN...MAX", arguments);
this.add_arg("F", "FUNCTION", "F");
this.add_arg("MIN", "FUNCTION", "MIN");
this.add_arg("MAX", "FUNCTION", "MAX");
}
getInteger(led) {
return clamp(this.F.getInteger(led), this.MIN.getInteger(led), this.MAX.getInteger(led));
}
};
function ClampFX(F, MIN, MAX) {
return new ClampFXClass(F, MIN, MAX);
}
class ClampFClass extends MACRO {
constructor(F, MIN, MAX) {
super("Returns F, clamped to MIN...MAX", arguments);
this.add_arg("F", "FUNCTION", "F");
this.add_arg("MIN", "INT", "MIN");
this.add_arg("MAX", "INT", "MAX");
this.SetExpansion(ClampFX(F.DOCOPY(), Int(MIN), Int(MAX)));
}
};
function ClampF(F, MIN, MAX) {
return new ClampFClass(F, MIN, MAX);
}
class VariationClass extends FUNCTION {
constructor() {
super("Returns the current variation", arguments);
}
getInteger(led) {
return Variant() & 0x7fff;
}
};
function Variation() {
return new VariationClass();
}
class AltFClass extends FUNCTION {
constructor() {
super("Returns the current alt", arguments);
}
getInteger(led) {
return Alt() & 0x7fff;
}
};
function AltF() {
return new AltFClass();
}
function MOD(x, m) {
if (x >= 0) return x % m;
return m + ~((~x) % m)
}
class SyncAltToVarianceFClass extends FUNCTION {
constructor() {
super("Synchronizes Alt and Variance.", arguments);
this.last_ = 0x7fffffff;
}
run(blade) {
super.run(blade)
if (num_alternatives == 0) return;
var VAR = MOD(Variant(), num_alternatives);
if (VAR == this.last_ && Alt() == this.last_) return;
if (this.last_ == 0x7fffffff) {
console.log("SYNC FIRST");
//////////// SafeguardInputs PR ///////////////
FIND("ALT_VALUE").value = VAR;
} else if (VAR != this.last_) {
if (isNaN(VAR)) VAR = 0;
console.log("SYNC ALT: " + VAR);
FIND("ALT_VALUE").value = VAR;
blade.addEffect(EFFECT_ALT_SOUND, 0.0);
} else {
console.log("SYNC VAR");
VAR = Alt();
if (isNaN(VAR)) VAR = 0;
console.log("SYNC VAR: " + VAR);
//////////// SafeguardInputs PR ///////////////
FIND("VARIANT_VALUE").value = VAR;
}
this.last_ = VAR;
}
getInteger(led) { return 0; }
}
function SyncAltToVarianceF() {
return new SyncAltToVarianceFClass();
}
class SyncAltToVarianceLClass extends MACRO {
constructor() {
super("Invisble layer for synchronizing Alt and Variance.", arguments);
this.SetExpansion(AlphaL(BLACK, SyncAltToVarianceF()));
}
}
function SyncAltToVarianceL() {
return new SyncAltToVarianceLClass();
}
class EffectPulseFClass extends FUNCTION {
constructor() {
super("Generate a pulse every time an effect occurs", arguments);
this.add_arg("EFFECT", "EFFECT", "Effect to trigger new random.");
this.effect = new OneshotEffectDetector(this.EFFECT);
this.value = 0;
}
run(blade) {
super.run(blade);
if (this.effect.Detect(blade)) {
this.value = 32768;
} else {
this.value = 0;
}
}
getInteger(led) { return this.value; }
};
function EffectPulseF(EFFECT) {
return new EffectPulseFClass(EFFECT);
}
class IncrementWithResetClass extends SVF_FUNCTION {
constructor(PULSE, RESET_PULSE, MAX, I) {
super("Increment by I each time PULSE occurs.", arguments);
this.add_arg("PULSE", "FUNCTION", "Pulse.");
this.add_arg("RESET_PULSE", "FUNCTION", "Reset pulse.", Int(0));
this.add_arg("MAX", "FUNCTION", "Max value", Int(32768));
this.add_arg("I", "FUNCTION", "Increment", Int(1));
this.value = 0;
}
run(blade) {
super.run(blade);
if (this.RESET_PULSE.getInteger(0) != 0) {
this.value = 0;
}
if (this.PULSE.getInteger(0) != 0) {
this.value = min(this.value + this.I.getInteger(0), this.MAX.getInteger(0));
}
}
getInteger(led) { return this.value; }
}
function IncrementWithReset(PULSE, RESET_PULSE, MAX, I) {
return new IncrementWithResetClass(PULSE, RESET_PULSE, MAX, I);
}
class IncrementModuloFClass extends SVF_FUNCTION {
constructor(PULSE, MAX, I) {
super("Increment by I each time PULSE occurs.", arguments);
this.add_arg("PULSE", "FUNCTION", "Pulse.");
this.add_arg("MAX", "FUNCTION", "Max value", Int(32768));
this.add_arg("I", "FUNCTION", "Increment", Int(1));
this.value = 0;
}
run(blade) {
super.run(blade);
if (this.PULSE.getInteger(0) != 0) {
this.value = (this.value + this.I.getInteger(0)) % this.MAX.getInteger(0);
}
}
getInteger(led) { return this.value; }
}
function IncrementModuloF(PULSE, MAX, I) {
return new IncrementModuloFClass(PULSE, MAX, I);
}
class ThresholdPulseFClass extends SVF_FUNCTION {
constructor(F, THRESHOLD, HYST_PERCENT) {
super("Generate a Pulse when F > THRESHOLD.", arguments);
this.add_arg("F", "FUNCTION", "Input");
this.add_arg("THRESHOLD", "FUNCTION", "Threshold", Int(32768));
this.add_arg("HYST_PERCENT", "FUNCTION", "Hysteresis percent", Int(66));
this.value = 0;
this.triggered = 0;
}
run(blade) {
super.run(blade);
var f = this.F.getInteger(0);
var threshold = this.THRESHOLD.getInteger(0);
this.value = 0;
if (this.triggered) {
if (f < threshold * this.HYST_PERCENT.getInteger(0) / 100) {
this.triggered = false;
}
} else {
if (f >= threshold) {
this.triggered = true;
this.value = 32768;
}
}
}
getInteger(led) { return this.value; }
}
function ThresholdPulseF(F, THRESHOLD, HYST_PERCENT) {
return new ThresholdPulseFClass(F, THRESHOLD, HYST_PERCENT);
}
class IncrementFClass extends MACRO {
constructor(F, V, MAX, I, HYST_PERCENT) {
super("Increase by I every time F > V.", arguments);
this.add_arg("F", "FUNCTION", "Input");
this.add_arg("V", "FUNCTION", "Compare value.", Int(32768));
this.add_arg("MAX", "FUNCTION", "Max value.", Int(32768));
this.add_arg("I", "FUNCTION", "Increment", Int(1));
this.add_arg("HYST_PERCENT", "FUNCTION", "Hysteresis percent", Int(66));
this.SetExpansion(IncrementModuloF(ThresholdPulseF(this.F, this.V, this.HYST_PERCENT), this.MAX, this.I));
}
};
function IncrementF(F, V, MAX, I, HYST_PERCENT) {
return new IncrementFClass(F, V, MAX, I, HYST_PERCENT);
}
class EffectIncrementFClass extends MACRO {
constructor(EFFECT, MAX, I) {
super("Increase by I every time F > V.", arguments);
this.add_arg("EFFECT", "EFFECT", "Effect to trigger increment.");
this.add_arg("MAX", "FUNCTION", "Max value.", Int(32768));
this.add_arg("I", "FUNCTION", "Increment", Int(1));
this.SetExpansion(IncrementModuloF(EffectPulseF(this.EFFECT), this.MAX, this.I));
}
};
function EffectIncrementF(EFFECT, MAX, I) {
return new EffectIncrementFClass(EFFECT, MAX, I);
}
class EffectRandomFClass extends FUNCTION {
constructor() {
super("Select a new random value every time an effect occurs", arguments);
this.add_arg("EFFECT", "EFFECT", "Effect to trigger new random.");
this.effect = new OneshotEffectDetector(this.EFFECT);
this.value = random(32768);
}
run(blade) {
super.run(blade);
if (this.effect.Detect(blade)) {
this.value = random(32768);
}
}
getInteger(led) { return this.value; }
};
function EffectRandomF(EFFECT) {
return new EffectRandomFClass(EFFECT);
}
class EffectPositionClass extends FUNCTION {
constructor() {
super("Select a new random value every time an effect occurs", arguments);
this.add_arg("EFFECT", "EFFECT", "Effect to trigger new random.", EFFECT(EFFECT_NONE));
this.value = 0;
}
run(blade) {
super.run(blade);
var effect;
if (this.EFFECT+0 == 0) {
effect = last_detected_blade_effect;
} else {