@kairotic/app 0.0.0-stage → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/README.md +9 -2
  2. package/index.html +1101 -0
  3. package/kairotic/api.cljs +362 -0
  4. package/kairotic/assistant.cljs +505 -0
  5. package/kairotic/backends/gun-backend.js +371 -0
  6. package/kairotic/canvas.cljs +751 -0
  7. package/kairotic/code.cljs +108 -0
  8. package/kairotic/components.cljs +838 -0
  9. package/kairotic/core.cljs +96 -0
  10. package/kairotic/corefns.cljs +473 -0
  11. package/kairotic/docs.mjs +2 -0
  12. package/kairotic/engine.cljs +482 -0
  13. package/kairotic/examples.mjs +96 -0
  14. package/kairotic/faust.cljs +225 -0
  15. package/kairotic/help.cljs +1036 -0
  16. package/kairotic/hiccup.cljs +566 -0
  17. package/kairotic/input.cljs +271 -0
  18. package/kairotic/kfile.mjs +138 -0
  19. package/kairotic/library.cljs +214 -0
  20. package/kairotic/links.cljs +41 -0
  21. package/kairotic/mt.cljs +1604 -0
  22. package/kairotic/mtboxes.cljs +276 -0
  23. package/kairotic/packages/analysis.mjs +64 -0
  24. package/kairotic/packages/controls.mjs +28 -0
  25. package/kairotic/packages/data.mjs +22 -0
  26. package/kairotic/packages/faust-units/cmaes.mjs +82 -0
  27. package/kairotic/packages/faust-units/graph.mjs +212 -0
  28. package/kairotic/packages/faust-units/listen.mjs +125 -0
  29. package/kairotic/packages/faust-units/notation.mjs +139 -0
  30. package/kairotic/packages/faust-units/parse.mjs +439 -0
  31. package/kairotic/packages/faust-units/recipe.mjs +195 -0
  32. package/kairotic/packages/faust-units/render.mjs +69 -0
  33. package/kairotic/packages/faust-units/units.mjs +130 -0
  34. package/kairotic/packages/faust-units/view.mjs +353 -0
  35. package/kairotic/packages/faust-units.mjs +9 -0
  36. package/kairotic/packages/io.mjs +58 -0
  37. package/kairotic/packages/net.mjs +28 -0
  38. package/kairotic/packages/patterns.mjs +34 -0
  39. package/kairotic/packages/spatial.mjs +22 -0
  40. package/kairotic/packages/spectral/shift.mjs +169 -0
  41. package/kairotic/packages/spectral.mjs +16 -0
  42. package/kairotic/packages/synth.mjs +28 -0
  43. package/kairotic/packages/theory/counterpoint.mjs +798 -0
  44. package/kairotic/packages/theory/mirror.mjs +221 -0
  45. package/kairotic/packages/theory.mjs +46 -0
  46. package/kairotic/patterns.cljs +538 -0
  47. package/kairotic/presets.cljs +227 -0
  48. package/kairotic/quantize.cljs +782 -0
  49. package/kairotic/record.cljs +169 -0
  50. package/kairotic/release.cljs +90 -0
  51. package/kairotic/ring.cljs +129 -0
  52. package/kairotic/ringaudio.cljs +232 -0
  53. package/kairotic/ringcore.mjs +270 -0
  54. package/kairotic/score.cljs +176 -0
  55. package/kairotic/shapes.cljs +283 -0
  56. package/kairotic/signal.cljs +327 -0
  57. package/kairotic/sounds.cljs +565 -0
  58. package/kairotic/sounds.mjs +108 -0
  59. package/kairotic/span.cljs +129 -0
  60. package/kairotic/spell.cljs +281 -0
  61. package/kairotic/structure.cljs +145 -0
  62. package/kairotic/talk.cljs +115 -0
  63. package/kairotic/tempo.cljs +226 -0
  64. package/kairotic/treecanvas.cljs +299 -0
  65. package/kairotic/tutorials.mjs +1283 -0
  66. package/kairotic/vendor/codemirror.mjs +23 -0
  67. package/package.json +23 -4
  68. package/page/00-work.cljs +862 -0
  69. package/page/01-commands.cljs +1219 -0
  70. package/page/02-patch-view.cljs +1051 -0
  71. package/page/03-history-view.cljs +481 -0
  72. package/page/04-panels.cljs +448 -0
  73. package/page/05-gestures.cljs +1247 -0
  74. package/page/06-connection.cljs +219 -0
  75. package/page/07-claude.cljs +430 -0
  76. package/page/08-sessions.cljs +423 -0
  77. package/page/09-learn.cljs +602 -0
  78. package/page/10-embed.cljs +469 -0
  79. package/page/11-startup.cljs +61 -0
  80. package/page/12-feedback.cljs +172 -0
  81. package/page/13-perform.cljs +325 -0
  82. package/page/14-editor.cljs +656 -0
  83. package/page/15-presets.cljs +230 -0
  84. package/page/16-release.cljs +242 -0
  85. package/page/17-sounds.cljs +147 -0
  86. package/page/18-lines.cljs +594 -0
  87. package/page/19-files.cljs +247 -0
  88. package/page/19-html.cljs +152 -0
  89. package/page/20-talk.cljs +527 -0
  90. package/page/21-net.cljs +110 -0
  91. package/page/22-learn.cljs +303 -0
  92. package/page/23-ids.cljs +233 -0
  93. package/page/24-clipboard.cljs +136 -0
  94. package/page/25-tidy.cljs +74 -0
  95. package/page/26-probes.cljs +111 -0
  96. package/sources.mjs +2 -0
@@ -0,0 +1,129 @@
1
+ ;; SPDX-License-Identifier: MPL-2.0
2
+ ;; Kairotic · span: a ring of messages and its heads, pure (discussion "Heads on a span of time", Studies 44 and 47)
3
+ ;;
4
+ ;; A ring keeps what was written in the last `dur` seconds. Its time starts at its first message (or the first
5
+ ;; one after :clear); its implicit write head is where that time is, and as a tape head it erases what it passes:
6
+ ;; what is older than one turn is gone. Positions on the ring are seconds in [0, dur) (the span's own units);
7
+ ;; events keep the time they belong to, unwrapped (seconds since the start), so a head that goes round is still
8
+ ;; moving forwards and an event is met again at every turn.
9
+ ;;
10
+ ;; {:ring true :dur 8 :t0 at-or-nil :events [{:t seconds-since-start :v value :w instant-written} …]}
11
+ ;;
12
+ ;; Writers (other boxes) send it edits: [:write p v mode] at position p (or :now, the head) — with :ovr it
13
+ ;; replaces what was written there before, not what arrives with it (a chord stays) —, [:cut a b] (what a writer
14
+ ;; passed going from a to b: forwards (a, b], backwards [b, a)), [:erase] (everything; time goes on).
15
+ ;; Readers meet the events they cross (crossings), in the order they meet them.
16
+
17
+ (ns kairotic.span)
18
+
19
+ (def eps 1e-9)
20
+
21
+ (defn ring
22
+ "An empty ring that keeps dur seconds, waiting for its first message to start its time."
23
+ [dur]
24
+ {:ring true :dur (if (and (number? dur) (pos? dur)) dur 8) :t0 nil :events []})
25
+
26
+ (defn ring? [x] (and (map? x) (true? (:ring x))))
27
+
28
+ (defn head
29
+ "Where the ring's write head is at instant at (seconds since its start, unwrapped), or nil before it starts."
30
+ [r at]
31
+ (when-let [t0 (:t0 r)] (- at t0)))
32
+
33
+ (defn wrap
34
+ "A position of the span, in [0, dur)."
35
+ [r p]
36
+ (let [l (:dur r) x (mod p l)] (if (< (- l x) eps) 0 x)))
37
+
38
+ (defn start
39
+ "The ring's time starts at at, if it had not."
40
+ [r at]
41
+ (if (:t0 r) r (assoc r :t0 at)))
42
+
43
+ (defn alive
44
+ "What the ring keeps at instant at: what was written within one turn of the head."
45
+ [r at]
46
+ (if-let [h (head r at)]
47
+ (let [lo (+ (- h (:dur r)) eps)]
48
+ (filterv (fn [e] (> (:t e) lo)) (:events r)))
49
+ []))
50
+
51
+ (defn- prune [r at] (assoc r :events (alive r at)))
52
+
53
+ (defn- place
54
+ "The time an event written at position p (wrapped) belongs to: the latest one, not after the head."
55
+ [r at p]
56
+ (let [h (head r at)] (- h (mod (- h (wrap r p)) (:dur r)))))
57
+
58
+ (defn- same-place? [r a b] (let [d (mod (- a b) (:dur r))] (or (< d 1e-6) (< (- (:dur r) d) 1e-6))))
59
+
60
+ (defn write
61
+ "Writes v at instant at: at the head (p :now or nil) or at position p. With :ovr, what was written there before
62
+ (at another instant) goes; what arrives with it stays."
63
+ ([r at v] (write r at :now v :mix))
64
+ ([r at p v mode]
65
+ (let [r (prune (start r at) at)
66
+ t (if (or (nil? p) (= :now p)) (head r at) (place r at p))
67
+ keep? (fn [e] (not (and (= :ovr mode) (not= (:w e) at) (same-place? r (:t e) t))))]
68
+ (assoc r :events (vec (sort-by :t (conj (filterv keep? (:events r)) {:t t :v v :w at})))))))
69
+
70
+ (defn- in-sweep?
71
+ "Is position x (wrapped) met by a head going from a to b (unwrapped)? Forwards (a, b], backwards [b, a)."
72
+ [l x a b]
73
+ (if (< a b)
74
+ (let [k (js/Math.ceil (/ (- (+ a eps) x) l)) y (+ x (* k l))] (<= y (+ b eps)))
75
+ (let [k (js/Math.floor (/ (- (- a eps) x) l)) y (+ x (* k l))] (>= y (- b eps)))))
76
+
77
+ (defn crossings
78
+ "The events met going from a to b (positions of the span, unwrapped: a move of more than a turn meets an event
79
+ more than once), in the order they are met, as [position-where-met event]."
80
+ [r at a b]
81
+ (let [l (:dur r) es (alive r at)]
82
+ (cond
83
+ (or (nil? a) (nil? b) (= a b)) []
84
+ (< a b)
85
+ (->> (for [e es
86
+ :let [k0 (js/Math.ceil (/ (- (+ a eps) (:t e)) l))]
87
+ k (range k0 (inc (js/Math.floor (/ (- (+ b eps) (:t e)) l))))]
88
+ [(+ (:t e) (* k l)) e])
89
+ (sort-by first) vec)
90
+ :else
91
+ (->> (for [e es
92
+ :let [k0 (js/Math.floor (/ (- (- a eps) (:t e)) l))]
93
+ k (range k0 (dec (js/Math.ceil (/ (- (- b eps) (:t e)) l))) -1)]
94
+ [(+ (:t e) (* k l)) e])
95
+ (sort-by (comp - first)) vec))))
96
+
97
+ (defn cut
98
+ "What a writer passed going from a to b (positions, unwrapped) is gone."
99
+ [r at a b]
100
+ (let [r (prune r at) l (:dur r)]
101
+ (if (or (nil? a) (nil? b) (= a b) (nil? (:t0 r)))
102
+ r
103
+ (let [lo (min a b) hi (max a b)]
104
+ (if (>= (- hi lo) l)
105
+ (assoc r :events [])
106
+ (assoc r :events (filterv (fn [e] (not (in-sweep? l (:t e) a b))) (:events r))))))))
107
+
108
+ (defn erase "Everything is gone; time goes on." [r] (assoc r :events []))
109
+
110
+ (defn shortest
111
+ "The move from position a to position q on a ring of length l: the one shorter than half a turn (a phasor~ that
112
+ goes back to 0 has gone on, not back)."
113
+ [l a q]
114
+ (let [d (mod (- q a) l)] (if (> d (/ l 2)) (- d l) d)))
115
+
116
+ ;; ───────── a reader that follows the ring's head ─────────
117
+ ;; d behind it, then drifting by (1 − speed) for each second: first the distance, then the speed (Study 47).
118
+ ;; Its position at instant T: head(T) − d − drift0 − (1 − speed)·(T − since).
119
+
120
+ (defn follow-at
121
+ "Where a following reader is at instant T (unwrapped), or nil before the ring starts."
122
+ [r {:keys [d speed drift0 since]} T]
123
+ (when-let [h (head r T)]
124
+ (- h d (or drift0 0) (* (- 1 speed) (- T (or since T))))))
125
+
126
+ (defn when-met
127
+ "The instant a following reader at position p at instant T (moving at speed) meets position x."
128
+ [p T speed x]
129
+ (if (zero? speed) T (max T (+ T (/ (- x p) speed)))))
@@ -0,0 +1,281 @@
1
+ ;; SPDX-License-Identifier: MPL-2.0
2
+ ;; Kairotic · spell
3
+ ;; How a pitch is written (issue #88): a letter, an alteration and an octave, not only a MIDI number.
4
+ ;; G# and Ab sound alike and are written apart; the line of fifths (… Bb F C G D A E B F# C# …, one
5
+ ;; integer each: C = 0, G = 1, F = -1) says how far a name is from another, so that transposing,
6
+ ;; inverting and choosing a name for a sound are arithmetic on it (as in Ooloi ADR-0026 or music21).
7
+ ;;
8
+ ;; a spelling {:step 0…6 (c d e f g a b) :alter -2…2 :oct n} "bb4", "f##3", "fx3", "ebb2", "c4"
9
+ ;; an interval "M3" "-P5" "A4" "d5" "m9" … , or [steps semitones]
10
+ ;; a key "eb" "c#m" "d dorian" "f lydian" :none, or a key signature as a number (3: three sharps)
11
+ ;;
12
+ ;; Spelling the notes that have no name (numbers, Hz, a freqt's strokes): `spell-steps` chooses, for each
13
+ ;; note, the name nearest the key on the line of fifths, with melodic and vertical sense (a Viterbi over
14
+ ;; the onsets: chromatic notes rise as sharps and fall as flats, no augmented seconds where a minor
15
+ ;; third does, chords stacked in thirds), the key given or estimated per bar (Temperley's profiles).
16
+
17
+ (ns kairotic.spell
18
+ (:require [clojure.string :as str]))
19
+
20
+ (def letters ["c" "d" "e" "f" "g" "a" "b"])
21
+ (def natural-pc [0 2 4 5 7 9 11])
22
+ (def natural-fifths [0 2 4 -1 1 3 5])
23
+
24
+ ;; ───────── spellings ─────────
25
+
26
+ (defn parse
27
+ "A pitch name → {:step :alter :oct}: \"bb4\", \"f#3\", \"fs3\", \"f##3\", \"fx3\", \"ebb2\", \"cn4\"; nil if
28
+ it is not one. c4 is middle C."
29
+ [s]
30
+ (when-let [[_ l acc o] (and (or (string? s) (keyword? s))
31
+ (re-matches #"(?i)\s*([a-g])(##|#|x|s|bb|b|n)?(-?\d+)\s*" (name s)))]
32
+ {:step (.indexOf letters (str/lower-case l))
33
+ :alter (case (some-> acc str/lower-case) ("##" "x") 2 ("#" "s") 1 "bb" -2 "b" -1 0)
34
+ :oct (js/parseInt o 10)}))
35
+
36
+ (defn ->midi [{:keys [step alter oct]}] (+ (* 12 (inc oct)) (natural-pc step) alter))
37
+
38
+ (defn fifths "Where a spelling is on the line of fifths (C 0, G 1, F -1, F# 6, Bb -2)." [{:keys [step alter]}]
39
+ (+ (natural-fifths step) (* 7 alter)))
40
+
41
+ (defn acc-str [alter] (case alter 2 "##" 1 "#" -1 "b" -2 "bb" ""))
42
+
43
+ (defn ->name "{:step 6 :alter -1 :oct 4} → \"bb4\"." [{:keys [step alter oct] :as sp}]
44
+ (when sp (str (letters step) (acc-str alter) oct)))
45
+
46
+ (defn from-fifths
47
+ "The spelling at position f of the line of fifths that sounds midi (an integer), or nil."
48
+ [f midi]
49
+ (let [base (- (mod (inc f) 7) 1)
50
+ alter (/ (- f base) 7)
51
+ step (.indexOf natural-fifths base)
52
+ oct (- (/ (- midi (natural-pc step) alter) 12) 1)]
53
+ (when (and (>= step 0) (== oct (js/Math.round oct))) {:step step :alter alter :oct oct})))
54
+
55
+ (defn spellings
56
+ "Every way to write an integer MIDI pitch with at most two sharps or flats, fewest alterations first:
57
+ 61 → c#4, db4 (and b##3)."
58
+ [midi]
59
+ (let [pc (mod midi 12)]
60
+ (->> (range 7)
61
+ (keep (fn [step]
62
+ (let [alter (- (mod (+ (- pc (natural-pc step)) 6) 12) 6)]
63
+ (when (<= (js/Math.abs alter) 2)
64
+ {:step step :alter alter :oct (- (/ (- midi (natural-pc step) alter) 12) 1)}))))
65
+ (sort-by (fn [sp] (js/Math.abs (:alter sp))))
66
+ vec)))
67
+
68
+ ;; ───────── intervals ─────────
69
+
70
+ (def ^:private simple-semis [0 2 4 5 7 9 11])
71
+
72
+ (defn interval
73
+ "An interval as [steps semitones]: \"M3\" → [2 4], \"-P5\" → [-4 -7], \"A4\" → [3 6], \"d5\" → [4 6],
74
+ \"m9\" → [8 13]; [steps semitones] as it is; nil if it does not read."
75
+ [x]
76
+ (cond
77
+ (and (vector? x) (= 2 (count x)) (every? integer? x)) x
78
+ (or (string? x) (keyword? x) (symbol? x))
79
+ (when-let [[_ sign q n] (re-matches #"\s*([+-]?)(P|M|m|A+|d+)(\d+)\s*" (if (symbol? x) (str x) (name x)))]
80
+ (let [n (js/parseInt n 10) s (mod (dec n) 7) octs (quot (dec n) 7)
81
+ perfect? (#{0 3 4} s)
82
+ adj (cond (= q "P") (when perfect? 0)
83
+ (= q "M") (when-not perfect? 0)
84
+ (= q "m") (when-not perfect? -1)
85
+ (str/starts-with? q "A") (count q)
86
+ (str/starts-with? q "d") (- (+ (count q) (if perfect? 0 1))))]
87
+ (when (and (pos? n) (some? adj))
88
+ (let [st (+ s (* 7 octs)) se (+ (simple-semis s) (* 12 octs) adj) k (if (= sign "-") -1 1)]
89
+ [(* k st) (* k se)]))))
90
+ :else nil))
91
+
92
+ (defn interval-of-semitones
93
+ "The interval a transposition by n semitones is written as: 1 → m2, 6 → A4 (or d5: dim? true), 7 → P5."
94
+ ([n] (interval-of-semitones n false))
95
+ ([n dim?]
96
+ (let [k (if (neg? n) -1 1) a (js/Math.abs n) o (quot a 12) r (mod a 12)
97
+ steps ([0 1 1 2 2 3 (if dim? 4 3) 4 5 5 6 6] r)]
98
+ [(* k (+ steps (* 7 o))) n])))
99
+
100
+ (defn transpose-spelling
101
+ "A spelling moved by an interval [steps semitones]: c4 + M3 = e4, c4 + A4 = f#4, c4 - m2 = b3."
102
+ [{:keys [step oct] :as sp} [steps semis]]
103
+ (let [s (+ step steps) nstep (mod s 7) noct (+ oct (js/Math.floor (/ s 7)))
104
+ target (+ (->midi sp) semis)
105
+ natural (+ (* 12 (inc noct)) (natural-pc nstep))]
106
+ {:step nstep :alter (- target natural) :oct noct}))
107
+
108
+ (defn invert-spelling
109
+ "A spelling mirrored around a pivot spelling: on the line of fifths and in pitch (c4 around e4 = g#4)."
110
+ [sp pivot]
111
+ (or (from-fifths (- (* 2 (fifths pivot)) (fifths sp)) (- (* 2 (->midi pivot)) (->midi sp)))
112
+ sp))
113
+
114
+ ;; ───────── keys ─────────
115
+
116
+ (def mode-offsets {:major 0 :ionian 0 :dorian -2 :phrygian -4 :lydian 1 :mixolydian -1 :minor -3 :aeolian -3 :locrian -5})
117
+
118
+ (defn parse-key
119
+ "A key → {:sig n (sharps +, flats -) :tonic fifths :mode kw}, {:none true} for :none (atonal: no key),
120
+ or nil. \"eb\", \"Eb\", \"c#m\", \"c# minor\", \"d dorian\", a number (a signature: 3 is A major's)."
121
+ [k]
122
+ (cond
123
+ (nil? k) nil
124
+ (or (= k :none) (= (str/lower-case (name (if (keyword? k) k (str k)))) "none")) {:none true :sig 0}
125
+ (and (number? k) (integer? k) (<= -7 k 7)) {:sig k :tonic k :mode :major}
126
+ (map? k) (when (integer? (:sig k)) k)
127
+ (or (string? k) (keyword? k))
128
+ (when-let [[_ l acc m] (re-matches #"(?i)\s*([a-g])(##|#|x|s|bb|b)?\s*(m|min|minor|maj|major|ionian|dorian|phrygian|lydian|mixolydian|aeolian|locrian)?\s*" (name k))]
129
+ (let [tonic (fifths {:step (.indexOf letters (str/lower-case l))
130
+ :alter (case (some-> acc str/lower-case) ("##" "x") 2 ("#" "s") 1 "bb" -2 "b" -1 0)})
131
+ mode (case (some-> m str/lower-case) ("m" "min" "minor") :minor (nil "maj" "major") :major (keyword (str/lower-case m)))
132
+ sig (+ tonic (mode-offsets mode))]
133
+ (when (<= -7 sig 7) {:sig sig :tonic tonic :mode mode})))
134
+ :else nil))
135
+
136
+ (defn key-name
137
+ "A key as text, from where its tonic is on the line of fifths and its mode: {:tonic -3 :mode :major} →
138
+ \"eb\", {:tonic 7 :mode :minor} → \"c#m\", {:tonic 2 :mode :dorian} → \"d dorian\"."
139
+ [{:keys [tonic mode]}]
140
+ (let [alter (js/Math.floor (/ (inc tonic) 7))
141
+ step (.indexOf natural-fifths (- tonic (* 7 alter)))]
142
+ (str (letters step) (acc-str alter)
143
+ (case mode (nil :major :ionian) "" (:minor :aeolian) "m" (str " " (name mode))))))
144
+
145
+ (defn transpose-key
146
+ "A key (as parse-key reads it) moved by an interval [steps semitones], as text: \"eb\" by a major third
147
+ is \"g\", \"c#m\" by a minor second down is \"cm\". :none, and what is not a key, stay as they are.
148
+ A key beyond seven sharps or flats takes the tonic that sounds the same (E# major → F major)."
149
+ [k iv]
150
+ (let [{:keys [tonic mode none] :as pk} (parse-key k)]
151
+ (if (or (nil? pk) none (nil? tonic))
152
+ k
153
+ (let [df (fifths (transpose-spelling {:step 0 :alter 0 :oct 4} iv))
154
+ t (+ tonic df) off (mode-offsets (or mode :major))
155
+ t (cond (> (+ t off) 7) (- t 12) (< (+ t off) -7) (+ t 12) :else t)]
156
+ (key-name {:tonic t :mode mode})))))
157
+
158
+ (defn mei-key-sig "A signature as MEI writes it: 3 → \"3s\", -2 → \"2f\", 0 → \"0\"." [sig]
159
+ (cond (pos? sig) (str sig "s") (neg? sig) (str (- sig) "f") :else "0"))
160
+
161
+ (defn key-alter
162
+ "The alteration a signature gives a step: in 2 sharps, F and C are sharp (1); in 1 flat, B is flat (-1)."
163
+ [sig step]
164
+ (let [x (natural-fifths step)]
165
+ (cond (and (pos? sig) (< x (dec sig))) 1
166
+ (and (neg? sig) (> x (+ 5 sig))) -1
167
+ :else 0)))
168
+
169
+ (defn key-center "The middle of a key's notes on the line of fifths (its signature + 2)." [sig] (+ sig 2))
170
+
171
+ ;; Temperley's key profiles (Music and Probability, 2007): they weigh notes outside a key lower than
172
+ ;; Krumhansl–Kessler's, which tells A minor from E major in a few notes (C is far from E major)
173
+ (def ^:private major-profile [5.0 2.0 3.5 2.0 4.5 4.0 2.0 4.5 2.0 3.5 1.5 4.0])
174
+ (def ^:private minor-profile [5.0 2.0 3.5 4.5 2.0 4.0 2.0 4.5 3.5 2.0 1.5 4.0])
175
+
176
+ (defn- correlation [xs ys]
177
+ (let [n (count xs) mx (/ (reduce + xs) n) my (/ (reduce + ys) n)
178
+ sxy (reduce + (map (fn [x y] (* (- x mx) (- y my))) xs ys))
179
+ sxx (reduce + (map (fn [x] (* (- x mx) (- x mx))) xs))
180
+ syy (reduce + (map (fn [y] (* (- y my) (- y my))) ys))]
181
+ (if (or (zero? sxx) (zero? syy)) 0 (/ sxy (js/Math.sqrt (* sxx syy))))))
182
+
183
+ (defn tonic-fifths
184
+ "Where a tonic of pitch class pc is on the line of fifths, so that its key (in mode) has the fewest
185
+ alterations: pc 1 major → Db (-5), pc 1 minor → C# (7)."
186
+ [pc mode]
187
+ (let [r (mod (* 7 pc) 12) off (mode-offsets mode)]
188
+ (first (sort-by (fn [f] [(js/Math.abs (+ f off)) (- f)]) [r (- r 12)]))))
189
+
190
+ (defn estimate-key
191
+ "The key whose profile best matches a duration per pitch class (a vector of 12), as parse-key gives
192
+ it, with :score its correlation (between two that fit alike, fewer alterations); nil when there is
193
+ nothing to hear."
194
+ [weights]
195
+ (when (pos? (reduce + weights))
196
+ (let [rot (fn [p k] (vec (for [i (range 12)] (p (mod (- i k) 12)))))
197
+ ;; between keys that fit as well, the one with fewer alterations
198
+ best (apply max-key (fn [x] (- (:score x) (* 0.01 (js/Math.abs (:sig x)))))
199
+ (for [k (range 12) [mode p] [[:major major-profile] [:minor minor-profile]]
200
+ :let [tonic (tonic-fifths k mode)]]
201
+ {:pc k :mode mode :tonic tonic :sig (+ tonic (mode-offsets mode)) :score (correlation weights (rot p k))}))]
202
+ (select-keys best [:sig :tonic :mode :score]))))
203
+
204
+ (defn estimate-sig
205
+ "The signature of the key the weights suggest, when they suggest one clearly (a correlation of 0.5 or
206
+ more): nil for notes that fit no key (a chromatic line, a whole-tone scale), which are then named
207
+ without one, near C."
208
+ [weights]
209
+ (let [k (estimate-key weights)]
210
+ (when (and k (>= (:score k) 0.5)) (:sig k))))
211
+
212
+ ;; ───────── spelling notes ─────────
213
+
214
+ (defn- step-cost
215
+ "How a set of spellings sounds as one onset in a key: far from the key's notes (center ± 3), double
216
+ alterations, and a chord spread wider than a tritone's span on the line of fifths."
217
+ [sps center]
218
+ (let [fs (map fifths sps)]
219
+ (+ (reduce + (map (fn [f] (let [d (js/Math.abs (- f center))] (if (<= d 3) 0 (* 0.8 (- d 3))))) fs))
220
+ (* 2.5 (count (filter (fn [sp] (= 2 (js/Math.abs (:alter sp)))) sps)))
221
+ (* 0.05 (reduce + (map (fn [sp] (js/Math.abs (:alter sp))) sps)))
222
+ (if (> (count fs) 1) (* 0.4 (max 0 (- (- (apply max fs) (apply min fs)) 6))) 0))))
223
+
224
+ (defn- move-cost
225
+ "How one voice moves from spelling a to b in a key of signature sig: an augmented or diminished step
226
+ (more than six fifths apart: C to D#, E to Gb) costs; a raised note that falls a semitone, or a
227
+ lowered one that rises, costs more than the key's preference (chromatic lines rise in sharps and fall
228
+ in flats)."
229
+ [a b sig]
230
+ (let [d (js/Math.abs (- (fifths b) (fifths a)))
231
+ semis (- (->midi b) (->midi a))]
232
+ (+ (if (<= d 6) 0 (* 0.7 (- d 6)))
233
+ (cond (and (= semis -1) (> (:alter a) (key-alter sig (:step a)))) 2
234
+ (and (= semis 1) (< (:alter a) (key-alter sig (:step a)))) 2
235
+ :else 0))))
236
+
237
+ (defn- combos [xss]
238
+ (reduce (fn [acc xs] (for [a acc x xs] (conj a x))) [[]] xss))
239
+
240
+ (defn spell-steps
241
+ "Names for notes in time: steps [{:sig n :notes [{:id :midi (an integer) :voice :fixed spelling-or-nil}]}],
242
+ one per onset, in order (:sig the key there). Returns {id spelling}. A note that is written keeps its
243
+ name (:fixed); the others take the names that cost least over the whole line (Viterbi): near the key,
244
+ moving well in their voice, stacked well in their chord."
245
+ [steps]
246
+ (let [cands (fn [{:keys [sig notes]}]
247
+ (let [c (key-center sig)
248
+ per (mapv (fn [n] (if (:fixed n)
249
+ [(:fixed n)]
250
+ (vec (take 3 (sort-by (fn [sp] (js/Math.abs (- (fifths sp) c))) (spellings (:midi n)))))))
251
+ notes)]
252
+ (if (> (reduce * 1 (map count per)) 81)
253
+ [(mapv first per)] ; too many notes at once: each its nearest
254
+ (vec (combos per)))))
255
+ layers (mapv (fn [st] (let [cs (cands st) c (key-center (:sig st))]
256
+ {:step st :states (mapv (fn [sps] {:sps sps :static (step-cost sps c)}) cs)}))
257
+ steps)
258
+ trans (fn [prev-st prev-sps st sps]
259
+ (let [pv (into {} (map (fn [n sp] [(:voice n) sp]) (:notes prev-st) prev-sps))]
260
+ (reduce + (map (fn [n sp] (if-let [a (get pv (:voice n))] (move-cost a sp (:sig st)) 0)) (:notes st) sps))))
261
+ dp (reduce (fn [acc {:keys [step states]}]
262
+ (if (empty? acc)
263
+ [(mapv (fn [s] {:cost (:static s) :back nil :sps (:sps s)}) states)]
264
+ (let [prev (peek acc) pst (:step (nth layers (dec (count acc))))]
265
+ (conj acc (mapv (fn [s]
266
+ (let [[bi bc] (reduce (fn [[bi bc] [i p]]
267
+ (let [c (+ (:cost p) (trans pst (:sps p) step (:sps s)))]
268
+ (if (< c bc) [i c] [bi bc])))
269
+ [0 js/Infinity] (map-indexed vector prev))]
270
+ {:cost (+ bc (:static s)) :back bi :sps (:sps s)}))
271
+ states)))))
272
+ [] layers)]
273
+ (if (empty? dp)
274
+ {}
275
+ (loop [i (dec (count dp))
276
+ j (first (apply min-key second (map-indexed (fn [j s] [j (:cost s)]) (peek dp))))
277
+ out {}]
278
+ (if (neg? i)
279
+ out
280
+ (let [s (nth (nth dp i) j) st (:step (nth layers i))]
281
+ (recur (dec i) (:back s) (into out (map (fn [n sp] [(:id n) sp]) (:notes st) (:sps s))))))))))
@@ -0,0 +1,145 @@
1
+ ;; SPDX-License-Identifier: MPL-2.0
2
+ ;; Kairotic · structure
3
+ ;; The form of what is heard or played, from how alike its moments are (issue #120):
4
+ ;; · a self-similarity matrix (SSM): the cosine of every two frames' vectors (Foote 1999). Frames can be of
5
+ ;; audio (a chroma, descriptors, an onset envelope: what form~ gets) or of notes (one per beat: the pitch
6
+ ;; classes sounding in it, its onsets, how strong they were);
7
+ ;; · novelty: how much the music changes at each frame, by a checkerboard kernel slid along the diagonal of the
8
+ ;; SSM (Foote 2000) — its peaks are the boundaries of segments;
9
+ ;; · the beat spectrum: how alike frames are at each lag, the mean of each diagonal (Foote & Uchihashi 2001) —
10
+ ;; its peaks are the periods that repeat (the bar, the phrase);
11
+ ;; · segments: the novelty peaks, with a shortest and a longest segment.
12
+ ;; Pure functions on numbers. Matrices are flat Float64Arrays, n × n ({:n :data}); a few hundred frames are cheap,
13
+ ;; thousands are not (the matrix grows with their square): use frames per beat, per onset, or ~100 ms apart.
14
+
15
+ (ns kairotic.structure)
16
+
17
+ (defn- sq [x] (* x x))
18
+
19
+ (defn unit
20
+ "A vector scaled to length 1 (a vector of zeros stays zeros)."
21
+ [v]
22
+ (let [l (js/Math.sqrt (reduce + (map sq v)))]
23
+ (if (pos? l) (mapv (fn [x] (/ x l)) v) (vec v))))
24
+
25
+ (defn ssm
26
+ "The self-similarity matrix of vectors (frames, all of one length): {:n :data}, data[i n + j] the cosine of
27
+ frames i and j (1 alike … 0 unrelated, for vectors of non-negative values)."
28
+ [vs]
29
+ (let [us (mapv unit vs) n (count us) d (js/Float64Array. (* n n))]
30
+ (dotimes [i n]
31
+ (let [a (us i)]
32
+ (loop [j i]
33
+ (when (< j n)
34
+ (let [b (us j) s (reduce + (map * a b))]
35
+ (aset d (+ (* i n) j) s) (aset d (+ (* j n) i) s)
36
+ (recur (inc j)))))))
37
+ {:n n :data d}))
38
+
39
+ (defn at "The similarity of frames i and j." [{:keys [n data]} i j] (aget data (+ (* i n) j)))
40
+
41
+ (defn novelty
42
+ "How much the music changes at each frame: a checkerboard kernel of half-width L (frames), tapered by a
43
+ Gaussian, slid along the diagonal (Foote 2000): high where the frames before are alike among themselves and
44
+ unlike the ones after. Scaled to 0 … 1. A vector of n values."
45
+ [{:keys [n] :as S} L]
46
+ (let [L (max 1 (js/Math.round L))
47
+ g (fn [a b] (js/Math.exp (- (/ (+ (sq (+ a 0.5)) (sq (+ b 0.5))) (* 2 (sq (* 0.5 L)))))))
48
+ raw (mapv (fn [i]
49
+ (loop [a (- L) acc 0]
50
+ (if (>= a L)
51
+ acc
52
+ (recur (inc a)
53
+ (loop [b (- L) acc acc]
54
+ (if (>= b L)
55
+ acc
56
+ (let [x (+ i a) y (+ i b)]
57
+ (recur (inc b)
58
+ (if (and (< -1 x n) (< -1 y n))
59
+ (+ acc (* (if (= (neg? a) (neg? b)) 1 -1) (g a b) (at S x y)))
60
+ acc)))))))))
61
+ (range n))
62
+ top (reduce max 1e-12 raw)]
63
+ (mapv (fn [x] (max 0 (/ x top))) raw)))
64
+
65
+ (defn novelty-at
66
+ "The novelty of frame i alone (see novelty), not scaled by the others: the checkerboard's sum divided by the
67
+ sum of its weights, so -1 … 1 (0 where it does not change). For a stream, where frames keep arriving: frame i
68
+ needs L frames after it. nil when it has fewer than L frames on either side."
69
+ [{:keys [n] :as S} L i]
70
+ (let [L (max 1 (js/Math.round L))]
71
+ (when (and (>= i L) (<= (+ i L) n))
72
+ (let [g (fn [a b] (js/Math.exp (- (/ (+ (sq (+ a 0.5)) (sq (+ b 0.5))) (* 2 (sq (* 0.5 L)))))))]
73
+ (loop [a (- L) acc 0 wsum 0]
74
+ (if (>= a L)
75
+ (/ acc (max 1e-12 wsum))
76
+ (let [[acc wsum] (loop [b (- L) acc acc wsum wsum]
77
+ (if (>= b L)
78
+ [acc wsum]
79
+ (let [w (g a b)]
80
+ (recur (inc b) (+ acc (* (if (= (neg? a) (neg? b)) 1 -1) w (at S (+ i a) (+ i b)))) (+ wsum w)))))]
81
+ (recur (inc a) acc wsum))))))))
82
+
83
+ (defn beat-spectrum
84
+ "How alike frames are at each lag 0 … max-lag: the mean of each diagonal of the SSM (Foote & Uchihashi 2001).
85
+ Its peaks are the periods that repeat."
86
+ [{:keys [n] :as S} max-lag]
87
+ (mapv (fn [l] (if (>= l n) 0 (/ (reduce + (map (fn [i] (at S i (+ i l))) (range (- n l)))) (- n l))))
88
+ (range (inc (min max-lag (dec n))))))
89
+
90
+ (defn periods
91
+ "The lags at which the beat spectrum peaks (higher than both neighbours), strongest first: [[lag value] …]."
92
+ [bs]
93
+ (let [v (vec bs)]
94
+ (->> (range 1 (dec (count v)))
95
+ (filter (fn [l] (and (> (v l) (v (dec l))) (>= (v l) (v (inc l))))))
96
+ (map (fn [l] [l (v l)]))
97
+ (sort-by (fn [[_ x]] (- x)))
98
+ vec)))
99
+
100
+ (defn boundaries
101
+ "The frames where segments begin: novelty peaks above mean + k × deviation, at least shortest frames apart
102
+ (the higher peak wins), and, in a segment longer than longest, its highest novelty point too (its middle,
103
+ if it is flat). Always
104
+ starts with 0."
105
+ [nov {:keys [shortest longest k] :or {shortest 8 longest 64 k 0.5}}]
106
+ (let [v (vec nov) n (count v)
107
+ mean (/ (reduce + v) (max 1 n))
108
+ sd (js/Math.sqrt (/ (reduce + (map (fn [x] (sq (- x mean))) v)) (max 1 n)))
109
+ peaks (filter (fn [i] (and (> (v i) (+ mean (* k sd)))
110
+ (>= (v i) (get v (dec i) 0)) (>= (v i) (get v (inc i) 0))))
111
+ (range 1 n))
112
+ ;; the highest first, each keeping the others shortest frames away
113
+ chosen (reduce (fn [acc i] (if (every? (fn [j] (>= (js/Math.abs (- i j)) shortest)) acc) (conj acc i) acc))
114
+ [0] (sort-by (fn [i] (- (v i))) peaks))
115
+ chosen (sort chosen)
116
+ ;; a segment longer than longest is cut at its highest point
117
+ split (fn split [a b]
118
+ (if (or (<= (- b a) longest) (< (- b a) (* 2 shortest)))
119
+ [a]
120
+ (let [mid (/ (+ a b) 2)
121
+ c (apply max-key (fn [i] (- (v i) (* 1e-9 (js/Math.abs (- i mid))))) (range (+ a shortest) (- b shortest)))]
122
+ (concat (split a c) (split c b)))))]
123
+ (vec (distinct (mapcat (fn [[a b]] (split a b)) (partition 2 1 (concat chosen [n])))))))
124
+
125
+ ;; ───────── frames of notes ─────────
126
+
127
+ (defn note-frames
128
+ "Notes ({:on :off :attrs {:pitch :vel}}) as one vector per frame between the times given (beats found,
129
+ or a grid): 12 pitch classes (each weighed by how long it sounds in the frame and how strong it was), then
130
+ the strength of the onsets in the frame and how many there are (scaled). For the SSM of what was played."
131
+ [notes times]
132
+ (let [ts (vec times) nf (dec (count ts))
133
+ midi (fn [n] (let [p (get-in n [:attrs :pitch])] (when (number? p) p)))
134
+ vel (fn [n] (let [v (get-in n [:attrs :vel])] (if (number? v) (if (> v 1) (/ v 127) v) 0.7)))]
135
+ (mapv (fn [f]
136
+ (let [a (ts f) b (ts (inc f)) len (max 1e-9 (- b a))
137
+ pcs (reduce (fn [pc n]
138
+ (if-let [m (midi n)]
139
+ (let [o (- (min b (:off n)) (max a (:on n)))]
140
+ (if (pos? o) (update pc (mod (js/Math.round m) 12) + (* (/ o len) (+ 0.3 (vel n)))) pc))
141
+ pc))
142
+ (vec (repeat 12 0)) notes)
143
+ starts (filter (fn [n] (and (>= (:on n) a) (< (:on n) b))) notes)]
144
+ (conj pcs (reduce + (map vel starts)) (* 0.25 (count starts)))))
145
+ (range (max 0 nf)))))
@@ -0,0 +1,115 @@
1
+ ;; SPDX-License-Identifier: MPL-2.0
2
+ ;; Kairotic · talk: what is said in a room, and a clock the pages of a room agree on (discussion #135,
3
+ ;; Studies 34 · io and 36 · talk). Pure: the page (app/page/20-talk.cljs) does the WebRTC and the sound.
4
+ ;;
5
+ ;; Everything said in a room is ephemeral: it lives in the pages that are there and goes when they go.
6
+ ;; Something stays only when someone keeps it, and then it is a gesture of the work:
7
+ ;; a message → a text annotation (layer :talk), on the box it names if it names one (@b3)
8
+ ;; a voice note → a sound in a sample box (kept by hash, as recorded audio is)
9
+ ;; a video frame → an image annotation
10
+ ;; Anyone keeps what is theirs at once; keeping what is someone else's asks them first.
11
+
12
+ (ns kairotic.talk
13
+ (:require [clojure.string :as str]))
14
+
15
+ (defn signal-url
16
+ "The relay's rooms for a GUN relay address: wss://host/gun → wss://host/signal (nil if it is not one)."
17
+ [gun-url]
18
+ (when-let [[_ scheme host] (re-matches #"(wss?)://([^/\s,]+)(/[^\s,]*)?" (str gun-url))]
19
+ (str scheme "://" host "/signal")))
20
+
21
+ ;; ───────── messages and their anchors ─────────
22
+ ;; In the text: @b3 a box, #12 a point of the history, ^33 a beat (of the room's tempo).
23
+
24
+ (defn anchors
25
+ "The anchors written in a text, in order, without repeating."
26
+ [text]
27
+ (->> (re-seq #"(?:^|\s)(@[A-Za-z][\w~-]*|#\d+|\^\d+(?:\.\d+)?)" (or text ""))
28
+ (map second)
29
+ (map (fn [w] (case (first w)
30
+ "@" {:box (subs w 1)}
31
+ "#" {:point (js/parseInt (subs w 1))}
32
+ "^" {:beat (js/parseFloat (subs w 1))})))
33
+ distinct vec))
34
+
35
+ (defn message
36
+ "A message: who says it (id: this session; who: this browser, which lasts), what, and when (shared ms)."
37
+ [{:keys [id who name]} text t]
38
+ (let [text (str/trim (str text))]
39
+ (when (seq text)
40
+ {:kind :message :id (str who "-" (js/Math.round t) "-" (rand-int 1e6)) :author who :peer id :name name
41
+ :text (subs text 0 (min 2000 (count text))) :t t :anchors (anchors text)})))
42
+
43
+ ;; ───────── keeping, and asking ─────────
44
+ ;; keep: what this page does when someone here wants to keep `item`:
45
+ ;; [:keep] it is theirs: kept now
46
+ ;; [:ask peer] someone else's, and they are in the room: ask them
47
+ ;; [:gone] someone else's, and they have left: it cannot be kept (their consent can't be asked)
48
+
49
+ (defn keep-step
50
+ [me item peers-in-room]
51
+ (cond
52
+ (= (:who me) (:author item)) [:keep]
53
+ (contains? (set peers-in-room) (:peer item)) [:ask (:peer item)]
54
+ :else [:gone]))
55
+
56
+ (defn kept
57
+ "A kept item as the work keeps it: what it is, who made it, who kept it, and only what lasts."
58
+ [item keeper t]
59
+ (cond-> {:kind (:kind item) :author (:author item) :name (:name item) :kept-by (:who keeper) :at t}
60
+ (= :message (:kind item)) (assoc :text (:text item) :anchors (:anchors item))
61
+ (= :note (:kind item)) (assoc :sound (:sound item) :dur (:dur item))
62
+ (= :frame (:kind item)) (assoc :image (:image item))))
63
+
64
+ ;; ───────── pieces: a voice note, a frame, over a data channel ─────────
65
+ ;; A note travels as base64 pieces small enough for any data channel (and for the relay when there is no
66
+ ;; channel), each with the hash of the whole, so the receiver knows when it has it all and that it is
67
+ ;; the same.
68
+
69
+ (def piece-size 12000)
70
+
71
+ (defn pieces
72
+ "A base64 string in pieces: [{:hash h :i 0 :n k :data …} …]."
73
+ [h b64]
74
+ (let [n (max 1 (js/Math.ceil (/ (count b64) piece-size)))]
75
+ (vec (for [i (range n)] {:hash h :i i :n n :data (subs b64 (* i piece-size) (min (count b64) (* (inc i) piece-size)))}))))
76
+
77
+ (defn gather
78
+ "Pieces received so far ({hash → {i data}}) with one more: [new state, the whole base64 when complete]."
79
+ [st {:keys [hash i n data]}]
80
+ (let [got (assoc (get st hash {}) i data)]
81
+ (if (= n (count got))
82
+ [(dissoc st hash) (apply str (map got (range n)))]
83
+ [(assoc st hash got) nil])))
84
+
85
+ ;; ───────── the clock ─────────
86
+ ;; Every page asks the relay for its time (ping → pong): sent at t0, the relay's time ts, back at t1 (ms).
87
+ ;; The relay is ahead of the page by ts − (t0 + t1)/2, give or take half the round trip; of the last
88
+ ;; samples, the median of the third with the shortest trips. Adding it to this page's time gives the
89
+ ;; shared time, whatever each machine's clock says.
90
+
91
+ (def clock-keep 24)
92
+
93
+ (defn new-clock [] {:samples []})
94
+
95
+ (defn clock-sample
96
+ "The clock with one more sample (t0 sent, ts the relay's time, t1 back, ms)."
97
+ [c t0 ts t1]
98
+ (let [rtt (- t1 t0)]
99
+ (if (neg? rtt)
100
+ c
101
+ (update c :samples (fn [ss] (vec (take-last clock-keep (conj ss {:rtt rtt :offset (- ts (/ (+ t0 t1) 2))}))))))))
102
+
103
+ (defn- median [xs] (let [v (vec (sort xs)) n (count v)]
104
+ (when (pos? n) (if (odd? n) (v (quot n 2)) (/ (+ (v (dec (quot n 2))) (v (quot n 2))) 2)))))
105
+
106
+ (defn clock-offset
107
+ "The relay's time minus this page's, in ms (nil before any sample)."
108
+ [c]
109
+ (let [ss (sort-by :rtt (:samples c))]
110
+ (median (map :offset (take (max 1 (js/Math.ceil (/ (count ss) 3))) ss)))))
111
+
112
+ (defn clock-error "Half the shortest round trip: how far off the offset can be, in ms." [c]
113
+ (when (seq (:samples c)) (/ (apply min (map :rtt (:samples c))) 2)))
114
+
115
+ (defn shared-time "This page's time (ms) as shared time, or nil before any sample." [c local] (when-let [o (clock-offset c)] (+ local o)))