@kairotic/analysis 0.1.0 → 0.2.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
@@ -11,6 +11,7 @@ From any signal — an `adc~`, a `sample` played by a `reader~`, a `faust~` —
11
11
  | `onset~ [threshold] [ms]` | a bang at the beginning of each sound (a note, a stroke, a syllable) and its strength |
12
12
  | `chroma~ [ms]` | 12 values from C to B and the strongest pitch class as a word (`:a`, `:c#`…) |
13
13
  | `spectrum~ [lo hi]` | the spectrum drawn in the patch, on a log scale of frequency |
14
+ | `transcribe [bpm] [notes\|glide] [ms]` | what `pitch~` hears (and `onset~` splits), written into a `freqt` as strokes: flat notes at the pitch they hold longest, or contours that follow the voice; times in beats at bpm. `rec` starts from 0, `stop` ends |
14
15
 
15
16
  Each sends only what changed. `pitch~` runs YIN in its own AudioWorklet; the others take frames of 2048
16
17
  samples through the Kairotic API (`api/tap`) and compute them with
@@ -24,4 +25,7 @@ samples through the Kairotic API (`api/tap`) and compute them with
24
25
  (connect :b1 0 :b2 0) (connect :b2 0 :b3 0) (connect :b3 0 :b4 0)
25
26
  ```
26
27
 
28
+ An improvisation into notation: `adc~` → `pitch~` → `transcribe` → `freqt`, with `onset~` on the right
29
+ of `transcribe`; `rec`, play, `stop`; then `(view :bN :score)` on the freqt, or `(keep! …)` and `mt`.
30
+
27
31
  Writing one of its classes in a work brings the package into it. Licence: [MPL-2.0](../../LICENSE).
package/dist/chroma.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  // Generated by packages/build.mjs from src/chroma.cljs
2
2
  export default {
3
3
  "class": "chroma~",
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
5
  "author": "bonuoq",
6
6
  "license": "MPL-2.0",
7
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.",
package/dist/features.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  // Generated by packages/build.mjs from src/features.cljs
2
2
  export default {
3
3
  "class": "features~",
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
5
  "author": "bonuoq",
6
6
  "license": "MPL-2.0",
7
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.",
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  // Generated by packages/build.mjs
2
2
  export default {
3
3
  "package": "@kairotic/analysis",
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
5
  "api": 1,
6
6
  "author": "bonuoq",
7
7
  "license": "MPL-2.0",
@@ -35,6 +35,12 @@ export default {
35
35
  "license": "MPL-2.0",
36
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
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
+ "class": "transcribe",
41
+ "license": "MPL-2.0",
42
+ "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) and stop to end; a bang on the right (from onset~) begins a new note even on the same pitch. In notes mode each note is a flat stroke at the pitch it holds longest (the freqt's tuning, or its score view, rounds it); 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 (60) 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.",
43
+ "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 (stroke-of cur t (:mode state)))\n long-enough (update :count inc)))\n state))\n start-note (fn [state t hz] (assoc state :cur {:pts [[t hz]] :midi (api/hz->midi hz)}))\n ;; ── writing: the strokes that ended, as one [:batch …], at most every flush-every seconds ──\n flush (fn [state at force]\n (cond\n (empty? (:done 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)\n [(assoc state :done [] :sent at :pending false :fresh false)\n [:batch (into (if (:fresh state) [[:clear]] []) (mapv (fn [pts] [:stroke pts]) (: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 ;; ── 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 (not (pos? hz)) (end-note state t)\n (nil? cur) (start-note state t hz)\n ;; notes: another note when the pitch moves more than 0.6 of a semitone away\n (and (= :notes (:mode state)) (> (js/Math.abs (- (api/hz->midi hz) (:midi cur))) 0.6))\n (-> state (end-note t) (start-note t hz))\n :else (update-in state [:cur :pts] conj [t hz]))]\n (flush state at false)))]\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) and stop to end; a bang on the right (from onset~) begins a new note even on the same pitch. In notes mode each note is a flat stroke at the pitch it holds longest (the freqt's tuning, or its score view, rounds it); 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 (60) 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 60]]\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state args at]}]\n (let [state (or state {})]\n (cond\n (= value :rec)\n {:state {:on true :t0 at :bpm (api/clamp (:bpm args) 10 400) :mode (mode-of args)\n :min-ms (api/clamp (:min-ms args) 0 2000) :done [] :count 0 :fresh true}\n :show {:on true :count 0}}\n (not (:on state)) {}\n (= value :stop)\n (let [st (end-note state (beats state at))\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 (assoc state :pending false) at false))\n ;; an onset: the note that sounds ends here and a new one begins at its pitch\n (and (= inlet 1) (:cur state))\n (let [t (beats state at) hz (second (peek (get-in state [:cur :pts])))]\n (result state (flush (-> state (end-note t) (start-note t hz)) at false)))\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"
38
44
  }
39
45
  ]
40
46
  };
package/dist/onset.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  // Generated by packages/build.mjs from src/onset.cljs
2
2
  export default {
3
3
  "class": "onset~",
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
5
  "author": "bonuoq",
6
6
  "license": "MPL-2.0",
7
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.",
package/dist/pitch.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  // Generated by packages/build.mjs from src/pitch.cljs
2
2
  export default {
3
3
  "class": "pitch~",
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
5
  "author": "bonuoq",
6
6
  "license": "MPL-2.0",
7
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~.",
package/dist/spectrum.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  // Generated by packages/build.mjs from src/spectrum.cljs
2
2
  export default {
3
3
  "class": "spectrum~",
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
5
  "author": "bonuoq",
6
6
  "license": "MPL-2.0",
7
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.",
@@ -0,0 +1,9 @@
1
+ // Generated by packages/build.mjs from src/transcribe.cljs
2
+ export default {
3
+ "class": "transcribe",
4
+ "version": "0.2.0",
5
+ "author": "bonuoq",
6
+ "license": "MPL-2.0",
7
+ "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) and stop to end; a bang on the right (from onset~) begins a new note even on the same pitch. In notes mode each note is a flat stroke at the pitch it holds longest (the freqt's tuning, or its score view, rounds it); 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 (60) 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.",
8
+ "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 (stroke-of cur t (:mode state)))\n long-enough (update :count inc)))\n state))\n start-note (fn [state t hz] (assoc state :cur {:pts [[t hz]] :midi (api/hz->midi hz)}))\n ;; ── writing: the strokes that ended, as one [:batch …], at most every flush-every seconds ──\n flush (fn [state at force]\n (cond\n (empty? (:done 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)\n [(assoc state :done [] :sent at :pending false :fresh false)\n [:batch (into (if (:fresh state) [[:clear]] []) (mapv (fn [pts] [:stroke pts]) (: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 ;; ── 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 (not (pos? hz)) (end-note state t)\n (nil? cur) (start-note state t hz)\n ;; notes: another note when the pitch moves more than 0.6 of a semitone away\n (and (= :notes (:mode state)) (> (js/Math.abs (- (api/hz->midi hz) (:midi cur))) 0.6))\n (-> state (end-note t) (start-note t hz))\n :else (update-in state [:cur :pts] conj [t hz]))]\n (flush state at false)))]\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) and stop to end; a bang on the right (from onset~) begins a new note even on the same pitch. In notes mode each note is a flat stroke at the pitch it holds longest (the freqt's tuning, or its score view, rounds it); 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 (60) 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 60]]\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state args at]}]\n (let [state (or state {})]\n (cond\n (= value :rec)\n {:state {:on true :t0 at :bpm (api/clamp (:bpm args) 10 400) :mode (mode-of args)\n :min-ms (api/clamp (:min-ms args) 0 2000) :done [] :count 0 :fresh true}\n :show {:on true :count 0}}\n (not (:on state)) {}\n (= value :stop)\n (let [st (end-note state (beats state at))\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 (assoc state :pending false) at false))\n ;; an onset: the note that sounds ends here and a new one begins at its pitch\n (and (= inlet 1) (:cur state))\n (let [t (beats state at) hz (second (peek (get-in state [:cur :pts])))]\n (result state (flush (-> state (end-note t) (start-note t hz)) at false)))\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"
9
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kairotic/analysis",
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",
3
+ "version": "0.2.0",
4
+ "description": "Kairotic analysis components: features~ (descriptors with Meyda), pitch~ (YIN), onset~, chroma~ spectrum~ and transcribe (pitch~ into a freqt), from any signal or adc~ into messages and pictures",
5
5
  "author": "bonuoq",
6
6
  "license": "MPL-2.0",
7
7
  "repository": {
@@ -35,6 +35,7 @@
35
35
  "pitch",
36
36
  "onset",
37
37
  "chroma",
38
- "meyda"
38
+ "meyda",
39
+ "transcription"
39
40
  ]
40
41
  }
@@ -0,0 +1,102 @@
1
+ ;; transcribe · what pitch~ hears, written as strokes of a freqt
2
+ ;; Part of @kairotic/analysis. Written against the Kairotic API (api/…). Pure logic: no audio of its own.
3
+ (let [;; ── what it understands ──
4
+ flush-every 2 ; seconds between two writes into the freqt (each write is one event of the history)
5
+ max-points 64 ; points of a stroke in glide mode
6
+ cents (fn [a b] (* 1200 (/ (js/Math.log (/ b a)) js/Math.LN2)))
7
+ ;; the pitch a note holds longest: pitch~ only sends what changed, so a steady note is one point and
8
+ ;; its attack many; each pitch weighs the time until the next one
9
+ held (fn [pts t1]
10
+ (let [ws (map (fn [[t f] [t2 _]] [f (max 0 (- t2 t))]) pts (concat (rest pts) [[t1 nil]]))
11
+ total (reduce + (map second ws))
12
+ sorted (sort-by first ws)]
13
+ (if (pos? total)
14
+ (loop [[[f w] & more] sorted acc 0]
15
+ (let [acc (+ acc w)] (if (or (>= acc (/ total 2)) (empty? more)) f (recur more acc))))
16
+ (second (last pts)))))
17
+ mode-of (fn [args] (if (= :glide (:mode args)) :glide :notes))
18
+ beats (fn [state at] (* (- at (:t0 state)) (/ (:bpm state) 60)))
19
+ r3 (fn [x] (/ (js/Math.round (* x 1000)) 1000))
20
+ ;; ── a note that ends: a stroke, if it lasted long enough ──
21
+ stroke-of (fn [{:keys [pts]} t1 mode]
22
+ (let [t0 (ffirst pts) hz (held pts t1)]
23
+ (if (= mode :glide)
24
+ (let [kept (reduce (fn [acc [t f]]
25
+ (if (> (js/Math.abs (cents (second (peek acc)) f)) 10) (conj acc [t f]) acc))
26
+ [(first pts)] (rest pts))
27
+ kept (if (> (count kept) max-points)
28
+ (vec (take-nth (js/Math.ceil (/ (count kept) max-points)) kept))
29
+ kept)]
30
+ (mapv (fn [[t f]] [(r3 t) (r3 f)]) (conj kept [t1 (second (peek pts))])))
31
+ [[(r3 t0) (r3 hz)] [(r3 t1) (r3 hz)]])))
32
+ end-note (fn [state t]
33
+ (if-let [cur (:cur state)]
34
+ (let [t0 (ffirst (:pts cur))
35
+ long-enough (>= (/ (- t t0) (/ (:bpm state) 60)) (/ (:min-ms state) 1000))]
36
+ (cond-> (dissoc state :cur)
37
+ long-enough (update :done conj (stroke-of cur t (:mode state)))
38
+ long-enough (update :count inc)))
39
+ state))
40
+ start-note (fn [state t hz] (assoc state :cur {:pts [[t hz]] :midi (api/hz->midi hz)}))
41
+ ;; ── writing: the strokes that ended, as one [:batch …], at most every flush-every seconds ──
42
+ flush (fn [state at force]
43
+ (cond
44
+ (empty? (:done state)) [state nil nil]
45
+ (or force (nil? (:sent state)) (>= (- at (:sent state)) flush-every))
46
+ ;; the first write of a take clears the freqt (as part of the same event)
47
+ [(assoc state :done [] :sent at :pending false :fresh false)
48
+ [:batch (into (if (:fresh state) [[:clear]] []) (mapv (fn [pts] [:stroke pts]) (:done state)))] nil]
49
+ (:pending state) [state nil nil]
50
+ :else [(assoc state :pending true) nil [(+ (:sent state) flush-every) 0 [:flush]]]))
51
+ result (fn [state [st edit later]]
52
+ (cond-> {:state st :show (select-keys st [:on :count])}
53
+ edit (assoc :out [[0 edit]])
54
+ later (assoc :later [later])))
55
+ ;; ── one value of pitch~ (Hz, 0 when it hears none) ──
56
+ hear (fn [state hz at]
57
+ (let [t (beats state at)
58
+ cur (:cur state)
59
+ state (cond
60
+ (not (pos? hz)) (end-note state t)
61
+ (nil? cur) (start-note state t hz)
62
+ ;; notes: another note when the pitch moves more than 0.6 of a semitone away
63
+ (and (= :notes (:mode state)) (> (js/Math.abs (- (api/hz->midi hz) (:midi cur))) 0.6))
64
+ (-> state (end-note t) (start-note t hz))
65
+ :else (update-in state [:cur :pts] conj [t hz]))]
66
+ (flush state at false)))]
67
+ {;; ── about ──
68
+ :class "transcribe"
69
+ :license "MPL-2.0"
70
+ :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) and stop to end; a bang on the right (from onset~) begins a new note even on the same pitch. In notes mode each note is a flat stroke at the pitch it holds longest (the freqt's tuning, or its score view, rounds it); 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 (60) 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."
71
+ :usage "transcribe [bpm] [notes|glide] [ms]"
72
+ :io [2 1]
73
+ :size [124 30]
74
+ :args [[:bpm 60] [:mode nil] [:min-ms 60]]
75
+
76
+ ;; ── logic ──
77
+ :receive (fn [{:keys [inlet value state args at]}]
78
+ (let [state (or state {})]
79
+ (cond
80
+ (= value :rec)
81
+ {:state {:on true :t0 at :bpm (api/clamp (:bpm args) 10 400) :mode (mode-of args)
82
+ :min-ms (api/clamp (:min-ms args) 0 2000) :done [] :count 0 :fresh true}
83
+ :show {:on true :count 0}}
84
+ (not (:on state)) {}
85
+ (= value :stop)
86
+ (let [st (end-note state (beats state at))
87
+ [st edit _] (flush st at true)]
88
+ (cond-> {:state (assoc st :on false) :show {:on false :count (:count st)}}
89
+ edit (assoc :out [[0 edit]])))
90
+ (= value [:flush]) (result state (flush (assoc state :pending false) at false))
91
+ ;; an onset: the note that sounds ends here and a new one begins at its pitch
92
+ (and (= inlet 1) (:cur state))
93
+ (let [t (beats state at) hz (second (peek (get-in state [:cur :pts])))]
94
+ (result state (flush (-> state (end-note t) (start-note t hz)) at false)))
95
+ (and (= inlet 0) (number? value)) (result state (hear state value at))
96
+ :else {})))
97
+
98
+ ;; ── view ──
99
+ :derive (fn [{:keys [shown]}]
100
+ {:label (if (:on shown) (str "● transcribing · " (or (:count shown) 0)) (str "transcribe · " (or (:count shown) 0) " notes"))})
101
+ :view [[:rect {:x 2 :y 2 :w 120 :h 26 :r 3 :stroke :grid :fill :bg}]
102
+ [:text {:x 8 :y 19 :size 10 :fill :soft} :$label]]})