Annote pattern.mjs and signal.mjs

This commit is contained in:
Václav Volhejn 2025-10-01 23:45:48 +02:00
parent c754dab1ef
commit 4a395f372d
4 changed files with 198 additions and 7 deletions

View file

@ -24,6 +24,7 @@ export const signal = (func) => {
* A sawtooth signal between 0 and 1.
*
* @return {Pattern}
* @group generators
* @example
* note("<c3 [eb3,g3] g2 [g3,bb3]>*8")
* .clip(saw.slow(2))
@ -38,6 +39,7 @@ export const saw = signal((t) => t % 1);
* A sawtooth signal between -1 and 1 (like `saw`, but bipolar).
*
* @return {Pattern}
* @group generators
*/
export const saw2 = saw.toBipolar();
@ -45,6 +47,7 @@ export const saw2 = saw.toBipolar();
* A sawtooth signal between 1 and 0 (like `saw`, but flipped).
*
* @return {Pattern}
* @group generators
* @example
* note("<c3 [eb3,g3] g2 [g3,bb3]>*8")
* .clip(isaw.slow(2))
@ -59,6 +62,7 @@ export const isaw = signal((t) => 1 - (t % 1));
* A sawtooth signal between 1 and -1 (like `saw2`, but flipped).
*
* @return {Pattern}
* @group generators
*/
export const isaw2 = isaw.toBipolar();
@ -66,12 +70,14 @@ export const isaw2 = isaw.toBipolar();
* A sine signal between -1 and 1 (like `sine`, but bipolar).
*
* @return {Pattern}
* @group generators
*/
export const sine2 = signal((t) => Math.sin(Math.PI * 2 * t));
/**
* A sine signal between 0 and 1.
* @return {Pattern}
* @group generators
* @example
* n(sine.segment(16).range(0,15))
* .scale("C:minor")
@ -83,6 +89,7 @@ export const sine = sine2.fromBipolar();
* A cosine signal between 0 and 1.
*
* @return {Pattern}
* @group generators
* @example
* n(stack(sine,cosine).segment(16).range(0,15))
* .scale("C:minor")
@ -94,12 +101,14 @@ export const cosine = sine._early(Fraction(1).div(4));
* A cosine signal between -1 and 1 (like `cosine`, but bipolar).
*
* @return {Pattern}
* @group generators
*/
export const cosine2 = sine2._early(Fraction(1).div(4));
/**
* A square signal between 0 and 1.
* @return {Pattern}
* @group generators
* @example
* n(square.segment(4).range(0,7)).scale("C:minor")
*
@ -110,6 +119,7 @@ export const square = signal((t) => Math.floor((t * 2) % 2));
* A square signal between -1 and 1 (like `square`, but bipolar).
*
* @return {Pattern}
* @group generators
*/
export const square2 = square.toBipolar();
@ -117,6 +127,7 @@ export const square2 = square.toBipolar();
* A triangle signal between 0 and 1.
*
* @return {Pattern}
* @group generators
* @example
* n(tri.segment(8).range(0,7)).scale("C:minor")
*
@ -127,6 +138,7 @@ export const tri = fastcat(saw, isaw);
* A triangle signal between -1 and 1 (like `tri`, but bipolar).
*
* @return {Pattern}
* @group generators
*/
export const tri2 = fastcat(saw2, isaw2);
@ -134,6 +146,7 @@ export const tri2 = fastcat(saw2, isaw2);
* An inverted triangle signal between 1 and 0 (like `tri`, but flipped).
*
* @return {Pattern}
* @group generators
* @example
* n(itri.segment(8).range(0,7)).scale("C:minor")
*
@ -144,6 +157,7 @@ export const itri = fastcat(isaw, saw);
* An inverted triangle signal between -1 and 1 (like `itri`, but bipolar).
*
* @return {Pattern}
* @group generators
*/
export const itri2 = fastcat(isaw2, saw2);
@ -151,6 +165,7 @@ export const itri2 = fastcat(isaw2, saw2);
* A signal representing the cycle time.
*
* @return {Pattern}
* @group generators
*/
export const time = signal(id);
@ -158,6 +173,7 @@ export const time = signal(id);
* The mouse's x position value ranges from 0 to 1.
* @name mousex
* @return {Pattern}
* @group external_io
* @example
* n(mousex.segment(4).range(0,7)).scale("C:minor")
*
@ -167,6 +183,7 @@ export const time = signal(id);
* The mouse's y position value ranges from 0 to 1.
* @name mousey
* @return {Pattern}
* @group external_io
* @example
* n(mousey.segment(4).range(0,7)).scale("C:minor")
*
@ -221,6 +238,7 @@ const timeToRands = (t, n) => timeToRandsPrime(timeToIntSeed(t), n);
/**
* A discrete pattern of numbers from 0 to n-1
* @group generators
* @example
* n(run(4)).scale("C4:pentatonic")
* // n("0 1 2 3").scale("C4:pentatonic")
@ -231,6 +249,7 @@ export const run = (n) => saw.range(0, n).round().segment(n);
* Creates a pattern from a binary number.
*
* @name binary
* @group generators
* @param {number} n - input number to convert to binary
* @example
* "hh".s().struct(binary(5))
@ -245,6 +264,7 @@ export const binary = (n) => {
* Creates a pattern from a binary number, padded to n bits long.
*
* @name binaryN
* @group generators
* @param {number} n - input number to convert to binary
* @param {number} nBits - pattern length, defaults to 16
* @example
@ -280,6 +300,7 @@ const _rearrangeWith = (ipat, n, pat) => {
* Slices a pattern into the given number of parts, then plays those parts in random order.
* Each part will be played exactly once per cycle.
* @name shuffle
* @group transforms
* @example
* note("c d e f").sound("piano").shuffle(4)
* @example
@ -293,6 +314,7 @@ export const shuffle = register('shuffle', (n, pat) => {
* Slices a pattern into the given number of parts, then plays those parts at random. Similar to `shuffle`,
* but parts might be played more than once, or not at all, per cycle.
* @name scramble
* @group transforms
* @example
* note("c d e f").sound("piano").scramble(4)
* @example
@ -306,6 +328,7 @@ export const scramble = register('scramble', (n, pat) => {
* A continuous pattern of random numbers, between 0 and 1.
*
* @name rand
* @group generators
* @example
* // randomly change the cutoff
* s("bd*4,hh*8").cutoff(rand.range(500,8000))
@ -323,6 +346,7 @@ export const _brandBy = (p) => rand.fmap((x) => x < p);
* A continuous pattern of 0 or 1 (binary random), with a probability for the value being 1
*
* @name brandBy
* @group generators
* @param {number} probability - a number between 0 and 1
* @example
* s("hh*10").pan(brandBy(0.2))
@ -333,6 +357,7 @@ export const brandBy = (pPat) => reify(pPat).fmap(_brandBy).innerJoin();
* A continuous pattern of 0 or 1 (binary random)
*
* @name brand
* @group generators
* @example
* s("hh*10").pan(brand)
*/
@ -344,6 +369,7 @@ export const _irand = (i) => rand.fmap((x) => Math.trunc(x * i));
* A continuous pattern of random integers, between 0 and n-1.
*
* @name irand
* @group generators
* @param {number} n max value (exclusive)
* @example
* // randomly select scale notes from 0 - 7 (= C to C)
@ -366,6 +392,7 @@ export const __chooseWith = (pat, xs) => {
/**
* Choose from the list of values (or patterns of values) using the given
* pattern of numbers, which should be in the range of 0..1
* @group transforms
* @param {Pattern} pat
* @param {*} xs
* @returns {Pattern}
@ -379,6 +406,7 @@ export const chooseWith = (pat, xs) => {
/**
* As with {chooseWith}, but the structure comes from the chosen values, rather
* than the pattern you're using to choose with.
* @group transforms
* @param {Pattern} pat
* @param {*} xs
* @returns {Pattern}
@ -389,6 +417,7 @@ export const chooseInWith = (pat, xs) => {
/**
* Chooses randomly from the given list of elements.
* @group transforms
* @param {...any} xs values / patterns to choose from.
* @returns {Pattern} - a continuous pattern.
* @example
@ -404,6 +433,7 @@ export const chooseOut = choose;
* Chooses from the given list of values (or patterns of values), according
* to the pattern that the method is called on. The pattern should be in
* the range 0 .. 1.
* @group transforms
* @param {...any} xs
* @returns {Pattern}
*/
@ -414,6 +444,7 @@ Pattern.prototype.choose = function (...xs) {
/**
* As with choose, but the pattern that this method is called on should be
* in the range -1 .. 1
* @group transforms
* @param {...any} xs
* @returns {Pattern}
*/
@ -423,6 +454,7 @@ Pattern.prototype.choose2 = function (...xs) {
/**
* Picks one of the elements at random each cycle.
* @group transforms
* @synonyms randcat
* @returns {Pattern}
* @example
@ -465,6 +497,7 @@ const wchooseWith = (...args) => _wchooseWith(...args).outerJoin();
/**
* Chooses randomly from the given list of elements by giving a probability to each element
* @group transforms
* @param {...any} pairs arrays of value and weight
* @returns {Pattern} - a continuous pattern.
* @example
@ -474,6 +507,7 @@ export const wchoose = (...pairs) => wchooseWith(rand, ...pairs);
/**
* Picks one of the elements at random each cycle by giving a probability to each element
* @group transforms
* @synonyms wrandcat
* @returns {Pattern}
* @example
@ -521,6 +555,7 @@ export const berlinWith = (tpat) => {
/**
* Generates a continuous pattern of [perlin noise](https://en.wikipedia.org/wiki/Perlin_noise), in the range 0..1.
*
* @group generators
* @name perlin
* @example
* // randomly change the cutoff
@ -533,6 +568,7 @@ export const perlin = perlinWith(time.fmap((v) => Number(v)));
* Generates a continuous pattern of [berlin noise](conceived by Jame Coyne and Jade Rowland as a joke but turned out to be surprisingly cool and useful,
* like perlin noise but with sawtooth waves), in the range 0..1.
*
* @group generators
* @name berlin
* @example
* // ascending arpeggios
@ -553,6 +589,7 @@ export const degradeByWith = register(
* 0 = 0% chance of removal
* 1 = 100% chance of removal
*
* @group transforms
* @name degradeBy
* @memberof Pattern
* @param {number} amount - a number between 0 and 1
@ -578,6 +615,7 @@ export const degradeBy = register(
*
* Randomly removes 50% of events from the pattern. Shorthand for `.degradeBy(0.5)`
*
* @group transforms
* @name degrade
* @memberof Pattern
* @returns Pattern
@ -594,6 +632,7 @@ export const degrade = register('degrade', (pat) => pat._degradeBy(0.5), true, t
* 1 = 0% chance of removal
* Events that would be removed by degradeBy are let through by undegradeBy and vice versa (see second example).
*
* @group transforms
* @name undegradeBy
* @memberof Pattern
* @param {number} amount - a number between 0 and 1
@ -622,6 +661,7 @@ export const undegradeBy = register(
* Inverse of `degrade`: Randomly removes 50% of events from the pattern. Shorthand for `.undegradeBy(0.5)`
* Events that would be removed by degrade are let through by undegrade and vice versa (see second example).
*
* @group transforms
* @name undegrade
* @memberof Pattern
* @returns Pattern
@ -640,6 +680,7 @@ export const undegrade = register('undegrade', (pat) => pat._undegradeBy(0.5), t
* Randomly applies the given function by the given probability.
* Similar to `someCyclesBy`
*
* @group transforms
* @name sometimesBy
* @memberof Pattern
* @param {number | Pattern} probability - a number between 0 and 1
@ -659,6 +700,7 @@ export const sometimesBy = register('sometimesBy', function (patx, func, pat) {
*
* Applies the given function with a 50% chance
*
* @group transforms
* @name sometimes
* @memberof Pattern
* @param {function} function - the transformation to apply
@ -680,6 +722,7 @@ export const sometimes = register('sometimes', function (func, pat) {
* @param {number | Pattern} probability - a number between 0 and 1
* @param {function} function - the transformation to apply
* @returns Pattern
* @group transforms
* @example
* s("bd,hh*8").someCyclesBy(.3, x=>x.speed("0.5"))
*/
@ -702,6 +745,7 @@ export const someCyclesBy = register('someCyclesBy', function (patx, func, pat)
* @name someCycles
* @memberof Pattern
* @returns Pattern
* @group transforms
* @example
* s("bd,hh*8").someCycles(x=>x.speed("0.5"))
*/
@ -716,6 +760,7 @@ export const someCycles = register('someCycles', function (func, pat) {
* @name often
* @memberof Pattern
* @returns Pattern
* @group transforms
* @example
* s("hh*8").often(x=>x.speed("0.5"))
*/
@ -730,6 +775,7 @@ export const often = register('often', function (func, pat) {
* @name rarely
* @memberof Pattern
* @returns Pattern
* @group transforms
* @example
* s("hh*8").rarely(x=>x.speed("0.5"))
*/
@ -741,6 +787,7 @@ export const rarely = register('rarely', function (func, pat) {
*
* Shorthand for `.sometimesBy(0.1, fn)`
*
* @group transforms
* @name almostNever
* @memberof Pattern
* @returns Pattern
@ -755,6 +802,7 @@ export const almostNever = register('almostNever', function (func, pat) {
*
* Shorthand for `.sometimesBy(0.9, fn)`
*
* @group transforms
* @name almostAlways
* @memberof Pattern
* @returns Pattern
@ -769,6 +817,7 @@ export const almostAlways = register('almostAlways', function (func, pat) {
*
* Shorthand for `.sometimesBy(0, fn)` (never calls fn)
*
* @group transforms
* @name never
* @memberof Pattern
* @returns Pattern
@ -783,6 +832,7 @@ export const never = register('never', function (_, pat) {
*
* Shorthand for `.sometimesBy(1, fn)` (always calls fn)
*
* @group transforms
* @name always
* @memberof Pattern
* @returns Pattern
@ -811,6 +861,7 @@ export function _keyDown(keyname) {
* Do something on a keypress, or array of keypresses
* [Key name reference](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values)
*
* @group external_io
* @name whenKey
* @memberof Pattern
* @returns Pattern
@ -827,6 +878,7 @@ export const whenKey = register('whenKey', function (input, func, pat) {
* returns true when a key or array of keys is held
* [Key name reference](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values)
*
* @group external_io
* @name keyDown
* @memberof Pattern
* @returns Pattern