From 09b14a57021773e7f703e7e892578b10b3c224de Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Wed, 14 Jun 2023 23:19:04 +0200 Subject: [PATCH] add ratio function --- packages/core/pattern.mjs | 18 ++++++++++++++++++ test/__snapshots__/examples.test.mjs.snap | 17 +++++++++++++++++ .../src/pages/functions/value-modifiers.mdx | 4 ++++ 3 files changed, 39 insertions(+) diff --git a/packages/core/pattern.mjs b/packages/core/pattern.mjs index e3316d40..2e5c1bca 100644 --- a/packages/core/pattern.mjs +++ b/packages/core/pattern.mjs @@ -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 diff --git a/test/__snapshots__/examples.test.mjs.snap b/test/__snapshots__/examples.test.mjs.snap index da9bda11..557516e3 100644 --- a/test/__snapshots__/examples.test.mjs.snap +++ b/test/__snapshots__/examples.test.mjs.snap @@ -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 ]", diff --git a/website/src/pages/functions/value-modifiers.mdx b/website/src/pages/functions/value-modifiers.mdx index 81874672..94372e75 100644 --- a/website/src/pages/functions/value-modifiers.mdx +++ b/website/src/pages/functions/value-modifiers.mdx @@ -139,6 +139,10 @@ This group of functions allows to modify the value of events. +## ratio + + + # Custom Parameters You can also create your own parameters: