From 1be9f40574aa213d3ef501024e5a0e18cb04fef9 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 18 Oct 2025 10:42:35 +0100 Subject: [PATCH] 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, +);