@kairotic/app 0.0.0-stage → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -2
- package/index.html +1101 -0
- package/kairotic/api.cljs +362 -0
- package/kairotic/assistant.cljs +505 -0
- package/kairotic/backends/gun-backend.js +371 -0
- package/kairotic/canvas.cljs +751 -0
- package/kairotic/code.cljs +108 -0
- package/kairotic/components.cljs +838 -0
- package/kairotic/core.cljs +96 -0
- package/kairotic/corefns.cljs +473 -0
- package/kairotic/docs.mjs +2 -0
- package/kairotic/engine.cljs +482 -0
- package/kairotic/examples.mjs +96 -0
- package/kairotic/faust.cljs +225 -0
- package/kairotic/help.cljs +1036 -0
- package/kairotic/hiccup.cljs +566 -0
- package/kairotic/input.cljs +271 -0
- package/kairotic/kfile.mjs +138 -0
- package/kairotic/library.cljs +214 -0
- package/kairotic/links.cljs +41 -0
- package/kairotic/mt.cljs +1604 -0
- package/kairotic/mtboxes.cljs +276 -0
- package/kairotic/packages/analysis.mjs +64 -0
- package/kairotic/packages/controls.mjs +28 -0
- package/kairotic/packages/data.mjs +22 -0
- package/kairotic/packages/faust-units/cmaes.mjs +82 -0
- package/kairotic/packages/faust-units/graph.mjs +212 -0
- package/kairotic/packages/faust-units/listen.mjs +125 -0
- package/kairotic/packages/faust-units/notation.mjs +139 -0
- package/kairotic/packages/faust-units/parse.mjs +439 -0
- package/kairotic/packages/faust-units/recipe.mjs +195 -0
- package/kairotic/packages/faust-units/render.mjs +69 -0
- package/kairotic/packages/faust-units/units.mjs +130 -0
- package/kairotic/packages/faust-units/view.mjs +353 -0
- package/kairotic/packages/faust-units.mjs +9 -0
- package/kairotic/packages/io.mjs +58 -0
- package/kairotic/packages/net.mjs +28 -0
- package/kairotic/packages/patterns.mjs +34 -0
- package/kairotic/packages/spatial.mjs +22 -0
- package/kairotic/packages/spectral/shift.mjs +169 -0
- package/kairotic/packages/spectral.mjs +16 -0
- package/kairotic/packages/synth.mjs +28 -0
- package/kairotic/packages/theory/counterpoint.mjs +798 -0
- package/kairotic/packages/theory/mirror.mjs +221 -0
- package/kairotic/packages/theory.mjs +46 -0
- package/kairotic/patterns.cljs +538 -0
- package/kairotic/presets.cljs +227 -0
- package/kairotic/quantize.cljs +782 -0
- package/kairotic/record.cljs +169 -0
- package/kairotic/release.cljs +90 -0
- package/kairotic/ring.cljs +129 -0
- package/kairotic/ringaudio.cljs +232 -0
- package/kairotic/ringcore.mjs +270 -0
- package/kairotic/score.cljs +176 -0
- package/kairotic/shapes.cljs +283 -0
- package/kairotic/signal.cljs +327 -0
- package/kairotic/sounds.cljs +565 -0
- package/kairotic/sounds.mjs +108 -0
- package/kairotic/span.cljs +129 -0
- package/kairotic/spell.cljs +281 -0
- package/kairotic/structure.cljs +145 -0
- package/kairotic/talk.cljs +115 -0
- package/kairotic/tempo.cljs +226 -0
- package/kairotic/treecanvas.cljs +299 -0
- package/kairotic/tutorials.mjs +1283 -0
- package/kairotic/vendor/codemirror.mjs +23 -0
- package/package.json +23 -4
- package/page/00-work.cljs +862 -0
- package/page/01-commands.cljs +1219 -0
- package/page/02-patch-view.cljs +1051 -0
- package/page/03-history-view.cljs +481 -0
- package/page/04-panels.cljs +448 -0
- package/page/05-gestures.cljs +1247 -0
- package/page/06-connection.cljs +219 -0
- package/page/07-claude.cljs +430 -0
- package/page/08-sessions.cljs +423 -0
- package/page/09-learn.cljs +602 -0
- package/page/10-embed.cljs +469 -0
- package/page/11-startup.cljs +61 -0
- package/page/12-feedback.cljs +172 -0
- package/page/13-perform.cljs +325 -0
- package/page/14-editor.cljs +656 -0
- package/page/15-presets.cljs +230 -0
- package/page/16-release.cljs +242 -0
- package/page/17-sounds.cljs +147 -0
- package/page/18-lines.cljs +594 -0
- package/page/19-files.cljs +247 -0
- package/page/19-html.cljs +152 -0
- package/page/20-talk.cljs +527 -0
- package/page/21-net.cljs +110 -0
- package/page/22-learn.cljs +303 -0
- package/page/23-ids.cljs +233 -0
- package/page/24-clipboard.cljs +136 -0
- package/page/25-tidy.cljs +74 -0
- package/page/26-probes.cljs +111 -0
- package/sources.mjs +2 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
;; SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
;; Kairotic · tempo
|
|
3
|
+
;; Played time as beats (issue #106). Notes played live are in seconds (or in beats at a tempo nobody chose:
|
|
4
|
+
;; transcribe writes them at 60 bpm), at an unknown tempo that moves. Before they can be quantized, their
|
|
5
|
+
;; time has to be mapped onto the beats of the music:
|
|
6
|
+
;; 1. the tempo: the period between beats that explains most intervals between onsets (each onset weighs
|
|
7
|
+
;; as much as the note it starts lasts), with a mild preference for moderate tempos;
|
|
8
|
+
;; 2. the beats: a dynamic programme over a grid of time (after Ellis 2007, "Beat tracking by dynamic
|
|
9
|
+
;; programming"): each beat sits where onsets are, and the gap to the one before stays close to the
|
|
10
|
+
;; period, so the beat can follow a tempo that speeds up or slows down;
|
|
11
|
+
;; 3. the bar: which beat is the first of the bar, by where the weight of the notes falls (a pickup when
|
|
12
|
+
;; the first beat is not a downbeat);
|
|
13
|
+
;; 4. the time map: [[time beat] …] (beats as kairotic.mt counts them, a quarter = 1), linear between its
|
|
14
|
+
;; points and beyond them. quantize uses it with the option :time (:track, or the map itself, kept).
|
|
15
|
+
;; Pure functions on numbers: no audio, no state.
|
|
16
|
+
|
|
17
|
+
(ns kairotic.tempo
|
|
18
|
+
(:require [kairotic.mt :as mt]))
|
|
19
|
+
|
|
20
|
+
(defn- sq [x] (* x x))
|
|
21
|
+
(defn- log2 [x] (/ (js/Math.log x) js/Math.LN2))
|
|
22
|
+
|
|
23
|
+
(def defaults
|
|
24
|
+
"The settings of the tracker: :prior the most likely beat period (units of the input: seconds at 60) and
|
|
25
|
+
how wide its preference is (octaves); :tight how much the beat resists changing its period; :chord how
|
|
26
|
+
close two onsets are to be one."
|
|
27
|
+
{:prior 0.6 :width 1.2 :tight 25 :chord 0.05 :weigh :note :last 0 :accent true :bass false})
|
|
28
|
+
|
|
29
|
+
(defn- accent-of
|
|
30
|
+
"How strong a note's attack was, 0 … 1: its :vel (0 … 1, or a MIDI velocity), or nil when it does not say."
|
|
31
|
+
[n]
|
|
32
|
+
(when-let [v (or (:vel n) (get-in n [:attrs :vel]))]
|
|
33
|
+
(when (number? v) (max 0 (min 1 (if (> v 1) (/ v 127) v))))))
|
|
34
|
+
|
|
35
|
+
(defn onsets
|
|
36
|
+
"Notes ({:on :off}, and their :attrs) as weighted onsets [{:t :w}], in order: onsets closer than :chord are
|
|
37
|
+
one; each weighs the notes it starts, longer notes more (a note of 1 unit about 1, one of a tenth about 0.4);
|
|
38
|
+
with :accent, a note played louder more (its :vel, issue #106: the strength of the attack is what places
|
|
39
|
+
the beat and the bar best); with :bass, a low note more."
|
|
40
|
+
[notes {:keys [chord accent bass]}]
|
|
41
|
+
(let [ns (sort-by :on (filter (fn [n] (and (number? (:on n)) (number? (:off n)))) notes))
|
|
42
|
+
w (fn [n] (* (max 0.3 (min 2 (log2 (+ 1 (* 2 (max 0 (- (:off n) (:on n))))))))
|
|
43
|
+
(if-let [a (and accent (accent-of n))] (+ 0.25 (* 1.5 a)) 1)
|
|
44
|
+
(if-let [p (and bass (mt/midi-of (:attrs n)))] (+ 1 (/ (max 0 (- 64 p)) 24)) 1)))]
|
|
45
|
+
(reduce (fn [acc n]
|
|
46
|
+
(let [lst (peek acc)]
|
|
47
|
+
(if (and lst (< (- (:on n) (:t lst)) chord))
|
|
48
|
+
(conj (pop acc) (update lst :w + (* 0.5 (w n))))
|
|
49
|
+
(conj acc {:t (:on n) :w (w n)}))))
|
|
50
|
+
[] ns)))
|
|
51
|
+
|
|
52
|
+
(defn period-score
|
|
53
|
+
"How well a beat period P explains the intervals between the onsets: each pair of onsets up to 4 periods
|
|
54
|
+
apart adds the product of their weights when their distance is a whole number of periods (a Gaussian of
|
|
55
|
+
the deviation, 6 % of the period wide)."
|
|
56
|
+
[os P]
|
|
57
|
+
(let [v (vec os) n (count v)]
|
|
58
|
+
(loop [i 0 acc 0]
|
|
59
|
+
(if (>= i n)
|
|
60
|
+
acc
|
|
61
|
+
(let [{ti :t wi :w} (v i)
|
|
62
|
+
acc (loop [j (inc i) acc acc]
|
|
63
|
+
(if (>= j n)
|
|
64
|
+
acc
|
|
65
|
+
(let [{tj :t wj :w} (v j) x (/ (- tj ti) P)]
|
|
66
|
+
(if (> x 4.5)
|
|
67
|
+
acc
|
|
68
|
+
(let [k (js/Math.round x)]
|
|
69
|
+
(recur (inc j) (if (>= k 1) (+ acc (* wi wj (js/Math.exp (/ (- (sq (- x k))) (* 2 (sq 0.06)))))) acc)))))))]
|
|
70
|
+
(recur (inc i) acc))))))
|
|
71
|
+
|
|
72
|
+
(defn period
|
|
73
|
+
"The beat period of weighted onsets: the best of the periods from 0.2 to 2.5 units (1 % apart), by
|
|
74
|
+
period-score times a preference for periods near :prior (a Gaussian in octaves, :width wide). nil with
|
|
75
|
+
fewer than 3 onsets."
|
|
76
|
+
[os {:keys [prior width]}]
|
|
77
|
+
(when (>= (count os) 3)
|
|
78
|
+
(let [ps (take-while (fn [p] (<= p 2.5)) (iterate (fn [p] (* p 1.01)) 0.2))
|
|
79
|
+
sc (fn [p] (* (period-score os p) (js/Math.exp (/ (- (sq (log2 (/ p prior)))) (* 2 (sq width))))))]
|
|
80
|
+
(apply max-key sc ps))))
|
|
81
|
+
|
|
82
|
+
(defn track
|
|
83
|
+
"The beats of weighted onsets with period P: their times, in order (after Ellis 2007). Time is a grid of
|
|
84
|
+
P/40; each point of it scores the onsets near it (a Gaussian 4 % of P wide), and a beat there adds its
|
|
85
|
+
score to the best beat before it, 0.66 to 1.5 periods back, less :tight × (log of the gap over P)². The
|
|
86
|
+
first beat is within 1.5 periods of the first onset; the last near the last onset."
|
|
87
|
+
[os P {:keys [tight]}]
|
|
88
|
+
(when (and P (seq os))
|
|
89
|
+
(let [dt (/ P 40) t0 (- (:t (first os)) (* 0.25 P)) t1 (+ (:t (peek (vec os))) (* 0.25 P))
|
|
90
|
+
n (inc (js/Math.ceil (/ (- t1 t0) dt)))
|
|
91
|
+
sig (* 0.04 P) reach (js/Math.ceil (/ (* 4 sig) dt))
|
|
92
|
+
env (let [a (make-array n)]
|
|
93
|
+
(dotimes [k n] (aset a k 0))
|
|
94
|
+
(doseq [{:keys [t w]} os
|
|
95
|
+
:let [c (js/Math.round (/ (- t t0) dt))]
|
|
96
|
+
k (range (max 0 (- c reach)) (min n (+ c reach 1)))]
|
|
97
|
+
(aset a k (+ (aget a k) (* w (js/Math.exp (/ (- (sq (- (+ t0 (* k dt)) t))) (* 2 (sq sig))))))))
|
|
98
|
+
a)
|
|
99
|
+
lo (js/Math.round (/ (* 0.66 P) dt)) hi (js/Math.round (/ (* 1.5 P) dt))
|
|
100
|
+
start (js/Math.round (/ (* 1.5 P) dt))
|
|
101
|
+
C (make-array n) prev (make-array n)]
|
|
102
|
+
(dotimes [k n]
|
|
103
|
+
(let [[bj bc] (loop [j (- k lo) bj -1 bc (- js/Infinity)]
|
|
104
|
+
(if (or (< j 0) (< j (- k hi)))
|
|
105
|
+
[bj bc]
|
|
106
|
+
(let [c (- (aget C j) (* tight (sq (js/Math.log (/ (* (- k j) dt) P)))))]
|
|
107
|
+
(if (> c bc) (recur (dec j) j c) (recur (dec j) bj bc)))))
|
|
108
|
+
fresh (when (< k start) 0)
|
|
109
|
+
[bj bc] (if (and fresh (or (< bj 0) (> fresh bc))) [-1 fresh] [bj bc])]
|
|
110
|
+
(aset C k (+ (aget env k) (if (< bj 0) (or fresh (- js/Infinity)) bc)))
|
|
111
|
+
(aset prev k bj)))
|
|
112
|
+
;; the last beat: the best point near the last onset
|
|
113
|
+
(let [kend (dec n) from (max 0 (- kend (js/Math.round (/ (* 0.75 P) dt))))
|
|
114
|
+
k* (apply max-key (fn [k] (aget C k)) (range from (inc kend)))]
|
|
115
|
+
(loop [k k* out ()]
|
|
116
|
+
(if (< k 0) (vec out) (recur (aget prev k) (cons (+ t0 (* k dt)) out))))))))
|
|
117
|
+
|
|
118
|
+
(defn phase-weights
|
|
119
|
+
"For each of the m phases (which of the first m beats is a downbeat), the share of the notes' weight that
|
|
120
|
+
falls on its downbeats (0 … 1): see bar-phase."
|
|
121
|
+
[os beats m]
|
|
122
|
+
(if (or (< m 2) (< (count beats) m))
|
|
123
|
+
(vec (repeat (max 1 m) (/ 1 (max 1 m))))
|
|
124
|
+
(let [v (vec os) bv (vec beats)
|
|
125
|
+
P (/ (- (peek bv) (first bv)) (max 1 (dec (count bv))))
|
|
126
|
+
s (mapv (fn [b] (reduce + (keep (fn [o] (when (< (js/Math.abs (- (:t o) b)) (* 0.15 P)) (:w o))) v))) bv)
|
|
127
|
+
total (max 1e-9 (reduce + s))]
|
|
128
|
+
(mapv (fn [k] (/ (reduce + (map-indexed (fn [i x] (if (= k (mod i m)) x 0)) s)) total)) (range m)))))
|
|
129
|
+
|
|
130
|
+
(defn bar-phase
|
|
131
|
+
"Which of the first m beats is a downbeat (0 … m-1): the one whose beats every m weigh most. A beat weighs
|
|
132
|
+
the onsets near it: by the time to the next onset (:weigh :ioi, long notes fall on strong beats) or by the
|
|
133
|
+
note's own weight (:weigh :note); with :last, the beat the last note starts on weighs that share of the
|
|
134
|
+
total more (a piece ends on a downbeat more often than not)."
|
|
135
|
+
([os beats m] (bar-phase os beats m {}))
|
|
136
|
+
([os beats m {:keys [weigh last] :or {weigh :ioi last 0}}]
|
|
137
|
+
(if (or (< m 2) (< (count beats) m))
|
|
138
|
+
0
|
|
139
|
+
(let [v (vec os) bv (vec beats)
|
|
140
|
+
P (/ (- (peek bv) (first bv)) (max 1 (dec (count bv))))
|
|
141
|
+
wt (if (= weigh :note)
|
|
142
|
+
(mapv :w v)
|
|
143
|
+
(mapv (fn [i] (min (* 2 P) (if (< (inc i) (count v)) (- (:t (v (inc i))) (:t (v i))) (* 2 P)))) (range (count v))))
|
|
144
|
+
s (mapv (fn [b] (reduce + (keep-indexed (fn [i o] (when (< (js/Math.abs (- (:t o) b)) (* 0.15 P)) (wt i))) v))) bv)
|
|
145
|
+
lastb (apply min-key (fn [i] (js/Math.abs (- (bv i) (:t (peek v))))) (range (count bv)))
|
|
146
|
+
total (reduce + s)]
|
|
147
|
+
(apply max-key (fn [k] (+ (reduce + (map-indexed (fn [i x] (if (= k (mod i m)) x 0)) s))
|
|
148
|
+
(if (= k (mod lastb m)) (* last total) 0)
|
|
149
|
+
(if (zero? k) 0.01 0)))
|
|
150
|
+
(range m))))))
|
|
151
|
+
|
|
152
|
+
(defn tactus
|
|
153
|
+
"The beat a meter is felt in, in quarters, and how many of them make a bar: 4/4 → [1 4], 3/4 → [1 3],
|
|
154
|
+
6/8 → [1.5 2], 9/8 → [1.5 3], 2/2 → [2 2], 5/8 [2+3] and other meters in eighths → [0.5 5]."
|
|
155
|
+
[meter]
|
|
156
|
+
(let [{[p q] :meter} (mt/meter-of meter) u (/ 4 q) bl (* p u)]
|
|
157
|
+
(cond (and (= q 8) (zero? (mod p 3)) (> p 3)) [1.5 (/ p 3)]
|
|
158
|
+
(>= q 8) [u p]
|
|
159
|
+
:else [u p])))
|
|
160
|
+
|
|
161
|
+
(defn beats-of
|
|
162
|
+
"The beats found in notes played freely ({:on :off}): {:os the weighted onsets :period :beats [times]}, or
|
|
163
|
+
nil when there are too few onsets."
|
|
164
|
+
[notes opts]
|
|
165
|
+
(let [o (merge defaults opts)
|
|
166
|
+
os (onsets notes o)
|
|
167
|
+
P (period os o)
|
|
168
|
+
beats (track os P o)]
|
|
169
|
+
(when (>= (count beats) 2) {:os os :period P :beats beats})))
|
|
170
|
+
|
|
171
|
+
(defn map-of
|
|
172
|
+
"A time map from beats found: each worth u quarters, m of them to a bar, the k-th the first downbeat (the
|
|
173
|
+
ones before it a pickup, at the end of a first bar)."
|
|
174
|
+
[beats u m k]
|
|
175
|
+
(mapv (fn [i t] [(/ (js/Math.round (* t 1e4)) 1e4) (+ (* (- i k) u) (if (pos? k) (* u m) 0))]) (range) beats))
|
|
176
|
+
|
|
177
|
+
(defn time-map
|
|
178
|
+
"The time map of notes played freely ({:on :off}, in units: seconds at 60): [[time beat] …], one point
|
|
179
|
+
per beat found, beats counted as kairotic.mt does (a quarter = 1) in the meter given, with a pickup bar
|
|
180
|
+
when the first beat found is not a downbeat. nil when there is too little to find a beat in.
|
|
181
|
+
opts: those of `defaults`, and :meter."
|
|
182
|
+
([notes] (time-map notes {}))
|
|
183
|
+
([notes opts]
|
|
184
|
+
(let [o (merge defaults opts)
|
|
185
|
+
os (onsets notes o)
|
|
186
|
+
P (period os o)
|
|
187
|
+
beats (track os P o)]
|
|
188
|
+
(when (>= (count beats) 2)
|
|
189
|
+
(let [[u m] (tactus (or (:meter o) [4 4]))
|
|
190
|
+
k (bar-phase os beats m (select-keys o [:weigh :last]))
|
|
191
|
+
bl (* u m)]
|
|
192
|
+
(mapv (fn [i t] [(/ (js/Math.round (* t 1e4)) 1e4) (+ (* (- i k) u) (if (pos? k) bl 0))]) (range) beats))))))
|
|
193
|
+
|
|
194
|
+
(defn sane-map
|
|
195
|
+
"A time map as kept in options: at least two points [time beat], both increasing; else nil."
|
|
196
|
+
[m]
|
|
197
|
+
(when (and (sequential? m) (>= (count m) 2)
|
|
198
|
+
(every? (fn [p] (and (sequential? p) (= 2 (count p)) (every? number? p))) m))
|
|
199
|
+
(let [m (vec (sort-by first (map vec m)))]
|
|
200
|
+
(when (every? (fn [[[a x] [b y]]] (and (< a b) (< x y))) (partition 2 1 m)) m))))
|
|
201
|
+
|
|
202
|
+
(defn ->beat
|
|
203
|
+
"A time as a beat, through a time map: linear between its points, and beyond them at the period of the
|
|
204
|
+
nearest two."
|
|
205
|
+
[m t]
|
|
206
|
+
(let [v (vec m) n (count v)
|
|
207
|
+
i (loop [i 0] (if (and (< i (- n 2)) (>= t (first (v (inc i))))) (recur (inc i)) i))
|
|
208
|
+
[[a x] [b y]] [(v i) (v (inc i))]]
|
|
209
|
+
(+ x (* (- t a) (/ (- y x) (- b a))))))
|
|
210
|
+
|
|
211
|
+
(defn ->time
|
|
212
|
+
"A beat as a time, through a time map (the inverse of ->beat): linear between its points and beyond them."
|
|
213
|
+
[m b]
|
|
214
|
+
(->beat (mapv (fn [[t x]] [x t]) m) b))
|
|
215
|
+
|
|
216
|
+
(defn tempo-of-map
|
|
217
|
+
"The tempo a time map stands for, in beats per minute of its units (60 units a minute): the median of the
|
|
218
|
+
tempos between its points."
|
|
219
|
+
[m]
|
|
220
|
+
(let [ts (sort (map (fn [[[a x] [b y]]] (* 60 (/ (- y x) (- b a)))) (partition 2 1 m)))]
|
|
221
|
+
(when (seq ts) (nth ts (quot (count ts) 2)))))
|
|
222
|
+
|
|
223
|
+
(defn map-notes
|
|
224
|
+
"Notes ({:on :off …}) with their times mapped through a time map, onto beats."
|
|
225
|
+
[notes m]
|
|
226
|
+
(mapv (fn [n] (assoc n :on (->beat m (:on n)) :off (->beat m (:off n)))) notes))
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
;; SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
;; Kairotic · treecanvas
|
|
3
|
+
;; A tree box seen and drawn as a canvas (discussion #92): the tree is the data, the canvas one more view
|
|
4
|
+
;; of it, as the score and the code are. Its notes are strokes of frequency over time (each with an id
|
|
5
|
+
;; that leads back to where the note is written), and what is drawn on the canvas becomes edits of the
|
|
6
|
+
;; tree (kairotic.mt/apply-op), so the history stays a history of the tree.
|
|
7
|
+
;;
|
|
8
|
+
;; · A tree without measures is free time: drawing adds a note placed with :at, dragging moves it,
|
|
9
|
+
;; erasing removes it. A free tree is kept as [:voice {…} [:par [:note {:at t :dur d :pitch p} …]]];
|
|
10
|
+
;; one written otherwise (a melody string, a sequence) takes that form the first time a note's time
|
|
11
|
+
;; changes (a change of pitch alone is written where the note is).
|
|
12
|
+
;; · A tree with measures stays measured: a change of pitch alone is written in the note; a change of
|
|
13
|
+
;; time re-quantizes the bars it touches (kairotic.quantize, in that bar's meter); with ⌥ a note leaves
|
|
14
|
+
;; the measures for a free voice of the tree ({:name "free"}), as a stroke drawn freehand does.
|
|
15
|
+
;; · Its grid and tuning are attributes of the tree's root: {:grid {:step 0.25 :cycles [4]} :tuning {…}};
|
|
16
|
+
;; a measured tree without :grid shows its bars, each in its own meter, and a tree without :tuning is drawn in semitones
|
|
17
|
+
;; (12-tet; :tuning false is continuous frequency).
|
|
18
|
+
|
|
19
|
+
(ns kairotic.treecanvas
|
|
20
|
+
(:require [kairotic.canvas :as cv]
|
|
21
|
+
[kairotic.mt :as mt]
|
|
22
|
+
[kairotic.quantize :as qz]))
|
|
23
|
+
|
|
24
|
+
(def eps 1e-6)
|
|
25
|
+
|
|
26
|
+
;; ───────── the view ─────────
|
|
27
|
+
|
|
28
|
+
(defn- note-events [x] (filterv (fn [e] (= :note (:kind e))) (mt/events x)))
|
|
29
|
+
|
|
30
|
+
(defn- pts-of
|
|
31
|
+
"The points of a note as a stroke: [t hz], its :gliss bending it."
|
|
32
|
+
[e]
|
|
33
|
+
(let [t0 (:t e) d (max 0.01 (:dur e)) hz (cv/midi->hz (:pitch e))
|
|
34
|
+
g (let [g (:gliss e)] (if (and (sequential? g) (>= (count g) 2)) g [[0 0] [1 0]]))]
|
|
35
|
+
(mapv (fn [[x c]] [(cv/round-to (+ t0 (* d x)) 4) (cv/round-to (cv/cents->hz c hz) 3)]) g)))
|
|
36
|
+
|
|
37
|
+
(defn- grid-of
|
|
38
|
+
"The canvas grid of a tree: its root's :grid, else the bars of its measures (of its first voice), each in
|
|
39
|
+
its own meter (:bars, when they change), else none."
|
|
40
|
+
[x]
|
|
41
|
+
(or (cv/sane-gx (:grid (mt/attrs x)))
|
|
42
|
+
(let [ms (mt/measures-of (first (mt/voices-of (mt/with-meters x))))
|
|
43
|
+
bar (fn [m] (let [{[p q] :meter g :groups} (mt/meter-of m) u (/ 4 q)]
|
|
44
|
+
{:L (* p u) :u u :groups (when (seq g) (mapv (fn [k] (* k u)) g))}))
|
|
45
|
+
bs (mapv bar ms)]
|
|
46
|
+
(when-let [b0 (first bs)]
|
|
47
|
+
(cond-> {:step (min 0.25 (reduce min (map :u bs))) :cycles (or (:groups b0) [(:L b0)])}
|
|
48
|
+
(some (fn [b] (not= (dissoc b :u) (dissoc b0 :u))) bs)
|
|
49
|
+
(assoc :bars (mapv (fn [s b] {:s s :L (:L b) :groups (:groups b)}) (reductions + 0 (map :L bs)) bs)))))))
|
|
50
|
+
|
|
51
|
+
(defn view-data
|
|
52
|
+
"A tree as the data of a freqt-like canvas: {:strokes {:n1 {:pts …}} :gx :gy :ids {:n1 {…}}}, where :ids
|
|
53
|
+
says for each stroke the note it is: its place in the expanded tree (:path), where it is written
|
|
54
|
+
(:src :sub, or :rtm/:use when it is not written as a note), its voice, start, duration and pitch."
|
|
55
|
+
[tree]
|
|
56
|
+
(let [x (mt/expand-source (or tree [:voice]) nil)
|
|
57
|
+
evs (note-events x)
|
|
58
|
+
ids (into {} (map-indexed (fn [i e]
|
|
59
|
+
(let [n (mt/node-at x (:path e)) m (meta n)]
|
|
60
|
+
[(keyword (str "n" (inc i)))
|
|
61
|
+
{:path (:path e) :src (:src m) :sub (:sub m) :rtm (:rtm m) :use (:use m)
|
|
62
|
+
:voice (:voice e) :t (:t e) :dur (:dur e) :pitch (:pitch e)
|
|
63
|
+
:attrs (mt/attrs n)}]))
|
|
64
|
+
evs))]
|
|
65
|
+
{:strokes (into {} (map-indexed (fn [i e] [(keyword (str "n" (inc i))) {:pts (pts-of e)}]) evs))
|
|
66
|
+
:gx (grid-of x)
|
|
67
|
+
;; a tree is written in semitones unless it says otherwise: 12-tet, its :tuning, or false (continuous)
|
|
68
|
+
:gy (let [a (mt/attrs x)]
|
|
69
|
+
(if (contains? a :tuning) (cv/sane-gy (:tuning a)) (assoc (get cv/tunings "12-tet") :ref cv/default-ref)))
|
|
70
|
+
:ids ids}))
|
|
71
|
+
|
|
72
|
+
;; ───────── strokes → notes ─────────
|
|
73
|
+
|
|
74
|
+
(defn- note-of-pts
|
|
75
|
+
"The note a stroke is: placed at its first point, as long as it lasts, at the pitch of its first point
|
|
76
|
+
(a name kept as it was when it still fits, see mt/set-midi), its bends a :gliss. was: the attributes the
|
|
77
|
+
note had (its marks are kept)."
|
|
78
|
+
[pts was]
|
|
79
|
+
(let [[t0 f0] (first pts) [t1 _] (peek pts)
|
|
80
|
+
m (cv/hz->midi f0) mr (js/Math.round m)
|
|
81
|
+
dur (max 0.01 (- t1 t0))
|
|
82
|
+
g (mapv (fn [[t f]] [(cv/round-to (/ (- t t0) dur) 3) (+ 0 (cv/round-to (cv/hz->cents f (cv/midi->hz mr)) 1))]) pts)
|
|
83
|
+
flat? (every? (fn [[_ c]] (< (js/Math.abs (- c (second (first g)))) 1)) g)
|
|
84
|
+
exact (if (< (js/Math.abs (- m mr)) 0.02) mr (cv/round-to m 2))
|
|
85
|
+
base (dissoc (or was {}) :gliss :at :dur :tie :grace)
|
|
86
|
+
a (if (some? (:pitch base)) (mt/set-midi base (if flat? exact mr)) (assoc base :pitch (if flat? exact mr)))]
|
|
87
|
+
(cond-> (assoc (dissoc a :hz) :at (cv/round-to t0 4) :dur (cv/round-to dur 4))
|
|
88
|
+
(not flat?) (assoc :gliss g))))
|
|
89
|
+
|
|
90
|
+
(defn free-form
|
|
91
|
+
"A tree as free time: [:voice {its root's attributes} [:par [:note {:at …}] …]], every note placed where
|
|
92
|
+
it sounds (ties joined), with the name it was written with."
|
|
93
|
+
[tree]
|
|
94
|
+
(let [x (mt/expand tree)
|
|
95
|
+
root (dissoc (mt/attrs x) :at :dur)
|
|
96
|
+
notes (for [e (note-events x)]
|
|
97
|
+
(let [a (mt/attrs (mt/node-at x (:path e)))]
|
|
98
|
+
[:note (cond-> (merge (select-keys a [:art :dyn :text :vel :env :gliss :cents :id :grace])
|
|
99
|
+
{:at (cv/round-to (:t e) 4) :dur (cv/round-to (:dur e) 4)
|
|
100
|
+
:pitch (if (mt/spelling-of a) (:pitch a) (:pitch e))})
|
|
101
|
+
(number? (:hz a)) (-> (dissoc :pitch) (assoc :hz (:hz a))))]))]
|
|
102
|
+
[:voice (dissoc root :meter :groups) (into [:par] notes)]))
|
|
103
|
+
|
|
104
|
+
(defn- canonical-free?
|
|
105
|
+
"Is a tree written as free-form writes it (so that a note's path is [0 i])?"
|
|
106
|
+
[tree]
|
|
107
|
+
(and (mt/node? tree) (= :voice (mt/tag tree)) (= 1 (count (mt/kids tree)))
|
|
108
|
+
(let [p (first (mt/kids tree))]
|
|
109
|
+
(and (mt/node? p) (= :par (mt/tag p))
|
|
110
|
+
(every? (fn [k] (and (mt/node? k) (= :note (mt/tag k)) (number? (:at (mt/attrs k))))) (mt/kids p))))))
|
|
111
|
+
|
|
112
|
+
;; ───────── where a note is written ─────────
|
|
113
|
+
|
|
114
|
+
(defn writable
|
|
115
|
+
"Edits that make a written note editable (a melody string becomes its nodes) and the path of the note:
|
|
116
|
+
[ops path], or nil when it comes from a fragment (:defs) or a proportional rhythm (:rtm)."
|
|
117
|
+
[t {:keys [src sub use rtm]}]
|
|
118
|
+
(cond
|
|
119
|
+
(or use rtm (nil? src)) nil
|
|
120
|
+
(nil? sub) [[] src]
|
|
121
|
+
:else (let [s (mt/node-at t src) nodes (mt/melody s) parent (pop src) idx (peek src)]
|
|
122
|
+
[(into [[:remove src]] (map-indexed (fn [k n] [:insert (conj parent (+ idx k)) n]) nodes))
|
|
123
|
+
(into (conj parent (+ idx (first sub))) (rest sub))])))
|
|
124
|
+
|
|
125
|
+
(defn- not-written [info]
|
|
126
|
+
(throw (js/Error. (if (:rtm info)
|
|
127
|
+
"That note comes from a proportional rhythm (:rtm): edit its :shares or :pitches"
|
|
128
|
+
"That note comes from a fragment (:defs): edit the fragment, or write it out"))))
|
|
129
|
+
|
|
130
|
+
;; ───────── measured trees: re-quantizing the bars a change touches ─────────
|
|
131
|
+
|
|
132
|
+
(defn- voice-nodes
|
|
133
|
+
"The :voice nodes of x with the name mt/events gives their notes (:name, :id, or v<depth>-<index>), or
|
|
134
|
+
the whole tree as one voice (named nil) when it has none."
|
|
135
|
+
[x]
|
|
136
|
+
(let [found (atom [])]
|
|
137
|
+
(letfn [(go [n path]
|
|
138
|
+
(when (mt/node? n)
|
|
139
|
+
(if (= :voice (mt/tag n))
|
|
140
|
+
(swap! found conj [(or (:name (mt/attrs n)) (:id (mt/attrs n)) (str "v" (count path) "-" (last path))) n])
|
|
141
|
+
(doseq [[i k] (map-indexed vector (mt/kids n))] (go k (conj path i))))))]
|
|
142
|
+
(go x []))
|
|
143
|
+
(if (seq @found) @found [[nil x]])))
|
|
144
|
+
|
|
145
|
+
(defn- bars-of
|
|
146
|
+
"The measures of each voice of the expanded tree x: {voice [{:src :start :len :attrs :meter} …]}."
|
|
147
|
+
[x]
|
|
148
|
+
(let [x (mt/with-meters x)]
|
|
149
|
+
(into {} (for [[k v] (voice-nodes x)
|
|
150
|
+
:let [ms (mt/measures-of v)
|
|
151
|
+
lens (map (fn [m] (let [[p q] (:meter (mt/attrs m))] (* p (/ 4 q)))) ms)]]
|
|
152
|
+
[k (mapv (fn [m s l] {:src (:src (meta m)) :use (:use (meta m)) :start s :len l :attrs (mt/attrs m)
|
|
153
|
+
:meter (select-keys (mt/attrs m) [:meter :groups])})
|
|
154
|
+
ms (reductions + 0 lens) lens)]))))
|
|
155
|
+
|
|
156
|
+
(defn- voice-key
|
|
157
|
+
"The key bars-of uses for an event's voice."
|
|
158
|
+
[bars e]
|
|
159
|
+
(if (contains? bars (:voice e)) (:voice e) (first (keys bars))))
|
|
160
|
+
|
|
161
|
+
(defn- requantize-bar
|
|
162
|
+
"The new children of one measure (start s, length L, its meter) for these notes in free time
|
|
163
|
+
({:on :off :attrs}, beats): each one clipped to the bar, and tied (:tie) when it goes on past it."
|
|
164
|
+
[notes {:keys [start len meter]}]
|
|
165
|
+
(let [end (+ start len)
|
|
166
|
+
inside (for [n notes :let [on (max (:on n) start) off (min (:off n) end)] :when (> off (+ on eps))]
|
|
167
|
+
{:on (- on start) :off (- off start)
|
|
168
|
+
:attrs (cond-> (:attrs n) (> (:off n) (+ end eps)) (assoc :tie true))})
|
|
169
|
+
v (qz/notes->voice (vec inside) (qz/options {:meter meter :voices 1 :legato 0} nil))
|
|
170
|
+
m (first (filter (fn [k] (and (mt/node? k) (= :measure (mt/tag k)))) (mt/kids v)))]
|
|
171
|
+
(vec (mt/kids m))))
|
|
172
|
+
|
|
173
|
+
(defn- measured-ops
|
|
174
|
+
"Ops that re-quantize, in the voice vk, the bars a change touches: the notes of that voice (as they
|
|
175
|
+
sound) with `change` applied (a function of that list of notes), written again in every bar that
|
|
176
|
+
touches [t0, t1]."
|
|
177
|
+
[x bars vk change t0 t1]
|
|
178
|
+
(let [bs (get bars vk)
|
|
179
|
+
evs (filter (fn [e] (= (voice-key bars e) vk)) (note-events x))
|
|
180
|
+
notes (change (mapv (fn [e] (let [a (mt/attrs (mt/node-at x (:path e)))]
|
|
181
|
+
{:path (:path e) :on (:t e) :off (+ (:t e) (:dur e))
|
|
182
|
+
:attrs (merge (select-keys a [:art :dyn :text :vel :env :gliss :cents :id])
|
|
183
|
+
{:pitch (if-let [s (mt/spelling-of a)] (:pitch a) (:pitch e))})}))
|
|
184
|
+
evs))
|
|
185
|
+
touched (filter (fn [b] (and (< (:start b) (- t1 eps)) (> (+ (:start b) (:len b)) (+ t0 eps)))) bs)
|
|
186
|
+
touched (if (seq touched) touched (take-last 1 bs))]
|
|
187
|
+
(when (some (fn [b] (or (:use b) (nil? (:src b)))) touched)
|
|
188
|
+
(throw (js/Error. "Those bars come from a fragment (:defs): write them out to edit them")))
|
|
189
|
+
(vec (for [b touched]
|
|
190
|
+
[:set (:src b) (into [:measure (:attrs b)] (requantize-bar notes b))]))))
|
|
191
|
+
|
|
192
|
+
(defn- free-voice-ops
|
|
193
|
+
"Ops that put a note in the free voice of a measured tree ({:name \"free\"}), making it when there is none."
|
|
194
|
+
[tree note]
|
|
195
|
+
(let [free-i (when (= :par (mt/tag tree))
|
|
196
|
+
(first (keep-indexed (fn [i k] (when (and (mt/node? k) (= :voice (mt/tag k)) (= "free" (:name (mt/attrs k)))) i))
|
|
197
|
+
(mt/kids tree))))]
|
|
198
|
+
(if free-i
|
|
199
|
+
(let [v (nth (mt/kids tree) free-i) p (first (mt/kids v))]
|
|
200
|
+
(if (and (mt/node? p) (= :par (mt/tag p)))
|
|
201
|
+
[[:insert [free-i 0 (count (mt/kids p))] note]]
|
|
202
|
+
[[:insert [free-i (count (mt/kids v))] note]]))
|
|
203
|
+
[[:set [] [:par (if (= :par (mt/tag tree)) (into [:par (mt/attrs tree)] (mt/kids tree)) tree)
|
|
204
|
+
[:voice {:name "free"} [:par note]]]]])))
|
|
205
|
+
|
|
206
|
+
;; ───────── canvas events → tree edits ─────────
|
|
207
|
+
|
|
208
|
+
(defn- root-attrs-op [tree f] [:attrs [] (f (mt/attrs tree))])
|
|
209
|
+
|
|
210
|
+
(defn edit-ops
|
|
211
|
+
"The edits of the tree (mt/apply-op operations) that make a canvas event on its view: [:stroke id pts]
|
|
212
|
+
(a new id draws a note), [:move id dt cents], [:stretch id k], [:erase id], [:clear], [:grid-x gx],
|
|
213
|
+
[:tuning gy] or [:batch [events]]. opts: {:free true} takes a note of a measured tree out of the
|
|
214
|
+
measures (⌥). Throws, saying why, for a note that is not written as one (:defs, :rtm)."
|
|
215
|
+
([tree ev] (edit-ops tree ev {}))
|
|
216
|
+
([tree [op & args :as ev] opts]
|
|
217
|
+
(let [tree (or tree [:voice [:par]])]
|
|
218
|
+
(case op
|
|
219
|
+
:batch (loop [t tree evs (filter vector? (first args)) out []]
|
|
220
|
+
(if (empty? evs)
|
|
221
|
+
out
|
|
222
|
+
(let [ops (edit-ops t (first evs) opts)]
|
|
223
|
+
(recur (reduce mt/apply-op t ops) (rest evs) (into out ops)))))
|
|
224
|
+
:grid-x [(root-attrs-op tree (fn [a] (if-let [g (cv/sane-gx (first args))] (assoc a :grid g) (dissoc a :grid))))]
|
|
225
|
+
(:tuning :grid-y) [(root-attrs-op tree (fn [a] (if-let [g (cv/sane-gy (first args))] (assoc a :tuning g) (assoc a :tuning false))))]
|
|
226
|
+
:clear [[:set [] [:voice (dissoc (mt/attrs tree) :meter :groups) [:par]]]]
|
|
227
|
+
(:stroke :move :stretch :erase)
|
|
228
|
+
(let [v (view-data tree)
|
|
229
|
+
v2 (cv/step (dissoc v :ids) ev)
|
|
230
|
+
sid (if (and (= op :stroke) (keyword? (first args))) (first args) (first args))
|
|
231
|
+
old (get-in v [:strokes sid :pts])
|
|
232
|
+
new (get-in v2 [:strokes sid :pts])
|
|
233
|
+
new (if (and (= op :stroke) (not (keyword? (first args)))) (cv/sane-pts (first args)) new)
|
|
234
|
+
info (get-in v [:ids sid])
|
|
235
|
+
measured? (mt/measured? tree)]
|
|
236
|
+
(cond
|
|
237
|
+
(and (not= op :stroke) (nil? info)) []
|
|
238
|
+
(and (= op :stroke) (nil? new)) []
|
|
239
|
+
;; a new note
|
|
240
|
+
(nil? old)
|
|
241
|
+
(let [note [:note (note-of-pts new nil)]]
|
|
242
|
+
(cond
|
|
243
|
+
(and measured? (:free opts)) (free-voice-ops tree note)
|
|
244
|
+
measured?
|
|
245
|
+
(let [x (mt/expand-source tree nil) bars (bars-of x)
|
|
246
|
+
a (mt/attrs note) on (:at a) off (+ (:at a) (:dur a))
|
|
247
|
+
;; the voice whose notes are nearest in pitch around there
|
|
248
|
+
evs (note-events x)
|
|
249
|
+
near (filter (fn [e] (< (js/Math.abs (- (:t e) on)) 8)) evs)
|
|
250
|
+
vk (if (seq near)
|
|
251
|
+
(voice-key bars (apply min-key (fn [e] (js/Math.abs (- (:pitch e) (mt/midi-of a)))) near))
|
|
252
|
+
(first (keys bars)))]
|
|
253
|
+
(measured-ops x bars vk (fn [ns] (conj ns {:on on :off off :attrs (dissoc a :at :dur)})) on off))
|
|
254
|
+
(canonical-free? tree) [[:insert [0 (count (mt/kids (first (mt/kids tree))))] note]]
|
|
255
|
+
:else (let [f (free-form tree)] (into [[:set [] f]] (edit-ops f ev opts)))))
|
|
256
|
+
;; erased
|
|
257
|
+
(nil? new)
|
|
258
|
+
(cond
|
|
259
|
+
;; in measures, an erased note leaves a rest as long (or leaves its chord)
|
|
260
|
+
measured?
|
|
261
|
+
(let [[pre target] (or (writable tree info) (not-written info))
|
|
262
|
+
t1 (reduce mt/apply-op tree pre)
|
|
263
|
+
n (mt/node-at t1 target)
|
|
264
|
+
in-chord? (= :chord (mt/tag (mt/node-at t1 (pop target))))
|
|
265
|
+
d (or (:dur (mt/attrs n)) (:dur (mt/attrs (mt/node-at (mt/explicit t1) target))) 1)]
|
|
266
|
+
(conj pre (if in-chord? [:remove target] [:set target [:rest {:dur d}]])))
|
|
267
|
+
(canonical-free? tree) [[:remove (:src info)]]
|
|
268
|
+
:else (let [[pre target] (or (writable tree info) (not-written info))]
|
|
269
|
+
(conj pre [:remove target])))
|
|
270
|
+
;; changed
|
|
271
|
+
:else
|
|
272
|
+
(let [[a0 _] (first old) [a1 _] (peek old) [b0 _] (first new) [b1 _] (peek new)
|
|
273
|
+
same-time? (and (< (js/Math.abs (- a0 b0)) 1e-3) (< (js/Math.abs (- a1 b1)) 1e-3))
|
|
274
|
+
was (:attrs info)
|
|
275
|
+
changed (note-of-pts new was)]
|
|
276
|
+
(cond
|
|
277
|
+
;; a change of pitch alone: in the note, wherever it is written
|
|
278
|
+
(and same-time? (not (:free opts)))
|
|
279
|
+
(let [[pre target] (or (writable tree info) (not-written info))
|
|
280
|
+
n (mt/node-at (reduce mt/apply-op tree pre) target)]
|
|
281
|
+
(conj pre [:attrs target (cond-> (merge (dissoc (mt/attrs n) :cents :hz :pitch) (dissoc changed :at :dur))
|
|
282
|
+
(not (:gliss changed)) (dissoc :gliss))]))
|
|
283
|
+
(and measured? (:free opts))
|
|
284
|
+
(let [x (mt/expand-source tree nil) bars (bars-of x)
|
|
285
|
+
out (measured-ops x bars (voice-key bars {:voice (:voice info)})
|
|
286
|
+
(fn [ns] (vec (remove (fn [n] (= (:path n) (:path info))) ns)))
|
|
287
|
+
(:t info) (+ (:t info) (:dur info)))
|
|
288
|
+
t2 (reduce mt/apply-op tree out)]
|
|
289
|
+
(into out (free-voice-ops t2 [:note changed])))
|
|
290
|
+
measured?
|
|
291
|
+
(let [x (mt/expand-source tree nil) bars (bars-of x)
|
|
292
|
+
a changed]
|
|
293
|
+
(measured-ops x bars (voice-key bars {:voice (:voice info)})
|
|
294
|
+
(fn [ns] (conj (vec (remove (fn [n] (= (:path n) (:path info))) ns))
|
|
295
|
+
{:on (:at a) :off (+ (:at a) (:dur a)) :attrs (dissoc a :at :dur)}))
|
|
296
|
+
(min (:t info) (:at a)) (max (+ (:t info) (:dur info)) (+ (:at a) (:dur a)))))
|
|
297
|
+
(canonical-free? tree) [[:set (:src info) [:note (merge (dissoc was :gliss) changed)]]]
|
|
298
|
+
:else (let [f (free-form tree)] (into [[:set [] f]] (edit-ops f ev opts)))))))
|
|
299
|
+
[]))))
|