diff --git a/packages/core/controls.mjs b/packages/core/controls.mjs index 94f46f44a..d2198765d 100644 --- a/packages/core/controls.mjs +++ b/packages/core/controls.mjs @@ -1525,7 +1525,7 @@ export const { cps } = registerControl('cps'); * note("c a f e").s("piano").clip("<.5 1 2>") * */ -export const { clip, legato } = registerControl('clip', 'legato'); +export const { clip } = registerControl('clip'); /** * Sets the duration of the event in cycles. Similar to clip / legato, it also cuts samples off at the end if they exceed the duration. diff --git a/packages/core/pattern.mjs b/packages/core/pattern.mjs index 5e2066220..0097d2321 100644 --- a/packages/core/pattern.mjs +++ b/packages/core/pattern.mjs @@ -1729,6 +1729,75 @@ export const { compressSpan, compressspan } = register(['compressSpan', 'compres return pat._compress(span.begin, span.end); }); +/** + * fills in the gap between consecutive notes + * + * @name fill + * @memberof Pattern + * @param {Pattern | number | array} fill relative note length + * @returns Pattern + * @example + * s("sawtooth").euclid(11,16).fill("<1 .5>")._pianoroll() + * @example + * // second array parameter is "lookahead" which is the number of cycles in the future to query for the next event + * s("supersaw").euclid(7,16).fill("1:<1 .15>")._pianoroll() + */ +export const fill = register('fill', function (legato, pat) { + let multiplier = legato; + let lookahead = 1; + if (Array.isArray(legato)) { + multiplier = legato[0] ?? 1; + lookahead = legato[1] ?? lookahead; + } + + let spanEnd; + let spanBegin; + return pat + .withQuerySpan((span) => { + spanEnd = span.end; + spanBegin = span.begin; + + return span.withEnd((e) => { + return e.add(lookahead); + }) + // .withBegin(e => e.sub(lookahead)); + }) + .withHaps((haps) => { + const newHaps = []; + haps + .sort((a, b) => { + return a.part.begin.sub(b.part.begin).valueOf(); + }) + .forEach((hap, i) => { + const nextHap = haps[i + 1]; + const partbegin = hap.part.begin; + let partend = hap.part.end; + const wholebegin = hap.whole.begin; + // filter out haps that were not part of the original span + if (wholebegin.gte(spanEnd) || wholebegin.lt(spanBegin)) { + return; + } + let wholeend = hap.whole.end; + if (nextHap != null) { + partend = nextHap.part.begin; + wholeend = nextHap.whole.begin; + } + + let wholeduration = wholeend.sub(wholebegin).mul(multiplier); + let partduration = partend.sub(partbegin).mul(multiplier); + + partend = partbegin.add(partduration); + wholeend = wholebegin.add(wholeduration); + + const newPart = new TimeSpan(partbegin, partend); + const newWhole = new TimeSpan(wholebegin, wholeend); + + newHaps.push(new Hap(newWhole, newPart, hap.value, hap.context)); + }); + return newHaps; + }); +}); + /** * speeds up a pattern like fast, but rather than it playing multiple times as fast would it instead leaves a gap in the remaining space of the cycle. For example, the following will play the sound pattern "bd sn" only once but compressed into the first half of the cycle, i.e. twice as fast. * @name fastGap diff --git a/packages/core/timespan.mjs b/packages/core/timespan.mjs index 4cbfb999a..81db8aac0 100644 --- a/packages/core/timespan.mjs +++ b/packages/core/timespan.mjs @@ -61,6 +61,14 @@ export class TimeSpan { // Applies given function to the end time of the timespan""" return new TimeSpan(this.begin, func_time(this.end)); } + withBegin(func_time) { + // Applies given function to the end time of the timespan""" + return new TimeSpan(func_time(this.begin), this.end); + } + withDuration(func_time) { + const duration = this.end.sub(this.begin) + return new TimeSpan(this.begin, this.begin.add(func_time(duration))) + } withCycle(func_time) { // Like withTime, but time is relative to relative to the cycle (i.e. the diff --git a/packages/draw/animate.mjs b/packages/draw/animate.mjs index d85081519..b31630a10 100644 --- a/packages/draw/animate.mjs +++ b/packages/draw/animate.mjs @@ -53,7 +53,7 @@ Pattern.prototype.animate = function ({ callback, sync = false, smear = 0.5 } = return silence; }; -export const { x, y, w, h, angle, r, fill, smear } = createParams('x', 'y', 'w', 'h', 'angle', 'r', 'fill', 'smear'); +export const { x, y, w, h, angle, r, fill, smear } = createParams('x', 'y', 'w', 'h', 'angle', 'r', 'smear'); export const rescale = register('rescale', function (f, pat) { return pat.mul(x(f).w(f).y(f).h(f)); diff --git a/test/__snapshots__/examples.test.mjs.snap b/test/__snapshots__/examples.test.mjs.snap index 3c1e39f8e..dab0cc861 100644 --- a/test/__snapshots__/examples.test.mjs.snap +++ b/test/__snapshots__/examples.test.mjs.snap @@ -3953,6 +3953,88 @@ exports[`runs examples > example "layer" example index 0 1`] = ` ] `; +exports[`runs examples > example "legato" example index 0 1`] = ` +[ + "[ 0/1 → 1/8 | s:sawtooth ]", + "[ 1/8 → 3/16 | s:sawtooth ]", + "[ 3/16 → 5/16 | s:sawtooth ]", + "[ 5/16 → 3/8 | s:sawtooth ]", + "[ 3/8 → 1/2 | s:sawtooth ]", + "[ 1/2 → 9/16 | s:sawtooth ]", + "[ 9/16 → 11/16 | s:sawtooth ]", + "[ 11/16 → 3/4 | s:sawtooth ]", + "[ 3/4 → 7/8 | s:sawtooth ]", + "[ 7/8 → 15/16 | s:sawtooth ]", + "[ 15/16 → 1/1 | s:sawtooth ]", + "[ 1/1 → 17/16 | s:sawtooth ]", + "[ 9/8 → 37/32 | s:sawtooth ]", + "[ 19/16 → 5/4 | s:sawtooth ]", + "[ 21/16 → 43/32 | s:sawtooth ]", + "[ 11/8 → 23/16 | s:sawtooth ]", + "[ 3/2 → 49/32 | s:sawtooth ]", + "[ 25/16 → 13/8 | s:sawtooth ]", + "[ 27/16 → 55/32 | s:sawtooth ]", + "[ 7/4 → 29/16 | s:sawtooth ]", + "[ 15/8 → 61/32 | s:sawtooth ]", + "[ 31/16 → 63/32 | s:sawtooth ]", + "[ 2/1 → 17/8 | s:sawtooth ]", + "[ 17/8 → 35/16 | s:sawtooth ]", + "[ 35/16 → 37/16 | s:sawtooth ]", + "[ 37/16 → 19/8 | s:sawtooth ]", + "[ 19/8 → 5/2 | s:sawtooth ]", + "[ 5/2 → 41/16 | s:sawtooth ]", + "[ 41/16 → 43/16 | s:sawtooth ]", + "[ 43/16 → 11/4 | s:sawtooth ]", + "[ 11/4 → 23/8 | s:sawtooth ]", + "[ 23/8 → 47/16 | s:sawtooth ]", + "[ 47/16 → 3/1 | s:sawtooth ]", + "[ 3/1 → 49/16 | s:sawtooth ]", + "[ 25/8 → 101/32 | s:sawtooth ]", + "[ 51/16 → 13/4 | s:sawtooth ]", + "[ 53/16 → 107/32 | s:sawtooth ]", + "[ 27/8 → 55/16 | s:sawtooth ]", + "[ 7/2 → 113/32 | s:sawtooth ]", + "[ 57/16 → 29/8 | s:sawtooth ]", + "[ 59/16 → 119/32 | s:sawtooth ]", + "[ 15/4 → 61/16 | s:sawtooth ]", + "[ 31/8 → 125/32 | s:sawtooth ]", + "[ 63/16 → 127/32 | s:sawtooth ]", +] +`; + +exports[`runs examples > example "legato" example index 1 1`] = ` +[ + "[ 0/1 → 3/16 | s:supersaw ]", + "[ 3/16 → 5/16 | s:supersaw ]", + "[ 5/16 → 7/16 | s:supersaw ]", + "[ 7/16 → 5/8 | s:supersaw ]", + "[ 5/8 → 3/4 | s:supersaw ]", + "[ 3/4 → 7/8 | s:supersaw ]", + "[ 7/8 → 1/1 | s:supersaw ]", + "[ 1/1 → 19/16 | s:supersaw ]", + "[ 19/16 → 21/16 | s:supersaw ]", + "[ 21/16 → 23/16 | s:supersaw ]", + "[ 23/16 → 13/8 | s:supersaw ]", + "[ 13/8 → 7/4 | s:supersaw ]", + "[ 7/4 → 15/8 | s:supersaw ]", + "[ 15/8 → 2/1 | s:supersaw ]", + "[ 2/1 → 35/16 | s:supersaw ]", + "[ 35/16 → 37/16 | s:supersaw ]", + "[ 37/16 → 39/16 | s:supersaw ]", + "[ 39/16 → 21/8 | s:supersaw ]", + "[ 21/8 → 11/4 | s:supersaw ]", + "[ 11/4 → 23/8 | s:supersaw ]", + "[ 23/8 → 3/1 | s:supersaw ]", + "[ 3/1 → 51/16 | s:supersaw ]", + "[ 51/16 → 53/16 | s:supersaw ]", + "[ 53/16 → 55/16 | s:supersaw ]", + "[ 55/16 → 29/8 | s:supersaw ]", + "[ 29/8 → 15/4 | s:supersaw ]", + "[ 15/4 → 31/8 | s:supersaw ]", + "[ 31/8 → 4/1 | s:supersaw ]", +] +`; + exports[`runs examples > example "leslie" example index 0 1`] = ` [ "[ 0/1 → 1/1 | n:0 s:supersquare leslie:0 ]",