forked from tidalcycles/strudel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mux operator, muxers.mjs with pick variants
- Loading branch information
Showing
4 changed files
with
207 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
muxers.mjs - <short description TODO> | ||
Copyright (C) 2022 Strudel contributors - see <https://github.com/tidalcycles/strudel/blob/main/packages/core/muxers.mjs> | ||
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 { Pattern, register } from './pattern.mjs'; | ||
|
||
|
||
/** * Picks patterns (or plain values) either from a list (by index) or a lookup table (by name). | ||
* Similar to `inhabit`, but maintains the structure of the original patterns. | ||
* @param {Pattern} pat | ||
* @param {*} xs | ||
* @returns {Pattern} | ||
* @example | ||
* note("<0 1 2!2 3>".pick(["g a", "e f", "f g f g" , "g c d"])) | ||
* @example | ||
* sound("<0 1 [2,0]>".pick(["bd sd", "cp cp", "hh hh"])) | ||
* @example | ||
* sound("<0!2 [0,1] 1>".pick(["bd(3,8)", "sd sd"])) | ||
* @example | ||
* s("<a!2 [a,b] b>".pick({a: "bd(3,8)", b: "sd sd"})) | ||
*/ | ||
|
||
export const pick = function (lookup, pat) { | ||
// backward compatibility - the args used to be flipped | ||
if (Array.isArray(pat)) { | ||
[pat, lookup] = [lookup, pat]; | ||
} | ||
return __pick(lookup, pat); | ||
}; | ||
|
||
const __pick = register('pick', function (lookup, pat) { | ||
return pat.mux.in(lookup); | ||
}); | ||
|
||
/** * The same as `pick`, but if you pick a number greater than the size of the list, | ||
* it wraps around, rather than sticking at the maximum value. | ||
* For example, if you pick the fifth pattern of a list of three, you'll get the | ||
* second one. | ||
* @param {Pattern} pat | ||
* @param {*} xs | ||
* @returns {Pattern} | ||
*/ | ||
|
||
export const pickmod = register('pickmod', function (lookup, pat) { | ||
return pat.muxm.in(lookup); | ||
}); | ||
|
||
/** * pickF lets you use a pattern of numbers to pick which function to apply to another pattern. | ||
* @param {Pattern} pat | ||
* @param {Pattern} lookup a pattern of indices | ||
* @param {function[]} funcs the array of functions from which to pull | ||
* @returns {Pattern} | ||
* @example | ||
* s("bd [rim hh]").pickF("<0 1 2>", [rev,jux(rev),fast(2)]) | ||
* @example | ||
* note("<c2 d2>(3,8)").s("square") | ||
* .pickF("<0 2> 1", [jux(rev),fast(2),x=>x.lpf(800)]) | ||
*/ | ||
export const pickF = register('pickF', function (lookup, funcs, pat) { | ||
return pat.apply(pick(lookup, funcs)); | ||
}); | ||
|
||
/** * The same as `pickF`, but if you pick a number greater than the size of the functions list, | ||
* it wraps around, rather than sticking at the maximum value. | ||
* @param {Pattern} pat | ||
* @param {Pattern} lookup a pattern of indices | ||
* @param {function[]} funcs the array of functions from which to pull | ||
* @returns {Pattern} | ||
*/ | ||
export const pickmodF = register('pickmodF', function (lookup, funcs, pat) { | ||
return pat.apply(pickmod(lookup, funcs)); | ||
}); | ||
|
||
/** * Similar to `pick`, but it applies an outerJoin instead of an innerJoin. | ||
* @param {Pattern} pat | ||
* @param {*} xs | ||
* @returns {Pattern} | ||
*/ | ||
export const pickOuter = register('pickOuter', function (lookup, pat) { | ||
return pat.mux.out(lookup); | ||
}); | ||
|
||
/** * The same as `pickOuter`, but if you pick a number greater than the size of the list, | ||
* it wraps around, rather than sticking at the maximum value. | ||
* @param {Pattern} pat | ||
* @param {*} xs | ||
* @returns {Pattern} | ||
*/ | ||
export const pickmodOuter = register('pickmodOuter', function (lookup, pat) { | ||
return pat.muxm.out(lookup); | ||
}); | ||
|
||
/** * Similar to `pick`, but the choosen pattern is restarted when its index is triggered. | ||
* @param {Pattern} pat | ||
* @param {*} xs | ||
* @returns {Pattern} | ||
*/ | ||
export const pickRestart = register('pickRestart', function (lookup, pat) { | ||
return pat.mux.trigzero(lookup); | ||
}); | ||
|
||
/** * The same as `pickRestart`, but if you pick a number greater than the size of the list, | ||
* it wraps around, rather than sticking at the maximum value. | ||
* @param {Pattern} pat | ||
* @param {*} xs | ||
* @returns {Pattern} | ||
*/ | ||
export const pickmodRestart = register('pickmodRestart', function (lookup, pat) { | ||
return pat.muxm.trigzero(lookup); | ||
}); | ||
|
||
/** * Similar to `pick`, but the choosen pattern is reset when its index is triggered. | ||
* @param {Pattern} pat | ||
* @param {*} xs | ||
* @returns {Pattern} | ||
*/ | ||
export const pickReset = register('pickReset', function (lookup, pat) { | ||
return pat.mux.trig(lookup); | ||
}); | ||
|
||
/** * The same as `pickReset`, but if you pick a number greater than the size of the list, | ||
* it wraps around, rather than sticking at the maximum value. | ||
* @param {Pattern} pat | ||
* @param {*} xs | ||
* @returns {Pattern} | ||
*/ | ||
export const pickmodReset = register('pickmodReset', function (lookup, pat) { | ||
return pat.muxm.trig(lookup); | ||
}); | ||
|
||
/** | ||
/** * Picks patterns (or plain values) either from a list (by index) or a lookup table (by name). | ||
* Similar to `pick`, but cycles are squeezed into the target ('inhabited') pattern. | ||
* @param {Pattern} pat | ||
* @param {*} xs | ||
* @returns {Pattern} | ||
* @example | ||
* "<a b [a,b]>".inhabit({a: s("bd(3,8)"), | ||
b: s("cp sd") | ||
}) | ||
* @example | ||
* s("a@2 [a b] a".inhabit({a: "bd(3,8)", b: "sd sd"})).slow(4) | ||
*/ | ||
export const inhabit = register('inhabit', function (lookup, pat) { | ||
return pat.mux.squeeze(lookup); | ||
}); | ||
|
||
/** * The same as `inhabit`, but if you pick a number greater than the size of the list, | ||
* it wraps around, rather than sticking at the maximum value. | ||
* For example, if you pick the fifth pattern of a list of three, you'll get the | ||
* second one. | ||
* @param {Pattern} pat | ||
* @param {*} xs | ||
* @returns {Pattern} | ||
*/ | ||
|
||
export const inhabitmod = register('inhabitmod', function (lookup, pat) { | ||
return pat.muxm.squeeze(lookup); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.