@t4sty/cecile-euclid 0.0.0-stage → 1.0.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 +58 -2
- package/dist/processor.js +80 -0
- package/package.json +41 -4
package/README.md
CHANGED
|
@@ -1,3 +1,59 @@
|
|
|
1
|
-
#
|
|
1
|
+
# euclid
|
|
2
2
|
|
|
3
|
-
|
|
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,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t4sty/cecile-euclid",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Euclidean rhythm sequencer for Cécile: spreads N hits as evenly as possible over M steps",
|
|
5
|
+
"keywords": ["cecile-module", "euclidean", "rhythm", "sequencer", "audioworklet"],
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/t4sty-it/cecile-euclid.git"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"files": ["dist/processor.js"],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"dev": "bun scripts/dev.ts",
|
|
17
|
+
"build": "bun build src/processor.ts --outfile dist/processor.js --format esm --target browser",
|
|
18
|
+
"test": "bun test",
|
|
19
|
+
"typecheck": "tsc --noEmit",
|
|
20
|
+
"prepublishOnly": "bun run typecheck && bun test && bun run build"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/bun": "^1.2.13",
|
|
24
|
+
"typescript": "^5.8.3"
|
|
25
|
+
},
|
|
26
|
+
"cecile": {
|
|
27
|
+
"api": 1,
|
|
28
|
+
"kind": "worklet",
|
|
29
|
+
"main": "dist/processor.js",
|
|
30
|
+
"processor": "euclid",
|
|
31
|
+
"category": "Control",
|
|
32
|
+
"params": [
|
|
33
|
+
{ "name": "input", "type": "input", "dataType": "signal" },
|
|
34
|
+
{ "name": "reset", "type": "input", "dataType": "signal" },
|
|
35
|
+
{ "name": "steps", "type": "input", "dataType": "number", "value": 8, "min": 1, "max": 32 },
|
|
36
|
+
{ "name": "density", "type": "input", "dataType": "number", "value": 3, "min": 0, "max": 32 },
|
|
37
|
+
{ "name": "rotation", "type": "input", "dataType": "number", "value": 0, "min": -31, "max": 31 },
|
|
38
|
+
{ "name": "output", "type": "output", "dataType": "signal" },
|
|
39
|
+
{ "name": "inverted", "type": "output", "dataType": "signal" },
|
|
40
|
+
{ "name": "cycle", "type": "output", "dataType": "signal" }
|
|
41
|
+
]
|
|
42
|
+
}
|
|
43
|
+
}
|