Merge pull request #1062 from daslyfe/osc_time_fix
fix OSC timing for recent scheduler updates
This commit is contained in:
commit
9348a8015a
7 changed files with 141 additions and 128 deletions
|
|
@ -61,6 +61,11 @@ export const valueToMidi = (value, fallbackValue) => {
|
||||||
return fallbackValue;
|
return fallbackValue;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// used to schedule external event like midi and osc out
|
||||||
|
export const getEventOffsetMs = (targetTimeSeconds, currentTimeSeconds) => {
|
||||||
|
return (targetTimeSeconds - currentTimeSeconds) * 1000;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated does not appear to be referenced or invoked anywhere in the codebase
|
* @deprecated does not appear to be referenced or invoked anywhere in the codebase
|
||||||
* @noAutocomplete
|
* @noAutocomplete
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { Invoke } from './utils.mjs';
|
import { Invoke } from './utils.mjs';
|
||||||
import { Pattern, noteToMidi } from '@strudel/core';
|
import { Pattern, getEventOffsetMs, noteToMidi } from '@strudel/core';
|
||||||
|
|
||||||
const ON_MESSAGE = 0x90;
|
const ON_MESSAGE = 0x90;
|
||||||
const OFF_MESSAGE = 0x80;
|
const OFF_MESSAGE = 0x80;
|
||||||
|
|
@ -9,8 +9,8 @@ Pattern.prototype.midi = function (output) {
|
||||||
return this.onTrigger((time_deprecate, hap, currentTime, cps, targetTime) => {
|
return this.onTrigger((time_deprecate, hap, currentTime, cps, targetTime) => {
|
||||||
let { note, nrpnn, nrpv, ccn, ccv, velocity = 0.9, gain = 1 } = hap.value;
|
let { note, nrpnn, nrpv, ccn, ccv, velocity = 0.9, gain = 1 } = hap.value;
|
||||||
//magic number to get audio engine to line up, can probably be calculated somehow
|
//magic number to get audio engine to line up, can probably be calculated somehow
|
||||||
const latency = 0.034;
|
const latencyMs = 34;
|
||||||
const offset = (targetTime - currentTime + latency) * 1000;
|
const offset = getEventOffsetMs(targetTime, currentTime) + latencyMs;
|
||||||
velocity = Math.floor(gain * velocity * 100);
|
velocity = Math.floor(gain * velocity * 100);
|
||||||
const duration = Math.floor((hap.duration.valueOf() / cps) * 1000 - 10);
|
const duration = Math.floor((hap.duration.valueOf() / cps) * 1000 - 10);
|
||||||
const roundedOffset = Math.round(offset);
|
const roundedOffset = Math.round(offset);
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import { parseNumeral, Pattern } from '@strudel/core';
|
import { parseNumeral, Pattern, getEventOffsetMs } from '@strudel/core';
|
||||||
import { Invoke } from './utils.mjs';
|
import { Invoke } from './utils.mjs';
|
||||||
|
|
||||||
Pattern.prototype.osc = function () {
|
Pattern.prototype.osc = function () {
|
||||||
return this.onTrigger(async (time, hap, currentTime, cps = 1) => {
|
return this.onTrigger(async (time, hap, currentTime, cps = 1, targetTime) => {
|
||||||
hap.ensureObjectValue();
|
hap.ensureObjectValue();
|
||||||
const cycle = hap.wholeOrPart().begin.valueOf();
|
const cycle = hap.wholeOrPart().begin.valueOf();
|
||||||
const delta = hap.duration.valueOf();
|
const delta = hap.duration.valueOf();
|
||||||
|
|
@ -13,7 +13,7 @@ Pattern.prototype.osc = function () {
|
||||||
|
|
||||||
const params = [];
|
const params = [];
|
||||||
|
|
||||||
const timestamp = Math.round(Date.now() + (time - currentTime) * 1000);
|
const timestamp = Math.round(Date.now() + getEventOffsetMs(targetTime, currentTime));
|
||||||
|
|
||||||
Object.keys(controls).forEach((key) => {
|
Object.keys(controls).forEach((key) => {
|
||||||
const val = controls[key];
|
const val = controls[key];
|
||||||
|
|
|
||||||
|
|
@ -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 * as _WebMidi from 'webmidi';
|
||||||
import { Pattern, isPattern, logger, ref } from '@strudel/core';
|
import { Pattern, getEventOffsetMs, isPattern, logger, ref } from '@strudel/core';
|
||||||
import { noteToMidi } from '@strudel/core';
|
import { noteToMidi } from '@strudel/core';
|
||||||
import { Note } from 'webmidi';
|
import { Note } from 'webmidi';
|
||||||
// if you use WebMidi from outside of this package, make sure to import that instance:
|
// if you use WebMidi from outside of this package, make sure to import that instance:
|
||||||
|
|
@ -120,10 +120,9 @@ Pattern.prototype.midi = function (output) {
|
||||||
const device = getDevice(output, WebMidi.outputs);
|
const device = getDevice(output, WebMidi.outputs);
|
||||||
hap.ensureObjectValue();
|
hap.ensureObjectValue();
|
||||||
//magic number to get audio engine to line up, can probably be calculated somehow
|
//magic number to get audio engine to line up, can probably be calculated somehow
|
||||||
const latency = 0.034;
|
const latencyMs = 34;
|
||||||
// passing a string with a +num into the webmidi api adds an offset to the current time https://webmidijs.org/api/classes/Output
|
// passing a string with a +num into the webmidi api adds an offset to the current time https://webmidijs.org/api/classes/Output
|
||||||
const timeOffsetString = `+${(targetTime - currentTime + latency) * 1000}`;
|
const timeOffsetString = `+${getEventOffsetMs(targetTime, currentTime) + latencyMs}`;
|
||||||
|
|
||||||
// destructure value
|
// destructure value
|
||||||
let { note, nrpnn, nrpv, ccn, ccv, midichan = 1, midicmd, gain = 1, velocity = 0.9 } = hap.value;
|
let { note, nrpnn, nrpv, ccn, ccv, midichan = 1, midicmd, gain = 1, velocity = 0.9 } = hap.value;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ This program is free software: you can redistribute it and/or modify it under th
|
||||||
|
|
||||||
import OSC from 'osc-js';
|
import OSC from 'osc-js';
|
||||||
|
|
||||||
import { logger, parseNumeral, Pattern } from '@strudel/core';
|
import { logger, parseNumeral, Pattern, getEventOffsetMs } from '@strudel/core';
|
||||||
|
|
||||||
let connection; // Promise<OSC>
|
let connection; // Promise<OSC>
|
||||||
function connect() {
|
function connect() {
|
||||||
|
|
@ -44,7 +44,7 @@ function connect() {
|
||||||
* @returns Pattern
|
* @returns Pattern
|
||||||
*/
|
*/
|
||||||
Pattern.prototype.osc = function () {
|
Pattern.prototype.osc = function () {
|
||||||
return this.onTrigger(async (time, hap, currentTime, cps = 1) => {
|
return this.onTrigger(async (time, hap, currentTime, cps = 1, targetTime) => {
|
||||||
hap.ensureObjectValue();
|
hap.ensureObjectValue();
|
||||||
const osc = await connect();
|
const osc = await connect();
|
||||||
const cycle = hap.wholeOrPart().begin.valueOf();
|
const cycle = hap.wholeOrPart().begin.valueOf();
|
||||||
|
|
@ -56,7 +56,8 @@ Pattern.prototype.osc = function () {
|
||||||
const keyvals = Object.entries(controls).flat();
|
const keyvals = Object.entries(controls).flat();
|
||||||
// time should be audio time of onset
|
// time should be audio time of onset
|
||||||
// currentTime should be current time of audio context (slightly before time)
|
// currentTime should be current time of audio context (slightly before time)
|
||||||
const offset = (time - currentTime) * 1000;
|
const offset = getEventOffsetMs(targetTime, currentTime);
|
||||||
|
|
||||||
// timestamp in milliseconds used to trigger the osc bundle at a precise moment
|
// timestamp in milliseconds used to trigger the osc bundle at a precise moment
|
||||||
const ts = Math.floor(Date.now() + offset);
|
const ts = Math.floor(Date.now() + offset);
|
||||||
const message = new OSC.Message('/dirt/play', ...keyvals);
|
const message = new OSC.Message('/dirt/play', ...keyvals);
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ Copyright (C) 2022 Strudel contributors - see <https://github.com/tidalcycles/st
|
||||||
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const OSC = require('osc-js');
|
import OSC from 'osc-js';
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
receiver: 'ws', // @param {string} Where messages sent via 'send' method will be delivered to, 'ws' for Websocket clients, 'udp' for udp client
|
receiver: 'ws', // @param {string} Where messages sent via 'send' method will be delivered to, 'ws' for Websocket clients, 'udp' for udp client
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
use rosc::{encoder, OscTime};
|
use rosc::{encoder, OscTime};
|
||||||
use rosc::{ OscMessage, OscPacket, OscType, OscBundle };
|
use rosc::{OscBundle, OscMessage, OscPacket, OscType};
|
||||||
|
|
||||||
use std::net::UdpSocket;
|
use std::net::UdpSocket;
|
||||||
|
|
||||||
use std::time::Duration;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use tokio::sync::{ mpsc, Mutex };
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use std::sync::Arc;
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
|
use std::time::Duration;
|
||||||
|
use tokio::sync::{mpsc, Mutex};
|
||||||
|
|
||||||
use crate::loggerbridge::Logger;
|
use crate::loggerbridge::Logger;
|
||||||
pub struct OscMsg {
|
pub struct OscMsg {
|
||||||
|
|
@ -28,9 +28,11 @@ pub fn init(
|
||||||
logger: Logger,
|
logger: Logger,
|
||||||
async_input_receiver: mpsc::Receiver<Vec<OscMsg>>,
|
async_input_receiver: mpsc::Receiver<Vec<OscMsg>>,
|
||||||
mut async_output_receiver: mpsc::Receiver<Vec<OscMsg>>,
|
mut async_output_receiver: mpsc::Receiver<Vec<OscMsg>>,
|
||||||
async_output_transmitter: mpsc::Sender<Vec<OscMsg>>
|
async_output_transmitter: mpsc::Sender<Vec<OscMsg>>,
|
||||||
) {
|
) {
|
||||||
tauri::async_runtime::spawn(async move { async_process_model(async_input_receiver, async_output_transmitter).await });
|
tauri::async_runtime::spawn(async move {
|
||||||
|
async_process_model(async_input_receiver, async_output_transmitter).await
|
||||||
|
});
|
||||||
let message_queue: Arc<Mutex<Vec<OscMsg>>> = Arc::new(Mutex::new(Vec::new()));
|
let message_queue: Arc<Mutex<Vec<OscMsg>>> = Arc::new(Mutex::new(Vec::new()));
|
||||||
/* ...........................................................
|
/* ...........................................................
|
||||||
Listen For incoming messages and add to queue
|
Listen For incoming messages and add to queue
|
||||||
|
|
@ -56,7 +58,8 @@ pub fn init(
|
||||||
let sock = UdpSocket::bind("127.0.0.1:57122").unwrap();
|
let sock = UdpSocket::bind("127.0.0.1:57122").unwrap();
|
||||||
let to_addr = String::from("127.0.0.1:57120");
|
let to_addr = String::from("127.0.0.1:57120");
|
||||||
sock.set_nonblocking(true).unwrap();
|
sock.set_nonblocking(true).unwrap();
|
||||||
sock.connect(to_addr).expect("could not connect to OSC address");
|
sock.connect(to_addr)
|
||||||
|
.expect("could not connect to OSC address");
|
||||||
|
|
||||||
/* ...........................................................
|
/* ...........................................................
|
||||||
Process queued messages
|
Process queued messages
|
||||||
|
|
@ -69,8 +72,10 @@ pub fn init(
|
||||||
let result = sock.send(&message.msg_buf);
|
let result = sock.send(&message.msg_buf);
|
||||||
if result.is_err() {
|
if result.is_err() {
|
||||||
logger.log(
|
logger.log(
|
||||||
format!("OSC Message failed to send, the server might no longer be available"),
|
format!(
|
||||||
"error".to_string()
|
"OSC Message failed to send, the server might no longer be available"
|
||||||
|
),
|
||||||
|
"error".to_string(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -83,7 +88,7 @@ pub fn init(
|
||||||
|
|
||||||
pub async fn async_process_model(
|
pub async fn async_process_model(
|
||||||
mut input_reciever: mpsc::Receiver<Vec<OscMsg>>,
|
mut input_reciever: mpsc::Receiver<Vec<OscMsg>>,
|
||||||
output_transmitter: mpsc::Sender<Vec<OscMsg>>
|
output_transmitter: mpsc::Sender<Vec<OscMsg>>,
|
||||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
while let Some(input) = input_reciever.recv().await {
|
while let Some(input) = input_reciever.recv().await {
|
||||||
let output = input;
|
let output = input;
|
||||||
|
|
@ -108,7 +113,7 @@ pub struct MessageFromJS {
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn sendosc(
|
pub async fn sendosc(
|
||||||
messagesfromjs: Vec<MessageFromJS>,
|
messagesfromjs: Vec<MessageFromJS>,
|
||||||
state: tauri::State<'_, AsyncInputTransmit>
|
state: tauri::State<'_, AsyncInputTransmit>,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let async_proc_input_tx = state.inner.lock().await;
|
let async_proc_input_tx = state.inner.lock().await;
|
||||||
let mut messages_to_process: Vec<OscMsg> = Vec::new();
|
let mut messages_to_process: Vec<OscMsg> = Vec::new();
|
||||||
|
|
@ -123,10 +128,10 @@ pub async fn sendosc(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let duration_since_epoch = Duration::from_millis(m.timestamp) + Duration::new(UNIX_OFFSET, 0);
|
let duration_since_epoch =
|
||||||
|
Duration::from_millis(m.timestamp) + Duration::new(UNIX_OFFSET, 0);
|
||||||
|
|
||||||
let seconds = u32
|
let seconds = u32::try_from(duration_since_epoch.as_secs())
|
||||||
::try_from(duration_since_epoch.as_secs())
|
|
||||||
.map_err(|_| "bit conversion failed for osc message timetag")?;
|
.map_err(|_| "bit conversion failed for osc message timetag")?;
|
||||||
|
|
||||||
let nanos = duration_since_epoch.subsec_nanos() as f64;
|
let nanos = duration_since_epoch.subsec_nanos() as f64;
|
||||||
|
|
@ -153,5 +158,8 @@ pub async fn sendosc(
|
||||||
messages_to_process.push(message_to_process);
|
messages_to_process.push(message_to_process);
|
||||||
}
|
}
|
||||||
|
|
||||||
async_proc_input_tx.send(messages_to_process).await.map_err(|e| e.to_string())
|
async_proc_input_tx
|
||||||
|
.send(messages_to_process)
|
||||||
|
.await
|
||||||
|
.map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue