@kairotic/analysis 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,27 @@
1
- # Temporary Holding Version
1
+ # @kairotic/analysis
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ *Official package of [Kairotic](https://github.com/bonuoq/kairotic) · [all packages](../README.md)*
4
+
5
+ From any signal — an `adc~`, a `sample` played by a `reader~`, a `faust~` — into messages and pictures:
6
+
7
+ | Class | What it gives |
8
+ |---|---|
9
+ | `features~ [ms] [descriptor …]` | one outlet per descriptor written after it, every ms (20): `rms`, `energy`, `zcr`, `loudness`, `centroid` and `spread` (Hz), `flatness`, `rolloff` (Hz), `kurtosis`, `skewness`, `slope`, `crest`, `sharpness`, `perceptualSpread`, `mfcc` (13), `chroma` (12). Without words: `rms centroid flatness` |
10
+ | `pitch~ [lo hi] [threshold]` | the frequency in Hz (0 once when it stops hearing one) and how clearly periodic it is, by YIN, between lo and hi (60 – 1500 Hz) |
11
+ | `onset~ [threshold] [ms]` | a bang at the beginning of each sound (a note, a stroke, a syllable) and its strength |
12
+ | `chroma~ [ms]` | 12 values from C to B and the strongest pitch class as a word (`:a`, `:c#`…) |
13
+ | `spectrum~ [lo hi]` | the spectrum drawn in the patch, on a log scale of frequency |
14
+
15
+ Each sends only what changed. `pitch~` runs YIN in its own AudioWorklet; the others take frames of 2048
16
+ samples through the Kairotic API (`api/tap`) and compute them with
17
+ [Meyda](https://meyda.js.org) (MIT), loaded from jsDelivr the first time one is used.
18
+
19
+ ```clojure
20
+ (box :b1 "adc~ 1" 40 30)
21
+ (box :b2 "pitch~ 80 1000" 40 90)
22
+ (box :b3 "ftom" 40 150)
23
+ (box :b4 "print" 40 200)
24
+ (connect :b1 0 :b2 0) (connect :b2 0 :b3 0) (connect :b3 0 :b4 0)
25
+ ```
26
+
27
+ Writing one of its classes in a work brings the package into it. Licence: [MPL-2.0](../../LICENSE).
@@ -0,0 +1,9 @@
1
+ // Generated by packages/build.mjs from src/chroma.cljs
2
+ export default {
3
+ "class": "chroma~",
4
+ "version": "0.1.0",
5
+ "author": "bonuoq",
6
+ "license": "MPL-2.0",
7
+ "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.",
8
+ "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) :alive (atom true) :inlets [[in 0]] :outlets []}\n on-block (fn [data at & _]\n (when-let [M @(: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) (.connect in w) (try (.disconnect w) (catch :default _ nil)))))\n (.catch (fn [e] (js/console.error \"chroma~\" e))))\n node))\n :dispose (fn [node]\n (reset! (:alive node) false)\n (try (.disconnect (:in node)) (catch :default _ nil)))}})\n"
9
+ };
@@ -0,0 +1,9 @@
1
+ // Generated by packages/build.mjs from src/features.cljs
2
+ export default {
3
+ "class": "features~",
4
+ "version": "0.1.0",
5
+ "author": "bonuoq",
6
+ "license": "MPL-2.0",
7
+ "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.",
8
+ "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 (when-let [M @(: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 (try (.disconnect (:in node)) (catch :default _ nil)))}})\n"
9
+ };
package/dist/index.mjs ADDED
@@ -0,0 +1,40 @@
1
+ // Generated by packages/build.mjs
2
+ export default {
3
+ "package": "@kairotic/analysis",
4
+ "version": "0.1.0",
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) :alive (atom true) :inlets [[in 0]] :outlets []}\n on-block (fn [data at & _]\n (when-let [M @(: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) (.connect in w) (try (.disconnect w) (catch :default _ nil)))))\n (.catch (fn [e] (js/console.error \"chroma~\" e))))\n node))\n :dispose (fn [node]\n (reset! (:alive node) false)\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 (when-let [M @(: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 (try (.disconnect (:in node)) (catch :default _ nil)))}})\n"
20
+ },
21
+ {
22
+ "class": "onset~",
23
+ "license": "MPL-2.0",
24
+ "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 right. threshold (3) is how far above the usual change of the last frames an onset must rise; ms (80) the least time between two. Every 512 samples (about 11 ms), on frames of 2048. Into a counter, a metro that follows, a sample's rec.",
25
+ "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), 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)))\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) 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 right. threshold (3) is how far above the usual change of the last frames an onset must rise; ms (80) the least time between two. Every 512 samples (about 11 ms), on frames of 2048. Into a counter, a metro that follows, a sample's rec.\"\n :usage \"onset~ [threshold] [ms]\"\n :io [1 2]\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 (and (= inlet :audio) (map? value))\n (let [[st strength] (detect state value at (api/clamp (:threshold args) 1 50) (api/clamp (:ms args) 10 2000))]\n (if strength\n {:state st :out [[1 strength] [0 :bang]] :show {:at at :strength strength}}\n {:state st}))\n :else {}))\n\n ;; ── view: a dot that lights on each onset ──\n :derive (fn [{:keys [shown]}]\n {:label (if (:strength shown) (str (:strength shown)) \"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) :alive (atom true) :inlets [[in 0]] :outlets []}\n on-block (fn [data at & _]\n (when-let [M @(: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) (.connect in w) (try (.disconnect w) (catch :default _ nil)))))\n (.catch (fn [e] (js/console.error \"onset~\" e))))\n node))\n :dispose (fn [node]\n (reset! (:alive node) false)\n (try (.disconnect (:in node)) (catch :default _ nil)))}})\n"
26
+ },
27
+ {
28
+ "class": "pitch~",
29
+ "license": "MPL-2.0",
30
+ "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~.",
31
+ "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 (try (.disconnect (:in node)) (catch :default _ nil)))}})\n"
32
+ },
33
+ {
34
+ "class": "spectrum~",
35
+ "license": "MPL-2.0",
36
+ "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.",
37
+ "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) :alive (atom true) :inlets [[in 0]] :outlets []}\n on-block (fn [data at & _]\n (when-let [M @(: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) (.connect in w) (try (.disconnect w) (catch :default _ nil)))))\n (.catch (fn [e] (js/console.error \"spectrum~\" e))))\n node))\n :dispose (fn [node]\n (reset! (:alive node) false)\n (try (.disconnect (:in node)) (catch :default _ nil)))}})\n"
38
+ }
39
+ ]
40
+ };
package/dist/onset.mjs ADDED
@@ -0,0 +1,9 @@
1
+ // Generated by packages/build.mjs from src/onset.cljs
2
+ export default {
3
+ "class": "onset~",
4
+ "version": "0.1.0",
5
+ "author": "bonuoq",
6
+ "license": "MPL-2.0",
7
+ "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 right. threshold (3) is how far above the usual change of the last frames an onset must rise; ms (80) the least time between two. Every 512 samples (about 11 ms), on frames of 2048. Into a counter, a metro that follows, a sample's rec.",
8
+ "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), 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)))\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) 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 right. threshold (3) is how far above the usual change of the last frames an onset must rise; ms (80) the least time between two. Every 512 samples (about 11 ms), on frames of 2048. Into a counter, a metro that follows, a sample's rec.\"\n :usage \"onset~ [threshold] [ms]\"\n :io [1 2]\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 (and (= inlet :audio) (map? value))\n (let [[st strength] (detect state value at (api/clamp (:threshold args) 1 50) (api/clamp (:ms args) 10 2000))]\n (if strength\n {:state st :out [[1 strength] [0 :bang]] :show {:at at :strength strength}}\n {:state st}))\n :else {}))\n\n ;; ── view: a dot that lights on each onset ──\n :derive (fn [{:keys [shown]}]\n {:label (if (:strength shown) (str (:strength shown)) \"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) :alive (atom true) :inlets [[in 0]] :outlets []}\n on-block (fn [data at & _]\n (when-let [M @(: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) (.connect in w) (try (.disconnect w) (catch :default _ nil)))))\n (.catch (fn [e] (js/console.error \"onset~\" e))))\n node))\n :dispose (fn [node]\n (reset! (:alive node) false)\n (try (.disconnect (:in node)) (catch :default _ nil)))}})\n"
9
+ };
package/dist/pitch.mjs ADDED
@@ -0,0 +1,9 @@
1
+ // Generated by packages/build.mjs from src/pitch.cljs
2
+ export default {
3
+ "class": "pitch~",
4
+ "version": "0.1.0",
5
+ "author": "bonuoq",
6
+ "license": "MPL-2.0",
7
+ "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~.",
8
+ "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 (try (.disconnect (:in node)) (catch :default _ nil)))}})\n"
9
+ };
@@ -0,0 +1,9 @@
1
+ // Generated by packages/build.mjs from src/spectrum.cljs
2
+ export default {
3
+ "class": "spectrum~",
4
+ "version": "0.1.0",
5
+ "author": "bonuoq",
6
+ "license": "MPL-2.0",
7
+ "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.",
8
+ "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) :alive (atom true) :inlets [[in 0]] :outlets []}\n on-block (fn [data at & _]\n (when-let [M @(: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) (.connect in w) (try (.disconnect w) (catch :default _ nil)))))\n (.catch (fn [e] (js/console.error \"spectrum~\" e))))\n node))\n :dispose (fn [node]\n (reset! (:alive node) false)\n (try (.disconnect (:in node)) (catch :default _ nil)))}})\n"
9
+ };
package/package.json CHANGED
@@ -1,6 +1,40 @@
1
1
  {
2
2
  "name": "@kairotic/analysis",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Kairotic analysis components: features~ (descriptors with Meyda), pitch~ (YIN), onset~, chroma~ and spectrum~, from any signal or adc~ into messages and pictures",
5
+ "author": "bonuoq",
6
+ "license": "MPL-2.0",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/bonuoq/kairotic.git",
10
+ "directory": "packages/analysis"
11
+ },
12
+ "homepage": "https://github.com/bonuoq/kairotic/tree/main/packages#readme",
13
+ "type": "module",
14
+ "main": "dist/index.mjs",
15
+ "files": [
16
+ "dist",
17
+ "src",
18
+ "README.md"
19
+ ],
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "kairotic": {
24
+ "api": 1
25
+ },
26
+ "scripts": {
27
+ "build": "node ../build.mjs analysis",
28
+ "prepublishOnly": "node ../build.mjs analysis"
29
+ },
30
+ "keywords": [
31
+ "kairotic",
32
+ "music",
33
+ "web audio",
34
+ "analysis",
35
+ "pitch",
36
+ "onset",
37
+ "chroma",
38
+ "meyda"
39
+ ]
40
+ }
@@ -0,0 +1,68 @@
1
+ ;; chroma~ · how much of each pitch class a sound has
2
+ ;; Part of @kairotic/analysis. Written against the Kairotic API (api/…). The chroma is Meyda's (MIT,
3
+ ;; meyda.js.org), loaded from jsDelivr the first time one is needed.
4
+ (let [meyda-url "npm:meyda@5.6.3/dist/web/meyda.min.js"
5
+ size 2048 ; the frames every analysis box of the package uses (see features~)
6
+ gate 0.005 ; rms under it is silence: no strongest class
7
+ classes [:c :c# :d :d# :e :f :f# :g :g# :a :a# :b]
8
+ names ["C" "C#" "D" "D#" "E" "F" "F#" "G" "G#" "A" "A#" "B"]
9
+ strongest (fn [xs] (when (seq xs) (nth classes (first (apply max-key (fn [[_ v]] v) (map-indexed vector xs))))))
10
+ ;; a picture of 12 bars in 168 × 64
11
+ bar-w 13 x0 6 base 46 tall 38]
12
+ {;; ── about ──
13
+ :class "chroma~"
14
+ :license "MPL-2.0"
15
+ :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."
16
+ :usage "chroma~ [ms]"
17
+ :io [1 2]
18
+ :size [168 64]
19
+ :args [[:ms 40]]
20
+
21
+ ;; ── logic ──
22
+ :receive (fn [{:keys [inlet value state]}]
23
+ (cond
24
+ (and (= inlet :audio) (:error value)) {:effects [[:print (:error value)]]}
25
+ (and (= inlet :audio) (map? value) (> (or (:rms value) 0) gate) (= 12 (count (:chroma value))))
26
+ (let [xs (:chroma value) top (strongest xs)
27
+ outs (cond-> []
28
+ (not= top (:top state)) (conj [1 top])
29
+ (not= xs (:xs state)) (conj [0 xs]))]
30
+ (if (seq outs) {:state {:xs xs :top top} :out outs :show xs} {}))
31
+ :else {}))
32
+
33
+ ;; ── view ──
34
+ :draw (fn [{:keys [shown]}]
35
+ (let [xs (if (and (sequential? shown) (= 12 (count shown))) shown (repeat 12 0))
36
+ top (when (some pos? xs) (apply max xs))]
37
+ (into [[:rect {:x 2 :y 2 :w 164 :h 60 :r 4 :stroke :grid :fill :bg}]]
38
+ (mapcat (fn [i v n]
39
+ (let [h (* tall (api/clamp v 0 1)) x (+ x0 (* i bar-w))]
40
+ [[:rect {:x x :y (- base h) :w (- bar-w 3) :h h :r 1 :stroke nil
41
+ :fill (if (and top (= v top)) :kairos :accent)}]
42
+ [:text {:x (+ x (/ (- bar-w 3) 2)) :y 58 :anchor :middle :size 7 :fill :soft} n]]))
43
+ (range 12) xs names))))
44
+
45
+ ;; ── audio ──
46
+ :audio {:create (fn [ac b _]
47
+ (let [in (.createGain ac)
48
+ sr (.-sampleRate ac)
49
+ ms (api/clamp (or (first (filter number? (:args b))) 40) 10 2000)
50
+ node {:in in :M (atom nil) :alive (atom true) :inlets [[in 0]] :outlets []}
51
+ on-block (fn [data at & _]
52
+ (when-let [M @(:M node)]
53
+ (set! (.-bufferSize M) size)
54
+ (set! (.-sampleRate M) sr)
55
+ (let [r (.extract M #js ["chroma" "rms"] (first data))
56
+ xs (mapv (fn [x] (/ (js/Math.round (* 100 (if (js/isFinite x) x 0))) 100))
57
+ (array-seq (.-chroma r)))]
58
+ (api/report! (:id b) {:chroma xs :rms (.-rms r)} at))))]
59
+ (-> (api/load-script meyda-url)
60
+ (.then (fn [_] (reset! (:M node) (.-Meyda js/window))))
61
+ (.catch (fn [e] (api/report! (:id b) {:error (str "chroma~: " (.-message e))} nil))))
62
+ (-> (api/tap ac {:mode :frames :size size :hop (js/Math.round (* sr (/ ms 1000)))} on-block)
63
+ (.then (fn [w] (if @(:alive node) (.connect in w) (try (.disconnect w) (catch :default _ nil)))))
64
+ (.catch (fn [e] (js/console.error "chroma~" e))))
65
+ node))
66
+ :dispose (fn [node]
67
+ (reset! (:alive node) false)
68
+ (try (.disconnect (:in node)) (catch :default _ nil)))}})
@@ -0,0 +1,69 @@
1
+ ;; features~ · descriptors of a sound, every few milliseconds
2
+ ;; Part of @kairotic/analysis. Written against the Kairotic API (api/…). The descriptors are Meyda's
3
+ ;; (MIT, meyda.js.org), loaded from jsDelivr the first time one is needed.
4
+ (let [meyda-url "npm:meyda@5.6.3/dist/web/meyda.min.js"
5
+ ;; every analysis box of the package uses frames of this size: Meyda is one object for the whole
6
+ ;; page and keeps some filter banks for the size it saw first
7
+ size 2048
8
+ aliases {:centroid :spectralCentroid :flatness :spectralFlatness :rolloff :spectralRolloff
9
+ :spread :spectralSpread :kurtosis :spectralKurtosis :skewness :spectralSkewness
10
+ :slope :spectralSlope :crest :spectralCrest :sharpness :perceptualSharpness}
11
+ known #{:rms :energy :zcr :loudness :mfcc :chroma :spectralCentroid :spectralFlatness :spectralRolloff
12
+ :spectralSpread :spectralKurtosis :spectralSkewness :spectralSlope :spectralCrest
13
+ :perceptualSharpness :perceptualSpread}
14
+ feats (fn [args]
15
+ (let [ws (->> args (filter keyword?) (map (fn [k] (get aliases k k))) (filter known) distinct vec)]
16
+ (if (seq ws) ws [:rms :spectralCentroid :spectralFlatness])))
17
+ hop-ms (fn [args] (api/clamp (or (first (filter number? args)) 20) 5 1000))
18
+ ok? (fn [x] (and (number? x) (js/isFinite x)))
19
+ ;; a value as it leaves the box: Hz where Meyda gives bins, the total of loudness, lists rounded
20
+ value (fn [k v sr]
21
+ (cond
22
+ (#{:spectralCentroid :spectralSpread} k) (when (ok? v) (api/nice (* v (/ sr size))))
23
+ (= k :loudness) (when v (let [t (.-total v)] (when (ok? t) (api/nice t))))
24
+ (#{:mfcc :chroma} k) (when v (let [xs (vec (array-seq v))] (when (every? ok? xs) (mapv api/nice xs))))
25
+ :else (when (ok? v) (api/nice v))))]
26
+ {;; ── about ──
27
+ :class "features~"
28
+ :license "MPL-2.0"
29
+ :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."
30
+ :usage "features~ [ms] [descriptor …]"
31
+ :io (fn [b] [1 (count (feats (:args b)))])
32
+ :dynamic-io true
33
+
34
+ ;; ── logic ──
35
+ :receive (fn [{:keys [inlet value state args]}]
36
+ (cond
37
+ (and (= inlet :audio) (:error value)) {:effects [[:print (:error value)]]}
38
+ (and (= inlet :audio) (map? value))
39
+ (let [fs (feats args)
40
+ outs (vec (keep-indexed (fn [i k] (let [v (get value k)]
41
+ (when (and (some? v) (not= v (get state k))) [i v])))
42
+ fs))]
43
+ (if (seq outs) {:state (merge state (select-keys value fs)) :out outs} {}))
44
+ :else {}))
45
+
46
+ ;; ── audio ──
47
+ :audio {:create (fn [ac b _]
48
+ (let [in (.createGain ac)
49
+ sr (.-sampleRate ac)
50
+ fs (feats (:args b))
51
+ names (clj->js (mapv name fs))
52
+ node {:in in :M (atom nil) :tap (atom nil) :alive (atom true) :inlets [[in 0]] :outlets []}
53
+ on-block (fn [data at & _]
54
+ (when-let [M @(:M node)]
55
+ (set! (.-bufferSize M) size)
56
+ (set! (.-sampleRate M) sr)
57
+ (let [r (.extract M names (first data))]
58
+ (api/report! (:id b) (into {} (for [k fs] [k (value k (aget r (name k)) sr)])) at))))]
59
+ (-> (api/load-script meyda-url)
60
+ (.then (fn [_] (reset! (:M node) (.-Meyda js/window))))
61
+ (.catch (fn [e] (api/report! (:id b) {:error (str "features~: " (.-message e))} nil))))
62
+ (-> (api/tap ac {:mode :frames :size size :hop (js/Math.round (* sr (/ (hop-ms (:args b)) 1000)))} on-block)
63
+ (.then (fn [w] (if @(:alive node) (do (.connect in w) (reset! (:tap node) w))
64
+ (try (.disconnect w) (catch :default _ nil)))))
65
+ (.catch (fn [e] (js/console.error "features~" e))))
66
+ node))
67
+ :dispose (fn [node]
68
+ (reset! (:alive node) false)
69
+ (try (.disconnect (:in node)) (catch :default _ nil)))}})
package/src/onset.cljs ADDED
@@ -0,0 +1,78 @@
1
+ ;; onset~ · the beginnings of sounds: a note, a stroke, a syllable
2
+ ;; Part of @kairotic/analysis. Written against the Kairotic API (api/…). The spectrum is Meyda's (MIT,
3
+ ;; meyda.js.org), loaded from jsDelivr the first time one is needed.
4
+ (let [meyda-url "npm:meyda@5.6.3/dist/web/meyda.min.js"
5
+ size 2048 ; the frames every analysis box of the package uses (see features~)
6
+ bins 256 ; the spectrum up to about 6 kHz at 48 kHz: where onsets are heard
7
+ ;; how much the spectrum grew since the frame before, relative to what sounds now (0 steady … 1
8
+ ;; from silence): what grows counts, what fades does not
9
+ flux (fn [cur prev]
10
+ (if-not prev
11
+ 0
12
+ (loop [i 1 up 0 all 0]
13
+ (if (< i bins)
14
+ (let [a (aget cur i) d (- a (aget prev i))]
15
+ (recur (inc i) (if (pos? d) (+ up d) up) (+ all a)))
16
+ (if (> all 1e-6) (/ up all) 0)))))
17
+ median (fn [xs] (if (empty? xs) 0 (nth (sort xs) (quot (count xs) 2))))
18
+ gate 0.005 ; rms under it (about -46 dB) is silence
19
+ ;; the decision, pure: an onset where the flux rises above threshold × the median of the last
20
+ ;; frames (and above 0.1), not sooner than ms after the last one
21
+ detect (fn [state {:keys [flux rms]} at threshold ms]
22
+ (let [hist (vec (take-last 16 (:hist state)))
23
+ m (median hist)
24
+ over (and (> rms gate) (> flux 0.1) (> flux (* threshold m)))
25
+ fire (and over (not (:over state)) (or (nil? (:last state)) (>= (- at (:last state)) (/ ms 1000))))]
26
+ [(cond-> (assoc state :hist (conj hist flux) :over over) fire (assoc :last at))
27
+ (when fire (/ (js/Math.round (* 100 flux)) 100))]))]
28
+ {;; ── about ──
29
+ :class "onset~"
30
+ :license "MPL-2.0"
31
+ :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 right. threshold (3) is how far above the usual change of the last frames an onset must rise; ms (80) the least time between two. Every 512 samples (about 11 ms), on frames of 2048. Into a counter, a metro that follows, a sample's rec."
32
+ :usage "onset~ [threshold] [ms]"
33
+ :io [1 2]
34
+ :size [64 30]
35
+ :args [[:threshold 3] [:ms 80]]
36
+
37
+ ;; ── logic ──
38
+ :receive (fn [{:keys [inlet value state args at]}]
39
+ (cond
40
+ (and (= inlet :audio) (:error value)) {:effects [[:print (:error value)]]}
41
+ (and (= inlet :audio) (map? value))
42
+ (let [[st strength] (detect state value at (api/clamp (:threshold args) 1 50) (api/clamp (:ms args) 10 2000))]
43
+ (if strength
44
+ {:state st :out [[1 strength] [0 :bang]] :show {:at at :strength strength}}
45
+ {:state st}))
46
+ :else {}))
47
+
48
+ ;; ── view: a dot that lights on each onset ──
49
+ :derive (fn [{:keys [shown]}]
50
+ {:label (if (:strength shown) (str (:strength shown)) "onset")})
51
+ :view [[:rect {:x 2 :y 2 :w 60 :h 26 :r 3 :stroke :grid :fill :bg}]
52
+ [:circle {:cx 15 :cy 15 :r 6 :stroke :accent :fill :accent}]
53
+ [:text {:x 26 :y 19 :size 10 :fill :soft} :$label]]
54
+
55
+ ;; ── audio ──
56
+ :audio {:create (fn [ac b _]
57
+ (let [in (.createGain ac)
58
+ sr (.-sampleRate ac)
59
+ node {:in in :M (atom nil) :prev (atom nil) :alive (atom true) :inlets [[in 0]] :outlets []}
60
+ on-block (fn [data at & _]
61
+ (when-let [M @(:M node)]
62
+ (set! (.-bufferSize M) size)
63
+ (set! (.-sampleRate M) sr)
64
+ (let [r (.extract M #js ["amplitudeSpectrum" "rms"] (first data))
65
+ cur (.-amplitudeSpectrum r)
66
+ f (flux cur @(:prev node))]
67
+ (reset! (:prev node) (.slice cur 0 bins))
68
+ (api/report! (:id b) {:flux f :rms (.-rms r)} at))))]
69
+ (-> (api/load-script meyda-url)
70
+ (.then (fn [_] (reset! (:M node) (.-Meyda js/window))))
71
+ (.catch (fn [e] (api/report! (:id b) {:error (str "onset~: " (.-message e))} nil))))
72
+ (-> (api/tap ac {:mode :frames :size size :hop 512} on-block)
73
+ (.then (fn [w] (if @(:alive node) (.connect in w) (try (.disconnect w) (catch :default _ nil)))))
74
+ (.catch (fn [e] (js/console.error "onset~" e))))
75
+ node))
76
+ :dispose (fn [node]
77
+ (reset! (:alive node) false)
78
+ (try (.disconnect (:in node)) (catch :default _ nil)))}})
package/src/pitch.cljs ADDED
@@ -0,0 +1,130 @@
1
+ ;; pitch~ · the pitch of a sound, by YIN
2
+ ;; Part of @kairotic/analysis. Written against the Kairotic API (api/…), with its AudioWorklet inside.
3
+ (let [worklet "class KairoticYin extends AudioWorkletProcessor {
4
+ // YIN (de Cheveigné & Kawahara, 2002) on the last W samples, every `hop` samples: the period whose
5
+ // cumulative mean normalized difference first falls under the threshold (refined by a parabola),
6
+ // between lo and hi Hz. Hands {hz, clarity, rms}; hz 0 where it finds no pitch or hears silence.
7
+ constructor(o) {
8
+ super();
9
+ const p = (o && o.processorOptions) || {};
10
+ this.W = 1024; this.hop = p.hop || 1024; this.count = 0;
11
+ this.set(p.lo || 60, p.hi || 1500, p.threshold || 0.15);
12
+ this.port.onmessage = (e) => { const d = e.data || {}; this.set(d.lo || this.lo, d.hi || this.hi, d.threshold || this.th); };
13
+ }
14
+ set(lo, hi, th) {
15
+ 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));
16
+ this.T = Math.min(4096, Math.ceil(sampleRate / this.lo) + 1);
17
+ this.tmin = Math.max(2, Math.floor(sampleRate / this.hi));
18
+ this.N = this.W + this.T + 1;
19
+ this.ring = new Float32Array(this.N); this.buf = new Float32Array(this.N); this.d = new Float32Array(this.T + 2);
20
+ this.w = 0;
21
+ }
22
+ analyse() {
23
+ const N = this.N, b = this.buf, r = this.ring, W = this.W, T = this.T, d = this.d;
24
+ for (let i = 0; i < N; i++) b[i] = r[(this.w + i) % N];
25
+ let e = 0; for (let i = N - W; i < N; i++) e += b[i] * b[i];
26
+ const rms = Math.sqrt(e / W);
27
+ if (rms < 0.002) return { hz: 0, clarity: 0, rms };
28
+ // the difference function, stopped as soon as the first dip under the threshold has its minimum
29
+ d[0] = 1; let sum = 0, best = -1;
30
+ for (let tau = 1; tau <= T; tau++) {
31
+ let s = 0;
32
+ for (let j = 0; j < W; j++) { const x = b[j] - b[j + tau]; s += x * x; }
33
+ sum += s; d[tau] = sum > 0 ? s * tau / sum : 1;
34
+ if (tau - 1 >= this.tmin && d[tau - 1] < this.th && d[tau] >= d[tau - 1]) { best = tau - 1; break; }
35
+ }
36
+ if (best < 1) return { hz: 0, clarity: 0, rms };
37
+ let t = best;
38
+ const a = d[best - 1], c = d[best], z = d[best + 1], den = a + z - 2 * c;
39
+ if (den > 0) t = best + (a - z) / (2 * den);
40
+ return { hz: sampleRate / t, clarity: Math.max(0, Math.min(1, 1 - c)), rms };
41
+ }
42
+ process(inputs) {
43
+ const ch = inputs[0] && inputs[0][0];
44
+ const n = ch ? ch.length : 128;
45
+ for (let i = 0; i < n; i++) {
46
+ this.ring[this.w] = ch ? ch[i] : 0;
47
+ this.w = (this.w + 1) % this.N;
48
+ if (++this.count >= this.hop) {
49
+ this.count = 0;
50
+ const r = this.analyse();
51
+ this.port.postMessage({ hz: r.hz, clarity: r.clarity, rms: r.rms, at: currentTime + (i + 1) / sampleRate });
52
+ }
53
+ }
54
+ return true;
55
+ }
56
+ }
57
+ registerProcessor('kairotic-yin', KairoticYin);
58
+ "
59
+ ;; what it says between a pitch it heard and the last one it sent: a change of more than a tenth of a cent
60
+ changed? (fn [a b] (or (nil? a) (nil? b) (zero? a) (> (js/Math.abs (- (api/hz->midi b) (api/hz->midi a))) 0.001)))
61
+ label (fn [hz]
62
+ (if (and (number? hz) (pos? hz))
63
+ (let [m (api/hz->midi hz) r (js/Math.round m) c (js/Math.round (* 100 (- m r)))]
64
+ (str (api/midi-name r) " " (if (neg? c) "" "+") c "¢"))
65
+ "–"))]
66
+ {;; ── about ──
67
+ :class "pitch~"
68
+ :license "MPL-2.0"
69
+ :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~."
70
+ :usage "pitch~ [lo hi] [threshold]"
71
+ :io [1 2]
72
+ :size [124 34]
73
+ :args [[:lo 60] [:hi 1500] [:threshold 0.15]]
74
+
75
+ ;; ── logic ──
76
+ :receive (fn [{:keys [inlet value state]}]
77
+ (if (and (= inlet :audio) (map? value))
78
+ (let [{:keys [hz clarity]} value
79
+ hz (if (and (number? hz) (pos? hz)) (/ (js/Math.round (* hz 100)) 100) 0)
80
+ cl (/ (js/Math.round (* 100 (or clarity 0))) 100)
81
+ last-hz (:hz state)
82
+ send-hz (cond (pos? hz) (when (changed? last-hz hz) hz)
83
+ (and last-hz (pos? last-hz)) 0)
84
+ outs (cond-> []
85
+ (some? send-hz) (conj [0 send-hz])
86
+ (not= cl (:clarity state)) (conj [1 cl]))]
87
+ (if (seq outs)
88
+ {:state {:hz (if (some? send-hz) send-hz last-hz) :clarity cl} :out outs :show {:hz hz :clarity cl}}
89
+ {}))
90
+ {}))
91
+
92
+ ;; ── view ──
93
+ :derive (fn [{:keys [shown]}]
94
+ (let [hz (:hz shown)]
95
+ {:note (label hz)
96
+ :hz (if (and (number? hz) (pos? hz)) (str (api/nice hz) " Hz") "")
97
+ :bar (* 116 (api/clamp (or (:clarity shown) 0) 0 1))}))
98
+ :view [[:rect {:x 2 :y 2 :w 120 :h 30 :r 3 :stroke :grid :fill :bg}]
99
+ [:rect {:x 4 :y 28 :w :$bar :h 2 :r 1 :stroke nil :fill :accent}]
100
+ [:text {:x 6 :y 20 :size 13} :$note]
101
+ [:text {:x 118 :y 20 :anchor :end :size 9 :fill :soft} :$hz]]
102
+
103
+ ;; ── audio ──
104
+ :audio {:create (fn [ac b _]
105
+ (let [in (.createGain ac)
106
+ args (:args b)
107
+ [lo hi th] (api/padded args [60 1500 0.15])
108
+ node {:in in :w (atom nil) :alive (atom true) :inlets [[in 0]] :outlets []}]
109
+ (-> (api/worklet-node ac "kairotic-yin" worklet
110
+ {:numberOfInputs 1 :numberOfOutputs 0 :channelCount 1 :channelCountMode "explicit"
111
+ :processorOptions {:lo lo :hi hi :threshold th}})
112
+ (.then (fn [w]
113
+ (if @(:alive node)
114
+ (do (set! (.. w -port -onmessage)
115
+ (fn [e] (let [d (.-data e)]
116
+ (when @(:alive node)
117
+ (api/report! (:id b) {:hz (.-hz d) :clarity (.-clarity d)} (.-at d))))))
118
+ (.connect in w)
119
+ (reset! (:w node) w))
120
+ (try (.disconnect w) (catch :default _ nil)))))
121
+ (.catch (fn [e] (js/console.error "pitch~" e))))
122
+ node))
123
+ ;; a new text: new limits, without a new worklet
124
+ :update (fn [node b _]
125
+ (when-let [w @(:w node)]
126
+ (let [[lo hi th] (api/padded (:args b) [60 1500 0.15])]
127
+ (.postMessage (.-port w) #js {:lo lo :hi hi :threshold th}))))
128
+ :dispose (fn [node]
129
+ (reset! (:alive node) false)
130
+ (try (.disconnect (:in node)) (catch :default _ nil)))}})
@@ -0,0 +1,82 @@
1
+ ;; spectrum~ · the spectrum of a sound, drawn in the patch
2
+ ;; Part of @kairotic/analysis. Written against the Kairotic API (api/…). The spectrum is Meyda's (MIT,
3
+ ;; meyda.js.org), loaded from jsDelivr the first time one is needed.
4
+ (let [meyda-url "npm:meyda@5.6.3/dist/web/meyda.min.js"
5
+ size 2048 ; the frames every analysis box of the package uses (see features~)
6
+ w 240 h 96 cols 116 ; the picture: 116 columns of 2 px, from x 4 to 236
7
+ floor -90 ; dB at the bottom
8
+ ;; for each bin of the spectrum, its column on a log scale of frequency from lo to hi (nil outside)
9
+ column-map (fn [sr lo hi]
10
+ (let [l0 (js/Math.log lo) span (- (js/Math.log hi) l0)]
11
+ (vec (for [k (range (/ size 2))]
12
+ (let [f (* k (/ sr size))]
13
+ (when (and (>= f lo) (< f hi))
14
+ (js/Math.floor (* cols (/ (- (js/Math.log f) l0) span)))))))))
15
+ ;; the loudest bin of each column, in dB below a full-scale sine (whose bin is size/4 with Hann)
16
+ columns (fn [spec cmap]
17
+ (let [out (js/Array. cols)]
18
+ (.fill out floor)
19
+ (loop [k 1]
20
+ (when (< k (count cmap))
21
+ (when-let [c (nth cmap k)]
22
+ (let [a (aget spec k)
23
+ d (if (> a 1e-9) (* 20 (/ (js/Math.log (/ a (/ size 4))) js/Math.LN10)) floor)]
24
+ (when (> d (aget out c)) (aset out c d))))
25
+ (recur (inc k))))
26
+ ;; columns with no bin of their own (low frequencies) take the one before
27
+ (loop [c 1]
28
+ (when (< c cols)
29
+ (when (= floor (aget out c)) (aset out c (aget out (dec c))))
30
+ (recur (inc c))))
31
+ (vec out)))
32
+ points (fn [ds]
33
+ (vec (map-indexed (fn [i d] [(+ 4 (* 2 i)) (+ 4 (* (- h 18) (/ (- (api/clamp d floor 0)) (- floor))))]) ds)))]
34
+ {;; ── about ──
35
+ :class "spectrum~"
36
+ :license "MPL-2.0"
37
+ :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."
38
+ :usage "spectrum~ [lo hi]"
39
+ :io [1 0]
40
+ :size [240 96]
41
+ :args [[:lo 40] [:hi 16000]]
42
+
43
+ ;; ── logic ──
44
+ :receive (fn [{:keys [inlet value]}]
45
+ (cond
46
+ (and (= inlet :audio) (:error value)) {:effects [[:print (:error value)]]}
47
+ (and (= inlet :audio) (vector? value)) {:show value}
48
+ :else {}))
49
+
50
+ ;; ── view ──
51
+ :derive (fn [{:keys [shown args]}]
52
+ {:pts (when (vector? shown) (points shown))
53
+ :range (str (api/nice (:lo args)) " – " (api/nice (:hi args)) " Hz")})
54
+ :view [[:rect {:x 2 :y 2 :w 236 :h 92 :r 4 :stroke :grid :fill :bg}]
55
+ [:line {:x1 4 :y1 30 :x2 236 :y2 30 :stroke :grid}]
56
+ [:line {:x1 4 :y1 54 :x2 236 :y2 54 :stroke :grid}]
57
+ [:path {:points :$pts :stroke :accent :weight 1.2}]
58
+ [:text {:x 234 :y 90 :anchor :end :size 8 :fill :soft} :$range]]
59
+
60
+ ;; ── audio ──
61
+ :audio {:create (fn [ac b _]
62
+ (let [in (.createGain ac)
63
+ sr (.-sampleRate ac)
64
+ [lo hi] (api/padded (:args b) [40 16000])
65
+ cmap (column-map sr (api/clamp (min lo hi) 10 20000) (api/clamp (max lo hi) 20 (/ sr 2)))
66
+ node {:in in :M (atom nil) :alive (atom true) :inlets [[in 0]] :outlets []}
67
+ on-block (fn [data at & _]
68
+ (when-let [M @(:M node)]
69
+ (set! (.-bufferSize M) size)
70
+ (set! (.-sampleRate M) sr)
71
+ (let [spec (.extract M "amplitudeSpectrum" (first data))]
72
+ (api/report! (:id b) (columns spec cmap) at))))]
73
+ (-> (api/load-script meyda-url)
74
+ (.then (fn [_] (reset! (:M node) (.-Meyda js/window))))
75
+ (.catch (fn [e] (api/report! (:id b) {:error (str "spectrum~: " (.-message e))} nil))))
76
+ (-> (api/tap ac {:mode :frames :size size :hop (js/Math.round (/ sr 30))} on-block)
77
+ (.then (fn [w] (if @(:alive node) (.connect in w) (try (.disconnect w) (catch :default _ nil)))))
78
+ (.catch (fn [e] (js/console.error "spectrum~" e))))
79
+ node))
80
+ :dispose (fn [node]
81
+ (reset! (:alive node) false)
82
+ (try (.disconnect (:in node)) (catch :default _ nil)))}})