offset calc

This commit is contained in:
Jade (Rose) Rowland 2024-08-08 17:25:45 -04:00
parent 686247f0cb
commit 94ccfa631e
2 changed files with 33 additions and 14 deletions

View File

@ -1,6 +1,7 @@
import { parseNumeral, Pattern, getEventOffsetMs } from '@strudel/core';
import { Invoke } from './utils.mjs';
let offsetTime;
Pattern.prototype.osc = function () {
return this.onTrigger(async (time, hap, currentTime, cps = 1, targetTime) => {
hap.ensureObjectValue();
@ -12,9 +13,17 @@ Pattern.prototype.osc = function () {
controls.note && (controls.note = parseNumeral(controls.note));
const params = [];
// console.log(time, currentTime)
const unixTimeSecs = Date.now() / 1000;
const timestamp = Math.round(Date.now() + getEventOffsetMs(targetTime, currentTime));
if (offsetTime == null) {
const unixTimeSecs = Date.now() / 1000;
offsetTime = unixTimeSecs - currentTime;
}
const timestamp = offsetTime + targetTime
// const timestamp = unixTimeSecs + (targetTime - currentTime)
console.log(offsetTime)
Object.keys(controls).forEach((key) => {
const val = controls[key];
const value = typeof val === 'number' ? val.toString() : val;
@ -29,15 +38,23 @@ Pattern.prototype.osc = function () {
});
});
const messagesfromjs = [];
if (params.length) {
messagesfromjs.push({ target: '/dirt/play', timestamp, params });
if (params.length === 0) {
return
}
const message = { target: '/dirt/play', timestamp, params };
setTimeout(() => {
Invoke('sendosc', { messagesfromjs: [message] });
});
// const messagesfromjs = [];
// if (params.length) {
// messagesfromjs.push({ target: '/dirt/play', timestamp, params });
// }
if (messagesfromjs.length) {
setTimeout(() => {
Invoke('sendosc', { messagesfromjs });
});
}
// if (messagesfromjs.length) {
// setTimeout(() => {
// Invoke('sendosc', { messagesfromjs });
// });
// }
});
};

View File

@ -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, SystemTime, UNIX_EPOCH};
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 {
@ -106,7 +106,7 @@ pub struct Param {
#[derive(Deserialize)]
pub struct MessageFromJS {
params: Vec<Param>,
timestamp: u64,
timestamp: f64,
target: String,
}
// Called from JS
@ -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")?;