From 1be9f40574aa213d3ef501024e5a0e18cb04fef9 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 18 Oct 2025 10:42:35 +0100 Subject: [PATCH 1/5] timeline feature --- packages/core/impure.mjs | 60 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 packages/core/impure.mjs diff --git a/packages/core/impure.mjs b/packages/core/impure.mjs new file mode 100644 index 000000000..66fc1dd5f --- /dev/null +++ b/packages/core/impure.mjs @@ -0,0 +1,60 @@ +/* +stateful.mjs - File of shame for stateful, impure and otherwise illegal pattern methods +Copyright (C) 2025 Strudel contributors - see +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 . +*/ + +import { register, reify } from './pattern.mjs'; + +let timelines = {}; + +export const reset_timelines = function () { + timelines = {}; +}; + +export const timeline = register( + 'timeline', + function (tpat, pat) { + tpat = reify(tpat); + const f = function (state) { + let scheduler = !!state.controls._cps; + + const timehaps = tpat.query(state); + const result = []; + for (const timehap of timehaps) { + const tlid = timehap.value; + const ignore = false; + let offset; + if (tlid in timelines) { + offset = timelines[tlid]; + } else { + const timearc = timehap.wholeOrPart(); + if (!scheduler || state.span.begin.lt(timearc.midpoint())) { + offset = timearc.begin; + } else { + // Sync to end of timearc if we first see it over halfway into its + // timespan. Allows 'cuing up' next timeline when live coding. + offset = timearc.end; + } + } + if (scheduler) { + // update state + timelines[tlid] = offset; + const negative = 0 - tlid; + if (negative in timelines) { + delete timelines[negative]; + } + } + + const pathaps = pat + .late(offset) + .query(state.setSpan(timehap.part)) + .map((h) => h.setContext(h.combineContext(timehap))); + result.push(...pathaps); + } + return result; + }; + return new Pattern(f, pat._steps); + }, + false, +); From b2ce4591d9bd4260511bb08a06f791f515452456 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 18 Oct 2025 10:42:55 +0100 Subject: [PATCH 2/5] sort exports and add impure.mjs --- packages/core/index.mjs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/core/index.mjs b/packages/core/index.mjs index e4daf445a..9260e3671 100644 --- a/packages/core/index.mjs +++ b/packages/core/index.mjs @@ -1,6 +1,6 @@ /* index.mjs - -Copyright (C) 2022 Strudel contributors - see +Copyright (C) 2025 Strudel contributors - see 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 . */ @@ -11,20 +11,21 @@ import createClock from './zyklus.mjs'; import { logger } from './logger.mjs'; export { Fraction, controls, createClock }; export * from './controls.mjs'; -export * from './hap.mjs'; -export * from './pattern.mjs'; -export * from './signal.mjs'; -export * from './pick.mjs'; -export * from './state.mjs'; -export * from './timespan.mjs'; -export * from './util.mjs'; -export * from './speak.mjs'; -export * from './evaluate.mjs'; -export * from './repl.mjs'; export * from './cyclist.mjs'; +export * from './evaluate.mjs'; +export * from './hap.mjs'; +export * from './impure.mjs'; export * from './logger.mjs'; +export * from './pattern.mjs'; +export * from './pick.mjs'; +export * from './repl.mjs'; +export * from './signal.mjs'; +export * from './speak.mjs'; +export * from './state.mjs'; export * from './time.mjs'; +export * from './timespan.mjs'; export * from './ui.mjs'; +export * from './util.mjs'; export { default as drawLine } from './drawLine.mjs'; // below won't work with runtime.mjs (json import fails) /* import * as p from './package.json'; From 92a82ce4bdf951ae80a34c0e609aa3515951fa6f Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 18 Oct 2025 10:49:15 +0100 Subject: [PATCH 3/5] delint --- packages/core/impure.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/impure.mjs b/packages/core/impure.mjs index 66fc1dd5f..e16cfbe45 100644 --- a/packages/core/impure.mjs +++ b/packages/core/impure.mjs @@ -4,7 +4,7 @@ Copyright (C) 2025 Strudel contributors - see . */ -import { register, reify } from './pattern.mjs'; +import { register, reify, Pattern } from './pattern.mjs'; let timelines = {}; From 8cbe4b945cfbd050ddbd85755c459a65135a90f0 Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 11 Dec 2025 16:39:08 +0000 Subject: [PATCH 4/5] Respond to glossing's review --- packages/core/impure.mjs | 35 +++++++++++++++++++++++++++++------ packages/core/repl.mjs | 4 ++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/packages/core/impure.mjs b/packages/core/impure.mjs index e16cfbe45..84886aa49 100644 --- a/packages/core/impure.mjs +++ b/packages/core/impure.mjs @@ -8,24 +8,48 @@ import { register, reify, Pattern } from './pattern.mjs'; let timelines = {}; +export const reset_state = function () { + reset_timelines(); +}; + export const reset_timelines = function () { timelines = {}; }; +/*** + * Allows you to switch a pattern between different 'timelines'. This is particularly useful when + * live coding, for example when you want to cue a pattern up to play from its start. + * + * Timelines are specified by number, so that if you had a pattern like + * `n("<0 1 2 3>").s("num").timeline(1)` playing, then changed the '1' + * to '2', it would always align '0' to the nearest cycle. You will likely want to trigger + * an evaluation a little bit before the cycle starts, to avoid missing events. + * + * If you change and evaluate a pattern without changing the timeline, it will stay on that timeline + * without resetting. + * + * Rather than incrementing a timeline to reset it, it's easier to negate it, e.g. by switching between `-2` + * and `2`. This is because when you negate a timeline it will always reset. + * + * You can also pattern the timeline if you want, to create strange resetting patterns. + */ + export const timeline = register( 'timeline', function (tpat, pat) { tpat = reify(tpat); const f = function (state) { - let scheduler = !!state.controls._cps; + // Is this called from the scheduler? (rather than from e.g. the visualiser) + const scheduler = !!state.controls._cyclist; const timehaps = tpat.query(state); const result = []; for (const timehap of timehaps) { const tlid = timehap.value; - const ignore = false; let offset; - if (tlid in timelines) { + if (tlid === 0) { + offset = 0; + } else if (tlid in timelines) { offset = timelines[tlid]; } else { const timearc = timehap.wholeOrPart(); @@ -40,9 +64,8 @@ export const timeline = register( if (scheduler) { // update state timelines[tlid] = offset; - const negative = 0 - tlid; - if (negative in timelines) { - delete timelines[negative]; + if (tlid !== 0) { + delete timelines[-tlid]; } } diff --git a/packages/core/repl.mjs b/packages/core/repl.mjs index 171697eb9..cd78cca48 100644 --- a/packages/core/repl.mjs +++ b/packages/core/repl.mjs @@ -5,6 +5,7 @@ import { errorLogger, logger } from './logger.mjs'; import { setTime } from './time.mjs'; import { evalScope } from './evaluate.mjs'; import { register, Pattern, isPattern, silence, stack } from './pattern.mjs'; +import { reset_state } from './impure.mjs'; export function repl({ defaultOutput, @@ -52,6 +53,9 @@ export function repl({ onToggle: (started) => { updateState({ started }); onToggle?.(started); + if (!started) { + reset_state(); + } }, setInterval, clearInterval, From 37990b459f3571d4b254ca99030e21d4cf836104 Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 11 Dec 2025 16:51:25 +0000 Subject: [PATCH 5/5] bugfix and tweak doc --- packages/core/impure.mjs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/core/impure.mjs b/packages/core/impure.mjs index 84886aa49..304f801f5 100644 --- a/packages/core/impure.mjs +++ b/packages/core/impure.mjs @@ -25,13 +25,21 @@ export const reset_timelines = function () { * to '2', it would always align '0' to the nearest cycle. You will likely want to trigger * an evaluation a little bit before the cycle starts, to avoid missing events. * - * If you change and evaluate a pattern without changing the timeline, it will stay on that timeline - * without resetting. + * After the first use, a timeline will continue with the same 'offset'. That is, if you change + * a pattern without changing its timeline number, it will stay on that timeline without resetting. * * Rather than incrementing a timeline to reset it, it's easier to negate it, e.g. by switching between `-2` * and `2`. This is because when you negate a timeline it will always reset. * * You can also pattern the timeline if you want, to create strange resetting patterns. + * @param {number | Pattern} timeline The timeline that the pattern should play on. + * @example + * n("<0 1 2 3>(3,8)") + * .sound("num") + * // resets the timeline every two cycles, by negating the timeline. + * // in a lot of cases this will be edited by a human live coder + * // rather than patterned! + * .timeline("<2 -2>".slow(2)) */ export const timeline = register( @@ -40,8 +48,7 @@ export const timeline = register( tpat = reify(tpat); const f = function (state) { // Is this called from the scheduler? (rather than from e.g. the visualiser) - const scheduler = !!state.controls._cyclist; - + const scheduler = !!state.controls.cyclist; const timehaps = tpat.query(state); const result = []; for (const timehap of timehaps) {