@kairotic/rhythm 0.5.0 → 0.6.0
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 +1 -0
- package/dist/chance.mjs +1 -1
- package/dist/euclid.mjs +1 -1
- package/dist/homometric.mjs +1 -1
- package/dist/index.mjs +7 -1
- package/dist/morph.mjs +1 -1
- package/dist/necklace.mjs +1 -1
- package/dist/phase.mjs +9 -0
- package/dist/swap.mjs +1 -1
- package/dist/vary.mjs +1 -1
- package/package.json +6 -3
- package/src/phase.cljs +88 -0
package/README.md
CHANGED
|
@@ -20,6 +20,7 @@ is their editor, measures them and writes them as music.
|
|
|
20
20
|
| `swap` | **how far apart two rhythms are** (Toussaint's swap distance): the left one against the right one (the reference, cold) — in the bar, around the circle, and as necklaces at the best rotation; with the Hamming distance beside it, and the directed swap distance when they hold different numbers of onsets |
|
|
21
21
|
| `morph [ratio 0]` | **from one rhythm to another, swap by swap**: a ratio 0…1 is a point on the swaps that turn the left rhythm into the target (middle, cold) — always a real rhythm; the way also as data (`[:swap from to]`, `[:remove i]`, `[:add j]`) |
|
|
22
22
|
| `vary [d 1] [seed 1] [depth] [downbeat]` | **again, but different**, drawn with a seed: exactly d swaps away, or (`depth`) the onsets on the strong pulses kept down to metrical level d and the rest drawn again — the same skeleton, a new surface |
|
|
23
|
+
| `phase [min 0]` | **where a performer is in a cycle**, from the onsets heard (#204): a pulse clock on the right, onsets on the left, the reference timeline in the middle; at each pulse, the rotation of the reference nearest to what was heard (swap distance) gives the phase — into a `necklace`'s clock inlet, its hand follows the performer |
|
|
23
24
|
|
|
24
25
|
```clojure
|
|
25
26
|
(box :b1 "msg [:rotation 6]" 40 20)
|
package/dist/chance.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Generated by packages/build.mjs from src/chance.cljs
|
|
2
2
|
export default {
|
|
3
3
|
"class": "chance",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.0",
|
|
5
5
|
"author": "bonuoq",
|
|
6
6
|
"license": "MPL-2.0",
|
|
7
7
|
"doc": "A generator of pulses, as euclid, with chance instead of evenness: each bang draws another rhythm of k onsets among n pulses, sent as pulses (ones and zeros) for a necklace. The draws are the seed's — the same seed, the same rhythms in the same order, for everyone in a room and again from the history; :reset starts its draws again. [:k 5], [:n 16], [:seed 7] (or a map of them) change it and start again; a number is k.",
|
package/dist/euclid.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Generated by packages/build.mjs from src/euclid.cljs
|
|
2
2
|
export default {
|
|
3
3
|
"class": "euclid",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.0",
|
|
5
5
|
"author": "bonuoq",
|
|
6
6
|
"license": "MPL-2.0",
|
|
7
7
|
"doc": "A Euclidean rhythm (Bjorklund's algorithm, after Toussaint), as pulses: k onsets spread as evenly as they can be over n pulses, from pulse rotation on — euclid 3 8 is the tresillo [1 0 0 1 0 0 1 0], euclid 5 8 the cinquillo, euclid 5 16 a rotation of the bossa nova (euclid 5 16 6 the bossa nova), euclid 7 12 the bembé. A generator with no view: a bang sends the pulses; [:k 5], [:n 16], [:rotation 2] (or a map of them) change them and send them when they are new, a number is k. Pulses are what every rhythm box takes: a necklace after it shows, edits and measures the rhythm, and writes it as a musical tree with pitches (euclid → necklace → tree).",
|
package/dist/homometric.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Generated by packages/build.mjs from src/homometric.cljs
|
|
2
2
|
export default {
|
|
3
3
|
"class": "homometric",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.0",
|
|
5
5
|
"author": "bonuoq",
|
|
6
6
|
"license": "MPL-2.0",
|
|
7
7
|
"doc": "The other rhythms with the same interval vector (Toussaint, after crystallography): homometric rhythms hold the same distances between onsets, as many times each, without being the same necklace turned or in a mirror — the two flat rhythms of four onsets in twelve; any half of the pulses of an even cycle and its complement, two drums with the same intervals. Given pulses, it searches every rhythm of as many onsets among as many pulses (when they are at most 7 000: any rhythm of 16 pulses) and sends the others it finds, each from its first onset, as a list of pulses (left: for nth, or one by one to a necklace) and how many there are (right); none, an empty list. A bang searches again.",
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Generated by packages/build.mjs
|
|
2
2
|
export default {
|
|
3
3
|
"package": "@kairotic/rhythm",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.0",
|
|
5
5
|
"api": 1,
|
|
6
6
|
"author": "bonuoq",
|
|
7
7
|
"license": "MPL-2.0",
|
|
@@ -36,6 +36,12 @@ export default {
|
|
|
36
36
|
"doc": "A cyclic rhythm on the circle and its geometry (Toussaint): k onsets among n pulses around a circle, time clockwise from the top — the same object as a pitch-class set on pcclock. A rhythm arrives as pulses (ones and zeros, as euclid's right outlet sends them), as intervals between onsets ([3 3 2]), as box notation (\"x..x..x.\"), as an :rtm map ({:shares [3 3 2]}, euclid's middle outlet), a tree with an :rtm, or {:onsets [0 3 6] :n 8}; it is drawn as a polygon, with its axes of mirror symmetry. Pressing a pulse (or a number, its index) adds or takes away an onset. :complement swaps onsets and silences, [:rotate 1] starts it a pulse later, [:n 12] makes the circle longer or shorter, :clear empties it, [:bars 4] and [:meter \"3/4\"] as euclid, a bang sends it again. Out, as euclid: a musical tree (left: a bar of n pulses with an :rtm whose shares are the intervals between onsets), the :rtm map alone (1), the pulses (2); and its measures (3): {:n :k :onsets :intervals :ivec — how often each distance occurs —, :evenness — the sum of the chords between all pairs (Block and Douthett) —, :even — that sum against the Euclidean rhythm's, 1 as even as it —, :deep — each distance a different number of times —, :flat — each once —, :axes — of mirror symmetry, in pulses —, :gap — the longest interval —, :oddity — no two onsets halving the cycle —, :euclid — the rotation of E(k,n) it is, or nil —, :fourier and :phases — its Fourier coefficients f1 … f⌊n/2⌋, magnitudes 0…1 and phases in degrees: |f1| near 0 balanced, |f_k| largest when maximally even —}; with the word fourier the magnitudes are drawn too, a bar each. Its middle inlet (cold) takes the pitches the notes take in turn, as euclid's. It also plays it, step by step, as a step sequencer: each bang on the right inlet (a metro, the room's pulse, an onset~…) moves the hand to the next pulse and, on an onset, sends a bang out of outlet 4 (for a player's step, a sample…); a number moves the hand to that pulse (and plays it); :reset makes the next bang play the first pulse. Editing the rhythm while it plays changes what the hand finds.",
|
|
37
37
|
"source": ";; necklace · a cyclic rhythm on the circle, and its geometry (Toussaint)\n;; Part of @kairotic/rhythm. Written against the Kairotic API (api/…).\n;; A rhythm that repeats is a necklace: k onsets among n pulses around a circle, time clockwise from the\n;; top. Seen so, it is the same object as a pitch-class set on pcclock, and the same geometry measures it\n;; (Toussaint, \"The Geometry of Musical Rhythm\"): its full-interval vector (how often each distance between\n;; onsets occurs), its evenness (the sum of the chords between all pairs of onsets, after Block and Douthett;\n;; arcs are too coarse: the six 4/4 claves would all score the same), whether it is deep (each distance a\n;; different number of times, as the diatonic scale and the bembé), flat (each distance once), its axes of\n;; mirror symmetry, its longest gap, rhythmic oddity (no two onsets halving the cycle), and whether it is a\n;; rotation of the Euclidean rhythm E(k,n) — and its Fourier coefficients (after Amiot and Milne): |f1| near 0\n;; is a balanced rhythm, a maximal |f_k| among the rhythms of k onsets a maximally even one, and the others its\n;; periodicities (in 16 pulses f4 the quarter notes, f3 a three against the four).\n;; It is the rhythm box: generators (euclid) make pulses, the necklace holds them, shows them, is their editor\n;; (pressing a pulse adds or takes away an onset), measures them and writes them as a musical tree, its :rtm\n;; map and its pulses, with the pitches of its right inlet (talea and color).\n(let [TAU (* 2 js/Math.PI)\n bjorklund (fn [k n]\n (cond (<= n 0) []\n (<= k 0) (vec (repeat n 0))\n (>= k n) (vec (repeat n 1))\n :else (loop [a (vec (repeat k [1])) b (vec (repeat (- n k) [0]))]\n (if (<= (count b) 1)\n (vec (apply concat (concat a b)))\n (let [m (min (count a) (count b))]\n (recur (mapv into (subvec a 0 m) (subvec b 0 m))\n (if (> (count a) m) (subvec a m) (subvec b m))))))))\n rotate (fn [xs r] (let [n (count xs)] (if (zero? n) xs (let [r (mod r n)] (vec (concat (subvec xs r) (subvec xs 0 r)))))))\n onsets-of (fn [ps] (vec (keep-indexed (fn [i x] (when (= 1 x) i)) ps)))\n intervals-of (fn [ps] (let [os (onsets-of ps) n (count ps)]\n (if (empty? os) [] (mapv (fn [a b] (- b a)) os (conj (subvec os 1) (+ n (first os)))))))\n shares-of (fn [ps] (let [os (onsets-of ps) n (count ps)]\n (cond (zero? n) []\n (empty? os) [(- n)]\n :else (vec (concat (when (pos? (first os)) [(- (first os))])\n (map (fn [a b] (- b a)) os (conj (subvec os 1) n)))))))\n r3 (fn [x] (/ (js/Math.round (* 1000 x)) 1000))\n ;; ── what arrives, as pulses (ones and zeros), or nil when it is not a rhythm ──\n whole? (fn [x] (and (number? x) (== x (js/Math.round x))))\n from-shares (fn [sh] (when (and (sequential? sh) (seq sh) (every? (fn [x] (and (whole? x) (not (zero? x)))) sh))\n (vec (mapcat (fn [x] (if (pos? x) (cons 1 (repeat (dec x) 0)) (repeat (- x) 0))) sh))))\n from-onsets (fn [os n] (when (and (whole? n) (pos? n) (sequential? os) (every? whole? os))\n (let [on (set (map (fn [o] (mod o n)) os))] (mapv (fn [i] (if (on i) 1 0)) (range n)))))\n ;; the first :rtm of a tree (as euclid sends it)\n rtm-in (fn rtm-in [t] (when (sequential? t)\n (if (= :rtm (first t)) (second t) (some rtm-in (filter sequential? t)))))\n ;; a list: ones and zeros are pulses; other whole numbers, the intervals between onsets (3 3 2)\n pulses-of-list (fn [v] (when (and (sequential? v) (seq v) (every? whole? v))\n (if (every? #{0 1} v) (vec v) (when (every? pos? v) (from-shares v)))))\n pulses-of (fn [v]\n (cond\n ;; box notation (TUBS): \"x..x..x.\" (x, X or 1 an onset; . - _ or 0 a pulse without one)\n (string? v) (let [cs (remove #{\" \"} (seq v))]\n (when (and (seq cs) (every? #{\"x\" \"X\" \"1\" \".\" \"-\" \"_\" \"0\"} cs))\n (mapv (fn [c] (if (#{\"x\" \"X\" \"1\"} c) 1 0)) cs)))\n (map? v) (cond (contains? v :shares) (from-shares (:shares v))\n (contains? v :pulses) (pulses-of-list (:pulses v))\n (contains? v :onsets) (from-onsets (:onsets v) (:n v)))\n (and (sequential? v) (keyword? (first v))) (some-> (rtm-in v) :shares from-shares)\n :else nil))\n ;; ── its geometry ──\n dist (fn [n a b] (let [d (js/Math.abs (- a b))] (min d (- n d))))\n pairs (fn [os] (for [i (range (count os)) j (range (inc i) (count os))] [(nth os i) (nth os j)]))\n ivec-of (fn [n os] (let [c (frequencies (map (fn [[a b]] (dist n a b)) (pairs os)))]\n (mapv (fn [d] (get c d 0)) (range 1 (inc (quot n 2))))))\n evenness-of (fn [n os] (reduce + 0 (map (fn [[a b]] (* 2 (js/Math.sin (/ (* js/Math.PI (dist n a b)) n)))) (pairs os))))\n ;; the axes of mirror symmetry, in pulses: x → a − x keeps the set; the axis passes through a/2 (and a/2 + n/2)\n axes-of (fn [n os] (let [s (set os)]\n (if (empty? s) []\n (vec (for [a (range n) :when (= s (set (map (fn [x] (mod (- a x) n)) os)))] (/ a 2))))))\n ;; the Fourier coefficients of the onsets, f1 … f⌊n/2⌋: magnitudes 0…1 (over k) and phases in degrees\n fourier-of (fn [n os]\n (let [k (count os)]\n (vec (for [j (range 1 (inc (quot n 2)))]\n (if (zero? k) [0 0]\n [(/ (reduce + (map (fn [x] (js/Math.cos (/ (* TAU j x) n))) os)) k)\n (- (/ (reduce + (map (fn [x] (js/Math.sin (/ (* TAU j x) n))) os)) k))])))))\n measures (fn [ps]\n (let [n (count ps) os (onsets-of ps) k (count os) iv (ivec-of n os) ev (evenness-of n os)\n e (bjorklund k n) eev (evenness-of n (onsets-of e))\n er (first (filter (fn [r] (= ps (rotate e r))) (range (max n 1))))]\n {:n n :k k :onsets os :intervals (intervals-of ps) :ivec iv\n :evenness (r3 ev) :even (if (pos? eev) (r3 (/ ev eev)) 1)\n :deep (and (>= k 2) (= (count (distinct iv)) (count iv)))\n :flat (and (seq iv) (every? #{1} iv))\n :axes (axes-of n os)\n :gap (if (seq os) (apply max (intervals-of ps)) n)\n :oddity (or (odd? n) (not-any? (fn [[a b]] (= (* 2 (dist n a b)) n)) (pairs os)))\n :euclid er\n :fourier (mapv (fn [[a b]] (r3 (js/Math.hypot a b))) (fourier-of n os))\n :phases (mapv (fn [[a b]] (if (< (js/Math.hypot a b) 1e-9) 0 (r3 (* (/ 180 js/Math.PI) (js/Math.atan2 b a))))) (fourier-of n os))}))\n fourier? (fn [args] (some (fn [a] (= \"fourier\" (if (keyword? a) (name a) (str a)))) (if (map? args) (when (:fourier args) [:fourier]) args)))\n ;; ── as euclid sends a rhythm: the meter of a bar of n pulses, the pitches, the color over bars ──\n pow2? (fn [x] (and (pos? x) (== x (js/Math.pow 2 (js/Math.round (js/Math.log2 x))))))\n meter-of (fn [n] (cond (and (>= n 4) (pow2? n)) [4 4]\n (and (>= n 12) (zero? (mod n 3)) (pow2? (/ n 3))) [12 8]\n (< n 4) [n 4]\n (<= n 12) [n 8]\n :else [n 16]))\n meter-arg (fn [v] (cond (and (sequential? v) (= 2 (count v)) (every? number? v)) (vec v)\n (string? v) v\n (keyword? v) (name v)\n :else nil))\n hz->m (fn [x] (when (and (number? x) (pos? x)) (/ (js/Math.round (* 1000 (+ 69 (* 12 (js/Math.log2 (/ x 440)))))) 1000)))\n pitches-of (fn [midi? v]\n (let [p (fn [x] (if midi? x (hz->m x)))]\n (cond (#{:clear} v) nil\n (map? v) (cond (or (contains? v :pitches) (contains? v :pitch)) (select-keys v [:pitches :pitch])\n (sequential? (:note v)) {:pitches (mapv (fn [x] (if (number? x) x \"r\")) (:note v))}\n :else :ignore)\n (string? v) {:pitches v}\n (number? v) (if-let [m (p v)] {:pitch m} :ignore)\n (and (sequential? v) (keyword? (first v))) {:pitches [v]}\n (and (sequential? v) (seq v) (every? number? v)) (let [ms (vec (keep p v))] (if (seq ms) {:pitches ms} :ignore))\n (and (sequential? v) (seq v)) {:pitches (vec v)}\n :else :ignore)))\n tokens (fn [ps] (cond (string? ps) (vec (remove #{\"\"} (.split (.trim ps) (js/RegExp. \"\\\\s+\"))))\n (sequential? ps) (vec ps)\n :else nil))\n color-at (fn [pm skip]\n (let [ts (tokens (:pitches pm))]\n (if (or (empty? ts) (zero? (mod skip (count ts))))\n pm\n (let [r (mod skip (count ts)) ts2 (vec (concat (subvec ts r) (subvec ts 0 r)))]\n (assoc pm :pitches (if (string? (:pitches pm)) (apply str (interpose \" \" ts2)) ts2))))))\n rhythm (fn [ps st]\n (let [k (count (onsets-of ps)) bars (:bars st) meter (or (:meter st) (meter-of (count ps)))\n rtm (merge {:shares (shares-of ps)} (or (:pm st) {}))\n bar (fn [i] (let [a (color-at rtm (* i k))]\n (if (zero? i) [:measure {:meter meter} [:rtm a]] [:measure [:rtm a]])))]\n {:rtm rtm :tree (into [:voice] (map bar (range bars)))}))\n ;; what goes out (the last first, as Max does): the measures (3), the pulses (2), the :rtm map (1), the tree (0)\n outs (fn [ps st] (let [r (rhythm ps st)] [[3 (measures ps)] [2 ps] [1 (:rtm r)] [0 (:tree r)]]))\n ;; a new state: sent when the rhythm, its bars or its meter are new (the same again changes nothing); a bang anyway\n settle (fn [st st2 always?]\n (let [ps (:pulses st2) key [ps (:bars st2) (:meter st2)]\n hand (when (:hand st2) (mod (:hand st2) (count ps)))\n show (assoc (measures ps) :pulses ps :hand hand)\n st2 (assoc st2 :hand hand :shown show)]\n (cond (empty? ps) {:state st2}\n (and (not always?) (= key (:sent st))) {:state st2 :show show}\n :else {:state (assoc st2 :sent key) :show show :out (outs ps st2)})))\n ;; a step of the hand (the third inlet): to pulse h, and a bang out of outlet 4 when there is an onset there\n step (fn [st h]\n (let [ps (:pulses st) h (mod h (count ps))\n show (assoc (or (:shown st) (assoc (measures ps) :pulses ps)) :hand h)]\n (cond-> {:state (assoc st :hand h :shown show) :show show}\n (= 1 (nth ps h)) (assoc :out [[4 :bang]]))))\n polar (fn [cx cy R i n] (let [a (* TAU (/ i n))] [(+ cx (* R (js/Math.sin a))) (- cy (* R (js/Math.cos a)))]))]\n {;; ── about ──\n :class \"necklace\"\n :license \"MPL-2.0\"\n :doc \"A cyclic rhythm on the circle and its geometry (Toussaint): k onsets among n pulses around a circle, time clockwise from the top — the same object as a pitch-class set on pcclock. A rhythm arrives as pulses (ones and zeros, as euclid's right outlet sends them), as intervals between onsets ([3 3 2]), as box notation (\\\"x..x..x.\\\"), as an :rtm map ({:shares [3 3 2]}, euclid's middle outlet), a tree with an :rtm, or {:onsets [0 3 6] :n 8}; it is drawn as a polygon, with its axes of mirror symmetry. Pressing a pulse (or a number, its index) adds or takes away an onset. :complement swaps onsets and silences, [:rotate 1] starts it a pulse later, [:n 12] makes the circle longer or shorter, :clear empties it, [:bars 4] and [:meter \\\"3/4\\\"] as euclid, a bang sends it again. Out, as euclid: a musical tree (left: a bar of n pulses with an :rtm whose shares are the intervals between onsets), the :rtm map alone (1), the pulses (2); and its measures (3): {:n :k :onsets :intervals :ivec — how often each distance occurs —, :evenness — the sum of the chords between all pairs (Block and Douthett) —, :even — that sum against the Euclidean rhythm's, 1 as even as it —, :deep — each distance a different number of times —, :flat — each once —, :axes — of mirror symmetry, in pulses —, :gap — the longest interval —, :oddity — no two onsets halving the cycle —, :euclid — the rotation of E(k,n) it is, or nil —, :fourier and :phases — its Fourier coefficients f1 … f⌊n/2⌋, magnitudes 0…1 and phases in degrees: |f1| near 0 balanced, |f_k| largest when maximally even —}; with the word fourier the magnitudes are drawn too, a bar each. Its middle inlet (cold) takes the pitches the notes take in turn, as euclid's. It also plays it, step by step, as a step sequencer: each bang on the right inlet (a metro, the room's pulse, an onset~…) moves the hand to the next pulse and, on an onset, sends a bang out of outlet 4 (for a player's step, a sample…); a number moves the hand to that pulse (and plays it); :reset makes the next bang play the first pulse. Editing the rhythm while it plays changes what the hand finds.\"\n :usage \"necklace [n 16] [bars 1] [midi] [fourier]\"\n :io [3 5]\n :size (fn [box] (if (fourier? (:args box)) [200 296] [200 240]))\n\n ;; ── arguments ──\n :args [[:n 16] [:bars 1] [:midi false] [:fourier false]]\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state args]}]\n (let [st (or state {:pulses (vec (repeat (max 1 (js/Math.round (or (:n args) 16))) 0))\n :bars (api/clamp (js/Math.round (or (:bars args) 1)) 1 64) :meter nil :pm nil})\n ps (:pulses st) n (count ps)\n to (fn [m] (settle st (merge st m) false))\n toggle (fn [i] (let [i (mod (js/Math.round i) n)] (to {:pulses (update ps i (fn [x] (- 1 x)))})))]\n (cond\n (= inlet 1)\n (let [pm (pitches-of (:midi args) value)]\n (if (= pm :ignore) {} {:state (assoc st :pm pm)}))\n ;; the clock: a step of the hand\n (= inlet 2)\n (cond (#{:bang} value) (step st (if (:hand st) (inc (:hand st)) 0))\n (number? value) (step st (js/Math.round value))\n (#{:reset} value) (let [s2 (assoc st :hand nil)] {:state (assoc s2 :shown (assoc (or (:shown st) {}) :hand nil)) :show (assoc (or (:shown st) (assoc (measures ps) :pulses ps)) :hand nil)})\n :else {})\n :else\n (cond\n (#{:bang} value) (settle st st true)\n (#{:clear} value) (to {:pulses (vec (repeat n 0))})\n (#{:complement} value) (to {:pulses (mapv (fn [x] (- 1 x)) ps)})\n (number? value) (toggle value)\n (and (vector? value) (= :pressed (first value)) (number? (second value))) (toggle (second value))\n (and (vector? value) (= :rotate (first value)) (number? (second value))) (to {:pulses (rotate ps (js/Math.round (second value)))})\n (and (vector? value) (= :n (first value)) (number? (second value)))\n (let [m (api/clamp (js/Math.round (second value)) 1 128)]\n (to {:pulses (vec (take m (concat ps (repeat 0))))}))\n (and (vector? value) (= :bars (first value)) (number? (second value))) (to {:bars (api/clamp (js/Math.round (second value)) 1 64)})\n (and (vector? value) (= :meter (first value))) (to {:meter (meter-arg (second value))})\n :else (if-let [p (or (pulses-of-list value) (pulses-of value))]\n (to {:pulses p})\n {})))))\n ;; pressing a pulse adds or takes away an onset (sent once, on release)\n :gesture (fn [{:keys [up hit]}] (when (and up (number? hit)) [:pressed hit]))\n\n ;; ── view ──\n :draw (fn [{:keys [shown w h args]}]\n (let [p (or shown (let [ps (vec (repeat (max 1 (js/Math.round (or (:n args) 16))) 0))] (assoc (measures ps) :pulses ps)))\n n (:n p) ps (:pulses p) os (:onsets p)\n fh (if (:fourier args) 60 0)\n cx (/ w 2) cy (/ (- h 44 fh) 2) R (- (min cx cy) 16)\n r (cond (> n 32) 2.2 (> n 16) 3 :else 4.5)\n flags (remove nil? [(str \"even \" (:even p))\n (let [a (count (:axes p))] (case a 0 \"asymmetric\" 1 \"1 axis\" (str a \" axes\")))\n (when (:deep p) \"deep\") (when (:flat p) \"flat\")\n (when (and (even? n) (:oddity p) (pos? (:k p))) \"oddity\")\n (when (:euclid p) (str \"E(\" (:k p) \",\" n \")\" (when (pos? (:euclid p)) (str \" ↻\" (:euclid p)))))])]\n (concat\n [[:circle {:cx cx :cy cy :r R :stroke :grid}]]\n (let [[x1 y1] (polar cx cy (- R 9) 0 n) [x2 y2] (polar cx cy (+ R 9) 0 n)]\n [[:line {:x1 x1 :y1 y1 :x2 x2 :y2 y2 :stroke :kairos :weight 1.5 :opacity 0.8}]])\n ;; the axes of symmetry, across the circle\n (for [a (:axes p)]\n (let [[x1 y1] (polar cx cy (+ R 7) a n) [x2 y2] (polar cx cy (+ R 7) (+ a (/ n 2)) n)]\n [:line {:x1 x1 :y1 y1 :x2 x2 :y2 y2 :stroke :kairos :weight 1 :opacity 0.5}]))\n (when (> (count os) 1) [[:path {:points (mapv (fn [i] (polar cx cy R i n)) os) :closed true :stroke :accent :weight 2}]])\n ;; the hand, when it plays: from the centre to the pulse it is on\n (when-let [hd (:hand p)]\n (let [[x y] (polar cx cy (- R 6) hd n)]\n [[:line {:x1 cx :y1 cy :x2 x :y2 y :stroke :kairos :weight 2}]\n [:circle {:cx cx :cy cy :r 2.5 :fill :kairos}]]))\n (for [i (range n)]\n (let [[x y] (polar cx cy R i n) on (= 1 (nth ps i 0))]\n [:group {:hit i}\n [[:circle {:cx x :cy y :r (if on (+ r 1.5) r) :fill (if on :accent :bg) :stroke (if on :accent :soft)}]]]))\n ;; the Fourier magnitudes, a bar each (f1 … f⌊n/2⌋), above the texts\n (when (:fourier args)\n (let [fs (or (:fourier p) []) m (count fs) bw (if (pos? m) (/ (- w 20) m) 0) base (- h 52)]\n (concat\n [[:line {:x1 10 :y1 base :x2 (- w 10) :y2 base :stroke :grid}]]\n (for [[j f] (map-indexed vector fs)]\n [:rect {:x (+ 10 (* j bw) 1) :y (- base (* 36 f)) :w (max 1 (- bw 2)) :h (* 36 f)\n :fill (if (= (inc j) (:k p)) :kairos :accent) :opacity 0.8}])\n (when (<= m 16)\n (for [j (range m)]\n [:text {:x (+ 10 (* j bw) (/ bw 2)) :y (+ base 9) :anchor :middle :size 7 :fill :soft} (str (inc j))])))))\n [[:text {:x cx :y (- h 29) :anchor :middle :size 10 :fill :ink}\n (str (:k p) \"/\" n \" · \" (if (seq (:intervals p)) (apply str (interpose \"-\" (:intervals p))) \"silence\"))]\n [:text {:x cx :y (- h 16) :anchor :middle :size 9 :fill :soft} (str \"<\" (apply str (interpose \" \" (:ivec p))) \">\")]\n [:text {:x cx :y (- h 3) :anchor :middle :size 9 :fill :soft} (apply str (interpose \" · \" flags))]])))})\n"
|
|
38
38
|
},
|
|
39
|
+
{
|
|
40
|
+
"class": "phase",
|
|
41
|
+
"license": "MPL-2.0",
|
|
42
|
+
"doc": "Where a performer is in a cycle, from the onsets heard (discussion #204): a timeline follower, onsets only. The right inlet is the pulse clock (a metro or the room's pulse, a bang a pulse), the left one the onsets (onset~'s bangs, or taps), each falling on its nearest pulse; the middle one the reference, as pulses (cold: a clave, a bell pattern, euclid's or a necklace's). At each pulse the last pulses heard are compared with every rotation of the reference (Toussaint's swap distance, two more for each onset one has and the other has not): the nearest says which pulse of the reference this is — the phase, sent on the left once enough is heard (min pulses, half the cycle by default, and two onsets). The pulse that just began is not judged (its onset may come a moment late, as onset~ hears it): the last whole pulse is found and this one is the next. Ties and near ties (less than a swap and a half) go to the phase the walk expects, so a few missing or extra onsets do not throw it. Into a necklace's clock inlet the phase moves its hand there and plays it. Out on the right: {:phase :distance (0: exactly the reference) :margin (how much nearer than the next guess: 0 is ambiguous) :heard :window :n}. :reset forgets what was heard.",
|
|
43
|
+
"source": ";; phase · where a performer is in a cycle, from the onsets heard (discussion #204)\n;; Part of @kairotic/rhythm. Written against the Kairotic API (api/…).\n;; Before following a score, follow a timeline: a clave, a bell pattern, any rhythm that repeats. A pulse\n;; clock (a metro or the room's pulse, one bang a pulse) gives the grid; onsets (onset~'s bangs, or taps)\n;; fall on its nearest pulse. At each pulse the last pulses heard are compared with the reference\n;; (Toussaint's swap distance, on every rotation of the reference): the rotation that brings them nearest\n;; says which pulse of the reference this one is — the phase. Into a necklace's clock inlet, the phase moves\n;; its hand there (and plays it); times a pulse's beats, it is a player's position. Ties (a symmetric rhythm,\n;; few onsets yet) go to the pulse after the last phase, so the phase walks on unless what is heard says\n;; otherwise. Only onsets, no pitch: \"time by occasion\" at its smallest.\n(let [onsets-of (fn [ps] (vec (keep-indexed (fn [i x] (when (= 1 x) i)) ps)))\n pulses? (fn [v] (and (sequential? v) (seq v) (every? #{0 1} v)))\n ;; the directed swap distance between two lists of onsets of one segment (each onset of the shorter\n ;; list to its own of the other, in order, the least moving in all)\n assign (fn [us vs]\n (let [[a b] (if (<= (count us) (count vs)) [us vs] [vs us]) p (count a) q (count b)]\n (if (zero? p) 0\n (loop [i 0 row (vec (repeat (inc q) 0))]\n (if (= i p) (peek row)\n (recur (inc i)\n (reduce (fn [nr j] (conj nr (min (if (> j (inc i)) (peek nr) js/Infinity)\n (+ (nth row (dec j)) (js/Math.abs (- (nth a i) (nth b (dec j))))))))\n [js/Infinity] (range 1 (inc q)))))))))\n ;; how far the window w (the last m pulses heard, the last one now) is from the reference read so that\n ;; now is its pulse ph: the swaps between them, and two for each onset one has and the other has not\n continuity 1.5\n cost (fn [ref w ph]\n (let [n (count ref) m (count w)\n seg (mapv (fn [i] (nth ref (mod (+ (- ph (dec m)) i) n))) (range m))\n a (onsets-of w) b (onsets-of seg)]\n (+ (assign a b) (* 2 (js/Math.abs (- (count a) (count b)))))))\n ;; at each pulse: what the pulses before it say. The pulse that just began is not judged — its onset may\n ;; come a moment late (onset~ hears the attack after it) —: the phase of the last whole pulse is found,\n ;; and this one is the next. Ties, and near ties (less than a swap and a half), go to the phase the walk\n ;; expects, so a few missing or extra onsets do not throw it\n judge (fn [st]\n (let [ref (:ref st) n (count ref)\n ts (vec (take-last n (butlast (:ticks st)))) pulse (:pulse st)\n ;; a pulse is an onset when one was heard within half a pulse of it\n w (mapv (fn [t] (if (some (fn [o] (and (> o (- t (/ pulse 2))) (<= o (+ t (/ pulse 2))))) (:onsets st)) 1 0)) ts)\n m (count w)\n expected (:phase st)\n raw (vec (for [ph (range n)] (cost ref w ph)))\n cs (vec (map-indexed (fn [ph c] (+ c (if (and expected (not= ph expected)) continuity 0))) raw))\n best (apply min cs)\n ph (first (keep-indexed (fn [i c] (when (= c best) i)) cs))\n second-best (apply min (or (seq (keep-indexed (fn [i c] (when (not= i ph) c)) raw)) [(nth raw ph)]))\n ready (and pulse (>= m (:min st)) (>= (count (onsets-of w)) 2))]\n {:phase (mod (inc ph) n) :distance (nth raw ph) :margin (- second-best (nth raw ph)) :heard m :ready ready\n :window w :n n}))\n tick (fn [st t]\n (let [ticks (vec (take-last 257 (conj (:ticks st) t)))\n ;; the pulse: the median of the last intervals between bangs of the clock\n ivs (sort (map - (rest (take-last 9 ticks)) (butlast (take-last 9 ticks))))\n pulse (when (seq ivs) (nth ivs (quot (count ivs) 2)))\n st (assoc st :ticks ticks :pulse pulse\n :onsets (vec (filter (fn [o] (> o (- (first ticks) 1))) (:onsets st))))]\n (cond\n (not (:ref st)) {:state st :show \"no reference yet (middle)\"}\n (< (count ticks) 2) {:state st :show \"listening\"}\n :else (let [j (judge st)]\n (if-not (:ready j)\n {:state st :show (str \"listening · \" (:heard j) \"/\" (:min st))}\n {:state (assoc st :phase (:phase j))\n :show (str \"pulse \" (:phase j) \" of \" (:n j) \" · distance \" (:distance j))\n :out [[1 (dissoc j :ready)] [0 (:phase j)]]})))))\n onset (fn [st t] {:state (assoc st :onsets (conj (vec (take-last 256 (:onsets st))) t))})]\n {;; ── about ──\n :class \"phase\"\n :license \"MPL-2.0\"\n :doc \"Where a performer is in a cycle, from the onsets heard (discussion #204): a timeline follower, onsets only. The right inlet is the pulse clock (a metro or the room's pulse, a bang a pulse), the left one the onsets (onset~'s bangs, or taps), each falling on its nearest pulse; the middle one the reference, as pulses (cold: a clave, a bell pattern, euclid's or a necklace's). At each pulse the last pulses heard are compared with every rotation of the reference (Toussaint's swap distance, two more for each onset one has and the other has not): the nearest says which pulse of the reference this is — the phase, sent on the left once enough is heard (min pulses, half the cycle by default, and two onsets). The pulse that just began is not judged (its onset may come a moment late, as onset~ hears it): the last whole pulse is found and this one is the next. Ties and near ties (less than a swap and a half) go to the phase the walk expects, so a few missing or extra onsets do not throw it. Into a necklace's clock inlet the phase moves its hand there and plays it. Out on the right: {:phase :distance (0: exactly the reference) :margin (how much nearer than the next guess: 0 is ambiguous) :heard :window :n}. :reset forgets what was heard.\"\n :usage \"phase [min 0]\"\n :io [3 2]\n\n ;; ── arguments ──\n :args [[:min 0]]\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state args at]}]\n (let [t (or at (/ (js/Date.now) 1000))\n st (or state {:ticks [] :onsets []})\n min-of (fn [ref] (let [m (js/Math.round (or (:min args) 0))] (if (pos? m) m (max 2 (quot (count ref) 2)))))]\n (case inlet\n 1 (if (pulses? value) {:state (assoc st :ref (vec value) :min (min-of value) :phase nil)} {})\n 2 (if (#{:bang} value) (tick st t) {})\n (cond (#{:reset} value) {:state (assoc st :ticks [] :onsets [] :phase nil) :show \"listening\"}\n (or (#{:bang} value) (number? value)) (onset st t)\n :else {}))))})\n"
|
|
44
|
+
},
|
|
39
45
|
{
|
|
40
46
|
"class": "swap",
|
|
41
47
|
"license": "MPL-2.0",
|
package/dist/morph.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Generated by packages/build.mjs from src/morph.cljs
|
|
2
2
|
export default {
|
|
3
3
|
"class": "morph",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.0",
|
|
5
5
|
"author": "bonuoq",
|
|
6
6
|
"license": "MPL-2.0",
|
|
7
7
|
"doc": "From one rhythm to another, swap by swap (Toussaint's swap distance as a way, discussion #206). Pulses on the left (A, hot) and on the middle inlet (B, the target, cold), both of n pulses; a ratio 0…1 on the right (hot): the rhythm after that share of the swaps that turn A into B with the fewest moves — always a real rhythm. Onsets paired in order (with different numbers, as the directed swap distance pairs them: the ones of A left over go first, those of B appear last); the swaps take turns, a pulse each, so the rhythm changes evenly. Out: the pulses at the ratio (left) and the way as data (right): {:steps [[:swap from to] [:remove i] [:add j] …] :swaps :at :of}. A bang sends it again. The ratio can be the args' (morph 0.5).",
|
package/dist/necklace.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Generated by packages/build.mjs from src/necklace.cljs
|
|
2
2
|
export default {
|
|
3
3
|
"class": "necklace",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.0",
|
|
5
5
|
"author": "bonuoq",
|
|
6
6
|
"license": "MPL-2.0",
|
|
7
7
|
"doc": "A cyclic rhythm on the circle and its geometry (Toussaint): k onsets among n pulses around a circle, time clockwise from the top — the same object as a pitch-class set on pcclock. A rhythm arrives as pulses (ones and zeros, as euclid's right outlet sends them), as intervals between onsets ([3 3 2]), as box notation (\"x..x..x.\"), as an :rtm map ({:shares [3 3 2]}, euclid's middle outlet), a tree with an :rtm, or {:onsets [0 3 6] :n 8}; it is drawn as a polygon, with its axes of mirror symmetry. Pressing a pulse (or a number, its index) adds or takes away an onset. :complement swaps onsets and silences, [:rotate 1] starts it a pulse later, [:n 12] makes the circle longer or shorter, :clear empties it, [:bars 4] and [:meter \"3/4\"] as euclid, a bang sends it again. Out, as euclid: a musical tree (left: a bar of n pulses with an :rtm whose shares are the intervals between onsets), the :rtm map alone (1), the pulses (2); and its measures (3): {:n :k :onsets :intervals :ivec — how often each distance occurs —, :evenness — the sum of the chords between all pairs (Block and Douthett) —, :even — that sum against the Euclidean rhythm's, 1 as even as it —, :deep — each distance a different number of times —, :flat — each once —, :axes — of mirror symmetry, in pulses —, :gap — the longest interval —, :oddity — no two onsets halving the cycle —, :euclid — the rotation of E(k,n) it is, or nil —, :fourier and :phases — its Fourier coefficients f1 … f⌊n/2⌋, magnitudes 0…1 and phases in degrees: |f1| near 0 balanced, |f_k| largest when maximally even —}; with the word fourier the magnitudes are drawn too, a bar each. Its middle inlet (cold) takes the pitches the notes take in turn, as euclid's. It also plays it, step by step, as a step sequencer: each bang on the right inlet (a metro, the room's pulse, an onset~…) moves the hand to the next pulse and, on an onset, sends a bang out of outlet 4 (for a player's step, a sample…); a number moves the hand to that pulse (and plays it); :reset makes the next bang play the first pulse. Editing the rhythm while it plays changes what the hand finds.",
|
package/dist/phase.mjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Generated by packages/build.mjs from src/phase.cljs
|
|
2
|
+
export default {
|
|
3
|
+
"class": "phase",
|
|
4
|
+
"version": "0.6.0",
|
|
5
|
+
"author": "bonuoq",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"doc": "Where a performer is in a cycle, from the onsets heard (discussion #204): a timeline follower, onsets only. The right inlet is the pulse clock (a metro or the room's pulse, a bang a pulse), the left one the onsets (onset~'s bangs, or taps), each falling on its nearest pulse; the middle one the reference, as pulses (cold: a clave, a bell pattern, euclid's or a necklace's). At each pulse the last pulses heard are compared with every rotation of the reference (Toussaint's swap distance, two more for each onset one has and the other has not): the nearest says which pulse of the reference this is — the phase, sent on the left once enough is heard (min pulses, half the cycle by default, and two onsets). The pulse that just began is not judged (its onset may come a moment late, as onset~ hears it): the last whole pulse is found and this one is the next. Ties and near ties (less than a swap and a half) go to the phase the walk expects, so a few missing or extra onsets do not throw it. Into a necklace's clock inlet the phase moves its hand there and plays it. Out on the right: {:phase :distance (0: exactly the reference) :margin (how much nearer than the next guess: 0 is ambiguous) :heard :window :n}. :reset forgets what was heard.",
|
|
8
|
+
"source": ";; phase · where a performer is in a cycle, from the onsets heard (discussion #204)\n;; Part of @kairotic/rhythm. Written against the Kairotic API (api/…).\n;; Before following a score, follow a timeline: a clave, a bell pattern, any rhythm that repeats. A pulse\n;; clock (a metro or the room's pulse, one bang a pulse) gives the grid; onsets (onset~'s bangs, or taps)\n;; fall on its nearest pulse. At each pulse the last pulses heard are compared with the reference\n;; (Toussaint's swap distance, on every rotation of the reference): the rotation that brings them nearest\n;; says which pulse of the reference this one is — the phase. Into a necklace's clock inlet, the phase moves\n;; its hand there (and plays it); times a pulse's beats, it is a player's position. Ties (a symmetric rhythm,\n;; few onsets yet) go to the pulse after the last phase, so the phase walks on unless what is heard says\n;; otherwise. Only onsets, no pitch: \"time by occasion\" at its smallest.\n(let [onsets-of (fn [ps] (vec (keep-indexed (fn [i x] (when (= 1 x) i)) ps)))\n pulses? (fn [v] (and (sequential? v) (seq v) (every? #{0 1} v)))\n ;; the directed swap distance between two lists of onsets of one segment (each onset of the shorter\n ;; list to its own of the other, in order, the least moving in all)\n assign (fn [us vs]\n (let [[a b] (if (<= (count us) (count vs)) [us vs] [vs us]) p (count a) q (count b)]\n (if (zero? p) 0\n (loop [i 0 row (vec (repeat (inc q) 0))]\n (if (= i p) (peek row)\n (recur (inc i)\n (reduce (fn [nr j] (conj nr (min (if (> j (inc i)) (peek nr) js/Infinity)\n (+ (nth row (dec j)) (js/Math.abs (- (nth a i) (nth b (dec j))))))))\n [js/Infinity] (range 1 (inc q)))))))))\n ;; how far the window w (the last m pulses heard, the last one now) is from the reference read so that\n ;; now is its pulse ph: the swaps between them, and two for each onset one has and the other has not\n continuity 1.5\n cost (fn [ref w ph]\n (let [n (count ref) m (count w)\n seg (mapv (fn [i] (nth ref (mod (+ (- ph (dec m)) i) n))) (range m))\n a (onsets-of w) b (onsets-of seg)]\n (+ (assign a b) (* 2 (js/Math.abs (- (count a) (count b)))))))\n ;; at each pulse: what the pulses before it say. The pulse that just began is not judged — its onset may\n ;; come a moment late (onset~ hears the attack after it) —: the phase of the last whole pulse is found,\n ;; and this one is the next. Ties, and near ties (less than a swap and a half), go to the phase the walk\n ;; expects, so a few missing or extra onsets do not throw it\n judge (fn [st]\n (let [ref (:ref st) n (count ref)\n ts (vec (take-last n (butlast (:ticks st)))) pulse (:pulse st)\n ;; a pulse is an onset when one was heard within half a pulse of it\n w (mapv (fn [t] (if (some (fn [o] (and (> o (- t (/ pulse 2))) (<= o (+ t (/ pulse 2))))) (:onsets st)) 1 0)) ts)\n m (count w)\n expected (:phase st)\n raw (vec (for [ph (range n)] (cost ref w ph)))\n cs (vec (map-indexed (fn [ph c] (+ c (if (and expected (not= ph expected)) continuity 0))) raw))\n best (apply min cs)\n ph (first (keep-indexed (fn [i c] (when (= c best) i)) cs))\n second-best (apply min (or (seq (keep-indexed (fn [i c] (when (not= i ph) c)) raw)) [(nth raw ph)]))\n ready (and pulse (>= m (:min st)) (>= (count (onsets-of w)) 2))]\n {:phase (mod (inc ph) n) :distance (nth raw ph) :margin (- second-best (nth raw ph)) :heard m :ready ready\n :window w :n n}))\n tick (fn [st t]\n (let [ticks (vec (take-last 257 (conj (:ticks st) t)))\n ;; the pulse: the median of the last intervals between bangs of the clock\n ivs (sort (map - (rest (take-last 9 ticks)) (butlast (take-last 9 ticks))))\n pulse (when (seq ivs) (nth ivs (quot (count ivs) 2)))\n st (assoc st :ticks ticks :pulse pulse\n :onsets (vec (filter (fn [o] (> o (- (first ticks) 1))) (:onsets st))))]\n (cond\n (not (:ref st)) {:state st :show \"no reference yet (middle)\"}\n (< (count ticks) 2) {:state st :show \"listening\"}\n :else (let [j (judge st)]\n (if-not (:ready j)\n {:state st :show (str \"listening · \" (:heard j) \"/\" (:min st))}\n {:state (assoc st :phase (:phase j))\n :show (str \"pulse \" (:phase j) \" of \" (:n j) \" · distance \" (:distance j))\n :out [[1 (dissoc j :ready)] [0 (:phase j)]]})))))\n onset (fn [st t] {:state (assoc st :onsets (conj (vec (take-last 256 (:onsets st))) t))})]\n {;; ── about ──\n :class \"phase\"\n :license \"MPL-2.0\"\n :doc \"Where a performer is in a cycle, from the onsets heard (discussion #204): a timeline follower, onsets only. The right inlet is the pulse clock (a metro or the room's pulse, a bang a pulse), the left one the onsets (onset~'s bangs, or taps), each falling on its nearest pulse; the middle one the reference, as pulses (cold: a clave, a bell pattern, euclid's or a necklace's). At each pulse the last pulses heard are compared with every rotation of the reference (Toussaint's swap distance, two more for each onset one has and the other has not): the nearest says which pulse of the reference this is — the phase, sent on the left once enough is heard (min pulses, half the cycle by default, and two onsets). The pulse that just began is not judged (its onset may come a moment late, as onset~ hears it): the last whole pulse is found and this one is the next. Ties and near ties (less than a swap and a half) go to the phase the walk expects, so a few missing or extra onsets do not throw it. Into a necklace's clock inlet the phase moves its hand there and plays it. Out on the right: {:phase :distance (0: exactly the reference) :margin (how much nearer than the next guess: 0 is ambiguous) :heard :window :n}. :reset forgets what was heard.\"\n :usage \"phase [min 0]\"\n :io [3 2]\n\n ;; ── arguments ──\n :args [[:min 0]]\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state args at]}]\n (let [t (or at (/ (js/Date.now) 1000))\n st (or state {:ticks [] :onsets []})\n min-of (fn [ref] (let [m (js/Math.round (or (:min args) 0))] (if (pos? m) m (max 2 (quot (count ref) 2)))))]\n (case inlet\n 1 (if (pulses? value) {:state (assoc st :ref (vec value) :min (min-of value) :phase nil)} {})\n 2 (if (#{:bang} value) (tick st t) {})\n (cond (#{:reset} value) {:state (assoc st :ticks [] :onsets [] :phase nil) :show \"listening\"}\n (or (#{:bang} value) (number? value)) (onset st t)\n :else {}))))})\n"
|
|
9
|
+
};
|
package/dist/swap.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Generated by packages/build.mjs from src/swap.cljs
|
|
2
2
|
export default {
|
|
3
3
|
"class": "swap",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.0",
|
|
5
5
|
"author": "bonuoq",
|
|
6
6
|
"license": "MPL-2.0",
|
|
7
7
|
"doc": "How far apart two rhythms are (Toussaint's swap distance). Both inlets take pulses of n pulses (euclid's, a necklace's outlet 2): the left one is compared with the right one, the reference (cold). A swap exchanges an onset and the silence beside it; the swap distance is how many turn one rhythm into the other — an onset moved far counts more than one moved near, as the Hamming distance (how many pulses differ) cannot tell. With different numbers of onsets, the directed swap distance (each onset of the shorter rhythm to its own of the other). Out, a map: {:swap — in the bar —, :circular — a swap may cross the end of the bar —, :necklace — the best over every rotation of the reference — and :rotation, the one that gives it, :hamming and :necklace-hamming, :n, :k [onsets of each]}. Rhythms of different lengths are not compared. A bang compares again.",
|
package/dist/vary.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Generated by packages/build.mjs from src/vary.cljs
|
|
2
2
|
export default {
|
|
3
3
|
"class": "vary",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.0",
|
|
5
5
|
"author": "bonuoq",
|
|
6
6
|
"license": "MPL-2.0",
|
|
7
7
|
"doc": "Again, but different (discussion #207): a variation of the rhythm on the left (pulses), drawn with a seed — the same seed, the same variations in the same order; each bang the next one, a new rhythm the first again. By default exactly d swaps away (Toussaint's swap distance): each swap takes an onset one pulse further from where it was, never past a neighbour (vary 1: one onset displaced). With depth, the onsets on the strong pulses down to metrical level d are kept (0 the downbeat, 1 the half bar…, the bar's levels its factors, twos first: 16 → 2·2·2·2, 12 → 2·2·3) and the others drawn again among the weaker pulses: the same skeleton, a new surface (discussion #321's \"vary from depth k\"). With downbeat the first pulse keeps what it has. Out: the variation (left, pulses) and how it was made (right): {:steps [[:swap from to] or [:remove i] [:add j] …] :mode :d :seed :draw, and :swaps (how many it could make) or :changed and :kept}. [:d 2], [:seed 7] (or a map of them) change it; :reset starts the draws again.",
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kairotic/rhythm",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Kairotic rhythm as geometry (after Toussaint): necklace, the rhythm box (a cyclic rhythm on the circle: drawn, edited, measured — interval vector, evenness, deep, flat, axes of symmetry, oddity, Fourier coefficients — and written as a musical tree whose :rtm merges with any pitches); generators of pulses (euclid, chance, homometric); swap, how far apart two rhythms are;
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "Kairotic rhythm as geometry (after Toussaint): necklace, the rhythm box (a cyclic rhythm on the circle: drawn, edited, measured — interval vector, evenness, deep, flat, axes of symmetry, oddity, Fourier coefficients — and written as a musical tree whose :rtm merges with any pitches); generators of pulses (euclid, chance, homometric); swap, how far apart two rhythms are; morph and vary, from one rhythm to another swap by swap, and again but different; and phase, where a performer is in a timeline, from the onsets heard",
|
|
5
5
|
"author": "bonuoq",
|
|
6
6
|
"license": "MPL-2.0",
|
|
7
7
|
"repository": {
|
|
@@ -42,6 +42,9 @@
|
|
|
42
42
|
"swap distance",
|
|
43
43
|
"homometric",
|
|
44
44
|
"morph",
|
|
45
|
-
"variation"
|
|
45
|
+
"variation",
|
|
46
|
+
"timeline",
|
|
47
|
+
"phase",
|
|
48
|
+
"following"
|
|
46
49
|
]
|
|
47
50
|
}
|
package/src/phase.cljs
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
;; phase · where a performer is in a cycle, from the onsets heard (discussion #204)
|
|
2
|
+
;; Part of @kairotic/rhythm. Written against the Kairotic API (api/…).
|
|
3
|
+
;; Before following a score, follow a timeline: a clave, a bell pattern, any rhythm that repeats. A pulse
|
|
4
|
+
;; clock (a metro or the room's pulse, one bang a pulse) gives the grid; onsets (onset~'s bangs, or taps)
|
|
5
|
+
;; fall on its nearest pulse. At each pulse the last pulses heard are compared with the reference
|
|
6
|
+
;; (Toussaint's swap distance, on every rotation of the reference): the rotation that brings them nearest
|
|
7
|
+
;; says which pulse of the reference this one is — the phase. Into a necklace's clock inlet, the phase moves
|
|
8
|
+
;; its hand there (and plays it); times a pulse's beats, it is a player's position. Ties (a symmetric rhythm,
|
|
9
|
+
;; few onsets yet) go to the pulse after the last phase, so the phase walks on unless what is heard says
|
|
10
|
+
;; otherwise. Only onsets, no pitch: "time by occasion" at its smallest.
|
|
11
|
+
(let [onsets-of (fn [ps] (vec (keep-indexed (fn [i x] (when (= 1 x) i)) ps)))
|
|
12
|
+
pulses? (fn [v] (and (sequential? v) (seq v) (every? #{0 1} v)))
|
|
13
|
+
;; the directed swap distance between two lists of onsets of one segment (each onset of the shorter
|
|
14
|
+
;; list to its own of the other, in order, the least moving in all)
|
|
15
|
+
assign (fn [us vs]
|
|
16
|
+
(let [[a b] (if (<= (count us) (count vs)) [us vs] [vs us]) p (count a) q (count b)]
|
|
17
|
+
(if (zero? p) 0
|
|
18
|
+
(loop [i 0 row (vec (repeat (inc q) 0))]
|
|
19
|
+
(if (= i p) (peek row)
|
|
20
|
+
(recur (inc i)
|
|
21
|
+
(reduce (fn [nr j] (conj nr (min (if (> j (inc i)) (peek nr) js/Infinity)
|
|
22
|
+
(+ (nth row (dec j)) (js/Math.abs (- (nth a i) (nth b (dec j))))))))
|
|
23
|
+
[js/Infinity] (range 1 (inc q)))))))))
|
|
24
|
+
;; how far the window w (the last m pulses heard, the last one now) is from the reference read so that
|
|
25
|
+
;; now is its pulse ph: the swaps between them, and two for each onset one has and the other has not
|
|
26
|
+
continuity 1.5
|
|
27
|
+
cost (fn [ref w ph]
|
|
28
|
+
(let [n (count ref) m (count w)
|
|
29
|
+
seg (mapv (fn [i] (nth ref (mod (+ (- ph (dec m)) i) n))) (range m))
|
|
30
|
+
a (onsets-of w) b (onsets-of seg)]
|
|
31
|
+
(+ (assign a b) (* 2 (js/Math.abs (- (count a) (count b)))))))
|
|
32
|
+
;; at each pulse: what the pulses before it say. The pulse that just began is not judged — its onset may
|
|
33
|
+
;; come a moment late (onset~ hears the attack after it) —: the phase of the last whole pulse is found,
|
|
34
|
+
;; and this one is the next. Ties, and near ties (less than a swap and a half), go to the phase the walk
|
|
35
|
+
;; expects, so a few missing or extra onsets do not throw it
|
|
36
|
+
judge (fn [st]
|
|
37
|
+
(let [ref (:ref st) n (count ref)
|
|
38
|
+
ts (vec (take-last n (butlast (:ticks st)))) pulse (:pulse st)
|
|
39
|
+
;; a pulse is an onset when one was heard within half a pulse of it
|
|
40
|
+
w (mapv (fn [t] (if (some (fn [o] (and (> o (- t (/ pulse 2))) (<= o (+ t (/ pulse 2))))) (:onsets st)) 1 0)) ts)
|
|
41
|
+
m (count w)
|
|
42
|
+
expected (:phase st)
|
|
43
|
+
raw (vec (for [ph (range n)] (cost ref w ph)))
|
|
44
|
+
cs (vec (map-indexed (fn [ph c] (+ c (if (and expected (not= ph expected)) continuity 0))) raw))
|
|
45
|
+
best (apply min cs)
|
|
46
|
+
ph (first (keep-indexed (fn [i c] (when (= c best) i)) cs))
|
|
47
|
+
second-best (apply min (or (seq (keep-indexed (fn [i c] (when (not= i ph) c)) raw)) [(nth raw ph)]))
|
|
48
|
+
ready (and pulse (>= m (:min st)) (>= (count (onsets-of w)) 2))]
|
|
49
|
+
{:phase (mod (inc ph) n) :distance (nth raw ph) :margin (- second-best (nth raw ph)) :heard m :ready ready
|
|
50
|
+
:window w :n n}))
|
|
51
|
+
tick (fn [st t]
|
|
52
|
+
(let [ticks (vec (take-last 257 (conj (:ticks st) t)))
|
|
53
|
+
;; the pulse: the median of the last intervals between bangs of the clock
|
|
54
|
+
ivs (sort (map - (rest (take-last 9 ticks)) (butlast (take-last 9 ticks))))
|
|
55
|
+
pulse (when (seq ivs) (nth ivs (quot (count ivs) 2)))
|
|
56
|
+
st (assoc st :ticks ticks :pulse pulse
|
|
57
|
+
:onsets (vec (filter (fn [o] (> o (- (first ticks) 1))) (:onsets st))))]
|
|
58
|
+
(cond
|
|
59
|
+
(not (:ref st)) {:state st :show "no reference yet (middle)"}
|
|
60
|
+
(< (count ticks) 2) {:state st :show "listening"}
|
|
61
|
+
:else (let [j (judge st)]
|
|
62
|
+
(if-not (:ready j)
|
|
63
|
+
{:state st :show (str "listening · " (:heard j) "/" (:min st))}
|
|
64
|
+
{:state (assoc st :phase (:phase j))
|
|
65
|
+
:show (str "pulse " (:phase j) " of " (:n j) " · distance " (:distance j))
|
|
66
|
+
:out [[1 (dissoc j :ready)] [0 (:phase j)]]})))))
|
|
67
|
+
onset (fn [st t] {:state (assoc st :onsets (conj (vec (take-last 256 (:onsets st))) t))})]
|
|
68
|
+
{;; ── about ──
|
|
69
|
+
:class "phase"
|
|
70
|
+
:license "MPL-2.0"
|
|
71
|
+
:doc "Where a performer is in a cycle, from the onsets heard (discussion #204): a timeline follower, onsets only. The right inlet is the pulse clock (a metro or the room's pulse, a bang a pulse), the left one the onsets (onset~'s bangs, or taps), each falling on its nearest pulse; the middle one the reference, as pulses (cold: a clave, a bell pattern, euclid's or a necklace's). At each pulse the last pulses heard are compared with every rotation of the reference (Toussaint's swap distance, two more for each onset one has and the other has not): the nearest says which pulse of the reference this is — the phase, sent on the left once enough is heard (min pulses, half the cycle by default, and two onsets). The pulse that just began is not judged (its onset may come a moment late, as onset~ hears it): the last whole pulse is found and this one is the next. Ties and near ties (less than a swap and a half) go to the phase the walk expects, so a few missing or extra onsets do not throw it. Into a necklace's clock inlet the phase moves its hand there and plays it. Out on the right: {:phase :distance (0: exactly the reference) :margin (how much nearer than the next guess: 0 is ambiguous) :heard :window :n}. :reset forgets what was heard."
|
|
72
|
+
:usage "phase [min 0]"
|
|
73
|
+
:io [3 2]
|
|
74
|
+
|
|
75
|
+
;; ── arguments ──
|
|
76
|
+
:args [[:min 0]]
|
|
77
|
+
|
|
78
|
+
;; ── logic ──
|
|
79
|
+
:receive (fn [{:keys [inlet value state args at]}]
|
|
80
|
+
(let [t (or at (/ (js/Date.now) 1000))
|
|
81
|
+
st (or state {:ticks [] :onsets []})
|
|
82
|
+
min-of (fn [ref] (let [m (js/Math.round (or (:min args) 0))] (if (pos? m) m (max 2 (quot (count ref) 2)))))]
|
|
83
|
+
(case inlet
|
|
84
|
+
1 (if (pulses? value) {:state (assoc st :ref (vec value) :min (min-of value) :phase nil)} {})
|
|
85
|
+
2 (if (#{:bang} value) (tick st t) {})
|
|
86
|
+
(cond (#{:reset} value) {:state (assoc st :ticks [] :onsets [] :phase nil) :show "listening"}
|
|
87
|
+
(or (#{:bang} value) (number? value)) (onset st t)
|
|
88
|
+
:else {}))))})
|