aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2025-03-23 21:38:29 +0000
committers-ol <s+removethis@s-ol.nu>2025-03-23 21:38:29 +0000
commitaf4032bdc919b386a1360c1a9c307c98c4ba2548 (patch)
treef9953388f2751f88ede558f7ff381bd72089a700
parentlib: fix midi port labels (diff)
downloadalive-af4032bdc919b386a1360c1a9c307c98c4ba2548.tar.gz
alive-af4032bdc919b386a1360c1a9c307c98c4ba2548.zip
builtins: add normalized switch-1
-rw-r--r--alv/builtins.moon34
1 files changed, 33 insertions, 1 deletions
diff --git a/alv/builtins.moon b/alv/builtins.moon
index 0d188f9..56a6eea 100644
--- a/alv/builtins.moon
+++ b/alv/builtins.moon
@@ -341,7 +341,7 @@ switch_ = Constant.meta
When only a single value is provided, it must be an array and is indexed as above."
value: class extends Op
- pattern = (sig.num / sig.bool / evt.num / evt.bool / evt.bang) + any!!*0
+ pattern = (any.num / any.bool / evt.bang) + any!!*0
setup: (inputs) =>
{ i, values } = pattern\match inputs
@@ -387,6 +387,37 @@ When only a single value is provided, it must be an array and is indexed as abov
with Op.vis @
.step = @state
+switch_norm = Constant.meta
+ meta:
+ name: 'switch-1'
+ summary: "Switch between multiple inputs based on normalized i."
+ examples: { '(switch-1 i v1 v2…)', '(switch i arr)' }
+ description: "
+Like `switch`, but numeric indexes are in the range `[0, 1[`."
+
+ value: class extends switch_.value
+ tick: =>
+ { :i, :values } = @inputs
+
+ length = @length or #values
+
+ if i\type! == T.bang
+ if i\dirty!
+ @state += 1
+ @state = @state % length
+ else
+ @state = switch i!
+ when true then 0
+ when false then 1
+ else (math.floor i! * length) % length
+
+ @out\set if @length
+ val = values[1]!
+ val and val[@state + 1]
+ else
+ val = values[@state + 1]
+ val and val!
+
trace_ = Constant.meta
meta:
name: 'trace='
@@ -723,6 +754,7 @@ Constant.meta
'if': if_
'when': when_
'switch': switch_
+ 'switch-1': switch_norm
'=': to_const
'~': to_sig