@kairotic/app 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -2
- package/index.html +1101 -0
- package/kairotic/api.cljs +362 -0
- package/kairotic/assistant.cljs +505 -0
- package/kairotic/backends/gun-backend.js +371 -0
- package/kairotic/canvas.cljs +751 -0
- package/kairotic/code.cljs +108 -0
- package/kairotic/components.cljs +838 -0
- package/kairotic/core.cljs +96 -0
- package/kairotic/corefns.cljs +473 -0
- package/kairotic/docs.mjs +2 -0
- package/kairotic/engine.cljs +482 -0
- package/kairotic/examples.mjs +96 -0
- package/kairotic/faust.cljs +225 -0
- package/kairotic/help.cljs +1036 -0
- package/kairotic/hiccup.cljs +566 -0
- package/kairotic/input.cljs +271 -0
- package/kairotic/kfile.mjs +138 -0
- package/kairotic/library.cljs +214 -0
- package/kairotic/links.cljs +41 -0
- package/kairotic/mt.cljs +1604 -0
- package/kairotic/mtboxes.cljs +276 -0
- package/kairotic/packages/analysis.mjs +64 -0
- package/kairotic/packages/controls.mjs +28 -0
- package/kairotic/packages/data.mjs +22 -0
- package/kairotic/packages/faust-units/cmaes.mjs +82 -0
- package/kairotic/packages/faust-units/graph.mjs +212 -0
- package/kairotic/packages/faust-units/listen.mjs +125 -0
- package/kairotic/packages/faust-units/notation.mjs +139 -0
- package/kairotic/packages/faust-units/parse.mjs +439 -0
- package/kairotic/packages/faust-units/recipe.mjs +195 -0
- package/kairotic/packages/faust-units/render.mjs +69 -0
- package/kairotic/packages/faust-units/units.mjs +130 -0
- package/kairotic/packages/faust-units/view.mjs +353 -0
- package/kairotic/packages/faust-units.mjs +9 -0
- package/kairotic/packages/io.mjs +58 -0
- package/kairotic/packages/net.mjs +28 -0
- package/kairotic/packages/patterns.mjs +34 -0
- package/kairotic/packages/spatial.mjs +22 -0
- package/kairotic/packages/spectral/shift.mjs +169 -0
- package/kairotic/packages/spectral.mjs +16 -0
- package/kairotic/packages/synth.mjs +28 -0
- package/kairotic/packages/theory/counterpoint.mjs +798 -0
- package/kairotic/packages/theory/mirror.mjs +221 -0
- package/kairotic/packages/theory.mjs +46 -0
- package/kairotic/patterns.cljs +538 -0
- package/kairotic/presets.cljs +227 -0
- package/kairotic/quantize.cljs +782 -0
- package/kairotic/record.cljs +169 -0
- package/kairotic/release.cljs +90 -0
- package/kairotic/ring.cljs +129 -0
- package/kairotic/ringaudio.cljs +232 -0
- package/kairotic/ringcore.mjs +270 -0
- package/kairotic/score.cljs +176 -0
- package/kairotic/shapes.cljs +283 -0
- package/kairotic/signal.cljs +327 -0
- package/kairotic/sounds.cljs +565 -0
- package/kairotic/sounds.mjs +108 -0
- package/kairotic/span.cljs +129 -0
- package/kairotic/spell.cljs +281 -0
- package/kairotic/structure.cljs +145 -0
- package/kairotic/talk.cljs +115 -0
- package/kairotic/tempo.cljs +226 -0
- package/kairotic/treecanvas.cljs +299 -0
- package/kairotic/tutorials.mjs +1283 -0
- package/kairotic/vendor/codemirror.mjs +23 -0
- package/package.json +23 -4
- package/page/00-work.cljs +862 -0
- package/page/01-commands.cljs +1219 -0
- package/page/02-patch-view.cljs +1051 -0
- package/page/03-history-view.cljs +481 -0
- package/page/04-panels.cljs +448 -0
- package/page/05-gestures.cljs +1247 -0
- package/page/06-connection.cljs +219 -0
- package/page/07-claude.cljs +430 -0
- package/page/08-sessions.cljs +423 -0
- package/page/09-learn.cljs +602 -0
- package/page/10-embed.cljs +469 -0
- package/page/11-startup.cljs +61 -0
- package/page/12-feedback.cljs +172 -0
- package/page/13-perform.cljs +325 -0
- package/page/14-editor.cljs +656 -0
- package/page/15-presets.cljs +230 -0
- package/page/16-release.cljs +242 -0
- package/page/17-sounds.cljs +147 -0
- package/page/18-lines.cljs +594 -0
- package/page/19-files.cljs +247 -0
- package/page/19-html.cljs +152 -0
- package/page/20-talk.cljs +527 -0
- package/page/21-net.cljs +110 -0
- package/page/22-learn.cljs +303 -0
- package/page/23-ids.cljs +233 -0
- package/page/24-clipboard.cljs +136 -0
- package/page/25-tidy.cljs +74 -0
- package/page/26-probes.cljs +111 -0
- package/sources.mjs +2 -0
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
;; SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
;; Kairotic · patterns
|
|
3
|
+
;; Patterns after SuperCollider's (Pbind, Pseq, Pwhite…), on Clojure's lazy sequences as Overtone does
|
|
4
|
+
;; (discussion #84). A pattern is a (possibly infinite) lazy sequence of event maps:
|
|
5
|
+
;; {:degree 2 :dur 1/2 :amp 0.3} → played by a `player` box (@kairotic/patterns)
|
|
6
|
+
;;
|
|
7
|
+
;; Two layers, because the history, rooms and links of a work travel as data and a lazy seq does not:
|
|
8
|
+
;; · the source travels: the text of a `pbind` (EDN) or a `pattern` (code) box, and a seed;
|
|
9
|
+
;; · the stream is local: each tab makes the sequence from the source. The same seed gives the same
|
|
10
|
+
;; music everywhere, and going back in the history plays the same again.
|
|
11
|
+
;; What goes along a line is a pattern value, a factory of streams ({:kairotic/pattern true :make (fn [] …)}),
|
|
12
|
+
;; never the sequence itself, so nothing infinite is printed or kept.
|
|
13
|
+
;;
|
|
14
|
+
;; A pbind is a map: a plain value is a constant, a vector a sequence (the shorter ones start again until
|
|
15
|
+
;; the longest ends; all of them, `repeat` times), and an EDN tagged literal #pat/… a generator:
|
|
16
|
+
;; #pat/seq [a b c] the same as a vector #pat/choose [a b c] one at random, each time
|
|
17
|
+
;; #pat/shuffle [a b c] every pass in a new order #pat/white [lo hi] random between lo and hi
|
|
18
|
+
;; #pat/walk [x step lo hi] a random walk #pat/series [x step] or [x step n]
|
|
19
|
+
;; #pat/hold [beats [a b]] one value per `beats` #pat/chord [0 4 7] a vector as one value
|
|
20
|
+
;; A generator can be an element of a vector: [1 #pat/choose [2 3]]. Each key has its own seed, made of
|
|
21
|
+
;; the box's seed and its name, so changing :dur does not change the randomness of :amp.
|
|
22
|
+
|
|
23
|
+
(ns kairotic.patterns
|
|
24
|
+
(:require [clojure.string :as str]
|
|
25
|
+
[clojure.edn :as edn]
|
|
26
|
+
[kairotic.score :as sc]
|
|
27
|
+
[kairotic.canvas :as cv]
|
|
28
|
+
[kairotic.code :as code]))
|
|
29
|
+
|
|
30
|
+
(def max-period
|
|
31
|
+
"The longest finite sequence a pattern treats as one (a lazy seq longer than this is a stream)."
|
|
32
|
+
4096)
|
|
33
|
+
|
|
34
|
+
;; ───────── seeded randomness ─────────
|
|
35
|
+
|
|
36
|
+
(defn rng
|
|
37
|
+
"A generator of numbers in [0, 1) from a seed (any value): the same seed gives the same numbers in
|
|
38
|
+
every tab (mulberry32 over the hash of the seed)."
|
|
39
|
+
[seed]
|
|
40
|
+
(let [s (volatile! (bit-or (hash seed) 0))]
|
|
41
|
+
(fn []
|
|
42
|
+
(let [a (vswap! s (fn [a] (bit-or (+ a 0x6D2B79F5) 0)))
|
|
43
|
+
t (js/Math.imul (bit-xor a (unsigned-bit-shift-right a 15)) (bit-or a 1))
|
|
44
|
+
t (bit-xor (+ t (js/Math.imul (bit-xor t (unsigned-bit-shift-right t 7)) (bit-or t 61))) t)]
|
|
45
|
+
(/ (unsigned-bit-shift-right (bit-xor t (unsigned-bit-shift-right t 14)) 0) 4294967296)))))
|
|
46
|
+
|
|
47
|
+
(defn whole? [x] (and (number? x) (== x (js/Math.floor x))))
|
|
48
|
+
|
|
49
|
+
(defn rand-in
|
|
50
|
+
"A random number between lo and hi from r: a whole one (both ends included) when both are whole."
|
|
51
|
+
[r lo hi]
|
|
52
|
+
(if (and (whole? lo) (whole? hi))
|
|
53
|
+
(+ (min lo hi) (js/Math.floor (* (r) (inc (js/Math.abs (- hi lo))))))
|
|
54
|
+
(+ lo (* (r) (- hi lo)))))
|
|
55
|
+
|
|
56
|
+
(defn pick "One element of the vector xs, at random." [r xs] (nth xs (js/Math.floor (* (r) (count xs)))))
|
|
57
|
+
|
|
58
|
+
(defn shuffled "The vector xs in a random order (Fisher–Yates)." [r xs]
|
|
59
|
+
(loop [v (vec xs) i (dec (count xs))]
|
|
60
|
+
(if (pos? i)
|
|
61
|
+
(let [j (js/Math.floor (* (r) (inc i)))]
|
|
62
|
+
(recur (assoc v i (nth v j) j (nth v i)) (dec i)))
|
|
63
|
+
v)))
|
|
64
|
+
|
|
65
|
+
;; ───────── generators: #pat/… in EDN ─────────
|
|
66
|
+
|
|
67
|
+
(def gen-names #{:seq :choose :shuffle :white :walk :series :hold :chord})
|
|
68
|
+
|
|
69
|
+
(defn gen "A generator as data." [g args] {:pat/gen g :pat/args args})
|
|
70
|
+
(defn gen? [x] (and (map? x) (contains? x :pat/gen)))
|
|
71
|
+
|
|
72
|
+
(def readers (into {} (for [g gen-names] [(symbol "pat" (name g)) (fn [x] (gen g x))])))
|
|
73
|
+
|
|
74
|
+
(defn read-spec
|
|
75
|
+
"The text of a pbind as data: #pat/… become generators. Throws on bad EDN or an unknown tag."
|
|
76
|
+
[source]
|
|
77
|
+
(edn/read-string {:readers readers
|
|
78
|
+
:default (fn [tag _]
|
|
79
|
+
(throw (js/Error. (str "unknown tag #" tag " (the generators are "
|
|
80
|
+
(str/join " " (map (fn [g] (str "#pat/" (name g))) (sort gen-names))) ")"))))}
|
|
81
|
+
(str source)))
|
|
82
|
+
|
|
83
|
+
(defn- nums? [xs n] (and (sequential? xs) (= n (count xs)) (every? number? xs)))
|
|
84
|
+
|
|
85
|
+
(defn gen-problem
|
|
86
|
+
"What is wrong with a generator's arguments, or nil."
|
|
87
|
+
[{g :pat/gen a :pat/args}]
|
|
88
|
+
(let [tag (str "#pat/" (name g))]
|
|
89
|
+
(case g
|
|
90
|
+
(:seq :choose :shuffle :chord) (when-not (and (sequential? a) (seq a)) (str tag " takes a vector of values: " tag " [0 2 4]"))
|
|
91
|
+
:white (when-not (nums? a 2) (str tag " takes [lo hi]: " tag " [0 7]"))
|
|
92
|
+
:walk (when-not (nums? a 4) (str tag " takes [start step lo hi]: " tag " [0 2 -7 7]"))
|
|
93
|
+
:series (when-not (or (nums? a 2) (nums? a 3)) (str tag " takes [start step] or [start step n]: " tag " [0 2 8]"))
|
|
94
|
+
:hold (when-not (and (sequential? a) (= 2 (count a)) (number? (first a)) (pos? (first a))
|
|
95
|
+
(sequential? (second a)) (seq (second a)))
|
|
96
|
+
(str tag " takes [beats [values]]: " tag " [4 [:c :g :d]]"))
|
|
97
|
+
(str "unknown generator " tag))))
|
|
98
|
+
|
|
99
|
+
(defn problems
|
|
100
|
+
"What is wrong with a pbind map (a list of reasons, empty if nothing)."
|
|
101
|
+
[m]
|
|
102
|
+
(if-not (map? m)
|
|
103
|
+
["a pbind is a map {:key values …}: {:degree [0 2 4] :dur 1/2}"]
|
|
104
|
+
(concat
|
|
105
|
+
(for [k (keys m) :when (not (keyword? k))] (str "the keys of a pbind are keywords (" (pr-str k) " is not)"))
|
|
106
|
+
(->> (tree-seq (fn [x] (or (map? x) (sequential? x))) (fn [x] (if (map? x) (if (gen? x) [(:pat/args x)] (vals x)) x)) m)
|
|
107
|
+
(filter gen?)
|
|
108
|
+
(keep gen-problem)))))
|
|
109
|
+
|
|
110
|
+
;; ───────── streams: one per key ─────────
|
|
111
|
+
;; stream-of gives {:stream (lazy, infinite) :period n-or-nil}: a finite sequence of n values cycles (so a
|
|
112
|
+
;; shorter one starts again), an endless one has no period; a constant is :const and a hold is :held.
|
|
113
|
+
|
|
114
|
+
(declare stream-of)
|
|
115
|
+
|
|
116
|
+
(defn rng-for [ctx] (rng [(:seed ctx) (:key ctx) (:pass ctx) (:path ctx)]))
|
|
117
|
+
|
|
118
|
+
(defn- elements
|
|
119
|
+
"The values of a vector, nested vectors flattened (as Overtone's pbind does); generators and lazy
|
|
120
|
+
sequences stay one element each."
|
|
121
|
+
[xs]
|
|
122
|
+
(mapcat (fn [x] (if (vector? x) (elements x) [x])) xs))
|
|
123
|
+
|
|
124
|
+
(defn- cycle-or-stream
|
|
125
|
+
"A lazy seq as a stream: a finite one round and round, an endless one as it is."
|
|
126
|
+
[s]
|
|
127
|
+
(if (<= (bounded-count (inc max-period) s) max-period) (if (seq s) (cycle s) (repeat nil)) s))
|
|
128
|
+
|
|
129
|
+
(defn- taker
|
|
130
|
+
"A function that gives the next value of an element each time it is called: a generator or a lazy seq
|
|
131
|
+
gives one of its values, anything else itself."
|
|
132
|
+
[x ctx]
|
|
133
|
+
(cond (gen? x) (let [s (volatile! (:stream (stream-of x ctx)))] (fn [] (let [v (first @s)] (vswap! s rest) v)))
|
|
134
|
+
(seq? x) (let [s (volatile! (cycle-or-stream x))] (fn [] (let [v (first @s)] (vswap! s rest) v)))
|
|
135
|
+
:else (constantly x)))
|
|
136
|
+
|
|
137
|
+
(defn- takers [xs ctx] (vec (map-indexed (fn [i x] (taker x (update ctx :path conj i))) xs)))
|
|
138
|
+
|
|
139
|
+
(defn- cycling
|
|
140
|
+
"The elements of a vector, round and round (a generator in it gives a new value each time it comes round)."
|
|
141
|
+
[xs ctx]
|
|
142
|
+
(let [ts (takers xs ctx) n (count ts)]
|
|
143
|
+
{:stream (map (fn [i] ((nth ts (mod i n)))) (range)) :period n}))
|
|
144
|
+
|
|
145
|
+
(defn stream-of
|
|
146
|
+
"The values of one key of a pbind, given ctx {:seed :key :pass :path}."
|
|
147
|
+
[x ctx]
|
|
148
|
+
(let [ctx (update ctx :path (fnil identity []))]
|
|
149
|
+
(cond
|
|
150
|
+
(gen? x)
|
|
151
|
+
(let [a (:pat/args x)]
|
|
152
|
+
(case (:pat/gen x)
|
|
153
|
+
:seq (stream-of (vec a) ctx)
|
|
154
|
+
:chord {:stream (repeat (vec a)) :const true}
|
|
155
|
+
:choose (let [r (rng-for ctx) ts (takers (vec (elements a)) ctx)]
|
|
156
|
+
{:stream (repeatedly (fn [] ((pick r ts))))})
|
|
157
|
+
:shuffle (let [r (rng-for ctx) ts (takers (vec (elements a)) ctx)
|
|
158
|
+
pass (fn pass [] (lazy-seq (concat (map (fn [t] (t)) (shuffled r ts)) (pass))))]
|
|
159
|
+
{:stream (pass) :period (count ts)})
|
|
160
|
+
:white (let [r (rng-for ctx) [lo hi] a] {:stream (repeatedly (fn [] (rand-in r lo hi)))})
|
|
161
|
+
:walk (let [r (rng-for ctx) [x0 step lo hi] a
|
|
162
|
+
lo' (min lo hi) hi' (max lo hi)
|
|
163
|
+
reflect (fn [x] (cond (> x hi') (max lo' (- hi' (- x hi'))) (< x lo') (min hi' (+ lo' (- lo' x))) :else x))]
|
|
164
|
+
{:stream (iterate (fn [x] (reflect (+ x (rand-in r (- step) step)))) x0)})
|
|
165
|
+
:series (let [[x0 step n] a s (iterate (fn [x] (+ x step)) x0)]
|
|
166
|
+
(if (and (number? n) (pos? n))
|
|
167
|
+
{:stream (cycle (take (js/Math.round n) s)) :period (js/Math.round n)}
|
|
168
|
+
{:stream s}))
|
|
169
|
+
:hold (let [[beats xs] a] {:held {:beats beats :values (vec (elements xs))}})
|
|
170
|
+
(throw (js/Error. (str "unknown generator #pat/" (name (:pat/gen x)))))))
|
|
171
|
+
(vector? x) (let [xs (vec (elements x))] (if (seq xs) (cycling xs ctx) {:stream (repeat nil) :const true}))
|
|
172
|
+
;; a lazy seq (from code): finite ones cycle like a vector, endless ones are a stream as they are
|
|
173
|
+
(seq? x) (if (<= (bounded-count (inc max-period) x) max-period)
|
|
174
|
+
(if (seq x) (cycling (vec x) ctx) {:stream (repeat nil) :const true})
|
|
175
|
+
{:stream x})
|
|
176
|
+
:else {:stream (repeat x) :const true})))
|
|
177
|
+
|
|
178
|
+
(defn static-period
|
|
179
|
+
"How many events one key gives in a pass, without making it: a number, nil (endless), :const or :held."
|
|
180
|
+
[x]
|
|
181
|
+
(cond
|
|
182
|
+
(gen? x) (case (:pat/gen x)
|
|
183
|
+
(:seq) (static-period (vec (:pat/args x)))
|
|
184
|
+
(:shuffle) (count (elements (:pat/args x)))
|
|
185
|
+
(:series) (let [n (nth (:pat/args x) 2 nil)] (if (and (number? n) (pos? n)) (js/Math.round n) nil))
|
|
186
|
+
(:chord) :const
|
|
187
|
+
(:hold) :held
|
|
188
|
+
nil)
|
|
189
|
+
(vector? x) (if (seq x) (count (elements x)) :const)
|
|
190
|
+
(seq? x) (let [n (bounded-count (inc max-period) x)] (if (<= n max-period) (if (pos? n) n :const) nil))
|
|
191
|
+
:else :const))
|
|
192
|
+
|
|
193
|
+
;; ───────── events ─────────
|
|
194
|
+
|
|
195
|
+
(defn dur-of "The length of an event in beats (:dur, 1 if it has none)." [e]
|
|
196
|
+
(let [d (:dur e)] (if (and (number? d) (>= d 0) (js/isFinite d)) d 1)))
|
|
197
|
+
|
|
198
|
+
(defn sustain
|
|
199
|
+
"How long the notes of an event sound, in beats: :sustain, or :dur × :legato (0.8)."
|
|
200
|
+
[e]
|
|
201
|
+
(let [s (:sustain e) l (:legato e)]
|
|
202
|
+
(if (and (number? s) (pos? s)) s (* (dur-of e) (if (number? l) l 0.8)))))
|
|
203
|
+
|
|
204
|
+
(defn- timed
|
|
205
|
+
"The events, each map with its #pat/hold keys filled in for the beat it starts on."
|
|
206
|
+
[rows holds]
|
|
207
|
+
(if (empty? holds)
|
|
208
|
+
rows
|
|
209
|
+
(letfn [(step [rows beat]
|
|
210
|
+
(lazy-seq
|
|
211
|
+
(when-let [e (first rows)]
|
|
212
|
+
(let [e (reduce (fn [e [k {:keys [beats values]}]]
|
|
213
|
+
(assoc e k (nth values (mod (js/Math.floor (/ (+ beat 1e-9) beats)) (count values)))))
|
|
214
|
+
e holds)]
|
|
215
|
+
(cons e (step (rest rows) (+ beat (dur-of e))))))))]
|
|
216
|
+
(step rows 0))))
|
|
217
|
+
|
|
218
|
+
(defn pbind-events
|
|
219
|
+
"The events of the pbind map m: a lazy seq of maps. opts {:seed :repeat} (repeat a number or :inf). As
|
|
220
|
+
in Overtone, a pattern with an endless key is endless; otherwise one pass lasts as long as its longest
|
|
221
|
+
sequence, and there are `repeat` passes."
|
|
222
|
+
[m {:keys [seed repeat]}]
|
|
223
|
+
(let [ks (sort-by str (keys m))
|
|
224
|
+
holds (keep (fn [k] (let [v (get m k)] (when (and (gen? v) (= :hold (:pat/gen v))) [k (:held (stream-of v {:key k}))]))) ks)
|
|
225
|
+
streamed (remove (set (map first holds)) ks)
|
|
226
|
+
periods (map (fn [k] (static-period (get m k))) streamed)
|
|
227
|
+
seqs (remove #{:const} periods)
|
|
228
|
+
endless? (some nil? seqs)
|
|
229
|
+
n (if (seq seqs) (apply max seqs) 1)
|
|
230
|
+
one-pass (fn [p]
|
|
231
|
+
(let [ss (map (fn [k] (:stream (stream-of (get m k) {:seed seed :key k :pass p}))) streamed)]
|
|
232
|
+
(if (seq ss)
|
|
233
|
+
(apply map (fn [& vs] (zipmap streamed vs)) ss)
|
|
234
|
+
(repeat {}))))
|
|
235
|
+
rows (cond endless? (one-pass 0)
|
|
236
|
+
(= repeat :inf) (letfn [(passes [p] (lazy-seq (concat (take n (one-pass p)) (passes (inc p)))))] (passes 0))
|
|
237
|
+
:else (mapcat (fn [p] (take n (one-pass p))) (range (max 1 (or repeat 1)))))]
|
|
238
|
+
(timed rows holds)))
|
|
239
|
+
|
|
240
|
+
(defn summary
|
|
241
|
+
"A few words about a pbind map: its keys and how many events a pass has."
|
|
242
|
+
[m]
|
|
243
|
+
(let [periods (keep (fn [[_ v]] (let [p (static-period v)] (when-not (#{:const :held} p) p))) m)
|
|
244
|
+
pass (cond (some nil? periods) "endless"
|
|
245
|
+
(seq periods) (str (apply max periods) " events a pass")
|
|
246
|
+
:else "1 event")]
|
|
247
|
+
(str (count m) (if (= 1 (count m)) " key · " " keys · ") pass)))
|
|
248
|
+
|
|
249
|
+
;; ───────── pattern values: what travels along a line ─────────
|
|
250
|
+
|
|
251
|
+
(defn pattern
|
|
252
|
+
"A pattern value: make gives a new stream (a lazy seq of event maps) each time; key tells whether two
|
|
253
|
+
patterns are the same (the same source and seed)."
|
|
254
|
+
[make key info]
|
|
255
|
+
{:kairotic/pattern true :key key :make make :summary info})
|
|
256
|
+
|
|
257
|
+
(defn pattern? [x] (and (map? x) (true? (:kairotic/pattern x))))
|
|
258
|
+
|
|
259
|
+
(defn events "A new stream of the pattern p." [p] ((:make p)))
|
|
260
|
+
|
|
261
|
+
(defn pbind-pattern
|
|
262
|
+
"The pattern of a pbind map. opts {:seed n :repeat n|:inf}."
|
|
263
|
+
[m opts]
|
|
264
|
+
(let [opts {:seed (or (:seed opts) 0) :repeat (or (:repeat opts) 1)}]
|
|
265
|
+
(pattern (fn [] (pbind-events m opts)) [:pbind m (:seed opts) (:repeat opts)] (summary m))))
|
|
266
|
+
|
|
267
|
+
;; ───────── patterns as code ─────────
|
|
268
|
+
;; The source of a `pattern` box is a Clojure expression evaluated inside
|
|
269
|
+
;; (fn [{:keys [rand rand-int rand-nth shuffle pbind pseq pwhite …]}] …)
|
|
270
|
+
;; so its randomness is seeded, and the pattern functions are at hand (no other name of clojure.core is
|
|
271
|
+
;; replaced: macros such as `for` expand into them). It returns a sequence of event
|
|
272
|
+
;; maps (nested sequences are flattened, as phrases) or one map, which is read as a pbind.
|
|
273
|
+
|
|
274
|
+
(defn code-env
|
|
275
|
+
"The names a pattern's code sees, with their randomness from r."
|
|
276
|
+
[r seed rep]
|
|
277
|
+
(let [pb (fn ([m] (pbind-events m {:seed seed :repeat rep})) ([m n] (pbind-events m {:seed seed :repeat (if (or (= n :inf) (= n js/Infinity)) :inf n)})))]
|
|
278
|
+
{:rand (fn ([] (r)) ([n] (* n (r))))
|
|
279
|
+
:rand-int (fn [n] (js/Math.floor (* n (r))))
|
|
280
|
+
:rand-nth (fn [xs] (pick r (vec xs)))
|
|
281
|
+
:shuffle (fn [xs] (shuffled r (vec xs)))
|
|
282
|
+
:pbind pb
|
|
283
|
+
:pseq (fn ([xs] (seq xs)) ([xs n] (if (or (= n :inf) (= n js/Infinity)) (cycle xs) (take (* n (count xs)) (cycle xs)))))
|
|
284
|
+
:pwhite (fn ([lo hi] (repeatedly (fn [] (rand-in r lo hi)))) ([lo hi n] (take n (repeatedly (fn [] (rand-in r lo hi))))))
|
|
285
|
+
:pchoose (fn [xs] (let [v (vec xs)] (repeatedly (fn [] (pick r v)))))
|
|
286
|
+
:pshuffle (fn [xs] (let [v (vec xs)] ((fn pass [] (lazy-seq (concat (shuffled r v) (pass)))))))
|
|
287
|
+
:pseries (fn ([x step] (iterate (fn [x] (+ x step)) x)) ([x step n] (take n (iterate (fn [x] (+ x step)) x))))
|
|
288
|
+
:pwalk (fn [x0 step lo hi] (:stream (stream-of (gen :walk [x0 step lo hi]) {:seed [seed (r)]})))}))
|
|
289
|
+
|
|
290
|
+
(def code-names (sort (map name (keys (code-env (constantly 0) 0 1)))))
|
|
291
|
+
|
|
292
|
+
(defn flat-events
|
|
293
|
+
"The event maps of what a pattern's code returned: a map is one pbind, a sequence is flattened."
|
|
294
|
+
[x seed rep]
|
|
295
|
+
(letfn [(fl [x] (cond (map? x) [x] (sequential? x) (mapcat fl x) :else []))]
|
|
296
|
+
(if (map? x)
|
|
297
|
+
(pbind-events x {:seed seed :repeat rep})
|
|
298
|
+
(fl x))))
|
|
299
|
+
|
|
300
|
+
(defn code-pattern
|
|
301
|
+
"The pattern of a compiled `pattern` source f. opts {:seed :repeat :source}."
|
|
302
|
+
[f opts]
|
|
303
|
+
(let [seed (or (:seed opts) 0) rep (or (:repeat opts) 1)]
|
|
304
|
+
(pattern (fn [] (flat-events (f (code-env (rng [seed :code]) seed rep)) seed rep))
|
|
305
|
+
[:code (:source opts) seed rep]
|
|
306
|
+
"code")))
|
|
307
|
+
|
|
308
|
+
;; ───────── pitch: what an event plays ─────────
|
|
309
|
+
|
|
310
|
+
(def scales
|
|
311
|
+
{:major [0 2 4 5 7 9 11] :ionian [0 2 4 5 7 9 11] :minor [0 2 3 5 7 8 10] :aeolian [0 2 3 5 7 8 10]
|
|
312
|
+
:dorian [0 2 3 5 7 9 10] :phrygian [0 1 3 5 7 8 10] :lydian [0 2 4 6 7 9 11] :mixolydian [0 2 4 5 7 9 10]
|
|
313
|
+
:locrian [0 1 3 5 6 8 10] :harmonic-minor [0 2 3 5 7 8 11] :melodic-minor [0 2 3 5 7 9 11]
|
|
314
|
+
:pentatonic [0 2 4 7 9] :major-pentatonic [0 2 4 7 9] :minor-pentatonic [0 3 5 7 10] :blues [0 3 5 6 7 10]
|
|
315
|
+
:whole-tone [0 2 4 6 8 10] :chromatic (vec (range 12)) :octatonic [0 2 3 5 6 8 9 11]})
|
|
316
|
+
|
|
317
|
+
(def rest-words #{:rest :_ :- :r "rest" "_" "-" "r"})
|
|
318
|
+
|
|
319
|
+
(defn rest?
|
|
320
|
+
"Is the event a silence? {:type :rest}, :rest true, or :rest (:_ :- :r) or nil as its note, degree or freq."
|
|
321
|
+
[e]
|
|
322
|
+
(boolean (or (= :rest (:type e)) (true? (:rest e))
|
|
323
|
+
(some (fn [k] (and (contains? e k) (let [v (get e k)] (or (nil? v) (contains? rest-words v)))))
|
|
324
|
+
[:note :midinote :degree :freq]))))
|
|
325
|
+
|
|
326
|
+
(defn pitch-class
|
|
327
|
+
"A pitch class: a number, or a name (:c :eb :f# \"Bb\") → 0…11 (nil if it is not one)."
|
|
328
|
+
[x]
|
|
329
|
+
(cond (number? x) x
|
|
330
|
+
(or (keyword? x) (string? x) (symbol? x))
|
|
331
|
+
(let [m (re-matches #"(?i)([a-g])(#|s|b)?" (if (symbol? x) (str x) (name x)))]
|
|
332
|
+
(when m (+ (get sc/pcs (str/lower-case (nth m 1))) (case (nth m 2) "#" 1 "s" 1 "S" 1 "b" -1 "B" -1 0))))
|
|
333
|
+
:else nil))
|
|
334
|
+
|
|
335
|
+
(defn scale-of [x]
|
|
336
|
+
(cond (and (sequential? x) (seq x) (every? number? x)) (vec x)
|
|
337
|
+
(or (keyword? x) (string? x) (symbol? x)) (get scales (keyword (if (keyword? x) (name x) (str x))) (:major scales))
|
|
338
|
+
:else (:major scales)))
|
|
339
|
+
|
|
340
|
+
(defn degree->midi
|
|
341
|
+
"A scale degree (0 = the root; fractional degrees glide between steps) as a MIDI pitch, as in
|
|
342
|
+
SuperCollider's default event: :scale (a name or semitones), :root (a pitch class), :octave (5 → c4)."
|
|
343
|
+
[d {:keys [scale root octave]}]
|
|
344
|
+
(let [sc (scale-of scale) n (count sc)
|
|
345
|
+
f (js/Math.floor d) frac (- d f)
|
|
346
|
+
o (js/Math.floor (/ f n)) i (mod f n)
|
|
347
|
+
s (+ (nth sc i) (* 12 o))
|
|
348
|
+
s2 (if (pos? frac) (+ (if (< (inc i) n) (nth sc (inc i)) (+ 12 (first sc))) (* 12 o)) s)]
|
|
349
|
+
(+ (* 12 (if (number? octave) octave 5)) (or (pitch-class root) 0) s (* frac (- s2 s)))))
|
|
350
|
+
|
|
351
|
+
(defn- as-vec [x] (if (sequential? x) (vec x) [x]))
|
|
352
|
+
|
|
353
|
+
(defn note->midi
|
|
354
|
+
"A note as a MIDI pitch: a number, \"c4\" or :c4, or a name without octave (:c) in :octave."
|
|
355
|
+
[x octave]
|
|
356
|
+
(or (sc/pitch x)
|
|
357
|
+
(when-let [pc (and (not (number? x)) (pitch-class x))] (+ (* 12 (if (number? octave) octave 5)) pc))))
|
|
358
|
+
|
|
359
|
+
(defn midis
|
|
360
|
+
"The MIDI pitches an event plays (a vector, fractional for microtones), [] for a rest, nil when it says
|
|
361
|
+
nothing about pitch. :freq is in Hz, so it gives nil here (see hz)."
|
|
362
|
+
[e]
|
|
363
|
+
(let [tr (if (number? (:transpose e)) (:transpose e) 0)]
|
|
364
|
+
(cond (rest? e) []
|
|
365
|
+
(some? (:freq e)) nil
|
|
366
|
+
(some? (or (:midinote e) (:note e)))
|
|
367
|
+
(vec (keep (fn [x] (some-> (note->midi x (:octave e)) (+ tr))) (as-vec (or (:midinote e) (:note e)))))
|
|
368
|
+
(some? (:degree e))
|
|
369
|
+
(mapv (fn [d] (+ tr (degree->midi d e))) (filter number? (as-vec (:degree e))))
|
|
370
|
+
:else nil)))
|
|
371
|
+
|
|
372
|
+
(defn hz
|
|
373
|
+
"The frequencies an event plays, in Hz: [] for a rest, nil when it has no pitch."
|
|
374
|
+
[e]
|
|
375
|
+
(cond (rest? e) []
|
|
376
|
+
(some? (:freq e)) (vec (filter number? (as-vec (:freq e))))
|
|
377
|
+
:else (some->> (midis e) (mapv (fn [m] (cv/round-to (cv/midi->hz m) 3))))))
|
|
378
|
+
|
|
379
|
+
;; ───────── render: a stretch of a pattern as a musical tree (discussion #93) ─────────
|
|
380
|
+
;; What a player at a position would play, worked out at once. A time map says which moment of the pattern
|
|
381
|
+
;; (y, its beats) is at each moment of the result (x, beats): the diagonal from `from` to `to` by default,
|
|
382
|
+
;; or the strokes of a canvas — a steeper line is faster, a flatter one slower, a line going down plays
|
|
383
|
+
;; backwards (each note from its end), a flat stretch holds the note it is in, a vertical step is a cut,
|
|
384
|
+
;; and several strokes are several layers of the same pattern (a canon, a canon by augmentation).
|
|
385
|
+
;; Every event is a span, [onset, onset + sustain): it sounds while the map's y is inside it, so a note of
|
|
386
|
+
;; the result lasts from where the map enters its span to where it leaves it. The result is a tree, so a
|
|
387
|
+
;; freqt shows it live, keep! writes it with its recipe, and a score quantizes it.
|
|
388
|
+
|
|
389
|
+
(def render-max 4096)
|
|
390
|
+
|
|
391
|
+
(defn- spans-of
|
|
392
|
+
"The events of pattern p whose spans may meet [lo, hi]: [{:on :end :e :hz [..]} …] (rests left out)."
|
|
393
|
+
[p lo hi]
|
|
394
|
+
(loop [s (events p) on 0 acc [] n 0]
|
|
395
|
+
(let [e (first s)]
|
|
396
|
+
(if (or (nil? e) (> on (+ hi 1e-9)) (>= n 100000) (>= (count acc) render-max))
|
|
397
|
+
acc
|
|
398
|
+
(let [d (dur-of e) hz (hz e)
|
|
399
|
+
acc (if (or (rest? e) (empty? hz) (< (+ on (max 0 (sustain e))) (- lo 1e-9)))
|
|
400
|
+
acc
|
|
401
|
+
(conj acc {:on on :end (+ on (max 0 (sustain e))) :e e :hz hz}))]
|
|
402
|
+
(recur (rest s) (+ on d) acc (inc n)))))))
|
|
403
|
+
|
|
404
|
+
(defn- map-notes
|
|
405
|
+
"The notes one time map makes: [[x0 x1 span back?] …] for the polyline pts ([[x y] …], in drawn order)."
|
|
406
|
+
[pts spans]
|
|
407
|
+
(let [eps 1e-9
|
|
408
|
+
inside? (fn [sp y] (and (<= (:on sp) (+ y eps)) (< y (- (:end sp) eps))))
|
|
409
|
+
instant? (fn [sp] (<= (- (:end sp) (:on sp)) eps))
|
|
410
|
+
idx (vec spans)
|
|
411
|
+
close (fn [[active out] i x] (if-let [[x0 back] (get active i)] [(dissoc active i) (conj out [x0 x (idx i) back])] [active out]))
|
|
412
|
+
[x0 y0] (first pts)
|
|
413
|
+
start [(into {} (for [i (range (count idx)) :when (inside? (idx i) y0)] [i [x0 false]])) []]]
|
|
414
|
+
(loop [st start [[xa ya] & more :as ps] pts]
|
|
415
|
+
(if (empty? more)
|
|
416
|
+
;; the end of the map: what still sounds ends there
|
|
417
|
+
(let [[active out] st] (into out (for [[i [x0 back]] active] [x0 xa (idx i) back])))
|
|
418
|
+
(let [[xb yb] (first more)
|
|
419
|
+
st (cond
|
|
420
|
+
;; a vertical step (or one back in x): a cut, as a seek — what still contains yb goes on
|
|
421
|
+
(<= xb (+ xa eps))
|
|
422
|
+
(let [[active out] (reduce (fn [st i] (if (inside? (idx i) yb) st (close st i xa))) st (keys (first st)))]
|
|
423
|
+
[(into active (for [i (range (count idx)) :when (and (inside? (idx i) yb) (not (contains? active i)))] [i [xb false]])) out])
|
|
424
|
+
;; flat: what sounds is held
|
|
425
|
+
(< (js/Math.abs (- yb ya)) eps) st
|
|
426
|
+
:else
|
|
427
|
+
(let [up? (> yb ya)
|
|
428
|
+
x-at (fn [y] (+ xa (* (- xb xa) (/ (- y ya) (- yb ya)))))
|
|
429
|
+
lo (min ya yb) hi (max ya yb)]
|
|
430
|
+
(reduce
|
|
431
|
+
(fn [[active out :as st] i]
|
|
432
|
+
(let [sp (idx i) a (:on sp) b (:end sp)]
|
|
433
|
+
(cond
|
|
434
|
+
(or (> a (+ hi eps)) (< b (- lo eps))) st
|
|
435
|
+
;; no length: it sounds where it is crossed, either way
|
|
436
|
+
(instant? sp)
|
|
437
|
+
(if (if up? (and (> a (+ ya eps)) (<= a (+ yb eps))) (and (< a (- ya eps)) (>= a (- yb eps))))
|
|
438
|
+
[active (conj out [(x-at a) (x-at a) sp (not up?)])]
|
|
439
|
+
st)
|
|
440
|
+
:else
|
|
441
|
+
(let [in0 (contains? active i) in1 (inside? sp yb)
|
|
442
|
+
[enter leave] (if up? [a b] [b a])]
|
|
443
|
+
(cond
|
|
444
|
+
(and in0 in1) st
|
|
445
|
+
in0 (close st i (x-at leave))
|
|
446
|
+
in1 [(assoc active i [(x-at enter) (not up?)]) out]
|
|
447
|
+
;; passed over entirely within the step
|
|
448
|
+
(if up? (and (< ya (- a eps)) (>= yb (- b eps))) (and (>= ya (- b eps)) (< yb (+ a eps))))
|
|
449
|
+
[active (conj out [(x-at enter) (x-at leave) sp (not up?)])]
|
|
450
|
+
:else st)))))
|
|
451
|
+
st (range (count idx)))))]
|
|
452
|
+
(recur st more))))))
|
|
453
|
+
|
|
454
|
+
(def render-skip #{:degree :note :midinote :freq :dur :legato :sustain :octave :root :scale :transpose :type :amp :gain :pan})
|
|
455
|
+
|
|
456
|
+
(defn- look-of
|
|
457
|
+
"How a note of event e is drawn: :amp as weight, :pan as a gradient, and the keys named in opts
|
|
458
|
+
(:tex k, :tone k) as a texture and a colour, one for each value met (in order)."
|
|
459
|
+
[e {:keys [tex tone]} seen]
|
|
460
|
+
(let [a (or (:amp e) (:gain e))
|
|
461
|
+
pick (fn [k choices] (when-let [v (and k (contains? e k) (get e k))]
|
|
462
|
+
(let [vs (get @seen k [])
|
|
463
|
+
vs (if (some #{v} vs) vs (conj vs v))]
|
|
464
|
+
(swap! seen assoc k vs)
|
|
465
|
+
(nth choices (mod (first (keep-indexed (fn [i x] (when (= x v) i)) vs)) (count choices))))))]
|
|
466
|
+
(cv/sane-look (cond-> {}
|
|
467
|
+
(number? a) (assoc :w (+ 0.4 (* 2.4 (max 0 (min 1 a)))))
|
|
468
|
+
(number? (:pan e)) (assoc :pan (:pan e))
|
|
469
|
+
tex (assoc :tex (pick tex [nil :dash :dot]))
|
|
470
|
+
tone (assoc :tone (pick tone [nil :kairos :ink :soft]))))))
|
|
471
|
+
|
|
472
|
+
(defn render-tree
|
|
473
|
+
"A stretch of pattern p as a musical tree [:par [:note {:at x :dur d :hz h} …]]. opts: :from :to (beats,
|
|
474
|
+
the identity map when there are no :maps), :maps (time maps: [[[x y] …] …]), :tex and :tone (the keys
|
|
475
|
+
drawn as texture and colour). Each note keeps the event's other keys under :ev, :back true when it was
|
|
476
|
+
entered from its end, and a :look for the freqt."
|
|
477
|
+
[p {:keys [from to maps] :as opts}]
|
|
478
|
+
(let [maps (if (seq maps) maps [[[(or from 0) (or from 0)] [(or to 16) (or to 16)]]])
|
|
479
|
+
ys (map second (apply concat maps))
|
|
480
|
+
spans (spans-of p (apply min ys) (apply max ys))
|
|
481
|
+
seen (atom {})
|
|
482
|
+
notes (for [[mi pts] (map-indexed vector maps)
|
|
483
|
+
:when (>= (count pts) 2)
|
|
484
|
+
[x0 x1 sp back] (map-notes pts spans)
|
|
485
|
+
;; a span entered and left at the same x (a cut right at its edge) is not a note
|
|
486
|
+
:when (or (> (- x1 x0) 1e-6) (<= (- (:end sp) (:on sp)) 1e-9))
|
|
487
|
+
hz (:hz sp)
|
|
488
|
+
:let [e (:e sp) ev (apply dissoc e render-skip) look (look-of e opts seen)]]
|
|
489
|
+
[x0 [:note (cond-> {:at (cv/round-to x0 4) :dur (cv/round-to (max 0.02 (- x1 x0)) 4) :hz hz}
|
|
490
|
+
back (assoc :back true)
|
|
491
|
+
(> (count maps) 1) (assoc :layer (inc mi))
|
|
492
|
+
look (assoc :look look)
|
|
493
|
+
(seq ev) (assoc :ev ev))]])]
|
|
494
|
+
(into [:par {:render true}] (map second (sort-by first notes)))))
|
|
495
|
+
|
|
496
|
+
;; ───────── writing a pbind back as text ─────────
|
|
497
|
+
|
|
498
|
+
(defn write
|
|
499
|
+
"A value of a pbind as EDN text, generators as #pat/…."
|
|
500
|
+
[x]
|
|
501
|
+
(cond (gen? x) (str "#pat/" (name (:pat/gen x)) " " (write (:pat/args x)))
|
|
502
|
+
(map? x) (str "{" (str/join ", " (map (fn [[k v]] (str (write k) " " (write v))) x)) "}")
|
|
503
|
+
(vector? x) (str "[" (str/join " " (map write x)) "]")
|
|
504
|
+
(seq? x) (str "[" (str/join " " (map write (take max-period x))) "]")
|
|
505
|
+
:else (pr-str x)))
|
|
506
|
+
|
|
507
|
+
(defn spec->source
|
|
508
|
+
"A pbind map as the text of its box, one key per line."
|
|
509
|
+
[m]
|
|
510
|
+
(str "{" (str/join "\n " (map (fn [[k v]] (str (write k) " " (write v))) m)) "}"))
|
|
511
|
+
|
|
512
|
+
;; ───────── the languages of the two code boxes ─────────
|
|
513
|
+
|
|
514
|
+
(code/register-language! "pbind"
|
|
515
|
+
(fn [source]
|
|
516
|
+
(js/Promise.
|
|
517
|
+
(fn [res rej]
|
|
518
|
+
(try
|
|
519
|
+
(let [m (read-spec source) ps (problems m)]
|
|
520
|
+
(if (seq ps)
|
|
521
|
+
(rej (js/Error. (first ps)))
|
|
522
|
+
(res {:value m :info {:summary (summary m)}})))
|
|
523
|
+
(catch :default e (rej e)))))))
|
|
524
|
+
|
|
525
|
+
(code/register-language! "pattern"
|
|
526
|
+
(fn [source]
|
|
527
|
+
(js/Promise.
|
|
528
|
+
(fn [res rej]
|
|
529
|
+
(if-not (code/allowed? source)
|
|
530
|
+
(rej (js/Error. "not run: this work is shared; (trust-scripts true) to evaluate its patterns"))
|
|
531
|
+
(try
|
|
532
|
+
(let [f (js/scittle.core.eval_string (str "(fn [{:keys [" (str/join " " code-names) "]}]\n(do " source "\n))"))
|
|
533
|
+
;; tried once, so a mistake is told now and not when it plays
|
|
534
|
+
first-events (take 2 (flat-events (f (code-env (rng 0) 0 1)) 0 1))]
|
|
535
|
+
(cond (not (fn? f)) (rej (js/Error. "a pattern is an expression"))
|
|
536
|
+
(empty? first-events) (rej (js/Error. "it gives no events: return event maps ({:degree 0 :dur 1}), a sequence of them, or a map to read as a pbind"))
|
|
537
|
+
:else (res {:value f :info {:summary (str "pattern · " (str/join " " (map name (sort (keys (first first-events))))))}})))
|
|
538
|
+
(catch :default e (rej e))))))))
|