Created
January 26, 2026 00:28
-
-
Save nattog/36377b529aeac2d49c6d47b8db78f133 to your computer and use it in GitHub Desktop.
Euclidean rhythms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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