@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,1219 @@
|
|
|
1
|
+
;; SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
;; Kairotic · the page, part 2 of 12: commands
|
|
3
|
+
;; The page's ClojureScript, in the order it is evaluated (app/index.html lists the files).
|
|
4
|
+
|
|
5
|
+
(in-ns 'user)
|
|
6
|
+
|
|
7
|
+
;; ───────────────────────── commands ─────────────────────────
|
|
8
|
+
|
|
9
|
+
(defonce ui (atom {:sel nil :drag nil :wire nil :hover-in nil :dirty false :hist [] :hidx nil :follow nil}))
|
|
10
|
+
|
|
11
|
+
;; What is selected: [:box id] · [:boxes #{id …}] (several, by ⇧+click or a frame) · [:line l] · nil.
|
|
12
|
+
;; :insp is the box whose inspector is open (⌘/Ctrl+click): a plain click selects and moves, nothing more.
|
|
13
|
+
|
|
14
|
+
(defn sel-ids
|
|
15
|
+
"The boxes selected now (a set)."
|
|
16
|
+
[]
|
|
17
|
+
(let [[kind v] (:sel @ui)] (case kind :box #{v} :boxes v #{})))
|
|
18
|
+
|
|
19
|
+
(defn sel-of
|
|
20
|
+
"The selection made of these boxes."
|
|
21
|
+
[ids]
|
|
22
|
+
(case (count ids) 0 nil 1 [:box (first ids)] [:boxes (set ids)]))
|
|
23
|
+
|
|
24
|
+
(defn fail [msg] (throw (js/Error. msg)))
|
|
25
|
+
|
|
26
|
+
(defn dispatch-root!
|
|
27
|
+
"An event of the whole work (its ids already lead from the top: [:in :b5 …] for a sub-patch), wherever the
|
|
28
|
+
view is: to the scratch patch, to the take in Perform, or to the history."
|
|
29
|
+
[ev]
|
|
30
|
+
(cond
|
|
31
|
+
@scratch (swap! scratch step ev)
|
|
32
|
+
@perf (perform-event! ev)
|
|
33
|
+
(= :connecting (:mode @hist)) (fail "Still connecting to the shared history; try again in a moment.")
|
|
34
|
+
(= :none (:mode @hist)) (fail "Open or create a work from the session manager.")
|
|
35
|
+
:else (do (when (:follow @ui) (swap! ui assoc :follow nil))
|
|
36
|
+
(commit! ev))))
|
|
37
|
+
|
|
38
|
+
(defn dispatch! [ev] (dispatch-root! (wrap-in (cmd-path) ev)))
|
|
39
|
+
|
|
40
|
+
(declare ensure-class!)
|
|
41
|
+
|
|
42
|
+
(defn need-box [id]
|
|
43
|
+
(or (get-in (current) [:boxes id]) (fail (str "There is no box " id))))
|
|
44
|
+
|
|
45
|
+
(defn next-id []
|
|
46
|
+
(let [nums (keep (fn [k] (let [m (re-matches #"b(\d+)" (name k))] (when m (js/parseInt (second m) 10))))
|
|
47
|
+
(keys (:boxes (current))))]
|
|
48
|
+
(keyword (str "b" (inc (if (seq nums) (apply max nums) 0))))))
|
|
49
|
+
|
|
50
|
+
(defn auto-pos []
|
|
51
|
+
(let [n (count (:boxes (current)))]
|
|
52
|
+
[(+ 40 (* 24 (mod n 12))) (+ 40 (* 36 (mod n 12)))]))
|
|
53
|
+
|
|
54
|
+
(defn dispatch-all!
|
|
55
|
+
"Several events as one event of the history (nothing if there are none)."
|
|
56
|
+
[evs] (when-let [e (rel/as-event evs)] (dispatch! e)))
|
|
57
|
+
|
|
58
|
+
(defn box
|
|
59
|
+
"Makes a box: (box \"osc~ 440\"), (box \"osc~ 440\" 40 120), or with its id, (box :b3 \"osc~ 440\" 40 120). Returns its id."
|
|
60
|
+
([text] (let [[x y] (auto-pos)] (box (next-id) text x y)))
|
|
61
|
+
([text x y] (box (next-id) text x y))
|
|
62
|
+
([id text x y]
|
|
63
|
+
(when-not (keyword? id) (fail "A box id is a keyword, e.g. :b3"))
|
|
64
|
+
(check-ref-text! text)
|
|
65
|
+
(forget-box! id)
|
|
66
|
+
(dispatch! [:box id (str text) (js/Math.round x) (js/Math.round y)])
|
|
67
|
+
(ensure-class! text)
|
|
68
|
+
id))
|
|
69
|
+
|
|
70
|
+
(defn connect
|
|
71
|
+
"Connects outlet o of box a to inlet i of box b: (connect :b1 0 :b2 0)."
|
|
72
|
+
[a o b i]
|
|
73
|
+
(let [[_ outs] (io (need-box a))
|
|
74
|
+
[ins _] (io (need-box b))]
|
|
75
|
+
;; boxes with computed ports (or of classes not defined yet) accept any port
|
|
76
|
+
(when-not (or (comp/dynamic? (need-box a)) (< -1 o outs)) (fail (str a " has no outlet " o " (it has " outs ")")))
|
|
77
|
+
(when-not (or (comp/dynamic? (need-box b)) (< -1 i ins)) (fail (str b " has no inlet " i " (it has " ins ")")))
|
|
78
|
+
(when (= a b) (fail "A box cannot be connected to itself"))
|
|
79
|
+
(dispatch! [:connect a o b i])
|
|
80
|
+
[a o b i]))
|
|
81
|
+
|
|
82
|
+
(defn disconnect
|
|
83
|
+
"Takes away the line from outlet o of box a to inlet i of box b: (disconnect :b1 0 :b2 0)."
|
|
84
|
+
[a o b i] (dispatch! [:disconnect a o b i]) :ok)
|
|
85
|
+
(defn move
|
|
86
|
+
"Moves a box to x y: (move :b3 40 120)."
|
|
87
|
+
[id x y] (need-box id) (dispatch! [:move id (js/Math.round x) (js/Math.round y)]) [x y])
|
|
88
|
+
(defn text!
|
|
89
|
+
"Rewrites the text of a box (its class and arguments): (text! :b3 \"osc~ 220\")."
|
|
90
|
+
[id text] (need-box id) (check-ref-text! text) (dispatch! [:text id (str text)]) (eng/forget! (fid id)) (ensure-class! text) id)
|
|
91
|
+
|
|
92
|
+
(defn delete
|
|
93
|
+
"Deletes boxes, with their lines: (delete :b3) · (delete :b3 :b4 :b7)."
|
|
94
|
+
[& ids]
|
|
95
|
+
(when (empty? ids) (fail "Which box? (delete :b3)"))
|
|
96
|
+
(doseq [id ids] (need-box id))
|
|
97
|
+
(doseq [id (distinct ids)] (forget-box! id) (dispatch! [:delete id]))
|
|
98
|
+
:ok)
|
|
99
|
+
|
|
100
|
+
(defn- shift-ids [ids dx dy where]
|
|
101
|
+
(when-not (and (sequential? ids) (seq ids)) (fail "Give the boxes as a vector: [:b1 :b2]"))
|
|
102
|
+
(when-not (and (number? dx) (number? dy)) (fail "dx and dy are numbers"))
|
|
103
|
+
(let [ids (distinct ids) p (current)]
|
|
104
|
+
(doseq [id ids] (need-box id))
|
|
105
|
+
(let [pos (for [id ids :let [b (get-in p [:boxes id])]]
|
|
106
|
+
[id (if (= where :present) (:present b) [(:x b) (:y b)])])]
|
|
107
|
+
(when (and (= where :present) (some (comp nil? second) pos))
|
|
108
|
+
(fail (str "Not in presentation: " (str/join " " (keep (fn [[id xy]] (when-not xy id)) pos)))))
|
|
109
|
+
;; nobody goes past the top or the left edge: the group stops there together
|
|
110
|
+
(let [dx (max dx (- (apply min (map (comp first second) pos))))
|
|
111
|
+
dy (max dy (- (apply min (map (comp second second) pos))))]
|
|
112
|
+
;; moved together, in one event of the history
|
|
113
|
+
(dispatch-all! (for [[id [x y]] pos] [where id (js/Math.round (+ x dx)) (js/Math.round (+ y dy))]))
|
|
114
|
+
[(js/Math.round dx) (js/Math.round dy)]))))
|
|
115
|
+
|
|
116
|
+
(defn next-mark-id []
|
|
117
|
+
(let [nums (keep (fn [k] (let [m (re-matches #"a(\d+)" (name k))] (when m (js/parseInt (second m) 10))))
|
|
118
|
+
(keys (:marks (current))))]
|
|
119
|
+
(keyword (str "a" (inc (if (seq nums) (apply max nums) 0))))))
|
|
120
|
+
|
|
121
|
+
(defn layer
|
|
122
|
+
"A layer of the patch, over or under its boxes, shown in the patch view and/or the presentation:
|
|
123
|
+
(layer :notes) · (layer :viz {:z :under :show #{:present}}) · (layer :viz nil) removes it, with its marks."
|
|
124
|
+
([id] (layer id {}))
|
|
125
|
+
([id opts]
|
|
126
|
+
(when-not (keyword? id) (fail "A layer is named with a keyword: (layer :notes)"))
|
|
127
|
+
(if (nil? opts)
|
|
128
|
+
(dispatch! [:layer id nil])
|
|
129
|
+
(dispatch! [:layer id (merge (or (get-in (current) [:layers id]) layer-default) opts)]))
|
|
130
|
+
id))
|
|
131
|
+
|
|
132
|
+
(defn mark
|
|
133
|
+
"An annotation on a layer. Its points are anchored to a box ([:b4 dx dy] from its top-left corner) or to
|
|
134
|
+
the paper ([:patch x y]): (mark :notes :a1 {:kind :arrow :pts [[:b1 120 10] [:b4 -6 10]]}) ·
|
|
135
|
+
(mark :notes {:kind :text :at [:patch 300 40] :txt \"breathe\"}) · kinds :line (a stroke), :arrow, :text, :image ({:at … :src \"data:image/jpeg;base64,…\" :w :h}, at most 48 KB: a frame kept from a room)."
|
|
136
|
+
([layer-id m] (mark layer-id (next-mark-id) m))
|
|
137
|
+
([layer-id id m]
|
|
138
|
+
(when-not (keyword? layer-id) (fail "Which layer? (mark :notes …)"))
|
|
139
|
+
(when-not (keyword? id) (fail "A mark id is a keyword, e.g. :a3"))
|
|
140
|
+
(let [m (sane-mark (assoc m :layer layer-id))]
|
|
141
|
+
(when-not m (fail "A mark is {:kind :line or :arrow :pts [[anchor dx dy] …]} or {:kind :text :at [anchor dx dy] :txt \"…\"}; an anchor is a box (:b4) or :patch"))
|
|
142
|
+
(dispatch! [:annot id m])
|
|
143
|
+
id)))
|
|
144
|
+
|
|
145
|
+
(defn unmark "Erases marks: (unmark :a3) · (unmark :a3 :a4)." [& ids] (doseq [id ids] (dispatch! [:annot id nil])) :ok)
|
|
146
|
+
|
|
147
|
+
(defn move-mark
|
|
148
|
+
"Moves a mark by dx dy: (move-mark :a3 20 -10). Each point keeps its anchor — a point anchored to a box
|
|
149
|
+
still follows it, from a little further — and a point on the paper moves on the paper."
|
|
150
|
+
[id dx dy]
|
|
151
|
+
(let [m (or (get-in (current) [:marks id]) (fail (str "There is no mark " id)))]
|
|
152
|
+
(when-not (and (number? dx) (number? dy)) (fail "dx and dy are numbers"))
|
|
153
|
+
(dispatch! [:annot id (shift-mark m dx dy)])
|
|
154
|
+
id))
|
|
155
|
+
|
|
156
|
+
(defn project
|
|
157
|
+
"Draws the view of a box again on a layer, at another size: (project :b9 :viz [40 40 800 480]) ·
|
|
158
|
+
(unproject :b9). The box keeps its own place; the projection is drawn from what it shows."
|
|
159
|
+
[id layer-id [x y w h :as rect]]
|
|
160
|
+
(need-box id)
|
|
161
|
+
(when-not (keyword? layer-id) (fail "Which layer? (project :b9 :viz [x y w h])"))
|
|
162
|
+
(when-not (and (sequential? rect) (= 4 (count rect)) (every? number? rect)) (fail "Where: [x y width height]"))
|
|
163
|
+
(when-not (own-view? (need-box id)) (fail (str id " has no view of its own to project (a component drawn with shapes)")))
|
|
164
|
+
(dispatch! [:project id {:layer layer-id :rect (vec rect)}])
|
|
165
|
+
id)
|
|
166
|
+
|
|
167
|
+
(defn unproject [id] (dispatch! [:project id nil]) :ok)
|
|
168
|
+
|
|
169
|
+
(defn move-by
|
|
170
|
+
"Moves several boxes together: (move-by [:b1 :b2 :b5] 40 0)."
|
|
171
|
+
[ids dx dy] (shift-ids ids dx dy :move))
|
|
172
|
+
|
|
173
|
+
(declare child-dims)
|
|
174
|
+
|
|
175
|
+
(defn present
|
|
176
|
+
"Shows a box in presentation (and in the panel of its sub-patch) at x y; without x y, next to the others."
|
|
177
|
+
([id]
|
|
178
|
+
(need-box id)
|
|
179
|
+
(let [p (current)
|
|
180
|
+
right (for [[k b] (:boxes p) :let [[x _] (:present b)] :when (and x (not= k id))]
|
|
181
|
+
(+ x (first (child-dims b))))]
|
|
182
|
+
(present id (if (seq right) (+ 8 (apply max right)) 0) 0)))
|
|
183
|
+
([id x y]
|
|
184
|
+
(need-box id)
|
|
185
|
+
(dispatch! [:present id (js/Math.round x) (js/Math.round y)])
|
|
186
|
+
[x y]))
|
|
187
|
+
|
|
188
|
+
(def size-min [220 110])
|
|
189
|
+
(def size-max [2400 1600])
|
|
190
|
+
|
|
191
|
+
(defn resizable?
|
|
192
|
+
"Boxes drawn with a size of their own: a canvas, a freqt, a score (a freqt or a tree shown as a score)
|
|
193
|
+
and an html box."
|
|
194
|
+
[b] (contains? #{:canvas :score :html} (display-of b)))
|
|
195
|
+
|
|
196
|
+
(defn resize
|
|
197
|
+
"Gives a canvas, freqt or score box a size of its own, as one event: (resize :b3 640 300). A score keeps
|
|
198
|
+
the height of its notation, and its width reflows it. (resize :b3) brings back its default size."
|
|
199
|
+
([id]
|
|
200
|
+
(need-box id)
|
|
201
|
+
(dispatch! [:size id nil])
|
|
202
|
+
:default)
|
|
203
|
+
([id w h]
|
|
204
|
+
(let [b (need-box id)]
|
|
205
|
+
(when-not (resizable? b) (fail (str id " is not drawn with a size of its own (a canvas, a freqt, a score or an html box is)")))
|
|
206
|
+
(when-not (and (number? w) (number? h)) (fail "A size is a width and a height: (resize :b3 640 300)"))
|
|
207
|
+
(let [[w0 h0] size-min [w1 h1] size-max
|
|
208
|
+
w (js/Math.round (max w0 (min w1 w))) h (js/Math.round (max h0 (min h1 h)))]
|
|
209
|
+
(dispatch! [:size id w h])
|
|
210
|
+
[w h]))))
|
|
211
|
+
|
|
212
|
+
(defn present-in-place
|
|
213
|
+
"Shows boxes in presentation where they are in the patch, as one event (issue #42): (present-in-place :b3 :b4)."
|
|
214
|
+
[& ids]
|
|
215
|
+
(when (empty? ids) (fail "Which boxes? (present-in-place :b3 :b4)"))
|
|
216
|
+
(let [evs (vec (for [id (distinct ids) :let [b (need-box id)]] [:present id (js/Math.round (:x b)) (js/Math.round (:y b))]))]
|
|
217
|
+
(dispatch! (if (= 1 (count evs)) (first evs) [:batch evs]))
|
|
218
|
+
(vec (distinct ids))))
|
|
219
|
+
|
|
220
|
+
(defn unpresent "Takes a box out of presentation." [id] (need-box id) (dispatch! [:present id nil]) :ok)
|
|
221
|
+
|
|
222
|
+
(defn present-by
|
|
223
|
+
"Moves several presented boxes together in presentation: (present-by [:b1 :b5] 0 24)."
|
|
224
|
+
[ids dx dy] (shift-ids ids dx dy :present))
|
|
225
|
+
|
|
226
|
+
(declare box-num box-num-e)
|
|
227
|
+
|
|
228
|
+
;; canvases: each canvas box (or its score view) keeps a graphic buffer of frequency × time;
|
|
229
|
+
;; its events arrive wrapped in [:in box event]
|
|
230
|
+
|
|
231
|
+
(defn music? [b] (contains? music-classes (klass (:text b))))
|
|
232
|
+
|
|
233
|
+
(defn tree-box? [b] (= "tree" (klass (:text b))))
|
|
234
|
+
|
|
235
|
+
(defn need-canvas [id]
|
|
236
|
+
(let [b (need-box id)]
|
|
237
|
+
(when-not (or (music? b) (tree-box? b)) (fail (str id " is not a canvas (nor a freqt, a score or a tree)")))
|
|
238
|
+
b))
|
|
239
|
+
|
|
240
|
+
;; a tree box seen as a canvas (discussion #92): its notes as strokes, computed once per tree
|
|
241
|
+
(defonce tree-views (atom {})) ; tree → view, for the last trees drawn
|
|
242
|
+
|
|
243
|
+
(defn tree-view [t]
|
|
244
|
+
(or (get @tree-views t)
|
|
245
|
+
(let [v (tc/view-data t)]
|
|
246
|
+
(swap! tree-views (fn [m] (assoc (if (> (count m) 32) {} m) t v)))
|
|
247
|
+
v)))
|
|
248
|
+
|
|
249
|
+
(defn canvas-data
|
|
250
|
+
"The buffer of a canvas box, or the empty one of its class (generic canvas or freqt); for a tree box,
|
|
251
|
+
its notes as the strokes of a freqt (kairotic.treecanvas)."
|
|
252
|
+
[b] (if (tree-box? b)
|
|
253
|
+
(tree-view (mtb/tree-of (:data b)))
|
|
254
|
+
(or (:data b) (:empty (comp/model-of b)) cv/empty)))
|
|
255
|
+
|
|
256
|
+
(defn canvas-dispatch!
|
|
257
|
+
"A canvas event for a box: as it is for a canvas or freqt; for a tree box, as the edits of its tree that
|
|
258
|
+
make it (kairotic.treecanvas/edit-ops; opts {:free true}: ⌥, out of the measures)."
|
|
259
|
+
([id ev] (canvas-dispatch! id ev {}))
|
|
260
|
+
([id ev opts]
|
|
261
|
+
(let [b (need-box id)]
|
|
262
|
+
(if (tree-box? b)
|
|
263
|
+
(let [t (mtb/tree-of (:data b))
|
|
264
|
+
ops (try (tc/edit-ops t ev opts) (catch :default e (fail (.-message e))))]
|
|
265
|
+
(when (seq ops)
|
|
266
|
+
(let [t2 (try (reduce mt/apply-op t ops) (catch :default e (fail (.-message e))))]
|
|
267
|
+
(when-let [ps (seq (mt/errors t2))] (fail (first ps)))
|
|
268
|
+
(dispatch! [:in id [:edit (vec ops)]]))))
|
|
269
|
+
(dispatch! [:in id ev])))))
|
|
270
|
+
|
|
271
|
+
;; ───── musical trees shown live (Study 19) ─────
|
|
272
|
+
;; A canvas, freqt or score that receives a tree (kairotic.mt) shows it over its buffer, without
|
|
273
|
+
;; touching the history; (keep! :bN) writes it into the buffer, as one event.
|
|
274
|
+
|
|
275
|
+
(defonce live-trees (atom {})) ; engine id → tree
|
|
276
|
+
;; trees kept (keep!) or put away (unlive!): the engine still remembers them as the last thing the box
|
|
277
|
+
;; received, so they are not shown again until a different tree arrives
|
|
278
|
+
(defonce dismissed-trees (atom {})) ; engine id → tree
|
|
279
|
+
|
|
280
|
+
(defn live-tree [id] (get @live-trees (fid id)))
|
|
281
|
+
|
|
282
|
+
(defn shown-data
|
|
283
|
+
"What a canvas or freqt draws: its buffer, or the tree it received (with the buffer's grids)."
|
|
284
|
+
[id b]
|
|
285
|
+
(let [d (canvas-data b)]
|
|
286
|
+
(if-let [t (live-tree id)]
|
|
287
|
+
(try (if (cv/generic? d)
|
|
288
|
+
(let [x (mt/->canvas t)] (assoc d :strokes (:strokes x) :ax (:ax x)))
|
|
289
|
+
(assoc d :strokes (:strokes (mt/->strokes t))))
|
|
290
|
+
(catch :default _ d))
|
|
291
|
+
d)))
|
|
292
|
+
|
|
293
|
+
(defn box-data
|
|
294
|
+
"The buffer of a canvas: {:strokes {:t1 {:pts [[x y] ...]}} :ax axes :gx grid :gy grid or tuning}."
|
|
295
|
+
[id] (canvas-data (need-canvas id)))
|
|
296
|
+
|
|
297
|
+
(defn need-stroke [id tid]
|
|
298
|
+
(or (get-in (box-data id) [:strokes tid]) (fail (str id " has no stroke " tid))))
|
|
299
|
+
|
|
300
|
+
(defn need-pitch [a]
|
|
301
|
+
(or (sc/pitch a) (fail (str "I do not understand the pitch " (pr-str a) ". Use 60, \"c4\", \"f#3\" or \"bb2\"."))))
|
|
302
|
+
|
|
303
|
+
(defn need-pts
|
|
304
|
+
([pts] (need-pts cv/empty pts))
|
|
305
|
+
([d pts]
|
|
306
|
+
(or (cv/sane-pts-for d pts)
|
|
307
|
+
(fail (if (cv/generic? d)
|
|
308
|
+
"A stroke is at least two different points [x y], e.g. [[0 0] [8 1]]"
|
|
309
|
+
"A stroke is at least two points [t hz] with increasing times, e.g. [[0 440] [1 440]]")))))
|
|
310
|
+
|
|
311
|
+
(defn need-freqt [id]
|
|
312
|
+
(when (cv/generic? (box-data id)) (fail (str id " is a generic canvas: notes, degrees and tunings belong to a freqt")))
|
|
313
|
+
id)
|
|
314
|
+
|
|
315
|
+
(defn stroke
|
|
316
|
+
"Draws (or rewrites) a stroke: (stroke :b1 [[0 440] [1 440]]) or (stroke :b1 :t4 [[0 440] [2 660]]), and
|
|
317
|
+
with how it looks: (stroke :b1 :t4 [[0 440] [2 660]] {:w 2 :pan -0.5 :tex :dash :tone :kairos})."
|
|
318
|
+
([id pts] (stroke id (cv/next-id (box-data id)) pts))
|
|
319
|
+
([id tid pts] (stroke id tid pts nil))
|
|
320
|
+
;; the fourth argument is how it looks (a map), or true / :free: drawn freehand (⌥), so that in a
|
|
321
|
+
;; measured tree box the note goes to its free voice
|
|
322
|
+
([id tid pts x]
|
|
323
|
+
(need-canvas id)
|
|
324
|
+
(when-not (keyword? tid) (fail "A stroke id is a keyword, e.g. :t3"))
|
|
325
|
+
(let [look (when (map? x) x) free? (or (true? x) (= x :free))]
|
|
326
|
+
(canvas-dispatch! id (if (cv/sane-look look)
|
|
327
|
+
[:stroke tid (need-pts (box-data id) pts) look]
|
|
328
|
+
[:stroke tid (need-pts (box-data id) pts)])
|
|
329
|
+
(if free? {:free true} {})))
|
|
330
|
+
tid))
|
|
331
|
+
|
|
332
|
+
(defn note
|
|
333
|
+
"A horizontal stroke: (note :b1 t \"c4\" dur), with the pitch as MIDI or by name (a4 = 440 Hz)."
|
|
334
|
+
[id t pch dur]
|
|
335
|
+
(need-freqt id)
|
|
336
|
+
(when-not (and (number? t) (number? dur) (>= t 0) (pos? dur)) (fail "A note is (note box t pitch duration)"))
|
|
337
|
+
(let [f (cv/round-to (cv/midi->hz (need-pitch pch)) 3)]
|
|
338
|
+
(stroke id [[t f] [(+ t dur) f]])))
|
|
339
|
+
|
|
340
|
+
(defn degree
|
|
341
|
+
"A horizontal stroke on degree n of the canvas tuning (0 = the reference)."
|
|
342
|
+
[id t n dur]
|
|
343
|
+
(let [gy (or (:gy (box-data id)) (fail (str id " has no tuning: use (tuning " id " \"12-tet\")")))
|
|
344
|
+
f (cv/round-to (cv/degree->hz gy n) 3)]
|
|
345
|
+
(when-not (and (number? t) (number? dur) (>= t 0) (pos? dur)) (fail "A degree is (degree box t n duration)"))
|
|
346
|
+
(stroke id [[t f] [(+ t dur) f]])))
|
|
347
|
+
|
|
348
|
+
(defn melody
|
|
349
|
+
"A line of notes from t0 (by default, where the canvas ends): \"c4/1 d4/.5 r/1 c4+e4/2\"."
|
|
350
|
+
([id txt] (melody id txt (cv/end-t (box-data id))))
|
|
351
|
+
([id txt t0]
|
|
352
|
+
(need-canvas id)
|
|
353
|
+
(need-freqt id)
|
|
354
|
+
(doall (for [[t a d] (sc/parse-melody txt t0)] (note id t a d)))))
|
|
355
|
+
|
|
356
|
+
(defn move-stroke
|
|
357
|
+
"Shifts a stroke by dx along X and dy along Y (in cents on a freqt or a log axis). In a measured tree,
|
|
358
|
+
:free (⌥) takes the note out of the measures."
|
|
359
|
+
([id tid dt dc] (move-stroke id tid dt dc nil))
|
|
360
|
+
([id tid dt dc free]
|
|
361
|
+
(need-stroke id tid)
|
|
362
|
+
(canvas-dispatch! id [:move tid dt dc] (if free {:free true} {}))
|
|
363
|
+
tid))
|
|
364
|
+
|
|
365
|
+
(defn stretch
|
|
366
|
+
"Lengthens (k > 1) or shortens (k < 1) a stroke in time, from its start."
|
|
367
|
+
[id tid k]
|
|
368
|
+
(need-stroke id tid)
|
|
369
|
+
(when-not (and (number? k) (pos? k)) (fail "The stretch factor is a positive number"))
|
|
370
|
+
(canvas-dispatch! id [:stretch tid k])
|
|
371
|
+
tid)
|
|
372
|
+
|
|
373
|
+
(defn erase-stroke [id tid] (need-stroke id tid) (canvas-dispatch! id [:erase tid]) :ok)
|
|
374
|
+
|
|
375
|
+
(defn canvas-edit!
|
|
376
|
+
"Several edits of a canvas as one event: (canvas-edit! :b1 [[:stroke :t5 [[0 1] [1 2]]] [:erase :t2]]).
|
|
377
|
+
What the messages on the left inlet of a canvas become (issue #26)."
|
|
378
|
+
[id evs]
|
|
379
|
+
(need-canvas id)
|
|
380
|
+
(when-not (and (sequential? evs) (every? vector? evs)) (fail "The edits are a vector of events: [[:stroke :t1 pts] …]"))
|
|
381
|
+
(canvas-dispatch! id [:batch (vec evs)])
|
|
382
|
+
(count evs))
|
|
383
|
+
|
|
384
|
+
(defn clear [id] (need-canvas id) (canvas-dispatch! id [:clear]) :ok)
|
|
385
|
+
|
|
386
|
+
(defn grid-x
|
|
387
|
+
"Time grid: (grid-x :b1 0.25 [4]) · (grid-x :b1 0.5 \"3+3+2\") · (grid-x :b1 nil).
|
|
388
|
+
The step quantizes while drawing; the cycles group (like bars, simple or additive)."
|
|
389
|
+
([id gx] (if (nil? gx) (do (need-canvas id) (canvas-dispatch! id [:grid-x nil]) :no-grid) (fail "Use (grid-x box step cycles) or nil")))
|
|
390
|
+
([id step-size cycles]
|
|
391
|
+
(need-canvas id)
|
|
392
|
+
(let [gx (cv/sane-gx {:step step-size :cycles cycles})]
|
|
393
|
+
(when-not gx (fail "The step is a positive number"))
|
|
394
|
+
(canvas-dispatch! id [:grid-x gx])
|
|
395
|
+
gx)))
|
|
396
|
+
|
|
397
|
+
(defn tunings
|
|
398
|
+
"Tunings available by name (besides any Scala .scl pasted as text)."
|
|
399
|
+
[] (vec (sort (keys cv/tunings))))
|
|
400
|
+
|
|
401
|
+
(defn tuning
|
|
402
|
+
"Frequency grid: a name from (tunings), the text of a .scl or {:name :cents}, and the
|
|
403
|
+
reference frequency of degree 0 (by default C4 = 261.63 Hz). With nil, continuous frequency."
|
|
404
|
+
([id tun] (tuning id tun cv/default-ref))
|
|
405
|
+
([id tun ref]
|
|
406
|
+
(need-canvas id)
|
|
407
|
+
(need-freqt id)
|
|
408
|
+
(if (nil? tun)
|
|
409
|
+
(do (canvas-dispatch! id [:tuning nil]) :continuous)
|
|
410
|
+
(let [base (cond (map? tun) tun
|
|
411
|
+
(and (string? tun) (get cv/tunings (str/lower-case tun))) (get cv/tunings (str/lower-case tun))
|
|
412
|
+
(string? tun) (cv/parse-scl tun)
|
|
413
|
+
:else (fail "Use a name from (tunings), the text of a .scl or a map {:name :cents}"))
|
|
414
|
+
gy (cv/sane-gy (assoc base :ref ref))]
|
|
415
|
+
(when-not gy (fail "That tuning is not valid"))
|
|
416
|
+
(canvas-dispatch! id [:tuning gy])
|
|
417
|
+
(select-keys gy [:name :ref])))))
|
|
418
|
+
|
|
419
|
+
(defn read-at
|
|
420
|
+
"What a canvas holds at x = v (or along :y): [[stroke value] ...], every crossing."
|
|
421
|
+
([id v] (read-at id :x v))
|
|
422
|
+
([id axis v] (cv/read-along (box-data id) axis v)))
|
|
423
|
+
|
|
424
|
+
(defn axis
|
|
425
|
+
"Range and scale of an axis of a generic canvas: (axis :b1 :y -1 1) · (axis :b1 :x 20 20000 :log)."
|
|
426
|
+
[id k lo hi & [scale]]
|
|
427
|
+
(need-canvas id)
|
|
428
|
+
(when-not (cv/generic? (box-data id)) (fail (str id " is a freqt: its axes are time and frequency")))
|
|
429
|
+
(when-not (#{:x :y} k) (fail "The axis is :x or :y"))
|
|
430
|
+
(let [ax (assoc (:ax (box-data id)) k {:lo lo :hi hi :log (= scale :log)})
|
|
431
|
+
ok (cv/sane-ax ax)]
|
|
432
|
+
(when-not (= (get ok k) (get ax k)) (fail "An axis goes from lo to hi (lo < hi; both positive if :log)"))
|
|
433
|
+
(dispatch! [:in id [:axis ok]])
|
|
434
|
+
(get ok k)))
|
|
435
|
+
|
|
436
|
+
(defn grid-y
|
|
437
|
+
"A regular grid on Y (values snap to it while drawing): (grid-y :b1 0.25) · (grid-y :b1 nil)."
|
|
438
|
+
[id step-size]
|
|
439
|
+
(need-canvas id)
|
|
440
|
+
(if (nil? step-size)
|
|
441
|
+
(do (dispatch! [:in id [:grid-y nil]]) :no-grid)
|
|
442
|
+
(let [gy (cv/sane-gy {:step step-size})]
|
|
443
|
+
(when-not gy (fail "The step is a positive number"))
|
|
444
|
+
(dispatch! [:in id [:grid-y gy]])
|
|
445
|
+
gy)))
|
|
446
|
+
|
|
447
|
+
;; code boxes: their source is data of the work; each tab compiles it locally
|
|
448
|
+
|
|
449
|
+
(declare render-all!)
|
|
450
|
+
|
|
451
|
+
(defn need-code-box [id]
|
|
452
|
+
(let [b (need-box id) d (box-def b)]
|
|
453
|
+
(when-not (= :code (:display d)) (fail (str id " is not a code box (faust~, script…)")))
|
|
454
|
+
d))
|
|
455
|
+
|
|
456
|
+
(declare tree!)
|
|
457
|
+
|
|
458
|
+
(defn code-source!
|
|
459
|
+
"Writes the source of a code box (as one event)."
|
|
460
|
+
[id source]
|
|
461
|
+
(let [d (need-code-box id)]
|
|
462
|
+
(when-not (string? source) (fail "The source is a string"))
|
|
463
|
+
(dispatch! [:in id [:source source]])
|
|
464
|
+
(:status (cbox/compiled (:language d) source))))
|
|
465
|
+
|
|
466
|
+
(defn data-source!
|
|
467
|
+
"Writes the data of a box from its text, as its model reads it (a canvas, freqt or score: the EDN of its
|
|
468
|
+
buffer). What changed, and only that, is one event of the history (discussion #30)."
|
|
469
|
+
[id text]
|
|
470
|
+
(let [b (need-box id) m (comp/model-of b)]
|
|
471
|
+
(when-not (and (fn? (:read m)) (fn? (:diff m))) (fail (str id " has no data that can be written as text")))
|
|
472
|
+
(when-not (string? text) (fail "The text is a string"))
|
|
473
|
+
(let [d (or (:data b) (:empty m))
|
|
474
|
+
d2 (try ((:read m) d text) (catch :default e (fail (.-message e))))
|
|
475
|
+
evs ((:diff m) d d2)]
|
|
476
|
+
(if (seq evs)
|
|
477
|
+
(do (dispatch! [:in id [:batch (vec evs)]])
|
|
478
|
+
(str (count evs) (if (= 1 (count evs)) " edit" " edits")))
|
|
479
|
+
:unchanged))))
|
|
480
|
+
|
|
481
|
+
(def keep-limit
|
|
482
|
+
"How long the code of a kept value may be (characters): it travels in the history, in links and to
|
|
483
|
+
everyone in a room."
|
|
484
|
+
65536)
|
|
485
|
+
|
|
486
|
+
(defn code
|
|
487
|
+
"Writes the source of a code box: (code :b7 \"process = os.osc(440) * 0.1;\"). For a tree box the text
|
|
488
|
+
is read as a tree, and what changed becomes edits of its nodes (tree!). For a canvas, freqt or score
|
|
489
|
+
it is the EDN of its buffer ((source :bN) gives it), and what changed becomes edits of its strokes."
|
|
490
|
+
[id source]
|
|
491
|
+
(let [b (need-box id)]
|
|
492
|
+
(cond
|
|
493
|
+
(= "tree" (klass (:text b)))
|
|
494
|
+
(do (when-not (string? source) (fail "The source is a string"))
|
|
495
|
+
(tree! id (try (mt/read-tree source) (catch :default e (fail (str "That is not EDN: " (.-message e)))))))
|
|
496
|
+
(fn? (:read (comp/model-of b))) (data-source! id source)
|
|
497
|
+
:else (code-source! id source))))
|
|
498
|
+
|
|
499
|
+
(defn source
|
|
500
|
+
"The source of a code box, or the data of a canvas, freqt or score as EDN text."
|
|
501
|
+
[id]
|
|
502
|
+
(or (comp/source-of (need-box id)) (fail (str id " has no code (faust~, script, tree…) nor a buffer (canvas, freqt, score)"))))
|
|
503
|
+
|
|
504
|
+
(defn view
|
|
505
|
+
"How a box is shown, among the views of its class: (view :b2 :roll) shows a tree as a piano roll and
|
|
506
|
+
(view :b2 :score) as a score again; a freqt can be a :canvas or a :score; :next is the next one. Its
|
|
507
|
+
data does not change, and its text opens from { } in its header (or (source :b2))."
|
|
508
|
+
[id k]
|
|
509
|
+
(let [b (need-box id) vs (vec (comp/views-of b))]
|
|
510
|
+
(when (< (count vs) 2) (fail (str id " has one view only")))
|
|
511
|
+
(when (and (= k :code) (tree-box? b))
|
|
512
|
+
(fail (str "A tree is seen as a score or a roll; its code opens from { } in its header, in the code window")))
|
|
513
|
+
(let [cur (view-of b)
|
|
514
|
+
k (comp/view-name b k)
|
|
515
|
+
k (if (= k :next)
|
|
516
|
+
(nth vs (mod (inc (or (first (keep-indexed (fn [i v] (when (= v cur) i)) vs)) 0)) (count vs)))
|
|
517
|
+
k)]
|
|
518
|
+
(when-not (or (nil? k) (some #{k} vs))
|
|
519
|
+
(fail (str id " can be shown as " (str/join " or " (map pr-str vs)))))
|
|
520
|
+
(when-not (= (or k (first vs)) cur)
|
|
521
|
+
(dispatch! [:view id (when-not (= k (first vs)) k)]))
|
|
522
|
+
(or k (first vs)))))
|
|
523
|
+
|
|
524
|
+
;; musical trees (Study 19): mt/… are the functions on trees (mt/transpose, mt/events, mt/pp …)
|
|
525
|
+
;; A tree box keeps its tree and its history is made of node edits (Study 20): tree! writes the
|
|
526
|
+
;; difference with what it had; tree-edit! writes exact edits.
|
|
527
|
+
|
|
528
|
+
(defn need-tree-box [id]
|
|
529
|
+
(let [b (need-box id)]
|
|
530
|
+
(when-not (= "tree" (klass (:text b))) (fail (str id " is not a tree box")))
|
|
531
|
+
b))
|
|
532
|
+
|
|
533
|
+
(defn tree-data "The tree a tree box holds (as written)." [id] (mtb/tree-of (:data (need-tree-box id))))
|
|
534
|
+
|
|
535
|
+
(defn tree!
|
|
536
|
+
"Writes a tree into a tree box: (tree! :b3 [:voice \"c4/1 e4 g4\"]). What changes is written as edits of
|
|
537
|
+
its nodes, in one event of the history."
|
|
538
|
+
[id t]
|
|
539
|
+
(need-tree-box id)
|
|
540
|
+
(when-let [ps (seq (mt/errors t))] (fail (first ps)))
|
|
541
|
+
(doseq [w (mt/warnings t)] (repl-print! "out" (esc (str "; ⚠ " w))))
|
|
542
|
+
(let [old (tree-data id)
|
|
543
|
+
ops (if (some? old) (mt/diff old t) [[:set [] t]])]
|
|
544
|
+
(if (seq ops)
|
|
545
|
+
(do (dispatch! [:in id [:edit ops]])
|
|
546
|
+
(str (count ops) (if (= 1 (count ops)) " edit" " edits") " · " (mt/summary t)))
|
|
547
|
+
:unchanged)))
|
|
548
|
+
|
|
549
|
+
(defn tree-edit!
|
|
550
|
+
"Edits the nodes of a tree box, as one event: (tree-edit! :b2 [:attrs [0 1 2] {:pitch \"d4\" :dur 1}]).
|
|
551
|
+
Edits: [:set path x] · [:attrs path {…}] · [:insert path x] · [:remove path]; a path is the list of child
|
|
552
|
+
indices from the root (attributes are not counted)."
|
|
553
|
+
[id & ops]
|
|
554
|
+
(need-tree-box id)
|
|
555
|
+
(let [t (tree-data id)
|
|
556
|
+
t2 (try (reduce mt/apply-op t ops) (catch :default e (fail (.-message e))))]
|
|
557
|
+
(when-let [ps (seq (mt/errors t2))] (fail (first ps)))
|
|
558
|
+
(doseq [w (mt/warnings t2)] (repl-print! "out" (esc (str "; ⚠ " w))))
|
|
559
|
+
(dispatch! [:in id [:edit (vec ops)]])
|
|
560
|
+
(mt/summary t2)))
|
|
561
|
+
|
|
562
|
+
(defn node-at "The node of a tree box at a path: (node-at :b2 [0 1])." [id path] (mt/node-at (tree-data id) path))
|
|
563
|
+
|
|
564
|
+
(defn as-tree
|
|
565
|
+
"A box as a tree: a tree box, the tree a canvas or score is showing, or the strokes of its buffer."
|
|
566
|
+
[id]
|
|
567
|
+
(let [b (need-box id)]
|
|
568
|
+
(cond
|
|
569
|
+
(= "tree" (klass (:text b))) (mt/expand (mtb/tree-of (:data b)))
|
|
570
|
+
(live-tree id) (live-tree id)
|
|
571
|
+
;; a buffer kept from a tree, unchanged since: the tree itself, not an approximation of its strokes
|
|
572
|
+
(and (:recipe b) (not (:stale (:recipe b)))) (:tree (:recipe b))
|
|
573
|
+
(music? b) (mt/<-strokes (canvas-data b))
|
|
574
|
+
:else (fail (str id " is not a tree, a canvas, a freqt or a score")))))
|
|
575
|
+
|
|
576
|
+
(defn quantize
|
|
577
|
+
"What a box's score shows, as a measured tree: a freqt's strokes quantized into measures (its meter
|
|
578
|
+
from its bar grid), a tree without measures quantized, a measured tree as it is. Options:
|
|
579
|
+
(quantize :b1 {:meter \"6/8\" :divs [1 2 3 4 6] :merge [] :grace false}); see kairotic.quantize."
|
|
580
|
+
([id] (quantize id {}))
|
|
581
|
+
([id opts]
|
|
582
|
+
(let [b (need-box id)
|
|
583
|
+
opts (if (map? opts) opts (fail "The options are a map: {:meter \"3/4\" :divs [1 2 3 4]}"))]
|
|
584
|
+
(try
|
|
585
|
+
(cond
|
|
586
|
+
(or (= "tree" (klass (:text b))) (live-tree id) (and (:recipe b) (not (:stale (:recipe b)))))
|
|
587
|
+
(let [t (as-tree id)] (if (mt/measured? t) t (qz/quantize-tree t opts)))
|
|
588
|
+
(and (music? b) (not (cv/generic? (canvas-data b)))) (qz/quantize (canvas-data b) (merge (:quant (canvas-data b)) opts))
|
|
589
|
+
:else (fail (str id " is not a tree or a freqt")))
|
|
590
|
+
(catch :default e (fail (.-message e)))))))
|
|
591
|
+
|
|
592
|
+
(defn- freqt-data
|
|
593
|
+
"The buffer of a freqt box (fails for anything else)."
|
|
594
|
+
[id]
|
|
595
|
+
(let [b (need-box id) d (canvas-data b)]
|
|
596
|
+
(when-not (and (music? b) (not (cv/generic? d))) (fail (str id " is not a freqt")))
|
|
597
|
+
d))
|
|
598
|
+
|
|
599
|
+
(defn- quant-src
|
|
600
|
+
"What a box's score is quantized from, and where its options are kept: a freqt (its strokes, its
|
|
601
|
+
buffer's :quant) or a tree box without measures (its notes, its root's :quant). {:q :gx :notes
|
|
602
|
+
:quantize (fn [q] tree) :keep! (fn [q])}."
|
|
603
|
+
[id]
|
|
604
|
+
(let [b (need-box id)]
|
|
605
|
+
(if (tree-box? b)
|
|
606
|
+
(let [t (or (mtb/tree-of (:data b)) [:voice])
|
|
607
|
+
root (mt/attrs t)]
|
|
608
|
+
(when (mt/measured? t)
|
|
609
|
+
(fail (str id " is written in measures: its divisions are written. (unquantize! " id ") makes it free time again")))
|
|
610
|
+
{:q (:quant root) :gx (:grid root) :notes (qz/tree-notes t)
|
|
611
|
+
:quantize (fn [q] (qz/quantize-tree (mt/with-attrs t (dissoc root :quant)) (or q {})))
|
|
612
|
+
:keep! (fn [q] (dispatch! [:in id [:edit [[:attrs [] (if (seq q) (assoc root :quant q) (dissoc root :quant))]]]]))})
|
|
613
|
+
(let [d (freqt-data id)]
|
|
614
|
+
{:q (:quant d) :gx (:gx d) :notes (qz/strokes->notes d)
|
|
615
|
+
:quantize (fn [q] (qz/quantize d q))
|
|
616
|
+
:keep! (fn [q] (dispatch! [:in id [:quantize q]]))}))))
|
|
617
|
+
|
|
618
|
+
(defn quant
|
|
619
|
+
"How the score of a freqt (or of a tree box without measures) is quantized, kept in its data (one event
|
|
620
|
+
of the history): (quant :b1 {:meter \"6/8\" :divs [1 2 3 4]}); (quant :b1) gives them; (quant :b1 nil)
|
|
621
|
+
goes back to the defaults."
|
|
622
|
+
([id] (:q (quant-src id)))
|
|
623
|
+
([id opts]
|
|
624
|
+
(let [src (quant-src id)]
|
|
625
|
+
(when-not (or (nil? opts) (map? opts)) (fail "The options are a map: {:meter \"6/8\" :divs [1 2 3 4]}, or nil"))
|
|
626
|
+
(try (qz/options opts nil) (catch :default e (fail (.-message e))))
|
|
627
|
+
((:keep! src) opts)
|
|
628
|
+
(or opts :defaults))))
|
|
629
|
+
|
|
630
|
+
(defn track!
|
|
631
|
+
"Finds the beats of a freqt (or a tree box without measures) played freely, in seconds or at any tempo
|
|
632
|
+
(kairotic.tempo, issue #106), and keeps the time map in its :quant (one event of the history), so that its
|
|
633
|
+
score is quantized through them and stays as it is while it is edited: (track! :b1), or with options
|
|
634
|
+
for the quantizer, (track! :b1 {:meter \"6/8\"}). (quant :b1 {:time :track}) finds them each time
|
|
635
|
+
instead; (track! :b1 nil) forgets them. → {:tempo bpm :beats n}."
|
|
636
|
+
([id] (track! id {}))
|
|
637
|
+
([id opts]
|
|
638
|
+
(let [{:keys [q gx notes keep!]} (quant-src id)]
|
|
639
|
+
(if (nil? opts)
|
|
640
|
+
(do (keep! (not-empty (dissoc q :time))) :forgotten)
|
|
641
|
+
(let [q2 (merge (dissoc q :time) opts)
|
|
642
|
+
m (qz/time-map-of notes (qz/options (assoc q2 :time :track) gx))]
|
|
643
|
+
(when-not m (fail (str id ": too few notes to find a beat in")))
|
|
644
|
+
(keep! (assoc q2 :time m))
|
|
645
|
+
{:tempo (js/Math.round (tm/tempo-of-map m)) :beats (count m)})))))
|
|
646
|
+
|
|
647
|
+
(defn enharmonic!
|
|
648
|
+
"Writes the note of a quantized score (a freqt's, or a free tree box's) that starts at beat t with MIDI
|
|
649
|
+
pitch m another way (c#4 → db4 → b##3 → c#4 …), or with the name given; kept with its options (:quant
|
|
650
|
+
:spell), one event of the history. (enharmonic! :b1 2 61), (enharmonic! :b1 2 61 \"db4\"),
|
|
651
|
+
(enharmonic! :b1 2 61 nil): chosen again by the key."
|
|
652
|
+
([id t m] (enharmonic! id t m ::next))
|
|
653
|
+
([id t m nm]
|
|
654
|
+
(let [{:keys [q quantize keep!]} (quant-src id) m (js/Math.round m)
|
|
655
|
+
near? (fn [[[bt p] _]] (and (< (js/Math.abs (- bt t)) 1e-4) (= p m)))
|
|
656
|
+
cur (or (some (fn [kv] (when (near? kv) (second kv))) (:spell q))
|
|
657
|
+
(some (fn [e] (when (and (< (js/Math.abs (- (:t e) t)) 1e-4) (= m (js/Math.round (:pitch e)))) (:name e)))
|
|
658
|
+
(mt/notes (quantize q))))
|
|
659
|
+
names (mapv sp/->name (sp/spellings m))
|
|
660
|
+
nm (cond (= nm ::next) (get names (mod (inc (max -1 (.indexOf names cur))) (count names)))
|
|
661
|
+
(nil? nm) nil
|
|
662
|
+
:else (or (some-> (sp/parse nm) (as-> w (when (= m (sp/->midi w)) (sp/->name w))))
|
|
663
|
+
(fail (str nm " does not sound " (mt/pitch-name m)))))
|
|
664
|
+
spell (cond-> (into {} (remove near? (:spell q))) nm (assoc [t m] nm))]
|
|
665
|
+
(keep! (not-empty (cond-> (dissoc q :spell) (seq spell) (assoc :spell spell))))
|
|
666
|
+
nm)))
|
|
667
|
+
|
|
668
|
+
(defn divisions
|
|
669
|
+
"The ways the beat that holds beat t of a quantized score (a freqt's, a free tree box's) can be
|
|
670
|
+
divided, the current one first: n (n parts), [n1 n2] (in halves), [:merge n] (with the next beat)."
|
|
671
|
+
[id t]
|
|
672
|
+
(let [{:keys [q gx notes]} (quant-src id)]
|
|
673
|
+
(qz/choices notes (qz/options q gx) t)))
|
|
674
|
+
|
|
675
|
+
(defn division!
|
|
676
|
+
"Divides the beat that holds beat t of a quantized score the next way (see divisions), or the way
|
|
677
|
+
given; the choice is kept with its options (:quant :fix), one event of the history. (division! :b1 2.5),
|
|
678
|
+
(division! :b1 2.5 3), (division! :b1 2.5 nil) lets the quantizer choose again."
|
|
679
|
+
([id t]
|
|
680
|
+
(let [{:keys [q gx notes keep!]} (quant-src id)
|
|
681
|
+
{:keys [choice opts]} (qz/next-choice notes q gx t)]
|
|
682
|
+
(keep! opts)
|
|
683
|
+
choice))
|
|
684
|
+
([id t c]
|
|
685
|
+
(let [{:keys [q gx notes keep!]} (quant-src id)
|
|
686
|
+
{:keys [s pair]} (qz/choices notes (qz/options q gx) t)
|
|
687
|
+
c (when (some? c) (or (qz/sane-choice c) (fail "A division is n, [n1 n2] or [:merge n]")))
|
|
688
|
+
near? (fn [a b] (and a b (< (js/Math.abs (- a b)) 1e-6)))
|
|
689
|
+
fix (into {} (remove (fn [[x _]] (or (near? x s) (near? x pair))) (:fix q)))
|
|
690
|
+
fix (if c (assoc fix (if (and (vector? c) (= :merge (first c))) (or pair s) s) c) fix)]
|
|
691
|
+
(keep! (not-empty (cond-> (dissoc q :fix) (seq fix) (assoc :fix fix))))
|
|
692
|
+
c)))
|
|
693
|
+
|
|
694
|
+
(defn quantize!
|
|
695
|
+
"Writes a tree box's free time in measures, as its score shows them (with its :quant, and opts): one
|
|
696
|
+
event of the history (undo goes back). Its key, tuning, grid and name stay on the tree."
|
|
697
|
+
([id] (quantize! id {}))
|
|
698
|
+
([id opts]
|
|
699
|
+
(let [b (need-box id)]
|
|
700
|
+
(when-not (tree-box? b) (fail (str id " is not a tree box: (to-tree! " id ") makes one from a freqt")))
|
|
701
|
+
(let [t (mtb/tree-of (:data b)) root (mt/attrs t)]
|
|
702
|
+
(when (mt/measured? t) (fail (str id " is already written in measures")))
|
|
703
|
+
(let [q (try (qz/quantize-tree t opts) (catch :default e (fail (.-message e))))
|
|
704
|
+
q (mt/update-attrs q merge (select-keys root [:name :key :tuning :grid :tempo :accidentals]))]
|
|
705
|
+
(dispatch! [:in id [:edit [[:set [] q]]]])
|
|
706
|
+
(mt/summary q))))))
|
|
707
|
+
|
|
708
|
+
(defn unquantize!
|
|
709
|
+
"Writes a tree box's notes in free time ([:voice [:par [:note {:at …}] …]]): one event of the history."
|
|
710
|
+
[id]
|
|
711
|
+
(let [b (need-box id)]
|
|
712
|
+
(when-not (tree-box? b) (fail (str id " is not a tree box")))
|
|
713
|
+
(let [f (tc/free-form (mtb/tree-of (:data b)))]
|
|
714
|
+
(dispatch! [:in id [:edit [[:set [] f]]]])
|
|
715
|
+
(mt/summary f))))
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
(defn to-tree!
|
|
720
|
+
"A new tree box beside a freqt with its strokes as notes in free time (and its grid, tuning and
|
|
721
|
+
quantization as the tree's), shown as a roll: the freqt's drawing, now as a tree."
|
|
722
|
+
[id]
|
|
723
|
+
(let [b (need-box id) d (freqt-data id)
|
|
724
|
+
ns (qz/strokes->notes d)
|
|
725
|
+
t [:voice (cond-> {} (:gx d) (assoc :grid (:gx d)) (:gy d) (assoc :tuning (:gy d)) (:quant d) (assoc :quant (:quant d)))
|
|
726
|
+
(into [:par] (for [n ns] [:note (assoc (:attrs n) :at (cv/round-to (:on n) 4) :dur (cv/round-to (- (:off n) (:on n)) 4))]))]
|
|
727
|
+
[x y] [(:x b) (:y b)]
|
|
728
|
+
nid (box "tree" (+ (or x 0) 40) (+ (or y 0) 40))]
|
|
729
|
+
(tree! nid t)
|
|
730
|
+
(view nid :roll)
|
|
731
|
+
nid))
|
|
732
|
+
|
|
733
|
+
(declare patch->code)
|
|
734
|
+
|
|
735
|
+
(defn tree->buffer
|
|
736
|
+
"The buffer d with the strokes of tree t (and, in a generic canvas, axes that fit it)."
|
|
737
|
+
[d t]
|
|
738
|
+
(if (cv/generic? d)
|
|
739
|
+
(let [x (mt/->canvas t)] (assoc d :strokes (:strokes x) :ax (:ax x)))
|
|
740
|
+
(assoc d :strokes (:strokes (mt/->strokes t)))))
|
|
741
|
+
|
|
742
|
+
(defn without-meta
|
|
743
|
+
"x without metadata (a tree remembers where it was written; its recipe keeps only the tree)."
|
|
744
|
+
[x]
|
|
745
|
+
(cond (map? x) (into {} (map (fn [[k v]] [(without-meta k) (without-meta v)]) x))
|
|
746
|
+
(vector? x) (mapv without-meta x)
|
|
747
|
+
(seq? x) (apply list (map without-meta x))
|
|
748
|
+
:else x))
|
|
749
|
+
|
|
750
|
+
(defn upstream-of
|
|
751
|
+
"The boxes of patch p whose output reaches box id, through any chain of lines."
|
|
752
|
+
[p id]
|
|
753
|
+
(loop [front #{id} seen #{}]
|
|
754
|
+
(let [new (set (for [[a _ b _] (:lines p) :when (and (front b) (not= a id) (not (seen a)))] a))]
|
|
755
|
+
(if (empty? new) seen (recur new (into seen new))))))
|
|
756
|
+
|
|
757
|
+
(defn recipe-for
|
|
758
|
+
"The recipe of tree t arriving at box id of the current patch: the tree, the boxes and lines upstream
|
|
759
|
+
as code (their own recipes left out), and the box it came from."
|
|
760
|
+
[id t]
|
|
761
|
+
(let [p (current)
|
|
762
|
+
up (upstream-of p id)
|
|
763
|
+
sub {:boxes (into (sorted-map) (for [k up :let [b (get-in p [:boxes k])] :when b] [k (dissoc b :recipe)]))
|
|
764
|
+
:lines (vec (filter (fn [[a _ b _]] (and (up a) (or (up b) (= b id)))) (:lines p)))}]
|
|
765
|
+
(cond-> {:tree (without-meta t)}
|
|
766
|
+
(seq up) (assoc :calls (patch->code sub))
|
|
767
|
+
(some (fn [[a _ b _]] (when (= b id) a)) (:lines p)) (assoc :from (some (fn [[a _ b _]] (when (= b id) a)) (:lines p))))))
|
|
768
|
+
|
|
769
|
+
(defn keep!
|
|
770
|
+
"Keeps what a box is showing, as its data, in one event of the history.
|
|
771
|
+
(keep! :b5): the tree a canvas, freqt or score is showing, written into its buffer with the recipe that
|
|
772
|
+
made it beside it (the tree and the boxes upstream).
|
|
773
|
+
(keep! :b5 d): the whole data of a box that keeps what it found (a box with :tasks and a :model, such as
|
|
774
|
+
fetch: (keep! :b5 {:url \"https://…\" :json {…}})); (keep! :b5 nil) empties it."
|
|
775
|
+
([id]
|
|
776
|
+
(let [b (need-canvas id) t (or (live-tree id) (fail (str id " is not showing a tree: send it one first")))]
|
|
777
|
+
(dispatch! [:kept id (tree->buffer (canvas-data b) t) (recipe-for id t)])
|
|
778
|
+
(swap! dismissed-trees assoc (fid id) t)
|
|
779
|
+
(swap! live-trees dissoc (fid id))
|
|
780
|
+
(mt/summary t)))
|
|
781
|
+
([id d]
|
|
782
|
+
(let [b (need-box id) m (comp/model-of b)]
|
|
783
|
+
(when-not m (fail (str id " keeps no data")))
|
|
784
|
+
(when-not (or (nil? d) (map? d)) (fail "The data of a box is a map, or nil to empty it"))
|
|
785
|
+
(if (= (when d ((:load m) d)) (:data b))
|
|
786
|
+
:unchanged
|
|
787
|
+
(do (dispatch! [:data id d]) :kept)))))
|
|
788
|
+
|
|
789
|
+
(defn values!
|
|
790
|
+
"Keeps values of an html box in its data, in one event of the history (issue #146), as a knob keeps its
|
|
791
|
+
value in its text: (values! :b5 {:freq 440 :name \"a\"}); nil forgets one. Its elements named so show
|
|
792
|
+
them, and the work keeps them."
|
|
793
|
+
[id m]
|
|
794
|
+
(let [b (need-box id)]
|
|
795
|
+
(when-not (= :html (display-of b)) (fail (str id " is not an html box")))
|
|
796
|
+
(when-not (map? m) (fail "The values are a map: (values! :b5 {:freq 440})"))
|
|
797
|
+
(let [vs (hic/clean-values m) old (hic/clean-values (get-in b [:data :values]))]
|
|
798
|
+
(when (empty? vs) (fail "No value that can be kept: names are words, values numbers, texts, keywords or true/false"))
|
|
799
|
+
(if (every? (fn [[k v]] (if (nil? v) (not (contains? old k)) (= v (get old k)))) vs)
|
|
800
|
+
:unchanged
|
|
801
|
+
(do (dispatch! [:in id [:values vs]]) :kept)))))
|
|
802
|
+
|
|
803
|
+
(defn recipe
|
|
804
|
+
"The recipe kept beside a buffer: (recipe :b5) → {:tree … :calls … :from … :stale true if the buffer
|
|
805
|
+
changed since}. (recipe :b5 r) sets it (the code of a work does), (recipe :b5 nil) removes it."
|
|
806
|
+
([id] (:recipe (need-box id)))
|
|
807
|
+
([id r] (need-box id) (dispatch! [:recipe id r]) (some? (sane-recipe r))))
|
|
808
|
+
|
|
809
|
+
(defn remake!
|
|
810
|
+
"Writes the tree of a buffer's recipe back into the buffer (one event): what keep! wrote, without the
|
|
811
|
+
changes made since."
|
|
812
|
+
[id]
|
|
813
|
+
(let [b (need-canvas id)
|
|
814
|
+
r (or (:recipe b) (fail (str id " has no recipe: it was not kept from a tree")))]
|
|
815
|
+
(dispatch! [:kept id (tree->buffer (canvas-data b) (:tree r)) r])
|
|
816
|
+
(mt/summary (:tree r))))
|
|
817
|
+
|
|
818
|
+
(defn unlive!
|
|
819
|
+
"Stops showing the tree a box received: its buffer again."
|
|
820
|
+
[id]
|
|
821
|
+
(when-let [t (live-tree id)] (swap! dismissed-trees assoc (fid id) t))
|
|
822
|
+
(swap! live-trees dissoc (fid id))
|
|
823
|
+
:ok)
|
|
824
|
+
|
|
825
|
+
(defn compilation
|
|
826
|
+
"How the compilation of a code box went: {:status :error :info}."
|
|
827
|
+
[id]
|
|
828
|
+
(let [d (need-code-box id)]
|
|
829
|
+
(select-keys (cbox/compiled (:language d) (source id)) [:status :error :info])))
|
|
830
|
+
|
|
831
|
+
(defn trust-scripts
|
|
832
|
+
"Allows (or not) this tab to evaluate the script boxes of the open work. Solo and
|
|
833
|
+
local works always allow it; in a shared room it has to be asked for, because a script runs the
|
|
834
|
+
code of whoever wrote it inside this page."
|
|
835
|
+
[on?]
|
|
836
|
+
(reset! cbox/trust (boolean on?))
|
|
837
|
+
(cbox/forget-language! "clojurescript")
|
|
838
|
+
(cbox/forget-language! "component")
|
|
839
|
+
(cbox/forget-language! "pattern")
|
|
840
|
+
(render-all!)
|
|
841
|
+
(boolean on?))
|
|
842
|
+
|
|
843
|
+
(defn components
|
|
844
|
+
"The available box classes."
|
|
845
|
+
[] (mapv first (comp/listing)))
|
|
846
|
+
|
|
847
|
+
(defn help
|
|
848
|
+
"What a box class does: (help \"ramp\")."
|
|
849
|
+
[cls-name]
|
|
850
|
+
(let [d (or (comp/lookup (str cls-name))
|
|
851
|
+
(when (seq (hlp/of (str cls-name))) (assoc (hlp/of (str cls-name)) :class (str cls-name)))
|
|
852
|
+
(fail (str "There is no class " (pr-str cls-name) "; see (components)")))]
|
|
853
|
+
(show-help! (:class d))
|
|
854
|
+
(cond-> {:class (:class d) :doc (:doc d)}
|
|
855
|
+
(:usage d) (assoc :usage (:usage d))
|
|
856
|
+
(vector? (:io d)) (assoc :inlets (first (:io d)) :outlets (second (:io d))))))
|
|
857
|
+
|
|
858
|
+
(defn patch [] (current))
|
|
859
|
+
|
|
860
|
+
(defn boxes
|
|
861
|
+
([] (keys (:boxes (current))))
|
|
862
|
+
([s] (keep (fn [[id b]] (when (str/includes? (:text b) s) id)) (:boxes (current)))))
|
|
863
|
+
|
|
864
|
+
;; navigating the history (creates no events: it only changes where you are)
|
|
865
|
+
|
|
866
|
+
(defn here []
|
|
867
|
+
(let [p (norm (:pos @world))]
|
|
868
|
+
(if p {:segment (first p) :n (second p) :moment (depth-of p)} :no-events)))
|
|
869
|
+
|
|
870
|
+
(defn stop-follow! [] (when (:follow @ui) (swap! ui assoc :follow nil)))
|
|
871
|
+
|
|
872
|
+
(defn children-at [nid idx]
|
|
873
|
+
(filter (fn [n] (and (= (:parent n) nid) (= (:at n) idx))) (vals (:nodes @hist))))
|
|
874
|
+
|
|
875
|
+
(defn leaf? [[nid idx]]
|
|
876
|
+
(and (node nid) (= idx (count (:events (node nid)))) (empty? (children-at nid idx))))
|
|
877
|
+
|
|
878
|
+
(defn auto-advance!
|
|
879
|
+
"With front on, moves forward to the newest tip that continues your line."
|
|
880
|
+
[]
|
|
881
|
+
(let [{:keys [pos auto]} @world]
|
|
882
|
+
(when (and auto pos (not (:follow @ui)))
|
|
883
|
+
(let [p (norm pos) f (front-from p)]
|
|
884
|
+
(when (and f (not= (norm f) p))
|
|
885
|
+
(swap! world assoc :prev-pos p :pos f :jumped-at (js/Date.now)))))))
|
|
886
|
+
|
|
887
|
+
(defn front
|
|
888
|
+
"Turns on or off moving forward with anyone's gestures."
|
|
889
|
+
[on?]
|
|
890
|
+
(swap! world assoc :auto (boolean on?) :prev-pos nil)
|
|
891
|
+
(when on? (auto-advance!))
|
|
892
|
+
(boolean on?))
|
|
893
|
+
|
|
894
|
+
(declare show-leave-dialog!)
|
|
895
|
+
|
|
896
|
+
(defn not-performing!
|
|
897
|
+
"Travelling in the history leaves the take behind: in Perform, it is asked for first."
|
|
898
|
+
[] (when @perf
|
|
899
|
+
(show-leave-dialog!)
|
|
900
|
+
(fail "You are in Perform: leave it first with (compose), keeping the take or not.")))
|
|
901
|
+
|
|
902
|
+
(defn perf-undo!
|
|
903
|
+
"In Perform, ◀ and Ctrl+Z take back your last changes of the take (and ▶ brings them again)."
|
|
904
|
+
[k]
|
|
905
|
+
(swap! perf (fn [st]
|
|
906
|
+
(loop [st st k k]
|
|
907
|
+
(let [e (peek (:own st))]
|
|
908
|
+
(if (or (zero? k) (nil? e) (<= (:t e) (or (:cut st) 0)))
|
|
909
|
+
(perf-recompute st)
|
|
910
|
+
(recur (-> st (update :own pop) (update :redo conj e)) (dec k)))))))
|
|
911
|
+
(jam-push!)
|
|
912
|
+
{:take (count (perf-events @perf))})
|
|
913
|
+
|
|
914
|
+
(defn perf-redo! [k]
|
|
915
|
+
(swap! perf (fn [st]
|
|
916
|
+
(loop [st st k k]
|
|
917
|
+
(let [e (peek (:redo st))]
|
|
918
|
+
(if (or (zero? k) (nil? e))
|
|
919
|
+
(perf-recompute st)
|
|
920
|
+
;; brought again now: it takes its place at the end of the take
|
|
921
|
+
(recur (-> st (update :redo pop) (update :own conj (assoc e :t (js/Date.now)))) (dec k)))))))
|
|
922
|
+
(jam-push!)
|
|
923
|
+
{:take (count (perf-events @perf))})
|
|
924
|
+
|
|
925
|
+
(defn goto [nid n]
|
|
926
|
+
(not-performing!)
|
|
927
|
+
(when-not (node nid) (fail (str "There is no segment " (pr-str nid))))
|
|
928
|
+
(stop-follow!)
|
|
929
|
+
(let [p (norm [nid n])]
|
|
930
|
+
(swap! world assoc :pos p :auto (leaf? p) :prev-pos nil))
|
|
931
|
+
(here))
|
|
932
|
+
|
|
933
|
+
(defn rewind
|
|
934
|
+
([] (rewind 1))
|
|
935
|
+
([k]
|
|
936
|
+
(if @perf
|
|
937
|
+
(perf-undo! k)
|
|
938
|
+
(do (stop-follow!)
|
|
939
|
+
(swap! world assoc :auto false :prev-pos nil)
|
|
940
|
+
(dotimes [_ k]
|
|
941
|
+
(let [p (norm (:pos @world))]
|
|
942
|
+
(when (and p (pos? (second p)))
|
|
943
|
+
(swap! world assoc :pos (norm [(first p) (dec (second p))])))))
|
|
944
|
+
(here)))))
|
|
945
|
+
|
|
946
|
+
(defn undo [] (rewind 1))
|
|
947
|
+
|
|
948
|
+
(defn forward
|
|
949
|
+
([] (forward 1))
|
|
950
|
+
([k]
|
|
951
|
+
(if @perf
|
|
952
|
+
(perf-redo! k)
|
|
953
|
+
(do (stop-follow!)
|
|
954
|
+
(swap! world assoc :auto false :prev-pos nil)
|
|
955
|
+
(dotimes [_ k]
|
|
956
|
+
(let [p (norm (:pos @world))]
|
|
957
|
+
(when p
|
|
958
|
+
(let [[nid idx] p n (node nid)]
|
|
959
|
+
(if (< idx (count (:events n)))
|
|
960
|
+
(swap! world assoc :pos [nid (inc idx)])
|
|
961
|
+
(when-let [c (last (sort-by :t (children-at nid idx)))]
|
|
962
|
+
(swap! world assoc :pos [(:id c) 1])))))))
|
|
963
|
+
(here)))))
|
|
964
|
+
|
|
965
|
+
(defn tip []
|
|
966
|
+
(not-performing!)
|
|
967
|
+
(stop-follow!)
|
|
968
|
+
(when-let [p (norm (:pos @world))] (swap! world assoc :pos (tip-of (first p)) :auto true :prev-pos nil))
|
|
969
|
+
(auto-advance!)
|
|
970
|
+
(here))
|
|
971
|
+
|
|
972
|
+
(defn merge-from
|
|
973
|
+
"Brings to where you are the events of the branch that goes through [segment n]. The merge is one more event."
|
|
974
|
+
[nid n]
|
|
975
|
+
(not-performing!)
|
|
976
|
+
(when-not (node nid) (fail (str "There is no segment " (pr-str nid))))
|
|
977
|
+
(when (#{:connecting :none} (:mode @hist)) (fail "Open a work before merging."))
|
|
978
|
+
(let [src (norm [nid n]) tgt (norm (:pos @world))]
|
|
979
|
+
(when-not tgt (fail "First place yourself at some point of the history."))
|
|
980
|
+
(let [{:keys [error events renamed]} (plan-merge tgt src)]
|
|
981
|
+
(when error (fail error))
|
|
982
|
+
(stop-follow!)
|
|
983
|
+
(let [id (new-node! (first tgt) (second tgt) events src)]
|
|
984
|
+
(swap! world assoc :pos [id (count events)] :auto true :prev-pos nil))
|
|
985
|
+
(cond-> {:events-merged (dec (count events))}
|
|
986
|
+
(seq renamed) (assoc :renamed renamed)))))
|
|
987
|
+
|
|
988
|
+
(defn simultaneity
|
|
989
|
+
"Interval (ms) below which two gestures are considered parallel and branch."
|
|
990
|
+
([] (:simul-ms @settings))
|
|
991
|
+
([ms]
|
|
992
|
+
(when-not (and (number? ms) (<= 0 ms 10000)) (fail "Use a number of milliseconds between 0 and 10000."))
|
|
993
|
+
(swap! settings assoc :simul-ms ms)
|
|
994
|
+
(when (persist?)
|
|
995
|
+
(let [path (work-base)]
|
|
996
|
+
(enqueue! path (fn [] (.update (.doc @dbref path) (clj->js {:simultaneity_ms ms}))))))
|
|
997
|
+
ms))
|
|
998
|
+
|
|
999
|
+
(defn branches []
|
|
1000
|
+
(let [ns (vals (:nodes @hist))
|
|
1001
|
+
parents (set (keep :parent ns))]
|
|
1002
|
+
{:segments (count ns)
|
|
1003
|
+
:tips (count (remove (fn [n] (parents (:id n))) ns))
|
|
1004
|
+
:events (reduce + (map (comp count :events) ns))}))
|
|
1005
|
+
|
|
1006
|
+
;; ───────────────────────── projections ─────────────────────────
|
|
1007
|
+
|
|
1008
|
+
(defn box-num [k] (let [m (re-matches #"b(\d+)" (name k))] (if m (js/parseInt (second m) 10) 1e9)))
|
|
1009
|
+
|
|
1010
|
+
(defn patch->code [p]
|
|
1011
|
+
(let [ids (sort-by box-num (keys (:boxes p)))]
|
|
1012
|
+
(str/join "\n"
|
|
1013
|
+
(concat [";; boxes"]
|
|
1014
|
+
(for [id ids :let [{:keys [text x y]} (get-in p [:boxes id])]]
|
|
1015
|
+
(str "(box " id " " (pr-str text) " " x " " y ")"))
|
|
1016
|
+
(let [pr (filter (fn [id] (:present (get-in p [:boxes id]))) ids)]
|
|
1017
|
+
(when (seq pr)
|
|
1018
|
+
(concat ["" ";; presentation"]
|
|
1019
|
+
(for [id pr :let [[x y] (get-in p [:boxes id :present])]]
|
|
1020
|
+
(str "(present " id " " x " " y ")")))))
|
|
1021
|
+
(let [vw (filter (fn [id] (:view (get-in p [:boxes id]))) ids)]
|
|
1022
|
+
(when (seq vw)
|
|
1023
|
+
(concat ["" ";; views"]
|
|
1024
|
+
(for [id vw] (str "(view " id " " (pr-str (get-in p [:boxes id :view])) ")")))))
|
|
1025
|
+
;; the `patch @name` boxes pinned to a point of their line (#117)
|
|
1026
|
+
(let [pn (filter (fn [id] (:pin (get-in p [:boxes id]))) ids)]
|
|
1027
|
+
(when (seq pn)
|
|
1028
|
+
(concat ["" ";; pinned lines"]
|
|
1029
|
+
(for [id pn] (str "(pin-ref " id " " (pr-str (get-in p [:boxes id :pin])) ")")))))
|
|
1030
|
+
(let [sz (filter (fn [id] (:size (get-in p [:boxes id]))) ids)]
|
|
1031
|
+
(when (seq sz)
|
|
1032
|
+
(concat ["" ";; sizes"]
|
|
1033
|
+
(for [id sz :let [[w h] (get-in p [:boxes id :size])]] (str "(resize " id " " w " " h ")")))))
|
|
1034
|
+
(when (seq (:lines p)) ["" ";; lines"])
|
|
1035
|
+
(for [[a o b i] (:lines p)]
|
|
1036
|
+
(str "(connect " a " " o " " b " " i ")"))
|
|
1037
|
+
(mapcat (fn [id]
|
|
1038
|
+
(let [b (get-in p [:boxes id])
|
|
1039
|
+
ls ((:code (comp/model-of b)) id (:data b))]
|
|
1040
|
+
(when (seq ls) (concat ["" (str ";; data of " id)] ls))))
|
|
1041
|
+
(filter (fn [id] (let [b (get-in p [:boxes id])] (and (:data b) (:code (comp/model-of b))))) ids))
|
|
1042
|
+
;; layers, their projections and marks
|
|
1043
|
+
(when (or (:layers p) (:marks p) (:projections p))
|
|
1044
|
+
(concat ["" ";; layers"]
|
|
1045
|
+
(for [[id l] (sort-by key (:layers p)) :when (not= l layer-default)]
|
|
1046
|
+
(str "(layer " id " " (pr-str l) ")"))
|
|
1047
|
+
(for [[id pr] (sort-by (comp box-num key) (:projections p))]
|
|
1048
|
+
(str "(project " id " " (:layer pr) " " (pr-str (:rect pr)) ")"))
|
|
1049
|
+
(for [[id m] (:marks p)]
|
|
1050
|
+
(str "(mark " (:layer m) " " id " " (pr-str (dissoc m :layer)) ")"))))
|
|
1051
|
+
;; beside the data, the recipe that made it (keep!)
|
|
1052
|
+
(mapcat (fn [id]
|
|
1053
|
+
(when-let [r (get-in p [:boxes id :recipe])]
|
|
1054
|
+
["" (str ";; recipe of " id (when (:stale r) " (stale: the buffer changed since)"))
|
|
1055
|
+
(str "(recipe " id " " (pr-str r) ")")]))
|
|
1056
|
+
ids)))))
|
|
1057
|
+
|
|
1058
|
+
;; ───── the work as data, pretty: what the Data view of the Code tab shows ─────
|
|
1059
|
+
;; Collections that fit in the width stay on one line; the others put one entry per line, aligned.
|
|
1060
|
+
;; Keys come in a readable order: :text :x :y first, :data last, and :b2 before :b10.
|
|
1061
|
+
|
|
1062
|
+
(def edn-width 72)
|
|
1063
|
+
(def key-rank {:class 0 :text 1 :x 2 :y 3 :present 4 :view 5 :size 6 :data 98 :recipe 99})
|
|
1064
|
+
|
|
1065
|
+
(defn natural-key
|
|
1066
|
+
"A sort key for map keys: the rank of known keys, then the name split into text and numbers."
|
|
1067
|
+
[k]
|
|
1068
|
+
(let [nm (if (keyword? k) (str (when (namespace k) (str (namespace k) "/")) (name k)) (pr-str k))]
|
|
1069
|
+
[(get key-rank k 50)
|
|
1070
|
+
(if (keyword? k) 0 1)
|
|
1071
|
+
(vec (map (fn [[_ txt num]] (if num [1 (js/parseInt num 10)] [0 txt]))
|
|
1072
|
+
(re-seq #"(\D+)|(\d+)" nm)))]))
|
|
1073
|
+
|
|
1074
|
+
(defn sorted-entries [m]
|
|
1075
|
+
(try (sort-by (comp natural-key key) m)
|
|
1076
|
+
(catch :default _ (seq m))))
|
|
1077
|
+
|
|
1078
|
+
(defn edn-line
|
|
1079
|
+
"EDN of x on one line, with the keys of its maps in the readable order."
|
|
1080
|
+
[x]
|
|
1081
|
+
(cond
|
|
1082
|
+
(map? x) (str "{" (str/join " " (for [[k v] (sorted-entries x)] (str (edn-line k) " " (edn-line v)))) "}")
|
|
1083
|
+
(vector? x) (str "[" (str/join " " (map edn-line x)) "]")
|
|
1084
|
+
(set? x) (str "#{" (str/join " " (map edn-line x)) "}")
|
|
1085
|
+
(seq? x) (str "(" (str/join " " (map edn-line x)) ")")
|
|
1086
|
+
:else (pr-str x)))
|
|
1087
|
+
|
|
1088
|
+
(defn pp-edn
|
|
1089
|
+
"EDN of x, at most `edn-width` wide where it can be, indented from column `col`."
|
|
1090
|
+
([x] (pp-edn x 0))
|
|
1091
|
+
([x col]
|
|
1092
|
+
(let [flat (edn-line x)]
|
|
1093
|
+
(if (or (<= (+ col (count flat)) edn-width) (not (coll? x)) (empty? x))
|
|
1094
|
+
flat
|
|
1095
|
+
(let [pad (fn [n] (apply str (repeat n " ")))]
|
|
1096
|
+
(cond
|
|
1097
|
+
(map? x)
|
|
1098
|
+
(str "{"
|
|
1099
|
+
(str/join (str "\n" (pad (inc col)))
|
|
1100
|
+
(for [[k v] (sorted-entries x)]
|
|
1101
|
+
(let [ks (edn-line k)
|
|
1102
|
+
vs (pp-edn v (+ col 2 (count ks)))]
|
|
1103
|
+
(if (and (coll? v) (seq v) (str/includes? vs "\n") (> (count ks) 12))
|
|
1104
|
+
(str ks "\n" (pad (+ col 2)) (pp-edn v (+ col 2)))
|
|
1105
|
+
(str ks " " vs)))))
|
|
1106
|
+
"}")
|
|
1107
|
+
:else
|
|
1108
|
+
(let [[o c] (cond (vector? x) ["[" "]"] (set? x) ["#{" "}"] :else ["(" ")"])
|
|
1109
|
+
inner (+ col (count o))]
|
|
1110
|
+
(str o (str/join (str "\n" (pad inner)) (map (fn [e] (pp-edn e inner)) x)) c))))))))
|
|
1111
|
+
|
|
1112
|
+
(defn patch->edn [p]
|
|
1113
|
+
(pp-edn {:boxes (:boxes p) :lines (vec (:lines p))}))
|
|
1114
|
+
|
|
1115
|
+
;; ───────────────────────── sub-patches ─────────────────────────
|
|
1116
|
+
;; A `patch` box keeps a whole patch as its data, with its own history: editing inside it produces
|
|
1117
|
+
;; events [:in :b5 [:box …]] in the history of the work, and its code is (inside :b5 …).
|
|
1118
|
+
|
|
1119
|
+
(comp/define! "patch"
|
|
1120
|
+
{:doc "An embedded sub-patch: double-click to open it. Its inlet / outlet boxes (numbered left to right) are its ports. patch @name uses the line called name instead (its newest point, or the point it is pinned to): double-click opens its tab."
|
|
1121
|
+
:usage "patch [label] · patch @line"
|
|
1122
|
+
;; its ports: the inlet / outlet boxes of what it holds, or of the line it uses (patch @name, #117)
|
|
1123
|
+
:io (fn [b] (comp/patch-io (or (eng/inner-patch b) empty-patch)))
|
|
1124
|
+
:dynamic-io true
|
|
1125
|
+
:model {:empty empty-patch :step step :load sane-patch
|
|
1126
|
+
:code (fn [id d] (concat [(str "(inside " id)]
|
|
1127
|
+
(map (fn [l] (if (str/blank? l) l (str " " l))) (str/split-lines (patch->code d)))
|
|
1128
|
+
[")"]))}})
|
|
1129
|
+
|
|
1130
|
+
(defn need-patch-box [id]
|
|
1131
|
+
(let [b (need-box id)]
|
|
1132
|
+
(when-not (embedded? b) (fail (str id " is not a patch box (create one with (box \"patch\" x y))")))
|
|
1133
|
+
id))
|
|
1134
|
+
|
|
1135
|
+
(defmacro inside
|
|
1136
|
+
"Runs the body inside the sub-patch :bN: (inside :b5 (box \"inlet\" 20 20) (connect …))."
|
|
1137
|
+
[id & body]
|
|
1138
|
+
`(do (need-patch-box ~id)
|
|
1139
|
+
(binding [*inside* (conj *inside* ~id)] ~@body)
|
|
1140
|
+
~id))
|
|
1141
|
+
|
|
1142
|
+
(declare render-all!)
|
|
1143
|
+
|
|
1144
|
+
(defn enter
|
|
1145
|
+
"Opens the sub-patch :bN: the patch, the code and the commands now act inside it."
|
|
1146
|
+
[id]
|
|
1147
|
+
;; a `patch @name` opens its line, in its tab (#117)
|
|
1148
|
+
(when (line-ref? (need-box id)) (fail (str id " uses a line: (open-ref " id ") opens it in its tab")))
|
|
1149
|
+
(need-patch-box id)
|
|
1150
|
+
(swap! view-path conj id)
|
|
1151
|
+
(swap! ui assoc :sel nil :nsel nil :dirty false)
|
|
1152
|
+
(render-all!)
|
|
1153
|
+
(cmd-path))
|
|
1154
|
+
|
|
1155
|
+
(defn up
|
|
1156
|
+
"Goes back to the patch that contains this one (or n levels up)."
|
|
1157
|
+
([] (up 1))
|
|
1158
|
+
([n] (swap! view-path (fn [p] (vec (drop-last (min n (count p)) p))))
|
|
1159
|
+
(swap! ui assoc :sel nil :nsel nil :dirty false)
|
|
1160
|
+
(render-all!)
|
|
1161
|
+
(cmd-path)))
|
|
1162
|
+
|
|
1163
|
+
(defn top "Goes back to the work itself." [] (up (count @view-path)))
|
|
1164
|
+
|
|
1165
|
+
(defn patch->source
|
|
1166
|
+
"A patch as EDN for the :patch of an abstraction."
|
|
1167
|
+
[p]
|
|
1168
|
+
(str "{:boxes {"
|
|
1169
|
+
(str/join "\n " (for [id (sort-by box-num (keys (:boxes p)))] (str id " " (pr-str (get-in p [:boxes id])))))
|
|
1170
|
+
"}\n :lines " (pr-str (:lines p)) "}"))
|
|
1171
|
+
|
|
1172
|
+
(defn abstract
|
|
1173
|
+
"Turns the sub-patch :bN into a class: a new component box defines it with :patch, and :bN becomes an
|
|
1174
|
+
instance of it. Write $1 $2 … in the texts inside to take the words of each instance."
|
|
1175
|
+
[id class-name]
|
|
1176
|
+
(need-patch-box id)
|
|
1177
|
+
(when-not (and (string? class-name) (re-matches #"[A-Za-z][A-Za-z0-9_~+*/<>=!?-]*" class-name))
|
|
1178
|
+
(fail "The class name is one word, e.g. \"voice\""))
|
|
1179
|
+
(when (comp/lookup class-name) (fail (str "«" class-name "» already exists")))
|
|
1180
|
+
(let [b (need-box id)
|
|
1181
|
+
src (str ";; " class-name " · an abstraction made from a patch\n"
|
|
1182
|
+
"{:class " (pr-str class-name) "\n"
|
|
1183
|
+
" :doc " (pr-str (str "Abstraction made from the sub-patch " id ".")) "\n"
|
|
1184
|
+
" :usage " (pr-str class-name) "\n"
|
|
1185
|
+
" :patch " (patch->source (or (:data b) empty-patch)) "}")
|
|
1186
|
+
nid (next-id)]
|
|
1187
|
+
(box nid "component" (+ (:x b) 180) (:y b))
|
|
1188
|
+
(code nid src)
|
|
1189
|
+
(text! id class-name)
|
|
1190
|
+
nid))
|
|
1191
|
+
|
|
1192
|
+
;; ───────────────────────── evaluation ─────────────────────────
|
|
1193
|
+
|
|
1194
|
+
(defn eval-str [code] (js/scittle.core.eval_string code))
|
|
1195
|
+
|
|
1196
|
+
(def example "\n(box :b1 \"canvas\" 40 60)\n(box :b2 \"loadbang\" 40 15)\n(box :b3 \"ramp 0 1 10 loop\" 140 15)\n(box :b4 \"reader path\" 40 290)\n(box :b5 \"freqt\" 500 60)\n(box :b6 \"phasor~ 0.25\" 500 15)\n(box :b7 \"reader~ norm 2 gate\" 500 290)\n(box :b8 \"osc~\" 500 340)\n(box :b9 \"osc~\" 600 340)\n(box :b10 \"gain~ 0.4\" 500 500)\n(box :b13 \"gain~\" 500 450)\n(box :b14 \"gain~\" 600 450)\n(box :b15 \"adsr~ 30 200 0.6 400\" 700 340)\n(box :b16 \"adsr~ 5 120 0.4 900\" 870 340)\n(box :b11 \"space~ 0 1\" 240 330)\n(box :b12 \"dac~ 1 2\" 240 470)\n(connect :b2 0 :b3 0)\n(connect :b3 0 :b4 0)\n(connect :b1 1 :b4 1)\n(connect :b6 0 :b7 0)\n(connect :b5 1 :b7 1)\n(connect :b7 0 :b8 0)\n(connect :b7 1 :b9 0)\n(connect :b8 0 :b13 0)\n(connect :b9 0 :b14 0)\n(connect :b7 2 :b15 0)\n(connect :b7 3 :b16 0)\n(connect :b15 0 :b13 1)\n(connect :b16 0 :b14 1)\n(connect :b13 0 :b10 0)\n(connect :b14 0 :b10 0)\n(connect :b10 0 :b11 0)\n(connect :b4 0 :b11 0)\n(connect :b11 0 :b12 0)\n(connect :b11 1 :b12 1)\n(axis :b1 :x -1 1)\n(axis :b1 :y -1 1)\n(stroke :b1 [[0 0.9] [0.7 0.6] [0.9 -0.1] [0.5 -0.7] [-0.2 -0.85] [-0.8 -0.4] [-0.9 0.3] [-0.45 0.8] [0 0.9]])\n(stroke :b5 [[0 220] [1 220] [2 330] [2.5 293.66] [3 440] [4 220]])\n(stroke :b5 [[0 330] [1.5 392]])\n(stroke :b5 [[2 261.63] [3 293.66] [4 330]])\n(present :b1 0 0)\n(present :b5 430 0)\n(present :b11 0 210)")
|
|
1197
|
+
|
|
1198
|
+
|
|
1199
|
+
|
|
1200
|
+
|
|
1201
|
+
|
|
1202
|
+
|
|
1203
|
+
(defn box-num-e [k] (cv/id-num k))
|
|
1204
|
+
|
|
1205
|
+
(defn build
|
|
1206
|
+
"Evaluates code in a scratch patch and returns the resulting patch, without touching the history."
|
|
1207
|
+
[code]
|
|
1208
|
+
(reset! scratch empty-patch)
|
|
1209
|
+
(try
|
|
1210
|
+
(eval-str (str "(do " code "\n nil)"))
|
|
1211
|
+
(let [p @scratch] (reset! scratch nil) p)
|
|
1212
|
+
(catch :default e (reset! scratch nil) (throw e))))
|
|
1213
|
+
|
|
1214
|
+
(defn run-code!
|
|
1215
|
+
"Rebuilds the patch from the code: it stays as a single :load event."
|
|
1216
|
+
[code]
|
|
1217
|
+
(let [p (build code)] (dispatch! [:load p]) :ok))
|
|
1218
|
+
|
|
1219
|
+
(defn load-example [] (run-code! example))
|