mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-26 13:08:28 +00:00
Rework binary to binaryN
This commit is contained in:
parent
947b263b9c
commit
34f4afad01
@ -160,16 +160,19 @@ const timeToRands = (t, n) => timeToRandsPrime(timeToIntSeed(t), n);
|
|||||||
export const run = (n) => saw.range(0, n).floor().segment(n);
|
export const run = (n) => saw.range(0, n).floor().segment(n);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A discrete binary pattern using a decimal/hex number as input
|
* @name binaryN
|
||||||
|
* Creates a discrete pattern using binary representation.
|
||||||
|
* @param {number} n - input number to convert to binary
|
||||||
|
* @param {number} nBits - pattern length, defaults to 16
|
||||||
* @example
|
* @example
|
||||||
* "hh".s().struct(binary(55532))
|
* "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")
|
* // "hh".s().struct("1 1 0 1 1 0 0 0 1 1 1 0 1 1 0 0")
|
||||||
*/
|
*/
|
||||||
export const binary = (n) => {
|
export const binaryN = (n, nBits = 16) => {
|
||||||
const binLen = 16;
|
nBits = reify(nBits);
|
||||||
// Shift right and mask, with msb at the end/right-side
|
// Shift and mask, putting msb on the right-side
|
||||||
const i = run(binLen).mul(-1).add(binLen - 1)
|
const i = run(nBits).mul(-1).add(nBits.sub(1));
|
||||||
return reify(n).segment(binLen).brshift(i).band(pure(1));
|
return reify(n).segment(nBits).brshift(i).band(pure(1));
|
||||||
};
|
};
|
||||||
|
|
||||||
export const randrun = (n) => {
|
export const randrun = (n) => {
|
||||||
|
|||||||
@ -46,7 +46,7 @@ import {
|
|||||||
rev,
|
rev,
|
||||||
time,
|
time,
|
||||||
run,
|
run,
|
||||||
binary,
|
binaryN,
|
||||||
pick,
|
pick,
|
||||||
stackLeft,
|
stackLeft,
|
||||||
stackRight,
|
stackRight,
|
||||||
@ -959,15 +959,15 @@ describe('Pattern', () => {
|
|||||||
expect(run(4).firstCycle()).toStrictEqual(sequence(0, 1, 2, 3).firstCycle());
|
expect(run(4).firstCycle()).toStrictEqual(sequence(0, 1, 2, 3).firstCycle());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('binary', () => {
|
describe('binaryN', () => {
|
||||||
it('Can make a binary pattern from a decimal', () => {
|
it('Can make a binary pattern from a decimal', () => {
|
||||||
expect(binary(55532).firstCycle()).toStrictEqual(
|
expect(binaryN(55532).firstCycle()).toStrictEqual(
|
||||||
sequence(1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0).firstCycle(),
|
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 a numerical pattern', () => {
|
it('Can make a binary pattern from patterned inputs', () => {
|
||||||
expect(binary(pure(0x1337)).firstCycle()).toStrictEqual(
|
expect(binaryN(pure(0x1337), pure(14)).firstCycle()).toStrictEqual(
|
||||||
sequence(0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1).firstCycle(),
|
sequence(0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1).firstCycle(),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -967,7 +967,8 @@ exports[`runs examples > example "begin" example index 0 1`] = `
|
|||||||
]
|
]
|
||||||
`;
|
`;
|
||||||
|
|
||||||
exports[`runs examples > example "binary" 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 ]",
|
"[ 0/1 → 1/16 | s:hh ]",
|
||||||
"[ 1/16 → 1/8 | s:hh ]",
|
"[ 1/16 → 1/8 | s:hh ]",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user