diff --git a/packages/core/pattern.mjs b/packages/core/pattern.mjs index f3f19f87..35f9cb6a 100644 --- a/packages/core/pattern.mjs +++ b/packages/core/pattern.mjs @@ -1023,6 +1023,7 @@ function _composeOp(a, b, func) { div: [numeralArgs((a, b) => a / b)], mod: [numeralArgs(_mod)], pow: [numeralArgs(Math.pow)], + log2: [numeralArgs(Math.log2)], band: [numeralArgs((a, b) => a & b)], bor: [numeralArgs((a, b) => a | b)], bxor: [numeralArgs((a, b) => a ^ b)], diff --git a/packages/core/signal.mjs b/packages/core/signal.mjs index c29c0399..5f61fe7a 100644 --- a/packages/core/signal.mjs +++ b/packages/core/signal.mjs @@ -159,6 +159,19 @@ const timeToRands = (t, n) => timeToRandsPrime(timeToIntSeed(t), n); */ export const run = (n) => saw.range(0, n).floor().segment(n); +/** + * @name binary + * Creates a discrete pattern using binary representation. + * @param {number} n - input number to convert to binary + * @example + * "hh".s().struct(binary(5)) + * // "hh".s().struct("1 0 1") + */ +export const binary = (n) => { + const nBits = reify(n).log2(0).floor().add(1); + return binaryN(n, nBits) +}; + /** * @name binaryN * Creates a discrete pattern using binary representation.