Make latency consistent across all cps

This commit is contained in:
Aria
2026-01-10 11:20:17 -06:00
parent 4743334a17
commit d753eedb90
3 changed files with 15 additions and 3 deletions
+2 -1
View File
@@ -2,7 +2,7 @@ import { NeoCyclist } from './neocyclist.mjs';
import { Cyclist } from './cyclist.mjs';
import { evaluate as _evaluate } from './evaluate.mjs';
import { errorLogger, logger } from './logger.mjs';
import { setTime } from './time.mjs';
import { setCpsFunc, setTime } from './time.mjs';
import { evalScope } from './evaluate.mjs';
import { register, Pattern, isPattern, silence, stack } from './pattern.mjs';
@@ -61,6 +61,7 @@ export function repl({
// NeoCyclist uses a shared worker to communicate between instances, which is not supported on mobile chrome
const scheduler =
sync && typeof SharedWorker != 'undefined' ? new NeoCyclist(schedulerOptions) : new Cyclist(schedulerOptions);
setCpsFunc(() => scheduler.cps);
let pPatterns = {};
let anonymousIndex = 0;
let allTransform;
+9
View File
@@ -1,4 +1,5 @@
let time;
let cpsFunc;
export function getTime() {
if (!time) {
throw new Error('no time set! use setTime to define a time source');
@@ -9,3 +10,11 @@ export function getTime() {
export function setTime(func) {
time = func;
}
export function setCpsFunc(func) {
cpsFunc = func;
}
export function getCps() {
return cpsFunc?.();
}
+4 -2
View File
@@ -5,7 +5,7 @@ This program is free software: you can redistribute it and/or modify it under th
*/
import * as _WebMidi from 'webmidi';
import { Hap, Pattern, TimeSpan, getTime, isPattern, logger, ref, reify } from '@strudel/core';
import { Hap, Pattern, TimeSpan, getCps, getTime, isPattern, logger, ref, reify } from '@strudel/core';
import { noteToMidi, getControlName } from '@strudel/core';
import { Note } from 'webmidi';
import { scheduleAtTime } from '../superdough/helpers.mjs';
@@ -586,7 +586,9 @@ export async function midikeys(input) {
const [note, velocity] = dataBytes;
const noteoff = message.command === 8;
const key = `${input}_${note}`;
const t = getTime() + 0.03; // slight delay so it's not too late for cyclist to catch
const cps = getCps() ?? 0.5;
const latencySeconds = 0.06; // slight delay so it's not too late for cyclist to catch
const t = getTime() + latencySeconds * cps;
const span = new TimeSpan(t, t);
let value = { midikey: key };
if (noteoff) {