From 8f06bc4b57dd7b6610c2766e428597483c726d72 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Mon, 1 Apr 2024 02:59:17 +0200 Subject: [PATCH] add swing + swingBy --- packages/core/pattern.mjs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/core/pattern.mjs b/packages/core/pattern.mjs index daacc946..8ab308e4 100644 --- a/packages/core/pattern.mjs +++ b/packages/core/pattern.mjs @@ -2110,6 +2110,24 @@ export const segment = register('segment', function (rate, pat) { return pat.struct(pure(true)._fast(rate)).setTactus(rate); }); +/** + * The function `swingBy x n` breaks each cycle into `n` slices, and then delays events in the second half of each slice by the amount `x`, which is relative to the size of the (half) slice. So if `x` is 0 it does nothing, `0.5` delays for half the note duration, and 1 will wrap around to doing nothing again. The end result is a shuffle or swing-like rhythm + * @param {number} subdivision + * @param {number} offset + * @example + * s("hh*8").swingBy(1/3, 4) + */ +export const swingBy = register('swingBy', (swing, n, pat) => pat.inside(n, late(seq(0, swing / 2)))); + +/** + * Shorthand for swingBy with 1/3: + * @param {number} subdivision + * @example + * s("hh*8").swing(4) + * // s("hh*8").swingBy(1/3, 4) + */ +export const swing = register('swing', (n, pat) => pat.swingBy(1 / 3, n)); + /** * Swaps 1s and 0s in a binary pattern. * @name invert