@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,276 @@
|
|
|
1
|
+
;; SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
;; Kairotic · boxes for musical trees (Study 19)
|
|
3
|
+
;; tree a tree in EDN (kairotic.mt), seen as a score or a roll, its text in the code window: a bang sends it
|
|
4
|
+
;; mt operations on trees: mt transpose 7 · mt invert c4 · mt retrograde · mt augment 2 ·
|
|
5
|
+
;; mt rotate 1 · mt canon 2 7 · mt thin 0.5 · mt seq · mt par · mt events · mt pitches · mt duration · mt play
|
|
6
|
+
;; A canvas, freqt or score that receives a tree shows it (kairotic.components/canvas-box).
|
|
7
|
+
|
|
8
|
+
(ns kairotic.mtboxes
|
|
9
|
+
(:require [clojure.string :as str]
|
|
10
|
+
[kairotic.components :as comp]
|
|
11
|
+
[kairotic.code :as cbox]
|
|
12
|
+
[kairotic.mt :as mt]
|
|
13
|
+
[kairotic.quantize :as qz]
|
|
14
|
+
[kairotic.treecanvas :as tc]))
|
|
15
|
+
|
|
16
|
+
;; ───────── tree: EDN data in the patch ─────────
|
|
17
|
+
;; Reading EDN evaluates nothing, so a tree box is safe in a shared room (no trust needed).
|
|
18
|
+
|
|
19
|
+
;; a canvas, freqt or score that shows a tree gives it as its buffer (a reader~ plays it): Study 23
|
|
20
|
+
(reset! comp/tree-buffer mt/->strokes)
|
|
21
|
+
|
|
22
|
+
(cbox/register-language! "edn"
|
|
23
|
+
(fn [source]
|
|
24
|
+
(js/Promise.
|
|
25
|
+
(fn [res rej]
|
|
26
|
+
(try
|
|
27
|
+
(let [t (mt/read-tree source) ps (when t (mt/errors t)) ws (when t (mt/warnings t))]
|
|
28
|
+
(cond (nil? t) (rej (js/Error. "empty"))
|
|
29
|
+
(seq ps) (rej (js/Error. (str (first ps) (when (> (count ps) 1) (str " (+" (dec (count ps)) " more)")))))
|
|
30
|
+
:else (res {:value (mt/expand t)
|
|
31
|
+
:info {:summary (str (mt/summary t) (when (seq ws) (str " · ⚠ " (first ws))))}})))
|
|
32
|
+
(catch :default e (rej e)))))))
|
|
33
|
+
|
|
34
|
+
;; The data of a tree box is the tree itself: {:tree t :source text}. Its history events are edits of
|
|
35
|
+
;; nodes ([:edit [[:attrs [0 1 2] {…}] …]], kairotic.mt/apply-op), so two branches that change different
|
|
36
|
+
;; measures can be merged; the text is only how it is shown (and edited: an edit of the text becomes
|
|
37
|
+
;; the node operations that make the difference, kairotic.mt/diff).
|
|
38
|
+
|
|
39
|
+
(defn- parse [text] (try (mt/read-tree text) (catch :default _ nil)))
|
|
40
|
+
|
|
41
|
+
(defn tree-of "The tree of a tree box's data (older works kept only its text)." [d]
|
|
42
|
+
(or (:tree d) (parse (:source d))))
|
|
43
|
+
|
|
44
|
+
(defn- with-tree [t] {:tree t :source (if (some? t) (mt/pp t) "")})
|
|
45
|
+
|
|
46
|
+
(defn step [d [op & args]]
|
|
47
|
+
(let [d (or d {:tree nil :source ""})]
|
|
48
|
+
(case op
|
|
49
|
+
:source (let [[text] args t (parse text)] (if (some? t) {:tree t :source (str text)} (assoc d :source (str text))))
|
|
50
|
+
:load (let [[x] args]
|
|
51
|
+
(cond (and (map? x) (contains? x :tree)) (with-tree (:tree x))
|
|
52
|
+
(and (map? x) (string? (:source x))) (step nil [:source (:source x)])
|
|
53
|
+
:else d))
|
|
54
|
+
:edit (let [[ops] args]
|
|
55
|
+
(try (with-tree (reduce mt/apply-op (tree-of d) ops))
|
|
56
|
+
(catch :default _ d)))
|
|
57
|
+
d)))
|
|
58
|
+
|
|
59
|
+
(def model
|
|
60
|
+
{:empty {:tree nil :source ""}
|
|
61
|
+
:step step
|
|
62
|
+
:load (fn [x] (step nil [:load x]))
|
|
63
|
+
;; in the Code tab, the tree as one call: (tree! :b2 [:voice …])
|
|
64
|
+
:code (fn [id d] (when-let [t (tree-of d)]
|
|
65
|
+
(concat [(str "(tree! " id)] (map (fn [l] (str " " l)) (str/split-lines (mt/pp t))) [")"])))})
|
|
66
|
+
|
|
67
|
+
(comp/define! "tree"
|
|
68
|
+
{:doc "A musical tree written in EDN (hiccup style: [:voice [:measure {:meter [4 4]} \"c4/1 e4 g4/2\"]]), seen as a score or as a roll of frequency over time (⇄ in its header), its text in the code window ({ } in its header) — one tree, two views (discussion #92): drawing on its roll writes notes into the tree (free time without measures, quantized bars with them). It sends its tree, expanded, whenever it changes (and again on a bang); `tree manual` only on a bang. Its second outlet is its notes as a buffer, for a reader~. On its inlet it takes edits [:edit [[:attrs path {…}] …]] (from a score's edits outlet) and whole trees, which change it."
|
|
69
|
+
:usage "tree"
|
|
70
|
+
;; two views (score, roll); its text opens in the code window from { }. Works that showed it as code
|
|
71
|
+
;; or as a canvas open it as a score or a roll
|
|
72
|
+
:display :score :views [:score :roll] :view-as {:roll :canvas} :view-aliases {:canvas :roll :code :score}
|
|
73
|
+
:language "edn" :model model
|
|
74
|
+
:io [1 2]
|
|
75
|
+
:template "[:voice {:name \"motif\"}\n [:measure {:meter [4 4]} \"c4/1 d4/.5 e4 g4/1 e4\"]\n [:measure \"f4/.5 e4 d4 c4 d4/2\"]]"
|
|
76
|
+
:receive (fn [m]
|
|
77
|
+
(let [t (tree-of (:data m)) v (:value m)]
|
|
78
|
+
(cond
|
|
79
|
+
;; edits and trees change the box: that is a gesture of the work, done by the page
|
|
80
|
+
(and (vector? v) (= :edit (first v))) {:effects [[:tree-edit (second v)]]}
|
|
81
|
+
;; its own tree come back (through mt boxes): a loop that would rewrite it for ever
|
|
82
|
+
(and (mt/node? v) (= (:from (meta v)) (:id m)))
|
|
83
|
+
{:effects [[:print (str "loop: " (name (:id m)) " received its own tree back; it does not change")]]}
|
|
84
|
+
(mt/node? v) {:effects [[:tree-set v]]}
|
|
85
|
+
(some? t)
|
|
86
|
+
(try {:out [[0 (mt/expand-source t (:id m))]] :show :bang}
|
|
87
|
+
(catch :default e {:effects [[:print (str "tree: " (.-message e))]]}))
|
|
88
|
+
:else {:effects [[:print "tree: empty, or text that does not read as EDN"]]})))})
|
|
89
|
+
|
|
90
|
+
;; ───────── quantize: free time written in measures, by hand ─────────
|
|
91
|
+
;; The material is the freqt on its right inlet (what transcribe or a drawing wrote), or a tree that came on the
|
|
92
|
+
;; left (free time, or measured: it is first made free again); the options are kept as they come; only a bang
|
|
93
|
+
;; quantizes and sends — a tree into a tree box is an event of the work's history, so nothing goes out while the
|
|
94
|
+
;; options are being tried.
|
|
95
|
+
|
|
96
|
+
(def quantize-keys #{:meter :meters :divs :merge :grace :chord :legato :voices :overlap :nest :key :tempo :time :ref-bpm :w :spell})
|
|
97
|
+
|
|
98
|
+
(defn quantize-report
|
|
99
|
+
"What a quantization found: its bars, its meter, its tempo, its notes and its voices."
|
|
100
|
+
[t]
|
|
101
|
+
(let [xt (mt/expand t)
|
|
102
|
+
ms (filter (fn [x] (and (mt/node? x) (= :measure (mt/tag x)))) (tree-seq mt/node? mt/kids xt))
|
|
103
|
+
vs (filter (fn [x] (and (mt/node? x) (= :voice (mt/tag x)))) (tree-seq mt/node? mt/kids xt))
|
|
104
|
+
m0 (some (fn [m] (:meter (mt/attrs m))) ms)]
|
|
105
|
+
{:bars (if (seq vs) (quot (count ms) (max 1 (count vs))) (count ms))
|
|
106
|
+
:meter (when m0 (str/join "/" m0))
|
|
107
|
+
:tempo (:tempo (mt/attrs xt))
|
|
108
|
+
:notes (count (filter (fn [e] (= :note (:kind e))) (mt/events xt)))
|
|
109
|
+
:voices (max 1 (count vs))}))
|
|
110
|
+
|
|
111
|
+
(comp/define! "quantize"
|
|
112
|
+
{:doc "Free time written in measures, when you ask: what transcribe or a drawing left in a freqt (connect the freqt's second outlet to its right inlet), or a tree in free time that comes on the left (a measured one is made free first), becomes a measured tree — measures, tuplets, ties, grace notes, voices —, for a tree box. Only a bang quantizes and sends it, so its options can be tried first: [:meter \"6/8\"], [:meter \"7/8[2+2+3]\"], [:meters [\"3/4\" \"5/8\"]], [:divs [1 2 3 4]] (the divisions a beat may take), [:merge []] (no beats joined into a tuplet), [:legato 0.25] (the largest gap that is not a rest, in beats), [:chord 0.04] (starts that close are one chord), [:voices 1], [:grace false], [:key \"eb\"], [:tempo 72], [:time :track] (find the beats of what was played freely, as track), or a map of them; [:meter nil] forgets one, :reset all of them. With the word track it finds the beats from the start. It is the quantization a freqt shown as a score uses (that is a preview); here it is a step of the patch, with its options in view, and two quantize boxes can write the same take two ways."
|
|
113
|
+
:usage "quantize [track]"
|
|
114
|
+
:io [2 2]
|
|
115
|
+
:reads-buffer true
|
|
116
|
+
:receive (fn [{:keys [inlet value state args buffer buffer-id]}]
|
|
117
|
+
(let [st (or state {:opts (if (some #{:track} args) {:time :track} {})})
|
|
118
|
+
show (fn [st extra] (merge {:opts (:opts st) :from (cond (:tree st) "a tree" buffer-id (name buffer-id) :else nil)} extra))]
|
|
119
|
+
(cond
|
|
120
|
+
;; the right inlet: the freqt is read when the bang comes (nothing to keep)
|
|
121
|
+
(= inlet 1) {}
|
|
122
|
+
;; an option, or several; nil forgets it
|
|
123
|
+
(and (vector? value) (= 2 (count value)) (quantize-keys (first value)))
|
|
124
|
+
(let [[k v] value st (if (nil? v) (update st :opts dissoc k) (assoc-in st [:opts k] v))] {:state st :show (show st nil)})
|
|
125
|
+
(and (map? value) (seq value) (every? quantize-keys (keys value)))
|
|
126
|
+
(let [st (update st :opts merge value)] {:state st :show (show st nil)})
|
|
127
|
+
(#{:reset} value) (let [st (assoc st :opts {})] {:state st :show (show st nil)})
|
|
128
|
+
;; a tree: the material, kept until the next bang
|
|
129
|
+
(mt/node? value) (let [st (assoc st :tree value)] {:state st :show (show st {:waiting true})})
|
|
130
|
+
(#{:clear} value) (let [st (dissoc st :tree)] {:state st :show (show st nil)})
|
|
131
|
+
;; a bang: quantized now, and sent
|
|
132
|
+
(= value :bang)
|
|
133
|
+
(let [t (:tree st)
|
|
134
|
+
free (fn [t] (if (mt/measured? t) (tc/free-form t) t))]
|
|
135
|
+
(try
|
|
136
|
+
(let [q (cond t (qz/quantize-tree (free t) (:opts st))
|
|
137
|
+
(and (map? buffer) (seq (:strokes buffer))) (qz/quantize buffer (merge (:quant buffer) (:opts st)))
|
|
138
|
+
:else nil)]
|
|
139
|
+
(if q
|
|
140
|
+
(let [r (quantize-report q)] {:state st :show (show st r) :out [[1 r] [0 q]]})
|
|
141
|
+
{:show (show st {:error "nothing to quantize: a freqt on the right, or a tree on the left"})}))
|
|
142
|
+
(catch :default e {:show (show st {:error (.-message e)})
|
|
143
|
+
:effects [[:print (str "quantize: " (.-message e))]]})))
|
|
144
|
+
:else {})))
|
|
145
|
+
:size [190 40]
|
|
146
|
+
:derive (fn [{:keys [shown]}]
|
|
147
|
+
{:main (str "quantize" (when-let [m (get-in shown [:opts :meter])] (str " · " m)) (when (= :track (get-in shown [:opts :time])) " · track"))
|
|
148
|
+
:sub (cond (:error shown) (str "✗ " (:error shown))
|
|
149
|
+
(:notes shown) (str (:bars shown) " bars of " (:meter shown) " · " (:notes shown) " notes" (when (:tempo shown) (str " · ♩ " (:tempo shown))))
|
|
150
|
+
(:waiting shown) "a tree is here: bang to quantize it"
|
|
151
|
+
:else "bang: quantize and send")})
|
|
152
|
+
:view [[:rect {:x 2 :y 2 :w 186 :h 36 :r 3 :stroke :grid :fill :bg}]
|
|
153
|
+
[:text {:x 10 :y 18 :size 12} :$main]
|
|
154
|
+
[:text {:x 10 :y 32 :size 9 :fill :soft} :$sub]]})
|
|
155
|
+
|
|
156
|
+
;; ───────── mt: operations ─────────
|
|
157
|
+
|
|
158
|
+
(defn op-of [args] (some-> (first args) name))
|
|
159
|
+
|
|
160
|
+
(def two-inlets #{"transpose" "invert" "augment" "rotate" "canon" "thin" "seq" "par"})
|
|
161
|
+
|
|
162
|
+
(defn num-arg [x d] (if (number? x) x d))
|
|
163
|
+
|
|
164
|
+
(defn transform
|
|
165
|
+
"What `mt <op> args` does to a tree (with the right inlet's value r, if it came)."
|
|
166
|
+
[op args r t]
|
|
167
|
+
(let [[a b] args]
|
|
168
|
+
(case op
|
|
169
|
+
"transpose" (mt/transpose (let [x (or r a)] (cond (number? x) x (or (symbol? x) (keyword? x) (string? x)) (name x) :else 0)) t)
|
|
170
|
+
"invert" (mt/invert (or r (when (some? a) (if (keyword? a) (name a) a))) t)
|
|
171
|
+
"retrograde" (mt/retrograde t)
|
|
172
|
+
"augment" (mt/augment (num-arg (or r a) 2) t)
|
|
173
|
+
"rotate" (mt/rotate (num-arg (or r a) 1) t)
|
|
174
|
+
"thin" (mt/thin (num-arg (or r a) 0.5) t)
|
|
175
|
+
;; mt canon 2 7 · or with the heads' words: mt canon d 2 k P5 speed 0.5 inversion
|
|
176
|
+
"canon" (if (some #{:d :k :speed :inversion} args)
|
|
177
|
+
(let [after (fn [w] (second (drop-while #(not= w %) args)))
|
|
178
|
+
k (after :k)]
|
|
179
|
+
(mt/canon {:d (num-arg (or r (after :d)) 1)
|
|
180
|
+
:k (cond (number? k) k (or (keyword? k) (symbol? k) (string? k)) (name k) :else 0)
|
|
181
|
+
:speed (num-arg (after :speed) 1) :inversion (boolean (some #{:inversion} args))}
|
|
182
|
+
t))
|
|
183
|
+
(mt/canon (num-arg (or r a) 1) (num-arg b 0) t))
|
|
184
|
+
"seq" [:seq (mt/expand t) (if (mt/tree? r) (mt/expand r) [:seq])]
|
|
185
|
+
"par" [:par (mt/expand t) (if (mt/tree? r) (mt/expand r) [:seq])]
|
|
186
|
+
"expand" (mt/expand t)
|
|
187
|
+
"events" (mt/events t)
|
|
188
|
+
"pitches" (mt/pitches t)
|
|
189
|
+
"duration" (mt/duration t)
|
|
190
|
+
(throw (js/Error. (str "mt: unknown operation " (pr-str op)))))))
|
|
191
|
+
|
|
192
|
+
(defn play-speed
|
|
193
|
+
"The speed of mt play: its words (mt play speed 0.5), or what a [:speed s] message set."
|
|
194
|
+
[st args]
|
|
195
|
+
(or (:speed st) (let [x (second (drop-while #(not= :speed %) args))] (when (and (number? x) (not (zero? x))) x)) 1))
|
|
196
|
+
|
|
197
|
+
(defn play-schedule
|
|
198
|
+
"What mt play plays at speed s: the times divided by |s|, and backwards (the tree's retrograde) when s < 0."
|
|
199
|
+
[tree s]
|
|
200
|
+
(let [t (if (neg? s) (mt/retrograde tree) tree) a (js/Math.abs s)]
|
|
201
|
+
{:sch (mapv (fn [[sec outs]] [(/ sec a) outs]) (mt/schedule t))
|
|
202
|
+
:len (/ (* (/ 60 (mt/tempo-of t)) (mt/duration t)) a)}))
|
|
203
|
+
|
|
204
|
+
(defn play [{:keys [state args inlet value at]}]
|
|
205
|
+
(let [st (or state {:gen 0})
|
|
206
|
+
tick? (and (vector? value) (= :tick (first value)))
|
|
207
|
+
loop? (some #{:loop} (rest args))]
|
|
208
|
+
(cond
|
|
209
|
+
tick?
|
|
210
|
+
(let [[_ g i] value sch (:sch st)]
|
|
211
|
+
(if (and (= g (:gen st)) sch (< i (count sch)))
|
|
212
|
+
(let [[_ outs] (nth sch i)
|
|
213
|
+
nxt (inc i)]
|
|
214
|
+
(cond
|
|
215
|
+
(< nxt (count sch)) {:state st :out outs :show i
|
|
216
|
+
:later [[(+ (:t0 st) (first (nth sch nxt))) 0 [:tick g nxt]]]}
|
|
217
|
+
loop? (let [t0 (+ (:t0 st) (:len st))]
|
|
218
|
+
{:state (assoc st :t0 t0) :out outs :later [[t0 0 [:tick g 0]]]})
|
|
219
|
+
:else {:state (assoc st :playing false) :out outs}))
|
|
220
|
+
{:state st}))
|
|
221
|
+
|
|
222
|
+
;; another speed (msg :speed 0.5, −1 backwards): from the next start, or now for what is playing
|
|
223
|
+
(and (vector? value) (= :speed (first value)) (number? (second value)) (not (zero? (second value))))
|
|
224
|
+
(let [st (assoc st :speed (second value))]
|
|
225
|
+
(if-let [t (:tree st)]
|
|
226
|
+
(let [{:keys [sch len]} (play-schedule t (second value)) st (assoc st :sch sch :len len)]
|
|
227
|
+
;; playing: it starts again, at the new speed
|
|
228
|
+
(if (and (:playing st) (seq sch))
|
|
229
|
+
(let [g (inc (:gen st))]
|
|
230
|
+
{:state (assoc st :gen g :t0 at) :later [[(+ at (first (first sch))) 0 [:tick g 0]]]})
|
|
231
|
+
{:state st}))
|
|
232
|
+
{:state st}))
|
|
233
|
+
|
|
234
|
+
(mt/tree? value)
|
|
235
|
+
(let [{:keys [sch len]} (play-schedule value (play-speed st args))]
|
|
236
|
+
(cond
|
|
237
|
+
;; the same music again (a tree box re-sends its tree when the patch around it changes, e.g. a
|
|
238
|
+
;; knob that keeps its value in its text): nothing changes, and what is playing goes on
|
|
239
|
+
(= sch (:sch st)) {:state st}
|
|
240
|
+
;; other music while playing: it goes on with the new one, from its beginning
|
|
241
|
+
(and (:playing st) (seq sch))
|
|
242
|
+
(let [g (inc (:gen st))]
|
|
243
|
+
{:state (assoc st :tree value :sch sch :len len :gen g :t0 at) :later [[(+ at (first (first sch))) 0 [:tick g 0]]]})
|
|
244
|
+
:else {:state (assoc st :tree value :sch sch :len len :gen (inc (:gen st)))}))
|
|
245
|
+
|
|
246
|
+
(= value :stop)
|
|
247
|
+
{:state (-> st (update :gen inc) (assoc :playing false)) :out [[1 []] [0 []]]}
|
|
248
|
+
|
|
249
|
+
(or (= value :bang) (= value :start) (number? value))
|
|
250
|
+
(if (seq (:sch st))
|
|
251
|
+
(let [g (inc (:gen st))]
|
|
252
|
+
{:state (assoc st :gen g :t0 at :playing true) :later [[(+ at (first (first (:sch st)))) 0 [:tick g 0]]]})
|
|
253
|
+
{:state st})
|
|
254
|
+
|
|
255
|
+
:else {:state st})))
|
|
256
|
+
|
|
257
|
+
(comp/define! "mt"
|
|
258
|
+
{:doc "Operations on musical trees: a tree on the left comes out transformed. transpose n (semitones, or an interval: M3, -P5, A4) · invert [pivot] · retrograde · augment f · rotate k · canon delay [interval], or with the words of a reader on a ring: canon d 2 k P5 speed 0.5 inversion (the comes d beats later, k away, at a speed — 0.5 twice as slow, −1 backwards —, upside down) · thin amount (keeps the notes on the strongest positions of each measure: a share, 0.5, or how many, 3) · seq / par (joins the left tree with the one on the right) · events · pitches · duration · play [loop] [speed s] (a bang plays the last tree: Hz and voice ids for synth~, parameters and marks; at a speed, 0.5 twice as slow, −1 the tree backwards — msg :speed −1 —: the reader of a tree on its own clock, as a reader on a ring follows its head)."
|
|
259
|
+
:usage "mt <operation> [arguments]"
|
|
260
|
+
:io (fn [b] (let [op (op-of (:args b))]
|
|
261
|
+
(cond (= op "play") [1 4]
|
|
262
|
+
(two-inlets op) [2 1]
|
|
263
|
+
:else [1 1])))
|
|
264
|
+
:dynamic-io true
|
|
265
|
+
:receive (fn [{:keys [state args inlet value] :as m}]
|
|
266
|
+
(let [op (op-of args)]
|
|
267
|
+
(cond
|
|
268
|
+
(= op "play") (play m)
|
|
269
|
+
(= inlet 1) {:state (assoc state :r value)}
|
|
270
|
+
(mt/tree? value)
|
|
271
|
+
(try {:state state
|
|
272
|
+
;; the result remembers which tree box the tree comes from (a loop back to it is seen)
|
|
273
|
+
:out [[0 (let [x (transform op (rest args) (:r state) value)]
|
|
274
|
+
(if (and (mt/node? x) (:from (meta value))) (vary-meta x assoc :from (:from (meta value))) x))]]}
|
|
275
|
+
(catch :default e {:state state :effects [[:print (str "mt " op ": " (.-message e))]]}))
|
|
276
|
+
:else {:state state})))})
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Generated by packages/build.mjs
|
|
2
|
+
export default {
|
|
3
|
+
"package": "@kairotic/analysis",
|
|
4
|
+
"version": "0.7.5",
|
|
5
|
+
"api": 1,
|
|
6
|
+
"author": "bonuoq",
|
|
7
|
+
"license": "MPL-2.0",
|
|
8
|
+
"components": [
|
|
9
|
+
{
|
|
10
|
+
"class": "chroma~",
|
|
11
|
+
"license": "MPL-2.0",
|
|
12
|
+
"doc": "How much of each pitch class the signal on its left has, every ms (40 by default): a list of 12 values from C to B (0…1, the strongest 1) on the left, and the strongest class as a word (:c, :c#, … :b) on the right when it changes. Silence sends nothing. Chords, keys and drones become data for mt and the patch. Computed with Meyda.",
|
|
13
|
+
"source": ";; chroma~ · how much of each pitch class a sound has\n;; Part of @kairotic/analysis. Written against the Kairotic API (api/…). The chroma is Meyda's (MIT,\n;; meyda.js.org), loaded from jsDelivr the first time one is needed.\n(let [meyda-url \"npm:meyda@5.6.3/dist/web/meyda.min.js\"\n size 2048 ; the frames every analysis box of the package uses (see features~)\n gate 0.005 ; rms under it is silence: no strongest class\n classes [:c :c# :d :d# :e :f :f# :g :g# :a :a# :b]\n names [\"C\" \"C#\" \"D\" \"D#\" \"E\" \"F\" \"F#\" \"G\" \"G#\" \"A\" \"A#\" \"B\"]\n strongest (fn [xs] (when (seq xs) (nth classes (first (apply max-key (fn [[_ v]] v) (map-indexed vector xs))))))\n ;; a picture of 12 bars in 168 × 64\n bar-w 13 x0 6 base 46 tall 38]\n {;; ── about ──\n :class \"chroma~\"\n :license \"MPL-2.0\"\n :doc \"How much of each pitch class the signal on its left has, every ms (40 by default): a list of 12 values from C to B (0…1, the strongest 1) on the left, and the strongest class as a word (:c, :c#, … :b) on the right when it changes. Silence sends nothing. Chords, keys and drones become data for mt and the patch. Computed with Meyda.\"\n :usage \"chroma~ [ms]\"\n :io [1 2]\n :size [168 64]\n :args [[:ms 40]]\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state]}]\n (cond\n (and (= inlet :audio) (:error value)) {:effects [[:print (:error value)]]}\n (and (= inlet :audio) (map? value) (> (or (:rms value) 0) gate) (= 12 (count (:chroma value))))\n (let [xs (:chroma value) top (strongest xs)\n outs (cond-> []\n (not= top (:top state)) (conj [1 top])\n (not= xs (:xs state)) (conj [0 xs]))]\n (if (seq outs) {:state {:xs xs :top top} :out outs :show xs} {}))\n :else {}))\n\n ;; ── view ──\n :draw (fn [{:keys [shown]}]\n (let [xs (if (and (sequential? shown) (= 12 (count shown))) shown (repeat 12 0))\n top (when (some pos? xs) (apply max xs))]\n (into [[:rect {:x 2 :y 2 :w 164 :h 60 :r 4 :stroke :grid :fill :bg}]]\n (mapcat (fn [i v n]\n (let [h (* tall (api/clamp v 0 1)) x (+ x0 (* i bar-w))]\n [[:rect {:x x :y (- base h) :w (- bar-w 3) :h h :r 1 :stroke nil\n :fill (if (and top (= v top)) :kairos :accent)}]\n [:text {:x (+ x (/ (- bar-w 3) 2)) :y 58 :anchor :middle :size 7 :fill :soft} n]]))\n (range 12) xs names))))\n\n ;; ── audio ──\n :audio {:create (fn [ac b _]\n (let [in (.createGain ac)\n sr (.-sampleRate ac)\n ms (api/clamp (or (first (filter number? (:args b))) 40) 10 2000)\n node {:in in :M (atom nil) :tap (atom nil) :alive (atom true) :inlets [[in 0]] :outlets []}\n on-block (fn [data at & _]\n ;; only while the box is here: a tap left behind would hand it silence\n (when-let [M (and @(:alive node) @(:M node))]\n (set! (.-bufferSize M) size)\n (set! (.-sampleRate M) sr)\n (let [r (.extract M #js [\"chroma\" \"rms\"] (first data))\n xs (mapv (fn [x] (/ (js/Math.round (* 100 (if (js/isFinite x) x 0))) 100))\n (array-seq (.-chroma r)))]\n (api/report! (:id b) {:chroma xs :rms (.-rms r)} at))))]\n (-> (api/load-script meyda-url)\n (.then (fn [_] (reset! (:M node) (.-Meyda js/window))))\n (.catch (fn [e] (api/report! (:id b) {:error (str \"chroma~: \" (.-message e))} nil))))\n (-> (api/tap ac {:mode :frames :size size :hop (js/Math.round (* sr (/ ms 1000)))} on-block)\n (.then (fn [w] (if @(:alive node) (do (.connect in w) (reset! (:tap node) w)) (api/untap w))))\n (.catch (fn [e] (js/console.error \"chroma~\" e))))\n node))\n :dispose (fn [node]\n (reset! (:alive node) false)\n (api/untap @(:tap node))\n (try (.disconnect (:in node)) (catch :default _ nil)))}})\n"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"class": "features~",
|
|
17
|
+
"license": "MPL-2.0",
|
|
18
|
+
"doc": "Descriptors of the signal on its left, every ms (20 by default), one outlet for each word written after it, in order: rms, energy, zcr, loudness, centroid and spread (in Hz), flatness, rolloff (Hz), kurtosis, skewness, slope, crest, sharpness, perceptualSpread, mfcc (13 values) and chroma (12). Without words: rms centroid flatness. Each outlet sends only what changed. Computed with Meyda.",
|
|
19
|
+
"source": ";; features~ · descriptors of a sound, every few milliseconds\n;; Part of @kairotic/analysis. Written against the Kairotic API (api/…). The descriptors are Meyda's\n;; (MIT, meyda.js.org), loaded from jsDelivr the first time one is needed.\n(let [meyda-url \"npm:meyda@5.6.3/dist/web/meyda.min.js\"\n ;; every analysis box of the package uses frames of this size: Meyda is one object for the whole\n ;; page and keeps some filter banks for the size it saw first\n size 2048\n aliases {:centroid :spectralCentroid :flatness :spectralFlatness :rolloff :spectralRolloff\n :spread :spectralSpread :kurtosis :spectralKurtosis :skewness :spectralSkewness\n :slope :spectralSlope :crest :spectralCrest :sharpness :perceptualSharpness}\n known #{:rms :energy :zcr :loudness :mfcc :chroma :spectralCentroid :spectralFlatness :spectralRolloff\n :spectralSpread :spectralKurtosis :spectralSkewness :spectralSlope :spectralCrest\n :perceptualSharpness :perceptualSpread}\n feats (fn [args]\n (let [ws (->> args (filter keyword?) (map (fn [k] (get aliases k k))) (filter known) distinct vec)]\n (if (seq ws) ws [:rms :spectralCentroid :spectralFlatness])))\n hop-ms (fn [args] (api/clamp (or (first (filter number? args)) 20) 5 1000))\n ok? (fn [x] (and (number? x) (js/isFinite x)))\n ;; a value as it leaves the box: Hz where Meyda gives bins, the total of loudness, lists rounded\n value (fn [k v sr]\n (cond\n (#{:spectralCentroid :spectralSpread} k) (when (ok? v) (api/nice (* v (/ sr size))))\n (= k :loudness) (when v (let [t (.-total v)] (when (ok? t) (api/nice t))))\n (#{:mfcc :chroma} k) (when v (let [xs (vec (array-seq v))] (when (every? ok? xs) (mapv api/nice xs))))\n :else (when (ok? v) (api/nice v))))]\n {;; ── about ──\n :class \"features~\"\n :license \"MPL-2.0\"\n :doc \"Descriptors of the signal on its left, every ms (20 by default), one outlet for each word written after it, in order: rms, energy, zcr, loudness, centroid and spread (in Hz), flatness, rolloff (Hz), kurtosis, skewness, slope, crest, sharpness, perceptualSpread, mfcc (13 values) and chroma (12). Without words: rms centroid flatness. Each outlet sends only what changed. Computed with Meyda.\"\n :usage \"features~ [ms] [descriptor …]\"\n :io (fn [b] [1 (count (feats (:args b)))])\n :dynamic-io true\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state args]}]\n (cond\n (and (= inlet :audio) (:error value)) {:effects [[:print (:error value)]]}\n (and (= inlet :audio) (map? value))\n (let [fs (feats args)\n outs (vec (keep-indexed (fn [i k] (let [v (get value k)]\n (when (and (some? v) (not= v (get state k))) [i v])))\n fs))]\n (if (seq outs) {:state (merge state (select-keys value fs)) :out outs} {}))\n :else {}))\n\n ;; ── audio ──\n :audio {:create (fn [ac b _]\n (let [in (.createGain ac)\n sr (.-sampleRate ac)\n fs (feats (:args b))\n names (clj->js (mapv name fs))\n node {:in in :M (atom nil) :tap (atom nil) :alive (atom true) :inlets [[in 0]] :outlets []}\n on-block (fn [data at & _]\n ;; only while the box is here: a tap left behind would hand it silence\n (when-let [M (and @(:alive node) @(:M node))]\n (set! (.-bufferSize M) size)\n (set! (.-sampleRate M) sr)\n (let [r (.extract M names (first data))]\n (api/report! (:id b) (into {} (for [k fs] [k (value k (aget r (name k)) sr)])) at))))]\n (-> (api/load-script meyda-url)\n (.then (fn [_] (reset! (:M node) (.-Meyda js/window))))\n (.catch (fn [e] (api/report! (:id b) {:error (str \"features~: \" (.-message e))} nil))))\n (-> (api/tap ac {:mode :frames :size size :hop (js/Math.round (* sr (/ (hop-ms (:args b)) 1000)))} on-block)\n (.then (fn [w] (if @(:alive node) (do (.connect in w) (reset! (:tap node) w))\n (try (.disconnect w) (catch :default _ nil)))))\n (.catch (fn [e] (js/console.error \"features~\" e))))\n node))\n :dispose (fn [node]\n (reset! (:alive node) false)\n (api/untap @(:tap node))\n (try (.disconnect (:in node)) (catch :default _ nil)))}})\n"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"class": "form~",
|
|
23
|
+
"license": "MPL-2.0",
|
|
24
|
+
"doc": "How alike the moments of what arrives are, over the last seconds (20): a self-similarity matrix, drawn, of one frame every step seconds (0.25) — the mean of the lists that arrived in it (a chroma~, a features~ mfcc), or, for a stream of numbers (onset~'s third outlet, an rms), the shape of its values. On the left the novelty of each frame (0…1: how much the music changes there, by a checkerboard of kernel seconds (2) on each side, so it comes kernel seconds late); a bang on the second outlet where a segment begins (a novelty peak high for the window, two kernels after the last at least), and, on the fourth, [:segment {:t :at :length :period}] for the segment it ends; on the third, the period that repeats most in the window, in seconds (the bar, the phrase), when it changes. [:seconds x], [:step x] and [:kernel x] change them, :clear forgets. Into onset~ (a [:segment …] makes it forget the usual change of the section before: adaptive onsets), a message box with [:threshold x], or a print. Self-similarity after Foote (1999, 2000, 2001); the same functions for a whole take or a tree are api/ssm, api/novelty, api/boundaries and api/note-frames.",
|
|
25
|
+
"source": ";; form~ · how alike the moments are: a self-similarity matrix over the last seconds, its changes, its periods\n;; Part of @kairotic/analysis. Written against the Kairotic API (api/…): the matrix, novelty and beat spectrum\n;; are those of the core (kairotic.structure, issue #120), through api/ssm, api/novelty-at, api/beat-spectrum.\n;; What arrives (a chroma, descriptors, an onset envelope) is gathered into one frame every step seconds; each\n;; new frame is compared with all those of the window. A segment begins where the frames before are alike\n;; among themselves and unlike the ones after (Foote 2000), and the lag at which frames are most alike is the\n;; period that repeats (Foote & Uchihashi 2001).\n(let [G 20 ; the matrix drawn: G × G cells at most (the patch draws at most 600 shapes)\n bins 8 ; a stream of numbers: each frame is the shape of its step, in 8 slices\n settings (fn [args state]\n (let [o (:set state)\n seconds (api/clamp (or (:seconds o) (:seconds args) 20) 2 120)\n step (api/clamp (or (:step o) (:step args) 0.25) 0.02 4)\n kernel (api/clamp (or (:kernel o) (:kernel args) 2) (* 2 step) (/ seconds 3))]\n {:seconds seconds :step step :kernel kernel\n :L (max 1 (js/Math.round (/ kernel step)))\n :N (max 4 (js/Math.round (/ seconds step)))}))\n frame-of (fn [xs]\n ;; vectors: their mean; numbers: the values of the step, resampled into bins slices\n (if (sequential? (first xs))\n (let [d (reduce max 0 (map count xs))]\n (mapv (fn [j] (/ (reduce + (map (fn [v] (or (get v j) 0)) xs)) (count xs))) (range d)))\n (let [v (vec xs) n (count v)]\n (mapv (fn [j] (let [a (js/Math.floor (/ (* j n) bins)) b (max (inc a) (js/Math.floor (/ (* (inc j) n) bins)))\n s (subvec v (min a (dec n)) (min b n))]\n (/ (reduce + s) (max 1 (count s)))))\n (range bins)))))\n usable (fn [v] (cond (number? v) v\n (and (sequential? v) (seq v) (every? number? v)) (vec v)\n :else nil))\n ;; the matrix drawn: the mean of each block of frames\n cells (fn [S]\n (let [n (:n S) g (min G n) blk (fn [k] [(js/Math.floor (/ (* k n) g)) (js/Math.floor (/ (* (inc k) n) g))])]\n (vec (for [r (range g)]\n (let [[a b] (blk r)]\n (vec (for [c (range g)]\n (let [[x y] (blk c) cnt (* (- b a) (- y x))]\n (/ (reduce + (for [i (range a b) j (range x y)] (api/ssm-at S i j))) (max 1 cnt))))))))))\n mean-sd (fn [xs] (let [n (max 1 (count xs)) m (/ (reduce + xs) n)]\n [m (js/Math.sqrt (/ (reduce + (map (fn [x] (* (- x m) (- x m))) xs)) n))]))\n r3 (fn [x] (/ (js/Math.round (* 1000 x)) 1000))\n ;; a frame is complete: into the window, and what it tells\n close (fn [state {:keys [N L step kernel]}]\n (let [f (frame-of (:acc state))\n t (+ (:t0 state) (/ step 2))\n frames (vec (take-last N (conj (or (:frames state) []) [t f])))\n S (api/ssm (mapv second frames))\n n (:n S)\n i (- n 1 L)\n nov (when (>= i L) (api/novelty-at S L i))\n nt (when nov (first (frames i)))\n hist (if nov (vec (take-last N (conj (or (:nov state) []) [nt (max 0 nov)]))) (or (:nov state) []))\n ;; a boundary: the novelty of the frame before is a peak, high for this window, and the last\n ;; boundary is at least two kernels away\n vs (mapv second hist) k (count vs)\n [m sd] (mean-sd vs)\n peak (when (>= k 3)\n (let [[tp p] (hist (- k 2))]\n (when (and (> p (vs (- k 3))) (>= p (vs (dec k))) (> p (max 0.05 (+ m sd)))\n (or (nil? (:last state)) (>= (- tp (:last state)) (* 2 kernel))))\n tp)))\n bs (when (>= n 8) (api/beat-spectrum S (quot n 2)))\n ;; a period is a lag more alike than the lags before it (a real dip between): music that does\n ;; not change is alike at every lag, and has none\n lag (some (fn [[l x]] (when (and (>= l 2) (> (- x (reduce min (subvec bs 1 l))) 0.1)) l))\n (when bs (api/periods bs)))\n period (when lag (r3 (* lag step)))\n s (cond-> (assoc state :frames frames :nov hist\n :cells (cells S) :period (or period (:period state)))\n peak (assoc :last peak :marks (vec (take-last 8 (conj (or (:marks state) []) peak)))))]\n {:state s\n :out (cond-> []\n nov (conj [0 (r3 (max 0 nov))])\n (and period (not= period (:period state))) (conj [2 period])\n peak (conj [3 [:segment {:t (or (:last state) (first (first frames))) :at peak\n :length (r3 (- peak (or (:last state) (first (first frames)))))\n :period (or period (:period state))}]]\n [1 :bang]))}))]\n {;; ── about ──\n :class \"form~\"\n :license \"MPL-2.0\"\n :doc \"How alike the moments of what arrives are, over the last seconds (20): a self-similarity matrix, drawn, of one frame every step seconds (0.25) — the mean of the lists that arrived in it (a chroma~, a features~ mfcc), or, for a stream of numbers (onset~'s third outlet, an rms), the shape of its values. On the left the novelty of each frame (0…1: how much the music changes there, by a checkerboard of kernel seconds (2) on each side, so it comes kernel seconds late); a bang on the second outlet where a segment begins (a novelty peak high for the window, two kernels after the last at least), and, on the fourth, [:segment {:t :at :length :period}] for the segment it ends; on the third, the period that repeats most in the window, in seconds (the bar, the phrase), when it changes. [:seconds x], [:step x] and [:kernel x] change them, :clear forgets. Into onset~ (a [:segment …] makes it forget the usual change of the section before: adaptive onsets), a message box with [:threshold x], or a print. Self-similarity after Foote (1999, 2000, 2001); the same functions for a whole take or a tree are api/ssm, api/novelty, api/boundaries and api/note-frames.\"\n :usage \"form~ [seconds] [step] [kernel]\"\n :io [1 4]\n :size [220 128]\n :args [[:seconds 20] [:step 0.25] [:kernel 2]]\n\n ;; ── logic ──\n ;; the state: {:frames [[t vector] …] :acc [values of the frame under way] :t0 its start :nov [[t value] …]\n ;; :last the last boundary :marks boundaries :cells the matrix drawn :period :set what messages changed}\n :receive (fn [{:keys [value state args at]}]\n (cond\n (= value :clear) {:state (select-keys state [:set]) :show nil}\n (and (vector? value) (= 2 (count value)) (#{:seconds :step :kernel} (first value)) (number? (second value)))\n (let [s (-> (select-keys state [:set]) (assoc-in [:set (first value)] (second value)))] {:state s :show nil})\n (and (usable value) (number? at))\n (let [o (settings args state)\n ;; the engine started again (its clock went back), or the kind of what arrives changed: from nothing\n state (if (or (and (:t0 state) (< at (- (:t0 state) 1)))\n (and (seq (:acc state)) (not= (number? (first (:acc state))) (number? (usable value)))))\n (select-keys state [:set]) state)\n v (usable value)]\n ;; a value a step after the frame under way began closes it, and begins the next\n (if (and (:t0 state) (seq (:acc state)) (>= (- at (:t0 state)) (:step o)))\n (let [{s :state out :out} (close state o)\n s (assoc s :acc [v] :t0 at)]\n (cond-> {:state s :show (select-keys s [:cells :nov :marks :period :frames])}\n (seq out) (assoc :out out)))\n {:state (cond-> (update state :acc (fnil conj []) v) (nil? (:t0 state)) (assoc :t0 at))}))\n :else {}))\n\n ;; ── view: the matrix (time runs down and to the right), the novelty under it, the period ──\n :draw (fn [{:keys [shown args state w h]}]\n (let [{:keys [step kernel seconds]} (settings args state)\n cs (:cells shown) g (count cs)\n side (- h 12) x0 6 y0 6 cw (if (pos? g) (/ side g) side)\n frames (:frames shown)\n t0 (first (first frames)) t1 (first (peek frames))\n px0 (+ x0 side 10) px1 (- w 6) pw (- px1 px0)\n tx (fn [t] (if (and t0 t1 (> t1 t0)) (+ px0 (* pw (/ (- t t0) (- t1 t0)))) px0))\n ny0 (+ y0 34) ny1 (- h 8)\n nov (filter (fn [[t]] (and t0 (>= t t0))) (:nov shown))\n ty (fn [v] (- ny1 (* (- ny1 ny0) (api/clamp v 0 1))))]\n (into\n [[:rect {:x 2 :y 2 :w (- w 4) :h (- h 4) :r 4 :stroke :grid :fill :bg}]\n [:rect {:x x0 :y y0 :w side :h side :stroke :grid :fill :bg}]\n [:text {:x px0 :y (+ y0 10) :size 10 :fill :ink} \"form\"]\n [:text {:x px0 :y (+ y0 23) :size 8 :fill :soft}\n (if-let [p (:period shown)] (str \"period \" (api/nice p) \" s\") (str (api/nice seconds) \" s · \" (api/nice step) \" · \" (api/nice kernel)))]\n [:line {:x1 px0 :y1 ny1 :x2 px1 :y2 ny1 :stroke :grid :weight 1}]]\n (concat\n (for [r (range g) c (range g) :let [v ((cs r) c)] :when (> v 0.1)]\n [:rect {:x (+ x0 (* c cw)) :y (+ y0 (* r cw)) :w (+ cw 0.3) :h (+ cw 0.3)\n :fill :accent :stroke nil :opacity (api/clamp (* v v) 0 1)}])\n (let [xys (mapv (fn [[t v]] [(api/clamp (tx t) px0 px1) (ty v)]) nov)]\n (when (> (count xys) 1) [[:path {:points xys :stroke :accent :weight 1.2}]]))\n (for [t (:marks shown) :when (and t0 (>= t t0))]\n [:line {:x1 (tx t) :y1 ny0 :x2 (tx t) :y2 ny1 :stroke :kairos :weight 1.2}])))))})\n"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"class": "monitor",
|
|
29
|
+
"license": "MPL-2.0",
|
|
30
|
+
"doc": "Draws the messages that arrive over the last seconds (6 by default), one lane per inlet: a lane for each word written after the seconds, which names it (monitor 6 rms centroid chroma has 3 inlets), or one lane without words. A number is drawn as a line in the range it has had in the window, a list of numbers as a strip with one row per element (chroma, mfcc), anything else (a bang, a word) as a mark. Time is that of the messages: the right edge is the last one. Sends nothing.",
|
|
31
|
+
"source": ";; monitor · the messages that arrive, drawn over the last seconds\n;; Part of @kairotic/analysis. Written against the Kairotic API (api/…).\n;; One lane per inlet. What a lane draws depends on what arrives: a number is a line in the range it has\n;; had in the window, a list of numbers is a strip of cells (one row per element: chroma, mfcc…), and\n;; anything else (a bang, a word) is a mark. Time is that of the messages: the right edge is the last one.\n(let [L 56 ; the column of names and values, on the left\n budget 360 ; cells for all the strips of a box (the patch draws at most 600 shapes)\n seconds (fn [args] (api/clamp (or (first (filter number? args)) 6) 0.5 120))\n names (fn [args] (vec (filter keyword? args)))\n lanes (fn [args] (let [ns (names args)] (if (seq ns) (count ns) 1)))\n kind (fn [v] (cond (number? v) :line\n (and (sequential? v) (seq v) (every? number? v)) :cells\n :else :mark))\n label (fn [v] (cond (number? v) (str (api/nice v))\n (sequential? v) (str \"[\" (count v) \"]\")\n (= v :bang) \"bang\"\n (keyword? v) (name v)\n :else (let [s (str v)] (if (> (count s) 10) (str (subs s 0 9) \"…\") s))))\n ;; keeps a value: a line keeps about 150 points in the window, a strip about 48, marks all\n ;; (64 at most); what left the window goes, but one point before it stays for the line to enter\n keep-in (fn [pts at v win]\n (let [k (kind v) [t-last v-last] (peek pts)\n step (case k :line (/ win 150) :cells (/ win 48) 0)\n pts (if (and t-last (= k (kind v-last)) (pos? step) (< (- at t-last) step))\n (conj (pop pts) [t-last v])\n (conj pts [at v]))\n pts (if (and (= k :mark) (> (count pts) 64)) (subvec pts (- (count pts) 64)) pts)\n old (count (take-while (fn [[t]] (< t (- at win))) pts))]\n (if (> old 1) (subvec pts (dec old)) pts)))]\n {;; ── about ──\n :class \"monitor\"\n :license \"MPL-2.0\"\n :doc \"Draws the messages that arrive over the last seconds (6 by default), one lane per inlet: a lane for each word written after the seconds, which names it (monitor 6 rms centroid chroma has 3 inlets), or one lane without words. A number is drawn as a line in the range it has had in the window, a list of numbers as a strip with one row per element (chroma, mfcc), anything else (a bang, a word) as a mark. Time is that of the messages: the right edge is the last one. Sends nothing.\"\n :usage \"monitor [seconds] [name …]\"\n :io (fn [b] [(lanes (:args b)) 0])\n :dynamic-io true\n :size (fn [b] [300 (+ 8 (* 44 (lanes (:args b))))])\n\n ;; ── logic ──\n ;; the state is what is drawn: {:t last instant :lanes {inlet [[at value] …]}}\n :receive (fn [{:keys [inlet value state args at]}]\n (if (and (integer? inlet) (some? value) (number? at))\n (let [win (seconds args)\n ;; the engine started again (its clock went back): start from nothing\n state (if (and (:t state) (< at (- (:t state) 1))) nil state)\n s (-> (or state {:lanes {}})\n (assoc :t (max at (or (:t state) at)))\n (update-in [:lanes inlet] (fn [pts] (keep-in (or pts []) at value win))))]\n {:state s :show s})\n {}))\n\n ;; ── view ──\n :draw (fn [{:keys [shown args w h]}]\n (let [win (seconds args) n (lanes args) ns (names args)\n lh (/ (- h 8) n) x0 L x1 (- w 6) pw (- x1 x0)\n t1 (or (:t shown) 0) t0 (- t1 win)\n tx (fn [at] (+ x0 (* pw (/ (- at t0) win))))\n strips (max 1 (count (filter (fn [i] (= :cells (kind (second (peek (get-in shown [:lanes i])))))) (range n))))]\n (into\n [[:rect {:x 2 :y 2 :w (- w 4) :h (- h 4) :r 4 :stroke :grid :fill :bg}]]\n (mapcat\n (fn [i]\n (let [pts (get-in shown [:lanes i] []) top (+ 4 (* i lh)) bottom (+ top lh -4)\n last-v (second (peek pts)) k (kind last-v)\n title (if-let [nm (get ns i)] (name nm) (str \"in \" i))]\n (concat\n [[:text {:x 8 :y (+ top 12) :size 9 :fill :ink} title]\n (when (some? last-v) [:text {:x 8 :y (+ top 24) :size 8 :fill :soft} (label last-v)])\n (when (pos? i) [:line {:x1 6 :y1 (- top 2) :x2 x1 :y2 (- top 2) :stroke :grid :weight 1}])]\n (case k\n :line\n (let [ps (filter (fn [[_ v]] (number? v)) pts)\n vs (map second ps)\n lo (if (seq vs) (apply min vs) 0) hi (if (seq vs) (apply max vs) 1)\n [lo hi] (if (< (- hi lo) 1e-9) [(- lo 0.5) (+ hi 0.5)] [lo hi])\n ty (fn [v] (- bottom (* (- bottom top 2) (/ (- v lo) (- hi lo)))))\n xys (mapv (fn [[at v]] [(api/clamp (tx at) x0 x1) (ty v)]) ps)]\n [[:text {:x 8 :y (+ top 35) :size 7 :fill :soft} (str (api/nice lo) \"…\" (api/nice hi))]\n (when (> (count xys) 1) [:path {:points xys :stroke :accent :weight 1.4}])\n (when-let [[x y] (peek xys)] [:circle {:cx x :cy y :r 2.4 :fill :accent :stroke nil}])])\n :cells\n (let [ps (filterv (fn [[at v]] (and (>= at t0) (= :cells (kind v)))) pts)\n rows (reduce max 1 (map (fn [[_ v]] (count v)) ps))\n cols (max 8 (min 48 (js/Math.floor (/ budget strips rows))))\n cw (/ pw cols) ch (/ (- bottom top) rows)\n ;; each column: the mean of what arrived in its slice of time (or the last value before)\n col-of (fn [at] (api/clamp (js/Math.floor (/ (- at t0) (/ win cols))) 0 (dec cols)))\n buckets (reduce (fn [m [at v]] (update m (col-of at) (fnil conj []) v)) {} ps)\n mean (fn [vs] (mapv (fn [j] (/ (reduce + (map (fn [v] (or (get v j) 0)) vs)) (count vs))) (range rows)))\n cells (into {} (for [[c vs] buckets] [c (mean vs)]))\n all (mapcat identity (vals cells))\n lo (if (seq all) (apply min all) 0) hi (if (seq all) (apply max all) 1)\n span (max 1e-9 (- hi lo))]\n (for [[c vs] cells [j v] (map-indexed vector vs)\n :let [u (/ (- v lo) span)] :when (> u 0.05)]\n [:rect {:x (+ x0 (* c cw)) :y (+ top (* (- rows j 1) ch)) :w (+ cw 0.4) :h (max 0.6 (- ch 0.6))\n :fill :accent :stroke nil :opacity (api/clamp u 0 1)}]))\n :mark\n (mapcat (fn [[at v]]\n (let [x (tx at)]\n (when (>= x x0)\n [[:line {:x1 x :y1 (+ top 2) :x2 x :y2 bottom :stroke :kairos :weight 1.4}]\n (when-not (= v :bang) [:text {:x (+ x 3) :y (+ top 12) :size 8 :fill :kairos} (label v)])])))\n pts)))))\n (range n)))))})\n"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"class": "onset~",
|
|
35
|
+
"license": "MPL-2.0",
|
|
36
|
+
"doc": "The beginnings of sounds in the signal on its left — a note, a stroke, a syllable —: a bang on the left for each, and how strong it was (the growth of its spectrum, 0…1) on the second outlet; on the third, that growth for every frame (the onset strength envelope, for beat tracking: issue #106). threshold (3) is how far above the usual change of the last frames an onset must rise, while the energy does not fall (a sound cut at once is not an onset); ms (80) the least time between two. Every 512 samples (about 11 ms), on frames of 2048. An onset is reported with the frame it is first seen in: measured on sines, plucked tones and noise bursts that start at a known sample, 6 ms after it as a median (0.7 to 10.7 ms: one hop), and 11 ms for a new note over a held one. [:threshold x] and [:ms y] change them while it listens, and a [:segment …] from form~ makes it forget the usual change of the section before (so a quiet section after a loud one is not deaf). Into a counter, a metro that follows, a sample's rec, or the right inlet of transcribe (its strength, as the weight of the note).",
|
|
37
|
+
"source": ";; onset~ · the beginnings of sounds: a note, a stroke, a syllable\n;; Part of @kairotic/analysis. Written against the Kairotic API (api/…). The spectrum is Meyda's (MIT,\n;; meyda.js.org), loaded from jsDelivr the first time one is needed.\n(let [meyda-url \"npm:meyda@5.6.3/dist/web/meyda.min.js\"\n size 2048 ; the frames every analysis box of the package uses (see features~)\n bins 256 ; the spectrum up to about 6 kHz at 48 kHz: where onsets are heard\n ;; how much the spectrum grew since the frame before, relative to what sounds now (0 steady … 1\n ;; from silence): what grows counts, what fades does not\n flux (fn [cur prev]\n (if-not prev\n 0\n (loop [i 1 up 0 all 0]\n (if (< i bins)\n (let [a (aget cur i) d (- a (aget prev i))]\n (recur (inc i) (if (pos? d) (+ up d) up) (+ all a)))\n (if (> all 1e-6) (/ up all) 0)))))\n median (fn [xs] (if (empty? xs) 0 (nth (sort xs) (quot (count xs) 2))))\n gate 0.005 ; rms under it (about -46 dB) is silence\n ;; the decision, pure: an onset where the flux rises above threshold × the median of the last\n ;; frames (and above 0.1) while the energy does not fall (a sound cut at once spreads its spectrum like an\n ;; attack, but its energy goes down: not an onset), not sooner than ms after the last one\n detect (fn [state {:keys [flux rms]} at threshold ms]\n (let [hist (vec (take-last 16 (:hist state)))\n m (median hist)\n over (and (> rms gate) (> flux 0.1) (> flux (* threshold m)) (>= rms (or (:rms state) 0)))\n fire (and over (not (:over state)) (or (nil? (:last state)) (>= (- at (:last state)) (/ ms 1000))))]\n [(cond-> (assoc state :hist (conj hist flux) :over over :rms rms) fire (assoc :last at))\n (when fire (/ (js/Math.round (* 100 flux)) 100))]))]\n {;; ── about ──\n :class \"onset~\"\n :license \"MPL-2.0\"\n :doc \"The beginnings of sounds in the signal on its left — a note, a stroke, a syllable —: a bang on the left for each, and how strong it was (the growth of its spectrum, 0…1) on the second outlet; on the third, that growth for every frame (the onset strength envelope, for beat tracking: issue #106). threshold (3) is how far above the usual change of the last frames an onset must rise, while the energy does not fall (a sound cut at once is not an onset); ms (80) the least time between two. Every 512 samples (about 11 ms), on frames of 2048. An onset is reported with the frame it is first seen in: measured on sines, plucked tones and noise bursts that start at a known sample, 6 ms after it as a median (0.7 to 10.7 ms: one hop), and 11 ms for a new note over a held one. [:threshold x] and [:ms y] change them while it listens, and a [:segment …] from form~ makes it forget the usual change of the section before (so a quiet section after a loud one is not deaf). Into a counter, a metro that follows, a sample's rec, or the right inlet of transcribe (its strength, as the weight of the note).\"\n :usage \"onset~ [threshold] [ms]\"\n :io [1 3]\n :size [64 30]\n :args [[:threshold 3] [:ms 80]]\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state args at]}]\n (cond\n (and (= inlet :audio) (:error value)) {:effects [[:print (:error value)]]}\n ;; adaptive: [:threshold x] and [:ms y] change them while it listens (from form~, a message box…);\n ;; a [:segment …] from form~ forgets the usual change of the section before\n (and (vector? value) (= 2 (count value)) (#{:threshold :ms} (first value)) (number? (second value)))\n {:state (assoc-in state [:set (first value)] (second value)) :show {:label (str (name (first value)) \" \" (api/nice (second value)))}}\n (and (vector? value) (= :segment (first value)))\n {:state (assoc state :hist [])}\n (and (= inlet :audio) (map? value))\n (let [[st strength] (detect state value at (api/clamp (or (get-in state [:set :threshold]) (:threshold args)) 1 50)\n (api/clamp (or (get-in state [:set :ms]) (:ms args)) 10 2000))]\n ;; the flux of every frame on the third outlet: the onset strength envelope (issue #106)\n (let [env [2 (/ (js/Math.round (* 1000 (:flux value))) 1000)]]\n (if strength\n {:state st :out [[1 strength] [0 :bang] env] :show {:at at :strength strength}}\n {:state st :out [env]})))\n :else {}))\n\n ;; ── view: a dot that lights on each onset ──\n :derive (fn [{:keys [shown]}]\n {:label (cond (:strength shown) (str (:strength shown)) (:label shown) (:label shown) :else \"onset\")})\n :view [[:rect {:x 2 :y 2 :w 60 :h 26 :r 3 :stroke :grid :fill :bg}]\n [:circle {:cx 15 :cy 15 :r 6 :stroke :accent :fill :accent}]\n [:text {:x 26 :y 19 :size 10 :fill :soft} :$label]]\n\n ;; ── audio ──\n :audio {:create (fn [ac b _]\n (let [in (.createGain ac)\n sr (.-sampleRate ac)\n node {:in in :M (atom nil) :prev (atom nil) :tap (atom nil) :alive (atom true) :inlets [[in 0]] :outlets []}\n on-block (fn [data at & _]\n ;; only while the box is here: a tap left behind would hand it silence\n (when-let [M (and @(:alive node) @(:M node))]\n (set! (.-bufferSize M) size)\n (set! (.-sampleRate M) sr)\n (let [r (.extract M #js [\"amplitudeSpectrum\" \"rms\"] (first data))\n cur (.-amplitudeSpectrum r)\n f (flux cur @(:prev node))]\n (reset! (:prev node) (.slice cur 0 bins))\n (api/report! (:id b) {:flux f :rms (.-rms r)} at))))]\n (-> (api/load-script meyda-url)\n (.then (fn [_] (reset! (:M node) (.-Meyda js/window))))\n (.catch (fn [e] (api/report! (:id b) {:error (str \"onset~: \" (.-message e))} nil))))\n (-> (api/tap ac {:mode :frames :size size :hop 512} on-block)\n (.then (fn [w] (if @(:alive node) (do (.connect in w) (reset! (:tap node) w)) (api/untap w))))\n (.catch (fn [e] (js/console.error \"onset~\" e))))\n node))\n :dispose (fn [node]\n (reset! (:alive node) false)\n (api/untap @(:tap node))\n (try (.disconnect (:in node)) (catch :default _ nil)))}})\n"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"class": "pitch~",
|
|
41
|
+
"license": "MPL-2.0",
|
|
42
|
+
"doc": "The pitch of the signal on its left, by YIN, every 1024 samples (about 21 ms): its frequency in Hz on the left, when it finds one between lo and hi (60 and 1500 by default), and 0 once when it no longer does (silence, noise, a chord); how clearly it is periodic (0…1) on the right. It sends only what changed. threshold (0.15) is how strict it is: lower finds fewer pitches, surer ones. Into ftom (Hz → MIDI pitch), a synth~, or a faust~.",
|
|
43
|
+
"source": ";; pitch~ · the pitch of a sound, by YIN\n;; Part of @kairotic/analysis. Written against the Kairotic API (api/…), with its AudioWorklet inside.\n(let [worklet \"class KairoticYin extends AudioWorkletProcessor {\n // YIN (de Cheveigné & Kawahara, 2002) on the last W samples, every `hop` samples: the period whose\n // cumulative mean normalized difference first falls under the threshold (refined by a parabola),\n // between lo and hi Hz. Hands {hz, clarity, rms}; hz 0 where it finds no pitch or hears silence.\n constructor(o) {\n super();\n const p = (o && o.processorOptions) || {};\n this.W = 1024; this.hop = p.hop || 1024; this.count = 0;\n this.set(p.lo || 60, p.hi || 1500, p.threshold || 0.15);\n this.port.onmessage = (e) => { const d = e.data || {}; this.set(d.lo || this.lo, d.hi || this.hi, d.threshold || this.th); };\n }\n set(lo, hi, th) {\n this.lo = Math.max(20, Math.min(lo, hi)); this.hi = Math.max(this.lo + 1, hi); this.th = Math.max(0.01, Math.min(0.9, th));\n this.T = Math.min(4096, Math.ceil(sampleRate / this.lo) + 1);\n this.tmin = Math.max(2, Math.floor(sampleRate / this.hi));\n this.N = this.W + this.T + 1;\n this.ring = new Float32Array(this.N); this.buf = new Float32Array(this.N); this.d = new Float32Array(this.T + 2);\n this.w = 0;\n }\n analyse() {\n const N = this.N, b = this.buf, r = this.ring, W = this.W, T = this.T, d = this.d;\n for (let i = 0; i < N; i++) b[i] = r[(this.w + i) % N];\n let e = 0; for (let i = N - W; i < N; i++) e += b[i] * b[i];\n const rms = Math.sqrt(e / W);\n if (rms < 0.002) return { hz: 0, clarity: 0, rms };\n // the difference function, stopped as soon as the first dip under the threshold has its minimum\n d[0] = 1; let sum = 0, best = -1;\n for (let tau = 1; tau <= T; tau++) {\n let s = 0;\n for (let j = 0; j < W; j++) { const x = b[j] - b[j + tau]; s += x * x; }\n sum += s; d[tau] = sum > 0 ? s * tau / sum : 1;\n if (tau - 1 >= this.tmin && d[tau - 1] < this.th && d[tau] >= d[tau - 1]) { best = tau - 1; break; }\n }\n if (best < 1) return { hz: 0, clarity: 0, rms };\n let t = best;\n const a = d[best - 1], c = d[best], z = d[best + 1], den = a + z - 2 * c;\n if (den > 0) t = best + (a - z) / (2 * den);\n return { hz: sampleRate / t, clarity: Math.max(0, Math.min(1, 1 - c)), rms };\n }\n process(inputs) {\n const ch = inputs[0] && inputs[0][0];\n const n = ch ? ch.length : 128;\n for (let i = 0; i < n; i++) {\n this.ring[this.w] = ch ? ch[i] : 0;\n this.w = (this.w + 1) % this.N;\n if (++this.count >= this.hop) {\n this.count = 0;\n const r = this.analyse();\n this.port.postMessage({ hz: r.hz, clarity: r.clarity, rms: r.rms, at: currentTime + (i + 1) / sampleRate });\n }\n }\n return true;\n }\n}\nregisterProcessor('kairotic-yin', KairoticYin);\n\"\n ;; what it says between a pitch it heard and the last one it sent: a change of more than a tenth of a cent\n changed? (fn [a b] (or (nil? a) (nil? b) (zero? a) (> (js/Math.abs (- (api/hz->midi b) (api/hz->midi a))) 0.001)))\n label (fn [hz]\n (if (and (number? hz) (pos? hz))\n (let [m (api/hz->midi hz) r (js/Math.round m) c (js/Math.round (* 100 (- m r)))]\n (str (api/midi-name r) \" \" (if (neg? c) \"\" \"+\") c \"¢\"))\n \"–\"))]\n {;; ── about ──\n :class \"pitch~\"\n :license \"MPL-2.0\"\n :doc \"The pitch of the signal on its left, by YIN, every 1024 samples (about 21 ms): its frequency in Hz on the left, when it finds one between lo and hi (60 and 1500 by default), and 0 once when it no longer does (silence, noise, a chord); how clearly it is periodic (0…1) on the right. It sends only what changed. threshold (0.15) is how strict it is: lower finds fewer pitches, surer ones. Into ftom (Hz → MIDI pitch), a synth~, or a faust~.\"\n :usage \"pitch~ [lo hi] [threshold]\"\n :io [1 2]\n :size [124 34]\n :args [[:lo 60] [:hi 1500] [:threshold 0.15]]\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state]}]\n (if (and (= inlet :audio) (map? value))\n (let [{:keys [hz clarity]} value\n hz (if (and (number? hz) (pos? hz)) (/ (js/Math.round (* hz 100)) 100) 0)\n cl (/ (js/Math.round (* 100 (or clarity 0))) 100)\n last-hz (:hz state)\n send-hz (cond (pos? hz) (when (changed? last-hz hz) hz)\n (and last-hz (pos? last-hz)) 0)\n outs (cond-> []\n (some? send-hz) (conj [0 send-hz])\n (not= cl (:clarity state)) (conj [1 cl]))]\n (if (seq outs)\n {:state {:hz (if (some? send-hz) send-hz last-hz) :clarity cl} :out outs :show {:hz hz :clarity cl}}\n {}))\n {}))\n\n ;; ── view ──\n :derive (fn [{:keys [shown]}]\n (let [hz (:hz shown)]\n {:note (label hz)\n :hz (if (and (number? hz) (pos? hz)) (str (api/nice hz) \" Hz\") \"\")\n :bar (* 116 (api/clamp (or (:clarity shown) 0) 0 1))}))\n :view [[:rect {:x 2 :y 2 :w 120 :h 30 :r 3 :stroke :grid :fill :bg}]\n [:rect {:x 4 :y 28 :w :$bar :h 2 :r 1 :stroke nil :fill :accent}]\n [:text {:x 6 :y 20 :size 13} :$note]\n [:text {:x 118 :y 20 :anchor :end :size 9 :fill :soft} :$hz]]\n\n ;; ── audio ──\n :audio {:create (fn [ac b _]\n (let [in (.createGain ac)\n args (:args b)\n [lo hi th] (api/padded args [60 1500 0.15])\n node {:in in :w (atom nil) :alive (atom true) :inlets [[in 0]] :outlets []}]\n (-> (api/worklet-node ac \"kairotic-yin\" worklet\n {:numberOfInputs 1 :numberOfOutputs 0 :channelCount 1 :channelCountMode \"explicit\"\n :processorOptions {:lo lo :hi hi :threshold th}})\n (.then (fn [w]\n (if @(:alive node)\n (do (set! (.. w -port -onmessage)\n (fn [e] (let [d (.-data e)]\n (when @(:alive node)\n (api/report! (:id b) {:hz (.-hz d) :clarity (.-clarity d)} (.-at d))))))\n (.connect in w)\n (reset! (:w node) w))\n (try (.disconnect w) (catch :default _ nil)))))\n (.catch (fn [e] (js/console.error \"pitch~\" e))))\n node))\n ;; a new text: new limits, without a new worklet\n :update (fn [node b _]\n (when-let [w @(:w node)]\n (let [[lo hi th] (api/padded (:args b) [60 1500 0.15])]\n (.postMessage (.-port w) #js {:lo lo :hi hi :threshold th}))))\n :dispose (fn [node]\n (reset! (:alive node) false)\n (when-let [w @(:w node)] (try (set! (.. w -port -onmessage) nil) (.disconnect w) (catch :default _ nil)))\n (try (.disconnect (:in node)) (catch :default _ nil)))}})\n"
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
"class": "realize~",
|
|
47
|
+
"license": "MPL-2.0",
|
|
48
|
+
"doc": "Plays a faust~ from descriptors, as a score of them: loudness (dB, or a level 0…1 as features~ rms), brightness (Hz, the spectral centroid), noisiness (0…1, the spectral flatness) and pitch (Hz as pitch~ sends it, 0 for none; MIDI with `midi`). Its left outlet goes to the faust~: the faust~'s pitch or freq slider takes the pitch, its gain slider the loudness, and its other sliders are set to the settings of that faust~ that sound nearest to the brightness and noisiness written. It learns them first by playing steady settings of the faust~ offline and listening (300 by default), when the first values arrive or on learn; learn again after changing the faust~'s code. With features~ of the faust~'s sound on its last three inlets (rms, centroid, flatness) it listens to what it plays and asks for more where it falls short. On the right, how far the nearest setting is from what is written (above 1: the faust~ cannot sound like that).",
|
|
49
|
+
"source": ";; realize~ · descriptors written → the sliders of a faust~ that sound like them (discussion #114, Study 37)\n;; Part of @kairotic/analysis. Written against the Kairotic API (api/…).\n;; What arrives is a frame of a score: a loudness, a brightness (the spectral centroid, Hz), a noisiness (the\n;; spectral flatness) and a pitch — what features~ and pitch~ hear, so another sound can drive it, or curves\n;; drawn on canvases read back (reader~ → snapshot~). What leaves is [:slider value] for the faust~ on its\n;; left outlet: its `pitch` (MIDI) or `freq` (Hz) slider takes the pitch, its `gain` (dB or a level) the\n;; loudness, and its other sliders, the timbre, are set to the blend of the settings of that faust~ that come\n;; nearest to the brightness and noisiness written (where a pitch is written, among the settings in which YIN\n;; would hear it). To know them, it learns the faust~ first (the task :learn: steady settings played offline\n;; and heard, @kairotic/faust-units' notation.mjs), when the first frame arrives or on `learn`.\n;; It can also listen to what it plays: features~ of the faust~'s sound on its last three inlets (loudness,\n;; brightness, noisiness heard). Then it asks for more where what is heard falls short of what is written,\n;; little by little (a correction that grows while the difference lasts, within limits), as the study's\n;; passes did offline.\n(let [meyda-url \"npm:meyda@5.6.3/dist/web/meyda.min.js\"\n origins [\"npm:@kairotic/faust-units@0.1.5/dist/notation.mjs\" \"kairotic/packages/faust-units/notation.mjs\"]\n silent -60 ; dB: below it nothing is written (a microphone in a room is often near -50)\n every 0.04 ; seconds: at most one new setting this often\n ;; listening: how much of each difference heard goes into the correction, and how far it may go\n rate 0.08\n leak 0.995 ; what it corrected fades unless it is still needed\n hold 0.25 ; seconds a sound has lasted before it is corrected (not its attack)\n limits {:loud 18 :lb 2 :nz 0.4}\n bank-size (fn [args] (js/Math.round (api/clamp (or (first (filter number? args)) 300) 40 2000)))\n midi-in? (fn [args] (some #{:midi} args))\n log2 (fn [x] (/ (js/Math.log x) js/Math.LN2))\n ;; a loudness as dB: a level (0 < v ≤ 1, as features~ rms) or dB already (≤ 0); 0 is silence\n db-of (fn [v] (cond (not (number? v)) nil (zero? v) -100 (and (pos? v) (<= v 1)) (api/db v) :else v))\n ;; the nearest settings of the bank to what is asked, blended: {:u [...] :loud0 dB :d distance}. The search\n ;; itself is the bank's query (notation.mjs: plain loops over typed arrays), asked for every frame it plays\n nearest (fn [bank {:keys [pitch lb nz voiced]}]\n (let [r ((:query bank) pitch lb nz (boolean voiced))]\n {:loud0 (aget r 0) :d (aget r 1) :u (vec (.slice r 2))}))\n ;; the values for the faust~, and only those that changed: [[0 [:cutoff 1800]] …]\n settle (fn [st at]\n (let [{:keys [bank]} st\n ;; a lane that never arrived keeps the middle of what the faust~ can do\n want (merge {:lb (first (:mid bank)) :nz (second (:mid bank))} (:want st))\n ;; what is asked of the bank: what is written, corrected by what was heard\n {cl :loud clb :lb cnz :nz} (:corr st)\n [lb-lo lb-hi] (or (:lbr bank) [0 20])\n ask (cond-> want\n (and cl (:loud want)) (update :loud + cl)\n clb (assoc :lb (api/clamp (+ (:lb want) clb) lb-lo lb-hi))\n cnz (assoc :nz (api/clamp (+ (:nz want) cnz) 0 1)))\n loud (:loud want)]\n (if (and (nil? loud) (nil? (:pitch want)) (nil? (get-in st [:want :lb])) (nil? (get-in st [:want :nz])))\n {:state (assoc st :t at)}\n (let [quiet? (and loud (< loud silent))\n {:keys [u loud0 d]} (if quiet? {:u (:u st) :loud0 0 :d (:reach st)} (nearest bank ask))\n ;; the timbre glides to where it goes (half the way every 40 ms), and keeps\n ;; gliding when nothing more arrives (a value written once, a canvas held)\n goal u\n u (if (and (:u st) (not quiet?)) (mapv (fn [a b] (+ a (* 0.5 (- b a)))) (:u st) u) u)\n moving? (and goal u (not quiet?) (some (fn [[a b]] (> (js/Math.abs (- a b)) 0.004)) (map vector u goal)))\n g (:gain bank) p (:pitch bank)\n gain-v (when (and g loud) (let [dB (if quiet? -100 (- (:loud ask) loud0))]\n (if (:db g) (api/clamp dB (:min g) (:max g))\n (api/clamp (js/Math.pow 10 (/ dB 20)) (:min g) (:max g)))))\n pitch-v (when (and p (:pitch want))\n (api/clamp (if (= \"hz\" (:kind p)) (api/midi->hz (:pitch want)) (:pitch want)) (:min p) (:max p)))\n vals (cond-> (if (and u (not quiet?))\n (into {} (map (fn [s x] [(:label s) (api/from-unit x (:min s) (:max s) (:log s))]) (:timbre bank) u))\n {})\n gain-v (assoc (:label g) gain-v)\n pitch-v (assoc (:label p) pitch-v))\n span (fn [label] (let [s (or (first (filter #(= label (:label %)) (:timbre bank))) (when (= label (:label g)) g) p)]\n (js/Math.abs (- (:max s) (:min s)))))\n changed (filter (fn [[k v]] (let [old (get (:sent st) k)]\n (or (nil? old) (> (js/Math.abs (- v old)) (* 0.002 (span k))))))\n vals)\n reach (when d (/ (js/Math.round (* 100 d)) 100))]\n {:state (-> st (assoc :t at :u (or u (:u st)) :reach reach :dirty (boolean moving?)\n :loud0 (if quiet? (:loud0 st) loud0)\n :tick (or (:tick st) (boolean moving?)))\n (update :sent merge (into {} changed)))\n :later (when (and moving? (not (:tick st))) [[(+ at every) 0 :realize-tick]])\n :out (cond-> (mapv (fn [[k v]] [0 [(keyword k) (api/nice v)]]) changed)\n (and reach (not= reach (:reach st))) (conj [1 reach]))\n :show {:status :ready :faust (:faust bank) :n (count (:entries bank)) :want want :reach reach\n :listening (some? (:corr st))}}))))\n ;; each request is numbered, so the answer to an older one is dropped\n learn (fn [st args]\n (let [k (inc (:k st 0))]\n {:state (assoc st :learning true :error nil :k k :corr nil)\n :show {:status :learning}\n :effects [[:learn {:n (bank-size args) :k k}]]}))]\n {;; ── about ──\n :class \"realize~\"\n :license \"MPL-2.0\"\n :doc \"Plays a faust~ from descriptors, as a score of them: loudness (dB, or a level 0…1 as features~ rms), brightness (Hz, the spectral centroid), noisiness (0…1, the spectral flatness) and pitch (Hz as pitch~ sends it, 0 for none; MIDI with `midi`). Its left outlet goes to the faust~: the faust~'s pitch or freq slider takes the pitch, its gain slider the loudness, and its other sliders are set to the settings of that faust~ that sound nearest to the brightness and noisiness written. It learns them first by playing steady settings of the faust~ offline and listening (300 by default), when the first values arrive or on learn; learn again after changing the faust~'s code. With features~ of the faust~'s sound on its last three inlets (rms, centroid, flatness) it listens to what it plays and asks for more where it falls short. On the right, how far the nearest setting is from what is written (above 1: the faust~ cannot sound like that).\"\n :usage \"realize~ [settings] [midi]\"\n :io [7 2]\n :size [236 46]\n\n ;; ── tasks: learning the faust~ it drives, outside its pure logic ──\n :tasks {:learn (fn [{:keys [n]} box]\n (let [f (first (filter (fn [w] (and (= \"faust~\" (:class w)) (zero? (:outlet w)))) (:wired box)))\n src (str (get-in f [:data :source]))\n ;; the compilation of its code, waited for (a faust~ just written is still compiling)\n compiled (fn compiled [tries]\n (let [r (api/compiled \"faust\" src)]\n (cond (= :ok (:status r)) (js/Promise.resolve r)\n (and (#{:compiling :empty} (:status r)) (pos? tries))\n (js/Promise. (fn [ok] (js/setTimeout (fn [] (ok nil)) 250)))\n :else (js/Promise.reject (js/Error. (str \"the faust~ does not compile: \" (or (:error r) \"no code\")))))))\n wait (fn wait [tries]\n (.then (compiled tries) (fn [r] (if r r (wait (dec tries))))))]\n (if-not f\n (js/Promise.reject (js/Error. \"connect its left outlet to a faust~: it learns the one it plays\"))\n (-> (wait 120)\n (.then (fn [r]\n (when (get-in r [:info :poly])\n (throw (js/Error. \"it plays a faust~ of one voice (this one declares nvoices)\")))\n (let [fac (.-factory (:value r))]\n (-> (js/Promise.all #js [(api/load-script meyda-url) (api/import-module origins)])\n (.then (fn [[_ m]]\n (.then (.learn m #js {:module (or (.-module fac) (.-code fac))\n :json (js/JSON.parse (.-json fac))\n :Meyda (.-Meyda js/window) :n n})\n (fn [b] [b (.index m b)]))))))))\n (.then (fn [[bjs query]]\n ;; the settings stay a JS array (the query reads them); the rest as data\n (let [es (.-entries bjs)\n col (fn [j] (map (fn [e] (aget e j)) (array-seq es)))\n avg (fn [j] (/ (reduce + (col j)) (.-length es)))]\n (-> (js->clj #js {:pitch (.-pitch bjs) :gain (.-gain bjs) :timbre (.-timbre bjs) :sd (.-sd bjs)}\n :keywordize-keys true)\n (assoc :query query :entries es :faust (name (:id f)) :mid [(avg 1) (avg 2)]\n :lbr [(reduce min (col 1)) (reduce max (col 1))])))))))))}\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state args at]}]\n (let [st (or state {})]\n (cond\n ;; what learning found\n (= inlet :task)\n (cond\n (not= (:k (:arg value)) (:k st)) {}\n (:error value) {:state (assoc st :learning false :error (:error value))\n :show {:status :error :error (:error value)}}\n :else (let [st (assoc st :learning false :bank (:value value) :sent {} :u nil)]\n (settle st (or at (:t st) 0))))\n\n (#{:learn :bang} value) (learn st args)\n\n ;; a moment later: the last frame that arrived while it waited\n (= value :realize-tick) (if (and (:bank st) (:dirty st)) (settle (assoc st :tick false) at) {:state (assoc st :tick false)})\n\n ;; what is heard in what it plays (features~ of the faust~): the correction grows by a part of\n ;; the difference, while something is written and sounds\n (and (#{4 5 6} inlet) (number? value))\n (let [heard (case inlet 4 (db-of value) 5 (when (pos? value) (log2 value)) 6 value)\n k ({4 :loud 5 :lb 6 :nz} inlet)\n want (:want st)\n st (cond-> st (= k :loud) (assoc :heard-loud heard))\n sounding? (and (:bank st) (number? (:loud want)) (> (:loud want) silent)\n (> (or (:heard-loud st) -100) silent))]\n (if (and sounding? (number? heard) (number? (get want k)) (:since st) (>= (- at (:since st)) hold))\n ;; within what the faust~ can give (the bank's brightness, noisiness 0…1, the gain slider's\n ;; range): a correction that cannot be played does not grow, so it does not wind up\n (let [bank (:bank st) w (get want k) g (:gain bank) [lb-lo lb-hi] (or (:lbr bank) [0 20])\n [lo hi] (case k\n :lb [(- lb-lo w) (- lb-hi w)]\n :nz [(- w) (- 1 w)]\n :loud (if (and g (:db g) (:loud0 st))\n [(+ (:min g) (:loud0 st) (- w)) (+ (:max g) (:loud0 st) (- w))]\n [(- (limits k)) (limits k)]))\n c (-> (+ (* leak (get-in st [:corr k] 0)) (* rate (- w heard)))\n (api/clamp (max lo (- (limits k))) (min hi (limits k))))\n st (-> st (assoc-in [:corr k] c) (assoc :dirty true))]\n (if (or (nil? (:t st)) (>= (- at (:t st)) every))\n (settle st at)\n (if (:tick st) {:state st} {:state (assoc st :tick true) :later [[(+ (:t st) every) 0 :realize-tick]]})))\n {:state st}))\n\n (and (integer? inlet) (number? value))\n (let [midi (when (and (= inlet 3) (pos? value)) (if (midi-in? args) value (api/hz->midi value)))\n want (case inlet\n 0 (assoc (:want st) :loud (db-of value))\n 1 (if (pos? value) (assoc (:want st) :lb (log2 value)) (:want st))\n 2 (assoc (:want st) :nz (api/clamp value 0 1))\n 3 (if midi (assoc (:want st) :pitch midi :voiced true) (assoc (:want st) :voiced false))\n (:want st))\n ;; since when what is written sounds (its attack is not corrected)\n st (cond-> (assoc st :want want :dirty true)\n (= inlet 0) (assoc :since (if (> (or (:loud want) -100) silent) (or (:since st) at) nil)))]\n (cond\n (:bank st) (if (or (nil? (:t st)) (>= (- at (:t st)) every))\n (settle st at)\n (if (:tick st)\n {:state st}\n {:state (assoc st :tick true) :later [[(+ (:t st) every) 0 :realize-tick]]}))\n (or (:learning st) (:error st)) {:state st}\n :else (learn st args)))\n\n :else {})))\n\n ;; ── view: what it learnt, what is written now, and how far the faust~ is from it ──\n :draw (fn [{:keys [shown w h]}]\n (let [{:keys [status faust n want reach error]} shown\n line1 (case status\n :learning \"learning the faust~ it plays…\"\n :ready (str \"plays \" faust \" · \" n \" settings heard\" (when (:listening shown) \" · listening\"))\n :error (str \"✗ \" error)\n \"waits for a faust~ and a score\")\n fmt (fn [x d] (if (number? x) (.toFixed x d) \"–\"))\n line2 (when (= status :ready)\n (str (fmt (:loud want) 0) \" dB · \"\n (if (:lb want) (str (js/Math.round (js/Math.pow 2 (:lb want))) \" Hz\") \"–\") \" · \"\n (fmt (:nz want) 2)\n (when (and (:voiced want) (:pitch want)) (str \" · \" (api/midi-name (js/Math.round (:pitch want)))))))\n bw (- w 16) far (when (number? reach) (api/clamp (/ reach 2) 0 1))]\n (cond-> [[:rect {:x 2 :y 2 :w (- w 4) :h (- h 4) :r 4 :stroke :grid :fill :bg}]\n [:text {:x 8 :y 15 :size 9 :fill (if (= status :error) :kairos :ink)} line1]]\n line2 (conj [:text {:x 8 :y 28 :size 9 :fill :soft} line2])\n far (conj [:rect {:x 8 :y 35 :w bw :h 4 :r 2 :fill :grid :stroke nil}]\n [:rect {:x 8 :y 35 :w (max 2 (* bw far)) :h 4 :r 2 :fill (if (> reach 1) :kairos :accent) :stroke nil}]))))})\n"
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
"class": "spectrum~",
|
|
53
|
+
"license": "MPL-2.0",
|
|
54
|
+
"doc": "The spectrum of the signal on its left, drawn in the patch about 30 times a second: frequency on a log scale from lo to hi Hz (40 and 16000 by default), level in dB from -90 to 0. Computed with Meyda.",
|
|
55
|
+
"source": ";; spectrum~ · the spectrum of a sound, drawn in the patch\n;; Part of @kairotic/analysis. Written against the Kairotic API (api/…). The spectrum is Meyda's (MIT,\n;; meyda.js.org), loaded from jsDelivr the first time one is needed.\n(let [meyda-url \"npm:meyda@5.6.3/dist/web/meyda.min.js\"\n size 2048 ; the frames every analysis box of the package uses (see features~)\n w 240 h 96 cols 116 ; the picture: 116 columns of 2 px, from x 4 to 236\n floor -90 ; dB at the bottom\n ;; for each bin of the spectrum, its column on a log scale of frequency from lo to hi (nil outside)\n column-map (fn [sr lo hi]\n (let [l0 (js/Math.log lo) span (- (js/Math.log hi) l0)]\n (vec (for [k (range (/ size 2))]\n (let [f (* k (/ sr size))]\n (when (and (>= f lo) (< f hi))\n (js/Math.floor (* cols (/ (- (js/Math.log f) l0) span)))))))))\n ;; the loudest bin of each column, in dB below a full-scale sine (whose bin is size/4 with Hann)\n columns (fn [spec cmap]\n (let [out (js/Array. cols)]\n (.fill out floor)\n (loop [k 1]\n (when (< k (count cmap))\n (when-let [c (nth cmap k)]\n (let [a (aget spec k)\n d (if (> a 1e-9) (* 20 (/ (js/Math.log (/ a (/ size 4))) js/Math.LN10)) floor)]\n (when (> d (aget out c)) (aset out c d))))\n (recur (inc k))))\n ;; columns with no bin of their own (low frequencies) take the one before\n (loop [c 1]\n (when (< c cols)\n (when (= floor (aget out c)) (aset out c (aget out (dec c))))\n (recur (inc c))))\n (vec out)))\n points (fn [ds]\n (vec (map-indexed (fn [i d] [(+ 4 (* 2 i)) (+ 4 (* (- h 18) (/ (- (api/clamp d floor 0)) (- floor))))]) ds)))]\n {;; ── about ──\n :class \"spectrum~\"\n :license \"MPL-2.0\"\n :doc \"The spectrum of the signal on its left, drawn in the patch about 30 times a second: frequency on a log scale from lo to hi Hz (40 and 16000 by default), level in dB from -90 to 0. Computed with Meyda.\"\n :usage \"spectrum~ [lo hi]\"\n :io [1 0]\n :size [240 96]\n :args [[:lo 40] [:hi 16000]]\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value]}]\n (cond\n (and (= inlet :audio) (:error value)) {:effects [[:print (:error value)]]}\n (and (= inlet :audio) (vector? value)) {:show value}\n :else {}))\n\n ;; ── view ──\n :derive (fn [{:keys [shown args]}]\n {:pts (when (vector? shown) (points shown))\n :range (str (api/nice (:lo args)) \" – \" (api/nice (:hi args)) \" Hz\")})\n :view [[:rect {:x 2 :y 2 :w 236 :h 92 :r 4 :stroke :grid :fill :bg}]\n [:line {:x1 4 :y1 30 :x2 236 :y2 30 :stroke :grid}]\n [:line {:x1 4 :y1 54 :x2 236 :y2 54 :stroke :grid}]\n [:path {:points :$pts :stroke :accent :weight 1.2}]\n [:text {:x 234 :y 90 :anchor :end :size 8 :fill :soft} :$range]]\n\n ;; ── audio ──\n :audio {:create (fn [ac b _]\n (let [in (.createGain ac)\n sr (.-sampleRate ac)\n [lo hi] (api/padded (:args b) [40 16000])\n cmap (column-map sr (api/clamp (min lo hi) 10 20000) (api/clamp (max lo hi) 20 (/ sr 2)))\n node {:in in :M (atom nil) :tap (atom nil) :alive (atom true) :inlets [[in 0]] :outlets []}\n on-block (fn [data at & _]\n ;; only while the box is here: a tap left behind would hand it silence\n (when-let [M (and @(:alive node) @(:M node))]\n (set! (.-bufferSize M) size)\n (set! (.-sampleRate M) sr)\n (let [spec (.extract M \"amplitudeSpectrum\" (first data))]\n (api/report! (:id b) (columns spec cmap) at))))]\n (-> (api/load-script meyda-url)\n (.then (fn [_] (reset! (:M node) (.-Meyda js/window))))\n (.catch (fn [e] (api/report! (:id b) {:error (str \"spectrum~: \" (.-message e))} nil))))\n (-> (api/tap ac {:mode :frames :size size :hop (js/Math.round (/ sr 30))} on-block)\n (.then (fn [w] (if @(:alive node) (do (.connect in w) (reset! (:tap node) w)) (api/untap w))))\n (.catch (fn [e] (js/console.error \"spectrum~\" e))))\n node))\n :dispose (fn [node]\n (reset! (:alive node) false)\n (api/untap @(:tap node))\n (try (.disconnect (:in node)) (catch :default _ nil)))}})\n"
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
"class": "transcribe",
|
|
59
|
+
"license": "MPL-2.0",
|
|
60
|
+
"doc": "Writes what pitch~ hears into a freqt, as strokes. Hz on the left (from pitch~: 0 when it hears none), :rec to start from 0 (its first write clears the freqt) or :rec t (msg :rec 8) to start from beat t, keeping what is before it, or :ovr t (msg :ovr 8) to write over only what the take covers, keeping what comes after and :stop to end; a bang on the right (from onset~) begins a new note even on the same pitch, and a number there (onset~'s strength, 0…1) does too and gives the note that weight: its stroke is written heavier (as an event's :amp), and the quantizer reads it as the note's :vel, which helps find the beat and the bar (issue #106). In notes mode the contour of each voiced stretch is cut into notes where it settles on another pitch (a slide, a vibrato or an ornament that passes does not start notes; an onset on the right does), each a flat stroke at its median pitch (the freqt's tuning, or its score view, rounds it), written when the voice stops; in glide mode a stroke follows the pitch as it moves. Times are beats at bpm (60: one beat a second). Notes shorter than ms (80) are left out. It writes into the freqt at most every 2 seconds, each time one event of the history. Connect its outlet to the freqt's inlet.",
|
|
61
|
+
"source": ";; transcribe · what pitch~ hears, written as strokes of a freqt\n;; Part of @kairotic/analysis. Written against the Kairotic API (api/…). Pure logic: no audio of its own.\n(let [;; ── what it understands ──\n flush-every 2 ; seconds between two writes into the freqt (each write is one event of the history)\n max-points 64 ; points of a stroke in glide mode\n cents (fn [a b] (* 1200 (/ (js/Math.log (/ b a)) js/Math.LN2)))\n ;; the pitch a note holds longest: pitch~ only sends what changed, so a steady note is one point and\n ;; its attack many; each pitch weighs the time until the next one\n held (fn [pts t1]\n (let [ws (map (fn [[t f] [t2 _]] [f (max 0 (- t2 t))]) pts (concat (rest pts) [[t1 nil]]))\n total (reduce + (map second ws))\n sorted (sort-by first ws)]\n (if (pos? total)\n (loop [[[f w] & more] sorted acc 0]\n (let [acc (+ acc w)] (if (or (>= acc (/ total 2)) (empty? more)) f (recur more acc))))\n (second (last pts)))))\n mode-of (fn [args] (if (= :glide (:mode args)) :glide :notes))\n beats (fn [state at] (* (- at (:t0 state)) (/ (:bpm state) 60)))\n r3 (fn [x] (/ (js/Math.round (* x 1000)) 1000))\n ;; ── a note that ends: a stroke, if it lasted long enough ──\n stroke-of (fn [{:keys [pts]} t1 mode]\n (let [t0 (ffirst pts) hz (held pts t1)]\n (if (= mode :glide)\n (let [kept (reduce (fn [acc [t f]]\n (if (> (js/Math.abs (cents (second (peek acc)) f)) 10) (conj acc [t f]) acc))\n [(first pts)] (rest pts))\n kept (if (> (count kept) max-points)\n (vec (take-nth (js/Math.ceil (/ (count kept) max-points)) kept))\n kept)]\n (mapv (fn [[t f]] [(r3 t) (r3 f)]) (conj kept [t1 (second (peek pts))])))\n [[(r3 t0) (r3 hz)] [(r3 t1) (r3 hz)]])))\n end-note (fn [state t]\n (if-let [cur (:cur state)]\n (let [t0 (ffirst (:pts cur))\n long-enough (>= (/ (- t t0) (/ (:bpm state) 60)) (/ (:min-ms state) 1000))]\n (cond-> (dissoc state :cur)\n long-enough (update :done conj {:pts (stroke-of cur t (:mode state)) :acc (:acc cur)})\n long-enough (update :count inc)))\n state))\n ;; a note takes the strength of the onset that began it (a number on the right inlet, from onset~), if it\n ;; came at most 0.15 s before: onset~ often hears the attack before pitch~ hears the pitch\n start-note (fn [state t hz]\n (let [[at s] (:acc state) acc (when (and at (<= (- t at) (* 0.15 (/ (:bpm state) 60)))) s)]\n (cond-> (assoc state :cur (cond-> {:pts [[t hz]] :midi (api/hz->midi hz)} acc (assoc :acc acc)))\n acc (dissoc :acc))))\n ;; how a note with the strength of its attack is written: the weight of its stroke, as an event's :amp is\n ;; (0 … 1 → 0.4 … 2.8 times the usual); the quantizer reads it back as the note's :vel (issue #106)\n edit-of (fn [{:keys [pts acc]}] (if (number? acc) [:stroke pts {:w (/ (js/Math.round (* 100 (+ 0.4 (* 2.4 acc)))) 100)}] [:stroke pts]))\n ;; ── writing: the strokes that ended, as one [:batch …], at most every flush-every seconds ──\n flush (fn [state at force]\n (let [now (r3 (beats state at))]\n (cond\n ;; ovr at stop: what the take covered since the last write is overwritten, even by silence\n (and (empty? (:done state)) (not (and force (:ovr state) (> now (:cut-from state))))) [state nil nil]\n (or force (nil? (:sent state)) (>= (- at (:sent state)) flush-every))\n ;; the first write of a take clears the freqt (as part of the same event); a take over (ovr)\n ;; cuts what it covered since the last write, and keeps what comes after\n [(cond-> (assoc state :done [] :sent at :pending false :fresh false)\n (:ovr state) (assoc :cut-from now))\n [:batch (into (cond (:ovr state) [[:cut (:cut-from state) now]]\n (:fresh state) [(if (:from state) [:cut (:from state)] [:clear])]\n :else [])\n (mapv edit-of (:done state)))] nil]\n (:pending state) [state nil nil]\n :else [(assoc state :pending true) nil [(+ (:sent state) flush-every) 0 [:flush]]])))\n result (fn [state [st edit later]]\n (cond-> {:state st :show (select-keys st [:on :count])}\n edit (assoc :out [[0 edit]])\n later (assoc :later [later])))\n ;; ── notes mode: the contour of each voiced stretch, segmented (issue #106, measured on cante2midi) ──\n ;; What pitch~ sends while a voice sounds is kept as a run of points; when the voice stops (or the run is long)\n ;; the run is read as a contour every 10 ms, smoothed by a median of 100 ms (vibrato and jitter), and cut into\n ;; notes with hysteresis: a note goes on while the contour stays within a semitone of its pitch; a new one\n ;; begins only where the contour, away from it, has held still (within 1.1 semitones) for 70 ms, so a slide\n ;; or an ornament that passes does not start notes. An onset (inlet 1) cuts there too. Each note is written\n ;; at the median pitch of its frames. Silences of 30 ms at most are bridged.\n median (fn [xs] (let [v (vec (sort xs))] (nth v (quot (count v) 2))))\n segment (fn [run marks t-end k minlen]\n (let [dt (* 0.01 k) w 5 band 1 stab 1.1 hold-n 7\n ts (vec (range (ffirst run) t-end dt))\n ;; the contour on the grid: each frame, the last point at or before it (as MIDI)\n raw (loop [i 0 [p & more :as ps] run cur nil out (transient [])]\n (if (>= i (count ts))\n (persistent! out)\n (let [t (ts i)]\n (if (and p (<= (first p) (+ t 1e-9)))\n (recur i more (api/hz->midi (second p)) out)\n (recur (inc i) ps cur (conj! out cur))))))\n n (count raw)\n sm (mapv (fn [i] (median (subvec raw (max 0 (- i w)) (min n (+ i w 1))))) (range n))\n cut? (fn [i] (let [a (if (pos? i) (ts (dec i)) (- (ts 0) dt)) b (ts i)] (some (fn [[mt]] (and (> mt a) (<= mt b))) marks)))\n note (fn [s e ms] {:s s :e e :ms ms})\n notes (loop [i 0 cur nil pend [] out []]\n (if (>= i n)\n (if cur (conj out (assoc cur :e t-end)) out)\n (let [t (ts i) m (sm i)]\n (cond\n (nil? cur) (recur (inc i) (note t (+ t dt) [m]) [] out)\n ;; (not while the contour is away from the note and not settled yet: there the contour\n ;; decides; measured on cante2midi with onsets from its Bark bands)\n (and (empty? pend) (cut? i) (> t (:s cur))) (recur (inc i) (note t (+ t dt) [m]) [] (conj out (assoc cur :e t)))\n (<= (js/Math.abs (- m (median (take-last 24 (:ms cur))))) band)\n (recur (inc i) (assoc cur :e (+ t dt) :ms (into (conj (:ms cur) m) (map second pend))) [] out)\n :else\n (let [pend (conj pend [t m]) tail (take-last hold-n pend) tm (map second tail)]\n (if (and (>= (count pend) hold-n) (< (- (apply max tm) (apply min tm)) stab))\n ;; the new note begins where the contour left the old one; its pitch is that of where it settled\n (recur (inc i) (note (ffirst pend) (+ t dt) (vec tm)) [] (conj out (assoc cur :e (ffirst pend))))\n (recur (inc i) cur pend out)))))))]\n (->> notes\n (filter (fn [{:keys [s e]}] (>= (- e s) minlen)))\n (mapv (fn [{:keys [s e ms]}]\n (let [hz (api/midi->hz (median ms))\n ;; the strength of an onset heard at most 0.15 s before the note (or just after its start)\n acc (some (fn [[mt x]] (when (and (number? x) (<= (- s (* 0.15 k)) mt (+ s (* 0.05 k)))) x)) marks)]\n (cond-> {:pts [[(r3 s) (r3 hz)] [(r3 e) (r3 hz)]]} acc (assoc :acc acc))))))))\n ;; the run segmented into :done: all of it (the voice stopped at t-end), or all but its last note (a long run\n ;; still sounding: that note goes on in the run)\n settle (fn [state t-end all?]\n (let [run (:run state) k (/ (:bpm state) 60) minlen (* k (/ (:min-ms state) 1000))]\n (if (or (empty? run) (<= t-end (ffirst run)))\n (dissoc state :run :silent)\n (let [ns (segment run (:marks state) t-end k minlen)\n [keep-ns rest-from] (if (or all? (< (count ns) 2)) [ns nil] [(pop ns) (ffirst (:pts (peek ns)))])\n state (-> state (update :done into keep-ns) (update :count + (count keep-ns)))]\n (if rest-from\n (let [before (last (filter (fn [[t]] (<= t rest-from)) run))]\n (assoc state :run (into [[rest-from (second before)]] (filter (fn [[t]] (> t rest-from)) run))\n :marks (filterv (fn [[t]] (>= t (- rest-from (* 0.15 k)))) (:marks state))))\n (-> state (dissoc :run :silent) (assoc :marks (filterv (fn [[t]] (> t t-end)) (:marks state)))))))))\n ;; a silence that lasted more than 30 ms: the run ends where it began\n settled (fn [state t]\n (if (and (:silent state) (> (- t (:silent state)) (* 0.03 (/ (:bpm state) 60))))\n (settle state (:silent state) true)\n state))\n hear-notes (fn [state hz t]\n (let [k (/ (:bpm state) 60)\n state (settled state t)]\n (cond\n (not (pos? hz)) (if (and (seq (:run state)) (nil? (:silent state))) (assoc state :silent t) state)\n :else (let [state (-> state (dissoc :silent) (update :run (fnil conj []) [t hz]))]\n ;; a run of 8 s and more: what is settled is written, the last note goes on\n (if (> (- t (ffirst (:run state))) (* 8 k)) (settle state t false) state)))))\n ;; ── one value of pitch~ (Hz, 0 when it hears none) ──\n hear (fn [state hz at]\n (let [t (beats state at)\n cur (:cur state)\n state (cond\n (= :notes (:mode state)) (hear-notes state hz t)\n (not (pos? hz)) (end-note state t)\n (nil? cur) (start-note state t hz)\n :else (update-in state [:cur :pts] conj [t hz]))\n [st edit later] (flush state at false)]\n ;; the voice stopped: a look again in a little while, to write its notes even if nothing else arrives\n [st edit (or later (when (and (:silent st) (not (:pending st))) [(+ at 0.05) 0 [:flush]]))]))]\n {;; ── about ──\n :class \"transcribe\"\n :license \"MPL-2.0\"\n :doc \"Writes what pitch~ hears into a freqt, as strokes. Hz on the left (from pitch~: 0 when it hears none), :rec to start from 0 (its first write clears the freqt) or :rec t (msg :rec 8) to start from beat t, keeping what is before it, or :ovr t (msg :ovr 8) to write over only what the take covers, keeping what comes after and :stop to end; a bang on the right (from onset~) begins a new note even on the same pitch, and a number there (onset~'s strength, 0…1) does too and gives the note that weight: its stroke is written heavier (as an event's :amp), and the quantizer reads it as the note's :vel, which helps find the beat and the bar (issue #106). In notes mode the contour of each voiced stretch is cut into notes where it settles on another pitch (a slide, a vibrato or an ornament that passes does not start notes; an onset on the right does), each a flat stroke at its median pitch (the freqt's tuning, or its score view, rounds it), written when the voice stops; in glide mode a stroke follows the pitch as it moves. Times are beats at bpm (60: one beat a second). Notes shorter than ms (80) are left out. It writes into the freqt at most every 2 seconds, each time one event of the history. Connect its outlet to the freqt's inlet.\"\n :usage \"transcribe [bpm] [notes|glide] [ms]\"\n :io [2 1]\n :size [124 30]\n :args [[:bpm 60] [:mode nil] [:min-ms 80]]\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state args at]}]\n (let [state (or state {})]\n (cond\n ;; rec: a take from 0 (its first write clears the freqt); [:rec t] (msg :rec 8): from beat t on,\n ;; keeping what is before t (punch-in)\n ;; ovr t (msg :ovr 8): over what is there from beat t to where the take stops, keeping what comes after\n (or (#{:rec :ovr} value) (and (vector? value) (#{:rec :ovr} (first value))))\n (let [bpm (api/clamp (:bpm args) 10 400)\n ovr (or (= value :ovr) (and (vector? value) (= :ovr (first value))))\n from (or (when (vector? value) (let [x (second value)] (when (and (number? x) (>= x 0)) x)))\n (when ovr 0))]\n {:state (cond-> {:on true :t0 (- at (* (or from 0) (/ 60 bpm))) :bpm bpm :mode (mode-of args)\n :min-ms (api/clamp (:min-ms args) 0 2000) :done [] :count 0 :fresh true}\n from (assoc :from from)\n ovr (assoc :ovr true :cut-from from))\n :show {:on true :count 0}})\n (not (:on state)) {}\n (= value :stop)\n (let [t (beats state at)\n st (if (seq (:run state)) (settle state (or (:silent state) t) true) state)\n st (end-note st t)\n [st edit _] (flush st at true)]\n (cond-> {:state (assoc st :on false) :show {:on false :count (:count st)}}\n edit (assoc :out [[0 edit]])))\n (= value [:flush]) (result state (flush (settled (assoc state :pending false) (beats state at)) at false))\n ;; an onset (a bang, or its strength 0…1 from onset~'s second outlet): the note that sounds ends\n ;; here and a new one begins at its pitch, with that strength; in silence, the strength waits for\n ;; the next note to begin\n (and (= inlet 1) (= :notes (:mode state)))\n {:state (update state :marks (fnil conj []) [(beats state at) (when (number? value) (api/clamp value 0 1))])}\n (= inlet 1)\n (let [t (beats state at)\n state (if (number? value) (assoc state :acc [t (api/clamp value 0 1)]) state)]\n (if (:cur state)\n (let [hz (second (peek (get-in state [:cur :pts])))]\n (result state (flush (-> state (end-note t) (start-note t hz)) at false)))\n {:state state}))\n (and (= inlet 0) (number? value)) (result state (hear state value at))\n :else {})))\n\n ;; ── view ──\n :derive (fn [{:keys [shown]}]\n {:label (if (:on shown) (str \"● transcribing · \" (or (:count shown) 0)) (str \"transcribe · \" (or (:count shown) 0) \" notes\"))})\n :view [[:rect {:x 2 :y 2 :w 120 :h 26 :r 3 :stroke :grid :fill :bg}]\n [:text {:x 8 :y 19 :size 10 :fill :soft} :$label]]})\n"
|
|
62
|
+
}
|
|
63
|
+
]
|
|
64
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Generated by packages/build.mjs
|
|
2
|
+
export default {
|
|
3
|
+
"package": "@kairotic/controls",
|
|
4
|
+
"version": "0.2.3",
|
|
5
|
+
"api": 1,
|
|
6
|
+
"author": "bonuoq",
|
|
7
|
+
"license": "MPL-2.0",
|
|
8
|
+
"components": [
|
|
9
|
+
{
|
|
10
|
+
"class": "keyboard",
|
|
11
|
+
"license": "MPL-2.0",
|
|
12
|
+
"doc": "Virtual MIDI keyboard. Press a key to play it and let go to release it; dragging glides from key to key. ⇧-click toggles a note in a chord that stays after mouse up (⇧-drag adds every key it passes); a plain click starts over; the strip above the keys releases everything. With the word hold every note stays (a latch) until the strip is clicked. Where the key is touched sets the velocity: soft at the top, full at the bottom. Outlets: Hz (0, most recent note first), voice ids = the MIDI numbers (1), gate (2), [:gain velocity] (3). For synth~ connect 0→0 and 1→1; for faust~ connect 0 and 3 to its left inlet.",
|
|
13
|
+
"source": ";; keyboard · a virtual MIDI keyboard with press and release tied to the mouse\n;; Part of @kairotic/controls. Written against the Kairotic API (api/…).\n;; First made with Claude inside Kairotic (Gallery, discussion #2), reviewed for the package.\n(let [;; ── layout (shared by logic and view) ─────────────────\n white? #{0 2 4 5 7 9 11}\n names [\"C\" \"C#\" \"D\" \"D#\" \"E\" \"F\" \"F#\" \"G\" \"G#\" \"A\" \"A#\" \"B\"]\n note-name (fn [n] (str (nth names (mod n 12)) (dec (quot n 12))))\n hz (fn [n] (* 440 (js/Math.pow 2 (/ (- n 69) 12))))\n kw 16 bw 10 top 14 bh 40 wh 66\n layout (fn [args]\n (let [[lo n] (api/padded (if (map? args) [(:low args) (:keys args)] args) [48 25])\n notes (range lo (min 128 (+ lo (max 1 n))))\n step (fn [[acc wi] m]\n (if (white? (mod m 12))\n [(conj acc {:midi m :white true :x (* wi kw) :w kw}) (inc wi)]\n [(conj acc {:midi m :white false :x (- (* wi kw) (/ bw 2)) :w bw}) wi]))\n [ks wn] (reduce step [[] 0] notes)]\n {:keys ks :width (* wn kw)}))\n ;; the key under (x, y), black keys first in the upper part, :stop on the strip above;\n ;; with the velocity of the touch: soft at the top of the key, full at its bottom edge\n hit (fn [args x y]\n (let [ks (:keys (layout args))\n inside (fn [k] (and (>= x (:x k)) (< x (+ (:x k) (:w k)))))\n white-at (fn [] (:midi (first (filter #(and (:white %) (inside %)) ks))))\n black-at (fn [] (:midi (first (filter #(and (not (:white %)) (inside %)) ks))))\n vel (fn [y0 y1] (api/clamp (+ 0.25 (* 0.75 (/ (- y y0) (- y1 y0)))) 0.25 1))]\n (cond\n (< y top) [:stop 1]\n (and (< y (+ top bh)) (black-at)) [(black-at) (vel top (+ top bh))]\n :else [(white-at) (vel top wh)])))\n\n ;; state: {:held [midi …] most recent first · :vel 0–1 · :keep true when the notes stay after mouse up}\n held-of (fn [state] (vec (if (map? state) (:held state) [])))\n vel-of (fn [state] (or (when (map? state) (:vel state)) 0.8))\n front (fn [held k] (vec (cons k (remove #(= % k) held))))\n ;; outlets, sent in this order: voice ids (1), velocity (3), Hz (0), gate (2) —\n ;; ids before Hz as a reader does, gain before the gate opens\n emit (fn [held vel keep]\n {:state {:held held :vel vel :keep (boolean keep)} :show held\n :out [[1 held] [3 [:gain vel]] [0 (mapv hz held)] [2 (if (seq held) 1 0)]]})\n ;; only a change of the held notes goes out\n move (fn [state held vel keep]\n (if (= (held-of state) held)\n {:state {:held held :vel vel :keep (boolean keep)}}\n (emit held vel keep)))]\n\n {;; ── about ──────────────────────────────────────────────\n :class \"keyboard\"\n :license \"MPL-2.0\"\n :doc \"Virtual MIDI keyboard. Press a key to play it and let go to release it; dragging glides from key to key. ⇧-click toggles a note in a chord that stays after mouse up (⇧-drag adds every key it passes); a plain click starts over; the strip above the keys releases everything. With the word hold every note stays (a latch) until the strip is clicked. Where the key is touched sets the velocity: soft at the top, full at the bottom. Outlets: Hz (0, most recent note first), voice ids = the MIDI numbers (1), gate (2), [:gain velocity] (3). For synth~ connect 0→0 and 1→1; for faust~ connect 0 and 3 to its left inlet.\"\n :usage \"keyboard [lowest-midi-note 48] [number-of-keys 25] [hold]\"\n :io [1 4]\n :size (fn [box] [(:width (layout (or (:args box) []))) wh])\n\n ;; ── arguments ──────────────────────────────────────────\n :args [[:low 48] [:keys 25] [:hold false]]\n\n ;; ── logic ──────────────────────────────────────────────\n ;; While the mouse is down the gesture sends [:key k vel] (plain), [:toggle k vel] (⇧ on press)\n ;; or [:add k vel] (⇧ while dragging). On release (:up) it sends [:up], or [:hold [notes] vel]\n ;; when the notes stay: that is what goes into the history and to the rest of a room, so\n ;; replaying it never leaves a note stuck and a chord arrives whole.\n :receive (fn [{:keys [state value args]}]\n (let [held (held-of state)\n vel (vel-of state)\n latch (:hold args)\n midi (fn [x] (when (number? x) (api/clamp (js/Math.round x) 0 127)))\n tag (when (sequential? value) (first value))\n [_ k v] (when tag value)\n v (if (number? v) v vel)]\n (cond\n (#{:key :toggle :add} tag)\n (cond\n (= k :stop) (move state [] vel false)\n (nil? k) {:state state}\n (= tag :key) (move state [k] v latch)\n (= tag :add) (move state (front held k) v true)\n (some #(= % k) held) (move state (vec (remove #(= % k) held)) vel true)\n :else (move state (front held k) v true))\n (= tag :up) (move state [] vel false)\n (= tag :hold) (move state (vec (distinct (keep midi k))) v true)\n (and (= tag :on) (midi k)) (emit (front held (midi k)) v true)\n (= tag :off) (emit (vec (remove #(= % (midi k)) held)) vel true)\n (number? value) (emit [(midi value)] vel latch)\n (= value :bang) (emit held vel (:keep state))\n (= value :stop) (emit [] vel false)\n :else {:state state})))\n\n :gesture (fn [{:keys [args state shown x y dx dy fine up]}]\n (if up\n (let [held (if (sequential? shown) (vec shown) [])]\n (if (and (seq held) (or (:hold args) (:keep state)))\n [:hold held (:vel state)]\n [:up]))\n (let [[k vel] (hit args x y)]\n (cond\n (not fine) [:key k vel]\n (and (zero? dx) (zero? dy)) [:toggle k vel]\n :else [:add k vel]))))\n\n ;; ── view ───────────────────────────────────────────────\n :draw (fn [{:keys [shown args state]}]\n (let [held (set (when (sequential? shown) shown))\n {ks :keys width :width} (layout args)\n kept (and (map? state) (:keep state) (seq held))\n label (if (seq held)\n (str (when kept \"◆ \") (str/join \" \" (map note-name (sort held))))\n (if (:hold args) \"hold · click = latch · ⇧ chord · here = off\" \"press keys · ⇧ chord · here = off\"))]\n (concat\n [[:rect {:x 0 :y 0 :w width :h (- top 2) :r 2 :stroke :grid :fill :bg}]\n [:text {:x 4 :y 10 :anchor :start :size 9 :fill (if kept :accent :soft)} label]]\n (for [k ks :when (:white k)]\n [:rect {:x (:x k) :y top :w kw :h (- wh top) :stroke :soft :fill (if (held (:midi k)) :accent :bg)}])\n (for [k ks :when (and (:white k) (= 0 (mod (:midi k) 12)))]\n [:text {:x (+ (:x k) 8) :y (- wh 3) :anchor :middle :size 8 :fill :soft} (note-name (:midi k))])\n (for [k ks :when (not (:white k))]\n [:rect {:x (:x k) :y top :w bw :h bh :stroke :ink :fill (if (held (:midi k)) :accent :ink)}]))))\n\n ;; ── help ───────────────────────────────────────────────\n :help {:inlets [\"a MIDI number plays it alone; [:on n] adds a note and [:off n] removes it (both kept); [:hold [n …]] sets the held notes; :stop releases all; :bang sends again\"]\n :outlets [\"list of the held notes in Hz, most recent first (synth~, faust~)\"\n \"voice ids: the held MIDI numbers, sent before the Hz (right inlet of synth~)\"\n \"gate: 1 while a note is held, 0 when none (adsr~)\"\n \"[:gain velocity], 0.25–1, before the Hz (left inlet of faust~)\"]\n :try \"keyboard → synth~ (0→0, 1→1) → dac~: press, hold, let go; ⇧-click several keys for a chord, ⇧-click one again to take it out, click the strip to release. With faust~: outlets 0 and 3 to its left inlet, and touch lower on the keys to play louder.\"}})\n"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"class": "knob",
|
|
17
|
+
"license": "MPL-2.0",
|
|
18
|
+
"doc": "Rotary knob: drag it up or down (⇧ for fine control). Sends its value between min and max; with log the travel is logarithmic (frequencies), and with a name it sends [:name value] (e.g. a faust~ parameter). A number sets it and sends it on; a bang sends it again.",
|
|
19
|
+
"source": ";; knob · a rotary control with its own view in the patch\n;; Part of @kairotic/controls. Written against the Kairotic API (api/…).\n{;; ── about ──────────────────────────────────────────────\n :class \"knob\"\n :license \"MPL-2.0\"\n :doc \"Rotary knob: drag it up or down (⇧ for fine control). Sends its value between min and max; with log the travel is logarithmic (frequencies), and with a name it sends [:name value] (e.g. a faust~ parameter). A number sets it and sends it on; a bang sends it again.\"\n :usage \"knob [min max initial] [log] [name]\"\n :io [1 1]\n :size [72 78]\n\n ;; ── arguments ──────────────────────────────────────────\n ;; numbers fill min, max and initial in order; \"log\" is a flag; any other word is the name\n :args [[:min 0] [:max 1] [:initial :min] [:log false] [:name nil]]\n\n ;; ── logic ──────────────────────────────────────────────\n :receive (fn [{:keys [state args value]}]\n (let [current (if (number? state) state (:initial args))\n outgoing (fn [v] (if (:name args) [(:name args) v] v))]\n (cond\n (number? value) (let [v (api/clamp value (:min args) (:max args))]\n {:state v :out [[0 (outgoing v)]] :show v})\n (= value :bang) {:state current :out [[0 (outgoing current)]] :show current}\n :else {:state current})))\n\n ;; on release the value stays in the box text as its initial value (Kairotic Study 13 and later)\n :persist (fn [v] {:initial (api/nice v)})\n\n :gesture (fn [{:keys [start args dy fine]}]\n (let [{lo :min hi :max log? :log} args\n u (api/to-unit (if (number? start) start (:initial args)) lo hi log?)]\n (api/from-unit (- u (/ dy (if fine 800 160))) lo hi log?)))\n\n ;; values for the holes of the view\n :derive (fn [{:keys [shown args]}]\n (let [{lo :min hi :max log? :log} args\n v (if (number? shown) shown (:initial args))]\n {:angle (+ -135 (* 270 (api/to-unit v lo hi log?)))\n :label (str (when (:name args) (str (name (:name args)) \" \")) (api/nice v))}))\n\n ;; ── view ───────────────────────────────────────────────\n :view [[:arc {:cx 36 :cy 33 :r 23 :from -135 :to 135 :stroke :grid :weight 5}]\n [:arc {:cx 36 :cy 33 :r 23 :from -135 :to :$angle :stroke :accent :weight 5}]\n [:circle {:cx 36 :cy 33 :r 16 :stroke :soft :fill :bg}]\n [:needle {:cx 36 :cy 33 :r0 5 :r 17 :angle :$angle :stroke :ink :weight 2.5}]\n [:text {:x 36 :y 72 :anchor :middle :size 11} :$label]]}\n"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"class": "scale",
|
|
23
|
+
"license": "MPL-2.0",
|
|
24
|
+
"doc": "A number from one range (in-lo…in-hi, 0…1 by default) to another (out-lo…out-hi): straight, or with log in equal ratios (for frequencies, a cutoff, a speed: equal moves are equal intervals); clipped to the range, and reversed when out-hi is under out-lo. With ms, it glides there in that time (a jumpy sensor made smooth). It takes a number, a list whose last element is the number ([:lx 0.3] from gamepad), or a map with :value. :learn: the next values it gets make the input range (move the controller from end to end), :learn :off stops. Between a controller (control, knob, motion…) and whatever it moves.",
|
|
25
|
+
"source": ";; scale · a number from one range to another, straight or logarithmic, smoothed if asked\n;; Part of @kairotic/controls. Written against the Kairotic API (api/…). Discussion #135.\n(let [lin (fn [v a b] (if (== a b) 0 (/ (- v a) (- b a))))\n out-of (fn [u {:keys [out-lo out-hi log]}]\n (let [u (api/clamp u 0 1)]\n (if (and log (pos? out-lo) (pos? out-hi))\n (* out-lo (js/Math.pow (/ out-hi out-lo) u))\n (+ out-lo (* u (- out-hi out-lo))))))\n ;; the number in a message: a number, the last number of a list ([:lx 0.3] from gamepad), or :value of a map\n number-in (fn [v] (cond (number? v) v\n (map? v) (let [x (:value v)] (when (number? x) x))\n (sequential? v) (let [x (last v)] (when (number? x) x))\n :else nil))\n tick 0.02]\n {;; ── about ──\n :class \"scale\"\n :license \"MPL-2.0\"\n :doc \"A number from one range (in-lo…in-hi, 0…1 by default) to another (out-lo…out-hi): straight, or with log in equal ratios (for frequencies, a cutoff, a speed: equal moves are equal intervals); clipped to the range, and reversed when out-hi is under out-lo. With ms, it glides there in that time (a jumpy sensor made smooth). It takes a number, a list whose last element is the number ([:lx 0.3] from gamepad), or a map with :value. :learn: the next values it gets make the input range (move the controller from end to end), :learn :off stops. Between a controller (control, knob, motion…) and whatever it moves.\"\n :usage \"scale [in-lo in-hi out-lo out-hi] [log] [ms]\"\n :io [1 1]\n :size [130 24]\n :args [[:in-lo 0] [:in-hi 1] [:out-lo 0] [:out-hi 1] [:log false] [:ms 0]]\n :help {:inlets [\"a number (or [name value], or {:value v}); :learn; :learn :off\"]\n :outlets [\"the number in the new range\"]\n :messages [[\":learn\" \"the next values make the input range\"] [\":learn :off\" \"keeps the range learnt\"]]\n :example \"(box :b1 \\\"knob\\\" 40 30)\\n(box :b2 \\\"scale 0 1 220 880 log 200\\\" 40 120)\\n(box :b3 \\\"number\\\" 40 160)\\n(connect :b1 0 :b2 0)\\n(connect :b2 0 :b3 0)\"\n :try \"Turn the knob: 220 to 880 Hz in equal steps of pitch, gliding 200 ms.\"\n :see [\"control\" \"knob\" \"mtof\"]}\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state args at]}]\n (let [st (or state {})\n lo (or (:lo st) (:in-lo args)) hi (or (:hi st) (:in-hi args))\n ms (max 0 (:ms args))]\n (cond\n (#{:learn} value) {:state (assoc st :learning true :lo nil :hi nil :seen nil) :show {:learn true}}\n (and (sequential? value) (#{:learn} (first value)) (#{:off 0} (second value)))\n {:state (dissoc st :learning) :show {:learn false}}\n ;; a glide: one step of it, scheduled to itself\n (and (= inlet 0) (vector? value) (= :scale-step (first value)))\n (let [[_ gen] value]\n (if (and (= gen (:gen st)) (:target st))\n (let [k (min 1 (/ tick (max 0.001 (/ ms 1000))))\n cur (+ (:cur st) (* k (- (:target st) (:cur st))))\n done (< (js/Math.abs (- (:target st) cur)) (* 1e-4 (max 1 (js/Math.abs (:target st)))))\n cur (if done (:target st) cur)]\n (cond-> {:state (assoc st :cur cur) :out [[0 (api/nice cur)]] :show {:v cur}}\n (not done) (assoc :later [[(+ at tick) 0 [:scale-step gen]]])))\n {:state st}))\n :else\n (if-let [v (number-in value)]\n (let [st (if (:learning st)\n (let [s (vec (take-last 512 (conj (or (:seen st) []) v)))]\n (assoc st :seen s :lo (apply min s) :hi (apply max s)))\n st)\n lo (or (:lo st) (:in-lo args)) hi (or (:hi st) (:in-hi args))\n target (out-of (lin v lo hi) args)]\n (if (and (pos? ms) (number? (:cur st)))\n (let [gen (inc (or (:gen st) 0))]\n {:state (assoc st :target target :gen gen) :later [[(+ at tick) 0 [:scale-step gen]]] :show {:v (:cur st)}})\n {:state (assoc st :cur target :target target) :out [[0 (api/nice target)]] :show {:v target}}))\n {:state st}))))\n\n ;; ── view ──\n :derive (fn [{:keys [shown args]}]\n {:label (str (if (:learn shown) \"learning… \" \"\")\n (api/nice (:in-lo args)) \"…\" (api/nice (:in-hi args)) \" → \" (api/nice (:out-lo args)) \"…\" (api/nice (:out-hi args))\n (when (:log args) \" log\"))\n :w (* 126 (api/clamp (if (number? (:v shown)) (lin (:v shown) (:out-lo args) (:out-hi args)) 0) 0 1))})\n :view [[:rect {:x 1 :y 1 :w 128 :h 22 :r 3 :stroke :accent :fill :bg}]\n [:rect {:x 2 :y 19 :w :$w :h 3 :r 1 :stroke nil :fill :accent}]\n [:text {:x 6 :y 15 :size 10.5} :$label]]})\n"
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
};
|