@kairotic/controls 0.0.0-stage → 0.1.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 +49 -2
- package/dist/index.mjs +22 -0
- package/dist/keyboard.mjs +9 -0
- package/dist/knob.mjs +9 -0
- package/package.json +37 -4
- package/src/keyboard.cljs +125 -0
- package/src/knob.cljs +45 -0
package/README.md
CHANGED
|
@@ -1,3 +1,50 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Kairotic official packages
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
*[Leer en castellano](../README.es.md)*
|
|
4
|
+
|
|
5
|
+
Components that are not structural live outside the core, in packages by category, published on
|
|
6
|
+
npm under the scope `@kairotic` and served inside claude.ai by jsDelivr.
|
|
7
|
+
|
|
8
|
+
| Package | Classes |
|
|
9
|
+
|---|---|
|
|
10
|
+
| [`@kairotic/synth`](../synth/) | `adsr~ [a d s r]` an envelope that follows a gate · `synth~` a polyphonic synthesizer |
|
|
11
|
+
| [`@kairotic/spatial`](../spatial/) | `pan~ [position]` stereo panning · `space~ [x y]` binaural placement around the listener |
|
|
12
|
+
| [`@kairotic/controls`](../controls/) | `knob [min max initial] [log] [name]` a rotary control with its own view in the patch · `keyboard [low keys] [hold]` a virtual MIDI keyboard with velocity and chords, for synth~ and faust~ |
|
|
13
|
+
|
|
14
|
+
Planned: `cac`, `notation`.
|
|
15
|
+
|
|
16
|
+
## In a work
|
|
17
|
+
|
|
18
|
+
Writing a class of a package (`adsr~ 20 300 0.5 800`) in a work that does not have it brings the
|
|
19
|
+
whole package into the work, as **one** `component` box pinned to its version. The work stays
|
|
20
|
+
complete and sounds the same even if the package changes later. You can also bring one by hand:
|
|
21
|
+
|
|
22
|
+
```clojure
|
|
23
|
+
(packages) ; the official packages and their classes
|
|
24
|
+
(add-package! "@kairotic/synth")
|
|
25
|
+
(import! "npm:@kairotic/synth@0.1.1/dist/index.mjs")
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Writing for a package
|
|
29
|
+
|
|
30
|
+
One file per component in `<package>/src/`, a ClojureScript expression that evaluates to a definition
|
|
31
|
+
(often `(let [helpers …] {…})`). It uses the core only through the [Kairotic API](../../core/kairotic/api.cljs),
|
|
32
|
+
`api/…`, and carries its own AudioWorklet code if it needs one (`api/worklet-node`). Each package
|
|
33
|
+
declares the API version it was written for (`"kairotic": {"api": 1}` in its `package.json`).
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
node packages/build.mjs # every package → <package>/dist/{<component>.mjs, index.mjs}
|
|
37
|
+
node packages/build.mjs synth # one
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
`dist/index.mjs` is the whole package (what a work imports); `dist/<component>.mjs` is each component
|
|
41
|
+
on its own, for `(import! …)` of a single class.
|
|
42
|
+
|
|
43
|
+
## Publishing
|
|
44
|
+
|
|
45
|
+
A GitHub release with the tag `<package>-vX.Y.Z` (`synth-v0.1.0`) publishes that package
|
|
46
|
+
([workflow](../../.github/workflows/publish-packages.yml)), with trusted publishing (no tokens). npm
|
|
47
|
+
only lets a trusted publisher be set on a package that exists, so the first version of each package
|
|
48
|
+
is published by hand once.
|
|
49
|
+
|
|
50
|
+
Licence: [MPL-2.0](../../LICENSE).
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Generated by packages/build.mjs
|
|
2
|
+
export default {
|
|
3
|
+
"package": "@kairotic/controls",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"api": 1,
|
|
6
|
+
"author": "bonuoq",
|
|
7
|
+
"license": "MPL-2.0",
|
|
8
|
+
"components": [
|
|
9
|
+
{
|
|
10
|
+
"class": "keyboard",
|
|
11
|
+
"license": "MPL-2.0",
|
|
12
|
+
"doc": "Virtual MIDI keyboard. Press a key to play it and let go to release it; dragging glides from key to key. ⇧-click toggles a note in a chord that stays after mouse up (⇧-drag adds every key it passes); a plain click starts over; the strip above the keys releases everything. With the word hold every note stays (a latch) until the strip is clicked. Where the key is touched sets the velocity: soft at the top, full at the bottom. Outlets: Hz (0, most recent note first), voice ids = the MIDI numbers (1), gate (2), [:gain velocity] (3). For synth~ connect 0→0 and 1→1; for faust~ connect 0 and 3 to its left inlet.",
|
|
13
|
+
"source": ";; keyboard · a virtual MIDI keyboard with press and release tied to the mouse\n;; Part of @kairotic/controls. Written against the Kairotic API (api/…).\n;; First made with Claude inside Kairotic (Gallery, discussion #2), reviewed for the package.\n(let [;; ── layout (shared by logic and view) ─────────────────\n white? #{0 2 4 5 7 9 11}\n names [\"C\" \"C#\" \"D\" \"D#\" \"E\" \"F\" \"F#\" \"G\" \"G#\" \"A\" \"A#\" \"B\"]\n note-name (fn [n] (str (nth names (mod n 12)) (dec (quot n 12))))\n hz (fn [n] (* 440 (js/Math.pow 2 (/ (- n 69) 12))))\n kw 16 bw 10 top 14 bh 40 wh 66\n layout (fn [args]\n (let [[lo n] (api/padded (if (map? args) [(:low args) (:keys args)] args) [48 25])\n notes (range lo (min 128 (+ lo (max 1 n))))\n step (fn [[acc wi] m]\n (if (white? (mod m 12))\n [(conj acc {:midi m :white true :x (* wi kw) :w kw}) (inc wi)]\n [(conj acc {:midi m :white false :x (- (* wi kw) (/ bw 2)) :w bw}) wi]))\n [ks wn] (reduce step [[] 0] notes)]\n {:keys ks :width (* wn kw)}))\n ;; the key under (x, y), black keys first in the upper part, :stop on the strip above;\n ;; with the velocity of the touch: soft at the top of the key, full at its bottom edge\n hit (fn [args x y]\n (let [ks (:keys (layout args))\n inside (fn [k] (and (>= x (:x k)) (< x (+ (:x k) (:w k)))))\n white-at (fn [] (:midi (first (filter #(and (:white %) (inside %)) ks))))\n black-at (fn [] (:midi (first (filter #(and (not (:white %)) (inside %)) ks))))\n vel (fn [y0 y1] (api/clamp (+ 0.25 (* 0.75 (/ (- y y0) (- y1 y0)))) 0.25 1))]\n (cond\n (< y top) [:stop 1]\n (and (< y (+ top bh)) (black-at)) [(black-at) (vel top (+ top bh))]\n :else [(white-at) (vel top wh)])))\n\n ;; state: {:held [midi …] most recent first · :vel 0–1 · :keep true when the notes stay after mouse up}\n held-of (fn [state] (vec (if (map? state) (:held state) [])))\n vel-of (fn [state] (or (when (map? state) (:vel state)) 0.8))\n front (fn [held k] (vec (cons k (remove #(= % k) held))))\n ;; outlets, sent in this order: voice ids (1), velocity (3), Hz (0), gate (2) —\n ;; ids before Hz as a reader does, gain before the gate opens\n emit (fn [held vel keep]\n {:state {:held held :vel vel :keep (boolean keep)} :show held\n :out [[1 held] [3 [:gain vel]] [0 (mapv hz held)] [2 (if (seq held) 1 0)]]})\n ;; only a change of the held notes goes out\n move (fn [state held vel keep]\n (if (= (held-of state) held)\n {:state {:held held :vel vel :keep (boolean keep)}}\n (emit held vel keep)))]\n\n {;; ── about ──────────────────────────────────────────────\n :class \"keyboard\"\n :license \"MPL-2.0\"\n :doc \"Virtual MIDI keyboard. Press a key to play it and let go to release it; dragging glides from key to key. ⇧-click toggles a note in a chord that stays after mouse up (⇧-drag adds every key it passes); a plain click starts over; the strip above the keys releases everything. With the word hold every note stays (a latch) until the strip is clicked. Where the key is touched sets the velocity: soft at the top, full at the bottom. Outlets: Hz (0, most recent note first), voice ids = the MIDI numbers (1), gate (2), [:gain velocity] (3). For synth~ connect 0→0 and 1→1; for faust~ connect 0 and 3 to its left inlet.\"\n :usage \"keyboard [lowest-midi-note 48] [number-of-keys 25] [hold]\"\n :io [1 4]\n :size (fn [box] [(:width (layout (or (:args box) []))) wh])\n\n ;; ── arguments ──────────────────────────────────────────\n :args [[:low 48] [:keys 25] [:hold false]]\n\n ;; ── logic ──────────────────────────────────────────────\n ;; While the mouse is down the gesture sends [:key k vel] (plain), [:toggle k vel] (⇧ on press)\n ;; or [:add k vel] (⇧ while dragging). On release (:up) it sends [:up], or [:hold [notes] vel]\n ;; when the notes stay: that is what goes into the history and to the rest of a room, so\n ;; replaying it never leaves a note stuck and a chord arrives whole.\n :receive (fn [{:keys [state value args]}]\n (let [held (held-of state)\n vel (vel-of state)\n latch (:hold args)\n midi (fn [x] (when (number? x) (api/clamp (js/Math.round x) 0 127)))\n tag (when (sequential? value) (first value))\n [_ k v] (when tag value)\n v (if (number? v) v vel)]\n (cond\n (#{:key :toggle :add} tag)\n (cond\n (= k :stop) (move state [] vel false)\n (nil? k) {:state state}\n (= tag :key) (move state [k] v latch)\n (= tag :add) (move state (front held k) v true)\n (some #(= % k) held) (move state (vec (remove #(= % k) held)) vel true)\n :else (move state (front held k) v true))\n (= tag :up) (move state [] vel false)\n (= tag :hold) (move state (vec (distinct (keep midi k))) v true)\n (and (= tag :on) (midi k)) (emit (front held (midi k)) v true)\n (= tag :off) (emit (vec (remove #(= % (midi k)) held)) vel true)\n (number? value) (emit [(midi value)] vel latch)\n (= value :bang) (emit held vel (:keep state))\n (= value :stop) (emit [] vel false)\n :else {:state state})))\n\n :gesture (fn [{:keys [args state shown x y dx dy fine up]}]\n (if up\n (let [held (if (sequential? shown) (vec shown) [])]\n (if (and (seq held) (or (:hold args) (:keep state)))\n [:hold held (:vel state)]\n [:up]))\n (let [[k vel] (hit args x y)]\n (cond\n (not fine) [:key k vel]\n (and (zero? dx) (zero? dy)) [:toggle k vel]\n :else [:add k vel]))))\n\n ;; ── view ───────────────────────────────────────────────\n :draw (fn [{:keys [shown args state]}]\n (let [held (set (when (sequential? shown) shown))\n {ks :keys width :width} (layout args)\n kept (and (map? state) (:keep state) (seq held))\n label (if (seq held)\n (str (when kept \"◆ \") (str/join \" \" (map note-name (sort held))))\n (if (:hold args) \"hold · click = latch · ⇧ chord · here = off\" \"press keys · ⇧ chord · here = off\"))]\n (concat\n [[:rect {:x 0 :y 0 :w width :h (- top 2) :r 2 :stroke :grid :fill :bg}]\n [:text {:x 4 :y 10 :anchor :start :size 9 :fill (if kept :accent :soft)} label]]\n (for [k ks :when (:white k)]\n [:rect {:x (:x k) :y top :w kw :h (- wh top) :stroke :soft :fill (if (held (:midi k)) :accent :bg)}])\n (for [k ks :when (and (:white k) (= 0 (mod (:midi k) 12)))]\n [:text {:x (+ (:x k) 8) :y (- wh 3) :anchor :middle :size 8 :fill :soft} (note-name (:midi k))])\n (for [k ks :when (not (:white k))]\n [:rect {:x (:x k) :y top :w bw :h bh :stroke :ink :fill (if (held (:midi k)) :accent :ink)}]))))\n\n ;; ── help ───────────────────────────────────────────────\n :help {:inlets [\"a MIDI number plays it alone; [:on n] adds a note and [:off n] removes it (both kept); [:hold [n …]] sets the held notes; :stop releases all; :bang sends again\"]\n :outlets [\"list of the held notes in Hz, most recent first (synth~, faust~)\"\n \"voice ids: the held MIDI numbers, sent before the Hz (right inlet of synth~)\"\n \"gate: 1 while a note is held, 0 when none (adsr~)\"\n \"[:gain velocity], 0.25–1, before the Hz (left inlet of faust~)\"]\n :try \"keyboard → synth~ (0→0, 1→1) → dac~: press, hold, let go; ⇧-click several keys for a chord, ⇧-click one again to take it out, click the strip to release. With faust~: outlets 0 and 3 to its left inlet, and touch lower on the keys to play louder.\"}})\n"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"class": "knob",
|
|
17
|
+
"license": "MPL-2.0",
|
|
18
|
+
"doc": "Rotary knob: drag it up or down (⇧ for fine control). Sends its value between min and max; with log the travel is logarithmic (frequencies), and with a name it sends [:name value] (e.g. a faust~ parameter). A number sets it and sends it on; a bang sends it again.",
|
|
19
|
+
"source": ";; knob · a rotary control with its own view in the patch\n;; Part of @kairotic/controls. Written against the Kairotic API (api/…).\n{;; ── about ──────────────────────────────────────────────\n :class \"knob\"\n :license \"MPL-2.0\"\n :doc \"Rotary knob: drag it up or down (⇧ for fine control). Sends its value between min and max; with log the travel is logarithmic (frequencies), and with a name it sends [:name value] (e.g. a faust~ parameter). A number sets it and sends it on; a bang sends it again.\"\n :usage \"knob [min max initial] [log] [name]\"\n :io [1 1]\n :size [72 78]\n\n ;; ── arguments ──────────────────────────────────────────\n ;; numbers fill min, max and initial in order; \"log\" is a flag; any other word is the name\n :args [[:min 0] [:max 1] [:initial :min] [:log false] [:name nil]]\n\n ;; ── logic ──────────────────────────────────────────────\n :receive (fn [{:keys [state args value]}]\n (let [current (if (number? state) state (:initial args))\n outgoing (fn [v] (if (:name args) [(:name args) v] v))]\n (cond\n (number? value) (let [v (api/clamp value (:min args) (:max args))]\n {:state v :out [[0 (outgoing v)]] :show v})\n (= value :bang) {:state current :out [[0 (outgoing current)]] :show current}\n :else {:state current})))\n\n ;; on release the value stays in the box text as its initial value (Kairotic Study 13 and later)\n :persist (fn [v] {:initial (api/nice v)})\n\n :gesture (fn [{:keys [start args dy fine]}]\n (let [{lo :min hi :max log? :log} args\n u (api/to-unit (if (number? start) start (:initial args)) lo hi log?)]\n (api/from-unit (- u (/ dy (if fine 800 160))) lo hi log?)))\n\n ;; values for the holes of the view\n :derive (fn [{:keys [shown args]}]\n (let [{lo :min hi :max log? :log} args\n v (if (number? shown) shown (:initial args))]\n {:angle (+ -135 (* 270 (api/to-unit v lo hi log?)))\n :label (str (when (:name args) (str (name (:name args)) \" \")) (api/nice v))}))\n\n ;; ── view ───────────────────────────────────────────────\n :view [[:arc {:cx 36 :cy 33 :r 23 :from -135 :to 135 :stroke :grid :weight 5}]\n [:arc {:cx 36 :cy 33 :r 23 :from -135 :to :$angle :stroke :accent :weight 5}]\n [:circle {:cx 36 :cy 33 :r 16 :stroke :soft :fill :bg}]\n [:needle {:cx 36 :cy 33 :r0 5 :r 17 :angle :$angle :stroke :ink :weight 2.5}]\n [:text {:x 36 :y 72 :anchor :middle :size 11} :$label]]}\n"
|
|
20
|
+
}
|
|
21
|
+
]
|
|
22
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Generated by packages/build.mjs from src/keyboard.cljs
|
|
2
|
+
export default {
|
|
3
|
+
"class": "keyboard",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"author": "bonuoq",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"doc": "Virtual MIDI keyboard. Press a key to play it and let go to release it; dragging glides from key to key. ⇧-click toggles a note in a chord that stays after mouse up (⇧-drag adds every key it passes); a plain click starts over; the strip above the keys releases everything. With the word hold every note stays (a latch) until the strip is clicked. Where the key is touched sets the velocity: soft at the top, full at the bottom. Outlets: Hz (0, most recent note first), voice ids = the MIDI numbers (1), gate (2), [:gain velocity] (3). For synth~ connect 0→0 and 1→1; for faust~ connect 0 and 3 to its left inlet.",
|
|
8
|
+
"source": ";; keyboard · a virtual MIDI keyboard with press and release tied to the mouse\n;; Part of @kairotic/controls. Written against the Kairotic API (api/…).\n;; First made with Claude inside Kairotic (Gallery, discussion #2), reviewed for the package.\n(let [;; ── layout (shared by logic and view) ─────────────────\n white? #{0 2 4 5 7 9 11}\n names [\"C\" \"C#\" \"D\" \"D#\" \"E\" \"F\" \"F#\" \"G\" \"G#\" \"A\" \"A#\" \"B\"]\n note-name (fn [n] (str (nth names (mod n 12)) (dec (quot n 12))))\n hz (fn [n] (* 440 (js/Math.pow 2 (/ (- n 69) 12))))\n kw 16 bw 10 top 14 bh 40 wh 66\n layout (fn [args]\n (let [[lo n] (api/padded (if (map? args) [(:low args) (:keys args)] args) [48 25])\n notes (range lo (min 128 (+ lo (max 1 n))))\n step (fn [[acc wi] m]\n (if (white? (mod m 12))\n [(conj acc {:midi m :white true :x (* wi kw) :w kw}) (inc wi)]\n [(conj acc {:midi m :white false :x (- (* wi kw) (/ bw 2)) :w bw}) wi]))\n [ks wn] (reduce step [[] 0] notes)]\n {:keys ks :width (* wn kw)}))\n ;; the key under (x, y), black keys first in the upper part, :stop on the strip above;\n ;; with the velocity of the touch: soft at the top of the key, full at its bottom edge\n hit (fn [args x y]\n (let [ks (:keys (layout args))\n inside (fn [k] (and (>= x (:x k)) (< x (+ (:x k) (:w k)))))\n white-at (fn [] (:midi (first (filter #(and (:white %) (inside %)) ks))))\n black-at (fn [] (:midi (first (filter #(and (not (:white %)) (inside %)) ks))))\n vel (fn [y0 y1] (api/clamp (+ 0.25 (* 0.75 (/ (- y y0) (- y1 y0)))) 0.25 1))]\n (cond\n (< y top) [:stop 1]\n (and (< y (+ top bh)) (black-at)) [(black-at) (vel top (+ top bh))]\n :else [(white-at) (vel top wh)])))\n\n ;; state: {:held [midi …] most recent first · :vel 0–1 · :keep true when the notes stay after mouse up}\n held-of (fn [state] (vec (if (map? state) (:held state) [])))\n vel-of (fn [state] (or (when (map? state) (:vel state)) 0.8))\n front (fn [held k] (vec (cons k (remove #(= % k) held))))\n ;; outlets, sent in this order: voice ids (1), velocity (3), Hz (0), gate (2) —\n ;; ids before Hz as a reader does, gain before the gate opens\n emit (fn [held vel keep]\n {:state {:held held :vel vel :keep (boolean keep)} :show held\n :out [[1 held] [3 [:gain vel]] [0 (mapv hz held)] [2 (if (seq held) 1 0)]]})\n ;; only a change of the held notes goes out\n move (fn [state held vel keep]\n (if (= (held-of state) held)\n {:state {:held held :vel vel :keep (boolean keep)}}\n (emit held vel keep)))]\n\n {;; ── about ──────────────────────────────────────────────\n :class \"keyboard\"\n :license \"MPL-2.0\"\n :doc \"Virtual MIDI keyboard. Press a key to play it and let go to release it; dragging glides from key to key. ⇧-click toggles a note in a chord that stays after mouse up (⇧-drag adds every key it passes); a plain click starts over; the strip above the keys releases everything. With the word hold every note stays (a latch) until the strip is clicked. Where the key is touched sets the velocity: soft at the top, full at the bottom. Outlets: Hz (0, most recent note first), voice ids = the MIDI numbers (1), gate (2), [:gain velocity] (3). For synth~ connect 0→0 and 1→1; for faust~ connect 0 and 3 to its left inlet.\"\n :usage \"keyboard [lowest-midi-note 48] [number-of-keys 25] [hold]\"\n :io [1 4]\n :size (fn [box] [(:width (layout (or (:args box) []))) wh])\n\n ;; ── arguments ──────────────────────────────────────────\n :args [[:low 48] [:keys 25] [:hold false]]\n\n ;; ── logic ──────────────────────────────────────────────\n ;; While the mouse is down the gesture sends [:key k vel] (plain), [:toggle k vel] (⇧ on press)\n ;; or [:add k vel] (⇧ while dragging). On release (:up) it sends [:up], or [:hold [notes] vel]\n ;; when the notes stay: that is what goes into the history and to the rest of a room, so\n ;; replaying it never leaves a note stuck and a chord arrives whole.\n :receive (fn [{:keys [state value args]}]\n (let [held (held-of state)\n vel (vel-of state)\n latch (:hold args)\n midi (fn [x] (when (number? x) (api/clamp (js/Math.round x) 0 127)))\n tag (when (sequential? value) (first value))\n [_ k v] (when tag value)\n v (if (number? v) v vel)]\n (cond\n (#{:key :toggle :add} tag)\n (cond\n (= k :stop) (move state [] vel false)\n (nil? k) {:state state}\n (= tag :key) (move state [k] v latch)\n (= tag :add) (move state (front held k) v true)\n (some #(= % k) held) (move state (vec (remove #(= % k) held)) vel true)\n :else (move state (front held k) v true))\n (= tag :up) (move state [] vel false)\n (= tag :hold) (move state (vec (distinct (keep midi k))) v true)\n (and (= tag :on) (midi k)) (emit (front held (midi k)) v true)\n (= tag :off) (emit (vec (remove #(= % (midi k)) held)) vel true)\n (number? value) (emit [(midi value)] vel latch)\n (= value :bang) (emit held vel (:keep state))\n (= value :stop) (emit [] vel false)\n :else {:state state})))\n\n :gesture (fn [{:keys [args state shown x y dx dy fine up]}]\n (if up\n (let [held (if (sequential? shown) (vec shown) [])]\n (if (and (seq held) (or (:hold args) (:keep state)))\n [:hold held (:vel state)]\n [:up]))\n (let [[k vel] (hit args x y)]\n (cond\n (not fine) [:key k vel]\n (and (zero? dx) (zero? dy)) [:toggle k vel]\n :else [:add k vel]))))\n\n ;; ── view ───────────────────────────────────────────────\n :draw (fn [{:keys [shown args state]}]\n (let [held (set (when (sequential? shown) shown))\n {ks :keys width :width} (layout args)\n kept (and (map? state) (:keep state) (seq held))\n label (if (seq held)\n (str (when kept \"◆ \") (str/join \" \" (map note-name (sort held))))\n (if (:hold args) \"hold · click = latch · ⇧ chord · here = off\" \"press keys · ⇧ chord · here = off\"))]\n (concat\n [[:rect {:x 0 :y 0 :w width :h (- top 2) :r 2 :stroke :grid :fill :bg}]\n [:text {:x 4 :y 10 :anchor :start :size 9 :fill (if kept :accent :soft)} label]]\n (for [k ks :when (:white k)]\n [:rect {:x (:x k) :y top :w kw :h (- wh top) :stroke :soft :fill (if (held (:midi k)) :accent :bg)}])\n (for [k ks :when (and (:white k) (= 0 (mod (:midi k) 12)))]\n [:text {:x (+ (:x k) 8) :y (- wh 3) :anchor :middle :size 8 :fill :soft} (note-name (:midi k))])\n (for [k ks :when (not (:white k))]\n [:rect {:x (:x k) :y top :w bw :h bh :stroke :ink :fill (if (held (:midi k)) :accent :ink)}]))))\n\n ;; ── help ───────────────────────────────────────────────\n :help {:inlets [\"a MIDI number plays it alone; [:on n] adds a note and [:off n] removes it (both kept); [:hold [n …]] sets the held notes; :stop releases all; :bang sends again\"]\n :outlets [\"list of the held notes in Hz, most recent first (synth~, faust~)\"\n \"voice ids: the held MIDI numbers, sent before the Hz (right inlet of synth~)\"\n \"gate: 1 while a note is held, 0 when none (adsr~)\"\n \"[:gain velocity], 0.25–1, before the Hz (left inlet of faust~)\"]\n :try \"keyboard → synth~ (0→0, 1→1) → dac~: press, hold, let go; ⇧-click several keys for a chord, ⇧-click one again to take it out, click the strip to release. With faust~: outlets 0 and 3 to its left inlet, and touch lower on the keys to play louder.\"}})\n"
|
|
9
|
+
};
|
package/dist/knob.mjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Generated by packages/build.mjs from src/knob.cljs
|
|
2
|
+
export default {
|
|
3
|
+
"class": "knob",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"author": "bonuoq",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"doc": "Rotary knob: drag it up or down (⇧ for fine control). Sends its value between min and max; with log the travel is logarithmic (frequencies), and with a name it sends [:name value] (e.g. a faust~ parameter). A number sets it and sends it on; a bang sends it again.",
|
|
8
|
+
"source": ";; knob · a rotary control with its own view in the patch\n;; Part of @kairotic/controls. Written against the Kairotic API (api/…).\n{;; ── about ──────────────────────────────────────────────\n :class \"knob\"\n :license \"MPL-2.0\"\n :doc \"Rotary knob: drag it up or down (⇧ for fine control). Sends its value between min and max; with log the travel is logarithmic (frequencies), and with a name it sends [:name value] (e.g. a faust~ parameter). A number sets it and sends it on; a bang sends it again.\"\n :usage \"knob [min max initial] [log] [name]\"\n :io [1 1]\n :size [72 78]\n\n ;; ── arguments ──────────────────────────────────────────\n ;; numbers fill min, max and initial in order; \"log\" is a flag; any other word is the name\n :args [[:min 0] [:max 1] [:initial :min] [:log false] [:name nil]]\n\n ;; ── logic ──────────────────────────────────────────────\n :receive (fn [{:keys [state args value]}]\n (let [current (if (number? state) state (:initial args))\n outgoing (fn [v] (if (:name args) [(:name args) v] v))]\n (cond\n (number? value) (let [v (api/clamp value (:min args) (:max args))]\n {:state v :out [[0 (outgoing v)]] :show v})\n (= value :bang) {:state current :out [[0 (outgoing current)]] :show current}\n :else {:state current})))\n\n ;; on release the value stays in the box text as its initial value (Kairotic Study 13 and later)\n :persist (fn [v] {:initial (api/nice v)})\n\n :gesture (fn [{:keys [start args dy fine]}]\n (let [{lo :min hi :max log? :log} args\n u (api/to-unit (if (number? start) start (:initial args)) lo hi log?)]\n (api/from-unit (- u (/ dy (if fine 800 160))) lo hi log?)))\n\n ;; values for the holes of the view\n :derive (fn [{:keys [shown args]}]\n (let [{lo :min hi :max log? :log} args\n v (if (number? shown) shown (:initial args))]\n {:angle (+ -135 (* 270 (api/to-unit v lo hi log?)))\n :label (str (when (:name args) (str (name (:name args)) \" \")) (api/nice v))}))\n\n ;; ── view ───────────────────────────────────────────────\n :view [[:arc {:cx 36 :cy 33 :r 23 :from -135 :to 135 :stroke :grid :weight 5}]\n [:arc {:cx 36 :cy 33 :r 23 :from -135 :to :$angle :stroke :accent :weight 5}]\n [:circle {:cx 36 :cy 33 :r 16 :stroke :soft :fill :bg}]\n [:needle {:cx 36 :cy 33 :r0 5 :r 17 :angle :$angle :stroke :ink :weight 2.5}]\n [:text {:x 36 :y 72 :anchor :middle :size 11} :$label]]}\n"
|
|
9
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kairotic/controls",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Kairotic control components: knob (a rotary control) and keyboard (a virtual MIDI keyboard), with their own views in the patch",
|
|
5
|
+
"author": "bonuoq",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/bonuoq/kairotic.git",
|
|
10
|
+
"directory": "packages/controls"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/bonuoq/kairotic/tree/main/packages#readme",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "dist/index.mjs",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"kairotic": {
|
|
24
|
+
"api": 1
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "node ../build.mjs controls",
|
|
28
|
+
"prepublishOnly": "node ../build.mjs controls"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"kairotic",
|
|
32
|
+
"music",
|
|
33
|
+
"controls",
|
|
34
|
+
"knob",
|
|
35
|
+
"keyboard",
|
|
36
|
+
"midi",
|
|
37
|
+
"patcher"
|
|
38
|
+
]
|
|
39
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
;; keyboard · a virtual MIDI keyboard with press and release tied to the mouse
|
|
2
|
+
;; Part of @kairotic/controls. Written against the Kairotic API (api/…).
|
|
3
|
+
;; First made with Claude inside Kairotic (Gallery, discussion #2), reviewed for the package.
|
|
4
|
+
(let [;; ── layout (shared by logic and view) ─────────────────
|
|
5
|
+
white? #{0 2 4 5 7 9 11}
|
|
6
|
+
names ["C" "C#" "D" "D#" "E" "F" "F#" "G" "G#" "A" "A#" "B"]
|
|
7
|
+
note-name (fn [n] (str (nth names (mod n 12)) (dec (quot n 12))))
|
|
8
|
+
hz (fn [n] (* 440 (js/Math.pow 2 (/ (- n 69) 12))))
|
|
9
|
+
kw 16 bw 10 top 14 bh 40 wh 66
|
|
10
|
+
layout (fn [args]
|
|
11
|
+
(let [[lo n] (api/padded (if (map? args) [(:low args) (:keys args)] args) [48 25])
|
|
12
|
+
notes (range lo (min 128 (+ lo (max 1 n))))
|
|
13
|
+
step (fn [[acc wi] m]
|
|
14
|
+
(if (white? (mod m 12))
|
|
15
|
+
[(conj acc {:midi m :white true :x (* wi kw) :w kw}) (inc wi)]
|
|
16
|
+
[(conj acc {:midi m :white false :x (- (* wi kw) (/ bw 2)) :w bw}) wi]))
|
|
17
|
+
[ks wn] (reduce step [[] 0] notes)]
|
|
18
|
+
{:keys ks :width (* wn kw)}))
|
|
19
|
+
;; the key under (x, y), black keys first in the upper part, :stop on the strip above;
|
|
20
|
+
;; with the velocity of the touch: soft at the top of the key, full at its bottom edge
|
|
21
|
+
hit (fn [args x y]
|
|
22
|
+
(let [ks (:keys (layout args))
|
|
23
|
+
inside (fn [k] (and (>= x (:x k)) (< x (+ (:x k) (:w k)))))
|
|
24
|
+
white-at (fn [] (:midi (first (filter #(and (:white %) (inside %)) ks))))
|
|
25
|
+
black-at (fn [] (:midi (first (filter #(and (not (:white %)) (inside %)) ks))))
|
|
26
|
+
vel (fn [y0 y1] (api/clamp (+ 0.25 (* 0.75 (/ (- y y0) (- y1 y0)))) 0.25 1))]
|
|
27
|
+
(cond
|
|
28
|
+
(< y top) [:stop 1]
|
|
29
|
+
(and (< y (+ top bh)) (black-at)) [(black-at) (vel top (+ top bh))]
|
|
30
|
+
:else [(white-at) (vel top wh)])))
|
|
31
|
+
|
|
32
|
+
;; state: {:held [midi …] most recent first · :vel 0–1 · :keep true when the notes stay after mouse up}
|
|
33
|
+
held-of (fn [state] (vec (if (map? state) (:held state) [])))
|
|
34
|
+
vel-of (fn [state] (or (when (map? state) (:vel state)) 0.8))
|
|
35
|
+
front (fn [held k] (vec (cons k (remove #(= % k) held))))
|
|
36
|
+
;; outlets, sent in this order: voice ids (1), velocity (3), Hz (0), gate (2) —
|
|
37
|
+
;; ids before Hz as a reader does, gain before the gate opens
|
|
38
|
+
emit (fn [held vel keep]
|
|
39
|
+
{:state {:held held :vel vel :keep (boolean keep)} :show held
|
|
40
|
+
:out [[1 held] [3 [:gain vel]] [0 (mapv hz held)] [2 (if (seq held) 1 0)]]})
|
|
41
|
+
;; only a change of the held notes goes out
|
|
42
|
+
move (fn [state held vel keep]
|
|
43
|
+
(if (= (held-of state) held)
|
|
44
|
+
{:state {:held held :vel vel :keep (boolean keep)}}
|
|
45
|
+
(emit held vel keep)))]
|
|
46
|
+
|
|
47
|
+
{;; ── about ──────────────────────────────────────────────
|
|
48
|
+
:class "keyboard"
|
|
49
|
+
:license "MPL-2.0"
|
|
50
|
+
:doc "Virtual MIDI keyboard. Press a key to play it and let go to release it; dragging glides from key to key. ⇧-click toggles a note in a chord that stays after mouse up (⇧-drag adds every key it passes); a plain click starts over; the strip above the keys releases everything. With the word hold every note stays (a latch) until the strip is clicked. Where the key is touched sets the velocity: soft at the top, full at the bottom. Outlets: Hz (0, most recent note first), voice ids = the MIDI numbers (1), gate (2), [:gain velocity] (3). For synth~ connect 0→0 and 1→1; for faust~ connect 0 and 3 to its left inlet."
|
|
51
|
+
:usage "keyboard [lowest-midi-note 48] [number-of-keys 25] [hold]"
|
|
52
|
+
:io [1 4]
|
|
53
|
+
:size (fn [box] [(:width (layout (or (:args box) []))) wh])
|
|
54
|
+
|
|
55
|
+
;; ── arguments ──────────────────────────────────────────
|
|
56
|
+
:args [[:low 48] [:keys 25] [:hold false]]
|
|
57
|
+
|
|
58
|
+
;; ── logic ──────────────────────────────────────────────
|
|
59
|
+
;; While the mouse is down the gesture sends [:key k vel] (plain), [:toggle k vel] (⇧ on press)
|
|
60
|
+
;; or [:add k vel] (⇧ while dragging). On release (:up) it sends [:up], or [:hold [notes] vel]
|
|
61
|
+
;; when the notes stay: that is what goes into the history and to the rest of a room, so
|
|
62
|
+
;; replaying it never leaves a note stuck and a chord arrives whole.
|
|
63
|
+
:receive (fn [{:keys [state value args]}]
|
|
64
|
+
(let [held (held-of state)
|
|
65
|
+
vel (vel-of state)
|
|
66
|
+
latch (:hold args)
|
|
67
|
+
midi (fn [x] (when (number? x) (api/clamp (js/Math.round x) 0 127)))
|
|
68
|
+
tag (when (sequential? value) (first value))
|
|
69
|
+
[_ k v] (when tag value)
|
|
70
|
+
v (if (number? v) v vel)]
|
|
71
|
+
(cond
|
|
72
|
+
(#{:key :toggle :add} tag)
|
|
73
|
+
(cond
|
|
74
|
+
(= k :stop) (move state [] vel false)
|
|
75
|
+
(nil? k) {:state state}
|
|
76
|
+
(= tag :key) (move state [k] v latch)
|
|
77
|
+
(= tag :add) (move state (front held k) v true)
|
|
78
|
+
(some #(= % k) held) (move state (vec (remove #(= % k) held)) vel true)
|
|
79
|
+
:else (move state (front held k) v true))
|
|
80
|
+
(= tag :up) (move state [] vel false)
|
|
81
|
+
(= tag :hold) (move state (vec (distinct (keep midi k))) v true)
|
|
82
|
+
(and (= tag :on) (midi k)) (emit (front held (midi k)) v true)
|
|
83
|
+
(= tag :off) (emit (vec (remove #(= % (midi k)) held)) vel true)
|
|
84
|
+
(number? value) (emit [(midi value)] vel latch)
|
|
85
|
+
(= value :bang) (emit held vel (:keep state))
|
|
86
|
+
(= value :stop) (emit [] vel false)
|
|
87
|
+
:else {:state state})))
|
|
88
|
+
|
|
89
|
+
:gesture (fn [{:keys [args state shown x y dx dy fine up]}]
|
|
90
|
+
(if up
|
|
91
|
+
(let [held (if (sequential? shown) (vec shown) [])]
|
|
92
|
+
(if (and (seq held) (or (:hold args) (:keep state)))
|
|
93
|
+
[:hold held (:vel state)]
|
|
94
|
+
[:up]))
|
|
95
|
+
(let [[k vel] (hit args x y)]
|
|
96
|
+
(cond
|
|
97
|
+
(not fine) [:key k vel]
|
|
98
|
+
(and (zero? dx) (zero? dy)) [:toggle k vel]
|
|
99
|
+
:else [:add k vel]))))
|
|
100
|
+
|
|
101
|
+
;; ── view ───────────────────────────────────────────────
|
|
102
|
+
:draw (fn [{:keys [shown args state]}]
|
|
103
|
+
(let [held (set (when (sequential? shown) shown))
|
|
104
|
+
{ks :keys width :width} (layout args)
|
|
105
|
+
kept (and (map? state) (:keep state) (seq held))
|
|
106
|
+
label (if (seq held)
|
|
107
|
+
(str (when kept "◆ ") (str/join " " (map note-name (sort held))))
|
|
108
|
+
(if (:hold args) "hold · click = latch · ⇧ chord · here = off" "press keys · ⇧ chord · here = off"))]
|
|
109
|
+
(concat
|
|
110
|
+
[[:rect {:x 0 :y 0 :w width :h (- top 2) :r 2 :stroke :grid :fill :bg}]
|
|
111
|
+
[:text {:x 4 :y 10 :anchor :start :size 9 :fill (if kept :accent :soft)} label]]
|
|
112
|
+
(for [k ks :when (:white k)]
|
|
113
|
+
[:rect {:x (:x k) :y top :w kw :h (- wh top) :stroke :soft :fill (if (held (:midi k)) :accent :bg)}])
|
|
114
|
+
(for [k ks :when (and (:white k) (= 0 (mod (:midi k) 12)))]
|
|
115
|
+
[:text {:x (+ (:x k) 8) :y (- wh 3) :anchor :middle :size 8 :fill :soft} (note-name (:midi k))])
|
|
116
|
+
(for [k ks :when (not (:white k))]
|
|
117
|
+
[:rect {:x (:x k) :y top :w bw :h bh :stroke :ink :fill (if (held (:midi k)) :accent :ink)}]))))
|
|
118
|
+
|
|
119
|
+
;; ── help ───────────────────────────────────────────────
|
|
120
|
+
:help {:inlets ["a MIDI number plays it alone; [:on n] adds a note and [:off n] removes it (both kept); [:hold [n …]] sets the held notes; :stop releases all; :bang sends again"]
|
|
121
|
+
:outlets ["list of the held notes in Hz, most recent first (synth~, faust~)"
|
|
122
|
+
"voice ids: the held MIDI numbers, sent before the Hz (right inlet of synth~)"
|
|
123
|
+
"gate: 1 while a note is held, 0 when none (adsr~)"
|
|
124
|
+
"[:gain velocity], 0.25–1, before the Hz (left inlet of faust~)"]
|
|
125
|
+
:try "keyboard → synth~ (0→0, 1→1) → dac~: press, hold, let go; ⇧-click several keys for a chord, ⇧-click one again to take it out, click the strip to release. With faust~: outlets 0 and 3 to its left inlet, and touch lower on the keys to play louder."}})
|
package/src/knob.cljs
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
;; knob · a rotary control with its own view in the patch
|
|
2
|
+
;; Part of @kairotic/controls. Written against the Kairotic API (api/…).
|
|
3
|
+
{;; ── about ──────────────────────────────────────────────
|
|
4
|
+
:class "knob"
|
|
5
|
+
:license "MPL-2.0"
|
|
6
|
+
:doc "Rotary knob: drag it up or down (⇧ for fine control). Sends its value between min and max; with log the travel is logarithmic (frequencies), and with a name it sends [:name value] (e.g. a faust~ parameter). A number sets it and sends it on; a bang sends it again."
|
|
7
|
+
:usage "knob [min max initial] [log] [name]"
|
|
8
|
+
:io [1 1]
|
|
9
|
+
:size [72 78]
|
|
10
|
+
|
|
11
|
+
;; ── arguments ──────────────────────────────────────────
|
|
12
|
+
;; numbers fill min, max and initial in order; "log" is a flag; any other word is the name
|
|
13
|
+
:args [[:min 0] [:max 1] [:initial :min] [:log false] [:name nil]]
|
|
14
|
+
|
|
15
|
+
;; ── logic ──────────────────────────────────────────────
|
|
16
|
+
:receive (fn [{:keys [state args value]}]
|
|
17
|
+
(let [current (if (number? state) state (:initial args))
|
|
18
|
+
outgoing (fn [v] (if (:name args) [(:name args) v] v))]
|
|
19
|
+
(cond
|
|
20
|
+
(number? value) (let [v (api/clamp value (:min args) (:max args))]
|
|
21
|
+
{:state v :out [[0 (outgoing v)]] :show v})
|
|
22
|
+
(= value :bang) {:state current :out [[0 (outgoing current)]] :show current}
|
|
23
|
+
:else {:state current})))
|
|
24
|
+
|
|
25
|
+
;; on release the value stays in the box text as its initial value (Kairotic Study 13 and later)
|
|
26
|
+
:persist (fn [v] {:initial (api/nice v)})
|
|
27
|
+
|
|
28
|
+
:gesture (fn [{:keys [start args dy fine]}]
|
|
29
|
+
(let [{lo :min hi :max log? :log} args
|
|
30
|
+
u (api/to-unit (if (number? start) start (:initial args)) lo hi log?)]
|
|
31
|
+
(api/from-unit (- u (/ dy (if fine 800 160))) lo hi log?)))
|
|
32
|
+
|
|
33
|
+
;; values for the holes of the view
|
|
34
|
+
:derive (fn [{:keys [shown args]}]
|
|
35
|
+
(let [{lo :min hi :max log? :log} args
|
|
36
|
+
v (if (number? shown) shown (:initial args))]
|
|
37
|
+
{:angle (+ -135 (* 270 (api/to-unit v lo hi log?)))
|
|
38
|
+
:label (str (when (:name args) (str (name (:name args)) " ")) (api/nice v))}))
|
|
39
|
+
|
|
40
|
+
;; ── view ───────────────────────────────────────────────
|
|
41
|
+
:view [[:arc {:cx 36 :cy 33 :r 23 :from -135 :to 135 :stroke :grid :weight 5}]
|
|
42
|
+
[:arc {:cx 36 :cy 33 :r 23 :from -135 :to :$angle :stroke :accent :weight 5}]
|
|
43
|
+
[:circle {:cx 36 :cy 33 :r 16 :stroke :soft :fill :bg}]
|
|
44
|
+
[:needle {:cx 36 :cy 33 :r0 5 :r 17 :angle :$angle :stroke :ink :weight 2.5}]
|
|
45
|
+
[:text {:x 36 :y 72 :anchor :middle :size 11} :$label]]}
|