@kairotic/app 0.0.0-stage → 0.2.1
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,751 @@
|
|
|
1
|
+
;; SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
;; Kairotic · canvas
|
|
3
|
+
;; A graphic buffer: strokes in a plane. It neither sounds nor advances by itself; it is only a
|
|
4
|
+
;; representation that other components read (kairotic.engine, box `reader`), along X or along Y.
|
|
5
|
+
;;
|
|
6
|
+
;; Two kinds of box share this model:
|
|
7
|
+
;; · canvas — generic: any numeric data. Its axes have a range and a scale (linear or log), strokes
|
|
8
|
+
;; go in any direction, and reading at x (or y) gives every crossing of every stroke.
|
|
9
|
+
;; {:strokes {:t1 {:pts [[x y] ...]}}
|
|
10
|
+
;; :ax {:x {:lo 0 :hi 8 :log false} :y {:lo 0 :hi 1 :log false}}
|
|
11
|
+
;; :gx {:step 0.5 :cycles [4]} :gy {:step 0.1}} ; grids (optional)
|
|
12
|
+
;; (a tree's roll may add :bars [{:s :L :groups}] to :gx: bars of different meters, one by one)
|
|
13
|
+
;; · freqt — frequency over time: no :ax; time goes from 0, frequency is on a log scale, strokes go
|
|
14
|
+
;; forward in time, and :gy can be a tuning {:name "12-tet" :cents [100 ... 1200] :ref 261.63}.
|
|
15
|
+
;; It may keep how its score is quantized: :quant {:meter "6/8" :divs [1 2 3 4] :fix {2 3} …}
|
|
16
|
+
;; (kairotic.quantize reads it; [:quantize opts] sets it, nil removes it).
|
|
17
|
+
;; Between two points the value is interpolated on the scale of its axis (log → geometric, as pitch).
|
|
18
|
+
|
|
19
|
+
(ns kairotic.canvas
|
|
20
|
+
(:refer-clojure :exclude [empty cycle])
|
|
21
|
+
(:require [clojure.string :as str]
|
|
22
|
+
[clojure.edn :as edn]))
|
|
23
|
+
|
|
24
|
+
(def empty {:strokes {} :gx nil :gy nil})
|
|
25
|
+
|
|
26
|
+
(def default-ax {:x {:lo 0 :hi 8 :log false} :y {:lo 0 :hi 1 :log false}})
|
|
27
|
+
(def generic-empty (assoc empty :ax default-ax))
|
|
28
|
+
|
|
29
|
+
(defn generic? "A generic canvas (with axes), not a freqt." [d] (map? (:ax d)))
|
|
30
|
+
|
|
31
|
+
(defn num? [x] (and (number? x) (not (js/isNaN x)) (js/isFinite x)))
|
|
32
|
+
(defn round-to [x d] (let [k (js/Math.pow 10 d)] (/ (js/Math.round (* x k)) k)))
|
|
33
|
+
(defn log2 [x] (/ (js/Math.log x) (js/Math.log 2)))
|
|
34
|
+
|
|
35
|
+
(defn hz->cents [hz ref] (* 1200 (log2 (/ hz ref))))
|
|
36
|
+
(defn cents->hz [c ref] (* ref (js/Math.pow 2 (/ c 1200))))
|
|
37
|
+
(defn midi->hz [m] (* 440 (js/Math.pow 2 (/ (- m 69) 12))))
|
|
38
|
+
(defn hz->midi [hz] (+ 69 (* 12 (log2 (/ hz 440)))))
|
|
39
|
+
|
|
40
|
+
(def note-names ["C" "C#" "D" "D#" "E" "F" "F#" "G" "G#" "A" "A#" "B"])
|
|
41
|
+
(defn midi-name [m] (str (nth note-names (mod m 12)) (dec (js/Math.floor (/ m 12)))))
|
|
42
|
+
|
|
43
|
+
(defn fmt
|
|
44
|
+
"A short number for code: no trailing zeros."
|
|
45
|
+
[x] (let [r (round-to x 3)] (if (= r (js/Math.round r)) (str (js/Math.round r)) (str r))))
|
|
46
|
+
|
|
47
|
+
;; ───────── tunings (Y grid) ─────────
|
|
48
|
+
;; As in Scala: the degrees in cents above the reference, without the 1/1; the last one is the period.
|
|
49
|
+
|
|
50
|
+
(defn ratio->cents [s]
|
|
51
|
+
(let [[a b] (str/split (str/trim s) #"/")
|
|
52
|
+
a (js/parseFloat a) b (if b (js/parseFloat b) 1)]
|
|
53
|
+
(* 1200 (log2 (/ a b)))))
|
|
54
|
+
|
|
55
|
+
(defn edo [n period] (vec (for [k (range 1 (inc n))] (round-to (* k (/ period n)) 4))))
|
|
56
|
+
|
|
57
|
+
(def tunings
|
|
58
|
+
{"12-tet" {:name "12-tet" :cents (edo 12 1200)}
|
|
59
|
+
"24-tet" {:name "24-tet" :cents (edo 24 1200)}
|
|
60
|
+
"19-tet" {:name "19-tet" :cents (edo 19 1200)}
|
|
61
|
+
"31-tet" {:name "31-tet" :cents (edo 31 1200)}
|
|
62
|
+
"just-5" {:name "just-5"
|
|
63
|
+
:cents (mapv (fn [r] (round-to (ratio->cents r) 4))
|
|
64
|
+
["16/15" "9/8" "6/5" "5/4" "4/3" "45/32" "3/2" "8/5" "5/3" "9/5" "15/8" "2/1"])}
|
|
65
|
+
"pythagorean" {:name "pythagorean"
|
|
66
|
+
:cents (mapv (fn [r] (round-to (ratio->cents r) 4))
|
|
67
|
+
["256/243" "9/8" "32/27" "81/64" "4/3" "729/512" "3/2" "128/81" "27/16" "16/9" "243/128" "2/1"])}
|
|
68
|
+
"bohlen-pierce" {:name "bohlen-pierce" :cents (edo 13 1901.955)}})
|
|
69
|
+
|
|
70
|
+
(def default-ref 261.6256)
|
|
71
|
+
|
|
72
|
+
(defn parse-scl
|
|
73
|
+
"Text of a Scala .scl file → {:name :cents}. Lines starting with ! are comments;
|
|
74
|
+
the first useful line is the description, the second the number of degrees; a value with a dot
|
|
75
|
+
is in cents and one without a dot is a ratio (3/2) or an integer (2)."
|
|
76
|
+
[txt]
|
|
77
|
+
(let [ls (remove (fn [l] (str/starts-with? (str/trim l) "!")) (str/split-lines (str txt)))
|
|
78
|
+
[desc nl & more] ls
|
|
79
|
+
n (js/parseInt (str/trim (str nl)) 10)
|
|
80
|
+
vals (take n (keep (fn [l] (let [w (first (str/split (str/trim l) #"\s+"))] (when (seq w) w))) more))
|
|
81
|
+
cents (mapv (fn [v] (if (str/includes? v ".") (js/parseFloat v) (ratio->cents v))) vals)]
|
|
82
|
+
(when (or (js/isNaN n) (zero? n) (not= n (count cents)) (some (fn [c] (not (num? c))) cents) (not (pos? (peek cents))))
|
|
83
|
+
(throw (js/Error. "Not a valid .scl: it needs a description, the number of degrees and the degrees (the last one is the period).")))
|
|
84
|
+
{:name (if (str/blank? desc) "scala" (str/trim desc)) :cents (mapv (fn [c] (round-to c 4)) cents)}))
|
|
85
|
+
|
|
86
|
+
(defn degrees [gy] (vec (cons 0 (butlast (:cents gy)))))
|
|
87
|
+
(defn period [gy] (peek (:cents gy)))
|
|
88
|
+
|
|
89
|
+
(defn hz->degree
|
|
90
|
+
"Index of the nearest degree (0 = the reference; it may be negative or go beyond the period)."
|
|
91
|
+
[gy hz]
|
|
92
|
+
(let [gs (degrees gy) m (count gs) P (period gy)
|
|
93
|
+
c (hz->cents hz (:ref gy))
|
|
94
|
+
k (js/Math.floor (/ c P))
|
|
95
|
+
r (- c (* k P))
|
|
96
|
+
cands (conj gs P)
|
|
97
|
+
i (apply min-key (fn [i] (js/Math.abs (- (nth cands i) r))) (range (count cands)))]
|
|
98
|
+
(+ (* k m) i)))
|
|
99
|
+
|
|
100
|
+
(defn degree->hz [gy n]
|
|
101
|
+
(let [gs (degrees gy) m (count gs)
|
|
102
|
+
k (js/Math.floor (/ n m))
|
|
103
|
+
i (- n (* k m))]
|
|
104
|
+
(cents->hz (+ (* k (period gy)) (nth gs i)) (:ref gy))))
|
|
105
|
+
|
|
106
|
+
(defn quantize-f
|
|
107
|
+
"A value on the Y grid: the nearest degree of a tuning, or the nearest multiple of a regular step."
|
|
108
|
+
[gy v]
|
|
109
|
+
(cond (nil? gy) v
|
|
110
|
+
(:cents gy) (degree->hz gy (hz->degree gy v))
|
|
111
|
+
(:step gy) (round-to (* (:step gy) (js/Math.round (/ v (:step gy)))) 6)
|
|
112
|
+
:else v))
|
|
113
|
+
|
|
114
|
+
;; ───────── X grid ─────────
|
|
115
|
+
|
|
116
|
+
(defn cycle [gx] (reduce + (or (seq (:cycles gx)) [4])))
|
|
117
|
+
(defn step-x [gx] (or (:step gx) 0.25))
|
|
118
|
+
(defn quantize-t [gx t] (if gx (let [p (:step gx)] (round-to (* p (js/Math.round (/ t p))) 6)) t))
|
|
119
|
+
|
|
120
|
+
(defn parse-cycles
|
|
121
|
+
"[4], 4, \"3+3+2\" or [3 3 2] → vector of group durations (in time units)."
|
|
122
|
+
[x]
|
|
123
|
+
(cond (num? x) [x]
|
|
124
|
+
(string? x) (vec (keep (fn [s] (let [n (js/parseFloat s)] (when (num? n) n))) (str/split x #"\+")))
|
|
125
|
+
(sequential? x) (vec (filter num? x))
|
|
126
|
+
:else []))
|
|
127
|
+
|
|
128
|
+
;; ───────── model ─────────
|
|
129
|
+
|
|
130
|
+
(defn sane-pts [pts]
|
|
131
|
+
(when (sequential? pts)
|
|
132
|
+
(let [ps (->> pts
|
|
133
|
+
(filter (fn [p] (and (sequential? p) (= 2 (count p)) (num? (first p)) (num? (second p))
|
|
134
|
+
(>= (first p) 0) (< 1 (second p) 30000))))
|
|
135
|
+
(map (fn [[t f]] [(round-to t 4) (round-to f 3)]))
|
|
136
|
+
(sort-by first)
|
|
137
|
+
vec)]
|
|
138
|
+
(when (and (>= (count ps) 2) (> (first (peek ps)) (first (first ps)))) ps))))
|
|
139
|
+
|
|
140
|
+
(defn sane-gx [gx]
|
|
141
|
+
(when (map? gx)
|
|
142
|
+
(let [p (:step gx) cs (filter (fn [c] (and (num? c) (pos? c))) (parse-cycles (:cycles gx)))]
|
|
143
|
+
(when (and (num? p) (> p 0.001))
|
|
144
|
+
{:step p :cycles (vec cs)}))))
|
|
145
|
+
|
|
146
|
+
(defn sane-gy [gy]
|
|
147
|
+
(when (map? gy)
|
|
148
|
+
(if (and (nil? (:cents gy)) (num? (:step gy)) (pos? (:step gy)))
|
|
149
|
+
{:step (:step gy)}
|
|
150
|
+
(let [cs (:cents gy) ref (:ref gy)]
|
|
151
|
+
(when (and (sequential? cs) (seq cs) (every? num? cs) (pos? (peek (vec cs))) (< (count cs) 400))
|
|
152
|
+
{:name (str (or (:name gy) "scale")) :cents (vec cs) :ref (if (and (num? ref) (< 1 ref 20000)) ref default-ref)})))))
|
|
153
|
+
|
|
154
|
+
(def quant-keys [:meter :groups :key :divs :merge :nest :grace :legato :chord :voices :overlap :tempo :w :fix :spell :time :meters])
|
|
155
|
+
|
|
156
|
+
(defn sane-quant
|
|
157
|
+
"How a freqt's score is quantized, as kept in its buffer: only the keys kairotic.quantize reads (their
|
|
158
|
+
values are checked there), nil when there are none."
|
|
159
|
+
[q]
|
|
160
|
+
(when (map? q) (not-empty (into {} (filter (fn [[k v]] (some? v)) (select-keys q quant-keys))))))
|
|
161
|
+
|
|
162
|
+
(defn sane-axis [a dflt]
|
|
163
|
+
(let [a (if (map? a) a {}) lo (:lo a) hi (:hi a) log? (boolean (:log a))]
|
|
164
|
+
(if (and (num? lo) (num? hi) (< lo hi) (or (not log?) (pos? lo)))
|
|
165
|
+
{:lo lo :hi hi :log log?}
|
|
166
|
+
dflt)))
|
|
167
|
+
|
|
168
|
+
(defn sane-ax [ax]
|
|
169
|
+
(when (map? ax)
|
|
170
|
+
{:x (sane-axis (:x ax) (:x default-ax)) :y (sane-axis (:y ax) (:y default-ax))}))
|
|
171
|
+
|
|
172
|
+
(defn x-log? [d] (and (generic? d) (boolean (get-in d [:ax :x :log]))))
|
|
173
|
+
(defn y-log? [d] (if (generic? d) (boolean (get-in d [:ax :y :log])) true))
|
|
174
|
+
|
|
175
|
+
(defn sane-free-pts
|
|
176
|
+
"Points of a stroke of a generic canvas: any direction, at least two different points."
|
|
177
|
+
[d pts]
|
|
178
|
+
(when (sequential? pts)
|
|
179
|
+
(let [ps (->> pts
|
|
180
|
+
(filter (fn [p] (and (sequential? p) (= 2 (count p)) (num? (first p)) (num? (second p))
|
|
181
|
+
(or (not (x-log? d)) (pos? (first p)))
|
|
182
|
+
(or (not (y-log? d)) (pos? (second p))))))
|
|
183
|
+
(map (fn [[x y]] [(round-to x 4) (round-to y 4)]))
|
|
184
|
+
dedupe
|
|
185
|
+
vec)]
|
|
186
|
+
(when (>= (count ps) 2) ps))))
|
|
187
|
+
|
|
188
|
+
(defn sane-pts-for "The right points check for this canvas." [d pts]
|
|
189
|
+
(if (generic? d) (sane-free-pts d pts) (sane-pts pts)))
|
|
190
|
+
|
|
191
|
+
;; How a stroke looks, beyond its points (what a render of a pattern draws, discussion #93):
|
|
192
|
+
;; {:w 1.5} its weight, times the usual (0.25…4): an event's :amp
|
|
193
|
+
;; {:pan -0.6} a vertical gradient across it, from its colour to the paper (−1…1): an event's :pan
|
|
194
|
+
;; {:tex :dash} :dash or :dot, a texture {:tone :kairos} :kairos, :ink or :soft, a colour
|
|
195
|
+
;; Plain data, kept in the history and in links like the points; a stroke without it looks as always.
|
|
196
|
+
(def look-texes #{:dash :dot})
|
|
197
|
+
(def look-tones #{:kairos :ink :soft})
|
|
198
|
+
|
|
199
|
+
(defn sane-look
|
|
200
|
+
"The look of a stroke, with only what can be drawn; nil when there is nothing to draw differently."
|
|
201
|
+
[l]
|
|
202
|
+
(when (map? l)
|
|
203
|
+
(let [w (:w l) pan (:pan l)
|
|
204
|
+
r (cond-> {}
|
|
205
|
+
(and (num? w) (not (== w 1))) (assoc :w (round-to (max 0.25 (min 4 w)) 3))
|
|
206
|
+
(and (num? pan) (not (zero? pan))) (assoc :pan (round-to (max -1 (min 1 pan)) 3))
|
|
207
|
+
(look-texes (:tex l)) (assoc :tex (:tex l))
|
|
208
|
+
(look-tones (:tone l)) (assoc :tone (:tone l)))]
|
|
209
|
+
(when (seq r) r))))
|
|
210
|
+
|
|
211
|
+
(defn with-look "A stroke {:pts …} with look l, if it has one." [s l] (if-let [l (sane-look l)] (assoc s :look l) s))
|
|
212
|
+
|
|
213
|
+
(defn shift-y
|
|
214
|
+
"A value moved by dy: in cents on a log axis, added on a linear one."
|
|
215
|
+
[d v dy] (if (y-log? d) (* v (js/Math.pow 2 (/ dy 1200))) (+ v dy)))
|
|
216
|
+
|
|
217
|
+
(defn delta-y
|
|
218
|
+
"How far b is from a, in the units of shift-y."
|
|
219
|
+
[d b a] (if (y-log? d) (hz->cents b a) (- b a)))
|
|
220
|
+
|
|
221
|
+
(defn transform [d pts f] (sane-pts-for d (mapv f pts)))
|
|
222
|
+
|
|
223
|
+
(defn step
|
|
224
|
+
"Data + event → data. Every change to a canvas goes through here."
|
|
225
|
+
[d [op & args]]
|
|
226
|
+
(let [d (or d empty)]
|
|
227
|
+
(case op
|
|
228
|
+
:stroke (let [[id pts look] args ps (sane-pts-for d pts)]
|
|
229
|
+
(if (and (keyword? id) ps) (assoc-in d [:strokes id] (with-look {:pts ps} look)) d))
|
|
230
|
+
;; [:move id dx dy]: dy in cents on a log axis (freqt), in units on a linear one
|
|
231
|
+
:move (let [[id dt dc] args pts (get-in d [:strokes id :pts])]
|
|
232
|
+
(if (and pts (num? dt) (num? dc))
|
|
233
|
+
(let [dt (if (generic? d) dt (max dt (- (ffirst pts))))]
|
|
234
|
+
(if-let [ps (transform d pts (fn [[t f]] [(+ t dt) (shift-y d f dc)]))] (assoc-in d [:strokes id :pts] ps) d))
|
|
235
|
+
d))
|
|
236
|
+
:stretch (let [[id k] args pts (get-in d [:strokes id :pts])]
|
|
237
|
+
(if (and pts (num? k) (pos? k))
|
|
238
|
+
(let [t0 (ffirst pts)]
|
|
239
|
+
(if-let [ps (transform d pts (fn [[t f]] [(+ t0 (* k (- t t0))) f]))] (assoc-in d [:strokes id :pts] ps) d))
|
|
240
|
+
d))
|
|
241
|
+
:erase (update d :strokes dissoc (first args))
|
|
242
|
+
;; several edits as one event (what a message on the inlet of a canvas makes, issue #26)
|
|
243
|
+
:batch (reduce step d (filter vector? (first args)))
|
|
244
|
+
:clear (assoc d :strokes {})
|
|
245
|
+
:grid-x (assoc d :gx (sane-gx (first args)))
|
|
246
|
+
:tuning (assoc d :gy (sane-gy (first args)))
|
|
247
|
+
:grid-y (assoc d :gy (sane-gy (first args)))
|
|
248
|
+
:quantize (if-let [q (sane-quant (first args))] (assoc d :quant q) (dissoc d :quant))
|
|
249
|
+
:axis (if (generic? d) (assoc d :ax (sane-ax (first args))) d)
|
|
250
|
+
:load (let [[x] args]
|
|
251
|
+
(if (map? x)
|
|
252
|
+
(let [base (cond-> {:gx (sane-gx (:gx x)) :gy (sane-gy (:gy x))}
|
|
253
|
+
(or (generic? d) (map? (:ax x))) (assoc :ax (or (sane-ax (:ax x)) default-ax))
|
|
254
|
+
(sane-quant (:quant x)) (assoc :quant (sane-quant (:quant x))))]
|
|
255
|
+
(assoc base :strokes (into {} (for [[k s] (:strokes x)
|
|
256
|
+
:let [ps (and (keyword? k) (map? s) (sane-pts-for base (:pts s)))] :when ps]
|
|
257
|
+
[k (with-look {:pts ps} (:look s))]))))
|
|
258
|
+
d))
|
|
259
|
+
d)))
|
|
260
|
+
|
|
261
|
+
(defn id-num [k] (let [m (re-matches #"t(\d+)" (name k))] (if m (js/parseInt (second m) 10) 1e9)))
|
|
262
|
+
|
|
263
|
+
(defn next-id [d]
|
|
264
|
+
(let [ns-ids (keep (fn [k] (let [n (id-num k)] (when (< n 1e9) n))) (keys (:strokes d)))]
|
|
265
|
+
(keyword (str "t" (inc (if (seq ns-ids) (apply max ns-ids) 0))))))
|
|
266
|
+
|
|
267
|
+
(defn sorted-strokes [d] (sort-by (fn [[id s]] [(ffirst (:pts s)) (id-num id)]) (:strokes d)))
|
|
268
|
+
|
|
269
|
+
(defn clip-out
|
|
270
|
+
"The pieces of polyline pts that lie outside the open window t1 < x < t2 (t2 nil: no end), each a vector
|
|
271
|
+
of points, in order. A segment that crosses an edge is cut there, its value interpolated on the scale
|
|
272
|
+
of the Y axis (geometric when ylog, as pitch)."
|
|
273
|
+
[pts t1 t2 ylog]
|
|
274
|
+
(let [inside? (fn [x] (and (> x t1) (or (nil? t2) (< x t2))))
|
|
275
|
+
at (fn [[xa ya] [xb yb] u]
|
|
276
|
+
[(+ xa (* u (- xb xa)))
|
|
277
|
+
(if (and ylog (pos? ya) (pos? yb)) (* ya (js/Math.pow (/ yb ya) u)) (+ ya (* u (- yb ya))))])
|
|
278
|
+
;; the params (0…1) where segment a→b crosses x = t1 or x = t2
|
|
279
|
+
cuts (fn [[xa :as a] [xb :as b]]
|
|
280
|
+
(if (= xa xb)
|
|
281
|
+
[]
|
|
282
|
+
(sort (filter (fn [u] (< 0 u 1)) (map (fn [t] (/ (- t xa) (- xb xa))) (remove nil? [t1 t2]))))))
|
|
283
|
+
close (fn [acc cur] (if (>= (count cur) 2) (conj acc cur) acc))]
|
|
284
|
+
(loop [[a b & more] pts acc [] cur (if (and (seq pts) (not (inside? (ffirst pts)))) [(first pts)] [])]
|
|
285
|
+
(if (nil? b)
|
|
286
|
+
(close acc cur)
|
|
287
|
+
(let [us (concat [0] (cuts a b) [1])
|
|
288
|
+
;; each piece of the segment, with whether its middle is outside the window
|
|
289
|
+
[acc cur] (reduce (fn [[acc cur] [u0 u1]]
|
|
290
|
+
(let [p0 (at a b u0) p1 (at a b u1)
|
|
291
|
+
out (not (inside? (first (at a b (/ (+ u0 u1) 2)))))]
|
|
292
|
+
(if out
|
|
293
|
+
[acc (conj (if (seq cur) cur [p0]) p1)]
|
|
294
|
+
[(close acc cur) []])))
|
|
295
|
+
[acc cur] (partition 2 1 us))]
|
|
296
|
+
(recur (cons b more) acc cur))))))
|
|
297
|
+
|
|
298
|
+
(defn cut-events
|
|
299
|
+
"The events that remove what buffer d holds between times (x) t1 and t2 (t2 nil: to the end): strokes
|
|
300
|
+
inside are erased, those that cross an edge end or begin there, and one that spans the window is split
|
|
301
|
+
in two (the part after it is a new stroke). What a take that records over a buffer writes (rec t, ovr)."
|
|
302
|
+
([d t] (cut-events d t nil))
|
|
303
|
+
([d t1 t2]
|
|
304
|
+
(let [ylog (y-log? d)
|
|
305
|
+
valid (fn [ps] (and (>= (count ps) 2) (or (generic? d) (> (first (peek ps)) (first (first ps))))))]
|
|
306
|
+
(first
|
|
307
|
+
(reduce (fn [[evs d] [id s]]
|
|
308
|
+
(let [pts (:pts s)
|
|
309
|
+
touched (some (fn [[x]] (and (> x t1) (or (nil? t2) (< x t2)))) pts)
|
|
310
|
+
crosses (some (fn [[[xa] [xb]]] (< (min xa xb) t1 (max xa xb))) (partition 2 1 pts))
|
|
311
|
+
crosses2 (and t2 (some (fn [[[xa] [xb]]] (< (min xa xb) t2 (max xa xb))) (partition 2 1 pts)))]
|
|
312
|
+
(if-not (or touched crosses crosses2)
|
|
313
|
+
[evs d]
|
|
314
|
+
(let [pieces (filter valid (map (fn [ps] (mapv (fn [[x y]] [(round-to x 4) (round-to y 4)]) ps))
|
|
315
|
+
(clip-out pts t1 t2 ylog)))
|
|
316
|
+
es (if (empty? pieces)
|
|
317
|
+
[[:erase id]]
|
|
318
|
+
(into [[:stroke id (first pieces)]]
|
|
319
|
+
(second (reduce (fn [[d es] ps] (let [nid (next-id d) e [:stroke nid ps]] [(step d e) (conj es e)]))
|
|
320
|
+
[(step d [:stroke id (first pieces)]) []] (rest pieces)))))]
|
|
321
|
+
[(into evs es) (reduce step d es)]))))
|
|
322
|
+
[[] d] (sorted-strokes d))))))
|
|
323
|
+
|
|
324
|
+
(defn end-t [d] (reduce max 0 (map (fn [s] (apply max (map first (:pts s)))) (vals (:strokes d)))))
|
|
325
|
+
|
|
326
|
+
;; ───────── reading ─────────
|
|
327
|
+
;; Reading at x = v (or y = v) gives, for each stroke, the other coordinate wherever the stroke
|
|
328
|
+
;; crosses that line. A segment counts from its start up to, not including, its end, so a stroke
|
|
329
|
+
;; going forward has an open end (as before) and a turning point is counted once.
|
|
330
|
+
|
|
331
|
+
(defn axis-u [log? v] (if log? (log2 v) v))
|
|
332
|
+
(defn axis-v [log? u] (if log? (js/Math.pow 2 u) u))
|
|
333
|
+
|
|
334
|
+
(defn crossings
|
|
335
|
+
"Values of the other coordinate where the polyline pts crosses coordinate k (0 = x, 1 = y) = v."
|
|
336
|
+
[pts k v logs]
|
|
337
|
+
(let [o (- 1 k) lk (nth logs k) lo (nth logs o)
|
|
338
|
+
uv (axis-u lk v)]
|
|
339
|
+
(vec (keep (fn [[a b]]
|
|
340
|
+
(let [ca (axis-u lk (nth a k)) cb (axis-u lk (nth b k))]
|
|
341
|
+
(when (or (and (< ca cb) (<= ca uv) (< uv cb))
|
|
342
|
+
(and (> ca cb) (< cb uv) (<= uv ca)))
|
|
343
|
+
(let [r (/ (- uv ca) (- cb ca))
|
|
344
|
+
oa (axis-u lo (nth a o)) ob (axis-u lo (nth b o))]
|
|
345
|
+
(axis-v lo (+ oa (* r (- ob oa))))))))
|
|
346
|
+
(partition 2 1 pts)))))
|
|
347
|
+
|
|
348
|
+
(defn read-along
|
|
349
|
+
"Everything a canvas holds at x = v (axis :x) or y = v (axis :y): [[id value] ...] in stroke order.
|
|
350
|
+
A stroke that crosses more than once gives :t1, :t1-2, :t1-3 …"
|
|
351
|
+
[d axis v]
|
|
352
|
+
(let [logs [(x-log? d) (y-log? d)] k (if (= axis :y) 1 0)]
|
|
353
|
+
(vec (mapcat (fn [[id s]]
|
|
354
|
+
(map-indexed (fn [i val] [(if (zero? i) id (keyword (str (name id) "-" (inc i)))) val])
|
|
355
|
+
(crossings (:pts s) k v logs)))
|
|
356
|
+
(sorted-strokes d)))))
|
|
357
|
+
|
|
358
|
+
(defn read-at "Everything that sounds at time t (reading along X)." [d t] (read-along d :x t))
|
|
359
|
+
|
|
360
|
+
;; ───────── trajectories ─────────
|
|
361
|
+
;; A stroke can also be read as a path: position u from 0 to 1 along it, in the order it was drawn,
|
|
362
|
+
;; proportionally to its length as it is seen (each axis on its own scale, both of the same size).
|
|
363
|
+
|
|
364
|
+
(declare ranges)
|
|
365
|
+
|
|
366
|
+
(defn stroke-for
|
|
367
|
+
"The points of stroke `sid` (a keyword), or of the first stroke if sid is nil or not there."
|
|
368
|
+
[d sid]
|
|
369
|
+
(or (when sid (get-in d [:strokes sid :pts]))
|
|
370
|
+
(:pts (second (first (sorted-strokes d))))))
|
|
371
|
+
|
|
372
|
+
(defn point-along
|
|
373
|
+
"The point [x y] at fraction u (0…1) of the length of a path."
|
|
374
|
+
[d pts u]
|
|
375
|
+
(when (>= (count pts) 2)
|
|
376
|
+
(let [{:keys [xa xb ya yb xlog ylog]} (ranges d)
|
|
377
|
+
nx (fn [x] (/ (- (axis-u xlog x) (axis-u xlog xa)) (- (axis-u xlog xb) (axis-u xlog xa))))
|
|
378
|
+
ny (fn [y] (/ (- (axis-u ylog y) (axis-u ylog ya)) (- (axis-u ylog yb) (axis-u ylog ya))))
|
|
379
|
+
segs (map (fn [[a b]] [a b (js/Math.hypot (- (nx (first b)) (nx (first a))) (- (ny (second b)) (ny (second a))))])
|
|
380
|
+
(partition 2 1 pts))
|
|
381
|
+
total (reduce + (map #(nth % 2) segs))
|
|
382
|
+
target (* (max 0 (min 1 u)) total)]
|
|
383
|
+
(if (zero? total)
|
|
384
|
+
(first pts)
|
|
385
|
+
(loop [[[a b len] & more] segs acc 0]
|
|
386
|
+
(if (or (empty? more) (<= target (+ acc len)))
|
|
387
|
+
(let [r (if (pos? len) (max 0 (min 1 (/ (- target acc) len))) 0)
|
|
388
|
+
lerp (fn [log? p q] (axis-v log? (+ (axis-u log? p) (* r (- (axis-u log? q) (axis-u log? p))))))]
|
|
389
|
+
[(lerp xlog (first a) (first b)) (lerp ylog (second a) (second b))])
|
|
390
|
+
(recur more (+ acc len))))))))
|
|
391
|
+
|
|
392
|
+
(defn x-range
|
|
393
|
+
"The span read along X by a position from 0 to 1: the X axis of a canvas, or 0 … end of a freqt."
|
|
394
|
+
[d]
|
|
395
|
+
(if (generic? d)
|
|
396
|
+
(let [{:keys [lo hi log]} (get-in d [:ax :x])] [lo hi (boolean log)])
|
|
397
|
+
[0 (max 1e-6 (end-t d)) false]))
|
|
398
|
+
|
|
399
|
+
(defn y-range
|
|
400
|
+
"The span read along Y: the Y axis of a canvas, or the lowest … highest frequency of a freqt."
|
|
401
|
+
[d]
|
|
402
|
+
(if (generic? d)
|
|
403
|
+
(let [{:keys [lo hi log]} (get-in d [:ax :y])] [lo hi (boolean log)])
|
|
404
|
+
(let [fs (map second (mapcat :pts (vals (:strokes d))))]
|
|
405
|
+
(if (seq fs) [(apply min fs) (max (+ (apply min fs) 1e-6) (apply max fs)) true] [100 1000 true]))))
|
|
406
|
+
|
|
407
|
+
(defn at-unit
|
|
408
|
+
"The value at fraction u of a span [lo hi log?]."
|
|
409
|
+
[[lo hi log?] u] (axis-v log? (+ (axis-u log? lo) (* u (- (axis-u log? hi) (axis-u log? lo))))))
|
|
410
|
+
|
|
411
|
+
(defn value-at
|
|
412
|
+
"Value of a stroke at x = t, or nil if it does not pass there (the end is open)."
|
|
413
|
+
[pts t] (first (crossings pts 0 t [false true])))
|
|
414
|
+
|
|
415
|
+
;; ───────── geometry and drawing ─────────
|
|
416
|
+
|
|
417
|
+
(def W 420)
|
|
418
|
+
(def H 200)
|
|
419
|
+
(def x0 40)
|
|
420
|
+
|
|
421
|
+
(defn f-range [d]
|
|
422
|
+
(let [ps (mapcat :pts (vals (:strokes d)))
|
|
423
|
+
L (cycle (:gx d))
|
|
424
|
+
T0 (if (seq ps) (apply max (map first ps)) 0)
|
|
425
|
+
T (* L (max 2 (inc (js/Math.ceil (/ T0 L)))))
|
|
426
|
+
fs (map second ps)
|
|
427
|
+
[lo hi] (if (seq fs) [(apply min fs) (apply max fs)] [220 880])
|
|
428
|
+
clo (- (log2 lo) 0.3) chi (+ (log2 hi) 0.3)
|
|
429
|
+
span (max 1.6 (- chi clo)) mid (/ (+ clo chi) 2)]
|
|
430
|
+
{:T T :lf0 (- mid (/ span 2)) :lf1 (+ mid (/ span 2))}))
|
|
431
|
+
|
|
432
|
+
(defn ranges
|
|
433
|
+
"What the box shows: x from xa to xb and y from ya to yb, each on its scale."
|
|
434
|
+
[d]
|
|
435
|
+
(if (generic? d)
|
|
436
|
+
(let [{:keys [x y]} (:ax d)]
|
|
437
|
+
{:xa (:lo x) :xb (:hi x) :xlog (:log x) :ya (:lo y) :yb (:hi y) :ylog (:log y) :T (:hi x)})
|
|
438
|
+
(let [{:keys [T lf0 lf1]} (f-range d)]
|
|
439
|
+
{:xa 0 :xb T :xlog false :ya (js/Math.pow 2 lf0) :yb (js/Math.pow 2 lf1) :ylog true :T T :lf0 lf0 :lf1 lf1})))
|
|
440
|
+
|
|
441
|
+
(defn geom
|
|
442
|
+
"Where a canvas box draws its data: in a box of W × H, or of the size it was given (issue #40)."
|
|
443
|
+
([d hdr] (geom d hdr nil))
|
|
444
|
+
([d hdr [bw bh]]
|
|
445
|
+
(let [y0 (+ hdr 3) bw (or bw W) bh (or bh H)]
|
|
446
|
+
(merge (ranges d) {:x0 x0 :y0 y0 :w (- bw x0 6) :h (- bh y0 6)}))))
|
|
447
|
+
|
|
448
|
+
(defn t->x [g t]
|
|
449
|
+
(let [a (axis-u (:xlog g) (:xa g)) b (axis-u (:xlog g) (:xb g))]
|
|
450
|
+
(+ (:x0 g) (* (/ (- (axis-u (:xlog g) t) a) (- b a)) (:w g)))))
|
|
451
|
+
(defn x->t [g x]
|
|
452
|
+
(let [a (axis-u (:xlog g) (:xa g)) b (axis-u (:xlog g) (:xb g))]
|
|
453
|
+
(axis-v (:xlog g) (+ a (* (/ (- x (:x0 g)) (:w g)) (- b a))))))
|
|
454
|
+
(defn f->y [g f]
|
|
455
|
+
(let [a (axis-u (:ylog g) (:ya g)) b (axis-u (:ylog g) (:yb g))]
|
|
456
|
+
(+ (:y0 g) (* (/ (- b (axis-u (:ylog g) f)) (- b a)) (:h g)))))
|
|
457
|
+
(defn y->f [g y]
|
|
458
|
+
(let [a (axis-u (:ylog g) (:ya g)) b (axis-u (:ylog g) (:yb g))]
|
|
459
|
+
(axis-v (:ylog g) (- b (* (/ (- y (:y0 g)) (:h g)) (- b a))))))
|
|
460
|
+
|
|
461
|
+
(defn nice-step
|
|
462
|
+
"A round step (1, 2 or 5 × 10^k) that cuts span into about n parts."
|
|
463
|
+
[span n]
|
|
464
|
+
(let [raw (/ span (max 1 n)) k (js/Math.pow 10 (js/Math.floor (/ (js/Math.log raw) (js/Math.log 10))))
|
|
465
|
+
m (/ raw k)]
|
|
466
|
+
(* k (cond (< m 1.5) 1 (< m 3.5) 2 (< m 7.5) 5 :else 10))))
|
|
467
|
+
|
|
468
|
+
(defn ticks
|
|
469
|
+
"[[v level label]] every nice step between a and b."
|
|
470
|
+
[a b n]
|
|
471
|
+
(let [p (nice-step (- b a) n)]
|
|
472
|
+
(for [k (range (js/Math.ceil (/ a p)) (inc (js/Math.floor (/ b p))))
|
|
473
|
+
:let [v (round-to (* k p) 6)]]
|
|
474
|
+
[v (if (zero? v) 1 0) (fmt v)])))
|
|
475
|
+
|
|
476
|
+
(defn bar-lines
|
|
477
|
+
"[[t level label]] of bars given one by one (a tree whose measures change meter): [{:s start :L length
|
|
478
|
+
:groups [lengths]}]. Past the last one, it goes on in its meter."
|
|
479
|
+
[bars a T]
|
|
480
|
+
(let [lst (last bars)
|
|
481
|
+
more (when (pos? (:L lst))
|
|
482
|
+
(map (fn [i] (assoc lst :s (+ (:s lst) (* (:L lst) i))))
|
|
483
|
+
(range 1 (inc (max 0 (js/Math.ceil (/ (- T (:s lst)) (:L lst))))))))]
|
|
484
|
+
(for [[i {:keys [s L groups]}] (map-indexed vector (concat bars more))
|
|
485
|
+
:when (<= (- a L) s T)
|
|
486
|
+
[b level] (cons [0 2] (map (fn [b] [b 1]) (butlast (rest (reductions + 0 (or (seq groups) [L]))))))
|
|
487
|
+
:let [t (+ s b)] :when (<= a t T)]
|
|
488
|
+
[t level (when (= level 2) (inc i))])))
|
|
489
|
+
|
|
490
|
+
(defn lines-x
|
|
491
|
+
"[[t level label]]: level 0 = step, 1 = group, 2 = start of cycle."
|
|
492
|
+
[gx g]
|
|
493
|
+
(let [a (:xa g) T (:xb g) px-per-unit (/ (:w g) (- T a))]
|
|
494
|
+
(cond
|
|
495
|
+
(and (not gx) (or (:xlog g) (not= a 0) (not (:lf0 g))))
|
|
496
|
+
(if (:xlog g)
|
|
497
|
+
(for [k (range (js/Math.ceil (log2 a)) (inc (js/Math.floor (log2 T)))) :let [v (js/Math.pow 2 k)]] [v 1 (fmt v)])
|
|
498
|
+
(ticks a T 8))
|
|
499
|
+
(not gx)
|
|
500
|
+
(for [t (range 0 (inc T))] [t (if (zero? (mod t 4)) 1 0) nil])
|
|
501
|
+
:else
|
|
502
|
+
(let [p (:step gx) cs (or (seq (:cycles gx)) [4]) L (reduce + cs)
|
|
503
|
+
bounds (butlast (reductions + 0 cs))
|
|
504
|
+
steps (when (>= (* p px-per-unit) 5)
|
|
505
|
+
(for [k (range (js/Math.ceil (/ a p)) (inc (js/Math.floor (/ T p))))] [(* k p) 0 nil]))]
|
|
506
|
+
(concat steps
|
|
507
|
+
(if-let [bars (seq (:bars gx))]
|
|
508
|
+
(bar-lines bars a T)
|
|
509
|
+
(for [c (range (js/Math.floor (/ a L)) (inc (js/Math.floor (/ T L)))) b bounds
|
|
510
|
+
:let [t (+ (* c L) b)] :when (<= a t T)]
|
|
511
|
+
[t (if (zero? b) 2 1) (when (zero? b) (inc c))])))))))
|
|
512
|
+
|
|
513
|
+
(defn lines-y
|
|
514
|
+
"[[hz level label]] within the visible range."
|
|
515
|
+
[gy g]
|
|
516
|
+
(let [f0 (:ya g) f1 (:yb g)]
|
|
517
|
+
(cond
|
|
518
|
+
(and (:step gy) (not (:cents gy)))
|
|
519
|
+
(let [p (:step gy) n (/ (- f1 f0) p) every (max 1 (js/Math.ceil (/ n (/ (:h g) 14))))]
|
|
520
|
+
(for [k (range (js/Math.ceil (/ f0 p)) (inc (js/Math.floor (/ f1 p))))
|
|
521
|
+
:let [v (round-to (* k p) 6)]]
|
|
522
|
+
[v (if (zero? (mod k every)) 1 0) (when (zero? (mod k every)) (fmt v))]))
|
|
523
|
+
(and (not gy) (not (:ylog g)))
|
|
524
|
+
(ticks f0 f1 6)
|
|
525
|
+
(not gy)
|
|
526
|
+
(for [k (range (js/Math.ceil (log2 (/ f0 55))) (inc (js/Math.floor (log2 (/ f1 55)))))
|
|
527
|
+
:let [f (* 55 (js/Math.pow 2 k))]]
|
|
528
|
+
[f 1 (str (round-to f 1))])
|
|
529
|
+
:else
|
|
530
|
+
(let [m (count (degrees gy)) a (hz->degree gy f0) b (hz->degree gy f1)
|
|
531
|
+
dense? (> (/ (- b a) (max 1 (/ (:h g) 3.5))) 1)
|
|
532
|
+
tet12? (= (:cents gy) (:cents (get tunings "12-tet")))]
|
|
533
|
+
(for [n (range (dec a) (+ b 2))
|
|
534
|
+
:let [f (degree->hz gy n) start? (zero? (mod n m))]
|
|
535
|
+
:when (and (<= f0 f f1) (or start? (not dense?)))]
|
|
536
|
+
[f (if start? 1 0)
|
|
537
|
+
(when start?
|
|
538
|
+
(if (and tet12? (< (js/Math.abs (- (:ref gy) default-ref)) 0.01))
|
|
539
|
+
(midi-name (js/Math.round (hz->midi f)))
|
|
540
|
+
(str (round-to f 1))))])))))
|
|
541
|
+
|
|
542
|
+
(defn path-d [g pts]
|
|
543
|
+
(str/join " " (map-indexed (fn [i [t f]] (str (if (zero? i) "M" "L") (round-to (t->x g t) 2) " " (round-to (f->y g f) 2))) pts)))
|
|
544
|
+
|
|
545
|
+
(defn look-path
|
|
546
|
+
"The drawn path of a stroke, with its look (weight, gradient, texture, colour; discussion #93)."
|
|
547
|
+
[idn ids g pts dd look sel?]
|
|
548
|
+
(let [{:keys [w pan tex tone]} look
|
|
549
|
+
width (* 3 (or w 1))
|
|
550
|
+
colour (if sel? "var(--kairos)" (case tone :kairos "var(--kairos)" :ink "var(--ink)" :soft "var(--ink-soft)" "var(--accent)"))
|
|
551
|
+
gid (str "lk-" idn "-" ids)
|
|
552
|
+
grad (when (and pan (not sel?))
|
|
553
|
+
(let [ys (map (fn [[_ f]] (f->y g f)) pts)
|
|
554
|
+
y0 (- (apply min ys) (/ width 2)) y1 (+ (apply max ys) (/ width 2))
|
|
555
|
+
faded (str "color-mix(in srgb, " colour ", var(--paper) " (js/Math.round (* 75 (js/Math.abs pan))) "%)")
|
|
556
|
+
[top bottom] (if (neg? pan) [colour faded] [faded colour])]
|
|
557
|
+
(str "<defs><linearGradient id=\"" gid "\" gradientUnits=\"userSpaceOnUse\" x1=\"0\" x2=\"0\" y1=\"" (round-to y0 2)
|
|
558
|
+
"\" y2=\"" (round-to (+ y1 0.01) 2) "\"><stop offset=\"0\" style=\"stop-color:" top "\"></stop>"
|
|
559
|
+
"<stop offset=\"1\" style=\"stop-color:" bottom "\"></stop></linearGradient></defs>")))
|
|
560
|
+
style (str (when (or w (:w look)) (str "stroke-width:" (round-to (if sel? (+ width 0.5) width) 2) ";"))
|
|
561
|
+
(cond grad (str "stroke:url(#" gid ");")
|
|
562
|
+
(and tone (not sel?)) (str "stroke:" colour ";"))
|
|
563
|
+
(case tex :dash (str "stroke-dasharray:" (round-to (* 2 width) 1) " " (round-to (* 1.4 width) 1) ";")
|
|
564
|
+
:dot (str "stroke-dasharray:0.1 " (round-to (* 2 width) 1) ";") nil))]
|
|
565
|
+
(str grad "<path class=\"stroke" (when sel? " sel") "\" d=\"" dd "\"" (when (seq style) (str " style=\"" style "\"")) "></path>")))
|
|
566
|
+
|
|
567
|
+
(defn svg
|
|
568
|
+
"SVG of the contents of a canvas box. `idn` is the box id (text), `sel` the selected stroke,
|
|
569
|
+
`previews` {id pts} to draw a drag in progress and `new-pts` the points of a stroke being drawn."
|
|
570
|
+
[idn d g sel previews new-pts]
|
|
571
|
+
(let [{:keys [x0 y0 w h]} g]
|
|
572
|
+
(str "<rect class=\"lbg mus-body\" data-box=\"" idn "\" x=\"" x0 "\" y=\"" y0 "\" width=\"" w "\" height=\"" h "\"></rect>"
|
|
573
|
+
"<svg x=\"0\" y=\"" y0 "\" width=\"" (+ x0 w) "\" height=\"" h "\" overflow=\"hidden\" pointer-events=\"none\">"
|
|
574
|
+
"<g transform=\"translate(0," (- y0) ")\">"
|
|
575
|
+
(apply str (for [[f level label] (lines-y (:gy d) g) :let [y (round-to (f->y g f) 2)]]
|
|
576
|
+
(str "<line class=\"lgy" level "\" x1=\"" x0 "\" x2=\"" (+ x0 w) "\" y1=\"" y "\" y2=\"" y "\"></line>"
|
|
577
|
+
(when label (str "<text class=\"rlabel\" x=\"4\" y=\"" (+ y 3) "\">" label "</text>")))))
|
|
578
|
+
(apply str (for [[t level label] (lines-x (:gx d) g) :let [x (round-to (t->x g t) 2)]]
|
|
579
|
+
(str "<line class=\"lgx" level "\" x1=\"" x "\" x2=\"" x "\" y1=\"" y0 "\" y2=\"" (+ y0 h) "\"></line>"
|
|
580
|
+
(when label (str "<text class=\"rlabel\" x=\"" (+ x 3) "\" y=\"" (+ y0 9) "\">" label "</text>")))))
|
|
581
|
+
"</g></svg>"
|
|
582
|
+
(apply str (for [[id s] (:strokes d)
|
|
583
|
+
:let [pts (or (get previews id) (:pts s)) dd (path-d g pts) ids (name id)]]
|
|
584
|
+
(str (look-path idn ids g pts dd (:look s) (= sel id))
|
|
585
|
+
"<path class=\"stroke-hit\" data-box=\"" idn "\" data-stroke=\"" ids "\" d=\"" dd "\"><title>"
|
|
586
|
+
ids " · " (fmt (ffirst pts)) "→" (fmt (first (peek pts))) " · " (fmt (second (first pts)))
|
|
587
|
+
(when-not (generic? d) " Hz") "</title></path>")))
|
|
588
|
+
(when (and new-pts (>= (count new-pts) 2))
|
|
589
|
+
(str "<path class=\"stroke new\" d=\"" (path-d g new-pts) "\"></path>")))))
|
|
590
|
+
|
|
591
|
+
(defn summary [d]
|
|
592
|
+
(if (generic? d)
|
|
593
|
+
(let [{:keys [x y]} (:ax d) rng (fn [a] (str (fmt (:lo a)) "…" (fmt (:hi a)) (when (:log a) " log")))]
|
|
594
|
+
(str "x " (rng x) " · y " (rng y) " · " (count (:strokes d)) " strokes"))
|
|
595
|
+
(str (if-let [gy (:gy d)] (:name gy) "continuous Hz")
|
|
596
|
+
" · " (if-let [gx (:gx d)] (str "step " (fmt (:step gx)) " · cycle " (str/join "+" (map fmt (or (seq (:cycles gx)) [4])))) "free time")
|
|
597
|
+
" · " (count (:strokes d)) " strokes")))
|
|
598
|
+
|
|
599
|
+
;; ───────── projections ─────────
|
|
600
|
+
|
|
601
|
+
(defn ->code
|
|
602
|
+
"Lines of code that rebuild the canvas of box `id`."
|
|
603
|
+
[id d]
|
|
604
|
+
(concat
|
|
605
|
+
(when (and (generic? d) (not= (:ax d) default-ax))
|
|
606
|
+
(for [k [:x :y] :let [a (get-in d [:ax k])] :when (not= a (get default-ax k))]
|
|
607
|
+
(str "(axis " id " " k " " (fmt (:lo a)) " " (fmt (:hi a)) (when (:log a) " :log") ")")))
|
|
608
|
+
(when-let [gy (and (:step (:gy d)) (not (:cents (:gy d))) (:gy d))]
|
|
609
|
+
[(str "(grid-y " id " " (fmt (:step gy)) ")")])
|
|
610
|
+
(when-let [gy (and (:cents (:gy d)) (:gy d))]
|
|
611
|
+
(let [preset (some (fn [[k v]] (when (= (:cents v) (:cents gy)) k)) tunings)]
|
|
612
|
+
[(str "(tuning " id " " (if preset (pr-str preset) (pr-str {:name (:name gy) :cents (:cents gy)})) " " (fmt (:ref gy)) ")")]))
|
|
613
|
+
(when-let [gx (:gx d)]
|
|
614
|
+
[(str "(grid-x " id " " (fmt (:step gx)) " " (pr-str (mapv (fn [c] (js/parseFloat (fmt c))) (:cycles gx))) ")")])
|
|
615
|
+
(for [[sid s] (sorted-strokes d)]
|
|
616
|
+
(str "(stroke " id " " sid " [" (str/join " " (map (fn [[t f]] (str "[" (fmt t) " " (fmt f) "]")) (:pts s))) "]"
|
|
617
|
+
(when (:look s) (str " " (pr-str (:look s)))) ")"))))
|
|
618
|
+
|
|
619
|
+
;; ───────── the buffer as text (discussion #30) ─────────
|
|
620
|
+
;; Any buffer can be read and edited as EDN, in the same code window as a code box: ->source writes it,
|
|
621
|
+
;; read-source takes the text back (checking it), and diff turns the change into the events of the
|
|
622
|
+
;; buffer, so applying the text is one event of the history made of what really changed.
|
|
623
|
+
;; {:gx {:step 0.25 :cycles [4]}
|
|
624
|
+
;; :gy {:name "12-tet" :ref 261.6256} ; a tuning by name: its cents come from `tunings`
|
|
625
|
+
;; :strokes {:t1 [[0 440] [1 440]]
|
|
626
|
+
;; :t2 [[1 330] [2 330]]}}
|
|
627
|
+
;; A generic canvas also has :ax {:x {:lo 0 :hi 8 :log false} :y {…}}.
|
|
628
|
+
|
|
629
|
+
(defn- num-src [x] (if (== x (js/Math.round x)) (str (js/Math.round x)) (str x)))
|
|
630
|
+
|
|
631
|
+
(defn- pts-src [pts] (str "[" (str/join " " (map (fn [[a b]] (str "[" (num-src a) " " (num-src b) "]")) pts)) "]"))
|
|
632
|
+
|
|
633
|
+
(defn- preset-of [gy] (some (fn [[k v]] (when (= (:cents v) (:cents gy)) k)) tunings))
|
|
634
|
+
|
|
635
|
+
(defn- gy-src [gy]
|
|
636
|
+
(cond (nil? gy) "nil"
|
|
637
|
+
(:step gy) (str "{:step " (num-src (:step gy)) "}")
|
|
638
|
+
(preset-of gy) (str "{:name " (pr-str (preset-of gy)) " :ref " (num-src (:ref gy)) "}")
|
|
639
|
+
:else (str "{:name " (pr-str (:name gy)) " :ref " (num-src (:ref gy))
|
|
640
|
+
" :cents [" (str/join " " (map num-src (:cents gy))) "]}")))
|
|
641
|
+
|
|
642
|
+
(defn- gx-src [gx]
|
|
643
|
+
(if (nil? gx) "nil" (str "{:step " (num-src (:step gx)) " :cycles [" (str/join " " (map num-src (:cycles gx))) "]}")))
|
|
644
|
+
|
|
645
|
+
(defn- ax-src [ax]
|
|
646
|
+
(let [a (fn [{:keys [lo hi log]}] (str "{:lo " (num-src lo) " :hi " (num-src hi) " :log " (boolean log) "}"))]
|
|
647
|
+
(str "{:x " (a (:x ax)) "\n :y " (a (:y ax)) "}")))
|
|
648
|
+
|
|
649
|
+
(defn ->source
|
|
650
|
+
"The buffer as EDN text, one stroke per line (read-source reads it back)."
|
|
651
|
+
[d]
|
|
652
|
+
(let [d (or d empty)
|
|
653
|
+
ss (sorted-strokes d)]
|
|
654
|
+
(str "{" (when (generic? d) (str ":ax " (ax-src (:ax d)) "\n "))
|
|
655
|
+
":gx " (gx-src (:gx d)) "\n"
|
|
656
|
+
" :gy " (gy-src (:gy d)) "\n"
|
|
657
|
+
(when (:quant d) (str " :quant " (pr-str (:quant d)) "\n"))
|
|
658
|
+
" :strokes {" (str/join "\n " (map (fn [[id s]] (str id " " (if (:look s)
|
|
659
|
+
(str "{:pts " (pts-src (:pts s)) " :look " (pr-str (:look s)) "}")
|
|
660
|
+
(pts-src (:pts s)))))
|
|
661
|
+
ss)) "}}")))
|
|
662
|
+
|
|
663
|
+
(defn- bad [msg] (throw (js/Error. msg)))
|
|
664
|
+
|
|
665
|
+
(defn read-source
|
|
666
|
+
"EDN text → a buffer of the kind of `d` (a generic canvas or a freqt). Nothing is evaluated. Throws,
|
|
667
|
+
saying what is wrong, rather than dropping what it cannot read."
|
|
668
|
+
[d text]
|
|
669
|
+
(let [forms (try (edn/read-string (str "[" text "\n]")) (catch :default e (bad (str "That is not EDN: " (.-message e)))))
|
|
670
|
+
_ (when (not= 1 (count forms))
|
|
671
|
+
(bad (if (empty? forms) "The text is empty: a buffer is a map {:strokes {…} :gx nil :gy nil}"
|
|
672
|
+
(str "One map only: there is more after it (" (pr-str (second forms)) ")"))))
|
|
673
|
+
x (first forms)
|
|
674
|
+
gen (generic? (or d empty))]
|
|
675
|
+
(when-not (map? x) (bad "A buffer is a map: {:strokes {:t1 [[0 440] [1 440]]} :gx nil :gy nil}"))
|
|
676
|
+
(when-let [k (first (remove #{:strokes :gx :gy :ax :quant} (keys x)))]
|
|
677
|
+
(bad (str "Unknown key " (pr-str k) ": a buffer has :strokes, :gx, :gy" (if gen " and :ax" " and :quant"))))
|
|
678
|
+
(when (and gen (some? (:quant x))) (bad "A canvas has no :quant: only a freqt is quantized into a score"))
|
|
679
|
+
(when (and (some? (:quant x)) (not (map? (:quant x)))) (bad ":quant is a map of options: {:meter \"6/8\" :divs [1 2 3 4]}"))
|
|
680
|
+
(when (and (not gen) (some? (:ax x))) (bad "A freqt has no :ax: its axes are time and frequency"))
|
|
681
|
+
(let [ax (when gen (if (some? (:ax x))
|
|
682
|
+
(let [a (sane-ax (:ax x))]
|
|
683
|
+
(when-not (and a (every? (fn [k] (let [i (get-in x [:ax k])]
|
|
684
|
+
(or (nil? i) (= (get a k) {:lo (:lo i) :hi (:hi i) :log (boolean (:log i))}))))
|
|
685
|
+
[:x :y]))
|
|
686
|
+
(bad ":ax is {:x {:lo 0 :hi 8 :log false} :y {:lo 0 :hi 1 :log false}} (lo < hi; both positive if :log)"))
|
|
687
|
+
a)
|
|
688
|
+
default-ax))
|
|
689
|
+
base (cond-> {:strokes {}} gen (assoc :ax ax))
|
|
690
|
+
gx (when (some? (:gx x)) (or (sane-gx (:gx x)) (bad ":gx is {:step 0.25 :cycles [4]} (or nil)")))
|
|
691
|
+
gy0 (:gy x)
|
|
692
|
+
gy0 (if-let [t (and (map? gy0) (nil? (:cents gy0)) (nil? (:step gy0)) (string? (:name gy0))
|
|
693
|
+
(get tunings (str/lower-case (:name gy0))))]
|
|
694
|
+
(assoc t :ref (:ref gy0))
|
|
695
|
+
gy0)
|
|
696
|
+
gy (when (some? gy0) (or (sane-gy gy0) (bad (str ":gy is a tuning {:name \"12-tet\" :ref 261.63} (a name from "
|
|
697
|
+
(str/join ", " (sort (keys tunings))) ", or with :cents), a grid {:step 0.1}, or nil"))))
|
|
698
|
+
ss (:strokes x)]
|
|
699
|
+
(when-not (or (nil? ss) (map? ss)) (bad ":strokes is a map: {:t1 [[0 440] [1 440]] :t2 …}"))
|
|
700
|
+
(assoc (cond-> base (sane-quant (:quant x)) (assoc :quant (sane-quant (:quant x)))) :gx gx :gy gy
|
|
701
|
+
:strokes (into {} (for [[k s] ss]
|
|
702
|
+
(let [pts (if (map? s) (:pts s) s)]
|
|
703
|
+
(when-not (keyword? k) (bad (str "A stroke id is a keyword such as :t1, not " (pr-str k))))
|
|
704
|
+
[k (with-look {:pts (or (let [r (sane-pts-for base pts)]
|
|
705
|
+
;; a freqt drops points it cannot hold: here that is an error
|
|
706
|
+
(when (and r (or gen (= (count r) (count pts)))) r))
|
|
707
|
+
(bad (str "Stroke " k ": "
|
|
708
|
+
(if gen "at least two different points [x y]"
|
|
709
|
+
"at least two points [t hz], times from 0 and growing, hz between 1 and 30000"))))}
|
|
710
|
+
(when (map? s) (:look s)))])))))))
|
|
711
|
+
|
|
712
|
+
(defn diff
|
|
713
|
+
"The events that turn buffer a into buffer b: (reduce step a (diff a b)) is b."
|
|
714
|
+
[a b]
|
|
715
|
+
(let [a (or a empty)]
|
|
716
|
+
(vec (concat
|
|
717
|
+
(when (and (generic? b) (not= (:ax a) (:ax b))) [[:axis (:ax b)]])
|
|
718
|
+
(when (not= (:gx a) (:gx b)) [[:grid-x (:gx b)]])
|
|
719
|
+
(when (not= (:gy a) (:gy b)) [[:tuning (:gy b)]])
|
|
720
|
+
(when (not= (:quant a) (:quant b)) [[:quantize (:quant b)]])
|
|
721
|
+
(for [[id _] (sorted-strokes a) :when (not (contains? (:strokes b) id))] [:erase id])
|
|
722
|
+
(for [[id s] (sorted-strokes b) :when (not= s (get-in a [:strokes id]))]
|
|
723
|
+
(if (:look s) [:stroke id (:pts s) (:look s)] [:stroke id (:pts s)]))))))
|
|
724
|
+
|
|
725
|
+
(defn ->voice
|
|
726
|
+
"Approximation of the canvas as a kairotic.score voice: each stroke is a note on the
|
|
727
|
+
semitone nearest to its first point; the deviation in cents and glissandi are annotated."
|
|
728
|
+
[d]
|
|
729
|
+
(let [L (cycle (:gx d))
|
|
730
|
+
snap (fn [x] (/ (js/Math.round (* x 4)) 4))]
|
|
731
|
+
{:tempo 90 :meter [(max 1 (js/Math.round L)) 4]
|
|
732
|
+
:events (into {} (for [[id s] (:strokes d)
|
|
733
|
+
:let [pts (:pts s) [t0 f0] (first pts) [t1 f1] (peek pts)
|
|
734
|
+
m (hz->midi f0) mr (js/Math.round m) dv (js/Math.round (* 100 (- m mr)))
|
|
735
|
+
gl (let [c (* 1200 (log2 (/ f1 f0)))] (cond (> c 30) "↗" (< c -30) "↘" :else nil))
|
|
736
|
+
txt (str (when (>= (js/Math.abs dv) 5) (str (when (pos? dv) "+") dv)) gl)]]
|
|
737
|
+
[id (cond-> {:t (snap t0) :pitch (max 0 (min 127 mr)) :dur (max 0.25 (snap (- t1 t0)))}
|
|
738
|
+
(seq txt) (assoc :txt txt))]))}))
|
|
739
|
+
|
|
740
|
+
(defn simplify
|
|
741
|
+
"Ramer–Douglas–Peucker over [x y] points (in pixels): keeps those that deviate more than `eps`."
|
|
742
|
+
[ps eps]
|
|
743
|
+
(if (< (count ps) 3)
|
|
744
|
+
(vec ps)
|
|
745
|
+
(let [[ax ay] (first ps) [bx by] (peek ps)
|
|
746
|
+
dx (- bx ax) dy (- by ay) L (max 1e-9 (js/Math.hypot dx dy))
|
|
747
|
+
dist (fn [[x y]] (/ (js/Math.abs (- (* dy (- x ax)) (* dx (- y ay)))) L))
|
|
748
|
+
[i dmax] (reduce (fn [[bi bd] i] (let [dd (dist (nth ps i))] (if (> dd bd) [i dd] [bi bd]))) [0 0] (range 1 (dec (count ps))))]
|
|
749
|
+
(if (> dmax eps)
|
|
750
|
+
(vec (concat (butlast (simplify (subvec ps 0 (inc i)) eps)) (simplify (subvec ps i) eps)))
|
|
751
|
+
[(first ps) (peek ps)]))))
|