fix absolute time

This commit is contained in:
Felix Roos 2022-04-02 21:46:56 +02:00
parent c3b15fffa4
commit 86b1e992a1
3 changed files with 12 additions and 9 deletions

View File

@ -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

View File

@ -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 = () => {

View File

@ -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);