@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,227 @@
1
+ ;; SPDX-License-Identifier: MPL-2.0
2
+ ;; Kairotic · presets (issue #35)
3
+ ;; The keyframes of the history (◆ snapshots, takes kept from Perform) as presets of the work: switch to
4
+ ;; one, morph between two, or make a new one from what changes and what remains among them.
5
+ ;;
6
+ ;; A configuration is what a preset holds: the text of every box, sub-patches included — its arguments,
7
+ ;; and the values that controls keep in their text (a knob). Boxes that were added or removed, lines and
8
+ ;; buffers are not part of it, so switching never rebuilds the patch. Configurations are keyed by the id
9
+ ;; the engine knows a box by (:b5.b2 is :b2 inside the sub-patch :b5).
10
+ ;;
11
+ ;; This file is pure: the page reads the keyframes from the history and writes what these functions
12
+ ;; return (app/page/15-presets.cljs). The `presets` box only asks the page to do it (an effect).
13
+
14
+ (ns kairotic.presets
15
+ (:require [clojure.string :as str]
16
+ [kairotic.components :as comp]
17
+ [kairotic.release :as rel]))
18
+
19
+ ;; ───────── configurations ─────────
20
+
21
+ (defn fid
22
+ "The engine id of box `id` of the sub-patch at `path` (as kairotic.engine/flat-id)."
23
+ [path id]
24
+ (if (empty? path) id (keyword (str (str/join "." (map name path)) "." (name id)))))
25
+
26
+ (defn path-of
27
+ "Back from an engine id to the ids that lead to it: :b5.b2 → [:b5 :b2]."
28
+ [f]
29
+ (mapv keyword (str/split (name f) #"\.")))
30
+
31
+ (defn configuration
32
+ "The texts of the boxes of a patch and of its embedded sub-patches, by engine id."
33
+ ([p] (configuration p []))
34
+ ([p path]
35
+ (reduce (fn [acc [id b]]
36
+ (let [acc (assoc acc (fid path id) (str (:text b)))]
37
+ (if (and (= "patch" (comp/klass (:text b))) (map? (:data b)))
38
+ (merge acc (configuration (:data b) (conj path id)))
39
+ acc)))
40
+ (sorted-map) (:boxes p))))
41
+
42
+ (defn words [text] (vec (remove str/blank? (str/split (str/trim (str text)) #"\s+"))))
43
+
44
+ (defn num
45
+ "The number a word writes, or nil."
46
+ [w]
47
+ (when (and (string? w) (re-matches #"-?(\d+\.?\d*|\.\d+)(e-?\d+)?" w)) (js/parseFloat w)))
48
+
49
+ (defn decimals
50
+ "How many decimals a number is written with."
51
+ [w]
52
+ (let [[_ d] (re-matches #"-?\d*\.(\d+)" (str w))] (count d)))
53
+
54
+ ;; ───────── controls: boxes that keep their value in their text ─────────
55
+
56
+ (defn- probe
57
+ "What a :persist function makes of x (a map of arguments), or nil if it does not take it."
58
+ [persist x]
59
+ (try (let [m (persist x)] (when (and (map? m) (seq m)) m)) (catch :default _ nil)))
60
+
61
+ (defn control
62
+ "The value a control box keeps in its text — a class with :persist and :args — as {:ks [argument …]
63
+ :v value :log bool :d definition}, or nil for any other box. The value is a number (a knob: [:initial])
64
+ or a vector of numbers (an envelope keeps [a d s r], a pad [x y]): which arguments hold it is asked of
65
+ :persist itself."
66
+ [text]
67
+ (let [d (comp/lookup (comp/klass text))]
68
+ (when (and d (fn? (:persist d)) (sequential? (:args d)))
69
+ (let [args (comp/args-for d text)]
70
+ (when (map? args)
71
+ (if-let [m (probe (:persist d) 1)]
72
+ (let [k (first (keys m)) v (get args k)]
73
+ (when (and (= 1 (count m)) (number? v))
74
+ {:ks [k] :v v :log (true? (:log args)) :d d}))
75
+ ;; a vector: the place of each argument is the number it got from [1 2 3 …]
76
+ (when-let [m (probe (:persist d) (vec (range 1 17)))]
77
+ (let [ks (mapv key (sort-by val (filter (fn [[_ x]] (number? x)) m)))
78
+ v (mapv (fn [k] (get args k)) ks)]
79
+ (when (and (= (count ks) (count m)) (every? number? v))
80
+ {:ks ks :v v :log false :d d})))))))))
81
+
82
+ (defn control-text
83
+ "The text of a control with the value v kept in it, as a drag leaves it on release."
84
+ [text v]
85
+ (when-let [{:keys [d]} (control text)]
86
+ (when-let [m (probe (:persist d) v)]
87
+ (comp/args-text d (comp/klass text) (merge (comp/args-for d text) m)))))
88
+
89
+ ;; ───────── recall: switch to a configuration ─────────
90
+
91
+ (defn recall-changes
92
+ "The boxes that are both now and in the target and whose text differs there: {id text}."
93
+ [now target]
94
+ (into (sorted-map)
95
+ (for [[f t] target :let [t0 (get now f)] :when (and (some? t0) (not= (words t0) (words t)))] [f t])))
96
+
97
+ ;; ───────── morph: between two configurations ─────────
98
+
99
+ (defn lerp
100
+ "Between a and b at t (0…1); geometric if log? and both are positive (frequencies). Vectors of numbers
101
+ (an envelope) go element by element."
102
+ [a b t log?]
103
+ (cond
104
+ (and (vector? a) (vector? b)) (mapv (fn [x y] (lerp x y t log?)) a b)
105
+ (and log? (pos? a) (pos? b)) (* a (js/Math.pow (/ b a) t))
106
+ :else (+ a (* (- b a) t))))
107
+
108
+ (defn between
109
+ "A position x among n keyframes (1 = the first) as [a b t]: halfway from 1 to 2 is 1.5 → [1 2 0.5]."
110
+ [x n]
111
+ (cond (< n 2) [1 1 0]
112
+ :else (let [x (max 1 (min n x))
113
+ a (min (dec n) (js/Math.floor x))]
114
+ [a (inc a) (- x a)])))
115
+
116
+ (defn morph-values
117
+ "The values the controls take at t between configurations a and b: {id value}. Only controls of the same
118
+ class now, in a and in b; not those in `exclude` (the boxes that drive the morph); and only those that
119
+ differ between a and b or whose text keeps another value now."
120
+ ([now a b t] (morph-values now a b t #{}))
121
+ ([now a b t exclude]
122
+ (into (sorted-map)
123
+ (for [[f tn] now
124
+ :when (not (contains? exclude f))
125
+ :let [ta (get a f) tb (get b f)]
126
+ :when (and ta tb (= (comp/klass tn) (comp/klass ta) (comp/klass tb)))
127
+ :let [cn (control tn) ca (control ta) cb (control tb)]
128
+ :when (and cn ca cb (= (:ks cn) (:ks ca) (:ks cb))
129
+ (or (not= (:v ca) (:v cb)) (not= (:v cn) (:v ca))))]
130
+ [f (lerp (:v ca) (:v cb) t (or (:log ca) (:log cb)))]))))
131
+
132
+ (defn keep-changes
133
+ "The texts that keep the values of a morph ({id value}) in the controls now: {id text}."
134
+ [now values]
135
+ (into (sorted-map)
136
+ (for [[f v] values :let [t (get now f) t2 (when t (control-text t v))]
137
+ :when (and t2 (not= (words t) (words t2)))]
138
+ [f t2])))
139
+
140
+ ;; ───────── generate: what changes and what remains ─────────
141
+
142
+ (defn variation
143
+ "What changes and what remains among configurations: for every box that is in all of them, with the same
144
+ class and as many words, one entry per word — {:same w} if it is the same in all, {:lo :hi :int :places}
145
+ for numbers that change (their range, whether all are whole, the decimals they are written with), or
146
+ {:words [w …]} for words that change. Boxes that do not fit are left out."
147
+ [cfgs]
148
+ (if (empty? cfgs)
149
+ (sorted-map)
150
+ (into (sorted-map)
151
+ (for [f (keys (first cfgs))
152
+ :let [ws (mapv (fn [c] (when-let [t (get c f)] (words t))) cfgs)]
153
+ :when (and (every? seq ws) (apply = (map count ws)) (apply = (map first ws)))]
154
+ [f (vec (for [i (range (count (first ws)))
155
+ :let [col (mapv (fn [w] (nth w i)) ws) ns (map num col)]]
156
+ (cond (apply = col) {:same (first col)}
157
+ (every? some? ns) {:lo (apply min ns) :hi (apply max ns)
158
+ :int (every? (fn [w] (zero? (decimals w))) col)
159
+ :places (apply max (map decimals col))}
160
+ :else {:words (vec (sort (distinct col)))})))]))))
161
+
162
+ (defn rng
163
+ "A generator of numbers in [0, 1) from a seed (mulberry32): the same seed, the same numbers."
164
+ [seed]
165
+ (let [s (atom (bit-or (js/Math.floor (or seed 0)) 0))]
166
+ (fn []
167
+ (let [x (swap! s (fn [x] (bit-or (+ x 0x6D2B79F5) 0)))
168
+ t (js/Math.imul (bit-xor x (unsigned-bit-shift-right x 15)) (bit-or x 1))
169
+ t (bit-xor t (+ t (js/Math.imul (bit-xor t (unsigned-bit-shift-right t 7)) (bit-or t 61))))]
170
+ (/ (unsigned-bit-shift-right (bit-xor t (unsigned-bit-shift-right t 14)) 0) 4294967296)))))
171
+
172
+ (defn- fixed [v d] (str (js/parseFloat (.toFixed v d))))
173
+
174
+ (defn- pick-word [{:keys [same lo hi int places words]} log? rand]
175
+ (cond
176
+ (some? same) same
177
+ words (nth words (min (dec (count words)) (js/Math.floor (* (rand) (count words)))))
178
+ :else (let [u (rand)
179
+ v (lerp lo hi u (and log? (pos? lo)))]
180
+ (if int (str (js/Math.round v)) (fixed v (inc places))))))
181
+
182
+ (defn generate-changes
183
+ "A new configuration from what changes and what remains among `cfgs`, for the boxes that are now: what is
184
+ the same in all of them stays, numbers that change take a value within their range (geometric in a box
185
+ written with log), words that change are one of theirs. `rand` gives numbers in [0, 1). {id text}."
186
+ [now cfgs rand]
187
+ (into (sorted-map)
188
+ (for [[f spec] (variation cfgs)
189
+ :let [t0 (get now f)]
190
+ :when (some? t0)
191
+ :let [log? (some (fn [s] (= "log" (:same s))) spec)
192
+ ws (mapv (fn [s] (pick-word s log? rand)) spec)]
193
+ :when (not= ws (words t0))]
194
+ [f (str/join " " ws)])))
195
+
196
+ ;; ───────── the box ─────────
197
+
198
+ (defn- ask [x] {:state nil :effects [[:presets x]]})
199
+
200
+ (comp/define! "presets"
201
+ {:doc "Presets from the keyframes of the history (◆ snapshots, takes kept): a number or a label recalls one, a position on the right morphs between two, a bang makes a new one from what changes and what remains."
202
+ :usage "presets [label or number …]"
203
+ :io [2 1]
204
+ ;; presets kept in the box (issue #45): part of the patch, so they travel in its code, links and embeds.
205
+ ;; A box that keeps presets uses them instead of the keyframes of the history.
206
+ :model {:empty {:presets []}
207
+ :step (fn [d [op x]] (case op :store {:presets (rel/sane-presets x)} :clear {:presets []} d))
208
+ :load (fn [x] {:presets (rel/sane-presets (:presets x))})
209
+ :code (fn [id d] (when (seq (:presets d)) [(str "(presets! " id " " (pr-str (:presets d)) ")")]))}
210
+ :receive (fn [{:keys [inlet value state]}]
211
+ (if (= inlet 1)
212
+ (cond (not (number? value)) {}
213
+ ;; the same position again changes nothing
214
+ (= value (:x state)) {}
215
+ :else {:state {:x value} :effects [[:presets [:morph-at value]]]})
216
+ (cond
217
+ (number? value) (if (integer? value)
218
+ (ask [:recall value])
219
+ {:effects [[:print "presets: a position between keyframes goes to the right inlet (1.5: halfway from 1 to 2)"]]})
220
+ (= value :bang) (ask [:generate nil])
221
+ (= value :keep) (ask [:keep])
222
+ (or (keyword? value) (string? value) (symbol? value)) (ask [:recall (if (symbol? value) (str value) (name value))])
223
+ (and (vector? value) (= :recall (first value))) (ask [:recall (second value)])
224
+ (and (vector? value) (= :generate (first value))) (ask [:generate (second value)])
225
+ (and (vector? value) (= :morph (first value)) (= 4 (count value)) (number? (nth value 3)))
226
+ (ask (into [:morph] (rest value)))
227
+ :else {:effects [[:print (str "presets: " (pr-str value) " is not understood: a number or a label recalls, bang generates, [:morph a b t], keep")]]})))})