aboutsummaryrefslogtreecommitdiffstats
path: root/alv-lib/osc.moon
blob: b7e25e7791c58c04a090dade64c568dadc036ecf (plain)
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
import Op, ValueStream, Input, val, evt from require 'alv.base'
import pack from require 'osc'
import dns, udp from require 'socket'

unpack or= table.unpack

connect = ValueStream.meta
  meta:
    name: 'connect'
    summary: "Create a UDP remote."
    examples: { '(osc/connect host port)' }

  value: class extends Op
    pattern = val.str + val.num
    setup: (inputs) =>
      @out or= ValueStream 'udp/socket'
      { host, port } = pattern\match inputs
      super
        host: Input.hot host
        port: Input.hot port

    tick: =>
      { :host, :port } = @unwrap_all!
      ip = dns.toip host

      @out\set with sock = udp!
        \setpeername ip, port

send = ValueStream.meta
  meta:
    name: 'send'
    summary: "Send events via OSC."
    examples: { '(osc/send [socket] path evt)' }
    description: "Sends an OSC message with `evt` as an argument.

- `socket` should be a `udp/socket` value. This argument can be omitted and the
  value be passed as a dynamic definition in `*sock*` instead.
- `path` is the OSC path to send the message to. It should be a string-value.
- `evt` is the argument to send. It should be an event stream."
  value: class extends Op
    pattern = -val['udp/socket'] + val.str + evt!
    setup: (inputs, scope) =>
      { socket, path, value } = pattern\match inputs
      super
        socket: Input.cold socket or scope\get '*sock*'
        path:   Input.cold path
        value:  Input.hot value

    tick: =>
      { :socket, :path, :value } = @unwrap_all!
      for val in *value
        msg = pack path, if 'table' == type val then unpack val else val
        socket\send msg

sync = ValueStream.meta
  meta:
    name: 'sync'
    summary: "Synchronize a value via OSC."
    examples: { '(osc/sync [socket] path val)' }
    description: "sends a message whenever any parameter is dirty."
    description: "Sends an OSC message with `val` as an argument whenever any
of the arguments change.

- `socket` should be a `udp/socket` value. This argument can be omitted and the
  value be passed as a dynamic definition in `*sock*` instead.
- `path` is the OSC path to send the message to. It should be a string-value.
- `val` is the value to Synchronize. It should be a value stream."

  value: class extends Op
    pattern = -val['udp/socket'] + val.str + val!
    setup: (inputs, scope) =>
      { socket, path, value } = pattern\match inputs
      super
        socket: Input.hot socket or scope\get '*sock*'
        path:   Input.hot path
        value:  Input.hot value

    tick: =>
      { :socket, :path, :value } = @unwrap_all!
      msg = pack path, value
      socket\send msg

{
  :connect
  :send
  :sync
}