@kairotic/io 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,55 @@
1
+ ;; control · one control of a controller, out of the events of midiin, gamepad, motion, oscin or serial
2
+ ;; Part of @kairotic/io. Written against the Kairotic API (api/…). Discussion #135.
3
+ (let [num (fn [s] (let [n (js/parseFloat s)] (when (js/isFinite n) n)))
4
+ ;; an OSC address pattern, only * and ? (the rest as it is)
5
+ osc-re (fn [p] (js/RegExp. (str "^" (-> p (.replace (js/RegExp. "[.+^${}()|[\\]\\\\]" "g") "\\$&") (.replaceAll "*" "[^/]*") (.replaceAll "?" "[^/]")) "$")))
6
+ ;; the spec, from the words after the class: [kind & words]
7
+ ;; (a word such as /synth/cut is read as a keyword with an empty namespace: put its / back)
8
+ spec-of (fn [args] (mapv (fn [a] (cond (and (keyword? a) (re-find #"/" (str a))) (subs (str a) 1)
9
+ (keyword? a) (name a)
10
+ :else (str a)))
11
+ args))
12
+ ;; the value of the event for the spec, or nil if the event is not that control
13
+ pick (fn [[kind a b] e]
14
+ (case kind
15
+ "cc" (when (and (= :cc (:type e)) (= (num a) (:cc e)) (or (nil? b) (= (num b) (:ch e)))) (:value e))
16
+ "note" (when (and (= :note (:type e)) (or (nil? a) (= (num a) (:note e))))
17
+ (if (:gate e) (or (:amp e) 1) 0))
18
+ "bend" (when (= :glide (:type e)) (:freq e))
19
+ "pad" (when (and (#{:axis :button} (:type e))
20
+ (or (= a (some-> (:name e) name))
21
+ (= a (str (if (= :axis (:type e)) "a" "b") (or (:axis e) (:button e))))))
22
+ (:value e))
23
+ "tilt" (when (= :tilt (:type e)) (if (= a "y") (:y e) (:x e)))
24
+ "shake" (when (= :shake (:type e)) (:value e))
25
+ "osc" (when (and a (:address e) (.test (osc-re a) (:address e)))
26
+ (let [v (first (:args e))] (when (number? v) v)))
27
+ "serial" (when (= :serial (:type e))
28
+ (cond (and (:key e) (= a (:key e))) (:value e)
29
+ (and (:values e) (num a)) (get (:values e) (js/Math.round (num a)))))
30
+ nil))]
31
+ {;; ── about ──
32
+ :class "control"
33
+ :license "MPL-2.0"
34
+ :doc "One control of a controller, out of the events that midiin, gamepad, motion, oscin or serial send (on their event outlet): its value, every time it moves. cc 74 [channel] a MIDI controller (0…1); note [n] a key (its loudness on, 0 off); bend the pitch of a glided note (Hz); pad lx · pad a · pad a3 a stick or button of a gamepad (by name, or a0 b3); tilt x · tilt y a phone's tilt (0…1); shake; osc /synth/cut the first number of an OSC address (* and ? allowed); serial pot · serial 0 a board's key, or the nth number of its lines. Into scale, to bring it to the range of what it moves — which is what learn makes."
35
+ :usage "control kind [which] [channel]"
36
+ :io [1 1]
37
+ :size [110 24]
38
+ :help {:inlets ["the events of midiin, gamepad, motion, oscin or serial"]
39
+ :outlets ["the value of that one control, when it moves"]
40
+ :example "(box :b1 \"midiin\" 40 30)\n(box :b2 \"control cc 1\" 40 80)\n(box :b3 \"scale 0 1 200 2000 log\" 40 120)\n(box :b4 \"print\" 40 160)\n(connect :b1 2 :b2 0)\n(connect :b2 0 :b3 0)\n(connect :b3 0 :b4 0)"
41
+ :try "Press ▶ and move the modulation wheel (CC 1) of a MIDI keyboard: 200 to 2000, in tune with the ear."
42
+ :see ["scale" "midiin" "gamepad" "motion" "oscin"]}
43
+
44
+ ;; ── logic ──
45
+ :receive (fn [{:keys [value args]}]
46
+ (if-let [v (and (map? value) (pick (spec-of args) value))]
47
+ {:out [[0 v]] :show {:v v}}
48
+ {}))
49
+
50
+ ;; ── view ──
51
+ :derive (fn [{:keys [shown args]}]
52
+ (let [s (str "control " (apply str (interpose " " (spec-of args))))]
53
+ {:label (if (number? (:v shown)) (str s " · " (api/nice (:v shown))) s)}))
54
+ :view [[:rect {:x 1 :y 1 :w 108 :h 22 :r 3 :stroke :accent :fill :bg}]
55
+ [:text {:x 6 :y 16 :size 10.5} :$label]]})
@@ -0,0 +1,80 @@
1
+ ;; gamepad · a game controller's buttons and sticks as Kairotic events
2
+ ;; Part of @kairotic/io. Written against the Kairotic API (api/…). Discussion #135, Study 34 · io.
3
+ (let [buttons [:a :b :x :y :lb :rb :lt :rt :back :start :ls :rs :up :down :left :right :home]
4
+ axes [:lx :ly :rx :ry]
5
+ r3 (fn [x] (/ (js/Math.round (* x 1000)) 1000))
6
+ ;; a stick's value without its dead zone (its rest is never exactly 0), still −1…1 outside it
7
+ dead (fn [v dz] (if (< (js/Math.abs v) dz) 0 (* (js/Math.sign v) (/ (- (js/Math.abs v) dz) (- 1 dz)))))
8
+ ;; one pad now against the same pad before: the events between them
9
+ pad-diff (fn [i before pad dz]
10
+ (let [std? (= "standard" (:mapping pad))
11
+ ev (fn [m] (assoc m :src :gamepad :pad i))
12
+ ax (mapv (fn [v] (r3 (dead v dz))) (:axes pad))
13
+ axis-evs (for [[k v] (map-indexed vector ax)
14
+ :let [old (get-in before [:axes k] 0)]
15
+ :when (or (>= (js/Math.abs (- v old)) 0.01) (and (zero? v) (not (zero? old))))]
16
+ (ev (cond-> {:type :axis :axis k :value v} (and std? (< k 4)) (assoc :name (nth axes k)))))
17
+ btn-evs (for [[k [p v]] (map-indexed vector (:buttons pad))
18
+ :let [[op ov] (get-in before [:buttons k] [false 0]) v (r3 v)]
19
+ :when (or (not= (boolean p) (boolean op)) (>= (js/Math.abs (- v ov)) 0.01))]
20
+ (ev (cond-> {:type :button :button k :value v}
21
+ (and std? (< k 17)) (assoc :name (nth buttons k))
22
+ (not= (boolean p) (boolean op)) (assoc :gate (boolean p)))))]
23
+ [{:axes ax :buttons (mapv (fn [[p v]] [(boolean p) (r3 v)]) (:buttons pad))} (vec (concat btn-evs axis-evs))]))
24
+ read-pads (fn []
25
+ (let [raw (or (.-kairoticFakePads js/window) (when (.-getGamepads js/navigator) (.getGamepads js/navigator)) #js [])]
26
+ (vec (for [p (js/Array.from raw) :when p]
27
+ {:index (.-index p) :id (.-id p) :mapping (.-mapping p) :axes (vec (js/Array.from (.-axes p)))
28
+ :buttons (mapv (fn [b] [(.-pressed b) (.-value b)]) (js/Array.from (.-buttons p)))}))))]
29
+ {;; ── about ──
30
+ :class "gamepad"
31
+ :license "MPL-2.0"
32
+ :doc "A game controller while ▶ runs (press a button to wake it): each button pressed or let go is {:type :button :pad :button :name :value :gate}, a trigger that moves without crossing is the same without :gate, and a stick {:type :axis :pad :axis :name :value −1…1}, with its dead zone (deadzone, 0.12) taken out. With the standard mapping they have names: :a :b :x :y :lb :rb :lt :rt :back :start :ls :rs :up :down :left :right :home, and :lx :ly :rx :ry. pad (-1, all) keeps one. Out: every event, and [name value] for a route or a number box."
33
+ :usage "gamepad [pad] [deadzone]"
34
+ :io [0 2]
35
+ :size [96 30]
36
+ :args [[:pad -1] [:deadzone 0.12]]
37
+ :help {:inlets []
38
+ :outlets ["every event, as a map" "[name value]: (:a 1), (:lx -0.4)…"]
39
+ :example "(box :b1 \"gamepad\" 40 30)\n(box :b2 \"print\" 40 80)\n(connect :b1 1 :b2 0)"
40
+ :try "Press ▶, then any button of a controller to wake it: print shows the buttons and sticks by name."
41
+ :see ["motion" "midiin" "oscout"]}
42
+
43
+ ;; ── logic ──
44
+ :receive (fn [{:keys [inlet value args]}]
45
+ (if (and (= inlet :audio) (:event value))
46
+ (let [e (:event value)]
47
+ (if (and (>= (:pad args) 0) (:pad e) (not= (:pad args) (:pad e)))
48
+ {}
49
+ {:out (cond-> [[0 e]] (#{:button :axis} (:type e)) (conj [1 [(or (:name e) (if (= :axis (:type e)) (:axis e) (:button e))) (:value e)]]))
50
+ :show {:label (case (:type e)
51
+ :connect (str "pad " (:pad e))
52
+ :disconnect "no pad"
53
+ (str (name (or (:name e) (:type e))) " " (api/nice (:value e))))}}))
54
+ {}))
55
+
56
+ ;; ── view ──
57
+ :derive (fn [{:keys [shown]}] {:label (or (:label shown) "gamepad")})
58
+ :view [[:rect {:x 1 :y 1 :w 94 :h 28 :r 3 :stroke :accent :fill :bg}]
59
+ [:text {:x 8 :y 19 :size 11} :$label]]
60
+
61
+ ;; ── outside: the pads, read on every frame while ▶ runs ──
62
+ :audio {:create (fn [_ b _]
63
+ (let [dz (let [n (js/parseFloat (nth (.split (.trim (str (:text b))) #"\s+") 2 ""))] (if (js/isNaN n) 0.12 n))
64
+ node {:alive (atom true) :last (atom {}) :inlets [] :outlets []}
65
+ report (fn [e] (api/report! (:id b) {:event e} nil))
66
+ poll (fn poll [& _]
67
+ (when @(:alive node)
68
+ (let [pads (into {} (map (fn [p] [(:index p) p]) (read-pads)))
69
+ before @(:last node)]
70
+ (doseq [i (keys before) :when (not (contains? pads i))] (report {:src :gamepad :type :disconnect :pad i}))
71
+ (reset! (:last node)
72
+ (into {} (for [[i p] (sort-by key pads)]
73
+ (let [[after es] (pad-diff i (get before i) p dz)]
74
+ (when-not (contains? before i) (report {:src :gamepad :type :connect :pad i :name (:id p)}))
75
+ (doseq [e es] (report e))
76
+ [i after])))))
77
+ (js/requestAnimationFrame poll)))]
78
+ (poll)
79
+ node))
80
+ :dispose (fn [node] (reset! (:alive node) false))}})
@@ -0,0 +1,135 @@
1
+ ;; midiin · MIDI from a keyboard, a controller or another program, as Kairotic events
2
+ ;; Part of @kairotic/io. Written against the Kairotic API (api/…). Discussion #135, Study 34 · io.
3
+ (let [;; ── the page's MIDI, asked once and shared by every midi box (Chrome, Edge, Firefox; not Safari) ──
4
+ access (fn []
5
+ (or (.-kairoticMidi js/window)
6
+ (let [p (if (.-requestMIDIAccess js/navigator)
7
+ (.requestMIDIAccess js/navigator #js {:sysex false})
8
+ (js/Promise.reject (js/Error. "this browser has no Web MIDI (Chrome, Edge or Firefox; not Safari)")))]
9
+ (set! (.-kairoticMidi js/window) p)
10
+ ;; refused: ask again next time
11
+ (.catch p (fn [_] (set! (.-kairoticMidi js/window) nil)))
12
+ p)))
13
+ r3 (fn [x] (/ (js/Math.round (* x 1000)) 1000))
14
+ r4 (fn [x] (/ (js/Math.round (* x 10000)) 10000))
15
+ ;; ── bytes → messages ──
16
+ decode (fn [[s a b]]
17
+ (let [hi (bit-and (or s 0) 0xF0) ch (inc (bit-and (or s 0) 0x0F))]
18
+ (case hi
19
+ 0x80 {:type :note-off :ch ch :note a}
20
+ 0x90 (if (zero? b) {:type :note-off :ch ch :note a} {:type :note-on :ch ch :note a :vel b})
21
+ 0xA0 {:type :poly :ch ch :note a :value b}
22
+ 0xB0 {:type :cc :ch ch :cc a :value b}
23
+ 0xC0 {:type :program :ch ch :value a}
24
+ 0xD0 {:type :pressure :ch ch :value a}
25
+ 0xE0 {:type :bend :ch ch :value (- (+ a (* 128 b)) 8192)}
26
+ 0xF0 (case s 0xF8 {:type :clock} 0xFA {:type :start} 0xFB {:type :continue} 0xFC {:type :stop} nil)
27
+ nil)))
28
+ ;; ── messages → events, with the state of what is held ──
29
+ ;; a voice id names a note on a channel: the same key on two MPE channels is two voices
30
+ voice-id (fn [ch note] (+ note (* 128 (dec ch))))
31
+ fresh (fn [range0] {:range0 range0 :ranges {} :bends {} :rpn {} :held {} :order []})
32
+ freq-of (fn [st ch note] (api/midi->hz (+ note (* (get (:bends st) ch 0) (get (:ranges st) ch (:range0 st))))))
33
+ ev (fn [m] (assoc m :src :midi))
34
+ step (fn [st m]
35
+ (let [ch (:ch m)]
36
+ (case (:type m)
37
+ :note-on (let [id (voice-id ch (:note m)) f (r3 (freq-of st ch (:note m))) a (r3 (/ (:vel m) 127))
38
+ offs (when (get (:held st) id) [(ev {:type :note :id id :ch ch :note (:note m) :freq f :gate false})])]
39
+ [(-> st (assoc-in [:held id] {:ch ch :note (:note m) :freq f})
40
+ (update :order (fn [o] (vec (cons id (remove #(= % id) o))))))
41
+ (conj (vec offs) (ev {:type :note :id id :ch ch :note (:note m) :freq f :amp a :gate true}))])
42
+ :note-off (let [id (voice-id ch (:note m)) h (get (:held st) id)]
43
+ (if h
44
+ [(-> st (update :held dissoc id) (update :order (fn [o] (vec (remove #(= % id) o)))))
45
+ [(ev {:type :note :id id :ch ch :note (:note m) :freq (:freq h) :gate false})]]
46
+ [st []]))
47
+ :bend (let [st (assoc-in st [:bends ch] (/ (:value m) (if (neg? (:value m)) 8192 8191)))]
48
+ (reduce (fn [[st out] id]
49
+ (let [{:keys [note]} (get (:held st) id) f (r3 (freq-of st ch note))]
50
+ [(assoc-in st [:held id :freq] f) (conj out (ev {:type :glide :id id :ch ch :note note :freq f}))]))
51
+ [st []] (filter #(= ch (:ch (get (:held st) %))) (:order st))))
52
+ ;; RPN 0 (CC 101 = 0, CC 100 = 0, then CC 6) is the bend range an MPE controller announces: read, not sent
53
+ :cc (let [n (:cc m) rpn (get (:rpn st) ch {})]
54
+ (cond (= n 101) [(assoc-in st [:rpn ch :msb] (:value m)) []]
55
+ (= n 100) [(assoc-in st [:rpn ch :lsb] (:value m)) []]
56
+ (and (= n 6) (= 0 (:msb rpn)) (= 0 (:lsb rpn))) [(assoc-in st [:ranges ch] (:value m)) []]
57
+ (= n 38) [st []]
58
+ :else [st [(ev {:type :cc :ch ch :cc n :value (r4 (/ (:value m) 127))})]]))
59
+ :pressure (let [id (first (filter #(= ch (:ch (get (:held st) %))) (:order st)))]
60
+ [st [(ev (cond-> {:type :pressure :ch ch :value (r4 (/ (:value m) 127))} id (assoc :id id)))]])
61
+ :poly [st [(ev {:type :pressure :ch ch :id (voice-id ch (:note m)) :value (r4 (/ (:value m) 127))})]]
62
+ :program [st [(ev {:type :program :ch ch :value (:value m)})]]
63
+ (:clock :start :stop :continue) [st [(ev {:type (:type m)})]]
64
+ [st []])))
65
+ sounding (fn [st] [(:order st) (mapv #(:freq (get (:held st) %)) (:order st))])
66
+ label (fn [e] (case (:type e)
67
+ :note (str (api/midi-name (:note e)) (if (:gate e) (str " " (js/Math.round (* 127 (or (:amp e) 0)))) " off"))
68
+ :glide (str (api/nice (:freq e)) " Hz")
69
+ :cc (str "cc " (:cc e) " " (api/nice (:value e)))
70
+ (name (:type e))))]
71
+ {;; ── about ──
72
+ :class "midiin"
73
+ :license "MPL-2.0"
74
+ :doc "MIDI from every input of the page (a keyboard, a controller, another program) as Kairotic events, while ▶ runs: a note is {:type :note :id :ch :note :freq :amp :gate}, a pitch bend moves the notes of its channel ({:type :glide :id :freq}), and controllers, pressure, program changes and the clock come as {:type :cc :ch :cc :value 0…1}, {:type :pressure …}, {:type :clock}…. Pitch travels as :freq in Hz: a bend, or an MPE controller (a channel per note; its bend range, RPN 0, is read), is only a different :freq. Out: the Hz of the notes sounding (with [:gain amp] just before a note starts) and their voice ids, as a keyboard or a player sends them, for synth~ or a polyphonic faust~; and every event. channel (0, all) keeps one; range (2) is the bend range of a controller that does not say its own. clear (or :stop) lets every held note go. Chrome, Edge and Firefox ask once to use MIDI; Safari has no MIDI."
75
+ :usage "midiin [channel] [range]"
76
+ :io [1 3]
77
+ :size [96 30]
78
+ :args [[:channel 0] [:range 2]]
79
+ :help {:inlets ["clear (or :stop): every held note goes"]
80
+ :outlets ["the Hz of the notes sounding, most recent first ([:gain amp] before a note starts)" "their voice ids (note + 128 per channel above the first)" "every event, as a map"]
81
+ :example "(box :b1 \"midiin\" 40 30)\n(box :b2 \"synth~\" 40 90)\n(box :b3 \"dac~\" 40 140)\n(box :b4 \"print\" 160 90)\n(connect :b1 0 :b2 0)\n(connect :b1 1 :b2 1)\n(connect :b2 0 :b3 0)\n(connect :b2 0 :b3 1)\n(connect :b1 2 :b4 0)"
82
+ :try "Press ▶ and play a MIDI keyboard: synth~ plays it and print shows every event. Bend a note: it glides."
83
+ :see ["midiout" "keyboard" "synth~" "oscin"]}
84
+
85
+ ;; ── logic ──
86
+ :receive (fn [{:keys [inlet value state args]}]
87
+ (let [st (or state (fresh (api/clamp (:range args) 1 96)))]
88
+ (cond
89
+ (and (= inlet :audio) (:error value)) {:effects [[:print (:error value)]] :show {:label "no MIDI"}}
90
+ (and (= inlet :audio) (:ports value)) {:state st :show {:label (if (pos? (:ports value)) (str "midi · " (:ports value) " in") "no MIDI input")}}
91
+ (and (= inlet :audio) (:bytes value))
92
+ (let [m (decode (:bytes value))
93
+ ch (:channel args)]
94
+ (if (or (nil? m) (and (pos? ch) (:ch m) (not= ch (:ch m))))
95
+ {:state st}
96
+ (let [[st2 es] (step st m)]
97
+ (if (empty? es)
98
+ {:state st2}
99
+ (let [[ids hz] (sounding st2)
100
+ notes? (some #(#{:note :glide} (:type %)) es)
101
+ on (first (filter #(and (= :note (:type %)) (:gate %)) es))]
102
+ {:state st2
103
+ :show {:label (label (last es))}
104
+ :out (vec (concat (when notes? [[1 ids]])
105
+ (when on [[0 [:gain (:amp on)]]])
106
+ (when notes? [[0 hz]])
107
+ (map (fn [e] [2 e]) es)))})))))
108
+ (#{:clear :stop "clear"} value)
109
+ (let [offs (for [id (:order st) :let [h (get (:held st) id)]]
110
+ {:src :midi :type :note :id id :ch (:ch h) :note (:note h) :freq (:freq h) :gate false})]
111
+ {:state (fresh (:range0 st)) :out (vec (concat [[1 []] [0 []]] (map (fn [e] [2 e]) offs)))})
112
+ :else {:state st})))
113
+
114
+ ;; ── view ──
115
+ :derive (fn [{:keys [shown]}] {:label (or (:label shown) "midiin")})
116
+ :view [[:rect {:x 1 :y 1 :w 94 :h 28 :r 3 :stroke :accent :fill :bg}]
117
+ [:text {:x 8 :y 19 :size 11} :$label]]
118
+
119
+ ;; ── outside: the inputs, while ▶ runs ──
120
+ :audio {:create (fn [_ b _]
121
+ (let [node {:alive (atom true) :ins (atom []) :inlets [] :outlets []}
122
+ on-msg (fn [e] (when @(:alive node) (api/report! (:id b) {:bytes (vec (js/Array.from (.-data e)))} nil)))
123
+ wire! (fn [m]
124
+ (doseq [i @(:ins node)] (.removeEventListener i "midimessage" on-msg))
125
+ (let [ins (vec (js/Array.from (.values (.-inputs m))))]
126
+ (doseq [i ins] (.addEventListener i "midimessage" on-msg))
127
+ (reset! (:ins node) ins)
128
+ (api/report! (:id b) {:ports (count ins)} nil)))]
129
+ (assoc node :unwire (fn [] (doseq [i @(:ins node)] (.removeEventListener i "midimessage" on-msg)))
130
+ :ready (-> (access)
131
+ (.then (fn [m] (when @(:alive node)
132
+ (wire! m)
133
+ (.addEventListener m "statechange" (fn [_] (when @(:alive node) (wire! m)))))))
134
+ (.catch (fn [e] (api/report! (:id b) {:error (str "midiin: " (.-message e))} nil)))))))
135
+ :dispose (fn [node] (reset! (:alive node) false) ((:unwire node)))}})
@@ -0,0 +1,146 @@
1
+ ;; midiout · Kairotic events and notes to a MIDI port, plain or MPE (microtones without rounding)
2
+ ;; Part of @kairotic/io. Written against the Kairotic API (api/…). Discussion #135, Study 34 · io.
3
+ (let [access (fn []
4
+ (or (.-kairoticMidi js/window)
5
+ (let [p (if (.-requestMIDIAccess js/navigator)
6
+ (.requestMIDIAccess js/navigator #js {:sysex false})
7
+ (js/Promise.reject (js/Error. "this browser has no Web MIDI (Chrome, Edge or Firefox; not Safari)")))]
8
+ (set! (.-kairoticMidi js/window) p)
9
+ (.catch p (fn [_] (set! (.-kairoticMidi js/window) nil)))
10
+ p)))
11
+ clamp (fn [x lo hi] (max lo (min hi x)))
12
+ ;; ── messages → bytes ──
13
+ encode (fn [{:keys [type ch note vel value cc]}]
14
+ (let [c (dec (or ch 1)) b7 (fn [x] (clamp (js/Math.round x) 0 127))]
15
+ (case type
16
+ :note-on [(+ 0x90 c) (b7 note) (b7 vel)]
17
+ :note-off [(+ 0x80 c) (b7 note) 0]
18
+ :cc [(+ 0xB0 c) (b7 cc) (b7 value)]
19
+ :pressure [(+ 0xD0 c) (b7 value)]
20
+ :bend (let [v (+ 8192 (clamp (js/Math.round value) -8192 8191))] [(+ 0xE0 c) (bit-and v 127) (bit-shift-right v 7)])
21
+ :clock [0xF8] :start [0xFA] :continue [0xFB] :stop [0xFC]
22
+ nil)))
23
+ ;; ── events → messages: plain (one channel, rounded) or MPE (a member channel per note) ──
24
+ fresh (fn [mpe ch r] {:mode (if mpe :mpe :plain) :ch ch :range r :voices {} :free (vec (range 2 17)) :n 0})
25
+ bend-of (fn [semis r] (clamp (js/Math.round (* (/ semis r) (if (neg? semis) 8192 8191))) -8192 8191))
26
+ key-and-bend (fn [hz r]
27
+ (let [m (api/hz->midi hz) k (clamp (js/Math.round m) 0 127)
28
+ k (if (> (js/Math.abs (- m k)) r) (clamp (js/Math.round (- m (* (js/Math.sign (- m k)) r))) 0 127) k)]
29
+ [k (bend-of (- m k) r)]))
30
+ vel-of (fn [e] (clamp (js/Math.round (* 127 (or (:amp e) 0.8))) 1 127))
31
+ off (fn [st id]
32
+ (if-let [v (get-in st [:voices id])]
33
+ [(-> st (update :voices dissoc id) (update :free #(if (= :mpe (:mode st)) (conj % (:ch v)) %)))
34
+ [{:type :note-off :ch (:ch v) :note (:key v)}]]
35
+ [st []]))
36
+ on (fn [st e]
37
+ (let [id (or (:id e) (js/Math.round (api/hz->midi (:freq e))))
38
+ [st pre] (off st id)]
39
+ (if (= :mpe (:mode st))
40
+ (let [[st ch pre] (if (seq (:free st))
41
+ [st (first (:free st)) pre]
42
+ ;; every channel busy: the oldest voice gives its own
43
+ (let [[oid v] (apply min-key (fn [[_ v]] (:n v)) (:voices st))]
44
+ [(update st :voices dissoc oid) (:ch v) (conj pre {:type :note-off :ch (:ch v) :note (:key v)})]))
45
+ [k b] (key-and-bend (:freq e) (:range st))]
46
+ [(-> st (update :free #(vec (remove #{ch} %))) (assoc-in [:voices id] {:ch ch :key k :n (:n st)}) (update :n inc))
47
+ (into pre [{:type :bend :ch ch :value b} {:type :note-on :ch ch :note k :vel (vel-of e)}])])
48
+ (let [k (clamp (js/Math.round (api/hz->midi (:freq e))) 0 127)]
49
+ [(-> st (assoc-in [:voices id] {:ch (:ch st) :key k :n (:n st)}) (update :n inc))
50
+ (conj pre {:type :note-on :ch (:ch st) :note k :vel (vel-of e)})]))))
51
+ out-step (fn [st e]
52
+ (let [e (if (and (nil? (:freq e)) (number? (:note e))) (assoc e :freq (api/midi->hz (:note e))) e)]
53
+ (case (:type e)
54
+ (:note nil) (cond (false? (:gate e)) (off st (:id e))
55
+ (and (number? (:freq e)) (pos? (:freq e))) (on st e)
56
+ :else [st []])
57
+ :glide (let [v (get-in st [:voices (:id e)])]
58
+ (if (and v (= :mpe (:mode st)) (number? (:freq e)))
59
+ [st [{:type :bend :ch (:ch v) :value (bend-of (- (api/hz->midi (:freq e)) (:key v)) (:range st))}]]
60
+ [st []]))
61
+ :cc [st [{:type :cc :ch (or (:ch e) (:ch st)) :cc (:cc e) :value (* 127 (:value e))}]]
62
+ :pressure [st (if-let [v (get-in st [:voices (:id e)])] [{:type :pressure :ch (:ch v) :value (* 127 (:value e))}] [])]
63
+ (:clock :start :stop :continue) [st [{:type (:type e)}]]
64
+ [st []])))
65
+ events (fn [st es] (reduce (fn [[st ms] e] (let [[st m] (out-step st e)] [st (into ms m)])) [st []] es))
66
+ ;; a snapshot of what sounds (Hz and voice ids, as a player or a keyboard sends them) against the one before
67
+ diff (fn [before after amp]
68
+ (let [pairs (fn [{:keys [hz ids]}] (into {} (map vector (or (seq ids) (map #(js/Math.round (api/hz->midi %)) hz)) hz)))
69
+ a (pairs before) b (pairs after)]
70
+ (vec (concat (for [[id _] a :when (not (contains? b id))] {:type :note :id id :gate false})
71
+ (for [[id f] b :when (and (contains? a id) (not= f (get a id)))] {:type :glide :id id :freq f})
72
+ (for [[id f] (reverse (seq b)) :when (not (contains? a id))] {:type :note :id id :freq f :amp amp :gate true})))))
73
+ setup (fn [r] (vec (concat [{:type :cc :ch 1 :cc 101 :value 0} {:type :cc :ch 1 :cc 100 :value 6} {:type :cc :ch 1 :cc 6 :value 15}]
74
+ (mapcat (fn [ch] [{:type :cc :ch ch :cc 101 :value 0} {:type :cc :ch ch :cc 100 :value 0}
75
+ {:type :cc :ch ch :cc 6 :value r} {:type :cc :ch ch :cc 101 :value 127} {:type :cc :ch ch :cc 100 :value 127}])
76
+ (range 2 17)))))
77
+ send (fn [st ms] (if (seq ms) {:state st :effects [[:send (vec (keep encode ms))]] :show {:n (count ms)}} {:state st}))]
78
+ {;; ── about ──
79
+ :class "midiout"
80
+ :license "MPL-2.0"
81
+ :doc "Notes and events to a MIDI port, while ▶ runs. On the left, what sounds as a player, a keyboard or a reader send it — the Hz of the notes (after their voice ids on the right, and [:gain amp] for the next ones) —, or an event from midiin, oscin or a room ({:type :note :freq :amp :gate}, :glide, :cc…). With mpe, each note gets a channel of its own (2…16) and its exact frequency as a key plus a pitch bend of that channel (range 48 semitones): microtones, just intervals and glides reach the synthesizer without rounding; at ▶ it announces the zone and the range (the MPE configuration message). Without it, one channel (channel, 1) and the nearest key. port (1) is which output of the page. clear (or :stop) ends every note."
82
+ :usage "midiout [mpe] [channel] [port] [range]"
83
+ :io [2 0]
84
+ :size [96 30]
85
+ :args [[:mpe false] [:channel 1] [:port 1] [:range 48]]
86
+ :help {:inlets ["the Hz of the notes sounding (a list), [:gain amp], an event map, or clear" "the voice ids of the Hz that follow"]
87
+ :outlets []
88
+ :example "(box :b1 \"keyboard\" 40 30)\n(box :b2 \"midiout mpe\" 40 120)\n(connect :b1 0 :b2 0)\n(connect :b1 1 :b2 1)"
89
+ :try "Press ▶ and play the keyboard: the notes go to the first MIDI output of the page. With an MPE synthesizer, a freqt read by a reader~ glides there without steps."
90
+ :see ["midiin" "synth~" "player" "oscout"]}
91
+
92
+ ;; ── logic ──
93
+ :receive (fn [{:keys [inlet value state args]}]
94
+ (let [st (or state {:enc (fresh (:mpe args) (api/clamp (:channel args) 1 16) (api/clamp (:range args) 1 96)) :snap {:hz [] :ids []} :ids nil :amp 0.8})
95
+ enc (:enc st)]
96
+ (cond
97
+ (and (= inlet :audio) (:error value)) {:state st :effects [[:print (:error value)]] :show {:label "no MIDI"}}
98
+ (and (= inlet :audio) (contains? value :port)) {:state st :show {:label (or (:port value) "no MIDI output")}}
99
+ (= inlet :audio) {:state st}
100
+ (= inlet 1) {:state (assoc st :ids (when (sequential? value) (vec value)))}
101
+ (and (vector? value) (= :gain (first value)) (number? (second value))) {:state (assoc st :amp (api/clamp (second value) 0 1))}
102
+ (#{:clear :stop "clear"} value)
103
+ (let [[e2 ms] (events enc (map (fn [id] {:type :note :id id :gate false}) (keys (:voices enc))))]
104
+ (send (assoc st :enc e2 :snap {:hz [] :ids []}) ms))
105
+ (map? value) (let [[e2 ms] (out-step enc value)] (send (assoc st :enc e2) ms))
106
+ (or (number? value) (sequential? value))
107
+ (let [hz (vec (filter number? (if (number? value) [value] value)))
108
+ snap {:hz hz :ids (when (= (count (:ids st)) (count hz)) (:ids st))}
109
+ [e2 ms] (events enc (diff (:snap st) snap (:amp st)))]
110
+ (send (assoc st :enc e2 :snap snap) ms))
111
+ :else {:state st})))
112
+
113
+ ;; ── view ──
114
+ :derive (fn [{:keys [shown args]}] {:label (or (:label shown) (str "midiout" (when (:mpe args) " mpe")))})
115
+ :view [[:rect {:x 1 :y 1 :w 94 :h 28 :r 3 :stroke :accent :fill :bg}]
116
+ [:text {:x 8 :y 19 :size 11} :$label]]
117
+
118
+ ;; ── outside: the port, while ▶ runs ──
119
+ :audio {:create (fn [ac b _]
120
+ (let [words (rest (.split (.trim (str (:text b))) #"\s+"))
121
+ mpe (some #{"mpe"} words)
122
+ nums (keep #(let [n (js/parseFloat %)] (when-not (js/isNaN n) n)) words)
123
+ port-n (or (second nums) 1)
124
+ r (or (nth nums 2 nil) 48)
125
+ node {:ac ac :port (atom nil) :alive (atom true) :inlets [] :outlets []}]
126
+ (-> (access)
127
+ (.then (fn [m]
128
+ (when @(:alive node)
129
+ (let [outs (vec (js/Array.from (.values (.-outputs m))))
130
+ p (get outs (dec port-n))]
131
+ (reset! (:port node) p)
132
+ (api/report! (:id b) {:port (when p (str "→ " (.-name p)))} nil)
133
+ (when (and p mpe) (doseq [msg (setup r)] (.send p (clj->js (encode msg)))))))))
134
+ (.catch (fn [e] (api/report! (:id b) {:error (str "midiout: " (.-message e))} nil))))
135
+ node))
136
+ :effects {:send (fn [node bytes at]
137
+ (when-let [p @(:port node)]
138
+ (let [ac (:ac node)
139
+ ;; at the time of the message on the audio clock, as a time of the page
140
+ ts (+ (js/performance.now) (* 1000 (max 0 (- (or at 0) (.-currentTime ac)))))]
141
+ (doseq [b bytes] (.send p (clj->js b) ts)))))}
142
+ :dispose (fn [node]
143
+ (reset! (:alive node) false)
144
+ ;; nothing left sounding on the synthesizer
145
+ (when-let [p @(:port node)]
146
+ (doseq [ch (range 16)] (.send p #js [(+ 0xB0 ch) 123 0]))))}})
@@ -0,0 +1,80 @@
1
+ ;; motion · a phone's tilt and shake as Kairotic events
2
+ ;; Part of @kairotic/io. Written against the Kairotic API (api/…). Discussion #135, Study 34 · io.
3
+ (let [r (fn [x d] (let [k (js/Math.pow 10 d)] (/ (js/Math.round (* x k)) k)))
4
+ clamp (fn [x lo hi] (max lo (min hi x)))
5
+ ;; the tilt as x (left 0 … right 1) and y (towards you 0 … away 1), both 0.5 lying flat
6
+ tilt-xy (fn [beta gamma] [(clamp (/ (+ gamma 90) 180) 0 1) (clamp (/ (+ (clamp beta -90 90) 90) 180) 0 1)])
7
+ mag (fn [[x y z]] (js/Math.sqrt (+ (* x x) (* y y) (* z z))))
8
+ ;; a reading: smoothed, and sent only when it moves (sensors read ~60 times a second, moving or not);
9
+ ;; a shake is one event, with a pause after it
10
+ step (fn [st t rd smooth shake]
11
+ (cond
12
+ (number? (:gamma rd))
13
+ (let [[tx ty] (tilt-xy (or (:beta rd) 0) (:gamma rd))
14
+ x (if (:x st) (+ (* smooth (:x st)) (* (- 1 smooth) tx)) tx)
15
+ y (if (:y st) (+ (* smooth (:y st)) (* (- 1 smooth) ty)) ty)
16
+ [sx sy] (:sent st) st (assoc st :x x :y y)]
17
+ (if (and sx (< (js/Math.abs (- x sx)) 0.004) (< (js/Math.abs (- y sy)) 0.004))
18
+ [st nil]
19
+ [(assoc st :sent [x y])
20
+ (cond-> {:src :motion :type :tilt :x (r x 3) :y (r y 3) :beta (r (or (:beta rd) 0) 1) :gamma (r (:gamma rd) 1)}
21
+ (number? (:alpha rd)) (assoc :alpha (r (:alpha rd) 1)))]))
22
+ (or (:acc rd) (:accg rd))
23
+ (let [m (if (:acc rd) (mag (:acc rd)) (js/Math.abs (- (mag (:accg rd)) 9.81)))]
24
+ (if (and (> m shake) (or (nil? (:last-shake st)) (>= (- t (:last-shake st)) 0.3)))
25
+ [(assoc st :last-shake t) {:src :motion :type :shake :value (r m 1)}]
26
+ [st nil]))
27
+ :else [st nil]))]
28
+ {;; ── about ──
29
+ :class "motion"
30
+ :license "MPL-2.0"
31
+ :doc "A phone's (or a tablet's) motion while ▶ runs: its tilt as x (left 0 … right 1) and y (towards you 0 … away 1), both 0.5 lying flat — smoothed, and sent only when it moves —; and a shake, when it is shaken harder than shake (14 m/s²), once with a pause after it. Out: [x y], the strength of each shake, and the events ({:type :tilt :x :y :alpha :beta :gamma}, {:type :shake :value}) for oscout or a room. smooth (0.3) is how much of the last value stays at each reading. iOS asks for permission at ▶. Open the work on the phone (Share/Export, Release/Perform) to play with it."
32
+ :usage "motion [smooth] [shake]"
33
+ :io [0 3]
34
+ :size [96 30]
35
+ :args [[:smooth 0.3] [:shake 14]]
36
+ :help {:inlets []
37
+ :outlets ["[x y], 0…1 each, when the tilt moves" "the strength of a shake (m/s²)" "every event, as a map"]
38
+ :example "(box :b1 \"motion\" 40 30)\n(box :b2 \"print\" 40 80)\n(connect :b1 0 :b2 0)"
39
+ :try "Open this on a phone, press ▶, and tilt it."
40
+ :see ["gamepad" "oscout"]}
41
+
42
+ ;; ── logic ──
43
+ :receive (fn [{:keys [inlet value state args at]}]
44
+ (if (and (= inlet :audio) (map? value))
45
+ (if (:error value)
46
+ {:effects [[:print (:error value)]] :show {:label "no motion"}}
47
+ (let [[st e] (step (or state {}) (or at 0) value (api/clamp (:smooth args) 0 0.95) (api/clamp (:shake args) 2 60))]
48
+ (cond
49
+ (nil? e) {:state st}
50
+ (= :tilt (:type e)) {:state st :out [[0 [(:x e) (:y e)]] [2 e]] :show {:x (:x e) :y (:y e)}}
51
+ :else {:state st :out [[1 (:value e)] [2 e]] :show (assoc (select-keys st [:x :y]) :shake true)})))
52
+ {}))
53
+
54
+ ;; ── view: where the tilt is ──
55
+ :derive (fn [{:keys [shown]}] {:cx (+ 60 (* 30 (- (or (:x shown) 0.5) 0.5) 2)) :cy (+ 15 (* 10 (- 0.5 (or (:y shown) 0.5)) 2))
56
+ :dot (if (:shake shown) :kairos :accent)})
57
+ :view [[:rect {:x 1 :y 1 :w 94 :h 28 :r 3 :stroke :accent :fill :bg}]
58
+ [:text {:x 6 :y 19 :size 10 :fill :soft} "motion"]
59
+ [:rect {:x 46 :y 4 :w 46 :h 22 :r 2 :stroke :grid :fill :bg}]
60
+ [:circle {:cx :$cx :cy :$cy :r 4 :stroke :$dot :fill :$dot}]]
61
+
62
+ ;; ── outside: the sensors, while ▶ runs ──
63
+ :audio {:create (fn [_ b _]
64
+ (let [node {:alive (atom true) :inlets [] :outlets []}
65
+ report (fn [rd] (when @(:alive node) (api/report! (:id b) rd nil)))
66
+ on-tilt (fn [e] (when (number? (.-gamma e)) (report {:alpha (.-alpha e) :beta (.-beta e) :gamma (.-gamma e)})))
67
+ on-move (fn [e] (let [a (.-acceleration e) g (.-accelerationIncludingGravity e)]
68
+ (cond (and a (number? (.-x a))) (report {:acc [(.-x a) (.-y a) (.-z a)]})
69
+ (and g (number? (.-x g))) (report {:accg [(.-x g) (.-y g) (.-z g)]}))))
70
+ listen! (fn [] (.addEventListener js/window "deviceorientation" on-tilt)
71
+ (.addEventListener js/window "devicemotion" on-move))
72
+ ask (some-> (.-DeviceOrientationEvent js/window) .-requestPermission)]
73
+ ;; iOS asks first, and only from a gesture: ▶ is one
74
+ (if (fn? ask)
75
+ (-> (ask) (.then (fn [r] (if (= r "granted") (listen!) (report {:error "motion: refused"}))))
76
+ (.catch (fn [e] (report {:error (str "motion: " (.-message e))}))))
77
+ (listen!))
78
+ (assoc node :off (fn [] (.removeEventListener js/window "deviceorientation" on-tilt)
79
+ (.removeEventListener js/window "devicemotion" on-move)))))
80
+ :dispose (fn [node] (reset! (:alive node) false) ((:off node)))}})
package/src/oscin.cljs ADDED
@@ -0,0 +1,95 @@
1
+ ;; oscin · OSC in, through a relay's bridge: from SuperCollider, Max, Pd, TouchOSC… as Kairotic events
2
+ ;; Part of @kairotic/io. Written against the Kairotic API (api/…). Discussion #135, Study 34 · io.
3
+ (let [default-url "ws://localhost:8765/osc"
4
+ relay-url (fn [] (or (try (.getItem js/localStorage "kairotic-osc-relay") (catch :default _ nil)) default-url))
5
+ ;; the same shared sockets as oscout (one per address, for every osc box of the page)
6
+ socket (fn [url]
7
+ (let [all (or (.-kairoticOsc js/window) (set! (.-kairoticOsc js/window) #js {}))
8
+ s (aget all url)]
9
+ (if (and s (< (.. s -ws -readyState) 2))
10
+ s
11
+ (let [ws (js/WebSocket. url)
12
+ s #js {:ws ws :listeners #js [] :queue #js []}]
13
+ (set! (.-onopen ws) (fn [_] (doseq [m (js/Array.from (.-queue s))] (.send ws m)) (set! (.-queue s) #js [])))
14
+ (set! (.-onmessage ws) (fn [e] (let [f (try (js/JSON.parse (.-data e)) (catch :default _ nil))]
15
+ (when f (doseq [l (js/Array.from (.-listeners s))] (l f))))))
16
+ (aset all url s)
17
+ s))))
18
+ r4 (fn [x] (if (number? x) (/ (js/Math.round (* x 10000)) 10000) x))
19
+ ;; ── the /kairotic/… addresses are events again; any other address arrives as it is ──
20
+ osc->event (fn [{:keys [address args from]}]
21
+ (let [a (vec args)]
22
+ (merge (cond-> {:src :osc} from (assoc :from from))
23
+ (case address
24
+ "/kairotic/note" (cond-> {:type :note :id (nth a 0 0) :gate (not= 0 (nth a 3 1))}
25
+ (number? (get a 1)) (assoc :freq (r4 (get a 1)))
26
+ (and (number? (get a 2)) (not= 0 (nth a 3 1))) (assoc :amp (r4 (get a 2))))
27
+ "/kairotic/glide" {:type :glide :id (nth a 0 0) :freq (r4 (nth a 1 0))}
28
+ "/kairotic/cc" {:type :cc :ch (nth a 0 1) :cc (nth a 1 0) :value (r4 (nth a 2 0))}
29
+ "/kairotic/pressure" {:type :pressure :id (nth a 0 0) :value (r4 (nth a 1 0))}
30
+ "/kairotic/tilt" {:type :tilt :x (r4 (nth a 0 0.5)) :y (r4 (nth a 1 0.5))}
31
+ "/kairotic/shake" {:type :shake :value (r4 (nth a 0 0))}
32
+ ("/kairotic/clock" "/kairotic/start" "/kairotic/stop" "/kairotic/continue") {:type (keyword (subs address 10))}
33
+ {:type :osc :address address :args (mapv r4 a)}))))
34
+ ;; ── OSC address patterns (OSC 1.0): ? * [a-z] [!a] {foo,bar}, none of them across a / ──
35
+ re-esc (fn [s] (.replace s (js/RegExp. "[.*+?^${}()|[\\]\\\\]" "g") "\\$&"))
36
+ pattern-re (fn [p]
37
+ (loop [cs (seq p) out "^"]
38
+ (if-not cs
39
+ (js/RegExp. (str out "$"))
40
+ (let [c (first cs)]
41
+ (case c
42
+ "?" (recur (next cs) (str out "[^/]"))
43
+ "*" (recur (next cs) (str out "[^/]*"))
44
+ "[" (let [body (take-while #(not= "]" %) (next cs)) neg (= "!" (first body))
45
+ body (apply str (if neg (rest body) body))]
46
+ (recur (next (drop-while #(not= "]" %) (next cs)))
47
+ (str out "[" (when neg "^") (.replace body (js/RegExp. "[\\\\^]" "g") "\\$&") "]")))
48
+ "{" (let [body (apply str (take-while #(not= "}" %) (next cs)))]
49
+ (recur (next (drop-while #(not= "}" %) (next cs)))
50
+ (str out "(?:" (apply str (interpose "|" (map re-esc (.split body ",")))) ")")))
51
+ (recur (next cs) (str out (re-esc c))))))))
52
+ matches? (fn [p address] (or (empty? p) (boolean (.test (pattern-re p) address))))]
53
+ {;; ── about ──
54
+ :class "oscin"
55
+ :license "MPL-2.0"
56
+ :doc "OSC in, through a relay's OSC bridge (npx @kairotic/relay --osc, on this machine), while ▶ runs: what SuperCollider, Max, Pd or TouchOSC send to the relay's port (57121) arrives here. The addresses oscout writes are events again (/kairotic/note id freq amp gate is {:type :note …}, /kairotic/glide, /kairotic/cc, /kairotic/tilt…); any other one is {:type :osc :address \"/x\" :args […] :from \"host:port\"}. pattern takes only the addresses that match it (OSC patterns: ? * [a-z] [!a] {note,glide}): oscin /synth/*/cutoff. Out: the event, and its values alone (the args of an OSC message; the Hz of a note) for a number box. [:relay \"ws://…\"] changes the bridge for the whole page."
57
+ :usage "oscin [pattern]"
58
+ :io [1 2]
59
+ :size [110 30]
60
+ :help {:inlets ["[:relay \"ws://…/osc\"]"]
61
+ :outlets ["every message that matches, as an event" "its values: the args of an OSC message, or the Hz of a note"]
62
+ :example "(box :b1 \"oscin /kairotic/*\" 40 30)\n(box :b2 \"print\" 40 80)\n(connect :b1 0 :b2 0)"
63
+ :try "Run npx @kairotic/relay --osc, press ▶, and from SuperCollider: NetAddr(\"127.0.0.1\", 57121).sendMsg(\"/kairotic/note\", 1, 440, 0.5, 1)."
64
+ :see ["oscout" "midiin"]}
65
+
66
+ ;; ── logic ──
67
+ :receive (fn [{:keys [inlet value]}]
68
+ (cond
69
+ (and (= inlet :audio) (:event value))
70
+ (let [e (:event value)]
71
+ {:out [[1 (if (= :osc (:type e)) (:args e) (or (:freq e) (:value e) (:x e)))] [0 e]]
72
+ :show {:label (or (:address e) (str "/kairotic/" (name (:type e))))}})
73
+ (and (= inlet :audio) (:status value)) {:show {:label (:status value)}}
74
+ (and (vector? value) (= :relay (first value)) (string? (second value)))
75
+ {:effects [[:relay (second value)]] :show {:label "relay set: ▶ again"}}
76
+ :else {}))
77
+ :tasks {:relay (fn [url _] (try (.setItem js/localStorage "kairotic-osc-relay" url) (catch :default _ nil)) (js/Promise.resolve url))}
78
+
79
+ ;; ── view ──
80
+ :derive (fn [{:keys [shown]}] {:label (or (:label shown) "oscin")})
81
+ :view [[:rect {:x 1 :y 1 :w 108 :h 28 :r 3 :stroke :accent :fill :bg}]
82
+ [:text {:x 8 :y 19 :size 11} :$label]]
83
+
84
+ ;; ── outside: the bridge, while ▶ runs ──
85
+ :audio {:create (fn [_ b _]
86
+ (let [pattern (or (second (.split (.trim (str (:text b))) #"\s+")) "")
87
+ node {:alive (atom true) :inlets [] :outlets []}
88
+ s (socket (relay-url))
89
+ l (fn [f] (when (and @(:alive node) (.-address f) (matches? pattern (.-address f)))
90
+ (api/report! (:id b) {:event (osc->event (js->clj f :keywordize-keys true))} nil)))]
91
+ (.push (.-listeners s) l)
92
+ (set! (.. s -ws -onerror)
93
+ (fn [_] (api/report! (:id b) {:status "no relay: npx @kairotic/relay --osc"} nil)))
94
+ (assoc node :off (fn [] (set! (.-listeners s) (.filter (.-listeners s) (fn [x & _] (not= x l))))))))
95
+ :dispose (fn [node] (reset! (:alive node) false) ((:off node)))}})