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

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