@kairotic/net 0.0.0-stage → 0.1.4
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.
- package/README.md +30 -2
- package/dist/index.mjs +28 -0
- package/dist/pulse.mjs +9 -0
- package/dist/share.mjs +9 -0
- package/dist/shared.mjs +9 -0
- package/package.json +37 -4
- package/src/pulse.cljs +62 -0
- package/src/share.cljs +32 -0
- package/src/shared.cljs +38 -0
package/README.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kairotic/net
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
*Official package of [Kairotic](https://github.com/bonuoq/kairotic) · [all packages](../README.md)*
|
|
4
|
+
|
|
5
|
+
Kairotic in a shared room (discussion [#135](https://github.com/bonuoq/kairotic/discussions/135), tried in
|
|
6
|
+
Studies 34 and 36). The pages of a room are connected to each other directly (WebRTC, through the
|
|
7
|
+
relay's `/signal`: the **Room** tab), and these boxes use that connection: values shared on channels, and
|
|
8
|
+
**one tempo for the whole room**, the same beat on every page from the room's clock — whatever each
|
|
9
|
+
computer's clock says.
|
|
10
|
+
|
|
11
|
+
| Class | What it does |
|
|
12
|
+
|---|---|
|
|
13
|
+
| `share [channel]` | what arrives goes to everyone in the room on that channel (`main` if none): any value, an event, a list; in order, with or without ▶. A message of the moment: the history does not keep it |
|
|
14
|
+
| `shared [channel]` | what anyone in the room shares on that channel — this page included —, while ▶ runs; on the right, who shared it |
|
|
15
|
+
| `pulse [division]` | the room's beat, while ▶ runs: the count of each pulse on the audio clock (division 1 beats, 2 eighths, 0.25 bars of four), the bpm on the right; `start [bpm]`, `stop`, `bpm n` change the tempo for everyone |
|
|
16
|
+
|
|
17
|
+
```clojure
|
|
18
|
+
;; on one page: a keyboard that others play
|
|
19
|
+
(box :b1 "midiin" 40 30)
|
|
20
|
+
(box :b2 "share notes" 40 80)
|
|
21
|
+
(connect :b1 2 :b2 0)
|
|
22
|
+
;; on another: what the first one plays, on its own synth~
|
|
23
|
+
(box :b3 "shared notes" 40 30)
|
|
24
|
+
(box :b4 "synth~" 40 80)
|
|
25
|
+
(connect :b3 0 :b4 0)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The Room tab shows the tempo, starts and stops it for everyone, and has a metronome. Outside a shared
|
|
29
|
+
room the boxes work for this page alone. Values travel as EDN (at most 60 KB each).
|
|
30
|
+
|
|
31
|
+
Writing one of its classes in a work brings the package into it. Licence: [MPL-2.0](../../LICENSE).
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Generated by packages/build.mjs
|
|
2
|
+
export default {
|
|
3
|
+
"package": "@kairotic/net",
|
|
4
|
+
"version": "0.1.4",
|
|
5
|
+
"api": 1,
|
|
6
|
+
"author": "bonuoq",
|
|
7
|
+
"license": "MPL-2.0",
|
|
8
|
+
"components": [
|
|
9
|
+
{
|
|
10
|
+
"class": "pulse",
|
|
11
|
+
"license": "MPL-2.0",
|
|
12
|
+
"doc": "The room's beat, while ▶ runs: the count of each pulse (0 at the start of the tempo) on the left, on the audio clock, divided (1, beats; 2, eighths; 0.25, bars of four); on the right, the bpm when the tempo starts or changes, and 0 when it stops. The tempo is one for the whole room — the same beat on every page, from the room's clock, whatever each computer's clock says —; start [bpm], stop and bpm n (which keeps the beat where it is) change it for everyone, as the Room tab does. Outside a room it is this page's tempo.",
|
|
13
|
+
"source": ";; pulse · the room's beat: one tempo for everyone in the room, the same beat on every page\n;; Part of @kairotic/net. Written against the Kairotic API (api/…). Discussion #135.\n(let [room (fn [] (.-kairoticRoom js/window))]\n {;; ── about ──\n :class \"pulse\"\n :license \"MPL-2.0\"\n :doc \"The room's beat, while ▶ runs: the count of each pulse (0 at the start of the tempo) on the left, on the audio clock, divided (1, beats; 2, eighths; 0.25, bars of four); on the right, the bpm when the tempo starts or changes, and 0 when it stops. The tempo is one for the whole room — the same beat on every page, from the room's clock, whatever each computer's clock says —; start [bpm], stop and bpm n (which keeps the beat where it is) change it for everyone, as the Room tab does. Outside a room it is this page's tempo.\"\n :usage \"pulse [division]\"\n :io [1 2]\n :size [96 30]\n :args [[:division 1]]\n :help {:inlets [\":start [bpm] · :stop · :bpm n: the tempo of the whole room\"]\n :outlets [\"the count of each pulse, on its beat\" \"the bpm (0 when it stops)\"]\n :example \"(box :b1 \\\"msg :start 100\\\" 40 30)\\n(box :b2 \\\"msg :stop\\\" 140 30)\\n(box :b3 \\\"pulse\\\" 40 80)\\n(box :b4 \\\"print\\\" 40 130)\\n(connect :b1 0 :b3 0)\\n(connect :b2 0 :b3 0)\\n(connect :b3 0 :b4 0)\"\n :try \"Press ▶ and click start 100: a count every beat, a second later. In a shared room, every page with a pulse box counts the same beat.\"\n :see [\"share\" \"shared\" \"metro\" \"player\"]}\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value]}]\n (cond\n (and (= inlet :audio) (number? (:count value))) {:out [[0 (:count value)]] :show {:count (:count value)}}\n (and (= inlet :audio) (contains? value :bpm)) {:out [[1 (or (:bpm value) 0)]] :show {:bpm (:bpm value)}}\n (= inlet :task) {}\n (#{:stop} value) {:effects [[:tempo [:stop]]]}\n (#{:start} value) {:effects [[:tempo [:start 120]]]}\n (and (sequential? value) (#{:start} (first value))) {:effects [[:tempo [:start (or (api/num-of (rest value)) 120)]]]}\n (and (sequential? value) (#{:bpm} (first value)) (api/num-of (rest value))) {:effects [[:tempo [:bpm (api/num-of (rest value))]]]}\n :else {}))\n :tasks {:tempo (fn [[op x] _]\n (if-let [r (room)]\n (do (case op :start ((.-start r) x) :bpm ((.-bpm r) x) :stop ((.-stop r))) (js/Promise.resolve true))\n (js/Promise.reject (js/Error. \"pulse: this page has no room layer\"))))}\n\n ;; ── view ──\n :derive (fn [{:keys [shown]}] {:label (cond (number? (:count shown)) (str \"pulse \" (:count shown))\n (:bpm shown) (str \"pulse · \" (api/nice (:bpm shown)) \" bpm\")\n :else \"pulse\")\n :dot (if (number? (:count shown)) :kairos :grid)})\n :view [[:rect {:x 1 :y 1 :w 94 :h 28 :r 3 :stroke :kairos :fill :bg}]\n [:circle {:cx 84 :cy 15 :r 5 :stroke :$dot :fill :$dot}]\n [:text {:x 8 :y 19 :size 11} :$label]]\n\n ;; ── outside: the room's tempo, read every 25 ms, each pulse reported 100 ms ahead on the audio clock ──\n :audio {:create (fn [ac b _]\n (let [div (let [n (js/parseFloat (second (.split (.trim (str (:text b))) #\"\\s+\")))] (if (and (js/isFinite n) (pos? n)) n 1))\n node {:alive (atom true) :until (atom nil) :tempo (atom :none) :inlets [] :outlets []}\n tick (fn []\n (when-let [r (room)]\n (let [t ((.-tempo r)) t (when t (js->clj t :keywordize-keys true)) n ((.-now r))]\n (when (not= t @(:tempo node))\n (reset! (:tempo node) t) (reset! (:until node) nil)\n (api/report! (:id b) {:bpm (:bpm t)} nil))\n (when-let [{:keys [t0 bpm]} t]\n (let [per (/ 60000 (* bpm div))\n from (max n (or @(:until node) n)) to (+ n 100)]\n (doseq [k (range (js/Math.ceil (/ (- from t0) per)) (inc (js/Math.floor (/ (- to t0) per))))\n :let [at (+ t0 (* k per))] :when (and (>= k 0) (>= at from) (< at to))]\n (api/report! (:id b) {:count k} (+ (.-currentTime ac) (/ (- at n) 1000))))\n (reset! (:until node) to))))))\n timer (js/setInterval (fn [] (when @(:alive node) (tick))) 25)]\n (assoc node :timer timer)))\n :dispose (fn [node] (reset! (:alive node) false) (js/clearInterval (:timer node)))}})\n"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"class": "share",
|
|
17
|
+
"license": "MPL-2.0",
|
|
18
|
+
"doc": "Shares what arrives with everyone in the room, on a channel (main if none): every shared box of the room listening to that channel sends it out — on every page, this one too. Any value: a number, a list, an event from midiin or motion, a map. It goes on the room's reliable channel, in order, with or without ▶. Outside a shared room it reaches the shared boxes of this page only. It is a message of the moment, not a gesture: the history does not keep it.",
|
|
19
|
+
"source": ";; share · a value to everyone in the room, on a channel\n;; Part of @kairotic/net. Written against the Kairotic API (api/…). Discussion #135.\n(let [room (fn [] (.-kairoticRoom js/window))]\n {;; ── about ──\n :class \"share\"\n :license \"MPL-2.0\"\n :doc \"Shares what arrives with everyone in the room, on a channel (main if none): every shared box of the room listening to that channel sends it out — on every page, this one too. Any value: a number, a list, an event from midiin or motion, a map. It goes on the room's reliable channel, in order, with or without ▶. Outside a shared room it reaches the shared boxes of this page only. It is a message of the moment, not a gesture: the history does not keep it.\"\n :usage \"share [channel]\"\n :io [1 0]\n :size [96 30]\n :args [[:channel nil]]\n :help {:inlets [\"any value: it goes to everyone in the room\"]\n :outlets []\n :example \"(box :b1 \\\"msg 60 64 67\\\" 40 30)\\n(box :b2 \\\"share chords\\\" 40 80)\\n(box :b3 \\\"shared chords\\\" 200 30)\\n(box :b4 \\\"print\\\" 200 80)\\n(connect :b1 0 :b2 0)\\n(connect :b3 0 :b4 0)\"\n :try \"Press ▶ and click the message: print shows it, and so does every page of the room with a shared chords box.\"\n :see [\"shared\" \"pulse\" \"oscout\"]}\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value args]}]\n (if (= inlet :task)\n {}\n {:effects [[:share [(if (:channel args) (name (:channel args)) \"main\") value]]]\n :show {:label (let [s (pr-str value)] (if (> (count s) 14) (str (subs s 0 13) \"…\") s))}}))\n :tasks {:share (fn [[ch v] _]\n (if-let [r (room)]\n (do ((.-share r) ch v) (js/Promise.resolve true))\n (js/Promise.reject (js/Error. \"share: this page has no room layer\"))))}\n\n ;; ── view ──\n :derive (fn [{:keys [shown args]}] {:label (or (:label shown) (str \"share \" (if (:channel args) (name (:channel args)) \"main\")))})\n :view [[:rect {:x 1 :y 1 :w 94 :h 28 :r 3 :stroke :kairos :fill :bg}]\n [:text {:x 8 :y 19 :size 11} :$label]]})\n"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"class": "shared",
|
|
23
|
+
"license": "MPL-2.0",
|
|
24
|
+
"doc": "What anyone in the room shares on a channel (main if none) with a share box — this page included —, while ▶ runs: the value on the left, the name of who shared it on the right. A midiin on one page and a shared box feeding synth~ on another is playing someone else's instrument; an event map from motion on a phone moves a knob on the computer.",
|
|
25
|
+
"source": ";; shared · what is shared in the room on a channel\n;; Part of @kairotic/net. Written against the Kairotic API (api/…). Discussion #135.\n(let [room (fn [] (.-kairoticRoom js/window))]\n {;; ── about ──\n :class \"shared\"\n :license \"MPL-2.0\"\n :doc \"What anyone in the room shares on a channel (main if none) with a share box — this page included —, while ▶ runs: the value on the left, the name of who shared it on the right. A midiin on one page and a shared box feeding synth~ on another is playing someone else's instrument; an event map from motion on a phone moves a knob on the computer.\"\n :usage \"shared [channel]\"\n :io [0 2]\n :size [96 30]\n :args [[:channel nil]]\n :help {:inlets []\n :outlets [\"the value shared\" \"who shared it\"]\n :example \"(box :b1 \\\"shared notes\\\" 40 30)\\n(box :b2 \\\"synth~\\\" 40 80)\\n(box :b3 \\\"dac~\\\" 40 130)\\n(connect :b1 0 :b2 0)\\n(connect :b2 0 :b3 0)\\n(connect :b2 0 :b3 1)\"\n :try \"In a shared room, press ▶: what someone sends to a share notes box, you hear.\"\n :see [\"share\" \"pulse\"]}\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value]}]\n (cond\n (and (= inlet :audio) (contains? value :v))\n {:out [[1 (str (:from value))] [0 (:v value)]] :show {:label (str (:from value) \" → \" (let [s (pr-str (:v value))] (if (> (count s) 10) (str (subs s 0 9) \"…\") s)))}}\n (and (= inlet :audio) (:error value)) {:effects [[:print (:error value)]]}\n :else {}))\n\n ;; ── view ──\n :derive (fn [{:keys [shown args]}] {:label (or (:label shown) (str \"shared \" (if (:channel args) (name (:channel args)) \"main\")))})\n :view [[:rect {:x 1 :y 1 :w 94 :h 28 :r 3 :stroke :kairos :fill :bg}]\n [:text {:x 8 :y 19 :size 11} :$label]]\n\n ;; ── outside: the room's channel, while ▶ runs ──\n :audio {:create (fn [_ b _]\n (let [ch (or (second (.split (.trim (str (:text b))) #\"\\s+\")) \"main\")\n node {:alive (atom true) :inlets [] :outlets []}]\n (if-let [r (room)]\n (assoc node :off ((.-onShare r) ch (fn [v from] (when @(:alive node) (api/report! (:id b) {:v v :from from} nil)))))\n (do (api/report! (:id b) {:error \"shared: this page has no room layer\"} nil) (assoc node :off (fn [] nil))))))\n :dispose (fn [node] (reset! (:alive node) false) ((:off node)))}})\n"
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
};
|
package/dist/pulse.mjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Generated by packages/build.mjs from src/pulse.cljs
|
|
2
|
+
export default {
|
|
3
|
+
"class": "pulse",
|
|
4
|
+
"version": "0.1.4",
|
|
5
|
+
"author": "bonuoq",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"doc": "The room's beat, while ▶ runs: the count of each pulse (0 at the start of the tempo) on the left, on the audio clock, divided (1, beats; 2, eighths; 0.25, bars of four); on the right, the bpm when the tempo starts or changes, and 0 when it stops. The tempo is one for the whole room — the same beat on every page, from the room's clock, whatever each computer's clock says —; start [bpm], stop and bpm n (which keeps the beat where it is) change it for everyone, as the Room tab does. Outside a room it is this page's tempo.",
|
|
8
|
+
"source": ";; pulse · the room's beat: one tempo for everyone in the room, the same beat on every page\n;; Part of @kairotic/net. Written against the Kairotic API (api/…). Discussion #135.\n(let [room (fn [] (.-kairoticRoom js/window))]\n {;; ── about ──\n :class \"pulse\"\n :license \"MPL-2.0\"\n :doc \"The room's beat, while ▶ runs: the count of each pulse (0 at the start of the tempo) on the left, on the audio clock, divided (1, beats; 2, eighths; 0.25, bars of four); on the right, the bpm when the tempo starts or changes, and 0 when it stops. The tempo is one for the whole room — the same beat on every page, from the room's clock, whatever each computer's clock says —; start [bpm], stop and bpm n (which keeps the beat where it is) change it for everyone, as the Room tab does. Outside a room it is this page's tempo.\"\n :usage \"pulse [division]\"\n :io [1 2]\n :size [96 30]\n :args [[:division 1]]\n :help {:inlets [\":start [bpm] · :stop · :bpm n: the tempo of the whole room\"]\n :outlets [\"the count of each pulse, on its beat\" \"the bpm (0 when it stops)\"]\n :example \"(box :b1 \\\"msg :start 100\\\" 40 30)\\n(box :b2 \\\"msg :stop\\\" 140 30)\\n(box :b3 \\\"pulse\\\" 40 80)\\n(box :b4 \\\"print\\\" 40 130)\\n(connect :b1 0 :b3 0)\\n(connect :b2 0 :b3 0)\\n(connect :b3 0 :b4 0)\"\n :try \"Press ▶ and click start 100: a count every beat, a second later. In a shared room, every page with a pulse box counts the same beat.\"\n :see [\"share\" \"shared\" \"metro\" \"player\"]}\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value]}]\n (cond\n (and (= inlet :audio) (number? (:count value))) {:out [[0 (:count value)]] :show {:count (:count value)}}\n (and (= inlet :audio) (contains? value :bpm)) {:out [[1 (or (:bpm value) 0)]] :show {:bpm (:bpm value)}}\n (= inlet :task) {}\n (#{:stop} value) {:effects [[:tempo [:stop]]]}\n (#{:start} value) {:effects [[:tempo [:start 120]]]}\n (and (sequential? value) (#{:start} (first value))) {:effects [[:tempo [:start (or (api/num-of (rest value)) 120)]]]}\n (and (sequential? value) (#{:bpm} (first value)) (api/num-of (rest value))) {:effects [[:tempo [:bpm (api/num-of (rest value))]]]}\n :else {}))\n :tasks {:tempo (fn [[op x] _]\n (if-let [r (room)]\n (do (case op :start ((.-start r) x) :bpm ((.-bpm r) x) :stop ((.-stop r))) (js/Promise.resolve true))\n (js/Promise.reject (js/Error. \"pulse: this page has no room layer\"))))}\n\n ;; ── view ──\n :derive (fn [{:keys [shown]}] {:label (cond (number? (:count shown)) (str \"pulse \" (:count shown))\n (:bpm shown) (str \"pulse · \" (api/nice (:bpm shown)) \" bpm\")\n :else \"pulse\")\n :dot (if (number? (:count shown)) :kairos :grid)})\n :view [[:rect {:x 1 :y 1 :w 94 :h 28 :r 3 :stroke :kairos :fill :bg}]\n [:circle {:cx 84 :cy 15 :r 5 :stroke :$dot :fill :$dot}]\n [:text {:x 8 :y 19 :size 11} :$label]]\n\n ;; ── outside: the room's tempo, read every 25 ms, each pulse reported 100 ms ahead on the audio clock ──\n :audio {:create (fn [ac b _]\n (let [div (let [n (js/parseFloat (second (.split (.trim (str (:text b))) #\"\\s+\")))] (if (and (js/isFinite n) (pos? n)) n 1))\n node {:alive (atom true) :until (atom nil) :tempo (atom :none) :inlets [] :outlets []}\n tick (fn []\n (when-let [r (room)]\n (let [t ((.-tempo r)) t (when t (js->clj t :keywordize-keys true)) n ((.-now r))]\n (when (not= t @(:tempo node))\n (reset! (:tempo node) t) (reset! (:until node) nil)\n (api/report! (:id b) {:bpm (:bpm t)} nil))\n (when-let [{:keys [t0 bpm]} t]\n (let [per (/ 60000 (* bpm div))\n from (max n (or @(:until node) n)) to (+ n 100)]\n (doseq [k (range (js/Math.ceil (/ (- from t0) per)) (inc (js/Math.floor (/ (- to t0) per))))\n :let [at (+ t0 (* k per))] :when (and (>= k 0) (>= at from) (< at to))]\n (api/report! (:id b) {:count k} (+ (.-currentTime ac) (/ (- at n) 1000))))\n (reset! (:until node) to))))))\n timer (js/setInterval (fn [] (when @(:alive node) (tick))) 25)]\n (assoc node :timer timer)))\n :dispose (fn [node] (reset! (:alive node) false) (js/clearInterval (:timer node)))}})\n"
|
|
9
|
+
};
|
package/dist/share.mjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Generated by packages/build.mjs from src/share.cljs
|
|
2
|
+
export default {
|
|
3
|
+
"class": "share",
|
|
4
|
+
"version": "0.1.4",
|
|
5
|
+
"author": "bonuoq",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"doc": "Shares what arrives with everyone in the room, on a channel (main if none): every shared box of the room listening to that channel sends it out — on every page, this one too. Any value: a number, a list, an event from midiin or motion, a map. It goes on the room's reliable channel, in order, with or without ▶. Outside a shared room it reaches the shared boxes of this page only. It is a message of the moment, not a gesture: the history does not keep it.",
|
|
8
|
+
"source": ";; share · a value to everyone in the room, on a channel\n;; Part of @kairotic/net. Written against the Kairotic API (api/…). Discussion #135.\n(let [room (fn [] (.-kairoticRoom js/window))]\n {;; ── about ──\n :class \"share\"\n :license \"MPL-2.0\"\n :doc \"Shares what arrives with everyone in the room, on a channel (main if none): every shared box of the room listening to that channel sends it out — on every page, this one too. Any value: a number, a list, an event from midiin or motion, a map. It goes on the room's reliable channel, in order, with or without ▶. Outside a shared room it reaches the shared boxes of this page only. It is a message of the moment, not a gesture: the history does not keep it.\"\n :usage \"share [channel]\"\n :io [1 0]\n :size [96 30]\n :args [[:channel nil]]\n :help {:inlets [\"any value: it goes to everyone in the room\"]\n :outlets []\n :example \"(box :b1 \\\"msg 60 64 67\\\" 40 30)\\n(box :b2 \\\"share chords\\\" 40 80)\\n(box :b3 \\\"shared chords\\\" 200 30)\\n(box :b4 \\\"print\\\" 200 80)\\n(connect :b1 0 :b2 0)\\n(connect :b3 0 :b4 0)\"\n :try \"Press ▶ and click the message: print shows it, and so does every page of the room with a shared chords box.\"\n :see [\"shared\" \"pulse\" \"oscout\"]}\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value args]}]\n (if (= inlet :task)\n {}\n {:effects [[:share [(if (:channel args) (name (:channel args)) \"main\") value]]]\n :show {:label (let [s (pr-str value)] (if (> (count s) 14) (str (subs s 0 13) \"…\") s))}}))\n :tasks {:share (fn [[ch v] _]\n (if-let [r (room)]\n (do ((.-share r) ch v) (js/Promise.resolve true))\n (js/Promise.reject (js/Error. \"share: this page has no room layer\"))))}\n\n ;; ── view ──\n :derive (fn [{:keys [shown args]}] {:label (or (:label shown) (str \"share \" (if (:channel args) (name (:channel args)) \"main\")))})\n :view [[:rect {:x 1 :y 1 :w 94 :h 28 :r 3 :stroke :kairos :fill :bg}]\n [:text {:x 8 :y 19 :size 11} :$label]]})\n"
|
|
9
|
+
};
|
package/dist/shared.mjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Generated by packages/build.mjs from src/shared.cljs
|
|
2
|
+
export default {
|
|
3
|
+
"class": "shared",
|
|
4
|
+
"version": "0.1.4",
|
|
5
|
+
"author": "bonuoq",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"doc": "What anyone in the room shares on a channel (main if none) with a share box — this page included —, while ▶ runs: the value on the left, the name of who shared it on the right. A midiin on one page and a shared box feeding synth~ on another is playing someone else's instrument; an event map from motion on a phone moves a knob on the computer.",
|
|
8
|
+
"source": ";; shared · what is shared in the room on a channel\n;; Part of @kairotic/net. Written against the Kairotic API (api/…). Discussion #135.\n(let [room (fn [] (.-kairoticRoom js/window))]\n {;; ── about ──\n :class \"shared\"\n :license \"MPL-2.0\"\n :doc \"What anyone in the room shares on a channel (main if none) with a share box — this page included —, while ▶ runs: the value on the left, the name of who shared it on the right. A midiin on one page and a shared box feeding synth~ on another is playing someone else's instrument; an event map from motion on a phone moves a knob on the computer.\"\n :usage \"shared [channel]\"\n :io [0 2]\n :size [96 30]\n :args [[:channel nil]]\n :help {:inlets []\n :outlets [\"the value shared\" \"who shared it\"]\n :example \"(box :b1 \\\"shared notes\\\" 40 30)\\n(box :b2 \\\"synth~\\\" 40 80)\\n(box :b3 \\\"dac~\\\" 40 130)\\n(connect :b1 0 :b2 0)\\n(connect :b2 0 :b3 0)\\n(connect :b2 0 :b3 1)\"\n :try \"In a shared room, press ▶: what someone sends to a share notes box, you hear.\"\n :see [\"share\" \"pulse\"]}\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value]}]\n (cond\n (and (= inlet :audio) (contains? value :v))\n {:out [[1 (str (:from value))] [0 (:v value)]] :show {:label (str (:from value) \" → \" (let [s (pr-str (:v value))] (if (> (count s) 10) (str (subs s 0 9) \"…\") s)))}}\n (and (= inlet :audio) (:error value)) {:effects [[:print (:error value)]]}\n :else {}))\n\n ;; ── view ──\n :derive (fn [{:keys [shown args]}] {:label (or (:label shown) (str \"shared \" (if (:channel args) (name (:channel args)) \"main\")))})\n :view [[:rect {:x 1 :y 1 :w 94 :h 28 :r 3 :stroke :kairos :fill :bg}]\n [:text {:x 8 :y 19 :size 11} :$label]]\n\n ;; ── outside: the room's channel, while ▶ runs ──\n :audio {:create (fn [_ b _]\n (let [ch (or (second (.split (.trim (str (:text b))) #\"\\s+\")) \"main\")\n node {:alive (atom true) :inlets [] :outlets []}]\n (if-let [r (room)]\n (assoc node :off ((.-onShare r) ch (fn [v from] (when @(:alive node) (api/report! (:id b) {:v v :from from} nil)))))\n (do (api/report! (:id b) {:error \"shared: this page has no room layer\"} nil) (assoc node :off (fn [] nil))))))\n :dispose (fn [node] (reset! (:alive node) false) ((:off node)))}})\n"
|
|
9
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kairotic/net",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.4",
|
|
4
|
+
"description": "Kairotic in a shared room: share values on channels with everyone in it, and one tempo for the whole room (the same beat on every page)",
|
|
5
|
+
"author": "bonuoq",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/bonuoq/kairotic.git",
|
|
10
|
+
"directory": "packages/net"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/bonuoq/kairotic/tree/main/packages#readme",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "dist/index.mjs",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"kairotic": {
|
|
24
|
+
"api": 1
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "node ../build.mjs net",
|
|
28
|
+
"prepublishOnly": "node ../build.mjs net"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"kairotic",
|
|
32
|
+
"music",
|
|
33
|
+
"network",
|
|
34
|
+
"webrtc",
|
|
35
|
+
"tempo",
|
|
36
|
+
"jam",
|
|
37
|
+
"patcher"
|
|
38
|
+
]
|
|
39
|
+
}
|
package/src/pulse.cljs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
;; pulse · the room's beat: one tempo for everyone in the room, the same beat on every page
|
|
2
|
+
;; Part of @kairotic/net. Written against the Kairotic API (api/…). Discussion #135.
|
|
3
|
+
(let [room (fn [] (.-kairoticRoom js/window))]
|
|
4
|
+
{;; ── about ──
|
|
5
|
+
:class "pulse"
|
|
6
|
+
:license "MPL-2.0"
|
|
7
|
+
:doc "The room's beat, while ▶ runs: the count of each pulse (0 at the start of the tempo) on the left, on the audio clock, divided (1, beats; 2, eighths; 0.25, bars of four); on the right, the bpm when the tempo starts or changes, and 0 when it stops. The tempo is one for the whole room — the same beat on every page, from the room's clock, whatever each computer's clock says —; start [bpm], stop and bpm n (which keeps the beat where it is) change it for everyone, as the Room tab does. Outside a room it is this page's tempo."
|
|
8
|
+
:usage "pulse [division]"
|
|
9
|
+
:io [1 2]
|
|
10
|
+
:size [96 30]
|
|
11
|
+
:args [[:division 1]]
|
|
12
|
+
:help {:inlets [":start [bpm] · :stop · :bpm n: the tempo of the whole room"]
|
|
13
|
+
:outlets ["the count of each pulse, on its beat" "the bpm (0 when it stops)"]
|
|
14
|
+
:example "(box :b1 \"msg :start 100\" 40 30)\n(box :b2 \"msg :stop\" 140 30)\n(box :b3 \"pulse\" 40 80)\n(box :b4 \"print\" 40 130)\n(connect :b1 0 :b3 0)\n(connect :b2 0 :b3 0)\n(connect :b3 0 :b4 0)"
|
|
15
|
+
:try "Press ▶ and click start 100: a count every beat, a second later. In a shared room, every page with a pulse box counts the same beat."
|
|
16
|
+
:see ["share" "shared" "metro" "player"]}
|
|
17
|
+
|
|
18
|
+
;; ── logic ──
|
|
19
|
+
:receive (fn [{:keys [inlet value]}]
|
|
20
|
+
(cond
|
|
21
|
+
(and (= inlet :audio) (number? (:count value))) {:out [[0 (:count value)]] :show {:count (:count value)}}
|
|
22
|
+
(and (= inlet :audio) (contains? value :bpm)) {:out [[1 (or (:bpm value) 0)]] :show {:bpm (:bpm value)}}
|
|
23
|
+
(= inlet :task) {}
|
|
24
|
+
(#{:stop} value) {:effects [[:tempo [:stop]]]}
|
|
25
|
+
(#{:start} value) {:effects [[:tempo [:start 120]]]}
|
|
26
|
+
(and (sequential? value) (#{:start} (first value))) {:effects [[:tempo [:start (or (api/num-of (rest value)) 120)]]]}
|
|
27
|
+
(and (sequential? value) (#{:bpm} (first value)) (api/num-of (rest value))) {:effects [[:tempo [:bpm (api/num-of (rest value))]]]}
|
|
28
|
+
:else {}))
|
|
29
|
+
:tasks {:tempo (fn [[op x] _]
|
|
30
|
+
(if-let [r (room)]
|
|
31
|
+
(do (case op :start ((.-start r) x) :bpm ((.-bpm r) x) :stop ((.-stop r))) (js/Promise.resolve true))
|
|
32
|
+
(js/Promise.reject (js/Error. "pulse: this page has no room layer"))))}
|
|
33
|
+
|
|
34
|
+
;; ── view ──
|
|
35
|
+
:derive (fn [{:keys [shown]}] {:label (cond (number? (:count shown)) (str "pulse " (:count shown))
|
|
36
|
+
(:bpm shown) (str "pulse · " (api/nice (:bpm shown)) " bpm")
|
|
37
|
+
:else "pulse")
|
|
38
|
+
:dot (if (number? (:count shown)) :kairos :grid)})
|
|
39
|
+
:view [[:rect {:x 1 :y 1 :w 94 :h 28 :r 3 :stroke :kairos :fill :bg}]
|
|
40
|
+
[:circle {:cx 84 :cy 15 :r 5 :stroke :$dot :fill :$dot}]
|
|
41
|
+
[:text {:x 8 :y 19 :size 11} :$label]]
|
|
42
|
+
|
|
43
|
+
;; ── outside: the room's tempo, read every 25 ms, each pulse reported 100 ms ahead on the audio clock ──
|
|
44
|
+
:audio {:create (fn [ac b _]
|
|
45
|
+
(let [div (let [n (js/parseFloat (second (.split (.trim (str (:text b))) #"\s+")))] (if (and (js/isFinite n) (pos? n)) n 1))
|
|
46
|
+
node {:alive (atom true) :until (atom nil) :tempo (atom :none) :inlets [] :outlets []}
|
|
47
|
+
tick (fn []
|
|
48
|
+
(when-let [r (room)]
|
|
49
|
+
(let [t ((.-tempo r)) t (when t (js->clj t :keywordize-keys true)) n ((.-now r))]
|
|
50
|
+
(when (not= t @(:tempo node))
|
|
51
|
+
(reset! (:tempo node) t) (reset! (:until node) nil)
|
|
52
|
+
(api/report! (:id b) {:bpm (:bpm t)} nil))
|
|
53
|
+
(when-let [{:keys [t0 bpm]} t]
|
|
54
|
+
(let [per (/ 60000 (* bpm div))
|
|
55
|
+
from (max n (or @(:until node) n)) to (+ n 100)]
|
|
56
|
+
(doseq [k (range (js/Math.ceil (/ (- from t0) per)) (inc (js/Math.floor (/ (- to t0) per))))
|
|
57
|
+
:let [at (+ t0 (* k per))] :when (and (>= k 0) (>= at from) (< at to))]
|
|
58
|
+
(api/report! (:id b) {:count k} (+ (.-currentTime ac) (/ (- at n) 1000))))
|
|
59
|
+
(reset! (:until node) to))))))
|
|
60
|
+
timer (js/setInterval (fn [] (when @(:alive node) (tick))) 25)]
|
|
61
|
+
(assoc node :timer timer)))
|
|
62
|
+
:dispose (fn [node] (reset! (:alive node) false) (js/clearInterval (:timer node)))}})
|
package/src/share.cljs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
;; share · a value to everyone in the room, on a channel
|
|
2
|
+
;; Part of @kairotic/net. Written against the Kairotic API (api/…). Discussion #135.
|
|
3
|
+
(let [room (fn [] (.-kairoticRoom js/window))]
|
|
4
|
+
{;; ── about ──
|
|
5
|
+
:class "share"
|
|
6
|
+
:license "MPL-2.0"
|
|
7
|
+
:doc "Shares what arrives with everyone in the room, on a channel (main if none): every shared box of the room listening to that channel sends it out — on every page, this one too. Any value: a number, a list, an event from midiin or motion, a map. It goes on the room's reliable channel, in order, with or without ▶. Outside a shared room it reaches the shared boxes of this page only. It is a message of the moment, not a gesture: the history does not keep it."
|
|
8
|
+
:usage "share [channel]"
|
|
9
|
+
:io [1 0]
|
|
10
|
+
:size [96 30]
|
|
11
|
+
:args [[:channel nil]]
|
|
12
|
+
:help {:inlets ["any value: it goes to everyone in the room"]
|
|
13
|
+
:outlets []
|
|
14
|
+
:example "(box :b1 \"msg 60 64 67\" 40 30)\n(box :b2 \"share chords\" 40 80)\n(box :b3 \"shared chords\" 200 30)\n(box :b4 \"print\" 200 80)\n(connect :b1 0 :b2 0)\n(connect :b3 0 :b4 0)"
|
|
15
|
+
:try "Press ▶ and click the message: print shows it, and so does every page of the room with a shared chords box."
|
|
16
|
+
:see ["shared" "pulse" "oscout"]}
|
|
17
|
+
|
|
18
|
+
;; ── logic ──
|
|
19
|
+
:receive (fn [{:keys [inlet value args]}]
|
|
20
|
+
(if (= inlet :task)
|
|
21
|
+
{}
|
|
22
|
+
{:effects [[:share [(if (:channel args) (name (:channel args)) "main") value]]]
|
|
23
|
+
:show {:label (let [s (pr-str value)] (if (> (count s) 14) (str (subs s 0 13) "…") s))}}))
|
|
24
|
+
:tasks {:share (fn [[ch v] _]
|
|
25
|
+
(if-let [r (room)]
|
|
26
|
+
(do ((.-share r) ch v) (js/Promise.resolve true))
|
|
27
|
+
(js/Promise.reject (js/Error. "share: this page has no room layer"))))}
|
|
28
|
+
|
|
29
|
+
;; ── view ──
|
|
30
|
+
:derive (fn [{:keys [shown args]}] {:label (or (:label shown) (str "share " (if (:channel args) (name (:channel args)) "main")))})
|
|
31
|
+
:view [[:rect {:x 1 :y 1 :w 94 :h 28 :r 3 :stroke :kairos :fill :bg}]
|
|
32
|
+
[:text {:x 8 :y 19 :size 11} :$label]]})
|
package/src/shared.cljs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
;; shared · what is shared in the room on a channel
|
|
2
|
+
;; Part of @kairotic/net. Written against the Kairotic API (api/…). Discussion #135.
|
|
3
|
+
(let [room (fn [] (.-kairoticRoom js/window))]
|
|
4
|
+
{;; ── about ──
|
|
5
|
+
:class "shared"
|
|
6
|
+
:license "MPL-2.0"
|
|
7
|
+
:doc "What anyone in the room shares on a channel (main if none) with a share box — this page included —, while ▶ runs: the value on the left, the name of who shared it on the right. A midiin on one page and a shared box feeding synth~ on another is playing someone else's instrument; an event map from motion on a phone moves a knob on the computer."
|
|
8
|
+
:usage "shared [channel]"
|
|
9
|
+
:io [0 2]
|
|
10
|
+
:size [96 30]
|
|
11
|
+
:args [[:channel nil]]
|
|
12
|
+
:help {:inlets []
|
|
13
|
+
:outlets ["the value shared" "who shared it"]
|
|
14
|
+
:example "(box :b1 \"shared notes\" 40 30)\n(box :b2 \"synth~\" 40 80)\n(box :b3 \"dac~\" 40 130)\n(connect :b1 0 :b2 0)\n(connect :b2 0 :b3 0)\n(connect :b2 0 :b3 1)"
|
|
15
|
+
:try "In a shared room, press ▶: what someone sends to a share notes box, you hear."
|
|
16
|
+
:see ["share" "pulse"]}
|
|
17
|
+
|
|
18
|
+
;; ── logic ──
|
|
19
|
+
:receive (fn [{:keys [inlet value]}]
|
|
20
|
+
(cond
|
|
21
|
+
(and (= inlet :audio) (contains? value :v))
|
|
22
|
+
{:out [[1 (str (:from value))] [0 (:v value)]] :show {:label (str (:from value) " → " (let [s (pr-str (:v value))] (if (> (count s) 10) (str (subs s 0 9) "…") s)))}}
|
|
23
|
+
(and (= inlet :audio) (:error value)) {:effects [[:print (:error value)]]}
|
|
24
|
+
:else {}))
|
|
25
|
+
|
|
26
|
+
;; ── view ──
|
|
27
|
+
:derive (fn [{:keys [shown args]}] {:label (or (:label shown) (str "shared " (if (:channel args) (name (:channel args)) "main")))})
|
|
28
|
+
:view [[:rect {:x 1 :y 1 :w 94 :h 28 :r 3 :stroke :kairos :fill :bg}]
|
|
29
|
+
[:text {:x 8 :y 19 :size 11} :$label]]
|
|
30
|
+
|
|
31
|
+
;; ── outside: the room's channel, while ▶ runs ──
|
|
32
|
+
:audio {:create (fn [_ b _]
|
|
33
|
+
(let [ch (or (second (.split (.trim (str (:text b))) #"\s+")) "main")
|
|
34
|
+
node {:alive (atom true) :inlets [] :outlets []}]
|
|
35
|
+
(if-let [r (room)]
|
|
36
|
+
(assoc node :off ((.-onShare r) ch (fn [v from] (when @(:alive node) (api/report! (:id b) {:v v :from from} nil)))))
|
|
37
|
+
(do (api/report! (:id b) {:error "shared: this page has no room layer"} nil) (assoc node :off (fn [] nil))))))
|
|
38
|
+
:dispose (fn [node] (reset! (:alive node) false) ((:off node)))}})
|