Skip to content

Commit

Permalink
Merge pull request #1157 from daslyfe/osc_clock_jitter
Browse files Browse the repository at this point in the history
Fix OSC clock jitter
  • Loading branch information
daslyfe authored Aug 9, 2024
2 parents c7c56a1 + 1b01f41 commit d850726
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
3 changes: 3 additions & 0 deletions packages/core/util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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) {
if (isNaN(Number(value))) {
logger(`"${value}" is not a number, falling back to ${fallback}`, 'warning');
Expand Down
43 changes: 32 additions & 11 deletions packages/desktopbridge/oscbridge.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { parseNumeral, Pattern, getEventOffsetMs } from '@strudel/core';
import { parseNumeral, Pattern, averageArray } from '@strudel/core';
import { Invoke } from './utils.mjs';

let offsetTime;
let timeAtPrevOffsetSample;
let prevOffsetTimes = [];

Pattern.prototype.osc = function () {
return this.onTrigger(async (time, hap, currentTime, cps = 1, targetTime) => {
hap.ensureObjectValue();
Expand All @@ -13,7 +17,27 @@ Pattern.prototype.osc = function () {

const params = [];

const timestamp = Math.round(Date.now() + getEventOffsetMs(targetTime, currentTime));
const unixTimeSecs = Date.now() / 1000;
const newOffsetTime = unixTimeSecs - currentTime;
if (offsetTime == null) {
offsetTime = newOffsetTime;
}
prevOffsetTimes.push(newOffsetTime);
if (prevOffsetTimes.length > 8) {
prevOffsetTimes.shift();
}
// 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;
}
}

const timestamp = offsetTime + targetTime;

Object.keys(controls).forEach((key) => {
const val = controls[key];
Expand All @@ -29,15 +53,12 @@ Pattern.prototype.osc = function () {
});
});

const messagesfromjs = [];
if (params.length) {
messagesfromjs.push({ target: '/dirt/play', timestamp, params });
}

if (messagesfromjs.length) {
setTimeout(() => {
Invoke('sendosc', { messagesfromjs });
});
if (params.length === 0) {
return;
}
const message = { target: '/dirt/play', timestamp, params };
setTimeout(() => {
Invoke('sendosc', { messagesfromjs: [message] });
});
});
};
10 changes: 6 additions & 4 deletions src-tauri/src/oscbridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use std::net::UdpSocket;
use serde::Deserialize;
use std::sync::Arc;
use std::thread::sleep;
use std::time::Duration;
use std::time::{Duration};
use tokio::sync::{mpsc, Mutex};

use crate::loggerbridge::Logger;
pub struct OscMsg {
pub msg_buf: Vec<u8>,
pub timestamp: u64,
pub timestamp: f64,
}

pub struct AsyncInputTransmit {
Expand Down Expand Up @@ -106,7 +106,7 @@ pub struct Param {
#[derive(Deserialize)]
pub struct MessageFromJS {
params: Vec<Param>,
timestamp: u64,
timestamp: f64,
target: String,
}
// Called from JS
Expand All @@ -127,9 +127,11 @@ pub async fn sendosc(
args.push(OscType::String(p.value));
}
}
// let start = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();

let time_delay = Duration::from_secs_f64(m.timestamp);
let duration_since_epoch =
Duration::from_millis(m.timestamp) + Duration::new(UNIX_OFFSET, 0);
time_delay + Duration::new(UNIX_OFFSET, 0);

let seconds = u32::try_from(duration_since_epoch.as_secs())
.map_err(|_| "bit conversion failed for osc message timetag")?;
Expand Down
19 changes: 19 additions & 0 deletions test/__snapshots__/examples.test.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5234,6 +5234,25 @@ exports[`runs examples > example "pickF" example index 1 1`] = `
]
`;

exports[`runs examples > example "pickmodRestart" example index 0 1`] = `
[
"[ 0/1 → 1/4 | note:C3 s:piano ]",
"[ 1/4 → 1/2 | note:D3 s:piano ]",
"[ 1/2 → 3/4 | note:E3 s:piano ]",
"[ 3/4 → 1/1 | note:C3 s:piano ]",
"[ 1/1 → 5/4 | note:C3 s:piano ]",
"[ 5/4 → 3/2 | note:D3 s:piano ]",
"[ 3/2 → 7/4 | note:E3 s:piano ]",
"[ 7/4 → 2/1 | note:C3 s:piano ]",
"[ 2/1 → 9/4 | note:E3 s:piano ]",
"[ 9/4 → 5/2 | note:F3 s:piano ]",
"[ 5/2 → 11/4 | note:G3 s:piano ]",
"[ 3/1 → 13/4 | note:E3 s:piano ]",
"[ 13/4 → 7/2 | note:F3 s:piano ]",
"[ 7/2 → 15/4 | note:G3 s:piano ]",
]
`;

exports[`runs examples > example "pitchwheel" example index 0 1`] = `
[
"[ 0/1 → 1/13 | note:C3 s:sawtooth cutoff:500 ]",
Expand Down

0 comments on commit d850726

Please sign in to comment.