add ratio function

This commit is contained in:
Felix Roos 2023-06-14 23:19:04 +02:00
parent e4a35fdd72
commit 09b14a5702
3 changed files with 39 additions and 0 deletions

View File

@ -1582,6 +1582,24 @@ export const range2 = register('range2', function (min, max, pat) {
return pat.fromBipolar()._range(min, max);
});
/**
* Allows dividing numbers via list notation using ":".
* Returns a new pattern with just numbers.
* @name ratio
* @memberof Pattern
* @returns Pattern
* @example
* ratio("1, 5:4, 3:2").mul(110).freq().s("piano").slow(2)
*/
export const ratio = register('ratio', (pat) =>
pat.fmap((v) => {
if (!Array.isArray(v)) {
return v;
}
return v.slice(1).reduce((acc, n) => acc / n, v[0]);
}),
);
//////////////////////////////////////////////////////////////////////
// Structural and temporal transformations

View File

@ -3193,6 +3193,23 @@ exports[`runs examples > example "rarely" example index 0 1`] = `
]
`;
exports[`runs examples > example "ratio" example index 0 1`] = `
[
"[ (0/1 → 1/1) ⇝ 2/1 | freq:110 s:piano ]",
"[ (0/1 → 1/1) ⇝ 2/1 | freq:137.5 s:piano ]",
"[ (0/1 → 1/1) ⇝ 2/1 | freq:165 s:piano ]",
"[ 0/1 ⇜ (1/1 → 2/1) | freq:110 s:piano ]",
"[ 0/1 ⇜ (1/1 → 2/1) | freq:137.5 s:piano ]",
"[ 0/1 ⇜ (1/1 → 2/1) | freq:165 s:piano ]",
"[ (2/1 → 3/1) ⇝ 4/1 | freq:110 s:piano ]",
"[ (2/1 → 3/1) ⇝ 4/1 | freq:137.5 s:piano ]",
"[ (2/1 → 3/1) ⇝ 4/1 | freq:165 s:piano ]",
"[ 2/1 ⇜ (3/1 → 4/1) | freq:110 s:piano ]",
"[ 2/1 ⇜ (3/1 → 4/1) | freq:137.5 s:piano ]",
"[ 2/1 ⇜ (3/1 → 4/1) | freq:165 s:piano ]",
]
`;
exports[`runs examples > example "release" example index 0 1`] = `
[
"[ 0/1 → 1/4 | note:c3 release:0 ]",

View File

@ -139,6 +139,10 @@ This group of functions allows to modify the value of events.
<JsDoc client:idle name="Pattern.range2" h={0} />
## ratio
<JsDoc client:idle name="Pattern.ratio" h={0} />
# Custom Parameters
You can also create your own parameters: