@kairotic/analysis 0.3.0 → 0.7.5

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.
@@ -0,0 +1,218 @@
1
+ ;; realize~ · descriptors written → the sliders of a faust~ that sound like them (discussion #114, Study 37)
2
+ ;; Part of @kairotic/analysis. Written against the Kairotic API (api/…).
3
+ ;; What arrives is a frame of a score: a loudness, a brightness (the spectral centroid, Hz), a noisiness (the
4
+ ;; spectral flatness) and a pitch — what features~ and pitch~ hear, so another sound can drive it, or curves
5
+ ;; drawn on canvases read back (reader~ → snapshot~). What leaves is [:slider value] for the faust~ on its
6
+ ;; left outlet: its `pitch` (MIDI) or `freq` (Hz) slider takes the pitch, its `gain` (dB or a level) the
7
+ ;; loudness, and its other sliders, the timbre, are set to the blend of the settings of that faust~ that come
8
+ ;; nearest to the brightness and noisiness written (where a pitch is written, among the settings in which YIN
9
+ ;; would hear it). To know them, it learns the faust~ first (the task :learn: steady settings played offline
10
+ ;; and heard, @kairotic/faust-units' notation.mjs), when the first frame arrives or on `learn`.
11
+ ;; It can also listen to what it plays: features~ of the faust~'s sound on its last three inlets (loudness,
12
+ ;; brightness, noisiness heard). Then it asks for more where what is heard falls short of what is written,
13
+ ;; little by little (a correction that grows while the difference lasts, within limits), as the study's
14
+ ;; passes did offline.
15
+ (let [meyda-url "npm:meyda@5.6.3/dist/web/meyda.min.js"
16
+ origins ["npm:@kairotic/faust-units@0.1.5/dist/notation.mjs" "kairotic/packages/faust-units/notation.mjs"]
17
+ silent -60 ; dB: below it nothing is written (a microphone in a room is often near -50)
18
+ every 0.04 ; seconds: at most one new setting this often
19
+ ;; listening: how much of each difference heard goes into the correction, and how far it may go
20
+ rate 0.08
21
+ leak 0.995 ; what it corrected fades unless it is still needed
22
+ hold 0.25 ; seconds a sound has lasted before it is corrected (not its attack)
23
+ limits {:loud 18 :lb 2 :nz 0.4}
24
+ bank-size (fn [args] (js/Math.round (api/clamp (or (first (filter number? args)) 300) 40 2000)))
25
+ midi-in? (fn [args] (some #{:midi} args))
26
+ log2 (fn [x] (/ (js/Math.log x) js/Math.LN2))
27
+ ;; a loudness as dB: a level (0 < v ≤ 1, as features~ rms) or dB already (≤ 0); 0 is silence
28
+ db-of (fn [v] (cond (not (number? v)) nil (zero? v) -100 (and (pos? v) (<= v 1)) (api/db v) :else v))
29
+ ;; the nearest settings of the bank to what is asked, blended: {:u [...] :loud0 dB :d distance}. The search
30
+ ;; itself is the bank's query (notation.mjs: plain loops over typed arrays), asked for every frame it plays
31
+ nearest (fn [bank {:keys [pitch lb nz voiced]}]
32
+ (let [r ((:query bank) pitch lb nz (boolean voiced))]
33
+ {:loud0 (aget r 0) :d (aget r 1) :u (vec (.slice r 2))}))
34
+ ;; the values for the faust~, and only those that changed: [[0 [:cutoff 1800]] …]
35
+ settle (fn [st at]
36
+ (let [{:keys [bank]} st
37
+ ;; a lane that never arrived keeps the middle of what the faust~ can do
38
+ want (merge {:lb (first (:mid bank)) :nz (second (:mid bank))} (:want st))
39
+ ;; what is asked of the bank: what is written, corrected by what was heard
40
+ {cl :loud clb :lb cnz :nz} (:corr st)
41
+ [lb-lo lb-hi] (or (:lbr bank) [0 20])
42
+ ask (cond-> want
43
+ (and cl (:loud want)) (update :loud + cl)
44
+ clb (assoc :lb (api/clamp (+ (:lb want) clb) lb-lo lb-hi))
45
+ cnz (assoc :nz (api/clamp (+ (:nz want) cnz) 0 1)))
46
+ loud (:loud want)]
47
+ (if (and (nil? loud) (nil? (:pitch want)) (nil? (get-in st [:want :lb])) (nil? (get-in st [:want :nz])))
48
+ {:state (assoc st :t at)}
49
+ (let [quiet? (and loud (< loud silent))
50
+ {:keys [u loud0 d]} (if quiet? {:u (:u st) :loud0 0 :d (:reach st)} (nearest bank ask))
51
+ ;; the timbre glides to where it goes (half the way every 40 ms), and keeps
52
+ ;; gliding when nothing more arrives (a value written once, a canvas held)
53
+ goal u
54
+ u (if (and (:u st) (not quiet?)) (mapv (fn [a b] (+ a (* 0.5 (- b a)))) (:u st) u) u)
55
+ moving? (and goal u (not quiet?) (some (fn [[a b]] (> (js/Math.abs (- a b)) 0.004)) (map vector u goal)))
56
+ g (:gain bank) p (:pitch bank)
57
+ gain-v (when (and g loud) (let [dB (if quiet? -100 (- (:loud ask) loud0))]
58
+ (if (:db g) (api/clamp dB (:min g) (:max g))
59
+ (api/clamp (js/Math.pow 10 (/ dB 20)) (:min g) (:max g)))))
60
+ pitch-v (when (and p (:pitch want))
61
+ (api/clamp (if (= "hz" (:kind p)) (api/midi->hz (:pitch want)) (:pitch want)) (:min p) (:max p)))
62
+ vals (cond-> (if (and u (not quiet?))
63
+ (into {} (map (fn [s x] [(:label s) (api/from-unit x (:min s) (:max s) (:log s))]) (:timbre bank) u))
64
+ {})
65
+ gain-v (assoc (:label g) gain-v)
66
+ pitch-v (assoc (:label p) pitch-v))
67
+ span (fn [label] (let [s (or (first (filter #(= label (:label %)) (:timbre bank))) (when (= label (:label g)) g) p)]
68
+ (js/Math.abs (- (:max s) (:min s)))))
69
+ changed (filter (fn [[k v]] (let [old (get (:sent st) k)]
70
+ (or (nil? old) (> (js/Math.abs (- v old)) (* 0.002 (span k))))))
71
+ vals)
72
+ reach (when d (/ (js/Math.round (* 100 d)) 100))]
73
+ {:state (-> st (assoc :t at :u (or u (:u st)) :reach reach :dirty (boolean moving?)
74
+ :loud0 (if quiet? (:loud0 st) loud0)
75
+ :tick (or (:tick st) (boolean moving?)))
76
+ (update :sent merge (into {} changed)))
77
+ :later (when (and moving? (not (:tick st))) [[(+ at every) 0 :realize-tick]])
78
+ :out (cond-> (mapv (fn [[k v]] [0 [(keyword k) (api/nice v)]]) changed)
79
+ (and reach (not= reach (:reach st))) (conj [1 reach]))
80
+ :show {:status :ready :faust (:faust bank) :n (count (:entries bank)) :want want :reach reach
81
+ :listening (some? (:corr st))}}))))
82
+ ;; each request is numbered, so the answer to an older one is dropped
83
+ learn (fn [st args]
84
+ (let [k (inc (:k st 0))]
85
+ {:state (assoc st :learning true :error nil :k k :corr nil)
86
+ :show {:status :learning}
87
+ :effects [[:learn {:n (bank-size args) :k k}]]}))]
88
+ {;; ── about ──
89
+ :class "realize~"
90
+ :license "MPL-2.0"
91
+ :doc "Plays a faust~ from descriptors, as a score of them: loudness (dB, or a level 0…1 as features~ rms), brightness (Hz, the spectral centroid), noisiness (0…1, the spectral flatness) and pitch (Hz as pitch~ sends it, 0 for none; MIDI with `midi`). Its left outlet goes to the faust~: the faust~'s pitch or freq slider takes the pitch, its gain slider the loudness, and its other sliders are set to the settings of that faust~ that sound nearest to the brightness and noisiness written. It learns them first by playing steady settings of the faust~ offline and listening (300 by default), when the first values arrive or on learn; learn again after changing the faust~'s code. With features~ of the faust~'s sound on its last three inlets (rms, centroid, flatness) it listens to what it plays and asks for more where it falls short. On the right, how far the nearest setting is from what is written (above 1: the faust~ cannot sound like that)."
92
+ :usage "realize~ [settings] [midi]"
93
+ :io [7 2]
94
+ :size [236 46]
95
+
96
+ ;; ── tasks: learning the faust~ it drives, outside its pure logic ──
97
+ :tasks {:learn (fn [{:keys [n]} box]
98
+ (let [f (first (filter (fn [w] (and (= "faust~" (:class w)) (zero? (:outlet w)))) (:wired box)))
99
+ src (str (get-in f [:data :source]))
100
+ ;; the compilation of its code, waited for (a faust~ just written is still compiling)
101
+ compiled (fn compiled [tries]
102
+ (let [r (api/compiled "faust" src)]
103
+ (cond (= :ok (:status r)) (js/Promise.resolve r)
104
+ (and (#{:compiling :empty} (:status r)) (pos? tries))
105
+ (js/Promise. (fn [ok] (js/setTimeout (fn [] (ok nil)) 250)))
106
+ :else (js/Promise.reject (js/Error. (str "the faust~ does not compile: " (or (:error r) "no code")))))))
107
+ wait (fn wait [tries]
108
+ (.then (compiled tries) (fn [r] (if r r (wait (dec tries))))))]
109
+ (if-not f
110
+ (js/Promise.reject (js/Error. "connect its left outlet to a faust~: it learns the one it plays"))
111
+ (-> (wait 120)
112
+ (.then (fn [r]
113
+ (when (get-in r [:info :poly])
114
+ (throw (js/Error. "it plays a faust~ of one voice (this one declares nvoices)")))
115
+ (let [fac (.-factory (:value r))]
116
+ (-> (js/Promise.all #js [(api/load-script meyda-url) (api/import-module origins)])
117
+ (.then (fn [[_ m]]
118
+ (.then (.learn m #js {:module (or (.-module fac) (.-code fac))
119
+ :json (js/JSON.parse (.-json fac))
120
+ :Meyda (.-Meyda js/window) :n n})
121
+ (fn [b] [b (.index m b)]))))))))
122
+ (.then (fn [[bjs query]]
123
+ ;; the settings stay a JS array (the query reads them); the rest as data
124
+ (let [es (.-entries bjs)
125
+ col (fn [j] (map (fn [e] (aget e j)) (array-seq es)))
126
+ avg (fn [j] (/ (reduce + (col j)) (.-length es)))]
127
+ (-> (js->clj #js {:pitch (.-pitch bjs) :gain (.-gain bjs) :timbre (.-timbre bjs) :sd (.-sd bjs)}
128
+ :keywordize-keys true)
129
+ (assoc :query query :entries es :faust (name (:id f)) :mid [(avg 1) (avg 2)]
130
+ :lbr [(reduce min (col 1)) (reduce max (col 1))])))))))))}
131
+
132
+ ;; ── logic ──
133
+ :receive (fn [{:keys [inlet value state args at]}]
134
+ (let [st (or state {})]
135
+ (cond
136
+ ;; what learning found
137
+ (= inlet :task)
138
+ (cond
139
+ (not= (:k (:arg value)) (:k st)) {}
140
+ (:error value) {:state (assoc st :learning false :error (:error value))
141
+ :show {:status :error :error (:error value)}}
142
+ :else (let [st (assoc st :learning false :bank (:value value) :sent {} :u nil)]
143
+ (settle st (or at (:t st) 0))))
144
+
145
+ (#{:learn :bang} value) (learn st args)
146
+
147
+ ;; a moment later: the last frame that arrived while it waited
148
+ (= value :realize-tick) (if (and (:bank st) (:dirty st)) (settle (assoc st :tick false) at) {:state (assoc st :tick false)})
149
+
150
+ ;; what is heard in what it plays (features~ of the faust~): the correction grows by a part of
151
+ ;; the difference, while something is written and sounds
152
+ (and (#{4 5 6} inlet) (number? value))
153
+ (let [heard (case inlet 4 (db-of value) 5 (when (pos? value) (log2 value)) 6 value)
154
+ k ({4 :loud 5 :lb 6 :nz} inlet)
155
+ want (:want st)
156
+ st (cond-> st (= k :loud) (assoc :heard-loud heard))
157
+ sounding? (and (:bank st) (number? (:loud want)) (> (:loud want) silent)
158
+ (> (or (:heard-loud st) -100) silent))]
159
+ (if (and sounding? (number? heard) (number? (get want k)) (:since st) (>= (- at (:since st)) hold))
160
+ ;; within what the faust~ can give (the bank's brightness, noisiness 0…1, the gain slider's
161
+ ;; range): a correction that cannot be played does not grow, so it does not wind up
162
+ (let [bank (:bank st) w (get want k) g (:gain bank) [lb-lo lb-hi] (or (:lbr bank) [0 20])
163
+ [lo hi] (case k
164
+ :lb [(- lb-lo w) (- lb-hi w)]
165
+ :nz [(- w) (- 1 w)]
166
+ :loud (if (and g (:db g) (:loud0 st))
167
+ [(+ (:min g) (:loud0 st) (- w)) (+ (:max g) (:loud0 st) (- w))]
168
+ [(- (limits k)) (limits k)]))
169
+ c (-> (+ (* leak (get-in st [:corr k] 0)) (* rate (- w heard)))
170
+ (api/clamp (max lo (- (limits k))) (min hi (limits k))))
171
+ st (-> st (assoc-in [:corr k] c) (assoc :dirty true))]
172
+ (if (or (nil? (:t st)) (>= (- at (:t st)) every))
173
+ (settle st at)
174
+ (if (:tick st) {:state st} {:state (assoc st :tick true) :later [[(+ (:t st) every) 0 :realize-tick]]})))
175
+ {:state st}))
176
+
177
+ (and (integer? inlet) (number? value))
178
+ (let [midi (when (and (= inlet 3) (pos? value)) (if (midi-in? args) value (api/hz->midi value)))
179
+ want (case inlet
180
+ 0 (assoc (:want st) :loud (db-of value))
181
+ 1 (if (pos? value) (assoc (:want st) :lb (log2 value)) (:want st))
182
+ 2 (assoc (:want st) :nz (api/clamp value 0 1))
183
+ 3 (if midi (assoc (:want st) :pitch midi :voiced true) (assoc (:want st) :voiced false))
184
+ (:want st))
185
+ ;; since when what is written sounds (its attack is not corrected)
186
+ st (cond-> (assoc st :want want :dirty true)
187
+ (= inlet 0) (assoc :since (if (> (or (:loud want) -100) silent) (or (:since st) at) nil)))]
188
+ (cond
189
+ (:bank st) (if (or (nil? (:t st)) (>= (- at (:t st)) every))
190
+ (settle st at)
191
+ (if (:tick st)
192
+ {:state st}
193
+ {:state (assoc st :tick true) :later [[(+ (:t st) every) 0 :realize-tick]]}))
194
+ (or (:learning st) (:error st)) {:state st}
195
+ :else (learn st args)))
196
+
197
+ :else {})))
198
+
199
+ ;; ── view: what it learnt, what is written now, and how far the faust~ is from it ──
200
+ :draw (fn [{:keys [shown w h]}]
201
+ (let [{:keys [status faust n want reach error]} shown
202
+ line1 (case status
203
+ :learning "learning the faust~ it plays…"
204
+ :ready (str "plays " faust " · " n " settings heard" (when (:listening shown) " · listening"))
205
+ :error (str "✗ " error)
206
+ "waits for a faust~ and a score")
207
+ fmt (fn [x d] (if (number? x) (.toFixed x d) "–"))
208
+ line2 (when (= status :ready)
209
+ (str (fmt (:loud want) 0) " dB · "
210
+ (if (:lb want) (str (js/Math.round (js/Math.pow 2 (:lb want))) " Hz") "–") " · "
211
+ (fmt (:nz want) 2)
212
+ (when (and (:voiced want) (:pitch want)) (str " · " (api/midi-name (js/Math.round (:pitch want)))))))
213
+ bw (- w 16) far (when (number? reach) (api/clamp (/ reach 2) 0 1))]
214
+ (cond-> [[:rect {:x 2 :y 2 :w (- w 4) :h (- h 4) :r 4 :stroke :grid :fill :bg}]
215
+ [:text {:x 8 :y 15 :size 9 :fill (if (= status :error) :kairos :ink)} line1]]
216
+ line2 (conj [:text {:x 8 :y 28 :size 9 :fill :soft} line2])
217
+ far (conj [:rect {:x 8 :y 35 :w bw :h 4 :r 2 :fill :grid :stroke nil}]
218
+ [:rect {:x 8 :y 35 :w (max 2 (* bw far)) :h 4 :r 2 :fill (if (> reach 1) :kairos :accent) :stroke nil}]))))})
package/src/spectrum.cljs CHANGED
@@ -63,9 +63,10 @@
63
63
  sr (.-sampleRate ac)
64
64
  [lo hi] (api/padded (:args b) [40 16000])
65
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 []}
66
+ node {:in in :M (atom nil) :tap (atom nil) :alive (atom true) :inlets [[in 0]] :outlets []}
67
67
  on-block (fn [data at & _]
68
- (when-let [M @(:M node)]
68
+ ;; only while the box is here: a tap left behind would hand it silence
69
+ (when-let [M (and @(:alive node) @(:M node))]
69
70
  (set! (.-bufferSize M) size)
70
71
  (set! (.-sampleRate M) sr)
71
72
  (let [spec (.extract M "amplitudeSpectrum" (first data))]
@@ -74,9 +75,10 @@
74
75
  (.then (fn [_] (reset! (:M node) (.-Meyda js/window))))
75
76
  (.catch (fn [e] (api/report! (:id b) {:error (str "spectrum~: " (.-message e))} nil))))
76
77
  (-> (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
+ (.then (fn [w] (if @(:alive node) (do (.connect in w) (reset! (:tap node) w)) (api/untap w))))
78
79
  (.catch (fn [e] (js/console.error "spectrum~" e))))
79
80
  node))
80
81
  :dispose (fn [node]
81
82
  (reset! (:alive node) false)
83
+ (api/untap @(:tap node))
82
84
  (try (.disconnect (:in node)) (catch :default _ nil)))}})
@@ -34,10 +34,18 @@
34
34
  (let [t0 (ffirst (:pts cur))
35
35
  long-enough (>= (/ (- t t0) (/ (:bpm state) 60)) (/ (:min-ms state) 1000))]
36
36
  (cond-> (dissoc state :cur)
37
- long-enough (update :done conj (stroke-of cur t (:mode state)))
37
+ long-enough (update :done conj {:pts (stroke-of cur t (:mode state)) :acc (:acc cur)})
38
38
  long-enough (update :count inc)))
39
39
  state))
40
- start-note (fn [state t hz] (assoc state :cur {:pts [[t hz]] :midi (api/hz->midi hz)}))
40
+ ;; a note takes the strength of the onset that began it (a number on the right inlet, from onset~), if it
41
+ ;; came at most 0.15 s before: onset~ often hears the attack before pitch~ hears the pitch
42
+ start-note (fn [state t hz]
43
+ (let [[at s] (:acc state) acc (when (and at (<= (- t at) (* 0.15 (/ (:bpm state) 60)))) s)]
44
+ (cond-> (assoc state :cur (cond-> {:pts [[t hz]] :midi (api/hz->midi hz)} acc (assoc :acc acc)))
45
+ acc (dissoc :acc))))
46
+ ;; how a note with the strength of its attack is written: the weight of its stroke, as an event's :amp is
47
+ ;; (0 … 1 → 0.4 … 2.8 times the usual); the quantizer reads it back as the note's :vel (issue #106)
48
+ edit-of (fn [{:keys [pts acc]}] (if (number? acc) [:stroke pts {:w (/ (js/Math.round (* 100 (+ 0.4 (* 2.4 acc)))) 100)}] [:stroke pts]))
41
49
  ;; ── writing: the strokes that ended, as one [:batch …], at most every flush-every seconds ──
42
50
  flush (fn [state at force]
43
51
  (let [now (r3 (beats state at))]
@@ -52,41 +60,115 @@
52
60
  [:batch (into (cond (:ovr state) [[:cut (:cut-from state) now]]
53
61
  (:fresh state) [(if (:from state) [:cut (:from state)] [:clear])]
54
62
  :else [])
55
- (mapv (fn [pts] [:stroke pts]) (:done state)))] nil]
63
+ (mapv edit-of (:done state)))] nil]
56
64
  (:pending state) [state nil nil]
57
65
  :else [(assoc state :pending true) nil [(+ (:sent state) flush-every) 0 [:flush]]])))
58
66
  result (fn [state [st edit later]]
59
67
  (cond-> {:state st :show (select-keys st [:on :count])}
60
68
  edit (assoc :out [[0 edit]])
61
69
  later (assoc :later [later])))
70
+ ;; ── notes mode: the contour of each voiced stretch, segmented (issue #106, measured on cante2midi) ──
71
+ ;; What pitch~ sends while a voice sounds is kept as a run of points; when the voice stops (or the run is long)
72
+ ;; the run is read as a contour every 10 ms, smoothed by a median of 100 ms (vibrato and jitter), and cut into
73
+ ;; notes with hysteresis: a note goes on while the contour stays within a semitone of its pitch; a new one
74
+ ;; begins only where the contour, away from it, has held still (within 1.1 semitones) for 70 ms, so a slide
75
+ ;; or an ornament that passes does not start notes. An onset (inlet 1) cuts there too. Each note is written
76
+ ;; at the median pitch of its frames. Silences of 30 ms at most are bridged.
77
+ median (fn [xs] (let [v (vec (sort xs))] (nth v (quot (count v) 2))))
78
+ segment (fn [run marks t-end k minlen]
79
+ (let [dt (* 0.01 k) w 5 band 1 stab 1.1 hold-n 7
80
+ ts (vec (range (ffirst run) t-end dt))
81
+ ;; the contour on the grid: each frame, the last point at or before it (as MIDI)
82
+ raw (loop [i 0 [p & more :as ps] run cur nil out (transient [])]
83
+ (if (>= i (count ts))
84
+ (persistent! out)
85
+ (let [t (ts i)]
86
+ (if (and p (<= (first p) (+ t 1e-9)))
87
+ (recur i more (api/hz->midi (second p)) out)
88
+ (recur (inc i) ps cur (conj! out cur))))))
89
+ n (count raw)
90
+ sm (mapv (fn [i] (median (subvec raw (max 0 (- i w)) (min n (+ i w 1))))) (range n))
91
+ cut? (fn [i] (let [a (if (pos? i) (ts (dec i)) (- (ts 0) dt)) b (ts i)] (some (fn [[mt]] (and (> mt a) (<= mt b))) marks)))
92
+ note (fn [s e ms] {:s s :e e :ms ms})
93
+ notes (loop [i 0 cur nil pend [] out []]
94
+ (if (>= i n)
95
+ (if cur (conj out (assoc cur :e t-end)) out)
96
+ (let [t (ts i) m (sm i)]
97
+ (cond
98
+ (nil? cur) (recur (inc i) (note t (+ t dt) [m]) [] out)
99
+ ;; (not while the contour is away from the note and not settled yet: there the contour
100
+ ;; decides; measured on cante2midi with onsets from its Bark bands)
101
+ (and (empty? pend) (cut? i) (> t (:s cur))) (recur (inc i) (note t (+ t dt) [m]) [] (conj out (assoc cur :e t)))
102
+ (<= (js/Math.abs (- m (median (take-last 24 (:ms cur))))) band)
103
+ (recur (inc i) (assoc cur :e (+ t dt) :ms (into (conj (:ms cur) m) (map second pend))) [] out)
104
+ :else
105
+ (let [pend (conj pend [t m]) tail (take-last hold-n pend) tm (map second tail)]
106
+ (if (and (>= (count pend) hold-n) (< (- (apply max tm) (apply min tm)) stab))
107
+ ;; the new note begins where the contour left the old one; its pitch is that of where it settled
108
+ (recur (inc i) (note (ffirst pend) (+ t dt) (vec tm)) [] (conj out (assoc cur :e (ffirst pend))))
109
+ (recur (inc i) cur pend out)))))))]
110
+ (->> notes
111
+ (filter (fn [{:keys [s e]}] (>= (- e s) minlen)))
112
+ (mapv (fn [{:keys [s e ms]}]
113
+ (let [hz (api/midi->hz (median ms))
114
+ ;; the strength of an onset heard at most 0.15 s before the note (or just after its start)
115
+ acc (some (fn [[mt x]] (when (and (number? x) (<= (- s (* 0.15 k)) mt (+ s (* 0.05 k)))) x)) marks)]
116
+ (cond-> {:pts [[(r3 s) (r3 hz)] [(r3 e) (r3 hz)]]} acc (assoc :acc acc))))))))
117
+ ;; the run segmented into :done: all of it (the voice stopped at t-end), or all but its last note (a long run
118
+ ;; still sounding: that note goes on in the run)
119
+ settle (fn [state t-end all?]
120
+ (let [run (:run state) k (/ (:bpm state) 60) minlen (* k (/ (:min-ms state) 1000))]
121
+ (if (or (empty? run) (<= t-end (ffirst run)))
122
+ (dissoc state :run :silent)
123
+ (let [ns (segment run (:marks state) t-end k minlen)
124
+ [keep-ns rest-from] (if (or all? (< (count ns) 2)) [ns nil] [(pop ns) (ffirst (:pts (peek ns)))])
125
+ state (-> state (update :done into keep-ns) (update :count + (count keep-ns)))]
126
+ (if rest-from
127
+ (let [before (last (filter (fn [[t]] (<= t rest-from)) run))]
128
+ (assoc state :run (into [[rest-from (second before)]] (filter (fn [[t]] (> t rest-from)) run))
129
+ :marks (filterv (fn [[t]] (>= t (- rest-from (* 0.15 k)))) (:marks state))))
130
+ (-> state (dissoc :run :silent) (assoc :marks (filterv (fn [[t]] (> t t-end)) (:marks state)))))))))
131
+ ;; a silence that lasted more than 30 ms: the run ends where it began
132
+ settled (fn [state t]
133
+ (if (and (:silent state) (> (- t (:silent state)) (* 0.03 (/ (:bpm state) 60))))
134
+ (settle state (:silent state) true)
135
+ state))
136
+ hear-notes (fn [state hz t]
137
+ (let [k (/ (:bpm state) 60)
138
+ state (settled state t)]
139
+ (cond
140
+ (not (pos? hz)) (if (and (seq (:run state)) (nil? (:silent state))) (assoc state :silent t) state)
141
+ :else (let [state (-> state (dissoc :silent) (update :run (fnil conj []) [t hz]))]
142
+ ;; a run of 8 s and more: what is settled is written, the last note goes on
143
+ (if (> (- t (ffirst (:run state))) (* 8 k)) (settle state t false) state)))))
62
144
  ;; ── one value of pitch~ (Hz, 0 when it hears none) ──
63
145
  hear (fn [state hz at]
64
146
  (let [t (beats state at)
65
147
  cur (:cur state)
66
148
  state (cond
149
+ (= :notes (:mode state)) (hear-notes state hz t)
67
150
  (not (pos? hz)) (end-note state t)
68
151
  (nil? cur) (start-note state t hz)
69
- ;; notes: another note when the pitch moves more than 0.6 of a semitone away
70
- (and (= :notes (:mode state)) (> (js/Math.abs (- (api/hz->midi hz) (:midi cur))) 0.6))
71
- (-> state (end-note t) (start-note t hz))
72
- :else (update-in state [:cur :pts] conj [t hz]))]
73
- (flush state at false)))]
152
+ :else (update-in state [:cur :pts] conj [t hz]))
153
+ [st edit later] (flush state at false)]
154
+ ;; the voice stopped: a look again in a little while, to write its notes even if nothing else arrives
155
+ [st edit (or later (when (and (:silent st) (not (:pending st))) [(+ at 0.05) 0 [:flush]]))]))]
74
156
  {;; ── about ──
75
157
  :class "transcribe"
76
158
  :license "MPL-2.0"
77
- :doc "Writes what pitch~ hears into a freqt, as strokes. Hz on the left (from pitch~: 0 when it hears none), rec to start from 0 (its first write clears the freqt) or rec t (msg rec 8) to start from beat t, keeping what is before it, or ovr t (msg ovr 8) to write over only what the take covers, keeping what comes after and stop to end; a bang on the right (from onset~) begins a new note even on the same pitch. 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."
159
+ :doc "Writes what pitch~ hears into a freqt, as strokes. Hz on the left (from pitch~: 0 when it hears none), :rec to start from 0 (its first write clears the freqt) or :rec t (msg :rec 8) to start from beat t, keeping what is before it, or :ovr t (msg :ovr 8) to write over only what the take covers, keeping what comes after and :stop to end; a bang on the right (from onset~) begins a new note even on the same pitch, and a number there (onset~'s strength, 0…1) does too and gives the note that weight: its stroke is written heavier (as an event's :amp), and the quantizer reads it as the note's :vel, which helps find the beat and the bar (issue #106). In notes mode the contour of each voiced stretch is cut into notes where it settles on another pitch (a slide, a vibrato or an ornament that passes does not start notes; an onset on the right does), each a flat stroke at its median pitch (the freqt's tuning, or its score view, rounds it), written when the voice stops; in glide mode a stroke follows the pitch as it moves. Times are beats at bpm (60: one beat a second). Notes shorter than ms (80) are left out. It writes into the freqt at most every 2 seconds, each time one event of the history. Connect its outlet to the freqt's inlet."
78
160
  :usage "transcribe [bpm] [notes|glide] [ms]"
79
161
  :io [2 1]
80
162
  :size [124 30]
81
- :args [[:bpm 60] [:mode nil] [:min-ms 60]]
163
+ :args [[:bpm 60] [:mode nil] [:min-ms 80]]
82
164
 
83
165
  ;; ── logic ──
84
166
  :receive (fn [{:keys [inlet value state args at]}]
85
167
  (let [state (or state {})]
86
168
  (cond
87
- ;; rec: a take from 0 (its first write clears the freqt); [:rec t] (msg rec 8): from beat t on,
169
+ ;; rec: a take from 0 (its first write clears the freqt); [:rec t] (msg :rec 8): from beat t on,
88
170
  ;; keeping what is before t (punch-in)
89
- ;; ovr t (msg ovr 8): over what is there from beat t to where the take stops, keeping what comes after
171
+ ;; ovr t (msg :ovr 8): over what is there from beat t to where the take stops, keeping what comes after
90
172
  (or (#{:rec :ovr} value) (and (vector? value) (#{:rec :ovr} (first value))))
91
173
  (let [bpm (api/clamp (:bpm args) 10 400)
92
174
  ovr (or (= value :ovr) (and (vector? value) (= :ovr (first value))))
@@ -99,15 +181,25 @@
99
181
  :show {:on true :count 0}})
100
182
  (not (:on state)) {}
101
183
  (= value :stop)
102
- (let [st (end-note state (beats state at))
184
+ (let [t (beats state at)
185
+ st (if (seq (:run state)) (settle state (or (:silent state) t) true) state)
186
+ st (end-note st t)
103
187
  [st edit _] (flush st at true)]
104
188
  (cond-> {:state (assoc st :on false) :show {:on false :count (:count st)}}
105
189
  edit (assoc :out [[0 edit]])))
106
- (= value [:flush]) (result state (flush (assoc state :pending false) at false))
107
- ;; an onset: the note that sounds ends here and a new one begins at its pitch
108
- (and (= inlet 1) (:cur state))
109
- (let [t (beats state at) hz (second (peek (get-in state [:cur :pts])))]
110
- (result state (flush (-> state (end-note t) (start-note t hz)) at false)))
190
+ (= value [:flush]) (result state (flush (settled (assoc state :pending false) (beats state at)) at false))
191
+ ;; an onset (a bang, or its strength 0…1 from onset~'s second outlet): the note that sounds ends
192
+ ;; here and a new one begins at its pitch, with that strength; in silence, the strength waits for
193
+ ;; the next note to begin
194
+ (and (= inlet 1) (= :notes (:mode state)))
195
+ {:state (update state :marks (fnil conj []) [(beats state at) (when (number? value) (api/clamp value 0 1))])}
196
+ (= inlet 1)
197
+ (let [t (beats state at)
198
+ state (if (number? value) (assoc state :acc [t (api/clamp value 0 1)]) state)]
199
+ (if (:cur state)
200
+ (let [hz (second (peek (get-in state [:cur :pts])))]
201
+ (result state (flush (-> state (end-note t) (start-note t hz)) at false)))
202
+ {:state state}))
111
203
  (and (= inlet 0) (number? value)) (result state (hear state value at))
112
204
  :else {})))
113
205