@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.
Files changed (96) hide show
  1. package/README.md +9 -2
  2. package/index.html +1101 -0
  3. package/kairotic/api.cljs +362 -0
  4. package/kairotic/assistant.cljs +505 -0
  5. package/kairotic/backends/gun-backend.js +371 -0
  6. package/kairotic/canvas.cljs +751 -0
  7. package/kairotic/code.cljs +108 -0
  8. package/kairotic/components.cljs +838 -0
  9. package/kairotic/core.cljs +96 -0
  10. package/kairotic/corefns.cljs +473 -0
  11. package/kairotic/docs.mjs +2 -0
  12. package/kairotic/engine.cljs +482 -0
  13. package/kairotic/examples.mjs +96 -0
  14. package/kairotic/faust.cljs +225 -0
  15. package/kairotic/help.cljs +1036 -0
  16. package/kairotic/hiccup.cljs +566 -0
  17. package/kairotic/input.cljs +271 -0
  18. package/kairotic/kfile.mjs +138 -0
  19. package/kairotic/library.cljs +214 -0
  20. package/kairotic/links.cljs +41 -0
  21. package/kairotic/mt.cljs +1604 -0
  22. package/kairotic/mtboxes.cljs +276 -0
  23. package/kairotic/packages/analysis.mjs +64 -0
  24. package/kairotic/packages/controls.mjs +28 -0
  25. package/kairotic/packages/data.mjs +22 -0
  26. package/kairotic/packages/faust-units/cmaes.mjs +82 -0
  27. package/kairotic/packages/faust-units/graph.mjs +212 -0
  28. package/kairotic/packages/faust-units/listen.mjs +125 -0
  29. package/kairotic/packages/faust-units/notation.mjs +139 -0
  30. package/kairotic/packages/faust-units/parse.mjs +439 -0
  31. package/kairotic/packages/faust-units/recipe.mjs +195 -0
  32. package/kairotic/packages/faust-units/render.mjs +69 -0
  33. package/kairotic/packages/faust-units/units.mjs +130 -0
  34. package/kairotic/packages/faust-units/view.mjs +353 -0
  35. package/kairotic/packages/faust-units.mjs +9 -0
  36. package/kairotic/packages/io.mjs +58 -0
  37. package/kairotic/packages/net.mjs +28 -0
  38. package/kairotic/packages/patterns.mjs +34 -0
  39. package/kairotic/packages/spatial.mjs +22 -0
  40. package/kairotic/packages/spectral/shift.mjs +169 -0
  41. package/kairotic/packages/spectral.mjs +16 -0
  42. package/kairotic/packages/synth.mjs +28 -0
  43. package/kairotic/packages/theory/counterpoint.mjs +798 -0
  44. package/kairotic/packages/theory/mirror.mjs +221 -0
  45. package/kairotic/packages/theory.mjs +46 -0
  46. package/kairotic/patterns.cljs +538 -0
  47. package/kairotic/presets.cljs +227 -0
  48. package/kairotic/quantize.cljs +782 -0
  49. package/kairotic/record.cljs +169 -0
  50. package/kairotic/release.cljs +90 -0
  51. package/kairotic/ring.cljs +129 -0
  52. package/kairotic/ringaudio.cljs +232 -0
  53. package/kairotic/ringcore.mjs +270 -0
  54. package/kairotic/score.cljs +176 -0
  55. package/kairotic/shapes.cljs +283 -0
  56. package/kairotic/signal.cljs +327 -0
  57. package/kairotic/sounds.cljs +565 -0
  58. package/kairotic/sounds.mjs +108 -0
  59. package/kairotic/span.cljs +129 -0
  60. package/kairotic/spell.cljs +281 -0
  61. package/kairotic/structure.cljs +145 -0
  62. package/kairotic/talk.cljs +115 -0
  63. package/kairotic/tempo.cljs +226 -0
  64. package/kairotic/treecanvas.cljs +299 -0
  65. package/kairotic/tutorials.mjs +1283 -0
  66. package/kairotic/vendor/codemirror.mjs +23 -0
  67. package/package.json +23 -4
  68. package/page/00-work.cljs +862 -0
  69. package/page/01-commands.cljs +1219 -0
  70. package/page/02-patch-view.cljs +1051 -0
  71. package/page/03-history-view.cljs +481 -0
  72. package/page/04-panels.cljs +448 -0
  73. package/page/05-gestures.cljs +1247 -0
  74. package/page/06-connection.cljs +219 -0
  75. package/page/07-claude.cljs +430 -0
  76. package/page/08-sessions.cljs +423 -0
  77. package/page/09-learn.cljs +602 -0
  78. package/page/10-embed.cljs +469 -0
  79. package/page/11-startup.cljs +61 -0
  80. package/page/12-feedback.cljs +172 -0
  81. package/page/13-perform.cljs +325 -0
  82. package/page/14-editor.cljs +656 -0
  83. package/page/15-presets.cljs +230 -0
  84. package/page/16-release.cljs +242 -0
  85. package/page/17-sounds.cljs +147 -0
  86. package/page/18-lines.cljs +594 -0
  87. package/page/19-files.cljs +247 -0
  88. package/page/19-html.cljs +152 -0
  89. package/page/20-talk.cljs +527 -0
  90. package/page/21-net.cljs +110 -0
  91. package/page/22-learn.cljs +303 -0
  92. package/page/23-ids.cljs +233 -0
  93. package/page/24-clipboard.cljs +136 -0
  94. package/page/25-tidy.cljs +74 -0
  95. package/page/26-probes.cljs +111 -0
  96. package/sources.mjs +2 -0
@@ -0,0 +1,230 @@
1
+ ;; SPDX-License-Identifier: MPL-2.0
2
+ ;; Kairotic · the page, part 16 of 16: presets
3
+ ;; The page's ClojureScript, in the order it is evaluated (app/index.html lists the files).
4
+
5
+ (in-ns 'user)
6
+
7
+ ;; ───────────────────────── presets from the keyframes of the history (issue #35) ─────────────────────────
8
+ ;; Every keyframe (◆ snapshot, or take kept from Perform) is a preset of the work: what it holds is the text
9
+ ;; of every box (kairotic.presets/configuration). A preset is recalled as one event that gives each box the
10
+ ;; text it had there; a morph moves the controls (boxes that keep their value in their text, a knob) between
11
+ ;; two presets as messages, as a drag does, until (morph :keep) writes them; a generated preset keeps what is
12
+ ;; the same in all of them and varies what changes, within its range. Everything acts on the whole work,
13
+ ;; wherever the view is, and in Perform it goes to the take, as any change does.
14
+
15
+ (defn keyframes
16
+ "The keyframes of the history, in order of moment (the same order in every tab of a room):
17
+ [{:n 1 :label \"intro\" :kind :snapshot :moment 4} …]."
18
+ []
19
+ (->> (for [n (vals (:nodes @hist))
20
+ [k ev] (map-indexed vector (:events n))
21
+ :when (and (vector? ev) (= :mark (first ev)))
22
+ :let [m (when (map? (second ev)) (second ev)) pos [(:id n) (inc k)]]]
23
+ (cond-> {:kind (or (:kind m) :snapshot) :pos pos :moment (depth-of pos)}
24
+ (seq (str (:label m))) (assoc :label (str (:label m)))))
25
+ (sort-by (juxt :moment (fn [k] (str (first (:pos k))))))
26
+ (map-indexed (fn [i k] (assoc k :n (inc i))))
27
+ vec))
28
+
29
+ (defn find-keyframe
30
+ "Keyframe `ref` among `kfs`: a number is its place in that list (1 = the first), a label the last one
31
+ written with it."
32
+ [kfs ref]
33
+ (cond
34
+ (number? ref) (or (when (integer? ref) (get kfs (dec ref)))
35
+ (fail (str "There is no keyframe " ref ": there " (if (= 1 (count kfs)) "is 1" (str "are " (count kfs))) ".")))
36
+ (or (string? ref) (keyword? ref))
37
+ (or (last (filter (fn [k] (= (:label k) (name ref))) kfs))
38
+ (fail (str "No snapshot is called " (pr-str (name ref)) ".")))
39
+ :else (fail "A keyframe is a number (1 = the first) or the label of a snapshot.")))
40
+
41
+ (defn- need-history! []
42
+ (when @scratch (fail "Presets come from the history of a work."))
43
+ (when (#{:connecting :none} (:mode @hist)) (fail "Open a work first.")))
44
+
45
+ (defn- need-keyframes [kfs n]
46
+ (when (< (count kfs) n)
47
+ (fail (if (empty? kfs)
48
+ "There are no keyframes yet: ◆ Snapshot (or (snapshot \"name\")) keeps the work as it is now."
49
+ (str "That needs " n " keyframes, and there " (if (= 1 (count kfs)) "is 1" (str "are " (count kfs))) ".")))))
50
+
51
+ (defn keyframe-config
52
+ "What a keyframe holds: the texts it keeps (a preset stored in a box: issue #45), or the work at its point."
53
+ [k] (if (:texts k) (:texts k) (pst/configuration (patch-at (:pos k)))))
54
+ (defn now-config [] (pst/configuration (work-patch)))
55
+
56
+ (defn moment-label [k] (str "keyframe " (:n k) (when (:label k) (str " (" (:label k) ")"))))
57
+
58
+ (defn write-texts!
59
+ "Gives boxes of the work new texts ({engine-id text}), as one event of the history."
60
+ [changes]
61
+ (when (seq changes)
62
+ (let [evs (vec (for [[f t] changes :let [path (pst/path-of f)]] (wrap-in (pop path) [:text (peek path) t])))]
63
+ (dispatch-root! (if (= 1 (count evs)) (first evs) [:batch evs]))
64
+ (doseq [t (vals changes)] (ensure-class! t))))
65
+ changes)
66
+
67
+ (defn control-values
68
+ "The values the controls among `texts` ({engine-id text}) keep: {engine-id value}."
69
+ [texts]
70
+ (into (sorted-map) (keep (fn [[f t]] (when-let [c (pst/control t)] [f (:v c)])) texts)))
71
+
72
+ (declare presets-list)
73
+
74
+ (defn presets-box
75
+ "A presets box of the work by its engine id (:b18, or :b5.b2 inside a sub-patch)."
76
+ [f] (or (get-in (eng/flat (engine-patch)) [:boxes f]) (fail (str "There is no presets box " f))))
77
+
78
+ (defn recall
79
+ "Recalls a keyframe as a preset: every box of the work that is also there takes the text it had there (its
80
+ arguments, and the values its controls keep), as one event. Boxes added or removed since, lines and
81
+ buffers stay as they are. (recall 2) · (recall \"intro\") · (recall :b18 2), among the presets of that box."
82
+ ([ref]
83
+ (need-history!)
84
+ (let [kfs (keyframes) _ (need-keyframes kfs 1)
85
+ k (find-keyframe kfs ref)
86
+ changes (pst/recall-changes (now-config) (keyframe-config k))]
87
+ (write-texts! changes)
88
+ {:recalled (moment-label k) :changed (count changes)}))
89
+ ([f ref]
90
+ (let [kfs (presets-list (presets-box f)) _ (need-keyframes kfs 1)
91
+ k (find-keyframe kfs ref)
92
+ changes (pst/recall-changes (now-config) (keyframe-config k))]
93
+ (write-texts! changes)
94
+ {:recalled (moment-label k) :changed (count changes)})))
95
+
96
+ (defonce last-morph (atom {})) ; the values the last morph gave the controls: {engine-id value}
97
+
98
+ (defn morph-with!
99
+ "Moves the controls between keyframes a and b at t, as messages (nothing is written): {engine-id value}."
100
+ ([a b t] (morph-with! a b t #{}))
101
+ ([a b t exclude]
102
+ (let [vals (pst/morph-values (now-config) (keyframe-config a) (keyframe-config b) (max 0 (min 1 t)) exclude)]
103
+ (doseq [[f v] vals]
104
+ (try (inject-local! f 0 v) (catch :default e (js/console.error "morph" e))))
105
+ (reset! last-morph vals)
106
+ (when (seq vals) (render-overlay!))
107
+ vals)))
108
+
109
+ (defn morph
110
+ "Moves the controls (boxes that keep their value in their text, such as knobs) between two keyframes, as a
111
+ drag does: (morph 1 2 0.5) is halfway. Nothing is written until (morph :keep), which keeps the values of
112
+ the last morph in their texts, as one event."
113
+ ([how]
114
+ (when-not (= how :keep) (fail "Use (morph a b t), or (morph :keep) to keep the last morph."))
115
+ (need-history!)
116
+ (let [changes (pst/keep-changes (now-config) @last-morph)]
117
+ (write-texts! changes)
118
+ (reset! last-morph {})
119
+ {:kept (count changes)}))
120
+ ([a b t]
121
+ (need-history!)
122
+ (when-not (number? t) (fail "t is a number between 0 and 1: (morph 1 2 0.5)."))
123
+ (let [kfs (keyframes)]
124
+ (need-keyframes kfs 1)
125
+ (morph-with! (find-keyframe kfs a) (find-keyframe kfs b) t))))
126
+
127
+ (defn generate
128
+ "A new preset from the keyframes: what is the same in all of them stays; a number that changes takes a value
129
+ within its range (geometric in a box written with log, whole if it always was); a word that changes is one
130
+ of theirs. Written as one event. (generate) · (generate 42), a seed: the same seed, the same preset ·
131
+ (generate 42 [1 3]) from some keyframes only · (generate :b18 42) among the presets of that box."
132
+ ([] (generate (js/Math.floor (* (js/Math.random) 1e9))))
133
+ ([seed] (generate seed nil))
134
+ ([x y]
135
+ (let [box (when (keyword? x) x)
136
+ seed (if box y x)
137
+ refs (when-not box y)]
138
+ (when-not box (need-history!))
139
+ (when-not (number? seed) (fail "The seed is a number: (generate 42)."))
140
+ (let [all (if box (presets-list (presets-box box)) (keyframes))
141
+ kfs (if (seq refs) (mapv (fn [r] (find-keyframe all r)) refs) all)
142
+ _ (need-keyframes kfs 2)
143
+ changes (pst/generate-changes (now-config) (mapv keyframe-config kfs) (pst/rng seed))]
144
+ (write-texts! changes)
145
+ {:generated (count changes) :seed seed :from (mapv :n kfs)}))))
146
+
147
+ ;; ───── the presets box: what it asks for, done here ─────
148
+
149
+ (defn stored-keyframes
150
+ "The presets a box keeps in its data, as keyframes: [{:n :label :kind :texts}]."
151
+ [b] (vec (map-indexed (fn [i p] (assoc p :n (inc i))) (get-in b [:data :presets]))))
152
+
153
+ (defn presets-list
154
+ "The keyframes a presets box uses: the presets it keeps, if any (issue #45: they were chosen when they were
155
+ kept), or, of the keyframes of the history, those its arguments name (numbers, labels), in that order, or all."
156
+ [b]
157
+ (let [stored (stored-keyframes b)]
158
+ (if (seq stored)
159
+ stored
160
+ (let [_ (need-history!)
161
+ all (keyframes)
162
+ refs (map (fn [x] (if (keyword? x) (name x) x)) (comp/args-of (:text b)))]
163
+ (if (seq refs) (mapv (fn [r] (find-keyframe all r)) refs) all)))))
164
+
165
+ (defn presets!
166
+ "Keeps presets in a presets box: (presets! :b18 [{:label \"calm\" :texts {:b13 \"knob 0.5 50 2 log strobe\"}} …]).
167
+ They are part of the patch: its code, links and embeds carry them. (presets! :b18 nil) forgets them."
168
+ [id v]
169
+ (let [b (need-box id)]
170
+ (when-not (= "presets" (klass (:text b))) (fail (str id " is not a presets box")))
171
+ (dispatch! [:in id (if (seq v) [:store v] [:clear])])
172
+ (count (rel/sane-presets v))))
173
+
174
+ (defn store-presets!
175
+ "Keeps in a presets box the keyframes it uses from the history (only what changes between them), so they
176
+ travel with the patch: (store-presets! :b18)."
177
+ [id]
178
+ (let [b (need-box id)]
179
+ (when-not (= "presets" (klass (:text b))) (fail (str id " is not a presets box")))
180
+ (need-history!)
181
+ (let [kfs (presets-list (assoc b :data nil)) _ (need-keyframes kfs 1)
182
+ v (rel/stored-presets (mapv (fn [k] (assoc k :config (keyframe-config k))) kfs))]
183
+ (presets! id v))))
184
+
185
+ (defn- emit-values! [f vals]
186
+ (when (seq vals)
187
+ (try (eng/emit! engine-patch f 0 vals) (catch :default e (js/console.error "presets" e)))))
188
+
189
+ (defn presets-effect!
190
+ "What a presets box (engine id f) asked for: a recall and a generation are gestures of the work (written in
191
+ the REPL, as any gesture); a morph moves the controls as messages."
192
+ [f [_ [op & xs]] _]
193
+ (js/setTimeout
194
+ (fn []
195
+ (try
196
+ (let [b (or (get-in (eng/flat (engine-patch)) [:boxes f]) (fail "the box is not in the work"))
197
+ kfs (presets-list b)
198
+ own? (seq (stored-keyframes b))]
199
+ (case op
200
+ :recall (let [_ (need-keyframes kfs 1)
201
+ k (find-keyframe kfs (first xs))
202
+ changes (pst/recall-changes (now-config) (keyframe-config k))]
203
+ (gesture! (if own? (str "(recall " f " " (:n k) ")") (str "(recall " (:n k) ")")))
204
+ (emit-values! f (control-values changes)))
205
+ :generate (let [_ (need-keyframes kfs 2)
206
+ seed (or (first xs) (js/Math.floor (* (js/Math.random) 1e9)))
207
+ now (now-config)
208
+ changes (pst/generate-changes now (mapv keyframe-config kfs) (pst/rng seed))]
209
+ (gesture! (if own?
210
+ (str "(generate " f " " seed ")")
211
+ (str "(generate " seed (when (not= kfs (keyframes)) (str " " (pr-str (mapv :n kfs)))) ")")))
212
+ (emit-values! f (control-values changes)))
213
+ :keep (gesture! "(morph :keep)")
214
+ ;; a position on the right inlet: 1.5 is halfway from the first keyframe of the box to the second.
215
+ ;; The controls upstream of the box (a knob that drives it) are left where they are.
216
+ :morph-at (do (need-keyframes kfs 2)
217
+ (let [[a b t] (pst/between (first xs) (count kfs))
218
+ exclude (reaching (:lines (eng/flat (engine-patch))) #{f})]
219
+ (emit-values! f (morph-with! (nth kfs (dec a)) (nth kfs (dec b)) t exclude))))
220
+ :morph (let [[a b t] xs
221
+ exclude (reaching (:lines (eng/flat (engine-patch))) #{f})]
222
+ (emit-values! f (morph-with! (find-keyframe kfs a) (find-keyframe kfs b) t exclude)))
223
+ nil))
224
+ (catch :default e
225
+ (repl-print! "err" (esc (str "; presets " (name f) ": " (or (.-message e) e)))))))
226
+ 0))
227
+
228
+ ;; the page receives the effects of boxes: presets here, the rest as before (03-history-view.cljs)
229
+ (swap! eng/rt assoc :effect-hook
230
+ (fn [f [k _ :as e] at] (if (= k :presets) (presets-effect! f e at) (tree-effect! f e at))))
@@ -0,0 +1,242 @@
1
+ ;; SPDX-License-Identifier: MPL-2.0
2
+ ;; Kairotic · the page, part 17 of 17: release
3
+ ;; The page's ClojureScript, in the order it is evaluated (app/index.html lists the files).
4
+
5
+ (in-ns 'user)
6
+
7
+ ;; ───────────────────────── release, and points of the history forgotten (issue #45) ─────────────────────────
8
+ ;; A release is a copy of the work with a compact history: one point per keyframe (its label kept), each
9
+ ;; written as the changes from the one before, then the work as it is, with every presets box keeping its
10
+ ;; presets (so they travel with the patch). The original work and its history are not touched.
11
+ ;; In a solo or local work, points of the history can be forgotten: the moments after them stay exactly as
12
+ ;; they were, and the work at those moments is lost. Branch points and the tip of a branch cannot be forgotten.
13
+
14
+ ;; ───── release ─────
15
+
16
+ (defn- box-data-path
17
+ "Where the data of a box at engine id f is, inside the patch: :b5.b2 → [:boxes :b5 :data :boxes :b2 :data]."
18
+ [f] (vec (concat (mapcat (fn [id] [:boxes id :data]) (pst/path-of f)))))
19
+
20
+ (defn with-stored-presets
21
+ "The patch with every presets box keeping its presets: those it keeps already, or the keyframes it uses."
22
+ [p]
23
+ (reduce (fn [p [f t]]
24
+ (if (and (= "presets" (klass t)) (empty? (get-in p (conj (box-data-path f) :presets))))
25
+ (let [b (get-in (eng/flat p) [:boxes f])
26
+ kfs (try (presets-list (assoc b :data nil)) (catch :default _ nil))]
27
+ (if (seq kfs)
28
+ (assoc-in p (box-data-path f)
29
+ {:presets (rel/stored-presets (mapv (fn [k] (assoc k :config (keyframe-config k))) kfs))})
30
+ p))
31
+ p))
32
+ p (pst/configuration p)))
33
+
34
+ (defn release-events
35
+ "The compact history of the work: for each keyframe (in order of moment) the changes from the one before and
36
+ its mark, then the changes to the work as it is now (with its presets boxes keeping their presets)."
37
+ []
38
+ (let [kfs (keyframes)
39
+ ;; one line (#117): the lines its patch uses go inside it, each as it played at that point
40
+ final (with-stored-presets (inline-lines (work-patch)))]
41
+ (loop [prev empty-patch [k & more] kfs out []]
42
+ (if k
43
+ (let [[nid i] (:pos k)
44
+ [op m] (nth (:events (node nid)) (dec i))
45
+ p (inline-lines (patch-at (:pos k)) (:fronts m))
46
+ e (rel/as-event (rel/patch-diff prev p))
47
+ ;; its fronts were points of lines the release does not have
48
+ mark [op (if (map? m) (dissoc m :fronts) m)]]
49
+ (recur p more (cond-> out e (conj e) true (conj mark))))
50
+ (let [e (rel/as-event (rel/patch-diff prev final))]
51
+ (cond-> out e (conj e)))))))
52
+
53
+ (defn history-size [] (reduce + 0 (map (fn [n] (count (:events n))) (vals (:nodes @hist)))))
54
+
55
+ (defn release
56
+ "A release of the work: a new solo work (a local one without an account) with a compact history, one point
57
+ per keyframe and then the work as it is, its presets boxes keeping their presets. The original stays as it
58
+ is. (release) · (release \"name\")."
59
+ ([] (release nil))
60
+ ([nm]
61
+ (when @scratch (fail "A release is made from a work."))
62
+ (when (#{:connecting :none} (:mode @hist)) (fail "Open a work first."))
63
+ (when @perf (fail "You are in Perform: (compose) first."))
64
+ (let [evs (release-events)
65
+ from (history-size)
66
+ nm (str (or (not-empty (str nm)) (str (or (:name @work) "Work") " · release")))
67
+ now (js/Date.now)
68
+ origin (str (name (or (:kind @work) :local)) ":" (:id @work))
69
+ report {:release nm :points (count evs) :from from :keyframes (count (keyframes))}]
70
+ (if (and @dbref (uid) (:writable @hist))
71
+ (let [db @dbref
72
+ ref (.doc (.collection db (coll-for :solo)))
73
+ id (.-id ref)
74
+ hcoll (str (coll-for :solo) "/" id "/history")
75
+ hid (.-id (.doc (.collection db hcoll)))
76
+ n {:id hid :parent nil :at 0 :author (my-author) :session session-id :t now :merge nil :events evs}]
77
+ (-> (.set ref (clj->js {:name nm :created now :author (uid) :simultaneity_ms (:simul-ms @settings)
78
+ :origin origin :release true}))
79
+ (.then (fn [_] (.set (.doc db (str hcoll "/" hid)) (node->js n))))
80
+ (.then (fn [_] (open-work! :solo id nm)))
81
+ (.catch db-failed!))
82
+ (assoc report :to :solo))
83
+ (let [lid (str "local-" (.toString (js/Math.floor (* (js/Math.random) 2176782336)) 36) (.toString now 36))]
84
+ (open-work! :local "release" nm)
85
+ (reset! cache {})
86
+ (swap! hist assoc :nodes {lid {:id lid :parent nil :at 0 :author (my-author) :session session-id :t now
87
+ :merge nil :events evs}})
88
+ (swap! world assoc :pos [lid (count evs)] :auto true :prev-pos nil)
89
+ (render-all!)
90
+ (assoc report :to :local))))))
91
+
92
+ ;; ───── forgetting points of the history ─────
93
+
94
+ (defonce picked (atom #{})) ; points of the history chosen to be forgotten: #{[node-id k] …}
95
+
96
+ (defn forget-allowed? [] (boolean (and (#{:solo :local} (:kind @work)) (not @perf) (not @lesson) (not @scratch))))
97
+
98
+ (defn forget-blocker
99
+ "Why point [nid k] cannot be forgotten, or nil."
100
+ [[nid k]]
101
+ (let [n (node nid)]
102
+ (cond
103
+ (nil? n) "it is not in the history"
104
+ (not (< 0 k (count (:events n)))) "it is the tip of a branch, or its start"
105
+ (some (fn [c] (and (= (:parent c) nid) (= (:at c) k))) (vals (:nodes @hist))) "a branch starts there"
106
+ (some (fn [c] (= (:merge c) [nid k])) (vals (:nodes @hist))) "a merge brought it elsewhere")))
107
+
108
+ (defn- runs
109
+ "Consecutive numbers as [first last] runs: #{2 3 5} → [[2 3] [5 5]]."
110
+ [ks]
111
+ (reduce (fn [acc k] (if (and (seq acc) (= k (inc (second (peek acc))))) (conj (pop acc) [(first (peek acc)) k]) (conj acc [k k])))
112
+ [] (sort ks)))
113
+
114
+ (defn- squash-node
115
+ "The events of node nid with the points `ks` forgotten, and how its old points map to the new ones."
116
+ [nid ks]
117
+ (let [evs (vec (:events (node nid)))
118
+ rs (runs ks)
119
+ ;; each run k1…k2: the events from k1 to k2+1 become the changes from the point before to the one after
120
+ ;; (and the mark of that next point, if it is a keyframe, stays a point of its own)
121
+ repl (into {} (for [[k1 k2] rs
122
+ :let [diff (rel/as-event (rel/patch-diff (patch-at [nid (dec k1)]) (patch-at [nid (inc k2)])))
123
+ nxt (nth evs k2)
124
+ mark? (and (vector? nxt) (= :mark (first nxt)))]]
125
+ [k1 {:to k2 :evs (vec (remove nil? (if mark? [diff nxt] [diff])))}]))
126
+ [new-evs mapping]
127
+ (loop [i 1 out [] m {0 0}]
128
+ (if (> i (count evs))
129
+ [out m]
130
+ (if-let [{:keys [to] r :evs} (get repl i)]
131
+ ;; old points i…to are gone; the point after the run (to+1) is the last of its replacement
132
+ (let [out (into out r) last-old (inc to)]
133
+ (recur (inc last-old) out (assoc m last-old (count out))))
134
+ (let [out (conj out (nth evs (dec i)))]
135
+ (recur (inc i) out (assoc m i (count out)))))))]
136
+ {:events new-evs :map mapping}))
137
+
138
+ (defn forget-points
139
+ "Forgets points of the history of a solo or local work: [[\"node-id\" k] …]. The moments after them stay as
140
+ they were; the work at those moments is lost (and a keyframe there with it). Branch points and tips cannot
141
+ be forgotten. It cannot be undone."
142
+ [pts]
143
+ (when-not (forget-allowed?)
144
+ (fail (if (= :room (:kind @work))
145
+ "In a shared room the history belongs to everyone: make a release (a copy) to get a compact version."
146
+ "Points can be forgotten in a solo or local work, outside Perform and lessons.")))
147
+ (let [pts (set (map (fn [[nid k]] [(str nid) k]) pts))]
148
+ (when (empty? pts) (fail "Which points? Select them with ⇧+click in the history."))
149
+ (doseq [p pts] (when-let [why (forget-blocker p)] (fail (str "Point " (pr-str p) " cannot be forgotten: " why "."))))
150
+ (let [by-node (group-by first pts)
151
+ plans (into {} (for [[nid ps] by-node] [nid (squash-node nid (map second ps))]))
152
+ remap (fn [[nid k :as pos]] (if-let [m (:map (get plans nid))]
153
+ ;; a forgotten point goes to the next point kept
154
+ [nid (or (get m k) (some (fn [j] (get m j)) (range k (+ k 1 (count (:events (node nid)))))) k)]
155
+ pos))
156
+ before (history-size)]
157
+ (swap! hist update :nodes
158
+ (fn [ns]
159
+ (into {} (for [[id n] ns]
160
+ [id (cond-> n
161
+ (contains? plans id) (assoc :events (:events (get plans id)))
162
+ (contains? plans (:parent n)) (assoc :at (second (remap [(:parent n) (:at n)])))
163
+ (and (vector? (:merge n)) (contains? plans (first (:merge n)))) (assoc :merge (remap (:merge n))))]))))
164
+ (swap! world (fn [w] (cond-> w (:pos w) (update :pos remap) (:prev-pos w) (assoc :prev-pos nil))))
165
+ (reset! cache {})
166
+ (reset! picked #{})
167
+ ;; a solo work: the changed segments are written again
168
+ (when (persist?)
169
+ (doseq [n (vals (:nodes @hist))
170
+ :when (or (contains? plans (:id n)) (contains? plans (:parent n))
171
+ (and (vector? (:merge n)) (contains? plans (first (:merge n)))))
172
+ :when (not (str/starts-with? (:id n) "local-"))]
173
+ (let [path (str (hist-path) "/" (:id n)) body (node->js n)]
174
+ (enqueue! path (fn [] (.set (.doc @dbref path) body))))))
175
+ (render-all!)
176
+ {:forgotten (count pts) :events-before before :events-after (history-size)})))
177
+
178
+ ;; ───── interface: ⇧+click picks points, Forget… and Release… ─────
179
+
180
+ (defn mark-picked!
181
+ "Marks in the drawn history the points chosen to be forgotten."
182
+ [svg]
183
+ (when (seq @picked)
184
+ (doseq [el (array-seq (.querySelectorAll svg "[data-goto]"))]
185
+ (let [[nid k] (str/split (.getAttribute el "data-goto") #"\|")]
186
+ (when (contains? @picked [nid (js/parseInt k 10)]) (.add (.-classList el) "picked")))))
187
+ (when-let [b ($ "forget-btn")]
188
+ (set! (.-hidden b) (empty? @picked))
189
+ (set! (.-textContent b) (str "Forget " (count @picked) "…"))))
190
+
191
+ (defn toggle-pick!
192
+ "⇧+click on a point of the history: chosen to be forgotten, or not any more."
193
+ [nid k]
194
+ (cond
195
+ (not (forget-allowed?))
196
+ (repl-print! "err" (esc (if (= :room (:kind @work))
197
+ "; in a shared room points cannot be forgotten: a release (a copy) gives a compact version"
198
+ "; points can be forgotten in a solo or local work, outside Perform and lessons")))
199
+ (forget-blocker [nid k]) (repl-print! "err" (esc (str "; that point cannot be forgotten: " (forget-blocker [nid k]))))
200
+ :else (do (swap! picked (fn [s] (if (contains? s [nid k]) (disj s [nid k]) (conj s [nid k]))))
201
+ (render-tree!))))
202
+
203
+ (defn release-pop [] (floating "release-pop" "perform-pop"))
204
+
205
+ (defn release-ask!
206
+ "Shows the popup of Forget… or Release… with `html` (not ask!, which is the Claude tab's button, 07-claude.cljs)."
207
+ [html]
208
+ (let [el (release-pop)]
209
+ (set! (.-innerHTML el) html)
210
+ (set! (.-hidden el) false)))
211
+
212
+ (.addEventListener (release-pop) "click"
213
+ (fn [e]
214
+ (when-let [a (.. e -target -dataset -rel)]
215
+ (set! (.-hidden (release-pop)) true)
216
+ (case a
217
+ "forget" (gesture! (str "(forget-points " (pr-str (vec (sort @picked))) ")"))
218
+ "release" (gesture! "(release)")
219
+ nil))))
220
+
221
+ (when-let [b ($ "forget-btn")]
222
+ (.addEventListener b "click"
223
+ (fn [_]
224
+ (let [n (count @picked)]
225
+ (release-ask! (str "<p><b>Forget " n (if (= 1 n) " point" " points") " of the history?</b> The work at "
226
+ (if (= 1 n) "that moment" "those moments") " will be lost (and any snapshot there); the moments after "
227
+ (if (= 1 n) "it" "them") " stay exactly as they are. This cannot be undone.</p>"
228
+ "<div class=\"pp-actions\"><button class=\"act primary\" data-rel=\"forget\">Forget "
229
+ (if (= 1 n) "it" "them") "</button><button class=\"act\" data-rel=\"cancel\">Cancel</button></div>"))))))
230
+
231
+ (when-let [b ($ "release-btn")]
232
+ (.addEventListener b "click"
233
+ (fn [_]
234
+ (let [k (count (keyframes))]
235
+ (release-ask! (str "<p><b>Release</b> · a new work with a compact history: " k (if (= 1 k) " keyframe" " keyframes")
236
+ " (out of " (history-size) " events) and then the work as it is; presets boxes keep their presets, so "
237
+ "they travel with the patch. This work stays as it is.</p>"
238
+ "<div class=\"pp-actions\"><button class=\"act primary\" data-rel=\"release\">Make the release</button>"
239
+ "<button class=\"act\" data-rel=\"cancel\">Cancel</button></div>"))))))
240
+
241
+ ;; another work opened: nothing is picked
242
+ (add-watch work ::release (fn [_ _ o n] (when (not= (select-keys o [:kind :id]) (select-keys n [:kind :id])) (reset! picked #{}))))
@@ -0,0 +1,147 @@
1
+ ;; SPDX-License-Identifier: MPL-2.0
2
+ ;; Kairotic · the page, part 18 of 18: sounds
3
+ ;; The page's ClojureScript, in the order it is evaluated (app/index.html lists the files).
4
+
5
+ (in-ns 'user)
6
+
7
+ ;; ───────────────────────── sounds: the data of sample boxes ─────────────────────────
8
+ ;; A sample box keeps a description of its sound in the work ({:audio hash :sr :frames :channels :peaks});
9
+ ;; the samples stay in this browser (kairotic.sounds). A recording reaches the history as a gesture,
10
+ ;; (sample! :bN {…}), which is also how the work's code writes it.
11
+
12
+ (defn need-sample [id]
13
+ (let [b (need-box id)]
14
+ (when-not (= "sample" (klass (:text b))) (fail (str id " is not a sample box")))
15
+ b))
16
+
17
+ (defn sample!
18
+ "The sound of a sample box, as a description: (sample! :b4 {:audio \"5a2c…\" :sr 48000 :frames 96000
19
+ :channels 1 :peaks […]}) — what a recording or (load-sound! :b4) writes — or only a URL,
20
+ (sample! :b4 {:url \"https://…/voice.ogg\"}), fetched by every browser that opens the work.
21
+ (sample! :b4 nil) empties it."
22
+ [id d]
23
+ (need-sample id)
24
+ (if (snd/sample? (snd/sane d))
25
+ (do (dispatch! [:in id [:set d]]) (snd/seconds (snd/sane d)))
26
+ (do (dispatch! [:in id [:clear]]) 0)))
27
+
28
+ (defn sound-of "The description of the sound of a sample box ({} if it is empty)." [id]
29
+ (or (:data (need-sample id)) {}))
30
+
31
+ (defn sound-file-name [d] (str "kairotic-" (.slice (str (:audio d)) 0 8) ".wav"))
32
+
33
+ (defn save-sound
34
+ "Saves the sound of a sample box as a WAV file (16 bits), if this browser has it: (save-sound :b4)."
35
+ [id]
36
+ (let [d (sound-of id)]
37
+ (when-not (snd/sample? d) (fail (str id " has no sound")))
38
+ (-> (js/Promise.all #js [(snd/sound-for d) (snd/lib)])
39
+ (.then (fn [[s L]]
40
+ (if-not s
41
+ (repl-print! "err" (esc (str "; " id ": its sound is not in this browser (it was recorded elsewhere)")))
42
+ (let [a (.createElement js/document "a")]
43
+ (set! (.-href a) (js/URL.createObjectURL (.wav L (:chans s) (:sr s))))
44
+ (set! (.-download a) (sound-file-name d))
45
+ (.click a))))))
46
+ (sound-file-name d)))
47
+
48
+ (defn decode-sound!
49
+ "A sound file (a File or Blob) into the sample box id: decoded at the sample rate of the engine, kept
50
+ in this browser, written as a gesture. Promise of its length in seconds."
51
+ [id file]
52
+ (-> (.arrayBuffer file)
53
+ (.then snd/decode)
54
+ (.then (fn [{:keys [sr chans]}] (snd/keep! sr chans)))
55
+ (.then (fn [d] (gesture! (str "(sample! " id " " (pr-str d) ")")) (snd/seconds d)))
56
+ (.catch (fn [e] (repl-print! "err" (esc (str "; " id ": could not read that sound (" (or (.-message e) e) ")")))))))
57
+
58
+ (defn url-sound!
59
+ "Loads the sound at url into the sample box id: fetched, decoded, kept in this browser and written as a
60
+ gesture, its URL included, so every browser that opens the work fetches it too. Promise of its length."
61
+ [id url]
62
+ (let [d (snd/sane {:url url})]
63
+ (-> (snd/sound-for d)
64
+ (.then (fn [_] (let [full (merge (snd/about d) d)]
65
+ (gesture! (str "(sample! " id " " (pr-str full) ")"))
66
+ (snd/seconds full))))
67
+ (.catch (fn [e] (repl-print! "err" (esc (str "; " id ": " (or (.-message e) e)))))))))
68
+
69
+ (defn load-sound!
70
+ "Brings a sound into a sample box (wav, mp3, ogg, flac… whatever this browser can decode):
71
+ (load-sound! :b4) opens the file chooser; (load-sound! :b4 \"https://…/voice.ogg\") loads it from a URL,
72
+ which every browser that opens the work fetches by itself (a shared session, a link, a test)."
73
+ ([id]
74
+ (need-sample id)
75
+ (let [input (.createElement js/document "input")]
76
+ (set! (.-type input) "file")
77
+ (set! (.-accept input) "audio/*")
78
+ (.addEventListener input "change" (fn [_] (when-let [f (aget (.-files input) 0)] (decode-sound! id f))))
79
+ (.click input)
80
+ :choose-a-file))
81
+ ([id url]
82
+ (need-sample id)
83
+ (when-not (snd/url? url) (fail (str (pr-str url) " is not a URL a sound can be loaded from")))
84
+ (url-sound! id url)
85
+ :loading))
86
+
87
+ ;; ───── sources of sounds: Freesound ─────
88
+ ;; github:owner/repo@ref/path needs nothing; freesound:id needs an API key of whoever opens the work (each
89
+ ;; person their own, from freesound.org/apiv2/apply), kept only in this browser.
90
+
91
+ (defn freesound-key!
92
+ "Keeps your Freesound API key in this browser (never in a work, a link or a room): (freesound-key! \"…\").
93
+ (freesound-key! nil) forgets it. Get one at https://freesound.org/apiv2/apply"
94
+ [k]
95
+ (if (snd/set-freesound-key! (when (string? k) (str/trim k)))
96
+ (do (reset! snd/status {}) :kept-in-this-browser)
97
+ :forgotten))
98
+
99
+ (defn sounds
100
+ "Searches Freesound: (sounds \"kick\") · (sounds \"rain\" {:max 30}) seconds at most · {:license \"cc0\"}
101
+ only CC0. Prints the first results, each with the line that loads it: (load-sound! :bN \"freesound:123\")."
102
+ ([q] (sounds q {}))
103
+ ([q {:keys [max license]}]
104
+ (let [filters (cond-> []
105
+ (number? max) (conj (str "duration:[0 TO " max "]"))
106
+ (= "cc0" (some-> license str str/lower-case)) (conj "license:\"Creative Commons 0\""))]
107
+ (-> (snd/freesound-get "search/text/"
108
+ (cond-> {:query (str q) :fields "id,name,username,license,duration" :page_size 12}
109
+ (seq filters) (assoc :filter (str/join " " filters))))
110
+ (.then (fn [j]
111
+ (let [rs (:results j)]
112
+ (repl-print! "out" (esc (str "; Freesound: " (:count j) " sounds for " (pr-str q)
113
+ (when (seq rs) " — the first ones:"))))
114
+ (doseq [x rs]
115
+ (repl-print! "out" (esc (str "(load-sound! :bN \"freesound:" (:id x) "\") ; "
116
+ (:name x) " · " (:username x) " · " (snd/license-label (:license x))
117
+ " · " (comp/nice (or (:duration x) 0)) " s")))))))
118
+ (.catch (fn [e] (repl-print! "err" (esc (str "; " (or (.-message e) e)))))))
119
+ :searching)))
120
+
121
+ (defn credits
122
+ "The credits of the sounds of the work (those that brought theirs, from Freesound): one line each."
123
+ []
124
+ (let [walk (fn walk [p]
125
+ (mapcat (fn [[_ b]]
126
+ (cond (= "patch" (klass (:text b))) (walk (:data b))
127
+ (= "sample" (klass (:text b)))
128
+ (when-let [c (:credit (merge (:data b) (snd/about (:data b))))] [c])))
129
+ (:boxes p)))
130
+ cs (distinct (walk (work-patch)))]
131
+ (mapv (fn [{:keys [name author license page]}] (str "\"" name "\" by " author (when license (str " (" license ")")) " — " page)) cs)))
132
+
133
+ ;; a sample box asked (a recording finished, or `clear`): done as a gesture, so it is an event of the history
134
+ (let [before (:effect-hook @eng/rt)]
135
+ (swap! eng/rt assoc :effect-hook
136
+ (fn [f [k x :as e] at]
137
+ (if (= k :sample)
138
+ (js/setTimeout
139
+ (fn []
140
+ (cond
141
+ (not (get-in (current) [:boxes f]))
142
+ (repl-print! "err" (esc (str "; " (name f) " is inside a sub-patch: a recording is kept only by the sample boxes of the patch on screen")))
143
+ (= :set (first x)) (gesture! (str "(sample! " f " " (pr-str (second x)) ")"))
144
+ (= :url (first x)) (url-sound! f (second x))
145
+ :else (gesture! (str "(sample! " f " nil)"))))
146
+ 0)
147
+ (when before (before f e at))))))