cleaning up

This commit is contained in:
Jade (Rose) Rowland 2024-08-08 22:52:06 -04:00
parent 28334ec94d
commit 814babbf7a
3 changed files with 24 additions and 24 deletions

View File

@ -91,6 +91,9 @@ export const midi2note = (n) => {
// modulo that works with negative numbers e.g. _mod(-1, 3) = 2. Works on numbers (rather than patterns of numbers, as @mod@ from pattern.mjs does) // modulo that works with negative numbers e.g. _mod(-1, 3) = 2. Works on numbers (rather than patterns of numbers, as @mod@ from pattern.mjs does)
export const _mod = (n, m) => ((n % m) + m) % m; export const _mod = (n, m) => ((n % m) + m) % m;
// average numbers in an array
export const averageArray = (arr) => arr.reduce((a, b) => a + b) / arr.length;
export function nanFallback(value, fallback = 0) { export function nanFallback(value, fallback = 0) {
if (isNaN(Number(value))) { if (isNaN(Number(value))) {
logger(`"${value}" is not a number, falling back to ${fallback}`, 'warning'); logger(`"${value}" is not a number, falling back to ${fallback}`, 'warning');

View File

@ -1,13 +1,10 @@
import { parseNumeral, Pattern, getEventOffsetMs } from '@strudel/core'; import { parseNumeral, Pattern, averageArray } from '@strudel/core';
import { Invoke } from './utils.mjs'; import { Invoke } from './utils.mjs';
let offsetTime; let offsetTime;
let timeAtPrevOffsetSample; let timeAtPrevOffsetSample;
let rollingOffsetTime; let prevOffsetTimes = [];
let prevOffsetTimes = []
const averageArray = arr => arr.reduce((a, b) => a + b) / arr.length;
// let prevTime = 0;
Pattern.prototype.osc = function () { Pattern.prototype.osc = function () {
return this.onTrigger(async (time, hap, currentTime, cps = 1, targetTime) => { return this.onTrigger(async (time, hap, currentTime, cps = 1, targetTime) => {
hap.ensureObjectValue(); hap.ensureObjectValue();
@ -20,26 +17,28 @@ Pattern.prototype.osc = function () {
const params = []; const params = [];
const unixTimeSecs = Date.now() / 1000; const unixTimeSecs = Date.now() / 1000;
const newOffsetTime = unixTimeSecs - currentTime; const newOffsetTime = unixTimeSecs - currentTime;
prevOffsetTimes.push(newOffsetTime) if (offsetTime == null) {
if (prevOffsetTimes.length > 8) { offsetTime = newOffsetTime;
prevOffsetTimes.shift()
} }
// every two seconds, the average of the previous 8 offset times is calculated prevOffsetTimes.push(newOffsetTime);
if ( unixTimeSecs - timeAtPrevOffsetSample > 2 || rollingOffsetTime == null) { if (prevOffsetTimes.length > 8) {
timeAtPrevOffsetSample = unixTimeSecs prevOffsetTimes.shift();
rollingOffsetTime = averageArray(prevOffsetTimes); }
// every two seconds, the average of the previous 8 offset times is calculated and used as a stable reference
// for calculating the timestamp that will be sent to the backend
if (timeAtPrevOffsetSample == null || unixTimeSecs - timeAtPrevOffsetSample > 2) {
timeAtPrevOffsetSample = unixTimeSecs;
const rollingOffsetTime = averageArray(prevOffsetTimes);
//account for the js clock freezing or resets set the new offset
if (Math.abs(rollingOffsetTime - offsetTime) > 0.01) {
offsetTime = rollingOffsetTime;
}
} }
//account for the js clock freezing or resets set the new offset const timestamp = offsetTime + targetTime;
if (offsetTime == null || Math.abs(rollingOffsetTime - offsetTime) > .1
) {
offsetTime = rollingOffsetTime;
}
const timestamp = offsetTime + targetTime
Object.keys(controls).forEach((key) => { Object.keys(controls).forEach((key) => {
const val = controls[key]; const val = controls[key];
const value = typeof val === 'number' ? val.toString() : val; const value = typeof val === 'number' ? val.toString() : val;
@ -54,14 +53,12 @@ Pattern.prototype.osc = function () {
}); });
}); });
if (params.length === 0) { if (params.length === 0) {
return return;
} }
const message = { target: '/dirt/play', timestamp, params }; const message = { target: '/dirt/play', timestamp, params };
setTimeout(() => { setTimeout(() => {
Invoke('sendosc', { messagesfromjs: [message] }); Invoke('sendosc', { messagesfromjs: [message] });
}); });
}); });
}; };

View File

@ -6,7 +6,7 @@ use std::net::UdpSocket;
use serde::Deserialize; use serde::Deserialize;
use std::sync::Arc; use std::sync::Arc;
use std::thread::sleep; use std::thread::sleep;
use std::time::{Duration, SystemTime, UNIX_EPOCH}; use std::time::{Duration};
use tokio::sync::{mpsc, Mutex}; use tokio::sync::{mpsc, Mutex};
use crate::loggerbridge::Logger; use crate::loggerbridge::Logger;