From 52931e879d35e487276cd5ff9874f541d34a954f Mon Sep 17 00:00:00 2001 From: Alex McLean Date: Thu, 23 Oct 2025 16:18:47 +0100 Subject: [PATCH] nearly there --- packages/osc/osc.mjs | 50 ++++++++++++++++++++++-------------------- packages/osc/server.js | 23 +++++++++---------- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/packages/osc/osc.mjs b/packages/osc/osc.mjs index 66b78c14d..d05e6ca4c 100644 --- a/packages/osc/osc.mjs +++ b/packages/osc/osc.mjs @@ -64,38 +64,40 @@ export async function oscTrigger(hap, currentTime, cps = 1, targetTime) { const keyvals = Object.entries(controls).flat(); const ts = collator.calculateTimestamp(currentTime, targetTime) * 1000; const msg = { address: '/dirt/play', args: keyvals, timestamp: ts }; - - if ('oschost' in hap.value) { - msg['host'] = hap.value['oschost']; - } - if ('oscport' in hap.value) { - msg['port'] = hap.value['oscport']; - } ws.send(JSON.stringify(msg)); } export async function superdirtTrigger(hap, currentTime, cps = 1, targetTime) { - const osc = await connect(); + const ws = await connect(); const controls = parseControlsFromHap(hap, cps); + const ts = collator.calculateTimestamp(currentTime, targetTime) * 1000; + + let oschost = '127.0.0.1'; + let oscport = 57120; + let bushost = oschost; + let busport = 57110; + + for (const [k, v] of Object.entries(controls)) { + if (k.startsWith('^')) { + const bus_id = v; + const bus_value = controls[k.substring(1)]; + const msg = JSON.stringify({ + oschost: bushost, + oscport: busport, + address: '/c_set', + args: [bus_id, bus_value], + timestamp: ts, + }); + console.log('bus message', msg); + ws.send(msg); + // So they aren't sent with trigger messages + delete controls[k]; + } + } if (hap.hasOnset) { const keyvals = Object.entries(controls).flat(); - - for (const [k, v] of Object.entries(hap.value)) { - if (k.startsWith('^')) { - const bus_id = v; - const bus_value = v[k.substring(1)]; - const message = new OSC.Message('/c_set', bus_id, bus_value); - - // So they aren't sent with trigger messages - delete controls[k]; - } - } - const ts = Math.round(collator.calculateTimestamp(currentTime, targetTime) * 1000); - const message = new OSC.Message('/dirt/play', ...keyvals); - const bundle = new OSC.Bundle([message], ts); - bundle.timestamp(ts); // workaround for https://github.com/adzialocha/osc-js/issues/60 - osc.send(bundle); + ws.send(JSON.stringify({ oschost, oscport, address: '/dirt/play', args: keyvals, timestamp: ts })); } } diff --git a/packages/osc/server.js b/packages/osc/server.js index ede35e975..b0faf9923 100755 --- a/packages/osc/server.js +++ b/packages/osc/server.js @@ -12,18 +12,13 @@ import { WebSocketServer } from 'ws'; import osc from 'osc'; const WS_PORT = 8080; // WebSocket server port -const OSC_REMOTE_IP = '127.0.0.1'; -const OSC_REMOTE_PORT = 57120; const udpPort = new osc.UDPPort({ localAddress: '0.0.0.0', localPort: 0, - remoteAddress: OSC_REMOTE_IP, - remotePort: OSC_REMOTE_PORT, }); udpPort.open(); -console.log(`[Sending OSC] ${OSC_REMOTE_IP}:${OSC_REMOTE_PORT}`); udpPort.on('error', (e) => { console.log('Error: ', e); @@ -36,23 +31,25 @@ wss.on('connection', (ws) => { console.log('New WebSocket connection'); ws.on('message', (message) => { - let osc_host = '127.0.0.1'; - let osc_port = 57120; + let oschost = '127.0.0.1'; + let oscport = 57120; try { const data = JSON.parse(message); - if ('host' in data) { - osc_host = data['host']; + if ('oschost' in data) { + oschost = data['oschost']; + delete data['oschost']; } - if ('port' in data) { - osc_port = data['port']; + if ('oscport' in data) { + oscport = data['oscport']; + delete data['oscport']; } let msg = { address: data['address'], args: data['args'] }; if ('timestamp' in data) { msg = { timeTag: osc.timeTag(0, data['timestamp']), packets: [msg] }; } - - udpPort.send(msg, osc_host, osc_port); + console.log(msg, oschost, oscport); + udpPort.send(msg, oschost, oscport); } catch (err) { console.error('Error parsing message:', err); }