You need to sign in or sign up before continuing.

@kairotic/rhythm 0.3.0 → 0.4.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
@@ -14,7 +14,7 @@ is their editor, measures them and writes them as music.
14
14
  | Class | What it does |
15
15
  |---|---|
16
16
  | `euclid [k 3] [n 8] [rotation 0]` | **a generator**: a Euclidean rhythm (Bjorklund's algorithm, after Toussaint) as pulses — k onsets as evenly as they can be over n pulses: `euclid 3 8` the tresillo, `5 8` the cinquillo, `5 16 6` the bossa nova, `7 12` the bembé. No view of its own: a bang sends the pulses; `[:k 5]`, `[:n 16]`, `[:rotation 6]` (or a map) change them, a number is k |
17
- | `necklace [n 16] [bars 1] [midi] [fourier]` | **the rhythm box**: takes a rhythm in any notation, draws it on the circle with its axes of symmetry, lets you edit it (pressing a pulse adds or takes away an onset), sends its **measures** (interval vector, evenness, deep, flat, axes, gap, oddity, the rotation of E(k,n) it is, Fourier coefficients) and writes it as a **musical tree** with the pitches of its right inlet |
17
+ | `necklace [n 16] [bars 1] [midi] [fourier]` | **the rhythm box**: takes a rhythm in any notation, draws it on the circle with its axes of symmetry, lets you edit it (pressing a pulse adds or takes away an onset), sends its **measures** (interval vector, evenness, deep, flat, axes, gap, oddity, the rotation of E(k,n) it is, Fourier coefficients), writes it as a **musical tree** with the pitches of its middle inlet, and **plays it step by step** on a clock (its right inlet) |
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 |
@@ -62,6 +62,31 @@ Its **right inlet** (cold) takes the pitches the notes take in turn, starting ag
62
62
  string, a list of Hz (the keys held on a keyboard; MIDI with `midi`), notes or chords of a tree; `:clear` forgets them.
63
63
  With `bars` the color goes on from bar to bar.
64
64
 
65
+ ## Step by step, and with patterns
66
+
67
+ A `necklace` is also a **step sequencer** — the clock of Toussaint's article: each bang on its right inlet (a `metro`,
68
+ the room's `pulse`, an `onset~`) moves its hand to the next pulse, drawn from the centre, and on an onset a bang goes
69
+ out of outlet 4. Into a `player`'s left inlet it plays one event of the player's pattern a step: the rhythm the
70
+ necklace's, the pitches the pattern's. A number moves the hand to that pulse; `:reset` makes the next bang play the
71
+ first; pressing pulses while it plays changes what the hand finds.
72
+
73
+ ```clojure
74
+ (box :b1 "metro 150" 40 20)
75
+ (box :b2 "necklace" 40 60)
76
+ (box :b3 "pbind 7 loop" 280 20)
77
+ (code :b3 "{:degree #pat/shuffle [0 2 4 7 9]}")
78
+ (box :b4 "player 120" 40 320)
79
+ (box :b5 "synth~" 40 370)
80
+ (box :b6 "dac~" 40 420)
81
+ (connect :b1 0 :b2 2) (connect :b2 4 :b4 0) (connect :b3 0 :b4 1)
82
+ (connect :b4 0 :b5 0) (connect :b4 1 :b5 1) (connect :b5 0 :b6 0) (connect :b5 0 :b6 1)
83
+ ```
84
+
85
+ The same rhythms live **inside a `pbind`** ([`@kairotic/patterns`](../patterns/)): `#pat/euclid [k n rotation]`,
86
+ `#pat/pulses [1 0 0 1 …]`, `#pat/necklace "x..x..x."` and `#pat/chance [k n]`. In `:dur` a rhythm gives the lengths
87
+ between its onsets, so the other keys go on note by note (talea and color, as a necklace's tree); in another key
88
+ (`:pulse`) it gives the pulses one by one, `:pulse 0` a silence.
89
+
65
90
  ## Generators and relations
66
91
 
67
92
  `euclid`, `chance` and `homometric` make pulses; `swap` compares two. They have no view: put a `necklace` after
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.3.0",
4
+ "version": "0.4.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.3.0",
4
+ "version": "0.4.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.3.0",
4
+ "version": "0.4.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.3.0",
4
+ "version": "0.4.0",
5
5
  "api": 1,
6
6
  "author": "bonuoq",
7
7
  "license": "MPL-2.0",
@@ -27,8 +27,8 @@ export default {
27
27
  {
28
28
  "class": "necklace",
29
29
  "license": "MPL-2.0",
30
- "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 right inlet (cold) takes the pitches the notes take in turn, as euclid's.",
31
- "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 show (assoc (measures ps) :pulses ps)]\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 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 right inlet (cold) takes the pitches the notes take in turn, as euclid's.\"\n :usage \"necklace [n 16] [bars 1] [midi] [fourier]\"\n :io [2 4]\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 (if (= inlet 1)\n (let [pm (pitches-of (:midi args) value)]\n (if (= pm :ignore) {} {:state (assoc st :pm pm)}))\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 (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"
30
+ "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.",
31
+ "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"
32
32
  },
33
33
  {
34
34
  "class": "swap",
package/dist/necklace.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  // Generated by packages/build.mjs from src/necklace.cljs
2
2
  export default {
3
3
  "class": "necklace",
4
- "version": "0.3.0",
4
+ "version": "0.4.0",
5
5
  "author": "bonuoq",
6
6
  "license": "MPL-2.0",
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 right inlet (cold) takes the pitches the notes take in turn, as euclid's.",
8
- "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 show (assoc (measures ps) :pulses ps)]\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 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 right inlet (cold) takes the pitches the notes take in turn, as euclid's.\"\n :usage \"necklace [n 16] [bars 1] [midi] [fourier]\"\n :io [2 4]\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 (if (= inlet 1)\n (let [pm (pitches-of (:midi args) value)]\n (if (= pm :ignore) {} {:state (assoc st :pm pm)}))\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 (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"
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.",
8
+ "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"
9
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.3.0",
4
+ "version": "0.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kairotic/rhythm",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
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",
5
5
  "author": "bonuoq",
6
6
  "license": "MPL-2.0",
package/src/necklace.cljs CHANGED
@@ -133,17 +133,25 @@
133
133
  ;; a new state: sent when the rhythm, its bars or its meter are new (the same again changes nothing); a bang anyway
134
134
  settle (fn [st st2 always?]
135
135
  (let [ps (:pulses st2) key [ps (:bars st2) (:meter st2)]
136
- show (assoc (measures ps) :pulses ps)]
136
+ hand (when (:hand st2) (mod (:hand st2) (count ps)))
137
+ show (assoc (measures ps) :pulses ps :hand hand)
138
+ st2 (assoc st2 :hand hand :shown show)]
137
139
  (cond (empty? ps) {:state st2}
138
140
  (and (not always?) (= key (:sent st))) {:state st2 :show show}
139
141
  :else {:state (assoc st2 :sent key) :show show :out (outs ps st2)})))
142
+ ;; a step of the hand (the third inlet): to pulse h, and a bang out of outlet 4 when there is an onset there
143
+ step (fn [st h]
144
+ (let [ps (:pulses st) h (mod h (count ps))
145
+ show (assoc (or (:shown st) (assoc (measures ps) :pulses ps)) :hand h)]
146
+ (cond-> {:state (assoc st :hand h :shown show) :show show}
147
+ (= 1 (nth ps h)) (assoc :out [[4 :bang]]))))
140
148
  polar (fn [cx cy R i n] (let [a (* TAU (/ i n))] [(+ cx (* R (js/Math.sin a))) (- cy (* R (js/Math.cos a)))]))]
141
149
  {;; ── about ──
142
150
  :class "necklace"
143
151
  :license "MPL-2.0"
144
- :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 right inlet (cold) takes the pitches the notes take in turn, as euclid's."
152
+ :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."
145
153
  :usage "necklace [n 16] [bars 1] [midi] [fourier]"
146
- :io [2 4]
154
+ :io [3 5]
147
155
  :size (fn [box] (if (fourier? (:args box)) [200 296] [200 240]))
148
156
 
149
157
  ;; ── arguments ──
@@ -156,9 +164,17 @@
156
164
  ps (:pulses st) n (count ps)
157
165
  to (fn [m] (settle st (merge st m) false))
158
166
  toggle (fn [i] (let [i (mod (js/Math.round i) n)] (to {:pulses (update ps i (fn [x] (- 1 x)))})))]
159
- (if (= inlet 1)
167
+ (cond
168
+ (= inlet 1)
160
169
  (let [pm (pitches-of (:midi args) value)]
161
170
  (if (= pm :ignore) {} {:state (assoc st :pm pm)}))
171
+ ;; the clock: a step of the hand
172
+ (= inlet 2)
173
+ (cond (#{:bang} value) (step st (if (:hand st) (inc (:hand st)) 0))
174
+ (number? value) (step st (js/Math.round value))
175
+ (#{: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)})
176
+ :else {})
177
+ :else
162
178
  (cond
163
179
  (#{:bang} value) (settle st st true)
164
180
  (#{:clear} value) (to {:pulses (vec (repeat n 0))})
@@ -198,6 +214,11 @@
198
214
  (let [[x1 y1] (polar cx cy (+ R 7) a n) [x2 y2] (polar cx cy (+ R 7) (+ a (/ n 2)) n)]
199
215
  [:line {:x1 x1 :y1 y1 :x2 x2 :y2 y2 :stroke :kairos :weight 1 :opacity 0.5}]))
200
216
  (when (> (count os) 1) [[:path {:points (mapv (fn [i] (polar cx cy R i n)) os) :closed true :stroke :accent :weight 2}]])
217
+ ;; the hand, when it plays: from the centre to the pulse it is on
218
+ (when-let [hd (:hand p)]
219
+ (let [[x y] (polar cx cy (- R 6) hd n)]
220
+ [[:line {:x1 cx :y1 cy :x2 x :y2 y :stroke :kairos :weight 2}]
221
+ [:circle {:cx cx :cy cy :r 2.5 :fill :kairos}]]))
201
222
  (for [i (range n)]
202
223
  (let [[x y] (polar cx cy R i n) on (= 1 (nth ps i 0))]
203
224
  [:group {:hit i}