git.s-ol.nu alive / b2da01c
lib: add osc/send_arr with support for [] tags s-ol 3 months ago
2 changed file(s) with 46 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
11 import opairs from require 'alv.util'
22 import T, Array, Struct from require 'alv.base'
33
4 add_item = (message, type, val) ->
4 add_item = (message, type, val, use_arrays=false) ->
55 switch type.__class
66 when Array
7 -- message\add '['
7 message\add '[' if use_arrays
88 for i=1,type.size
9 add_item message, type.type, val[i]
10 -- message\add ']'
9 add_item message, type.type, val[i], use_arrays
10 message\add ']' if use_arrays
1111 when Struct
12 -- message\add '['
12 message\add '[' if use_arrays
1313 for key, subtype in opairs type.types
14 -- message\add '['
1514 message\add 's', key
16 add_item message, subtype, val[key]
17 -- message\add ']'
18 -- message\add ']'
15 add_item message, subtype, val[key], use_arrays
16 message\add ']' if use_arrays
1917 else
2018 ts = switch type
2119 when T.num then 'f'
6565 add_item msg, @inputs[i]\type!, args[i]
6666 socket\send msg.pack msg.content
6767
68 send_arr = Constant.meta
69 meta:
70 name: 'send-arr'
71 summary: "Send an OSC message using arrays."
72 examples: { '(osc/send [socket] path val…)' }
73 description: "Sends an OSC message to `path` with `val…` as arguments.
74
75 - `socket` should be a `udp/socket` value. This argument can be omitted and the
76 value be passed as a dynamic definition in `*sock*` instead.
77 - `path` is the OSC path to send the message to. It should be a string-value.
78 - the arguments can be any type:
79 - `num` will be sent as `f`
80 - `str` will be sent as `s`
81 - `bool` will be sent as `T`/`F`
82 - `bang` will be sent as `T`
83 - arrays will be sent as a series of values surrounded by `[…]`
84 - structs will be sent as a series of key (`s`)/value tuples surrounded by `[…]`
85
86 This is a pure op, so between the values at most one !-stream input is allowed."
87
88 value: class extends PureOp
89 pattern: any!^0
90
91 full_pattern = -sig['udp/socket'] + sig.str + any!^0
92 setup: (inputs, scope) =>
93 { socket, path, values } = full_pattern\match inputs
94 super values, scope, {
95 socket: Input.cold socket or scope\get '*sock*'
96 path: Input.cold path
97 }
98
99 tick: =>
100 args = @unwrap_all!
101 { :socket, :path } = args
102 msg = new_message path
103 for i=1,#args
104 add_item msg, @inputs[i]\type!, args[i], true
105 socket\send msg.pack msg.content
106
68107 Constant.meta
69108 meta:
70109 name: 'osc'