@t4sty/cecile-euclid 0.0.0-stage → 1.0.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.
package/README.md CHANGED
@@ -1,3 +1,59 @@
1
- # Temporary Holding Version
1
+ # euclid
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A Euclidean rhythm sequencer for Cécile: it spreads
4
+ `density` hits as evenly as possible over `steps` steps, and plays one step on every clock
5
+ pulse. E(3,8) is the Cuban tresillo, `x..x..x.`; E(5,8) the cinquillo, `x.xx.xx.`.
6
+ The patterns are Bjorklund's, in the rotation of Toussaint's paper
7
+ [*The Euclidean Algorithm Generates Traditional Musical Rhythms*](https://cgm.cs.mcgill.ca/~godfried/publications/banff.pdf).
8
+
9
+ ```
10
+ #import @t4sty/cecile-euclid as euclid
11
+ oscillator @frequency=110 > gain:vca @gain=0 > out
12
+ clock @bpm=480 > euclid:kick @steps=16 @density=5 > ahr @release=0.1 > gain{ $:vca
13
+ ```
14
+
15
+ ## Inlets
16
+
17
+ | Name | What it does |
18
+ |------|--------------|
19
+ | `input` | The clock. Every rising edge (crossing 0.5) moves to the next step. |
20
+ | `reset` | A rising edge makes the next clock pulse play step 0. |
21
+ | `steps` | Length of the pattern, 1 to 32. |
22
+ | `density` | Hits in the pattern, 0 to `steps`. |
23
+ | `rotation` | Shifts the pattern later by this many steps: E(3,8) rotated by 1 is `.x..x..x`. Negative values shift it earlier. |
24
+
25
+ `steps`, `density` and `rotation` can be patched as well as turned. They're rounded to the
26
+ nearest integer and read on each clock edge, so modulating them never cuts a gate short.
27
+
28
+ ## Outlets
29
+
30
+ All three outlets let the clock through (1 while it's high, 0 otherwise), so their gates
31
+ are as long as the clock's `pulseWidth`:
32
+
33
+ | Name | Open on |
34
+ |------|---------|
35
+ | `output` | the hits. |
36
+ | `inverted` | the rests. |
37
+ | `cycle` | the first step of each cycle, to sync or chain other sequencers. |
38
+
39
+ ```
40
+ $euclid:kick } inverted > ahr
41
+ ```
42
+
43
+ ## Development
44
+
45
+ ```
46
+ bun install
47
+ bun run dev # rebuilds on change and serves the module on localhost, from port 5174
48
+ bun test # the algorithm, and the built processor run as Cécile runs it
49
+ bun run build # src/ → dist/processor.js, the single file Cécile loads
50
+ npm publish --access public # typechecks, tests and builds first
51
+ ```
52
+
53
+ With `bun run dev` running, `#import http://localhost:5174 as euclid` in Cécile loads the
54
+ local build; run the import again after a change to reload it. If 5174 is taken, e.g. by
55
+ another module's dev server, it picks the next free port and prints the `#import` to use
56
+ (`PORT=…` forces one).
57
+
58
+ `dist/` isn't committed: Cécile fetches modules from npm (through jsDelivr or unpkg), and
59
+ `prepublishOnly` builds it into the package.
@@ -0,0 +1,80 @@
1
+ // src/euclid.ts
2
+ var MAX_STEPS = 32;
3
+ function bjorklund(steps, pulses) {
4
+ if (pulses <= 0)
5
+ return Array(steps).fill(false);
6
+ if (pulses >= steps)
7
+ return Array(steps).fill(true);
8
+ let a = Array.from({ length: pulses }, () => [true]);
9
+ let b = Array.from({ length: steps - pulses }, () => [false]);
10
+ do {
11
+ const m = Math.min(a.length, b.length);
12
+ const joined = a.slice(0, m).map((g, i) => g.concat(b[i]));
13
+ b = a.length > m ? a.slice(m) : b.slice(m);
14
+ a = joined;
15
+ } while (b.length > 1);
16
+ return a.concat(b).flat();
17
+ }
18
+ var mod = (x, n) => (x % n + n) % n;
19
+ var clamp = (x, lo, hi) => Math.min(Math.max(x, lo), hi);
20
+
21
+ class Euclid {
22
+ position = -1;
23
+ clockHigh = false;
24
+ resetHigh = false;
25
+ steps = 0;
26
+ pulses = -1;
27
+ pattern = [];
28
+ step = { active: false, first: false };
29
+ tick(clock, reset, steps, density, rotation) {
30
+ const resetHigh = reset > 0.5;
31
+ if (resetHigh && !this.resetHigh)
32
+ this.position = -1;
33
+ this.resetHigh = resetHigh;
34
+ const clockHigh = clock > 0.5;
35
+ if (clockHigh && !this.clockHigh)
36
+ this.advance(steps, density, rotation);
37
+ this.clockHigh = clockHigh;
38
+ return clockHigh;
39
+ }
40
+ advance(steps, density, rotation) {
41
+ const n = clamp(Math.round(steps) || 1, 1, MAX_STEPS);
42
+ const k = clamp(Math.round(density) || 0, 0, n);
43
+ const r = Math.round(rotation) || 0;
44
+ if (n != this.steps || k != this.pulses) {
45
+ this.pattern = bjorklund(n, k);
46
+ this.steps = n;
47
+ this.pulses = k;
48
+ }
49
+ this.position = (this.position + 1) % n;
50
+ this.step.active = this.pattern[mod(this.position - r, n)];
51
+ this.step.first = this.position == 0;
52
+ }
53
+ }
54
+
55
+ // src/processor.ts
56
+ class EuclidProcessor extends AudioWorkletProcessor {
57
+ euclid = new Euclid;
58
+ process(inputs, outputs, parameters) {
59
+ const clock = inputs[0]?.[0];
60
+ const reset = inputs[1]?.[0];
61
+ const [out, inv, cycle] = outputs;
62
+ const { steps, density, rotation } = parameters;
63
+ const length = out?.[0]?.length ?? 128;
64
+ const step = this.euclid.step;
65
+ for (let i = 0;i < length; i++) {
66
+ const high = this.euclid.tick(clock?.[i] ?? 0, reset?.[i] ?? 0, steps.length > 1 ? steps[i] : steps[0], density.length > 1 ? density[i] : density[0], rotation.length > 1 ? rotation[i] : rotation[0]);
67
+ write(out, i, high && step.active ? 1 : 0);
68
+ write(inv, i, high && !step.active ? 1 : 0);
69
+ write(cycle, i, high && step.first ? 1 : 0);
70
+ }
71
+ return true;
72
+ }
73
+ }
74
+ function write(output, i, value) {
75
+ if (!output)
76
+ return;
77
+ for (let ch = 0;ch < output.length; ch++)
78
+ output[ch][i] = value;
79
+ }
80
+ registerProcessor("euclid", EuclidProcessor);
package/package.json CHANGED
@@ -1,6 +1,92 @@
1
1
  {
2
2
  "name": "@t4sty/cecile-euclid",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.1",
4
+ "description": "Euclidean rhythm sequencer for Cécile: spreads N hits as evenly as possible over M steps",
5
+ "keywords": [
6
+ "cecile-module",
7
+ "euclidean",
8
+ "rhythm",
9
+ "sequencer",
10
+ "audioworklet"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/t4sty-it/cecile-euclid.git"
15
+ },
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "type": "module",
20
+ "files": [
21
+ "dist/processor.js"
22
+ ],
23
+ "scripts": {
24
+ "dev": "bun scripts/dev.ts",
25
+ "build": "bun build src/processor.ts --outfile dist/processor.js --format esm --target browser",
26
+ "test": "bun test",
27
+ "typecheck": "tsc --noEmit",
28
+ "prepublishOnly": "bun run typecheck && bun test && bun run build"
29
+ },
30
+ "devDependencies": {
31
+ "@types/bun": "^1.2.13",
32
+ "typescript": "^5.8.3"
33
+ },
34
+ "cecile": {
35
+ "api": 1,
36
+ "kind": "worklet",
37
+ "main": "dist/processor.js",
38
+ "processor": "euclid",
39
+ "category": "Control",
40
+ "params": [
41
+ {
42
+ "name": "input",
43
+ "type": "input",
44
+ "dataType": "signal"
45
+ },
46
+ {
47
+ "name": "reset",
48
+ "type": "input",
49
+ "dataType": "signal"
50
+ },
51
+ {
52
+ "name": "steps",
53
+ "type": "input",
54
+ "dataType": "number",
55
+ "value": 8,
56
+ "min": 1,
57
+ "max": 32
58
+ },
59
+ {
60
+ "name": "density",
61
+ "type": "input",
62
+ "dataType": "number",
63
+ "value": 3,
64
+ "min": 0,
65
+ "max": 32
66
+ },
67
+ {
68
+ "name": "rotation",
69
+ "type": "input",
70
+ "dataType": "number",
71
+ "value": 0,
72
+ "min": -31,
73
+ "max": 31
74
+ },
75
+ {
76
+ "name": "output",
77
+ "type": "output",
78
+ "dataType": "signal"
79
+ },
80
+ {
81
+ "name": "inverted",
82
+ "type": "output",
83
+ "dataType": "signal"
84
+ },
85
+ {
86
+ "name": "cycle",
87
+ "type": "output",
88
+ "dataType": "signal"
89
+ }
90
+ ]
91
+ }
92
+ }