diff --git a/packages/core/pattern.mjs b/packages/core/pattern.mjs index f3f19f874..35f9cb6a8 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 215eac2f4..31fdbb6ab 100644 --- a/packages/core/signal.mjs +++ b/packages/core/signal.mjs @@ -5,7 +5,7 @@ This program is free software: you can redistribute it and/or modify it under th */ import { Hap } from './hap.mjs'; -import { Pattern, fastcat, reify, silence, stack, register } from './pattern.mjs'; +import { Pattern, fastcat, pure, register, reify, silence, stack } from './pattern.mjs'; import Fraction from './fraction.mjs'; import { id, _mod } from './util.mjs'; @@ -159,6 +159,35 @@ const timeToRands = (t, n) => timeToRandsPrime(timeToIntSeed(t), n); */ export const run = (n) => saw.range(0, n).floor().segment(n); +/** + * @name binary + * Creates a pattern from a binary number. + * @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 pattern from a binary number, padded to n bits long. + * @param {number} n - input number to convert to binary + * @param {number} nBits - pattern length, defaults to 16 + * @example + * "hh".s().struct(binaryN(55532, 16)) + * // "hh".s().struct("1 1 0 1 1 0 0 0 1 1 1 0 1 1 0 0") + */ +export const binaryN = (n, nBits = 16) => { + nBits = reify(nBits); + // Shift and mask, putting msb on the right-side + const bitPos = run(nBits).mul(-1).add(nBits.sub(1)); + return reify(n).segment(nBits).brshift(bitPos).band(pure(1)); +}; + export const randrun = (n) => { return signal((t) => { // Without adding 0.5, the first cycle is always 0,1,2,3,... diff --git a/packages/core/test/pattern.test.mjs b/packages/core/test/pattern.test.mjs index 31ec48681..7e6d8fd82 100644 --- a/packages/core/test/pattern.test.mjs +++ b/packages/core/test/pattern.test.mjs @@ -46,6 +46,7 @@ import { rev, time, run, + binaryN, pick, stackLeft, stackRight, @@ -958,6 +959,18 @@ describe('Pattern', () => { expect(run(4).firstCycle()).toStrictEqual(sequence(0, 1, 2, 3).firstCycle()); }); }); + describe('binaryN', () => { + it('Can make a binary pattern from a decimal', () => { + expect(binaryN(55532).firstCycle()).toStrictEqual( + sequence(1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0).firstCycle(), + ); + }); + it('Can make a binary pattern from patterned inputs', () => { + expect(binaryN(pure(0x1337), pure(14)).firstCycle()).toStrictEqual( + sequence(0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1).firstCycle(), + ); + }); + }); describe('ribbon', () => { it('Can ribbon', () => { expect(cat(0, 1, 2, 3, 4, 5, 6, 7).ribbon(2, 4).fast(4).firstCycle()).toStrictEqual( diff --git a/test/__snapshots__/examples.test.mjs.snap b/test/__snapshots__/examples.test.mjs.snap index 3ad6ea01e..6dce57390 100644 --- a/test/__snapshots__/examples.test.mjs.snap +++ b/test/__snapshots__/examples.test.mjs.snap @@ -967,6 +967,48 @@ exports[`runs examples > example "begin" example index 0 1`] = ` ] `; +exports[`runs examples > example "binaryN +Creates a discrete pattern using binary representation." example index 0 1`] = ` +[ + "[ 0/1 → 1/16 | s:hh ]", + "[ 1/16 → 1/8 | s:hh ]", + "[ 3/16 → 1/4 | s:hh ]", + "[ 1/4 → 5/16 | s:hh ]", + "[ 1/2 → 9/16 | s:hh ]", + "[ 9/16 → 5/8 | s:hh ]", + "[ 5/8 → 11/16 | s:hh ]", + "[ 3/4 → 13/16 | s:hh ]", + "[ 13/16 → 7/8 | s:hh ]", + "[ 1/1 → 17/16 | s:hh ]", + "[ 17/16 → 9/8 | s:hh ]", + "[ 19/16 → 5/4 | s:hh ]", + "[ 5/4 → 21/16 | s:hh ]", + "[ 3/2 → 25/16 | s:hh ]", + "[ 25/16 → 13/8 | s:hh ]", + "[ 13/8 → 27/16 | s:hh ]", + "[ 7/4 → 29/16 | s:hh ]", + "[ 29/16 → 15/8 | s:hh ]", + "[ 2/1 → 33/16 | s:hh ]", + "[ 33/16 → 17/8 | s:hh ]", + "[ 35/16 → 9/4 | s:hh ]", + "[ 9/4 → 37/16 | s:hh ]", + "[ 5/2 → 41/16 | s:hh ]", + "[ 41/16 → 21/8 | s:hh ]", + "[ 21/8 → 43/16 | s:hh ]", + "[ 11/4 → 45/16 | s:hh ]", + "[ 45/16 → 23/8 | s:hh ]", + "[ 3/1 → 49/16 | s:hh ]", + "[ 49/16 → 25/8 | s:hh ]", + "[ 51/16 → 13/4 | s:hh ]", + "[ 13/4 → 53/16 | s:hh ]", + "[ 7/2 → 57/16 | s:hh ]", + "[ 57/16 → 29/8 | s:hh ]", + "[ 29/8 → 59/16 | s:hh ]", + "[ 15/4 → 61/16 | s:hh ]", + "[ 61/16 → 31/8 | s:hh ]", +] +`; + exports[`runs examples > example "bite" example index 0 1`] = ` [ "[ 0/1 → 1/8 | note:Bb3 ]",