Skip to content

Commit

Permalink
Merge pull request tidalcycles#1247 from tidalcycles/get-control
Browse files Browse the repository at this point in the history
add "as" function + getControlName
  • Loading branch information
felixroos authored Jan 29, 2025
2 parents f105ea6 + 1058c27 commit dbbe2d7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
27 changes: 27 additions & 0 deletions packages/core/controls.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ export function createParam(names) {
return func;
}

// maps control alias names to the "main" control name
const controlAlias = new Map();

export function registerControl(names, ...aliases) {
const name = Array.isArray(names) ? names[0] : names;
let bag = {};
bag[name] = createParam(names);
aliases.forEach((alias) => {
bag[alias] = bag[name];
controlAlias.set(alias, name);
Pattern.prototype[alias] = Pattern.prototype[name];
});
return bag;
Expand Down Expand Up @@ -1614,3 +1618,26 @@ export const ar = register('ar', (t, pat) => {
const [attack, release = attack] = t;
return pat.set({ attack, release });
});

export const getControlName = (alias) => {
if (controlAlias.has(alias)) {
return controlAlias.get(alias);
}
return alias;
};

/**
* Sets properties in a batch.
*
* @name as
* @param {Array} mapping the control names that are set
* @example
* "c:.5 a:1 f:.25 e:.8".as("note:clip")
*/
export const as = register('as', (mapping, pat) => {
return pat.fmap((v) => {
v = Array.isArray(v) ? v : [v];
v = Object.fromEntries(mapping.map((prop, i) => [getControlName(prop), v[i]]));
return v;
});
});
6 changes: 5 additions & 1 deletion packages/core/test/controls.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright (C) 2023 Strudel contributors - see <https://github.com/tidalcycles/st
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { s, pan } from '../controls.mjs';
import { s, pan, getControlName } from '../controls.mjs';
import { mini } from '../../mini/mini.mjs';
import { describe, it, expect } from 'vitest';
import Fraction from '../fraction.mjs';
Expand Down Expand Up @@ -39,4 +39,8 @@ describe('controls', () => {
it('combines tactus of the pattern for .mix as lcm', () => {
expect(s(mini('bd cp mt').set.mix(pan(mini('1 2 3 4')))).tactus).toEqual(Fraction(12));
});
it('finds control name by alias', () => {
expect(getControlName('lpf')).toEqual('cutoff');
expect(getControlName('cutoff')).toEqual('cutoff');
});
});
21 changes: 21 additions & 0 deletions test/__snapshots__/examples.test.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,27 @@ exports[`runs examples > example "arrange" example index 0 1`] = `
]
`;

exports[`runs examples > example "as" example index 0 1`] = `
[
"[ 0/1 → 1/4 | note:c clip:0.5 ]",
"[ 1/4 → 1/2 | note:a clip:1 ]",
"[ 1/2 → 3/4 | note:f clip:0.25 ]",
"[ 3/4 → 1/1 | note:e clip:0.8 ]",
"[ 1/1 → 5/4 | note:c clip:0.5 ]",
"[ 5/4 → 3/2 | note:a clip:1 ]",
"[ 3/2 → 7/4 | note:f clip:0.25 ]",
"[ 7/4 → 2/1 | note:e clip:0.8 ]",
"[ 2/1 → 9/4 | note:c clip:0.5 ]",
"[ 9/4 → 5/2 | note:a clip:1 ]",
"[ 5/2 → 11/4 | note:f clip:0.25 ]",
"[ 11/4 → 3/1 | note:e clip:0.8 ]",
"[ 3/1 → 13/4 | note:c clip:0.5 ]",
"[ 13/4 → 7/2 | note:a clip:1 ]",
"[ 7/2 → 15/4 | note:f clip:0.25 ]",
"[ 15/4 → 4/1 | note:e clip:0.8 ]",
]
`;

exports[`runs examples > example "attack" example index 0 1`] = `
[
"[ 0/1 → 1/4 | note:c3 attack:0 ]",
Expand Down
4 changes: 4 additions & 0 deletions website/src/pages/functions/value-modifiers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ This group of functions allows to modify the value of events.

<JsDoc client:idle name="Pattern.ratio" h={0} />

## as

<JsDoc client:idle name="as" h={0} />

# Custom Parameters

You can also create your own parameters:
Expand Down

0 comments on commit dbbe2d7

Please sign in to comment.