Skip to content

Instantly share code, notes, and snippets.

@nattog
Created January 26, 2026 00:28
Show Gist options
  • Select an option

  • Save nattog/36377b529aeac2d49c6d47b8db78f133 to your computer and use it in GitHub Desktop.

Select an option

Save nattog/36377b529aeac2d49c6d47b8db78f133 to your computer and use it in GitHub Desktop.
Euclidean rhythms
function euclid(n, k, r) {
n = Math.floor(n);
k = Math.floor(k);
r = Math.floor(r);
const pulses = Math.min(n, k);
const steps = Math.max(n, k);
const pattern = [];
let previous = null;
const slope = pulses / steps;
for (let i = 0; i < steps; i++) {
const val = Math.floor(slope * i);
pattern.push(val === previous ? 0 : 1);
previous = val;
}
return rotate(pattern, r);
}
function rotate(arr, offset) {
if (offset === 0) return arr;
offset = Math.round(offset) % arr.length;
if (offset < 0) offset += arr.length;
return arr.slice(-offset).concat(arr.slice(0, -offset));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment