@kairotic/app 0.0.0-stage → 0.2.2

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.
Files changed (96) hide show
  1. package/README.md +9 -2
  2. package/index.html +1101 -0
  3. package/kairotic/api.cljs +362 -0
  4. package/kairotic/assistant.cljs +505 -0
  5. package/kairotic/backends/gun-backend.js +371 -0
  6. package/kairotic/canvas.cljs +751 -0
  7. package/kairotic/code.cljs +108 -0
  8. package/kairotic/components.cljs +838 -0
  9. package/kairotic/core.cljs +96 -0
  10. package/kairotic/corefns.cljs +473 -0
  11. package/kairotic/docs.mjs +2 -0
  12. package/kairotic/engine.cljs +482 -0
  13. package/kairotic/examples.mjs +96 -0
  14. package/kairotic/faust.cljs +225 -0
  15. package/kairotic/help.cljs +1036 -0
  16. package/kairotic/hiccup.cljs +566 -0
  17. package/kairotic/input.cljs +271 -0
  18. package/kairotic/kfile.mjs +138 -0
  19. package/kairotic/library.cljs +214 -0
  20. package/kairotic/links.cljs +41 -0
  21. package/kairotic/mt.cljs +1604 -0
  22. package/kairotic/mtboxes.cljs +276 -0
  23. package/kairotic/packages/analysis.mjs +64 -0
  24. package/kairotic/packages/controls.mjs +28 -0
  25. package/kairotic/packages/data.mjs +22 -0
  26. package/kairotic/packages/faust-units/cmaes.mjs +82 -0
  27. package/kairotic/packages/faust-units/graph.mjs +212 -0
  28. package/kairotic/packages/faust-units/listen.mjs +125 -0
  29. package/kairotic/packages/faust-units/notation.mjs +139 -0
  30. package/kairotic/packages/faust-units/parse.mjs +439 -0
  31. package/kairotic/packages/faust-units/recipe.mjs +195 -0
  32. package/kairotic/packages/faust-units/render.mjs +69 -0
  33. package/kairotic/packages/faust-units/units.mjs +130 -0
  34. package/kairotic/packages/faust-units/view.mjs +353 -0
  35. package/kairotic/packages/faust-units.mjs +9 -0
  36. package/kairotic/packages/io.mjs +58 -0
  37. package/kairotic/packages/net.mjs +28 -0
  38. package/kairotic/packages/patterns.mjs +34 -0
  39. package/kairotic/packages/spatial.mjs +22 -0
  40. package/kairotic/packages/spectral/shift.mjs +169 -0
  41. package/kairotic/packages/spectral.mjs +16 -0
  42. package/kairotic/packages/synth.mjs +28 -0
  43. package/kairotic/packages/theory/counterpoint.mjs +798 -0
  44. package/kairotic/packages/theory/mirror.mjs +221 -0
  45. package/kairotic/packages/theory.mjs +46 -0
  46. package/kairotic/patterns.cljs +538 -0
  47. package/kairotic/presets.cljs +227 -0
  48. package/kairotic/quantize.cljs +782 -0
  49. package/kairotic/record.cljs +169 -0
  50. package/kairotic/release.cljs +90 -0
  51. package/kairotic/ring.cljs +129 -0
  52. package/kairotic/ringaudio.cljs +232 -0
  53. package/kairotic/ringcore.mjs +270 -0
  54. package/kairotic/score.cljs +176 -0
  55. package/kairotic/shapes.cljs +283 -0
  56. package/kairotic/signal.cljs +327 -0
  57. package/kairotic/sounds.cljs +565 -0
  58. package/kairotic/sounds.mjs +108 -0
  59. package/kairotic/span.cljs +129 -0
  60. package/kairotic/spell.cljs +281 -0
  61. package/kairotic/structure.cljs +145 -0
  62. package/kairotic/talk.cljs +115 -0
  63. package/kairotic/tempo.cljs +226 -0
  64. package/kairotic/treecanvas.cljs +299 -0
  65. package/kairotic/tutorials.mjs +1283 -0
  66. package/kairotic/vendor/codemirror.mjs +23 -0
  67. package/package.json +23 -4
  68. package/page/00-work.cljs +862 -0
  69. package/page/01-commands.cljs +1219 -0
  70. package/page/02-patch-view.cljs +1051 -0
  71. package/page/03-history-view.cljs +481 -0
  72. package/page/04-panels.cljs +448 -0
  73. package/page/05-gestures.cljs +1247 -0
  74. package/page/06-connection.cljs +219 -0
  75. package/page/07-claude.cljs +430 -0
  76. package/page/08-sessions.cljs +423 -0
  77. package/page/09-learn.cljs +602 -0
  78. package/page/10-embed.cljs +469 -0
  79. package/page/11-startup.cljs +61 -0
  80. package/page/12-feedback.cljs +172 -0
  81. package/page/13-perform.cljs +325 -0
  82. package/page/14-editor.cljs +656 -0
  83. package/page/15-presets.cljs +230 -0
  84. package/page/16-release.cljs +242 -0
  85. package/page/17-sounds.cljs +147 -0
  86. package/page/18-lines.cljs +594 -0
  87. package/page/19-files.cljs +247 -0
  88. package/page/19-html.cljs +152 -0
  89. package/page/20-talk.cljs +527 -0
  90. package/page/21-net.cljs +110 -0
  91. package/page/22-learn.cljs +303 -0
  92. package/page/23-ids.cljs +233 -0
  93. package/page/24-clipboard.cljs +136 -0
  94. package/page/25-tidy.cljs +74 -0
  95. package/page/26-probes.cljs +111 -0
  96. package/sources.mjs +2 -0
@@ -0,0 +1,527 @@
1
+ (in-ns 'user)
2
+ ;; ───────────────────────── talking in a room (discussion #135, Studies 34 and 36) ─────────────────────────
3
+ ;; In a shared room, the pages of the people in it find each other through the relay's rooms (/signal,
4
+ ;; beside GUN's /gun: relay 0.3) and open a WebRTC connection to each other: a reliable data channel for
5
+ ;; what is said, and an audio and a video track that stay empty until someone turns the microphone or the
6
+ ;; camera on (so turning them on never renegotiates). The same socket gives the relay's time: the clock
7
+ ;; every page of the room agrees on (talk-now).
8
+ ;;
9
+ ;; What is said is ephemeral: it lives in these pages. Keeping it makes it a gesture of the work — a
10
+ ;; message a text annotation (layer :talk, on the box it names: @b3), a voice note a sample box (its sound
11
+ ;; kept by hash, and handed to every page of the room), a frame of someone's camera an image annotation.
12
+ ;; What is yours is kept at once; what is someone else's asks them first, and cannot be kept once they
13
+ ;; have left. The model is kairotic.talk (tk/…).
14
+
15
+ ;; the room's channels and tempo, in 21-net.cljs
16
+ (declare net-on-share! net-on-tempo! net-joined!)
17
+
18
+ (defonce talk-room (atom {:ws nil :key nil :id nil :peers {} :n 0 :clock (tk/new-clock) :timer nil}))
19
+ (defonce talk-state (atom {:items {} :order [] :asks [] :mine-asks {} :pieces {} :talking false}))
20
+ (defonce talk-media (atom {:mic nil :cam nil :rec nil}))
21
+
22
+ (defn talk-name [] (let [n (str/trim (str (some-> ($ "talk-name") .-value)))] (if (seq n) n "someone")))
23
+ (defn talk-me [] {:id (:id @talk-room) :who (my-author) :name (talk-name)})
24
+ (defn talk-ms [] (+ (.-timeOrigin js/performance) (js/performance.now)))
25
+
26
+ (defn talk-now
27
+ "The room's shared time in ms (the relay's clock as this page reads it), or this page's time outside a room."
28
+ [] (or (tk/shared-time (:clock @talk-room) (talk-ms)) (talk-ms)))
29
+
30
+ (defn talk-signal-url
31
+ "Where the rooms are: ?signal=… if given, else the /signal of the page's relay (nil without a relay)."
32
+ []
33
+ (or (not-empty (.get (js/URLSearchParams. (.. js/window -location -search)) "signal"))
34
+ (tk/signal-url (first (current-relays)))))
35
+
36
+ ;; ───────── bytes ─────────
37
+
38
+ (defn talk-b64 [buf]
39
+ (let [b (js/Uint8Array. buf) parts #js []]
40
+ (loop [i 0] (when (< i (.-length b))
41
+ (.push parts (.apply (.-fromCharCode js/String) nil (.subarray b i (min (.-length b) (+ i 8192)))))
42
+ (recur (+ i 8192))))
43
+ (js/btoa (.join parts ""))))
44
+
45
+ (defn talk-bytes [s] (let [bin (js/atob s) out (js/Uint8Array. (.-length bin))]
46
+ (dotimes [i (.-length bin)] (aset out i (.charCodeAt bin i))) out))
47
+
48
+ (defn talk-sha [bytes]
49
+ (.then (.digest js/crypto.subtle "SHA-256" bytes)
50
+ (fn [d] (apply str (map (fn [x] (.padStart (.toString x 16) 2 "0")) (js/Array.from (.subarray (js/Uint8Array. d) 0 16)))))))
51
+
52
+ ;; ───────── sending ─────────
53
+
54
+ (defn talk-signal! [m]
55
+ (when-let [ws (:ws @talk-room)] (when (= 1 (.-readyState ws)) (.send ws (js/JSON.stringify (clj->js m))))))
56
+
57
+ (defn talk-to! [id m]
58
+ (let [dc (get-in @talk-room [:peers id :dc])]
59
+ (if (and dc (= "open" (.-readyState dc)))
60
+ (.send dc (js/JSON.stringify (clj->js m)))
61
+ (talk-signal! {:to id :data {:talk m}}))))
62
+
63
+ (defn talk-to-all! [m] (doseq [id (keys (:peers @talk-room))] (talk-to! id m)))
64
+
65
+ (defn talk-item [o] (let [m (js->clj o :keywordize-keys true)] (cond-> m (:kind m) (update :kind keyword))))
66
+
67
+ ;; ───────── drawing ─────────
68
+
69
+ (defn talk-msg! [text kind] (when-let [el ($ "talk-state")] (set! (.-innerHTML el) text) (set! (.-className el) (str "talk-state " kind))))
70
+
71
+ (defn render-talk-peers! []
72
+ (when-let [el ($ "talk-peers")]
73
+ (let [ps (:peers @talk-room)]
74
+ (set! (.-innerHTML el)
75
+ (if (:id @talk-room)
76
+ (str "<span class=\"tpeer" (when (:talking @talk-state) " talk") "\">" (esc (talk-name)) " <i>you</i></span>"
77
+ (apply str (for [[id p] (sort-by (comp :n val) ps)]
78
+ (str "<span class=\"tpeer" (when (= "p2p" (:state p)) " p2p") (when (:talk p) " talk") "\" data-peer=\"" (esc id) "\">"
79
+ (esc (or (not-empty (:name p)) "someone")) " <i>" (:state p) (when (:talk p) " · talking") (when (:camera p) " · camera") "</i></span>"))))
80
+ "")))
81
+ (doseq [[id p] (:peers @talk-room)]
82
+ (when-let [t (.querySelector js/document (str ".ttile[data-peer='" id "']"))]
83
+ (if (:talk p) (.add (.-classList t) "talk") (.remove (.-classList t) "talk"))))))
84
+
85
+ (defn talk-anchor-chips [as]
86
+ (apply str (for [a as] (str "<span class=\"tchip\">" (esc (cond (:box a) (str "@" (:box a)) (:point a) (str "#" (:point a)) :else (str "^" (:beat a)))) "</span>"))))
87
+
88
+ (defn talk-item-state [it]
89
+ (let [a (get-in @talk-state [:mine-asks (:id it)])]
90
+ (cond (:kept it) "<span class=\"tst ok\">kept</span>"
91
+ (= a :asked) (str "<span class=\"tst\">asked " (esc (:name it)) "…</span>")
92
+ (= a :declined) (str "<span class=\"tst bad\">" (esc (:name it)) " declined</span>")
93
+ (= a :gone) (str "<span class=\"tst bad\">" (esc (:name it)) " has left: it can't be kept</span>")
94
+ :else "")))
95
+
96
+ (defn render-talk-chat! []
97
+ (when-let [el ($ "talk-chat")]
98
+ (let [{:keys [items order]} @talk-state
99
+ at-end (< (- (.-scrollHeight el) (.-scrollTop el) (.-clientHeight el)) 30)]
100
+ (set! (.-innerHTML el)
101
+ (if (seq order)
102
+ (apply str
103
+ (for [id order :let [it (get items id)] :when it]
104
+ (str "<div class=\"titem" (when (= (my-author) (:author it)) " mine") "\" data-item=\"" (esc id) "\">"
105
+ "<div class=\"twho\">" (esc (:name it)) "<time>" (.slice (.toISOString (js/Date. (:t it))) 11 19) "</time></div>"
106
+ "<div class=\"tside\">"
107
+ (when-not (:kept it) (str "<button class=\"act small\" data-talk-keep=\"" (esc id) "\" title=\"Keep it in the work\">Keep</button>"))
108
+ (talk-item-state it) "</div>"
109
+ "<div class=\"twhat\">"
110
+ (case (:kind it)
111
+ :message (str (esc (:text it)) (when (seq (:anchors it)) (str "<div>" (talk-anchor-chips (:anchors it)) "</div>")))
112
+ :note (if (:url it)
113
+ (str "voice note · " (.toFixed (or (:dur it) 0) 1) " s <audio controls preload=\"none\" src=\"" (:url it) "\"></audio>")
114
+ "voice note · arriving…")
115
+ :frame (str "<img alt=\"a frame of " (esc (:name it)) "\" src=\"" (:image it) "\">")
116
+ "")
117
+ "</div></div>")))
118
+ (if (:id @talk-room)
119
+ "<p class=\"ask-note\">What you say here is for the people in the room now; <b>Keep</b> makes it part of the work.</p>"
120
+ "")))
121
+ (when at-end (set! (.-scrollTop el) (.-scrollHeight el))))))
122
+
123
+ (defn render-talk-asks! []
124
+ (when-let [el ($ "talk-asks")]
125
+ (let [as (:asks @talk-state)]
126
+ (set! (.-hidden el) (empty? as))
127
+ (set! (.-innerHTML el)
128
+ (apply str (for [{:keys [item-id by-name kind]} as :let [it (get-in @talk-state [:items item-id])]]
129
+ (str "<div class=\"task\"><div><b>" (esc by-name) "</b> wants to keep your "
130
+ (case kind "note" "voice note" "frame" "video frame" "message")
131
+ (when (= :message (:kind it)) (str " “" (esc (subs (:text it) 0 (min 80 (count (:text it))))) "”"))
132
+ " in the work.</div><div class=\"ask-bar\"><button class=\"act primary small\" data-talk-answer=\"yes\" data-id=\"" (esc item-id) "\">Allow</button>"
133
+ "<button class=\"act small\" data-talk-answer=\"no\" data-id=\"" (esc item-id) "\">Decline</button></div></div>")))))))
134
+
135
+ (defn render-talk! []
136
+ (let [in-room (and (= :room (:kind @work)) (talk-signal-url))]
137
+ (when-let [el ($ "talk-off")]
138
+ (set! (.-hidden el) (boolean in-room))
139
+ (set! (.-innerHTML el)
140
+ (cond (not= :room (:kind @work)) "Talking happens in a <b>shared room</b>: open or create one in <b>Sessions</b>, and everyone in it can write, talk, send voice notes and see each other."
141
+ :else "This page has no relay (<code>?peer=off</code>, or claude.ai): rooms to talk need a relay 0.3 (<code>npx @kairotic/relay</code>).")))
142
+ (when-let [el ($ "talk-on")] (set! (.-hidden el) (not in-room))))
143
+ (render-talk-peers!) (render-talk-chat!) (render-talk-asks!))
144
+
145
+ ;; ───────── items: said here, and arriving ─────────
146
+
147
+ (defn talk-add! [it]
148
+ (when-not (get-in @talk-state [:items (:id it)])
149
+ (swap! talk-state (fn [t] (-> t (assoc-in [:items (:id it)] it) (update :order conj (:id it))))))
150
+ (render-talk-chat!))
151
+
152
+ (defn talk-say!
153
+ "Says something to the people in the room: (talk-say! \"from #12, @b3 enters\")."
154
+ [text]
155
+ (when-let [m (tk/message (talk-me) text (talk-now))]
156
+ (talk-add! m)
157
+ (talk-to-all! {:item m})
158
+ (:id m)))
159
+
160
+ ;; ───────── keeping: a gesture of the work ─────────
161
+
162
+ (defn talk-free-y
163
+ "Where on the paper the next kept thing goes: under the last one of layer :talk."
164
+ []
165
+ (let [ms (filter (fn [[_ m]] (and (= :talk (:layer m)) (= :patch (first (:at m))))) (:marks (current)))]
166
+ (+ 30 (reduce max 0 (map (fn [[_ m]] (+ (nth (:at m) 2) (if (= :image (:kind m)) (:h m) 0))) ms)))))
167
+
168
+ (defn talk-mark! [m] (gesture! (str "(mark :talk " (next-mark-id) " " (pr-str m) ")")))
169
+
170
+ (defn talk-keep-message! [it]
171
+ (let [box (some (fn [a] (when-let [b (:box a)] (let [k (keyword b)] (when (get-in (current) [:boxes k]) k)))) (:anchors it))
172
+ txt (str (:name it) ": " (:text it))]
173
+ (talk-mark! {:kind :text :at (if box [box 0 -12] [:patch 30 (+ 20 (talk-free-y))]) :txt txt})
174
+ (js/Promise.resolve {})))
175
+
176
+ (defn talk-keep-note! [it]
177
+ (-> (snd/decode (.-buffer (.slice (talk-bytes (:b64 it)))))
178
+ (.then (fn [{:keys [sr chans]}] (snd/keep! sr chans)))
179
+ (.then (fn [d]
180
+ (let [id (next-id) [x y] (auto-pos)]
181
+ (gesture! (str "(box " id " \"sample\" " x " " y ")"))
182
+ (gesture! (str "(sample! " id " " (pr-str d) ")"))
183
+ (talk-mark! {:kind :text :at [id 0 -10] :txt (str "voice note · " (:name it))})
184
+ {:sound (:audio d)})))))
185
+
186
+ (defn talk-keep-frame! [it]
187
+ (let [y (+ 20 (talk-free-y))]
188
+ (talk-mark! {:kind :image :at [:patch 30 y] :src (:image it) :w 160 :h 120})
189
+ (talk-mark! {:kind :text :at [:patch 30 (+ y 134)] :txt (str "frame · " (:name it))})
190
+ (js/Promise.resolve {})))
191
+
192
+ (defn talk-kept! [k]
193
+ (when (get-in @talk-state [:items (:item k)]) (swap! talk-state assoc-in [:items (:item k) :kept] true))
194
+ (render-talk-chat!))
195
+
196
+ (defn talk-keep-now!
197
+ "Keeps an item (mine, or allowed): a gesture of the work here; every page of the room hears that it was
198
+ kept, and a sound is handed to them under its hash."
199
+ [it]
200
+ (-> (case (:kind it) :message (talk-keep-message! it) :note (talk-keep-note! it) :frame (talk-keep-frame! it) (js/Promise.resolve {}))
201
+ (.then (fn [extra]
202
+ (let [k (merge {:item (:id it) :kind (name (:kind it)) :by (talk-name)} extra)]
203
+ (talk-kept! k) (talk-to-all! {:kept k}))))
204
+ (.catch (fn [e] (talk-msg! (str "could not keep it: " (esc (or (.-message e) e))) "bad")))))
205
+
206
+ (defn talk-keep!
207
+ "Keeps something said in the room, by its id: at once if it is yours, else asking whoever said it."
208
+ [id]
209
+ (when-let [it (get-in @talk-state [:items id])]
210
+ (let [[what peer] (tk/keep-step (talk-me) it (keys (:peers @talk-room)))]
211
+ (case what
212
+ :keep (talk-keep-now! it)
213
+ :ask (do (swap! talk-state assoc-in [:mine-asks id] :asked)
214
+ (talk-to! peer {:ask {:item-id id :by-name (talk-name) :kind (name (:kind it))}}))
215
+ :gone (swap! talk-state assoc-in [:mine-asks id] :gone))
216
+ (render-talk-chat!))))
217
+
218
+ (defn talk-answer! [id yes?]
219
+ (let [ask (first (filter #(= id (:item-id %)) (:asks @talk-state)))]
220
+ (swap! talk-state update :asks (fn [as] (vec (remove #(= id (:item-id %)) as))))
221
+ (render-talk-asks!)
222
+ (when ask (talk-to! (:from ask) {:answer {:item-id id :ok yes?}}))))
223
+
224
+ ;; a sound kept by someone else: this page keeps its copy under the same hash, so the work sounds here too
225
+ (defn talk-on-kept! [o]
226
+ (let [k (js->clj o :keywordize-keys true)]
227
+ (talk-kept! k)
228
+ (when-let [h (:sound k)]
229
+ (when-let [it (get-in @talk-state [:items (:item k)])]
230
+ (when (:b64 it)
231
+ (-> (snd/decode (.-buffer (.slice (talk-bytes (:b64 it)))))
232
+ (.then (fn [{:keys [sr chans]}] (snd/store! h sr chans)))
233
+ (.catch (fn [e] (js/console.error "talk: a kept sound" e)))))))))
234
+
235
+ ;; ───────── what arrives from a peer ─────────
236
+
237
+ (defn talk-on! [peer o]
238
+ (cond
239
+ (.-item o) (talk-add! (assoc (talk-item (.-item o)) :peer peer))
240
+ (.-piece o) (let [p (js->clj (.-piece o) :keywordize-keys true)
241
+ [st whole] (tk/gather (:pieces @talk-state) p)]
242
+ (swap! talk-state assoc :pieces st)
243
+ (when whole
244
+ (let [bytes (talk-bytes whole)]
245
+ (.then (talk-sha bytes)
246
+ (fn [h] (when-let [[id it] (first (filter (fn [[_ it]] (= h (:hash it))) (:items @talk-state)))]
247
+ (swap! talk-state update-in [:items id] assoc :b64 whole
248
+ :url (js/URL.createObjectURL (js/Blob. #js [bytes] #js {:type (:mime it)})))
249
+ (render-talk-chat!)))))))
250
+ (.-state o) (let [s (.-state o)]
251
+ (swap! talk-room update-in [:peers peer] merge {:talk (.-talk s) :camera (.-camera s) :name (.-name s) :who (.-who s)})
252
+ (when-let [t (.querySelector js/document (str ".ttile[data-peer='" peer "']"))]
253
+ (set! (.-hidden t) (not (.-camera s)))
254
+ (set! (.-textContent (.querySelector t ".tlabel")) (.-name s)))
255
+ (render-talk-peers!))
256
+ (.-ask o) (do (swap! talk-state update :asks conj (assoc (js->clj (.-ask o) :keywordize-keys true) :from peer))
257
+ (render-talk-asks!)
258
+ ;; asked: the Room tab says so
259
+ (when-let [t ($ "tab-talk")] (.add (.-classList t) "attn")))
260
+ (.-answer o) (let [{:keys [item-id ok]} (js->clj (.-answer o) :keywordize-keys true)]
261
+ (swap! talk-state assoc-in [:mine-asks item-id] (if ok :allowed :declined))
262
+ (if ok (talk-keep-now! (get-in @talk-state [:items item-id])) (render-talk-chat!)))
263
+ (.-kept o) (talk-on-kept! (.-kept o))
264
+ ;; @kairotic/net (21-net.cljs): a value shared on a channel, the room's tempo
265
+ (.-share o) (net-on-share! (.-share o))
266
+ (.hasOwnProperty o "tempo") (net-on-tempo! (.-tempo o))))
267
+
268
+ ;; ───────── voice notes ─────────
269
+
270
+ (defn talk-note-start! []
271
+ (when-let [mic (:mic @talk-media)]
272
+ (let [mime (or (first (filter #(.isTypeSupported js/MediaRecorder %) ["audio/webm;codecs=opus" "audio/ogg;codecs=opus" "audio/mp4"])) "")
273
+ stream (js/MediaStream. #js [(.clone (first (.getAudioTracks mic)))])
274
+ rec (js/MediaRecorder. stream (if (seq mime) #js {:mimeType mime} #js {}))
275
+ chunks #js []]
276
+ (set! (.-enabled (first (.getAudioTracks stream))) true)
277
+ (set! (.-ondataavailable rec) (fn [e] (.push chunks (.-data e))))
278
+ (set! (.-onstop rec)
279
+ (fn [_]
280
+ (doseq [t (.getTracks stream)] (.stop t))
281
+ (let [blob (js/Blob. chunks #js {:type (.-mimeType rec)})]
282
+ (-> (.arrayBuffer blob)
283
+ (.then (fn [buf]
284
+ (-> (.decodeAudioData (eng/ctx) (.slice buf 0))
285
+ (.then (fn [ab]
286
+ (.then (talk-sha (js/Uint8Array. buf))
287
+ (fn [h]
288
+ (let [b64 (talk-b64 buf)
289
+ it {:kind :note :id (str (my-author) "-" (js/Math.round (talk-ms))) :author (my-author)
290
+ :peer (:id @talk-room) :name (talk-name) :t (talk-now) :hash h :mime (.-mimeType rec) :dur (.-duration ab)}]
291
+ (talk-add! (assoc it :b64 b64 :url (js/URL.createObjectURL blob)))
292
+ (talk-to-all! {:item it})
293
+ (doseq [p (tk/pieces h b64)] (talk-to-all! {:piece p}))))))))))
294
+ (.catch (fn [e] (talk-msg! (str "the note: " (esc (.-message e))) "bad")))))))
295
+ (.start rec)
296
+ (swap! talk-media assoc :rec rec)
297
+ (js/setTimeout (fn [] (when (= rec (:rec @talk-media)) (.stop rec) (swap! talk-media assoc :rec nil))) 60000))))
298
+
299
+ (defn talk-note-stop! []
300
+ (when-let [rec (:rec @talk-media)] (swap! talk-media assoc :rec nil) (when (= "recording" (.-state rec)) (.stop rec))))
301
+
302
+ ;; ───────── microphone, push to talk, camera ─────────
303
+
304
+ (defn talk-senders! []
305
+ (doseq [[_ p] (:peers @talk-room) :let [pc (:pc p)] :when pc
306
+ tr (.getTransceivers pc) :let [kind (.. tr -receiver -track -kind)]]
307
+ (.catch (.replaceTrack (.-sender tr) (case kind
308
+ "audio" (some-> (:mic @talk-media) .getAudioTracks first)
309
+ "video" (some-> (:cam @talk-media) .getVideoTracks first)
310
+ nil))
311
+ (fn [_] nil))))
312
+
313
+ (defn talk-send-state! []
314
+ (talk-to-all! {:state {:talk (:talking @talk-state) :camera (some? (:cam @talk-media)) :name (talk-name) :who (my-author)}}))
315
+
316
+ (defn talk-push! [on?]
317
+ (when-let [mic (:mic @talk-media)]
318
+ (when (not= on? (:talking @talk-state))
319
+ (doseq [t (.getAudioTracks mic)] (set! (.-enabled t) on?))
320
+ (swap! talk-state assoc :talking on?)
321
+ (when-let [b ($ "talk-ptt")] (.setAttribute b "aria-pressed" (str on?)) (set! (.-textContent b) (if on? "Talking…" "Hold to talk")))
322
+ (talk-send-state!) (render-talk-peers!))))
323
+
324
+ (defn talk-mic! []
325
+ (-> (.getUserMedia js/navigator.mediaDevices #js {:audio #js {:echoCancellation true :noiseSuppression true :autoGainControl true}})
326
+ (.then (fn [s]
327
+ (doseq [t (.getAudioTracks s)] (set! (.-enabled t) false))
328
+ (swap! talk-media assoc :mic s)
329
+ (doseq [id ["talk-ptt" "talk-note"]] (set! (.-disabled ($ id)) false))
330
+ (set! (.-disabled ($ "talk-mic")) true)
331
+ (talk-senders!)))
332
+ (.catch (fn [e] (talk-msg! (str "microphone: " (esc (.-message e))) "bad")))))
333
+
334
+ (defn talk-own-tile! []
335
+ (let [t (or (.querySelector js/document ".ttile[data-peer='me']")
336
+ (let [d (js/document.createElement "div")]
337
+ (set! (.-className d) "ttile") (.setAttribute d "data-peer" "me")
338
+ (set! (.-innerHTML d) "<video autoplay playsinline muted></video><span class=\"tlabel\"></span><button class=\"act small\" data-talk-frame=\"me\">Keep a frame</button>")
339
+ (.prepend ($ "talk-tiles") d) d))]
340
+ (set! (.-textContent (.querySelector t ".tlabel")) (str (talk-name) " (you)"))
341
+ (set! (.-srcObject (.querySelector t "video")) (:cam @talk-media))
342
+ (set! (.-hidden t) (nil? (:cam @talk-media)))))
343
+
344
+ (defn talk-cam! []
345
+ (if-let [c (:cam @talk-media)]
346
+ (do (doseq [t (.getTracks c)] (.stop t))
347
+ (swap! talk-media assoc :cam nil) (talk-senders!) (talk-own-tile!) (talk-send-state!)
348
+ (.setAttribute ($ "talk-cam") "aria-pressed" "false"))
349
+ (-> (.getUserMedia js/navigator.mediaDevices #js {:video #js {:width 320 :height 240 :frameRate 15}})
350
+ (.then (fn [s] (swap! talk-media assoc :cam s) (talk-senders!) (talk-own-tile!) (talk-send-state!)
351
+ (.setAttribute ($ "talk-cam") "aria-pressed" "true")))
352
+ (.catch (fn [e] (talk-msg! (str "camera: " (esc (.-message e))) "bad"))))))
353
+
354
+ (defn talk-frame!
355
+ "A frame of a camera tile (\"me\" or a peer's id), small, as something said in the room (and so kept like the rest)."
356
+ [peer-id]
357
+ (when-let [v (.querySelector js/document (str ".ttile[data-peer='" peer-id "'] video"))]
358
+ (when (pos? (.-videoWidth v))
359
+ (let [c (js/document.createElement "canvas")]
360
+ (set! (.-width c) 160) (set! (.-height c) 120)
361
+ (.drawImage (.getContext c "2d") v 0 0 160 120)
362
+ (let [mine? (= peer-id "me") p (get-in @talk-room [:peers peer-id])
363
+ it {:kind :frame :id (str "f-" (js/Math.round (talk-ms)) "-" (rand-int 1e6))
364
+ :author (if mine? (my-author) (:who p)) :peer (if mine? (:id @talk-room) peer-id)
365
+ :name (if mine? (talk-name) (:name p)) :t (talk-now) :image (.toDataURL c "image/jpeg" 0.6)}]
366
+ (talk-add! it) (talk-to-all! {:item it})
367
+ (:id it))))))
368
+
369
+ ;; ───────── the room: its peers and their connections ─────────
370
+
371
+ (defn talk-tile! [id]
372
+ (or (.querySelector js/document (str ".ttile[data-peer='" id "']"))
373
+ (let [d (js/document.createElement "div")]
374
+ (set! (.-className d) "ttile") (.setAttribute d "data-peer" id) (set! (.-hidden d) true)
375
+ (set! (.-innerHTML d) (str "<video autoplay playsinline muted></video><span class=\"tlabel\"></span>"
376
+ "<button class=\"act small\" data-talk-frame=\"" (esc id) "\">Keep a frame</button>"))
377
+ (.append ($ "talk-tiles") d) d)))
378
+
379
+ (defn talk-add-peer! [id nm]
380
+ (swap! talk-room (fn [r] (-> r (assoc-in [:peers id] {:name nm :n (:n r) :state "relay" :pending []}) (update :n inc))))
381
+ (render-talk-peers!))
382
+
383
+ (defn talk-drop-peer! [id]
384
+ (when-let [pc (get-in @talk-room [:peers id :pc])] (.close pc))
385
+ (some-> (.querySelector js/document (str ".ttile[data-peer='" id "']")) .remove)
386
+ (some-> (.querySelector js/document (str "audio.taudio[data-peer='" id "']")) .remove)
387
+ (swap! talk-room update :peers dissoc id)
388
+ (swap! talk-state update :asks (fn [as] (vec (remove #(= id (:from %)) as))))
389
+ (render-talk-asks!) (render-talk-peers!))
390
+
391
+ (defn talk-pc! [id initiator?]
392
+ (let [pc (js/RTCPeerConnection. #js {:iceServers #js [#js {:urls "stun:stun.l.google.com:19302"}]})
393
+ dc (.createDataChannel pc "talk" #js {:negotiated true :id 1 :ordered true})
394
+ state! (fn [s] (when (get-in @talk-room [:peers id]) (swap! talk-room assoc-in [:peers id :state] s) (render-talk-peers!)))]
395
+ (set! (.-onicecandidate pc) (fn [ev] (when-let [c (.-candidate ev)] (talk-signal! {:to id :data {:candidate (.toJSON c)}}))))
396
+ (set! (.-onopen dc) (fn [_] (state! "p2p") (talk-send-state!)))
397
+ (set! (.-onclose dc) (fn [_] (state! "relay")))
398
+ (set! (.-onmessage dc) (fn [ev] (talk-on! id (js/JSON.parse (.-data ev)))))
399
+ (set! (.-ontrack pc)
400
+ (fn [ev]
401
+ (let [tr (.-track ev) s (js/MediaStream. #js [tr])]
402
+ (if (= "audio" (.-kind tr))
403
+ (let [a (or (.querySelector js/document (str "audio.taudio[data-peer='" id "']"))
404
+ (let [a (js/document.createElement "audio")]
405
+ (set! (.-className a) "taudio") (.setAttribute a "data-peer" id) (set! (.-autoplay a) true)
406
+ (.append js/document.body a) a))]
407
+ (set! (.-srcObject a) s) (.catch (.play a) (fn [_] nil)))
408
+ (let [t (talk-tile! id)]
409
+ (set! (.-srcObject (.querySelector t "video")) s)
410
+ (set! (.-textContent (.querySelector t ".tlabel")) (or (get-in @talk-room [:peers id :name]) "")))))))
411
+ (when initiator?
412
+ (.addTransceiver pc "audio" #js {:direction "sendrecv"})
413
+ (.addTransceiver pc "video" #js {:direction "sendrecv"}))
414
+ (swap! talk-room update-in [:peers id] merge {:pc pc :dc dc})
415
+ (when initiator?
416
+ (talk-senders!)
417
+ (-> (.createOffer pc) (.then #(.setLocalDescription pc %))
418
+ (.then #(talk-signal! {:to id :data {:sdp (.toJSON (.-localDescription pc))}}))))
419
+ pc))
420
+
421
+ (defn talk-from! [id data]
422
+ (when (get-in @talk-room [:peers id])
423
+ (cond
424
+ (.-sdp data)
425
+ (let [d (.-sdp data) pc (or (get-in @talk-room [:peers id :pc]) (talk-pc! id false))]
426
+ (-> (.setRemoteDescription pc d)
427
+ (.then (fn [_]
428
+ (doseq [c (get-in @talk-room [:peers id :pending])] (.catch (.addIceCandidate pc c) (fn [_] nil)))
429
+ (swap! talk-room assoc-in [:peers id :pending] [])
430
+ (when (= "offer" (.-type d))
431
+ (doseq [tr (.getTransceivers pc)] (set! (.-direction tr) "sendrecv"))
432
+ (talk-senders!)
433
+ (-> (.createAnswer pc) (.then #(.setLocalDescription pc %))
434
+ (.then #(talk-signal! {:to id :data {:sdp (.toJSON (.-localDescription pc))}}))))))
435
+ (.catch (fn [e] (js/console.error "talk: WebRTC" e)))))
436
+ (.-candidate data)
437
+ (let [pc (get-in @talk-room [:peers id :pc])]
438
+ (if (and pc (.-remoteDescription pc))
439
+ (.catch (.addIceCandidate pc (.-candidate data)) (fn [_] nil))
440
+ (swap! talk-room update-in [:peers id :pending] conj (.-candidate data))))
441
+ (.-talk data) (talk-on! id (.-talk data)))))
442
+
443
+ (defn talk-on-signal! [f]
444
+ (cond
445
+ (.-pong f) (swap! talk-room update :clock tk/clock-sample (.-pong f) (.-t f) (talk-ms))
446
+ (.-welcome f) (let [w (.-welcome f)]
447
+ (swap! talk-room assoc :id (.-id w))
448
+ (talk-msg! "in the room" "ok")
449
+ (doseq [p (.-peers w)] (talk-add-peer! (.-id p) (.-name p)) (talk-pc! (.-id p) true))
450
+ (render-talk!))
451
+ (.-joined f) (let [j (.-joined f)] (talk-add-peer! (.-id j) (.-name j)) (net-joined! (.-id j)))
452
+ (.-left f) (talk-drop-peer! (.-left f))
453
+ (.-from f) (talk-from! (.-from f) (.-data f))
454
+ (.-error f) (talk-msg! (esc (.-error f)) "bad")))
455
+
456
+ (defn talk-ping! [] (talk-signal! {:ping (talk-ms)}))
457
+
458
+ (defn talk-leave! []
459
+ (when-let [t (:timer @talk-room)] (js/clearInterval t))
460
+ (doseq [id (keys (:peers @talk-room))] (talk-drop-peer! id))
461
+ (when-let [ws (:ws @talk-room)] (set! (.-onclose ws) nil) (.close ws))
462
+ (swap! talk-room assoc :ws nil :key nil :id nil :peers {} :clock (tk/new-clock) :timer nil)
463
+ ;; what was said goes with the room
464
+ (reset! talk-state {:items {} :order [] :asks [] :mine-asks {} :pieces {} :talking false})
465
+ (render-talk!))
466
+
467
+ (defn talk-join! [k url]
468
+ (talk-leave!)
469
+ (let [ws (try (js/WebSocket. url) (catch :default e (talk-msg! (esc (.-message e)) "bad") nil))]
470
+ (when ws
471
+ (swap! talk-room assoc :ws ws :key k)
472
+ (talk-msg! "connecting…" "")
473
+ (set! (.-onopen ws)
474
+ (fn [_]
475
+ (talk-signal! {:join k :name (talk-name)})
476
+ (doseq [i (range 8)] (js/setTimeout talk-ping! (* 60 i)))
477
+ (swap! talk-room assoc :timer (js/setInterval talk-ping! 2000))))
478
+ (set! (.-onmessage ws) (fn [ev] (talk-on-signal! (js/JSON.parse (.-data ev)))))
479
+ (set! (.-onerror ws) (fn [_] (talk-msg! "the relay has no rooms (relay 0.3 or later)" "bad")))
480
+ (set! (.-onclose ws) (fn [_] (when (= ws (:ws @talk-room)) (talk-msg! "out of the room" "") (swap! talk-room assoc :ws nil :id nil) (render-talk!)))))))
481
+
482
+ (defn talk-sync!
483
+ "In the room the open work is (a shared room, with a relay), or in none."
484
+ []
485
+ (let [url (talk-signal-url)
486
+ k (when (and url (= :room (:kind @work)) (:id @work)) (str "kairotic-" (:id @work)))]
487
+ (cond (and k (not= k (:key @talk-room))) (talk-join! k url)
488
+ (and (nil? k) (:key @talk-room)) (talk-leave!)
489
+ :else (render-talk!))))
490
+
491
+ (defn talk-audio-in
492
+ "Promise of the bytes of voice received from the room so far (all its connections): to see that it flows."
493
+ []
494
+ (js/Promise.all (clj->js (for [[_ p] (:peers @talk-room) :when (:pc p)]
495
+ (.then (.getStats (:pc p))
496
+ (fn [r] (let [n (atom 0)]
497
+ (.forEach r (fn [s & _] (when (and (= "inbound-rtp" (.-type s)) (= "audio" (.-kind s)))
498
+ (swap! n + (or (.-bytesReceived s) 0)))))
499
+ @n)))))))
500
+
501
+ ;; ───────── wiring ─────────
502
+
503
+ (defn talk-hold! [el down up]
504
+ (.addEventListener el "pointerdown" (fn [e] (when-not (.-disabled el) (.preventDefault e) (down))))
505
+ (doseq [t ["pointerup" "pointercancel" "pointerleave"]] (.addEventListener el t (fn [_] (up)))))
506
+
507
+ (when ($ "panel-talk")
508
+ (set! (.-value ($ "talk-name")) (or (try (.getItem js/localStorage "kairotic-name") (catch :default _ nil))
509
+ (str "player " (.toUpperCase (.slice (.toString (js/Math.random) 36) 2 5)))))
510
+ (.addEventListener ($ "talk-name") "change"
511
+ (fn [_] (try (.setItem js/localStorage "kairotic-name" (talk-name)) (catch :default _ nil)) (talk-send-state!) (render-talk-peers!)))
512
+ (.addEventListener ($ "talk-send") "click" (fn [_] (talk-say! (.-value ($ "talk-say"))) (set! (.-value ($ "talk-say")) "")))
513
+ (.addEventListener ($ "talk-say") "keydown"
514
+ (fn [e] (when (= "Enter" (.-key e)) (.stopPropagation e) (talk-say! (.-value ($ "talk-say"))) (set! (.-value ($ "talk-say")) ""))))
515
+ (.addEventListener ($ "talk-mic") "click" (fn [_] (talk-mic!)))
516
+ (.addEventListener ($ "talk-cam") "click" (fn [_] (talk-cam!)))
517
+ (talk-hold! ($ "talk-ptt") #(talk-push! true) #(talk-push! false))
518
+ (talk-hold! ($ "talk-note") (fn [] (.setAttribute ($ "talk-note") "aria-pressed" "true") (talk-note-start!))
519
+ (fn [] (.setAttribute ($ "talk-note") "aria-pressed" "false") (talk-note-stop!)))
520
+ (.addEventListener ($ "panel-talk") "click"
521
+ (fn [e] (let [t (.-target e)]
522
+ (when-let [id (.getAttribute t "data-talk-keep")] (talk-keep! id))
523
+ (when-let [a (.getAttribute t "data-talk-answer")] (talk-answer! (.getAttribute t "data-id") (= a "yes")))
524
+ (when-let [p (.getAttribute t "data-talk-frame")] (talk-frame! p)))))
525
+ (.addEventListener ($ "tab-talk") "click" (fn [_] (.remove (.-classList ($ "tab-talk")) "attn") (render-talk!)))
526
+ (add-watch work :talk (fn [_ _ o n] (when (not= [(:kind o) (:id o)] [(:kind n) (:id n)]) (talk-sync!))))
527
+ (talk-sync!))
@@ -0,0 +1,110 @@
1
+ (in-ns 'user)
2
+ ;; ───────────────────────── the room's channels and tempo (@kairotic/net, discussion #135) ─────────────────────────
3
+ ;; Boxes of @kairotic/net (share, shared, pulse) reach the room through window.kairoticRoom, so a package
4
+ ;; never touches the page: a value shared on a channel goes to every page of the room (this one too, as
5
+ ;; a bus), and the tempo is one for the whole room — started on any page, the same beat on all, from the
6
+ ;; room's clock (talk-now). Outside a room they work for this page alone.
7
+
8
+ (defonce net-listeners (atom {})) ; channel → {key fn}
9
+ (defonce net-tempo (atom nil)) ; {:t0 shared-ms :bpm b} or nil
10
+ (defonce net-tempo-watchers (atom {})) ; key → fn
11
+
12
+ (defn net-deliver! [ch v from]
13
+ (doseq [[_ f] (get @net-listeners ch)] (try (f v from) (catch :default e (js/console.error "share" e)))))
14
+
15
+ (defn net-share!
16
+ "Shares a value on a channel of the room: (net-share! \"notes\" [60 64 67])."
17
+ [ch v]
18
+ (let [edn (pr-str v)]
19
+ (when (< (count edn) 60000)
20
+ (talk-to-all! {:share {:ch (str ch) :v edn :from (talk-name)}})
21
+ (net-deliver! (str ch) v (talk-name)))
22
+ v))
23
+
24
+ (defn net-on-share! [o]
25
+ (let [v (try (clojure.edn/read-string {:default (fn [_ x] x)} (.-v o)) (catch :default _ ::bad))]
26
+ (when-not (= v ::bad) (net-deliver! (str (.-ch o)) v (.-from o)))))
27
+
28
+ (defn net-set-tempo!
29
+ "The tempo of the room: {:t0 shared-ms :bpm b} (beat 0 at t0), or nil to stop. Every page of the room hears it."
30
+ [t]
31
+ (reset! net-tempo t)
32
+ (talk-to-all! {:tempo t})
33
+ t)
34
+
35
+ (defn net-on-tempo! [t]
36
+ (reset! net-tempo (when t {:t0 (.-t0 t) :bpm (.-bpm t)})))
37
+
38
+ (defn net-joined! [id] (talk-to! id {:tempo @net-tempo}))
39
+
40
+ (defn net-beat "The beat of the room now (fractional), or nil without a tempo." []
41
+ (when-let [{:keys [t0 bpm]} @net-tempo] (/ (* (- (talk-now) t0) bpm) 60000)))
42
+
43
+ (defn net-start!
44
+ "Starts the room's tempo at bpm: beat 0 on the next whole second plus one, the same for every page."
45
+ [bpm]
46
+ (let [n (talk-now)]
47
+ (net-set-tempo! {:t0 (+ 1000 (* 1000 (js/Math.ceil (/ n 1000)))) :bpm (api/clamp bpm 20 400)})))
48
+
49
+ (defn net-bpm!
50
+ "Changes the tempo of the room keeping the beat where it is (no jump)."
51
+ [bpm]
52
+ (if-let [b (net-beat)]
53
+ (let [bpm (api/clamp bpm 20 400) n (talk-now)]
54
+ (net-set-tempo! {:t0 (- n (/ (* b 60000) bpm)) :bpm bpm}))
55
+ (net-start! bpm)))
56
+
57
+ (add-watch net-tempo :watchers (fn [_ _ _ t] (doseq [[_ f] @net-tempo-watchers] (try (f t) (catch :default _ nil)))))
58
+
59
+ ;; what the boxes of @kairotic/net see
60
+ (set! (.-kairoticRoom js/window)
61
+ #js {:now (fn [] (talk-now))
62
+ :inRoom (fn [] (boolean (:id @talk-room)))
63
+ :share (fn [ch v] (net-share! ch v))
64
+ :onShare (fn [ch f] (let [k (gensym "l")] (swap! net-listeners assoc-in [(str ch) k] f)
65
+ (fn [] (swap! net-listeners update (str ch) dissoc k))))
66
+ :tempo (fn [] @net-tempo)
67
+ :start (fn [bpm] (net-start! bpm))
68
+ :bpm (fn [bpm] (net-bpm! bpm))
69
+ :stop (fn [] (net-set-tempo! nil))})
70
+
71
+ ;; ───────── the tempo in the Room tab, with a metronome ─────────
72
+
73
+ (defonce net-metro (atom {:on false :until nil}))
74
+
75
+ (defn net-click! [at-ms down?]
76
+ (let [c (eng/ctx) t (+ (.-currentTime c) (max 0 (/ (- at-ms (talk-now)) 1000)))
77
+ o (.createOscillator c) g (.createGain c)]
78
+ (set! (.. o -frequency -value) (if down? 1760 1320))
79
+ (.setValueAtTime (.-gain g) 0.3 t) (.exponentialRampToValueAtTime (.-gain g) 0.001 (+ t 0.05))
80
+ (.connect o g) (.connect g (.-destination c)) (.start o t) (.stop o (+ t 0.06))))
81
+
82
+ (defn net-text! [el v] (when (and el (not= v (.-textContent el))) (set! (.-textContent el) v)))
83
+
84
+ (defn net-frame! [& _]
85
+ (let [t @net-tempo
86
+ shown (some-> ($ "panel-talk") .-hidden not)]
87
+ ;; nothing to draw unless the Room tab is open, and nothing to do without a tempo or a room
88
+ (when (and shown (or t (:id @talk-room)))
89
+ (let [n (talk-now) el ($ "net-beat") b (net-beat)]
90
+ (net-text! el (cond (nil? b) "·" (neg? b) (str "in " (.toFixed (/ (- (:t0 t) n) 1000) 1) " s") :else (str (inc (js/Math.floor b)))))
91
+ (when el (.toggle (.-classList el) "on" (boolean (and b (>= b 0) (< (mod b 1) 0.15)))))
92
+ (net-text! ($ "net-clock") (if (tk/clock-offset (:clock @talk-room)) (str "clock ±" (.toFixed (tk/clock-error (:clock @talk-room)) 1) " ms") ""))))
93
+ ;; the metronome: clicks 150 ms ahead on the audio clock
94
+ (when (and t (:on @net-metro))
95
+ (let [n (talk-now) from (max n (or (:until @net-metro) n)) to (+ n 150)
96
+ per (/ 60000 (:bpm t))]
97
+ (doseq [k (range (js/Math.ceil (/ (* (- from (:t0 t)) (:bpm t)) 60000)) (inc (js/Math.floor (/ (* (- to (:t0 t)) (:bpm t)) 60000))))
98
+ :let [at (+ (:t0 t) (* k per))] :when (and (>= k 0) (>= at from) (< at to))]
99
+ (net-click! at (zero? (mod k 4))))
100
+ (swap! net-metro assoc :until to))))
101
+ (js/requestAnimationFrame net-frame!))
102
+
103
+ (when ($ "net-start")
104
+ (.addEventListener ($ "net-start") "click" (fn [_] (.resume (eng/ctx)) (net-start! (js/parseFloat (.-value ($ "net-bpm")))) nil))
105
+ (.addEventListener ($ "net-stop") "click" (fn [_] (net-set-tempo! nil) nil))
106
+ (.addEventListener ($ "net-bpm") "change" (fn [_] (when @net-tempo (net-bpm! (js/parseFloat (.-value ($ "net-bpm"))))) nil))
107
+ (.addEventListener ($ "net-metro") "change"
108
+ (fn [_] (.resume (eng/ctx)) (swap! net-metro assoc :on (.-checked ($ "net-metro")) :until nil) nil))
109
+ (add-watch net-tempo :tab (fn [_ _ _ t] (when (and t ($ "net-bpm")) (set! (.-value ($ "net-bpm")) (str (js/Math.round (:bpm t)))))))
110
+ (net-frame!))