mirror of
https://codeberg.org/uzu/strudel
synced 2026-07-13 14:26:58 -04:00
Merge pull request 'Creates a pattern of numbers in base x from a number or pattern of numbers @stretchyboy' (#2075) from stretchyboy-base-function into main
Reviewed-on: https://codeberg.org/uzu/strudel/pulls/2075
This commit is contained in:
@@ -4132,3 +4132,59 @@ Pattern.prototype.worklet = function (src, ...inputs) {
|
||||
};
|
||||
|
||||
export const worklet = (...args) => pure({}).worklet(...args);
|
||||
|
||||
/**
|
||||
* Creates a pattern of numbers in base b from a number or pattern of numbers
|
||||
* limited to d digits long from the right
|
||||
*
|
||||
* @name base
|
||||
* @tags generators
|
||||
* @param {number} n - number to convert (can be a pattern or array)
|
||||
* @param {number} b - base to convert to (defaults to 10) (can be a pattern)
|
||||
* @param {number} d - max number of digits to produce for each n (defaults to 0 for all) (can be a pattern)
|
||||
* @example
|
||||
* $: note(base("7175 543", 10, 3)).scale("c:major").s("saw")
|
||||
* // $: note("1 7 5 5 4 3").scale("c:major").s("saw")
|
||||
*/
|
||||
export const base = (n, b = 10, d = 0) => {
|
||||
if (Array.isArray(n)) {
|
||||
n = sequence(n);
|
||||
}
|
||||
n = reify(n);
|
||||
b = reify(b);
|
||||
d = reify(d);
|
||||
|
||||
return d
|
||||
.withValue((e) => {
|
||||
return b
|
||||
.withValue((c) => {
|
||||
return n
|
||||
.withValue((v) => {
|
||||
let digits = [];
|
||||
let value = v;
|
||||
while (value > 0) {
|
||||
digits.unshift(value % c);
|
||||
value = Math.floor(value / c);
|
||||
}
|
||||
if (e) {
|
||||
const l = digits.length;
|
||||
if (l > e) {
|
||||
digits = digits.slice(-1 * e);
|
||||
}
|
||||
/*
|
||||
if (l < e){
|
||||
for (let i = l; i < e; i++) {
|
||||
digits.unshift("~");//0); //Would like to be padding this but ~- doesn't work
|
||||
}
|
||||
console.log("digits", digits);
|
||||
}
|
||||
*/
|
||||
}
|
||||
return sequence(digits);
|
||||
})
|
||||
.squeezeJoin();
|
||||
})
|
||||
.squeezeJoin();
|
||||
})
|
||||
.squeezeJoin();
|
||||
};
|
||||
|
||||
@@ -1242,6 +1242,35 @@ exports[`runs examples > example "bank" example index 0 1`] = `
|
||||
]
|
||||
`;
|
||||
|
||||
exports[`runs examples > example "base" example index 0 1`] = `
|
||||
[
|
||||
"[ 0/1 → 1/6 | note:D3 s:saw ]",
|
||||
"[ 1/6 → 1/3 | note:C4 s:saw ]",
|
||||
"[ 1/3 → 1/2 | note:A3 s:saw ]",
|
||||
"[ 1/2 → 2/3 | note:A3 s:saw ]",
|
||||
"[ 2/3 → 5/6 | note:G3 s:saw ]",
|
||||
"[ 5/6 → 1/1 | note:F3 s:saw ]",
|
||||
"[ 1/1 → 7/6 | note:D3 s:saw ]",
|
||||
"[ 7/6 → 4/3 | note:C4 s:saw ]",
|
||||
"[ 4/3 → 3/2 | note:A3 s:saw ]",
|
||||
"[ 3/2 → 5/3 | note:A3 s:saw ]",
|
||||
"[ 5/3 → 11/6 | note:G3 s:saw ]",
|
||||
"[ 11/6 → 2/1 | note:F3 s:saw ]",
|
||||
"[ 2/1 → 13/6 | note:D3 s:saw ]",
|
||||
"[ 13/6 → 7/3 | note:C4 s:saw ]",
|
||||
"[ 7/3 → 5/2 | note:A3 s:saw ]",
|
||||
"[ 5/2 → 8/3 | note:A3 s:saw ]",
|
||||
"[ 8/3 → 17/6 | note:G3 s:saw ]",
|
||||
"[ 17/6 → 3/1 | note:F3 s:saw ]",
|
||||
"[ 3/1 → 19/6 | note:D3 s:saw ]",
|
||||
"[ 19/6 → 10/3 | note:C4 s:saw ]",
|
||||
"[ 10/3 → 7/2 | note:A3 s:saw ]",
|
||||
"[ 7/2 → 11/3 | note:A3 s:saw ]",
|
||||
"[ 11/3 → 23/6 | note:G3 s:saw ]",
|
||||
"[ 23/6 → 4/1 | note:F3 s:saw ]",
|
||||
]
|
||||
`;
|
||||
|
||||
exports[`runs examples > example "beat" example index 0 1`] = `
|
||||
[
|
||||
"[ 0/1 → 1/16 | s:bd ]",
|
||||
|
||||
Reference in New Issue
Block a user