/ Examples / OSC / OSC Out

Right-click to copy examples to your workspace


This example shows how to establish a write-only connection with OSC servers runnin int a different process and potentially on a different computer on your LAN.

Sending OSC is slightly more complex than receiving OSC since you must specify a computer (host) and its OSC port. Here, we originate OSC traffic targeting a remote OSC peers. The example is a toy in the sense that 1. the OSC messages are trivial and 2. we don't anticipate there being OSC servers running on the computers and ports in your LAN. A typical use for OSCOut would be to send Ableton, Max, etc OSC messages targeting objects in their control

For more on Hz's OSC support including a discussion on hostnames and port numbers, see here.

oscout.js

// This script sends dota from Hz to separate
// processes on the desired host(s) listening on specified port. 
// If no process is listening or the host is invalid, the script 
// still runs but has no effect other than network pollution.
// Here we "spawn" three separate fibers and communicate differently
// with each target.

let scene = await Ascene.BeginFiber(this);

let sDefaultCfg = {host:"localhost", port:10000, loops:10, notedur:.25};

async function go(cfg = {})
{
    let lcfg = Object.assign({}, sDefaultCfg, cfg);
    let {host, port, loops, notedur} = lcfg;
    let ipaddress;
    if(host.match(/[a-zA-z]/) && host != "localhost")
    {
        // DNS lookups can take some time
        ipaddress = await OSCUtil.GetHostAddr(host, port);
        console.log(`${host} -> ${ipaddress}`);
    }
    else
        ipaddress = host;
    let oout = new OSCOut(ipaddress, port);
    let tgt = `${host}:${port}`;
    let dur = scene.Seconds(notedur);
    for(let i=0;i<loops;i++)
    {
        oout.StartMsg("/foo/note")
        oout.AddValue(40+i); // the MIDI key
        oout.AddValue(.33 * i); // the velocity
        oout.AddValue(tgt);
        oout.SendMsg();
        console.info(`send ${tgt} ${i}/${loops}`);
        await scene.Wait(dur)
    }
}

let promises = [];
// NB: writing to the broadcast address requires heightened priveleges.
// promises.push(Async.Run(go, {host:"255.255.255.255", notedur:1})); // permission denied
promises.push(Async.Run(go, {host:"limpet.local", notedur:1}));
promises.push(Async.Run(go, {host:"localhost", notedur:.1}));
promises.push(Async.Run(go, {host:"192.168.1.113", notedur:.5}));
await Promise.all(promises);

console.log("done");
home .. topics .. interface .. reference .. examples .. tipjar