@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.
- package/README.md +9 -2
- package/index.html +1101 -0
- package/kairotic/api.cljs +362 -0
- package/kairotic/assistant.cljs +505 -0
- package/kairotic/backends/gun-backend.js +371 -0
- package/kairotic/canvas.cljs +751 -0
- package/kairotic/code.cljs +108 -0
- package/kairotic/components.cljs +838 -0
- package/kairotic/core.cljs +96 -0
- package/kairotic/corefns.cljs +473 -0
- package/kairotic/docs.mjs +2 -0
- package/kairotic/engine.cljs +482 -0
- package/kairotic/examples.mjs +96 -0
- package/kairotic/faust.cljs +225 -0
- package/kairotic/help.cljs +1036 -0
- package/kairotic/hiccup.cljs +566 -0
- package/kairotic/input.cljs +271 -0
- package/kairotic/kfile.mjs +138 -0
- package/kairotic/library.cljs +214 -0
- package/kairotic/links.cljs +41 -0
- package/kairotic/mt.cljs +1604 -0
- package/kairotic/mtboxes.cljs +276 -0
- package/kairotic/packages/analysis.mjs +64 -0
- package/kairotic/packages/controls.mjs +28 -0
- package/kairotic/packages/data.mjs +22 -0
- package/kairotic/packages/faust-units/cmaes.mjs +82 -0
- package/kairotic/packages/faust-units/graph.mjs +212 -0
- package/kairotic/packages/faust-units/listen.mjs +125 -0
- package/kairotic/packages/faust-units/notation.mjs +139 -0
- package/kairotic/packages/faust-units/parse.mjs +439 -0
- package/kairotic/packages/faust-units/recipe.mjs +195 -0
- package/kairotic/packages/faust-units/render.mjs +69 -0
- package/kairotic/packages/faust-units/units.mjs +130 -0
- package/kairotic/packages/faust-units/view.mjs +353 -0
- package/kairotic/packages/faust-units.mjs +9 -0
- package/kairotic/packages/io.mjs +58 -0
- package/kairotic/packages/net.mjs +28 -0
- package/kairotic/packages/patterns.mjs +34 -0
- package/kairotic/packages/spatial.mjs +22 -0
- package/kairotic/packages/spectral/shift.mjs +169 -0
- package/kairotic/packages/spectral.mjs +16 -0
- package/kairotic/packages/synth.mjs +28 -0
- package/kairotic/packages/theory/counterpoint.mjs +798 -0
- package/kairotic/packages/theory/mirror.mjs +221 -0
- package/kairotic/packages/theory.mjs +46 -0
- package/kairotic/patterns.cljs +538 -0
- package/kairotic/presets.cljs +227 -0
- package/kairotic/quantize.cljs +782 -0
- package/kairotic/record.cljs +169 -0
- package/kairotic/release.cljs +90 -0
- package/kairotic/ring.cljs +129 -0
- package/kairotic/ringaudio.cljs +232 -0
- package/kairotic/ringcore.mjs +270 -0
- package/kairotic/score.cljs +176 -0
- package/kairotic/shapes.cljs +283 -0
- package/kairotic/signal.cljs +327 -0
- package/kairotic/sounds.cljs +565 -0
- package/kairotic/sounds.mjs +108 -0
- package/kairotic/span.cljs +129 -0
- package/kairotic/spell.cljs +281 -0
- package/kairotic/structure.cljs +145 -0
- package/kairotic/talk.cljs +115 -0
- package/kairotic/tempo.cljs +226 -0
- package/kairotic/treecanvas.cljs +299 -0
- package/kairotic/tutorials.mjs +1283 -0
- package/kairotic/vendor/codemirror.mjs +23 -0
- package/package.json +23 -4
- package/page/00-work.cljs +862 -0
- package/page/01-commands.cljs +1219 -0
- package/page/02-patch-view.cljs +1051 -0
- package/page/03-history-view.cljs +481 -0
- package/page/04-panels.cljs +448 -0
- package/page/05-gestures.cljs +1247 -0
- package/page/06-connection.cljs +219 -0
- package/page/07-claude.cljs +430 -0
- package/page/08-sessions.cljs +423 -0
- package/page/09-learn.cljs +602 -0
- package/page/10-embed.cljs +469 -0
- package/page/11-startup.cljs +61 -0
- package/page/12-feedback.cljs +172 -0
- package/page/13-perform.cljs +325 -0
- package/page/14-editor.cljs +656 -0
- package/page/15-presets.cljs +230 -0
- package/page/16-release.cljs +242 -0
- package/page/17-sounds.cljs +147 -0
- package/page/18-lines.cljs +594 -0
- package/page/19-files.cljs +247 -0
- package/page/19-html.cljs +152 -0
- package/page/20-talk.cljs +527 -0
- package/page/21-net.cljs +110 -0
- package/page/22-learn.cljs +303 -0
- package/page/23-ids.cljs +233 -0
- package/page/24-clipboard.cljs +136 -0
- package/page/25-tidy.cljs +74 -0
- package/page/26-probes.cljs +111 -0
- package/sources.mjs +2 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Generated by packages/build.mjs
|
|
2
|
+
export default {
|
|
3
|
+
"package": "@kairotic/data",
|
|
4
|
+
"version": "0.1.4",
|
|
5
|
+
"api": 1,
|
|
6
|
+
"author": "bonuoq",
|
|
7
|
+
"license": "MPL-2.0",
|
|
8
|
+
"components": [
|
|
9
|
+
{
|
|
10
|
+
"class": "edn",
|
|
11
|
+
"license": "MPL-2.0",
|
|
12
|
+
"doc": "Shows any value as a tree, like Portal: maps, vectors, sets and lists open and close (▸ ▾), each value coloured by its kind, a closed collection previewed on its line. Picking a line sends that part of the value (left) and its path (right): the path a fetch box takes as [:get …]. Dragging scrolls (or the bar on the right). :depth n opens it n levels down; bang sends the picked part again (the whole value if none); :clear empties it.",
|
|
13
|
+
"source": ";; edn · any value as a tree to open, read and pick from\n;; Part of @kairotic/data. Written against the Kairotic API (api/…).\n;; After Portal (djblue/portal): what arrives is shown as a tree whose collections open and close, each\n;; value coloured by its kind, a closed collection previewed on its line. Picking a line sends that part\n;; of the value (left) and its path (right), the path a fetch box takes as [:get …]. Dragging scrolls.\n;; What happens in the view (open, pick, scroll) is a message to the box itself, like any gesture, so\n;; the same value sent again changes nothing.\n(let [row-h 14 top-y 22 foot-h 16 cw 5.6 ; a line, where lines start, the footer, a character (size 9)\n max-children 200 max-rows 3000\n coll-kind (fn [v] (cond (map? v) :map (vector? v) :vector (set? v) :set (sequential? v) :list :else nil))\n opener {:map \"{\" :vector \"[\" :set \"#{\" :list \"(\"}\n plural (fn [n one many] (str n \" \" (if (= 1 n) one many)))\n size-text (fn [v] (case (coll-kind v)\n :map (plural (count v) \"key\" \"keys\")\n (:vector :list) (plural (count v) \"item\" \"items\")\n :set (plural (count v) \"element\" \"elements\")\n nil))\n ;; the colour of a value, by its kind (the palette of the patch, light or dark)\n ink-of (fn [v] (cond (string? v) :ink (number? v) :kairos (keyword? v) :accent\n (or (nil? v) (boolean? v)) :soft (coll-kind v) :soft :else :ink))\n ;; one level down: the parts of a collection, in a steady order, at most max-children of them\n entries (fn [c]\n (let [es (case (coll-kind c)\n :map (for [[k v] (sort-by (fn [[k _]] (pr-str k)) c)] {:k k :label (pr-str k) :key? true :v v})\n :set (for [v (sort-by pr-str c)] {:k v :label \"\" :v v})\n (map-indexed (fn [i v] {:k i :label (str i) :v v}) c))\n shown (vec (take max-children es))]\n (if (> (count c) max-children)\n (conj shown {:more (- (count c) max-children)})\n shown)))\n ;; a part of the value by its path: get-in, also through lists and sets\n part (fn [root path]\n (reduce (fn [c k] (case (coll-kind c)\n (:map :set) (get c k)\n (:vector :list) (if (number? k) (nth (seq c) k nil) nil)\n nil))\n root path))\n ;; the lines of the tree as it is open: [{:path :depth :label :key? :v :open? :more}]\n rows-of (fn [{:keys [root open]}]\n (if-not (coll-kind root)\n []\n (loop [todo (into () (reverse (map (fn [e] (assoc e :path [(:k e)] :depth 0)) (entries root))))\n out (transient [])]\n (if (or (empty? todo) (>= (count out) max-rows))\n (persistent! out)\n (let [r (first todo) more (rest todo)\n open? (and (coll-kind (:v r)) (contains? open (:path r)) (not (:more r)))\n kids (when open? (map (fn [e] (assoc e :path (conj (:path r) (:k e)) :depth (inc (:depth r))))\n (entries (:v r))))]\n (recur (if open? (into more (reverse kids)) more)\n (conj! out (assoc r :open? open?))))))))\n visible (fn [h] (max 1 (js/Math.floor (/ (- h top-y foot-h) row-h))))\n size-of (fn [args] (api/padded (filter number? args) [320 300]))\n ;; every collection down to depth n, open (at most max-rows of them)\n open-to (fn [root n]\n (loop [todo (list [[] root 0]) acc #{}]\n (if (or (empty? todo) (> (count acc) max-rows))\n acc\n (let [[p v d] (first todo)\n kids (when (and (coll-kind v) (< d n))\n (for [e (entries v) :when (and (not (:more e)) (coll-kind (:v e)))] [(conj p (:k e)) (:v e) (inc d)]))]\n (recur (into (rest todo) kids) (if (and (seq p) (coll-kind v) (<= d n)) (conj acc p) acc))))))\n shown-of (fn [st] {:top (:top st 0) :sel (:sel st) :open (count (:open st))})\n with-top (fn [st top h] (let [n (count (rows-of st))] (assoc st :top (max 0 (min (js/Math.round top) (- n (visible h)))))))]\n {;; ── about ──\n :class \"edn\"\n :license \"MPL-2.0\"\n :doc \"Shows any value as a tree, like Portal: maps, vectors, sets and lists open and close (▸ ▾), each value coloured by its kind, a closed collection previewed on its line. Picking a line sends that part of the value (left) and its path (right): the path a fetch box takes as [:get …]. Dragging scrolls (or the bar on the right). :depth n opens it n levels down; bang sends the picked part again (the whole value if none); :clear empties it.\"\n :usage \"edn [width height]\"\n :io [1 2]\n :size (fn [box] (size-of (:args box)))\n\n ;; ── logic ──\n :receive (fn [{:keys [value state args]}]\n (let [st (or state {:open #{} :top 0})\n [_ h] (size-of args)\n msg? (fn [k] (and (vector? value) (= k (first value))))\n view (fn [st2] {:state st2 :show (shown-of st2)})]\n (cond\n ;; what happens in the view\n (msg? :edn-open)\n (let [[_ p on] value]\n (view (with-top (update st :open (if on conj disj) p) (:top st 0) h)))\n (msg? :edn-top) (view (with-top st (second value) h))\n (msg? :edn-pick)\n (let [p (second value)]\n (if (or (= p (:sel st)) (not (contains? st :root)))\n {:state st}\n {:state (assoc st :sel p) :show (shown-of (assoc st :sel p))\n :out [[1 p] [0 (part (:root st) p)]]}))\n ;; messages\n (and (msg? :depth) (number? (second value)))\n (view (with-top (assoc st :open (open-to (:root st) (max 0 (dec (second value))))) 0 h))\n (#{:bang} value)\n (if (contains? st :root)\n (let [p (or (:sel st) [])] {:out [[1 p] [0 (part (:root st) p)]]})\n {})\n (#{:clear} value) (view {:open #{} :top 0})\n ;; a value: the same again changes nothing; another keeps what was open and picked\n (and (contains? st :root) (= value (:root st))) {:state st}\n :else\n (let [st2 (assoc st :root value)]\n (view (with-top st2 (:top st 0) h))))))\n\n ;; a line pressed (on its arrow: open or close it; elsewhere: pick it), or a drag that scrolls\n :gesture (fn [{:keys [up hit dy start state h]}]\n (let [st (or state {}) rows (rows-of st) n (count rows) vis (visible h)\n t0 (:top start (:top st 0))]\n (cond\n (>= (js/Math.abs dy) 3)\n [:edn-top (if (= hit :bar)\n (+ t0 (* dy (/ n (* vis row-h))))\n (- t0 (/ dy row-h)))]\n (not up) nil\n ;; a line is its index; its arrow, -1 - its index\n (and (number? hit) (neg? hit))\n (when-let [r (get rows (- -1 hit))] (when (coll-kind (:v r)) [:edn-open (:path r) (not (:open? r))]))\n (number? hit) (when-let [r (get rows hit)] (when-not (:more r) [:edn-pick (:path r)]))\n :else nil)))\n\n ;; ── view ──\n :draw (fn [{:keys [state w h]}]\n (let [st (or state {}) root (:root st) rows (rows-of st) n (count rows)\n vis (visible h) top (max 0 (min (:top st 0) (- n vis))) bar? (> n vis)\n right (- w (if bar? 14 6))\n chars (fn [x] (max 0 (js/Math.floor (/ (- right x) cw))))\n cut (fn [s x] (let [m (chars x)] (if (> (count s) m) (str (subs s 0 (max 0 (dec m))) \"…\") s)))\n line (fn [j r i]\n (let [y (+ top-y (* j row-h)) base (+ y 10)\n x (+ 8 (* 12 (:depth r))) v (:v r) kind (coll-kind v)\n lbl (:label r) vx (+ x 12 (if (seq lbl) (* cw (inc (count lbl))) 0))\n sel? (= (:sel st) (:path r))]\n (if (:more r)\n [[:text {:x (+ x 12) :y base :size 9 :fill :soft} (str \"… \" (:more r) \" more\")]]\n (concat\n [[:rect {:x 2 :y y :w (- right 2) :h row-h :fill (if sel? :accent :bg) :opacity (if sel? 0.14 0) :hit i}]]\n (when kind\n [[:rect {:x (- x 2) :y y :w 12 :h row-h :fill :bg :opacity 0 :hit (- -1 i)}]\n [:text {:x x :y base :size 9 :fill :soft} (if (:open? r) \"▾\" \"▸\")]])\n (when (seq lbl)\n [[:text {:x (+ x 12) :y base :size 9 :fill (if (:key? r) :accent :soft)} (cut lbl (+ x 12))]])\n [[:text {:x vx :y base :size 9 :fill (ink-of v)}\n (cut (cond (and kind (:open? r)) (str (opener kind) \" \" (size-text v))\n kind (pr-str v)\n :else (pr-str v))\n vx)]]))))]\n (concat\n [[:rect {:x 1 :y 1 :w (- w 2) :h (- h 2) :r 4 :stroke :grid :fill :bg}]\n [:text {:x 8 :y 15 :size 9 :fill :soft}\n (cond (not (contains? st :root)) \"edn · send it a value\"\n (coll-kind root) (str (name (coll-kind root)) \" · \" (size-text root))\n :else (str (cond (string? root) \"string\" (number? root) \"number\" (keyword? root) \"keyword\"\n (nil? root) \"nil\" (boolean? root) \"boolean\" :else \"value\")))]\n [:line {:x1 1 :y1 19 :x2 (- w 1) :y2 19 :stroke :grid}]]\n ;; a value that is not a collection: itself, on one line\n (when (and (contains? st :root) (not (coll-kind root)))\n [[:text {:x 8 :y (+ top-y 10) :size 9 :fill (ink-of root)} (cut (pr-str root) 8)]])\n (mapcat (fn [j] (line j (nth rows (+ top j)) (+ top j))) (range (min vis (- n top))))\n (when bar?\n (let [track (* vis row-h) th (max 14 (* track (/ vis n))) ty (+ top-y (* (- track th) (/ top (max 1 (- n vis)))))]\n [[:rect {:x (- w 10) :y top-y :w 6 :h track :r 3 :fill :grid :hit :bar}]\n [:rect {:x (- w 10) :y ty :w 6 :h th :r 3 :fill :soft :hit :bar}]]))\n [[:line {:x1 1 :y1 (- h foot-h) :x2 (- w 1) :y2 (- h foot-h) :stroke :grid}]\n [:text {:x 8 :y (- h 5) :size 8 :fill :soft}\n (cut (cond (:sel st) (str \"→ \" (pr-str (:sel st)))\n (seq rows) (str n (if (= 1 n) \" line\" \" lines\") (when bar? (str \" · \" (inc top) \"–\" (min n (+ top vis)))) \" · pick a line\")\n :else \"\")\n 8)]])))\n\n ;; ── help ──\n :help {:inlets [\"any value: a map, a vector, a set, a list, or a single value; :depth n · bang · :clear\"]\n :outlets [\"the part picked\" \"its path, for [:get …] of a fetch box\"]\n :messages [[\":depth 2\" \"opens it two levels down\"]\n [\"bang\" \"sends the part picked again (the whole value if none)\"]\n [\":clear\" \"empties it\"]]\n :try \"Send it a fetch's answer and open the branches: pick a line and its path comes out on the right.\"}})\n"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"class": "fetch",
|
|
17
|
+
"license": "MPL-2.0",
|
|
18
|
+
"doc": "Brings in what is at a URL: JSON (read as EDN: keys that can be keywords become keywords), text or EDN. A URL (https://…, or github:owner/repo@ref/path through jsDelivr) asks for it; what arrives goes out on the left and is kept as the data of the box, in the history, so everyone in a room has the same and going back does not fetch again (a URL it already holds is sent again, not fetched: refresh fetches it again). With live it is only held in this tab, for answers that change. On the right: :loading, :ready or [:error \"…\"].",
|
|
19
|
+
"source": ";; fetch · what is at a URL (JSON, text or EDN), brought in as a task\n;; Part of @kairotic/data. Written against the Kairotic API (api/…).\n;; Its logic never touches the network: a URL asks for the task :get, the engine runs it (api/http-get)\n;; and what it finds comes back to this box on inlet :task. By default the box keeps it as its data\n;; ([:keep …]): one gesture of the history, the same for everyone in a room, and going back in the\n;; history does not fetch it again. With `live` it is only held in this tab, for answers that change.\n(let [formats #{:json :text :edn}\n fmt-of (fn [args] (if (formats (:as args)) (:as args) :json))\n ;; what the box holds: kept in the work, or (live) only here\n sane (fn [d] (if (and (map? d) (string? (:url d))) (select-keys d [:url :value :date]) {}))\n ;; kept in the work (its data) or, live or before it could be kept (inside a sub-patch, too long, played\n ;; by someone else in a jam), only here (its state)\n held-of (fn [args data state] (if (and (not (:live args)) (contains? data :value)) data (:got state)))\n kept? (fn [args data] (and (not (:live args)) (contains? data :value)))\n summary (fn [v] (cond (map? v) (str \"{} \" (count v) (if (= 1 (count v)) \" key\" \" keys\"))\n (sequential? v) (str \"[] \" (count v) (if (= 1 (count v)) \" item\" \" items\"))\n (string? v) (str \"\\\"\\\" \" (count v) \" characters\")\n (nil? v) \"nil\"\n :else (pr-str v)))\n url-in (fn [value]\n (cond (api/url-of value) (api/url-of value)\n (and (sequential? value) (#{:fetch :url} (first value))) (api/url-of (second value))\n :else nil))\n path-in (fn [value] (let [p (rest value)] (if (and (= 1 (count p)) (sequential? (first p))) (vec (first p)) (vec p))))]\n {;; ── about ──\n :class \"fetch\"\n :license \"MPL-2.0\"\n :doc \"Brings in what is at a URL: JSON (read as EDN: keys that can be keywords become keywords), text or EDN. A URL (https://…, or github:owner/repo@ref/path through jsDelivr) asks for it; what arrives goes out on the left and is kept as the data of the box, in the history, so everyone in a room has the same and going back does not fetch again (a URL it already holds is sent again, not fetched: refresh fetches it again). With live it is only held in this tab, for answers that change. On the right: :loading, :ready or [:error \\\"…\\\"].\"\n :usage \"fetch [json|text|edn] [live]\"\n :io [1 2]\n :size [230 42]\n\n ;; ── arguments ──\n :args [[:as nil] [:live false]]\n\n ;; ── data: what it keeps, in the history ──\n :model {:empty {}\n :step (fn [d [op x]] (case op :set (sane x) :clear {} d))\n :load sane\n :code (fn [id d] (when (contains? d :value) [(str \"(keep! \" id \" \" (pr-str d) \")\")]))}\n\n ;; ── tasks: outside its pure logic ──\n :tasks {:get (fn [{:keys [url as]} _]\n (.then (api/http-get url {:as as})\n (fn [v] {:body v :date (.toISOString (js/Date.))})))}\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state data args]}]\n (let [st (or state {})\n held (held-of args data st)\n ask (fn [u] (let [req {:url u :as (fmt-of args) :n (inc (:n st 0))}]\n {:state (assoc st :waiting req :n (:n req))\n :show {:status :loading :url u}\n :out [[1 :loading]]\n :effects [[:get req]]}))]\n (cond\n ;; what the task found\n (= inlet :task)\n (cond\n (not= (:arg value) (:waiting st)) {} ; the answer to an older request: dropped\n (:error value)\n {:state (dissoc st :waiting)\n :show {:status :error :url (:url (:arg value)) :error (:error value)}\n :out [[1 [:error (:error value)]]]}\n :else\n (let [got {:url (:url (:arg value)) :value (:body (:value value)) :date (:date (:value value))}\n same? (and (kept? args data) (= (:url data) (:url got)) (= (:value data) (:value got)))]\n {:state (-> st (dissoc :waiting) (assoc :got got))\n :show {:status :ready :url (:url got) :summary (summary (:value got))}\n :out [[1 :ready] [0 (:value got)]]\n :effects (if (or (:live args) same?) [] [[:keep got]])}))\n\n ;; a URL: fetched, unless it is the one it already keeps (sent again, then)\n (url-in value)\n (let [u (url-in value)]\n (cond (= u (:url (:waiting st))) {}\n (and (kept? args data) (= u (:url held))) {:out [[1 :ready] [0 (:value held)]]}\n :else (ask u)))\n (#{:refresh} value)\n (if-let [u (or (:url held) (:url (:waiting st)))] (ask u) {})\n\n (#{:bang} value) (if held {:out [[1 :ready] [0 (:value held)]]} {})\n (and (sequential? value) (#{:get} (first value)))\n (if held\n (let [p (path-in value) x (get-in (:value held) p ::none)]\n (if (= x ::none)\n {:out [[1 [:error (str \"nothing at \" (pr-str p))]]]}\n {:out [[0 x]]}))\n {})\n (#{:clear} value)\n {:state {:n (:n st 0)} :show {:status :idle}\n :effects (if (kept? args data) [[:keep nil]] [])}\n :else {})))\n\n :derive (fn [{:keys [shown data state args]}]\n (let [held (held-of args data state)\n status (or (:status shown) (if held :held :idle))\n short (fn [s n] (if (> (count s) n) (str (subs s 0 (dec n)) \"…\") s))]\n {:color (case status (:ready :held) :accent :loading :soft :error :kairos :grid)\n :label (short (case status\n :loading \"loading…\"\n :error (str \"✕ \" (:error shown))\n (:ready :held) (str (cond (kept? args data) \"kept · \" (:live args) \"live · \" :else \"here · \")\n (summary (:value held)))\n (str \"send a URL\" (when (:live args) \" · live\")))\n 38)\n :url (short (str (or (:url shown) (:url held) \"\")) 46)}))\n\n ;; ── view ──\n :view [[:rect {:x 1 :y 1 :w 228 :h 40 :r 4 :stroke :grid :fill :bg}]\n [:circle {:cx 12 :cy 14 :r 4 :fill :$color}]\n [:text {:x 22 :y 17 :size 10 :fill :ink} :$label]\n [:text {:x 8 :y 33 :size 8 :fill :soft} :$url]]\n\n ;; ── help ──\n :help {:inlets [\"a URL (msg https://… · a string · [:fetch url]); refresh; bang; [:get key …]; clear\"]\n :outlets [\"what it found (or the part a [:get …] asks for)\" \":loading, :ready or [:error \\\"…\\\"]\"]\n :messages [[\"a URL\" \"brings it in (if it already keeps that URL, sends it again)\"]\n [\":refresh\" \"fetches its URL again\"]\n [\"bang\" \"sends what it holds\"]\n [\":get :items 0 :name · [:get [:items 0 :name]]\" \"sends one part of it\"]\n [\":clear\" \"forgets it\"]]\n :try \"Click the message with the URL: the answer comes out on the left, into an edn box. Go back in the history: it is still there, without fetching.\"}})\n"
|
|
20
|
+
}
|
|
21
|
+
]
|
|
22
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
// @kairotic/faust-units · a search that listens: CMA-ES on a distance (discussion #114, Study 32).
|
|
3
|
+
//
|
|
4
|
+
// The covariance matrix adaptation evolution strategy (Hansen), in its plain (μ/μ_w, λ) form, in [0, 1]^n
|
|
5
|
+
// (candidates outside are clipped), with a seeded generator so a run can be repeated. No gradient is
|
|
6
|
+
// needed: f renders and listens. f may return a promise (the page gives the browser a moment between
|
|
7
|
+
// renders), so the search is async.
|
|
8
|
+
function rng(seed) {
|
|
9
|
+
let a = seed >>> 0;
|
|
10
|
+
return () => { a = (a + 0x6d2b79f5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// eigen-decomposition of a symmetric matrix (Jacobi): C = B diag(d) Bᵀ
|
|
14
|
+
function eig(C) {
|
|
15
|
+
const n = C.length, A = C.map((r) => r.slice()), V = A.map((_, i) => A.map((_, j) => (i === j ? 1 : 0)));
|
|
16
|
+
for (let sweep = 0; sweep < 60; sweep++) {
|
|
17
|
+
let off = 0;
|
|
18
|
+
for (let p = 0; p < n; p++) for (let q = p + 1; q < n; q++) off += A[p][q] * A[p][q];
|
|
19
|
+
if (off < 1e-22) break;
|
|
20
|
+
for (let p = 0; p < n; p++) for (let q = p + 1; q < n; q++) {
|
|
21
|
+
if (Math.abs(A[p][q]) < 1e-300) continue;
|
|
22
|
+
const th = (A[q][q] - A[p][p]) / (2 * A[p][q]);
|
|
23
|
+
const t = Math.sign(th || 1) / (Math.abs(th) + Math.sqrt(th * th + 1)), c = 1 / Math.sqrt(t * t + 1), s = t * c;
|
|
24
|
+
for (let k = 0; k < n; k++) { const akp = A[k][p], akq = A[k][q]; A[k][p] = c * akp - s * akq; A[k][q] = s * akp + c * akq; }
|
|
25
|
+
for (let k = 0; k < n; k++) { const apk = A[p][k], aqk = A[q][k]; A[p][k] = c * apk - s * aqk; A[q][k] = s * apk + c * aqk; }
|
|
26
|
+
for (let k = 0; k < n; k++) { const vkp = V[k][p], vkq = V[k][q]; V[k][p] = c * vkp - s * vkq; V[k][q] = s * vkp + c * vkq; }
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return { d: A.map((r, i) => Math.max(1e-20, r[i])), B: V };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// minimise f over [0, 1]^n from x0, with step sigma, in at most `budget` evaluations of f.
|
|
33
|
+
// onStep(best, evals) after each generation; returns { x, fx, evals, history: [[evals, best fx]…] }
|
|
34
|
+
export async function cmaes(f, x0, { sigma = 0.15, budget = 120, lambda = 0, seed = 1, onStep = () => {} } = {}) {
|
|
35
|
+
const n = x0.length, r = rng(seed);
|
|
36
|
+
const gauss = () => Math.sqrt(-2 * Math.log(r() + 1e-12)) * Math.cos(2 * Math.PI * r());
|
|
37
|
+
const lam = lambda || 4 + Math.floor(3 * Math.log(n)), mu = Math.floor(lam / 2);
|
|
38
|
+
let w = Array.from({ length: mu }, (_, i) => Math.log(mu + 0.5) - Math.log(i + 1));
|
|
39
|
+
const ws = w.reduce((a, b) => a + b, 0); w = w.map((x) => x / ws);
|
|
40
|
+
const mueff = 1 / w.reduce((a, b) => a + b * b, 0);
|
|
41
|
+
const cc = (4 + mueff / n) / (n + 4 + (2 * mueff) / n), cs = (mueff + 2) / (n + mueff + 5);
|
|
42
|
+
const c1 = 2 / ((n + 1.3) ** 2 + mueff), cmu = Math.min(1 - c1, (2 * (mueff - 2 + 1 / mueff)) / ((n + 2) ** 2 + mueff));
|
|
43
|
+
const damps = 1 + 2 * Math.max(0, Math.sqrt((mueff - 1) / (n + 1)) - 1) + cs, chiN = Math.sqrt(n) * (1 - 1 / (4 * n) + 1 / (21 * n * n));
|
|
44
|
+
const clip = (x) => x.map((v) => Math.min(1, Math.max(0, v)));
|
|
45
|
+
let m = clip(x0.slice()), sig = sigma, pc = new Array(n).fill(0), ps = new Array(n).fill(0);
|
|
46
|
+
let C = m.map((_, i) => m.map((_, j) => (i === j ? 1 : 0))), B = C.map((r) => r.slice()), D = new Array(n).fill(1);
|
|
47
|
+
let best = { x: m.slice(), fx: await f(m) }, evals = 1, gen = 0;
|
|
48
|
+
const history = [[evals, best.fx]];
|
|
49
|
+
while (evals + lam <= budget) {
|
|
50
|
+
gen++;
|
|
51
|
+
const pop = [];
|
|
52
|
+
for (let k = 0; k < lam; k++) {
|
|
53
|
+
const z = Array.from({ length: n }, gauss), y = B.map((row) => row.reduce((s, b, j) => s + b * D[j] * z[j], 0));
|
|
54
|
+
const x = clip(m.map((v, i) => v + sig * y[i]));
|
|
55
|
+
const fx = await f(x); evals++;
|
|
56
|
+
pop.push({ x, y: x.map((v, i) => (v - m[i]) / sig), fx });
|
|
57
|
+
if (fx < best.fx) best = { x: x.slice(), fx };
|
|
58
|
+
}
|
|
59
|
+
pop.sort((a, b) => a.fx - b.fx);
|
|
60
|
+
const old = m.slice();
|
|
61
|
+
m = m.map((_, i) => pop.slice(0, mu).reduce((s, p, k) => s + w[k] * p.x[i], 0));
|
|
62
|
+
const yw = m.map((v, i) => (v - old[i]) / sig);
|
|
63
|
+
// C^(-1/2) yw
|
|
64
|
+
const Bt_yw = B[0].map((_, j) => B.reduce((s, row, i) => s + row[j] * yw[i], 0));
|
|
65
|
+
const invsq = B.map((row) => row.reduce((s, b, j) => s + (b * Bt_yw[j]) / D[j], 0));
|
|
66
|
+
ps = ps.map((v, i) => (1 - cs) * v + Math.sqrt(cs * (2 - cs) * mueff) * invsq[i]);
|
|
67
|
+
const psn = Math.sqrt(ps.reduce((s, v) => s + v * v, 0));
|
|
68
|
+
const hs = psn / Math.sqrt(1 - Math.pow(1 - cs, 2 * gen)) / chiN < 1.4 + 2 / (n + 1) ? 1 : 0;
|
|
69
|
+
pc = pc.map((v, i) => (1 - cc) * v + hs * Math.sqrt(cc * (2 - cc) * mueff) * yw[i]);
|
|
70
|
+
C = C.map((row, i) => row.map((c, j) => {
|
|
71
|
+
let rank = 0;
|
|
72
|
+
for (let k = 0; k < mu; k++) rank += w[k] * pop[k].y[i] * pop[k].y[j];
|
|
73
|
+
return (1 - c1 - cmu) * c + c1 * (pc[i] * pc[j] + (1 - hs) * cc * (2 - cc) * c) + cmu * rank;
|
|
74
|
+
}));
|
|
75
|
+
sig *= Math.exp((cs / damps) * (psn / chiN - 1));
|
|
76
|
+
sig = Math.min(0.5, Math.max(1e-4, sig));
|
|
77
|
+
const e = eig(C); B = e.B; D = e.d.map(Math.sqrt);
|
|
78
|
+
history.push([evals, best.fx]);
|
|
79
|
+
onStep(best, evals);
|
|
80
|
+
}
|
|
81
|
+
return { x: best.x, fx: best.fx, evals, history };
|
|
82
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
// @kairotic/faust-units · a subpatch of unit boxes, and the Faust code it is.
|
|
3
|
+
//
|
|
4
|
+
// The graph: {boxes: [{id, type, args, name, x, y, io?}], wires: [{from: [id, outlet], to: [id, inlet]}]}
|
|
5
|
+
// (io = {ins, outs} for a box of Faust code, whose ports come from compiling it).
|
|
6
|
+
//
|
|
7
|
+
// patch → code: each box is one definition named after it, in a `with`, in the order signals flow;
|
|
8
|
+
// a loop goes through a one-sample delay box ('): the network becomes a function of the delays' outputs,
|
|
9
|
+
// fed back with ~ (which is the delay); a definition that depends on a delay's output takes it as a parameter. The layout of
|
|
10
|
+
// the boxes travels in a comment at the end, so the code alone is enough to rebuild the patch.
|
|
11
|
+
|
|
12
|
+
import { UNITS, unitOf, inletSlots, fmt, boxText } from "./units.mjs";
|
|
13
|
+
|
|
14
|
+
// names Faust or its libraries already use: a box is never named like this
|
|
15
|
+
const RESERVED = new Set(("process with letrec case seq par sum prod import component library environment declare " +
|
|
16
|
+
"inputs outputs route waveform soundfile ffunction fconstant fvariable button checkbox hslider vslider nentry " +
|
|
17
|
+
"hbargraph vbargraph hgroup vgroup tgroup attach enable control mem prefix int float rdtable rwtable select2 select3 " +
|
|
18
|
+
"sin cos tan asin acos atan atan2 exp log log10 pow sqrt abs min max fmod remainder floor ceil rint round tanh " +
|
|
19
|
+
"sinh cosh lowest highest ma ba os no fi en de si re ef co sp an aa ve pf ho dm sf ro mi ot ds wd vl fd").split(" "));
|
|
20
|
+
|
|
21
|
+
export function inlets(box) {
|
|
22
|
+
const u = unitOf(box);
|
|
23
|
+
return u.kind === "code" ? (box.io ? box.io.ins : 0) : inletSlots(u).length;
|
|
24
|
+
}
|
|
25
|
+
export function outlets(box) {
|
|
26
|
+
const u = unitOf(box);
|
|
27
|
+
return u.kind === "code" ? (box.io ? box.io.outs : 0) : u.outs;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// a valid, unused Faust name for a box
|
|
31
|
+
export function nameFor(base, taken) {
|
|
32
|
+
let b = String(base).replace(/[^A-Za-z0-9_]/g, "_").replace(/^([^A-Za-z_])/, "_$1") || "b";
|
|
33
|
+
if (!RESERVED.has(b) && !taken.has(b)) return b;
|
|
34
|
+
for (let i = 1; ; i++) if (!taken.has(b + i) && !RESERVED.has(b + i)) return b + i;
|
|
35
|
+
}
|
|
36
|
+
export function name(graph, box) {
|
|
37
|
+
if (box.name) return box.name;
|
|
38
|
+
const u = unitOf(box), taken = new Set(graph.boxes.map((b) => b.name).filter(Boolean));
|
|
39
|
+
if (box.type === "in") return (box.name = nameFor("x" + (box.args[0] || 1), taken));
|
|
40
|
+
if (box.type === "out") return (box.name = nameFor("y" + (box.args[0] || 1), taken));
|
|
41
|
+
if (u.kind === "ui") return (box.name = nameFor(box.args[0], taken));
|
|
42
|
+
return (box.name = nameFor(u.base, taken));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// what each slot of a box gets from the numbers written after its name
|
|
46
|
+
export function slotArgs(box) {
|
|
47
|
+
const u = unitOf(box), args = box.args || [], out = new Array(u.slots.length).fill(undefined);
|
|
48
|
+
if (u.kind === "prim") {
|
|
49
|
+
const n = u.slots.length, k = Math.min(args.length, n);
|
|
50
|
+
for (let j = 0; j < k; j++) out[n - k + j] = args[j];
|
|
51
|
+
} else if (u.kind === "lib") {
|
|
52
|
+
let j = 0;
|
|
53
|
+
u.slots.forEach((s, i) => { if (s.kind !== "in" && j < args.length) out[i] = args[j++]; });
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function toFaust(graph) {
|
|
59
|
+
const byId = new Map(graph.boxes.map((b) => [b.id, b]));
|
|
60
|
+
graph.boxes.forEach((b) => name(graph, b));
|
|
61
|
+
const into = new Map(); // "id:inlet" → [ports]
|
|
62
|
+
for (const w of graph.wires) {
|
|
63
|
+
if (!byId.has(w.from[0]) || !byId.has(w.to[0])) continue;
|
|
64
|
+
const k = w.to[0] + ":" + w.to[1];
|
|
65
|
+
if (!into.has(k)) into.set(k, []);
|
|
66
|
+
into.get(k).push(w.from);
|
|
67
|
+
}
|
|
68
|
+
const sources = (b) => {
|
|
69
|
+
const out = [];
|
|
70
|
+
for (let i = 0; i < inlets(b); i++) (into.get(b.id + ":" + i) || []).forEach((p) => out.push(p[0]));
|
|
71
|
+
return out;
|
|
72
|
+
};
|
|
73
|
+
const isMem = (b) => b.type === "'";
|
|
74
|
+
|
|
75
|
+
// which delay boxes close a loop: those whose input depends on themselves
|
|
76
|
+
const rec = new Set();
|
|
77
|
+
for (const m of graph.boxes.filter(isMem)) {
|
|
78
|
+
const seen = new Set(), stack = sources(m);
|
|
79
|
+
while (stack.length) {
|
|
80
|
+
const id = stack.pop();
|
|
81
|
+
if (id === m.id) { rec.add(m.id); break; }
|
|
82
|
+
if (seen.has(id)) continue;
|
|
83
|
+
seen.add(id);
|
|
84
|
+
stack.push(...sources(byId.get(id)));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// the order of definitions follows the signal back from the outputs (so the same patch always gives the
|
|
88
|
+
// same code), then from what feeds each loop; it also finds the loop variables each one depends on, and
|
|
89
|
+
// loops with no delay in them
|
|
90
|
+
const ins = graph.boxes.filter((b) => b.type === "in").sort((a, b) => (a.args[0] || 1) - (b.args[0] || 1));
|
|
91
|
+
const outs = graph.boxes.filter((b) => b.type === "out").sort((a, b) => (a.args[0] || 1) - (b.args[0] || 1));
|
|
92
|
+
const order = [], recOrder = [], deps = new Map(), state = new Map();
|
|
93
|
+
const noteRec = (id) => { if (!recOrder.includes(id)) recOrder.push(id); };
|
|
94
|
+
function visit(id) {
|
|
95
|
+
if (rec.has(id)) { noteRec(id); return new Set(); }
|
|
96
|
+
if (state.get(id) === 2) return deps.get(id);
|
|
97
|
+
if (state.get(id) === 1) throw new Error("a loop without a one-sample delay: put a ' box in it");
|
|
98
|
+
state.set(id, 1);
|
|
99
|
+
const d = new Set();
|
|
100
|
+
for (const s of sources(byId.get(id))) {
|
|
101
|
+
if (rec.has(s)) { d.add(byId.get(s).name); noteRec(s); }
|
|
102
|
+
else visit(s).forEach((x) => d.add(x));
|
|
103
|
+
}
|
|
104
|
+
state.set(id, 2);
|
|
105
|
+
deps.set(id, d);
|
|
106
|
+
order.push(id);
|
|
107
|
+
return d;
|
|
108
|
+
}
|
|
109
|
+
outs.forEach((b) => visit(b.id));
|
|
110
|
+
for (let i = 0; i < recOrder.length; i++) sources(byId.get(recOrder[i])).forEach(visit);
|
|
111
|
+
graph.boxes.forEach((b) => visit(b.id));
|
|
112
|
+
for (let i = 0; i < recOrder.length; i++) sources(byId.get(recOrder[i])).forEach(visit);
|
|
113
|
+
|
|
114
|
+
const params = (d) => (d && d.size ? "(" + [...d].sort().join(", ") + ")" : "");
|
|
115
|
+
const ref = ([id, k]) => {
|
|
116
|
+
const b = byId.get(id);
|
|
117
|
+
if (rec.has(id) || b.type === "in") return b.name;
|
|
118
|
+
const r = b.name + params(deps.get(id));
|
|
119
|
+
const n = outlets(b);
|
|
120
|
+
return n > 1 ? `(${r} : ba.selector(${k}, ${n}))` : r;
|
|
121
|
+
};
|
|
122
|
+
const inletText = (b, i, fallback) => {
|
|
123
|
+
const ps = into.get(b.id + ":" + i) || [];
|
|
124
|
+
if (!ps.length) return fallback;
|
|
125
|
+
const rs = ps.map(ref);
|
|
126
|
+
return rs.length > 1 ? "(" + rs.join(" + ") + ")" : rs[0];
|
|
127
|
+
};
|
|
128
|
+
const exprOf = (b) => {
|
|
129
|
+
const u = unitOf(b);
|
|
130
|
+
if (b.type === "num") return fmt(b.args[0]);
|
|
131
|
+
if (u.kind === "code") {
|
|
132
|
+
const ins = [];
|
|
133
|
+
for (let i = 0; i < inlets(b); i++) ins.push(inletText(b, i, "0"));
|
|
134
|
+
return ins.length ? `(${ins.join(", ")} : ${b.args[0]})` : `(${b.args[0]})`;
|
|
135
|
+
}
|
|
136
|
+
if (u.kind === "ui") return u.code([], b.args.map((a, i) => (i ? fmt(a) : a)));
|
|
137
|
+
const sa = slotArgs(b), il = inletSlots(u);
|
|
138
|
+
const v = u.slots.map((s, i) => {
|
|
139
|
+
const inlet = il.indexOf(i);
|
|
140
|
+
const fallback = fmt(sa[i] !== undefined ? sa[i] : s.def);
|
|
141
|
+
return inlet >= 0 ? inletText(b, inlet, fallback) : fallback;
|
|
142
|
+
});
|
|
143
|
+
return u.code(v, b.args);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const defs = [];
|
|
147
|
+
for (const id of order) {
|
|
148
|
+
const b = byId.get(id);
|
|
149
|
+
if (b.type === "in" || b.type === "out") continue;
|
|
150
|
+
defs.push(` ${b.name}${params(deps.get(id))} = ${exprOf(b)};`);
|
|
151
|
+
}
|
|
152
|
+
const head = `process${ins.length ? "(" + ins.map((b) => b.name).join(", ") + ")" : ""} = `;
|
|
153
|
+
const outExprs = outs.length ? outs.map((b) => inletText(b, 0, "0")) : ["0"];
|
|
154
|
+
let body;
|
|
155
|
+
if (recOrder.length) {
|
|
156
|
+
// a loop: the whole network as a function of the delays' outputs, returning what goes into the
|
|
157
|
+
// delays and then the outputs; ~ feeds the first ones back one sample later (that is the delay)
|
|
158
|
+
const mems = recOrder.map((id) => byId.get(id)), K = mems.length;
|
|
159
|
+
const loop = nameFor("loop", new Set(graph.boxes.map((b) => b.name)));
|
|
160
|
+
defs.unshift(` ${loop}(${mems.map((m) => m.name).join(", ")}) = ${mems.map((m) => inletText(m, 0, "0")).concat(outExprs).join(", ")};`);
|
|
161
|
+
body = `${loop} ~ (${Array(K).fill("_").join(", ")}) : (${Array(K).fill("!").concat(Array(outExprs.length).fill("_")).join(", ")})`;
|
|
162
|
+
} else body = outExprs.join(", ");
|
|
163
|
+
let code = 'import("stdfaust.lib");\n\n' + head + body;
|
|
164
|
+
if (defs.length) code += "\nwith {\n" + defs.join("\n") + "\n}";
|
|
165
|
+
code += ";\n";
|
|
166
|
+
const seq = ins.concat(order.map((id) => byId.get(id)).filter((b) => b.type !== "in" && b.type !== "out"),
|
|
167
|
+
recOrder.map((id) => byId.get(id)), outs);
|
|
168
|
+
const lay = seq.map((b) => `${b.name} ${Math.round(b.x)} ${Math.round(b.y)}`);
|
|
169
|
+
for (let i = 0; i < lay.length; i += 6) code += (i ? "" : "\n") + "//@ layout " + lay.slice(i, i + 6).join(", ") + "\n";
|
|
170
|
+
return code;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// boxes without a position: in rows by depth, top to bottom as signals flow
|
|
174
|
+
export function layout(graph, W = 640) {
|
|
175
|
+
const byId = new Map(graph.boxes.map((b) => [b.id, b]));
|
|
176
|
+
const ups = new Map(graph.boxes.map((b) => [b.id, []]));
|
|
177
|
+
for (const w of graph.wires) if (ups.has(w.to[0]) && byId.get(w.from[0]).type !== "'") ups.get(w.to[0]).push(w.from[0]);
|
|
178
|
+
const depth = new Map();
|
|
179
|
+
const d = (id, seen = new Set()) => {
|
|
180
|
+
if (depth.has(id)) return depth.get(id);
|
|
181
|
+
if (seen.has(id)) return 0;
|
|
182
|
+
seen.add(id);
|
|
183
|
+
const v = Math.max(-1, ...ups.get(id).map((u) => d(u, seen))) + 1;
|
|
184
|
+
depth.set(id, v);
|
|
185
|
+
return v;
|
|
186
|
+
};
|
|
187
|
+
graph.boxes.forEach((b) => d(b.id));
|
|
188
|
+
const rows = new Map();
|
|
189
|
+
for (const b of graph.boxes) {
|
|
190
|
+
if (b.x !== undefined && b.y !== undefined) continue;
|
|
191
|
+
const r = b.type === "out" ? Math.max(...depth.values()) : depth.get(b.id);
|
|
192
|
+
if (!rows.has(r)) rows.set(r, []);
|
|
193
|
+
rows.get(r).push(b);
|
|
194
|
+
}
|
|
195
|
+
// side by side, each as wide as its text (as the patch view draws it)
|
|
196
|
+
const w = (b) => Math.max(56, 20 + 7.6 * boxText(b).length);
|
|
197
|
+
for (const [r, bs] of rows) {
|
|
198
|
+
let x = 20;
|
|
199
|
+
for (const b of bs) { b.x = Math.round(x); b.y = 24 + r * 60; x += w(b) + 24; }
|
|
200
|
+
}
|
|
201
|
+
return graph;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function newBox(graph, type, args, x, y, extra = {}) {
|
|
205
|
+
const id = graph.boxes.reduce((m, b) => Math.max(m, b.id), 0) + 1;
|
|
206
|
+
const b = { id, type, args: args || [], x, y, ...extra };
|
|
207
|
+
graph.boxes.push(b);
|
|
208
|
+
name(graph, b);
|
|
209
|
+
return b;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export { UNITS };
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
// @kairotic/faust-units · listening as features~ and pitch~ do (discussion #114, Studies 30 and 32).
|
|
3
|
+
//
|
|
4
|
+
// analyse: Meyda (passed in, so the same code runs in the page, a worker or Node) on frames of 2048
|
|
5
|
+
// samples hopping 1024: rms, centroid (Hz), flatness, rolloff, zcr and 13 MFCCs. matcher: a distance
|
|
6
|
+
// between two such analyses, each number shaped (levels and Hz on a log scale) and standardised by a bank.
|
|
7
|
+
// pitchOf: YIN as pitch~ (window 1024, threshold 0.15, 60–1500 Hz), the median of up to 12 windows.
|
|
8
|
+
|
|
9
|
+
export const SR = 44100, SIZE = 2048, HOP = 1024;
|
|
10
|
+
const FEATURES = ["rms", "spectralCentroid", "spectralFlatness", "spectralRolloff", "zcr", "mfcc"];
|
|
11
|
+
const D = 18; // rms centroid flatness rolloff zcr + 13 MFCCs
|
|
12
|
+
|
|
13
|
+
const fin = (x) => (typeof x === "number" && isFinite(x) ? x : 0);
|
|
14
|
+
export function analyse(Meyda, y, sr = SR) {
|
|
15
|
+
Meyda.bufferSize = SIZE;
|
|
16
|
+
Meyda.sampleRate = sr;
|
|
17
|
+
Meyda.numberOfMFCCCoefficients = 13;
|
|
18
|
+
const frames = [];
|
|
19
|
+
for (let i = 0; i + SIZE <= y.length; i += HOP) {
|
|
20
|
+
const r = Meyda.extract(FEATURES, y.subarray(i, i + SIZE));
|
|
21
|
+
const silent = !(r.rms > 1e-5);
|
|
22
|
+
// a silent frame has no centroid or flatness, and Meyda puts its rolloff at the top: all three 0
|
|
23
|
+
frames.push([
|
|
24
|
+
fin(r.rms),
|
|
25
|
+
silent ? 0 : fin(r.spectralCentroid) * sr / SIZE,
|
|
26
|
+
silent ? 0 : fin(r.spectralFlatness),
|
|
27
|
+
silent ? 0 : fin(r.spectralRolloff),
|
|
28
|
+
fin(r.zcr)
|
|
29
|
+
].concat(Array.from(r.mfcc || [], fin)));
|
|
30
|
+
}
|
|
31
|
+
return frames;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// descriptors as the distance sees them: Hz and level on a log scale, then every column standardised
|
|
35
|
+
const shape = (row) => row.map((x, j) =>
|
|
36
|
+
j === 0 ? Math.log10(x + 1e-4) : j === 1 || j === 3 ? Math.log2(1 + x) : x);
|
|
37
|
+
|
|
38
|
+
export function matcher(train) {
|
|
39
|
+
const mu = new Float64Array(D), sd = new Float64Array(D);
|
|
40
|
+
let count = 0;
|
|
41
|
+
for (const e of train) for (const row of e.f) { const s = shape(row); for (let j = 0; j < D; j++) mu[j] += s[j]; count++; }
|
|
42
|
+
for (let j = 0; j < D; j++) mu[j] /= count || 1;
|
|
43
|
+
for (const e of train) for (const row of e.f) { const s = shape(row); for (let j = 0; j < D; j++) sd[j] += (s[j] - mu[j]) ** 2; }
|
|
44
|
+
for (let j = 0; j < D; j++) sd[j] = Math.sqrt(sd[j] / (count || 1)) || 1;
|
|
45
|
+
const vec = (f) => {
|
|
46
|
+
const out = new Float32Array(f.length * D);
|
|
47
|
+
f.forEach((row, t) => shape(row).forEach((x, j) => { out[t * D + j] = (x - mu[j]) / sd[j]; }));
|
|
48
|
+
return out;
|
|
49
|
+
};
|
|
50
|
+
const bank = train.map((e) => vec(e.f));
|
|
51
|
+
// distance per number, so it reads the same whatever the number of frames
|
|
52
|
+
const dist = (a, b) => {
|
|
53
|
+
let s = 0;
|
|
54
|
+
const m = Math.min(a.length, b.length);
|
|
55
|
+
for (let i = 0; i < m; i++) s += (a[i] - b[i]) ** 2;
|
|
56
|
+
return Math.sqrt(s / m);
|
|
57
|
+
};
|
|
58
|
+
return {
|
|
59
|
+
vec, dist,
|
|
60
|
+
nearest(f, skip) {
|
|
61
|
+
const q = vec(f);
|
|
62
|
+
let best = -1, bd = Infinity;
|
|
63
|
+
bank.forEach((b, i) => { if (i === skip) return; const d = dist(q, b); if (d < bd) { bd = d; best = i; } });
|
|
64
|
+
return { index: best, distance: bd, example: train[best] };
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// the loudest frame's rms
|
|
70
|
+
export const peakRms = (y) => { let p = 0; for (let at = 0; at + SIZE <= y.length; at += HOP) { let e = 0; for (let i = 0; i < SIZE; i++) e += y[at + i] * y[at + i]; p = Math.max(p, Math.sqrt(e / SIZE)); } return p; };
|
|
71
|
+
|
|
72
|
+
// YIN's window and hop
|
|
73
|
+
const YW = 1024, YHOP = 512;
|
|
74
|
+
|
|
75
|
+
// YIN on one window starting at `at`: [hz, clarity] (hz 0 when no dip falls under the threshold)
|
|
76
|
+
function yin(x, at, sr, lo, hi, th, d) {
|
|
77
|
+
const T = Math.min(d.length - 2, Math.ceil(sr / lo) + 1), tmin = Math.max(2, Math.floor(sr / hi));
|
|
78
|
+
d[0] = 1;
|
|
79
|
+
let run = 0, best = -1;
|
|
80
|
+
for (let tau = 1; tau <= T; tau++) {
|
|
81
|
+
let s = 0;
|
|
82
|
+
for (let i = 0; i < YW; i++) { const v = x[at + i] - x[at + i + tau]; s += v * v; }
|
|
83
|
+
run += s;
|
|
84
|
+
d[tau] = run > 0 ? (s * tau) / run : 1;
|
|
85
|
+
if (tau - 1 >= tmin && d[tau - 1] < th && d[tau] >= d[tau - 1]) { best = tau - 1; break; }
|
|
86
|
+
}
|
|
87
|
+
if (best < 0) return [0, 0];
|
|
88
|
+
const a = d[best - 1], b = d[best], c = d[best + 1] ?? b, den = a - 2 * b + c;
|
|
89
|
+
const t = den ? best + (0.5 * (a - c)) / den : best;
|
|
90
|
+
return [sr / t, Math.max(0, 1 - b)];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// the pitch of a note: the median MIDI pitch of its sounding, clearly periodic windows, and how sure it is
|
|
94
|
+
// (the share of sounding windows that had a pitch). { midi, hz, sure } or { midi: null } (noise, silence)
|
|
95
|
+
export function pitchOf(x, sr = 44100, { lo = 60, hi = 1500, th = 0.15, most = 12 } = {}) {
|
|
96
|
+
const T = Math.ceil(sr / lo) + 2, d = new Float32Array(T + 2), wins = [];
|
|
97
|
+
let peak = 0;
|
|
98
|
+
for (let at = 0; at + YW + T < x.length; at += YHOP) {
|
|
99
|
+
let e = 0;
|
|
100
|
+
for (let i = 0; i < YW; i++) e += x[at + i] * x[at + i];
|
|
101
|
+
wins.push([at, Math.sqrt(e / YW)]);
|
|
102
|
+
peak = Math.max(peak, Math.sqrt(e / YW));
|
|
103
|
+
}
|
|
104
|
+
const all = wins.filter(([, r]) => r > 0.1 * peak && r > 1e-3);
|
|
105
|
+
// at most `most` of them, spread over the note: enough for a median, and quick
|
|
106
|
+
const step = Math.max(1, all.length / most), sounding = [];
|
|
107
|
+
for (let k = 0; k < all.length && sounding.length < most; k += step) sounding.push(all[Math.floor(k)]);
|
|
108
|
+
const ms = [];
|
|
109
|
+
for (const [at] of sounding) {
|
|
110
|
+
const [hz] = yin(x, at, sr, lo, hi, th, d);
|
|
111
|
+
if (hz > 0) ms.push(69 + 12 * Math.log2(hz / 440));
|
|
112
|
+
}
|
|
113
|
+
if (!sounding.length || ms.length < Math.max(2, 0.3 * sounding.length)) return { midi: null, hz: 0, sure: sounding.length ? ms.length / sounding.length : 0 };
|
|
114
|
+
ms.sort((a, b) => a - b);
|
|
115
|
+
const midi = ms[ms.length >> 1];
|
|
116
|
+
return { midi, hz: 440 * Math.pow(2, (midi - 69) / 12), sure: ms.length / sounding.length };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// the octave: YIN's pitch class, at the register a rough guess (the network's) points to
|
|
120
|
+
export function octaveOf(yinMidi, guessMidi, lo = 36, hi = 84) {
|
|
121
|
+
let m = yinMidi + 12 * Math.round((guessMidi - yinMidi) / 12);
|
|
122
|
+
while (m < lo - 0.5) m += 12;
|
|
123
|
+
while (m > hi + 0.5) m -= 12;
|
|
124
|
+
return m;
|
|
125
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
// @kairotic/faust-units · what a faust~ can sound like, heard, so descriptors can be written to it
|
|
3
|
+
// (discussion #114, Study 37): the bank realize~ (in @kairotic/analysis) learns.
|
|
4
|
+
//
|
|
5
|
+
// Its sliders: `pitch` (MIDI) or `freq` (Hz) follows a pitch, `gain` (dB, or a linear level) a loudness, and
|
|
6
|
+
// the others are its timbre. learn() plays steady settings of it offline — the timbre drawn at random, a
|
|
7
|
+
// pitch in its range, the gain at 0 dB — and listens to each as features~ and pitch~ do: its level, its
|
|
8
|
+
// brightness (spectral centroid, as log2 Hz), its noisiness (spectral flatness) and how much of it YIN hears
|
|
9
|
+
// at its own pitch. A frame of a score is then played by the settings nearest to it (realize~ does that).
|
|
10
|
+
// It runs in the page a moment at a time and stops when its signal is aborted.
|
|
11
|
+
//
|
|
12
|
+
// const bank = await learn({ module, json, Meyda, n: 300 });
|
|
13
|
+
// bank → { pitch: {label, kind} | null, gain: {label, db, min, max} | null, timbre: [{label, min, max, log}],
|
|
14
|
+
// entries: [[pitch, lb, nz, voiced, loud0, u0, u1, …] …], sd: [lb, nz], n }
|
|
15
|
+
|
|
16
|
+
import { renderer } from "./render.mjs";
|
|
17
|
+
import { pitchOf, SIZE, HOP } from "./listen.mjs";
|
|
18
|
+
import { sliders, pitchSlider, toValue } from "./recipe.mjs";
|
|
19
|
+
|
|
20
|
+
function rng(seed) {
|
|
21
|
+
let a = seed >>> 0;
|
|
22
|
+
return () => { a = (a + 0x6d2b79f5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
|
|
23
|
+
}
|
|
24
|
+
const mean = (xs) => xs.reduce((s, x) => s + x, 0) / (xs.length || 1);
|
|
25
|
+
const stopped = () => Object.assign(new Error("stopped"), { name: "AbortError" });
|
|
26
|
+
const now = () => (typeof performance !== "undefined" ? performance.now() : Date.now());
|
|
27
|
+
// a moment given back to the page: a message to itself (a timer in a page that is not in front waits a second
|
|
28
|
+
// or more; a message does not)
|
|
29
|
+
const breath = () => new Promise((ok) => {
|
|
30
|
+
if (typeof MessageChannel === "undefined") return setTimeout(ok, 0);
|
|
31
|
+
const c = new MessageChannel();
|
|
32
|
+
c.port1.onmessage = () => { c.port1.close(); ok(); };
|
|
33
|
+
c.port2.postMessage(0);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// which slider does what
|
|
37
|
+
export function roles(json) {
|
|
38
|
+
const cs = sliders(json), ps = pitchSlider(cs);
|
|
39
|
+
const gi = cs.findIndex((c) => /^(gain|level|volume|amp)$/i.test(c.label));
|
|
40
|
+
const timbre = cs.map((_, i) => i).filter((i) => !(ps && i === ps.index) && i !== gi);
|
|
41
|
+
return { cs, ps, gi, timbre };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// what one steady setting sounds like: [level dB, log2 brightness, noisiness, voiced] or null if silent.
|
|
45
|
+
// Only what is needed, as features~ computes it (Meyda: centroid in Hz, flatness), on the frames after the
|
|
46
|
+
// first 40 % (the sliders settle), and YIN on four windows: a bank of 300 is heard in a few seconds.
|
|
47
|
+
function heard(Meyda, y, midi, sr = 44100) {
|
|
48
|
+
Meyda.bufferSize = SIZE;
|
|
49
|
+
Meyda.sampleRate = sr;
|
|
50
|
+
const lb = [], nz = [];
|
|
51
|
+
let e = 0, k = 0;
|
|
52
|
+
for (let at = Math.floor((y.length - SIZE) * 0.4 / HOP) * HOP; at + SIZE <= y.length; at += HOP) {
|
|
53
|
+
const fr = y.subarray(at, at + SIZE);
|
|
54
|
+
let fe = 0;
|
|
55
|
+
for (let j = 0; j < SIZE; j++) fe += fr[j] * fr[j];
|
|
56
|
+
e += fe; k += SIZE;
|
|
57
|
+
if (fe / SIZE < 1e-10) continue;
|
|
58
|
+
const r = Meyda.extract(["spectralCentroid", "spectralFlatness"], fr);
|
|
59
|
+
const c = r.spectralCentroid * sr / SIZE;
|
|
60
|
+
if (c > 0 && Number.isFinite(c)) { lb.push(Math.log2(c)); nz.push(Number.isFinite(r.spectralFlatness) ? r.spectralFlatness : 0); }
|
|
61
|
+
}
|
|
62
|
+
const loud = 10 * Math.log10(Math.max(e / (k || 1), 1e-10));
|
|
63
|
+
if (!lb.length || loud < -70) return null;
|
|
64
|
+
const p = pitchOf(y.subarray(Math.floor(y.length * 0.4)), sr, { most: 4 });
|
|
65
|
+
const voiced = p.midi !== null && (midi === null || Math.abs(p.midi - midi) < 0.5) ? p.sure : 0;
|
|
66
|
+
return [loud, mean(lb), mean(nz), voiced];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function learn({ module, json, Meyda, n = 300, seed = 3, dur = 0.4, signal = null, slice = 25, onProgress = () => {} }) {
|
|
70
|
+
const R = roles(json);
|
|
71
|
+
if (!R.timbre.length) throw new Error("the faust~ has no slider for the timbre (besides pitch and gain)");
|
|
72
|
+
const render = renderer(module, json), r = rng(seed), entries = [];
|
|
73
|
+
const pc = R.ps ? R.cs[R.ps.index] : null;
|
|
74
|
+
const toMidi = (v) => (R.ps.kind === "midi" ? v : 69 + 12 * Math.log2(Math.max(1, v) / 440));
|
|
75
|
+
const fromMidi = (m) => (R.ps.kind === "midi" ? m : 440 * Math.pow(2, (m - 69) / 12));
|
|
76
|
+
const lo = pc ? Math.max(24, toMidi(pc.min)) : 60, hi = pc ? Math.min(96, toMidi(pc.max)) : 60;
|
|
77
|
+
const gc = R.gi >= 0 ? R.cs[R.gi] : null, gdb = gc ? gc.unit === "dB" || gc.min < 0 : false;
|
|
78
|
+
let t0 = now();
|
|
79
|
+
for (let k = 0; k < n; k++) {
|
|
80
|
+
if (signal && signal.aborted) throw stopped();
|
|
81
|
+
const u = R.timbre.map(() => r()), midi = pc ? lo + (hi - lo) * r() : null, v = {};
|
|
82
|
+
R.timbre.forEach((i, j) => { v[R.cs[i].label] = toValue(R.cs[i], u[j]); });
|
|
83
|
+
if (pc) v[pc.label] = fromMidi(midi);
|
|
84
|
+
if (gc) v[gc.label] = gdb ? Math.min(gc.max, Math.max(gc.min, 0)) : Math.min(gc.max, Math.max(gc.min, 1));
|
|
85
|
+
const h = heard(Meyda, render(v, { dur, gate: dur + 1 }), midi);
|
|
86
|
+
if (h) entries.push([pc ? midi : 60, h[1], h[2], h[3], h[0], ...u]);
|
|
87
|
+
onProgress(k + 1, n);
|
|
88
|
+
if (now() - t0 > slice) { await breath(); t0 = now(); }
|
|
89
|
+
}
|
|
90
|
+
if (entries.length < 8) throw new Error("the faust~ hardly sounds: " + entries.length + " of " + n + " settings were heard");
|
|
91
|
+
const sd = (j) => { const xs = entries.map((e) => e[j]), m = mean(xs); return Math.sqrt(mean(xs.map((x) => (x - m) ** 2))) || 1; };
|
|
92
|
+
const slim = (c) => ({ label: c.label, min: c.min, max: c.max, log: c.scale === "log" && c.min > 0 && c.max > c.min, step: c.step });
|
|
93
|
+
return {
|
|
94
|
+
pitch: pc ? { label: pc.label, kind: R.ps.kind, min: pc.min, max: pc.max } : null,
|
|
95
|
+
gain: gc ? { label: gc.label, db: gdb, min: gc.min, max: gc.max } : null,
|
|
96
|
+
timbre: R.timbre.map((i) => slim(R.cs[i])),
|
|
97
|
+
entries: entries.map((e) => e.map((x) => +x.toPrecision(5))),
|
|
98
|
+
sd: [sd(1), sd(2)],
|
|
99
|
+
n
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// index(bank) → query(pitch, lb, nz, voiced) → [loud0, distance, u0, u1, …]: the six settings nearest to a
|
|
104
|
+
// frame (in pitch, log2 brightness, noisiness and whether YIN would hear the pitch), blended by closeness.
|
|
105
|
+
// Plain loops over typed arrays, so realize~ can ask it for every frame it plays.
|
|
106
|
+
export function index(bank) {
|
|
107
|
+
const E = bank.entries, n = E.length, w = E[0].length, data = new Float64Array(n * w);
|
|
108
|
+
E.forEach((e, i) => data.set(e, i * w));
|
|
109
|
+
const [sl, sn] = bank.sd, nu = w - 5, K = Math.min(6, n);
|
|
110
|
+
const bd = new Float64Array(K), bi = new Int32Array(K);
|
|
111
|
+
return function query(pitch, lb, nz, voiced) {
|
|
112
|
+
let m = 0;
|
|
113
|
+
for (let i = 0; i < n; i++) {
|
|
114
|
+
const o = i * w;
|
|
115
|
+
let d = ((lb - data[o + 1]) / sl) ** 2 + ((nz - data[o + 2]) / sn) ** 2;
|
|
116
|
+
if (pitch !== null && pitch !== undefined) d += ((pitch - data[o]) / 6) ** 2;
|
|
117
|
+
if (voiced) d += (2 * (1 - data[o + 3])) ** 2;
|
|
118
|
+
d = Math.sqrt(d);
|
|
119
|
+
if (m < K || d < bd[m - 1]) {
|
|
120
|
+
let j = m < K ? m++ : K - 1;
|
|
121
|
+
while (j > 0 && bd[j - 1] > d) { bd[j] = bd[j - 1]; bi[j] = bi[j - 1]; j--; }
|
|
122
|
+
bd[j] = d; bi[j] = i;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const out = new Array(2 + nu).fill(0);
|
|
126
|
+
let W = 0;
|
|
127
|
+
for (let j = 0; j < m; j++) {
|
|
128
|
+
const wt = 1 / (bd[j] + 0.05), o = bi[j] * w;
|
|
129
|
+
W += wt; out[0] += wt * data[o + 4];
|
|
130
|
+
for (let q = 0; q < nu; q++) out[2 + q] += wt * data[o + 5 + q];
|
|
131
|
+
}
|
|
132
|
+
for (let q = 0; q < out.length; q++) out[q] /= W;
|
|
133
|
+
out[1] = bd[0];
|
|
134
|
+
return out;
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// what api/import-module gives the page (it takes a module's default export)
|
|
139
|
+
export default { learn, roles, index };
|