From 86b1e992a158a56d8fa03aef0f6c2407c803015b Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Sat, 2 Apr 2022 21:46:56 +0200 Subject: [PATCH] fix absolute time --- packages/osc/osc.mjs | 8 +++----- repl/src/useCycle.mjs | 9 +++++++-- repl/src/useRepl.mjs | 4 ++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/osc/osc.mjs b/packages/osc/osc.mjs index bfa3cf96..3baefb0f 100644 --- a/packages/osc/osc.mjs +++ b/packages/osc/osc.mjs @@ -1,17 +1,15 @@ import OSC from './node_modules/osc-js/lib/osc.js'; -import { Pattern, isPattern } from '@strudel.cycles/core/strudel.mjs'; +import { Pattern } from '@strudel.cycles/core/strudel.mjs'; const comm = new OSC(); comm.open(); -// TODO - get startTime from scheduler -const startTime = Date.now(); const latency = 0.1; Pattern.prototype.osc = function () { return this._withEvent((event) => { - const onTrigger = (time, event) => { + const onTrigger = (time, event, startedAt) => { const keyvals = Object.entries(event.value).flat(); - const ts = Math.floor(startTime + ((time + latency) * 1000)); + const ts = Math.floor(startedAt + (time + latency) * 1000); const message = new OSC.Message('/dirt/play', ...keyvals); const bundle = new OSC.Bundle([message], ts); bundle.timestamp(ts); // workaround for https://github.com/adzialocha/osc-js/issues/60 diff --git a/repl/src/useCycle.mjs b/repl/src/useCycle.mjs index 199ffb59..9c5f16c4 100644 --- a/repl/src/useCycle.mjs +++ b/repl/src/useCycle.mjs @@ -10,6 +10,8 @@ import { State, TimeSpan } from '@strudel.cycles/core'; ready?: boolean; // if false, query will not be called on change props } */ +export let startedAt; + // function useCycle(props: UseCycleProps) { function useCycle(props) { // onX must use useCallback! @@ -42,7 +44,7 @@ function useCycle(props) { ?.filter((event) => event.part.begin.equals(event.whole.begin)) .forEach((event) => { Tone.getTransport().schedule((time) => { - onEvent(time, event); + onEvent(time, event, startedAt); Tone.Draw.schedule(() => { // do drawing or DOM manipulation here onDraw?.(time, event); @@ -57,7 +59,10 @@ function useCycle(props) { const start = async () => { setStarted(true); - await Tone.start(); + if (!startedAt) { + await Tone.start(); + startedAt = Date.now() - Tone.getContext().currentTime * 1000; + } Tone.getTransport().start('+0.1'); }; const stop = () => { diff --git a/repl/src/useRepl.mjs b/repl/src/useRepl.mjs index f1a3dcde..ce823f9b 100644 --- a/repl/src/useRepl.mjs +++ b/repl/src/useRepl.mjs @@ -27,7 +27,7 @@ function useRepl({ tune, defaultSynth, autolink = true, onEvent, onDraw }) { const cycle = useCycle({ onDraw, onEvent: useCallback( - (time, event) => { + (time, event, startedAt) => { try { onEvent?.(event); const { onTrigger, velocity } = event.context; @@ -41,7 +41,7 @@ function useRepl({ tune, defaultSynth, autolink = true, onEvent, onDraw }) { /* console.warn('no instrument chosen', event); throw new Error(`no instrument chosen for ${JSON.stringify(event)}`); */ } else { - onTrigger(time, event); + onTrigger(time, event, startedAt); } } catch (err) { console.warn(err);