@kairotic/rhythm 0.4.0 → 0.5.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 CHANGED
@@ -18,6 +18,8 @@ is their editor, measures them and writes them as music.
18
18
  | `chance [k 3] [n 8] [seed 1]` | **a generator**: k onsets among n pulses drawn with a seed — each bang another rhythm, the same seed the same rhythms in the same order (in a room, from the history); `:reset` starts them again |
19
19
  | `homometric` | **a generator from a rhythm**: the other rhythms with the same interval vector, not the same necklace turned or in a mirror (the two flat rhythms of 4 in 12; a half of an even cycle and its complement) — a list of pulses, and how many |
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
+ | `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
+ | `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 |
21
23
 
22
24
  ```clojure
23
25
  (box :b1 "msg [:rotation 6]" 40 20)
@@ -99,4 +101,10 @@ the rumba and 1 from the bossa nova, and 2 by Hamming from both. With different
99
101
  swap distance (the tresillo is 0 from the cinquillo: it is inside it). `:circular` lets a swap cross the end of the
100
102
  bar; `:necklace` turns the reference to where it comes nearest, and `:rotation` says how far.
101
103
 
104
+ **morph** and **vary** make what `swap` measures: their steps (`[:swap from to]`, `[:remove i]`, `[:add j]`) are the
105
+ operations a swap distance counts, so the way between two rhythms or a variation of one is data the history can
106
+ keep and cite — one grammar read both ways (discussions #206, #207 and #321). A ratio moved by a knob, a `ramp` or a
107
+ line is a morph in time; a `vary` after a `necklace` playing step by step, banged once a bar, is a timeline that
108
+ keeps changing by a measured amount.
109
+
102
110
  Writing one of its classes in a work brings the package into it. Licence: [MPL-2.0](../../LICENSE).
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.0",
4
+ "version": "0.5.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.0",
4
+ "version": "0.5.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).",
@@ -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.0",
4
+ "version": "0.5.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.0",
4
+ "version": "0.5.0",
5
5
  "api": 1,
6
6
  "author": "bonuoq",
7
7
  "license": "MPL-2.0",
@@ -24,6 +24,12 @@ export default {
24
24
  "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.",
25
25
  "source": ";; homometric · the other rhythms with the same interval vector\n;; Part of @kairotic/rhythm. Written against the Kairotic API (api/…).\n;; Two rhythms are homometric when they hold the same distances between onsets, as many times each — the\n;; same interval vector, the same \"histogram\" — without being the same necklace turned or seen in a mirror\n;; (Toussaint, after crystallography: Patterson, Rosenblatt and Seymour). The two flat rhythms of four onsets\n;; in twelve are homometric; so is any half of the pulses of an even cycle with its complement, which gives two\n;; drums (high and low congas) the same intervals. Given a rhythm as pulses, it searches every rhythm of as many\n;; onsets among as many pulses (up to 7 000 of them: any rhythm of 16 pulses; a moment for the largest) and sends the others it finds, each from its first onset, as a list of\n;; pulses (for nth, or a necklace one by one), and how many there are.\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 dist (fn [n a b] (let [d (js/Math.abs (- a b))] (min d (- n d))))\n ivec-of (fn [n os] (let [c (frequencies (for [i (range (count os)) j (range (inc i) (count os))] (dist n (nth os i) (nth os j))))]\n (mapv (fn [d] (get c d 0)) (range 1 (inc (quot n 2))))))\n ;; a necklace up to rotation and mirror, as its smallest form starting at 0 (a bracelet)\n bracelet (fn [n os]\n (first (sort (for [f [identity (fn [x] (mod (- n x) n))] r (range n)]\n (vec (sort (map (fn [x] (mod (- (f x) r) n)) os)))))))\n ;; the subsets of 1…n−1 of size m (0 is always an onset: rotations are the same necklace)\n subsets (fn subsets [from n m]\n (cond (zero? m) [[]]\n (> m (- n from)) []\n :else (concat (map (fn [s] (cons from s)) (subsets (inc from) n (dec m))) (subsets (inc from) n m))))\n ;; how many rhythms the search goes through: C(n−1, k−1)\n choose (fn [a b] (reduce (fn [acc i] (/ (* acc (- a i)) (inc i))) 1 (range (min b (- a b)))))\n limit 7000\n search (fn [ps]\n (let [n (count ps) os (onsets-of ps) k (count os) target (ivec-of n os) own (bracelet n os)]\n (if (zero? k) []\n (->> (subsets 1 n (dec k))\n (map (fn [s] (vec (cons 0 s))))\n (filter (fn [o] (= target (ivec-of n o))))\n (map (fn [o] (bracelet n o)))\n distinct\n (remove (fn [b] (= b own)))\n sort\n (mapv (fn [b] (let [on (set b)] (mapv (fn [i] (if (on i) 1 0)) (range n)))))))))]\n {;; ── about ──\n :class \"homometric\"\n :license \"MPL-2.0\"\n :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.\"\n :usage \"homometric\"\n :io [1 2]\n\n ;; ── logic ──\n :receive (fn [{:keys [value state]}]\n (let [st (or state {})\n go (fn [ps] (if (> (choose (dec (count ps)) (max 0 (dec (count (onsets-of ps))))) limit)\n {:state (assoc st :ps ps) :show (str \"too many to search (more than \" limit \" rhythms)\")}\n (let [hs (search ps)]\n {:state (assoc st :ps ps) :show (str (count hs) \" homometric\")\n :out [[1 (count hs)] [0 hs]]})))]\n (cond\n (and (#{:bang} value) (:ps st)) (go (:ps st))\n (pulses? value) (if (= (vec value) (:ps st)) {:state st} (go (vec value)))\n :else {})))})\n"
26
26
  },
27
+ {
28
+ "class": "morph",
29
+ "license": "MPL-2.0",
30
+ "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).",
31
+ "source": ";; morph · from one rhythm to another, swap by swap\n;; Part of @kairotic/rhythm. Written against the Kairotic API (api/…).\n;; Toussaint's swap distance does not only measure how far two rhythms are: it gives the way, the swaps\n;; (an onset exchanged with the silence beside it) that turn one into the other with the fewest moves. A\n;; ratio 0…1 is a point on that way: the rhythm after that share of its swaps — always a real rhythm, never a\n;; blurred average (discussion #206: a merge of two branches as a musical operation). The way is sent as data\n;; too, a derivation of steps — the operations a distance counts (parse) are the ones a morph or a variation\n;; makes (generate), the same grammar read both ways (discussion #321).\n;; Onsets are paired in order; with different numbers of onsets, as the directed swap distance pairs them\n;; (each onset of the shorter rhythm to its own of the other): the ones left over go first (from A) or\n;; appear last (into B). The swaps take turns, one pulse each, so the rhythm changes evenly along the way.\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 onsets of the shorter list paired, in order, with those of the longer one, the least moving in\n ;; all: [[u v] …] and the onsets of the longer one left over\n pairing (fn [us vs]\n (let [flip? (> (count us) (count vs)) [a b] (if flip? [vs us] [us vs]) p (count a) q (count b)\n d (fn [x y] (js/Math.abs (- x y)))\n ;; cost[i][j]: the first i of a among the first j of b; choice to walk back\n table (reduce (fn [rows i]\n (conj rows (reduce (fn [row j]\n (conj row (min (if (> j i) (peek row) js/Infinity)\n (+ (get-in rows [(dec i) (dec j)]) (d (nth a (dec i)) (nth b (dec j)))))))\n [js/Infinity] (range 1 (inc q)))))\n [(vec (repeat (inc q) 0))] (range 1 (inc p)))\n pairs (loop [i p j q acc ()]\n (cond (zero? i) acc\n (and (> j i) (= (get-in table [i j]) (get-in table [i (dec j)]))) (recur i (dec j) acc)\n :else (recur (dec i) (dec j) (conj acc [(nth a (dec i)) (nth b (dec j))]))))\n used (set (map second pairs))\n left (vec (remove used b))]\n (if flip?\n {:pairs (mapv (fn [[x y]] [y x]) pairs) :removed left :added []}\n {:pairs (vec pairs) :removed [] :added left})))\n ;; the way: first the onsets of A left over go, then the paired ones move a pulse at a time, taking turns\n ;; (one blocked by a neighbour waits for it), then the onsets of B left over appear\n way (fn [A B]\n (let [n (count A) {:keys [pairs removed added]} (pairing (onsets-of A) (onsets-of B))\n start (reduce (fn [ps i] (assoc ps i 0)) (vec A) removed)\n k (count pairs)\n ;; taking turns: after onset i moves, the search for the next move starts at i + 1\n moves (loop [ps start pos (mapv first pairs) nxt 0 acc []]\n (let [mv (first (for [o (range k)\n :let [i (mod (+ nxt o) k) p (nth pos i) t (second (nth pairs i))]\n :when (not= p t)\n :let [q (if (< p t) (inc p) (dec p))]\n :when (and (< -1 q n) (zero? (nth ps q)))]\n [i p q]))]\n (if-not mv\n acc\n (let [[i p q] mv]\n (recur (assoc ps p 0 q 1) (assoc pos i q) (inc i) (conj acc [:swap p q]))))))]\n (vec (concat (map (fn [i] [:remove i]) removed) moves (map (fn [j] [:add j]) added)))))\n apply-step (fn [ps [op a b]] (case op :swap (assoc ps a 0 b 1) :remove (assoc ps a 0) :add (assoc ps a 1) ps))\n at (fn [A steps t] (reduce apply-step (vec A) (take (js/Math.round (* (api/clamp t 0 1) (count steps))) steps)))\n send (fn [st]\n (let [{:keys [a b t]} st]\n (if (and a b (= (count a) (count b)))\n (let [steps (way a b) r (at a steps t)\n n-swaps (count (filter (fn [s] (= :swap (first s))) steps))]\n {:state (assoc st :steps steps)\n :show (str \"→ \" (js/Math.round (* 100 t)) \"% · \" n-swaps \" swaps\")\n :out [[1 {:steps steps :swaps n-swaps :at (js/Math.round (* t (count steps))) :of (count steps)}] [0 r]]})\n {:state st :show (cond (nil? a) \"no rhythm yet (left)\" (nil? b) \"no target yet (middle)\"\n :else (str (count a) \" pulses towards \" (count b) \": not morphed\"))})))]\n {;; ── about ──\n :class \"morph\"\n :license \"MPL-2.0\"\n :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).\"\n :usage \"morph [ratio 0]\"\n :io [3 2]\n\n ;; ── arguments ──\n :args [[:ratio 0]]\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state args]}]\n (let [st (or state {:t (api/clamp (or (:ratio args) 0) 0 1)})]\n (case inlet\n 1 (if (pulses? value) {:state (assoc st :b (vec value))} {})\n 2 (if (number? value) (send (assoc st :t (api/clamp value 0 1))) {})\n (cond (#{:bang} value) (send st)\n (pulses? value) (send (assoc st :a (vec value)))\n (number? value) (send (assoc st :t (api/clamp value 0 1)))\n :else {}))))})\n"
32
+ },
27
33
  {
28
34
  "class": "necklace",
29
35
  "license": "MPL-2.0",
@@ -35,6 +41,12 @@ export default {
35
41
  "license": "MPL-2.0",
36
42
  "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.",
37
43
  "source": ";; swap · how far apart two rhythms are (Toussaint's swap distance)\n;; Part of @kairotic/rhythm. Written against the Kairotic API (api/…).\n;; Two rhythms of n pulses, as pulses. A swap exchanges an onset and the silence beside it, and the swap\n;; distance is how many of them turn one rhythm into the other: an onset moved far counts more than one moved\n;; near, which the Hamming distance (how many pulses differ) cannot tell. With the same number of onsets it\n;; is the sum of |uᵢ − vᵢ| over the onsets in order; with different numbers, the directed swap distance (each\n;; onset of the shorter rhythm goes to its own onset of the other, the least moving in all). Around the circle\n;; a swap may cross the end of the bar; as necklaces, the rhythms are also compared at every rotation (the\n;; one that brings them closest is said). For the futuribles and the agents: is a variation still the same\n;; timeline? Both inlets take pulses (euclid's, a necklace's outlet 2); the left one compares, the right one\n;; is the reference (cold).\n(let [onsets-of (fn [ps] (vec (keep-indexed (fn [i x] (when (= 1 x) i)) ps)))\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 pulses? (fn [v] (and (sequential? v) (seq v) (every? #{0 1} v)))\n cdist (fn [n a b] (let [d (js/Math.abs (- a b))] (min d (- n d))))\n ;; onsets in order, each of the shorter list to its own of the longer one, the least moving in all\n ;; (dynamic programming over both lists: the directed swap distance; with equal lengths, Σ|uᵢ − vᵢ|)\n assign (fn [d us vs]\n (let [[a b] (if (<= (count us) (count vs)) [us vs] [vs us]) p (count a) q (count b)]\n (cond (zero? p) 0\n :else (loop [i 0 row (vec (repeat (inc q) 0))]\n (if (= i p)\n (peek row)\n (recur (inc i)\n (reduce (fn [nr j]\n (conj nr (min (if (> j (inc i)) (peek nr) js/Infinity)\n (+ (nth row (dec j)) (d (nth a i) (nth b (dec j)))))))\n [js/Infinity] (range 1 (inc q)))))))))\n linear (fn [A B] (assign (fn [x y] (js/Math.abs (- x y))) (onsets-of A) (onsets-of B)))\n ;; around the circle: with as many onsets, the best cyclic shift of the pairing; otherwise the best place\n ;; to cut the circle open\n circular (fn [A B]\n (let [n (count A) us (onsets-of A) vs (onsets-of B) k (count us)]\n (cond (or (empty? us) (empty? vs)) (if (= (count us) (count vs)) 0 nil)\n (= k (count vs)) (apply min (for [s (range k)] (reduce + (map (fn [i] (cdist n (nth us i) (nth vs (mod (+ i s) k)))) (range k)))))\n :else (apply min (for [c (range n)]\n (assign (fn [x y] (cdist n x y)) (onsets-of (rotate A c)) (onsets-of (rotate B c))))))))\n hamming (fn [A B] (count (filter false? (map = A B))))\n compare-them (fn [A B]\n (let [n (count A) ka (count (onsets-of A)) kb (count (onsets-of B))\n ;; a rhythm without onsets is not far from another one by swaps: only from silence\n none? (and (or (zero? ka) (zero? kb)) (not= ka kb))\n rs (vec (for [r (range n)] (circular A (rotate B r))))\n best (when-not none? (apply min rs))\n hs (vec (for [r (range n)] (hamming A (rotate B r))))]\n {:n n :k [ka kb]\n :swap (when-not none? (linear A B)) :circular (when-not none? (circular A B))\n :necklace best :rotation (when best (first (keep-indexed (fn [r x] (when (= x best) r)) rs)))\n :hamming (hamming A B) :necklace-hamming (apply min hs)}))]\n {;; ── about ──\n :class \"swap\"\n :license \"MPL-2.0\"\n :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.\"\n :usage \"swap\"\n :io [2 1]\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state]}]\n (let [st (or state {})]\n (cond\n (= inlet 1) (if (pulses? value) {:state (assoc st :ref (vec value))} {})\n (and (#{:bang} value) (:a st)) (let [A (:a st) B (:ref st)]\n (if (and B (= (count A) (count B))) {:out [[0 (compare-them A B)]]} {}))\n (pulses? value) (let [A (vec value) B (:ref st)]\n (cond (nil? B) {:state (assoc st :a A) :show \"no reference yet (right inlet)\"}\n (not= (count A) (count B)) {:state (assoc st :a A) :show (str (count A) \" pulses against \" (count B) \": not compared\")}\n :else (let [m (compare-them A B)]\n {:state (assoc st :a A) :show (str \"swap \" (:swap m) \" · necklace \" (:necklace m)) :out [[0 m]]})))\n :else {})))})\n"
44
+ },
45
+ {
46
+ "class": "vary",
47
+ "license": "MPL-2.0",
48
+ "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.",
49
+ "source": ";; vary · again, but different: a rhythm d swaps away, or redrawn below a metrical depth\n;; Part of @kairotic/rhythm. Written against the Kairotic API (api/…).\n;; Discussion #207 (redo with variation): \"again, but different\" with a measured likeness. Two readings of\n;; how different, both drawn with a seed (the same seed, the same variations in the same order: a room and\n;; the history hear the same; each bang the next one):\n;; swaps (by default) exactly d swaps away (Toussaint's swap distance): each swap takes an onset one pulse\n;; further from where it was — d = 1 is the same timeline with one onset displaced;\n;; depth the onsets on the strong pulses kept, down to metrical level d (0 the downbeat, 1\n;; the half bar…), the others drawn again among the weaker pulses — the same skeleton,\n;; a new surface: discussion #321's \"vary from depth k\", for rhythm, with the bar's\n;; metrical levels (twos, then threes: 16 pulses 2·2·2·2, 12 pulses 2·2·3) as its grammar.\n;; With downbeat the first pulse keeps what it has. The variation goes out as pulses, and how it was made as\n;; data: the steps from the rhythm given ([:swap from to], or [:remove i] [:add j]), and how far it went.\n(let [M 2147483647\n rng (fn [seed i]\n (let [s (volatile! (inc (mod (+ (* 7919 (js/Math.abs (js/Math.round seed))) (* 104729 i) 31337) (dec M))))]\n (dotimes [_ 4] (vreset! s (mod (* @s 16807) M)))\n (fn [] (vreset! s (mod (* @s 16807) M)) (/ (dec @s) (dec M)))))\n 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 metrical level of each of n pulses: n's factors, twos first (16 → 2·2·2·2, 12 → 2·2·3, 6 → 2·3);\n ;; pulse i is at the first level whose division reaches it (0 the downbeat)\n factors (fn [n] (let [fs (loop [m n f 2 acc []] (cond (= m 1) acc (zero? (mod m f)) (recur (/ m f) f (conj acc f)) :else (recur m (inc f) acc)))]\n (vec (sort fs))))\n levels (fn [n] (let [fs (factors n)\n spans (reductions (fn [span f] (/ span f)) n fs)] ; n, n/f1, n/f1/f2 … 1\n (mapv (fn [i] (first (keep-indexed (fn [l span] (when (zero? (mod i span)) l)) spans))) (range n))))\n ;; d swaps, each taking an onset one pulse further from where it was (never past a neighbour)\n by-swaps (fn [ps d r keep0?]\n (let [n (count ps) home (onsets-of ps)]\n (loop [cur ps pos home left d steps []]\n (let [opts (vec (for [i (range (count pos)) dir [-1 1]\n :let [p (nth pos i) q (+ p dir) h (nth home i)]\n :when (and (< -1 q n) (zero? (nth cur q))\n (> (js/Math.abs (- q h)) (js/Math.abs (- p h)))\n (not (and keep0? (or (= p 0) (= q 0)))))]\n [i p q]))]\n (if (or (zero? left) (empty? opts))\n {:pulses cur :steps steps :went (- d left)}\n (let [[i p q] (nth opts (js/Math.floor (* (r) (count opts))))]\n (recur (assoc cur p 0 q 1) (assoc pos i q) (dec left) (conj steps [:swap p q]))))))))\n ;; the onsets down to level k kept; the others drawn again among the pulses deeper than k\n by-depth (fn [ps k r keep0?]\n (let [n (count ps) lv (levels n)\n kept? (fn [i] (or (<= (nth lv i) k) (and keep0? (zero? i))))\n free (vec (filter (fn [i] (not (kept? i))) (range n)))\n moving (vec (filter (fn [i] (and (= 1 (nth ps i)) (not (kept? i)))) (range n)))\n chosen (set (take (count moving) (loop [v free j 0]\n (if (< j (count moving))\n (let [m (+ j (js/Math.floor (* (r) (- (count v) j))))]\n (recur (assoc v j (nth v m) m (nth v j)) (inc j)))\n v))))\n out (mapv (fn [i] (if (kept? i) (nth ps i) (if (chosen i) 1 0))) (range n))\n gone (vec (remove chosen moving)) came (vec (remove (set moving) (sort chosen)))]\n {:pulses out :steps (vec (concat (map (fn [i] [:remove i]) gone) (map (fn [j] [:add j]) came)))\n :went (count gone) :kept (vec (filter (fn [i] (and (kept? i) (= 1 (nth ps i)))) (range n)))}))\n params (fn [args over] (let [o (merge args over)] {:d (max 0 (js/Math.round (or (:d o) 1))) :seed (or (:seed o) 1)\n :depth (boolean (:depth o)) :downbeat (boolean (:downbeat o))}))\n draw (fn [st args]\n (let [{:keys [d seed depth downbeat]} (params args (:over st)) r (rng seed (:i st))\n v (if depth (by-depth (:ps st) d r downbeat) (by-swaps (:ps st) d r downbeat))]\n {:state (update st :i inc)\n :show (str (if depth (str \"from depth \" d) (str d \" swap\" (when (not= 1 d) \"s\"))) \" · seed \" seed \" · \" (inc (:i st)))\n :out [[1 (merge {:steps (:steps v) :mode (if depth :depth :swaps) :d d :seed seed :draw (inc (:i st))}\n (if depth {:changed (:went v) :kept (:kept v)} {:swaps (:went v)}))]\n [0 (:pulses v)]]}))]\n {;; ── about ──\n :class \"vary\"\n :license \"MPL-2.0\"\n :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.\"\n :usage \"vary [d 1] [seed 1] [depth] [downbeat]\"\n :io [1 2]\n\n ;; ── arguments ──\n :args [[:d 1] [:seed 1] [:depth false] [:downbeat false]]\n\n ;; ── logic ──\n :receive (fn [{:keys [value state args]}]\n (let [st (or state {:over {} :i 0})]\n (cond\n (pulses? value) (if (= (vec value) (:ps st)) {:state st} (draw (assoc st :ps (vec value) :i 0) args))\n (and (#{:bang} value) (:ps st)) (draw st args)\n (#{:reset} value) {:state (assoc st :i 0)}\n (and (vector? value) (= 2 (count value)) (#{:d :seed} (first value)) (number? (second value)))\n (let [s2 (assoc st :over (assoc (:over st) (first value) (second value)) :i 0)] (if (:ps s2) (draw s2 args) {:state s2}))\n (map? value) (let [s2 (assoc st :over (merge (:over st) (select-keys value [:d :seed :depth :downbeat])) :i 0)]\n (if (:ps s2) (draw s2 args) {:state s2}))\n :else {})))})\n"
38
50
  }
39
51
  ]
40
52
  };
package/dist/morph.mjs ADDED
@@ -0,0 +1,9 @@
1
+ // Generated by packages/build.mjs from src/morph.cljs
2
+ export default {
3
+ "class": "morph",
4
+ "version": "0.5.0",
5
+ "author": "bonuoq",
6
+ "license": "MPL-2.0",
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).",
8
+ "source": ";; morph · from one rhythm to another, swap by swap\n;; Part of @kairotic/rhythm. Written against the Kairotic API (api/…).\n;; Toussaint's swap distance does not only measure how far two rhythms are: it gives the way, the swaps\n;; (an onset exchanged with the silence beside it) that turn one into the other with the fewest moves. A\n;; ratio 0…1 is a point on that way: the rhythm after that share of its swaps — always a real rhythm, never a\n;; blurred average (discussion #206: a merge of two branches as a musical operation). The way is sent as data\n;; too, a derivation of steps — the operations a distance counts (parse) are the ones a morph or a variation\n;; makes (generate), the same grammar read both ways (discussion #321).\n;; Onsets are paired in order; with different numbers of onsets, as the directed swap distance pairs them\n;; (each onset of the shorter rhythm to its own of the other): the ones left over go first (from A) or\n;; appear last (into B). The swaps take turns, one pulse each, so the rhythm changes evenly along the way.\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 onsets of the shorter list paired, in order, with those of the longer one, the least moving in\n ;; all: [[u v] …] and the onsets of the longer one left over\n pairing (fn [us vs]\n (let [flip? (> (count us) (count vs)) [a b] (if flip? [vs us] [us vs]) p (count a) q (count b)\n d (fn [x y] (js/Math.abs (- x y)))\n ;; cost[i][j]: the first i of a among the first j of b; choice to walk back\n table (reduce (fn [rows i]\n (conj rows (reduce (fn [row j]\n (conj row (min (if (> j i) (peek row) js/Infinity)\n (+ (get-in rows [(dec i) (dec j)]) (d (nth a (dec i)) (nth b (dec j)))))))\n [js/Infinity] (range 1 (inc q)))))\n [(vec (repeat (inc q) 0))] (range 1 (inc p)))\n pairs (loop [i p j q acc ()]\n (cond (zero? i) acc\n (and (> j i) (= (get-in table [i j]) (get-in table [i (dec j)]))) (recur i (dec j) acc)\n :else (recur (dec i) (dec j) (conj acc [(nth a (dec i)) (nth b (dec j))]))))\n used (set (map second pairs))\n left (vec (remove used b))]\n (if flip?\n {:pairs (mapv (fn [[x y]] [y x]) pairs) :removed left :added []}\n {:pairs (vec pairs) :removed [] :added left})))\n ;; the way: first the onsets of A left over go, then the paired ones move a pulse at a time, taking turns\n ;; (one blocked by a neighbour waits for it), then the onsets of B left over appear\n way (fn [A B]\n (let [n (count A) {:keys [pairs removed added]} (pairing (onsets-of A) (onsets-of B))\n start (reduce (fn [ps i] (assoc ps i 0)) (vec A) removed)\n k (count pairs)\n ;; taking turns: after onset i moves, the search for the next move starts at i + 1\n moves (loop [ps start pos (mapv first pairs) nxt 0 acc []]\n (let [mv (first (for [o (range k)\n :let [i (mod (+ nxt o) k) p (nth pos i) t (second (nth pairs i))]\n :when (not= p t)\n :let [q (if (< p t) (inc p) (dec p))]\n :when (and (< -1 q n) (zero? (nth ps q)))]\n [i p q]))]\n (if-not mv\n acc\n (let [[i p q] mv]\n (recur (assoc ps p 0 q 1) (assoc pos i q) (inc i) (conj acc [:swap p q]))))))]\n (vec (concat (map (fn [i] [:remove i]) removed) moves (map (fn [j] [:add j]) added)))))\n apply-step (fn [ps [op a b]] (case op :swap (assoc ps a 0 b 1) :remove (assoc ps a 0) :add (assoc ps a 1) ps))\n at (fn [A steps t] (reduce apply-step (vec A) (take (js/Math.round (* (api/clamp t 0 1) (count steps))) steps)))\n send (fn [st]\n (let [{:keys [a b t]} st]\n (if (and a b (= (count a) (count b)))\n (let [steps (way a b) r (at a steps t)\n n-swaps (count (filter (fn [s] (= :swap (first s))) steps))]\n {:state (assoc st :steps steps)\n :show (str \"→ \" (js/Math.round (* 100 t)) \"% · \" n-swaps \" swaps\")\n :out [[1 {:steps steps :swaps n-swaps :at (js/Math.round (* t (count steps))) :of (count steps)}] [0 r]]})\n {:state st :show (cond (nil? a) \"no rhythm yet (left)\" (nil? b) \"no target yet (middle)\"\n :else (str (count a) \" pulses towards \" (count b) \": not morphed\"))})))]\n {;; ── about ──\n :class \"morph\"\n :license \"MPL-2.0\"\n :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).\"\n :usage \"morph [ratio 0]\"\n :io [3 2]\n\n ;; ── arguments ──\n :args [[:ratio 0]]\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state args]}]\n (let [st (or state {:t (api/clamp (or (:ratio args) 0) 0 1)})]\n (case inlet\n 1 (if (pulses? value) {:state (assoc st :b (vec value))} {})\n 2 (if (number? value) (send (assoc st :t (api/clamp value 0 1))) {})\n (cond (#{:bang} value) (send st)\n (pulses? value) (send (assoc st :a (vec value)))\n (number? value) (send (assoc st :t (api/clamp value 0 1)))\n :else {}))))})\n"
9
+ };
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.0",
4
+ "version": "0.5.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/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.0",
4
+ "version": "0.5.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 ADDED
@@ -0,0 +1,9 @@
1
+ // Generated by packages/build.mjs from src/vary.cljs
2
+ export default {
3
+ "class": "vary",
4
+ "version": "0.5.0",
5
+ "author": "bonuoq",
6
+ "license": "MPL-2.0",
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.",
8
+ "source": ";; vary · again, but different: a rhythm d swaps away, or redrawn below a metrical depth\n;; Part of @kairotic/rhythm. Written against the Kairotic API (api/…).\n;; Discussion #207 (redo with variation): \"again, but different\" with a measured likeness. Two readings of\n;; how different, both drawn with a seed (the same seed, the same variations in the same order: a room and\n;; the history hear the same; each bang the next one):\n;; swaps (by default) exactly d swaps away (Toussaint's swap distance): each swap takes an onset one pulse\n;; further from where it was — d = 1 is the same timeline with one onset displaced;\n;; depth the onsets on the strong pulses kept, down to metrical level d (0 the downbeat, 1\n;; the half bar…), the others drawn again among the weaker pulses — the same skeleton,\n;; a new surface: discussion #321's \"vary from depth k\", for rhythm, with the bar's\n;; metrical levels (twos, then threes: 16 pulses 2·2·2·2, 12 pulses 2·2·3) as its grammar.\n;; With downbeat the first pulse keeps what it has. The variation goes out as pulses, and how it was made as\n;; data: the steps from the rhythm given ([:swap from to], or [:remove i] [:add j]), and how far it went.\n(let [M 2147483647\n rng (fn [seed i]\n (let [s (volatile! (inc (mod (+ (* 7919 (js/Math.abs (js/Math.round seed))) (* 104729 i) 31337) (dec M))))]\n (dotimes [_ 4] (vreset! s (mod (* @s 16807) M)))\n (fn [] (vreset! s (mod (* @s 16807) M)) (/ (dec @s) (dec M)))))\n 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 metrical level of each of n pulses: n's factors, twos first (16 → 2·2·2·2, 12 → 2·2·3, 6 → 2·3);\n ;; pulse i is at the first level whose division reaches it (0 the downbeat)\n factors (fn [n] (let [fs (loop [m n f 2 acc []] (cond (= m 1) acc (zero? (mod m f)) (recur (/ m f) f (conj acc f)) :else (recur m (inc f) acc)))]\n (vec (sort fs))))\n levels (fn [n] (let [fs (factors n)\n spans (reductions (fn [span f] (/ span f)) n fs)] ; n, n/f1, n/f1/f2 … 1\n (mapv (fn [i] (first (keep-indexed (fn [l span] (when (zero? (mod i span)) l)) spans))) (range n))))\n ;; d swaps, each taking an onset one pulse further from where it was (never past a neighbour)\n by-swaps (fn [ps d r keep0?]\n (let [n (count ps) home (onsets-of ps)]\n (loop [cur ps pos home left d steps []]\n (let [opts (vec (for [i (range (count pos)) dir [-1 1]\n :let [p (nth pos i) q (+ p dir) h (nth home i)]\n :when (and (< -1 q n) (zero? (nth cur q))\n (> (js/Math.abs (- q h)) (js/Math.abs (- p h)))\n (not (and keep0? (or (= p 0) (= q 0)))))]\n [i p q]))]\n (if (or (zero? left) (empty? opts))\n {:pulses cur :steps steps :went (- d left)}\n (let [[i p q] (nth opts (js/Math.floor (* (r) (count opts))))]\n (recur (assoc cur p 0 q 1) (assoc pos i q) (dec left) (conj steps [:swap p q]))))))))\n ;; the onsets down to level k kept; the others drawn again among the pulses deeper than k\n by-depth (fn [ps k r keep0?]\n (let [n (count ps) lv (levels n)\n kept? (fn [i] (or (<= (nth lv i) k) (and keep0? (zero? i))))\n free (vec (filter (fn [i] (not (kept? i))) (range n)))\n moving (vec (filter (fn [i] (and (= 1 (nth ps i)) (not (kept? i)))) (range n)))\n chosen (set (take (count moving) (loop [v free j 0]\n (if (< j (count moving))\n (let [m (+ j (js/Math.floor (* (r) (- (count v) j))))]\n (recur (assoc v j (nth v m) m (nth v j)) (inc j)))\n v))))\n out (mapv (fn [i] (if (kept? i) (nth ps i) (if (chosen i) 1 0))) (range n))\n gone (vec (remove chosen moving)) came (vec (remove (set moving) (sort chosen)))]\n {:pulses out :steps (vec (concat (map (fn [i] [:remove i]) gone) (map (fn [j] [:add j]) came)))\n :went (count gone) :kept (vec (filter (fn [i] (and (kept? i) (= 1 (nth ps i)))) (range n)))}))\n params (fn [args over] (let [o (merge args over)] {:d (max 0 (js/Math.round (or (:d o) 1))) :seed (or (:seed o) 1)\n :depth (boolean (:depth o)) :downbeat (boolean (:downbeat o))}))\n draw (fn [st args]\n (let [{:keys [d seed depth downbeat]} (params args (:over st)) r (rng seed (:i st))\n v (if depth (by-depth (:ps st) d r downbeat) (by-swaps (:ps st) d r downbeat))]\n {:state (update st :i inc)\n :show (str (if depth (str \"from depth \" d) (str d \" swap\" (when (not= 1 d) \"s\"))) \" · seed \" seed \" · \" (inc (:i st)))\n :out [[1 (merge {:steps (:steps v) :mode (if depth :depth :swaps) :d d :seed seed :draw (inc (:i st))}\n (if depth {:changed (:went v) :kept (:kept v)} {:swaps (:went v)}))]\n [0 (:pulses v)]]}))]\n {;; ── about ──\n :class \"vary\"\n :license \"MPL-2.0\"\n :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.\"\n :usage \"vary [d 1] [seed 1] [depth] [downbeat]\"\n :io [1 2]\n\n ;; ── arguments ──\n :args [[:d 1] [:seed 1] [:depth false] [:downbeat false]]\n\n ;; ── logic ──\n :receive (fn [{:keys [value state args]}]\n (let [st (or state {:over {} :i 0})]\n (cond\n (pulses? value) (if (= (vec value) (:ps st)) {:state st} (draw (assoc st :ps (vec value) :i 0) args))\n (and (#{:bang} value) (:ps st)) (draw st args)\n (#{:reset} value) {:state (assoc st :i 0)}\n (and (vector? value) (= 2 (count value)) (#{:d :seed} (first value)) (number? (second value)))\n (let [s2 (assoc st :over (assoc (:over st) (first value) (second value)) :i 0)] (if (:ps s2) (draw s2 args) {:state s2}))\n (map? value) (let [s2 (assoc st :over (merge (:over st) (select-keys value [:d :seed :depth :downbeat])) :i 0)]\n (if (:ps s2) (draw s2 args) {:state s2}))\n :else {})))})\n"
9
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kairotic/rhythm",
3
- "version": "0.4.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); and swap, how far apart two rhythms are",
3
+ "version": "0.5.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; and morph and vary, from one rhythm to another swap by swap, and again but different",
5
5
  "author": "bonuoq",
6
6
  "license": "MPL-2.0",
7
7
  "repository": {
@@ -40,6 +40,8 @@
40
40
  "isorhythm",
41
41
  "fourier",
42
42
  "swap distance",
43
- "homometric"
43
+ "homometric",
44
+ "morph",
45
+ "variation"
44
46
  ]
45
47
  }
package/src/morph.cljs ADDED
@@ -0,0 +1,85 @@
1
+ ;; morph · from one rhythm to another, swap by swap
2
+ ;; Part of @kairotic/rhythm. Written against the Kairotic API (api/…).
3
+ ;; Toussaint's swap distance does not only measure how far two rhythms are: it gives the way, the swaps
4
+ ;; (an onset exchanged with the silence beside it) that turn one into the other with the fewest moves. A
5
+ ;; ratio 0…1 is a point on that way: the rhythm after that share of its swaps — always a real rhythm, never a
6
+ ;; blurred average (discussion #206: a merge of two branches as a musical operation). The way is sent as data
7
+ ;; too, a derivation of steps — the operations a distance counts (parse) are the ones a morph or a variation
8
+ ;; makes (generate), the same grammar read both ways (discussion #321).
9
+ ;; Onsets are paired in order; with different numbers of onsets, as the directed swap distance pairs them
10
+ ;; (each onset of the shorter rhythm to its own of the other): the ones left over go first (from A) or
11
+ ;; appear last (into B). The swaps take turns, one pulse each, so the rhythm changes evenly along the way.
12
+ (let [onsets-of (fn [ps] (vec (keep-indexed (fn [i x] (when (= 1 x) i)) ps)))
13
+ pulses? (fn [v] (and (sequential? v) (seq v) (every? #{0 1} v)))
14
+ ;; the onsets of the shorter list paired, in order, with those of the longer one, the least moving in
15
+ ;; all: [[u v] …] and the onsets of the longer one left over
16
+ pairing (fn [us vs]
17
+ (let [flip? (> (count us) (count vs)) [a b] (if flip? [vs us] [us vs]) p (count a) q (count b)
18
+ d (fn [x y] (js/Math.abs (- x y)))
19
+ ;; cost[i][j]: the first i of a among the first j of b; choice to walk back
20
+ table (reduce (fn [rows i]
21
+ (conj rows (reduce (fn [row j]
22
+ (conj row (min (if (> j i) (peek row) js/Infinity)
23
+ (+ (get-in rows [(dec i) (dec j)]) (d (nth a (dec i)) (nth b (dec j)))))))
24
+ [js/Infinity] (range 1 (inc q)))))
25
+ [(vec (repeat (inc q) 0))] (range 1 (inc p)))
26
+ pairs (loop [i p j q acc ()]
27
+ (cond (zero? i) acc
28
+ (and (> j i) (= (get-in table [i j]) (get-in table [i (dec j)]))) (recur i (dec j) acc)
29
+ :else (recur (dec i) (dec j) (conj acc [(nth a (dec i)) (nth b (dec j))]))))
30
+ used (set (map second pairs))
31
+ left (vec (remove used b))]
32
+ (if flip?
33
+ {:pairs (mapv (fn [[x y]] [y x]) pairs) :removed left :added []}
34
+ {:pairs (vec pairs) :removed [] :added left})))
35
+ ;; the way: first the onsets of A left over go, then the paired ones move a pulse at a time, taking turns
36
+ ;; (one blocked by a neighbour waits for it), then the onsets of B left over appear
37
+ way (fn [A B]
38
+ (let [n (count A) {:keys [pairs removed added]} (pairing (onsets-of A) (onsets-of B))
39
+ start (reduce (fn [ps i] (assoc ps i 0)) (vec A) removed)
40
+ k (count pairs)
41
+ ;; taking turns: after onset i moves, the search for the next move starts at i + 1
42
+ moves (loop [ps start pos (mapv first pairs) nxt 0 acc []]
43
+ (let [mv (first (for [o (range k)
44
+ :let [i (mod (+ nxt o) k) p (nth pos i) t (second (nth pairs i))]
45
+ :when (not= p t)
46
+ :let [q (if (< p t) (inc p) (dec p))]
47
+ :when (and (< -1 q n) (zero? (nth ps q)))]
48
+ [i p q]))]
49
+ (if-not mv
50
+ acc
51
+ (let [[i p q] mv]
52
+ (recur (assoc ps p 0 q 1) (assoc pos i q) (inc i) (conj acc [:swap p q]))))))]
53
+ (vec (concat (map (fn [i] [:remove i]) removed) moves (map (fn [j] [:add j]) added)))))
54
+ apply-step (fn [ps [op a b]] (case op :swap (assoc ps a 0 b 1) :remove (assoc ps a 0) :add (assoc ps a 1) ps))
55
+ at (fn [A steps t] (reduce apply-step (vec A) (take (js/Math.round (* (api/clamp t 0 1) (count steps))) steps)))
56
+ send (fn [st]
57
+ (let [{:keys [a b t]} st]
58
+ (if (and a b (= (count a) (count b)))
59
+ (let [steps (way a b) r (at a steps t)
60
+ n-swaps (count (filter (fn [s] (= :swap (first s))) steps))]
61
+ {:state (assoc st :steps steps)
62
+ :show (str "→ " (js/Math.round (* 100 t)) "% · " n-swaps " swaps")
63
+ :out [[1 {:steps steps :swaps n-swaps :at (js/Math.round (* t (count steps))) :of (count steps)}] [0 r]]})
64
+ {:state st :show (cond (nil? a) "no rhythm yet (left)" (nil? b) "no target yet (middle)"
65
+ :else (str (count a) " pulses towards " (count b) ": not morphed"))})))]
66
+ {;; ── about ──
67
+ :class "morph"
68
+ :license "MPL-2.0"
69
+ :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)."
70
+ :usage "morph [ratio 0]"
71
+ :io [3 2]
72
+
73
+ ;; ── arguments ──
74
+ :args [[:ratio 0]]
75
+
76
+ ;; ── logic ──
77
+ :receive (fn [{:keys [inlet value state args]}]
78
+ (let [st (or state {:t (api/clamp (or (:ratio args) 0) 0 1)})]
79
+ (case inlet
80
+ 1 (if (pulses? value) {:state (assoc st :b (vec value))} {})
81
+ 2 (if (number? value) (send (assoc st :t (api/clamp value 0 1))) {})
82
+ (cond (#{:bang} value) (send st)
83
+ (pulses? value) (send (assoc st :a (vec value)))
84
+ (number? value) (send (assoc st :t (api/clamp value 0 1)))
85
+ :else {}))))})
package/src/vary.cljs ADDED
@@ -0,0 +1,88 @@
1
+ ;; vary · again, but different: a rhythm d swaps away, or redrawn below a metrical depth
2
+ ;; Part of @kairotic/rhythm. Written against the Kairotic API (api/…).
3
+ ;; Discussion #207 (redo with variation): "again, but different" with a measured likeness. Two readings of
4
+ ;; how different, both drawn with a seed (the same seed, the same variations in the same order: a room and
5
+ ;; the history hear the same; each bang the next one):
6
+ ;; swaps (by default) exactly d swaps away (Toussaint's swap distance): each swap takes an onset one pulse
7
+ ;; further from where it was — d = 1 is the same timeline with one onset displaced;
8
+ ;; depth the onsets on the strong pulses kept, down to metrical level d (0 the downbeat, 1
9
+ ;; the half bar…), the others drawn again among the weaker pulses — the same skeleton,
10
+ ;; a new surface: discussion #321's "vary from depth k", for rhythm, with the bar's
11
+ ;; metrical levels (twos, then threes: 16 pulses 2·2·2·2, 12 pulses 2·2·3) as its grammar.
12
+ ;; With downbeat the first pulse keeps what it has. The variation goes out as pulses, and how it was made as
13
+ ;; data: the steps from the rhythm given ([:swap from to], or [:remove i] [:add j]), and how far it went.
14
+ (let [M 2147483647
15
+ rng (fn [seed i]
16
+ (let [s (volatile! (inc (mod (+ (* 7919 (js/Math.abs (js/Math.round seed))) (* 104729 i) 31337) (dec M))))]
17
+ (dotimes [_ 4] (vreset! s (mod (* @s 16807) M)))
18
+ (fn [] (vreset! s (mod (* @s 16807) M)) (/ (dec @s) (dec M)))))
19
+ onsets-of (fn [ps] (vec (keep-indexed (fn [i x] (when (= 1 x) i)) ps)))
20
+ pulses? (fn [v] (and (sequential? v) (seq v) (every? #{0 1} v)))
21
+ ;; the metrical level of each of n pulses: n's factors, twos first (16 → 2·2·2·2, 12 → 2·2·3, 6 → 2·3);
22
+ ;; pulse i is at the first level whose division reaches it (0 the downbeat)
23
+ factors (fn [n] (let [fs (loop [m n f 2 acc []] (cond (= m 1) acc (zero? (mod m f)) (recur (/ m f) f (conj acc f)) :else (recur m (inc f) acc)))]
24
+ (vec (sort fs))))
25
+ levels (fn [n] (let [fs (factors n)
26
+ spans (reductions (fn [span f] (/ span f)) n fs)] ; n, n/f1, n/f1/f2 … 1
27
+ (mapv (fn [i] (first (keep-indexed (fn [l span] (when (zero? (mod i span)) l)) spans))) (range n))))
28
+ ;; d swaps, each taking an onset one pulse further from where it was (never past a neighbour)
29
+ by-swaps (fn [ps d r keep0?]
30
+ (let [n (count ps) home (onsets-of ps)]
31
+ (loop [cur ps pos home left d steps []]
32
+ (let [opts (vec (for [i (range (count pos)) dir [-1 1]
33
+ :let [p (nth pos i) q (+ p dir) h (nth home i)]
34
+ :when (and (< -1 q n) (zero? (nth cur q))
35
+ (> (js/Math.abs (- q h)) (js/Math.abs (- p h)))
36
+ (not (and keep0? (or (= p 0) (= q 0)))))]
37
+ [i p q]))]
38
+ (if (or (zero? left) (empty? opts))
39
+ {:pulses cur :steps steps :went (- d left)}
40
+ (let [[i p q] (nth opts (js/Math.floor (* (r) (count opts))))]
41
+ (recur (assoc cur p 0 q 1) (assoc pos i q) (dec left) (conj steps [:swap p q]))))))))
42
+ ;; the onsets down to level k kept; the others drawn again among the pulses deeper than k
43
+ by-depth (fn [ps k r keep0?]
44
+ (let [n (count ps) lv (levels n)
45
+ kept? (fn [i] (or (<= (nth lv i) k) (and keep0? (zero? i))))
46
+ free (vec (filter (fn [i] (not (kept? i))) (range n)))
47
+ moving (vec (filter (fn [i] (and (= 1 (nth ps i)) (not (kept? i)))) (range n)))
48
+ chosen (set (take (count moving) (loop [v free j 0]
49
+ (if (< j (count moving))
50
+ (let [m (+ j (js/Math.floor (* (r) (- (count v) j))))]
51
+ (recur (assoc v j (nth v m) m (nth v j)) (inc j)))
52
+ v))))
53
+ out (mapv (fn [i] (if (kept? i) (nth ps i) (if (chosen i) 1 0))) (range n))
54
+ gone (vec (remove chosen moving)) came (vec (remove (set moving) (sort chosen)))]
55
+ {:pulses out :steps (vec (concat (map (fn [i] [:remove i]) gone) (map (fn [j] [:add j]) came)))
56
+ :went (count gone) :kept (vec (filter (fn [i] (and (kept? i) (= 1 (nth ps i)))) (range n)))}))
57
+ params (fn [args over] (let [o (merge args over)] {:d (max 0 (js/Math.round (or (:d o) 1))) :seed (or (:seed o) 1)
58
+ :depth (boolean (:depth o)) :downbeat (boolean (:downbeat o))}))
59
+ draw (fn [st args]
60
+ (let [{:keys [d seed depth downbeat]} (params args (:over st)) r (rng seed (:i st))
61
+ v (if depth (by-depth (:ps st) d r downbeat) (by-swaps (:ps st) d r downbeat))]
62
+ {:state (update st :i inc)
63
+ :show (str (if depth (str "from depth " d) (str d " swap" (when (not= 1 d) "s"))) " · seed " seed " · " (inc (:i st)))
64
+ :out [[1 (merge {:steps (:steps v) :mode (if depth :depth :swaps) :d d :seed seed :draw (inc (:i st))}
65
+ (if depth {:changed (:went v) :kept (:kept v)} {:swaps (:went v)}))]
66
+ [0 (:pulses v)]]}))]
67
+ {;; ── about ──
68
+ :class "vary"
69
+ :license "MPL-2.0"
70
+ :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."
71
+ :usage "vary [d 1] [seed 1] [depth] [downbeat]"
72
+ :io [1 2]
73
+
74
+ ;; ── arguments ──
75
+ :args [[:d 1] [:seed 1] [:depth false] [:downbeat false]]
76
+
77
+ ;; ── logic ──
78
+ :receive (fn [{:keys [value state args]}]
79
+ (let [st (or state {:over {} :i 0})]
80
+ (cond
81
+ (pulses? value) (if (= (vec value) (:ps st)) {:state st} (draw (assoc st :ps (vec value) :i 0) args))
82
+ (and (#{:bang} value) (:ps st)) (draw st args)
83
+ (#{:reset} value) {:state (assoc st :i 0)}
84
+ (and (vector? value) (= 2 (count value)) (#{:d :seed} (first value)) (number? (second value)))
85
+ (let [s2 (assoc st :over (assoc (:over st) (first value) (second value)) :i 0)] (if (:ps s2) (draw s2 args) {:state s2}))
86
+ (map? value) (let [s2 (assoc st :over (merge (:over st) (select-keys value [:d :seed :depth :downbeat])) :i 0)]
87
+ (if (:ps s2) (draw s2 args) {:state s2}))
88
+ :else {})))})