Simple utilities for open sound control in node.js
npm install osc-min_simple utilities for open sound control in node.js_
This package provides some node.js utilities for working with
OSC, a format for sound and systems control.
Here we implement the [OSC 1.1][spec11] specification. OSC is a transport-independent
protocol, so we don't provide any server objects, as you should be able to
use OSC over any transport you like. The most common is probably udp, but tcp
is not unheard of.
[spec11]: http://opensoundcontrol.org/spec-1_1
---
Further examples available in the examples folder.
``js
const sock = udp.createSocket("udp4", (msg) => {
try {
console.log(osc.fromBuffer(msg));
} catch (e) {
console.log("invalid OSC packet", e);
}
});
sock.bind(inport);
`
`js
const sendHeartbeat = () => {
const buf = toBuffer({
address: "/heartbeat",
args: [
12,
"sttttring",
new TextEncoder().encode("beat"),
{
type: "integer",
value: 7,
},
],
});
return udp.send(buf, 0, buf.byteLength, outport, "localhost");
};
setInterval(sendHeartbeat, 2000);
`
`js/redirect${address}
const sock = dgram.createSocket("udp4", (msg) => {
try {
const redirected = osc.applyAddressTransform(
msg,
(address) => error redirecting: ${e}
);
return sock.send(
redirected,
0,
redirected.byteLength,
outport,
"localhost"
);
} catch (e) {
return console.log();
}
});
sock.bind(inport);
`
---
See the [spec][spec] for more information on the OSC types.
- An _OSC Packet_ is an _OSC Message_ or an _OSC Bundle_.
- An _OSC Message_:
{
oscType : "message"
address : "/address/pattern/might/have/wildcards"
args : [arg1,arg2]
}
Where args is an array of _OSC Arguments_. oscType is optional.args can be a single element.
- An _OSC Argument_ is represented as a javascript object with the following layout:
{
type : "string"
value : "value"
}
Where the type is one of the following:
- string - string valuefloat
- - numeric valueinteger
- - numeric valuecolor
- - JS object containing red, green, blue, alpha in range 0-255midi
- - four-element array of numbers representing a midi packet of datasymbol
- - string valuecharacter
- - a single-character stringdouble
- - numeric valuebigint
- - 64-bit bigint value (watch out, this will be truncated to 64 bits!)blob
- - ArrayBuffer, DataView, TypedArray or node.js Buffertrue
- - value is boolean truefalse
- - value is boolean falsenull
- - no valuebang
- - no value (this is the I type tag)timetag
- - Javascript Datearray
- - array of _OSC Arguments_
Note that type is always a string - i.e. "true" rather than true.
The following non-standard types are also supported:
- double - numeric value (encodes to a float64 value)
For messages sent to the toBuffer function, type is optional.string
If the argument is not an object, it will be interpreted as either, float, array or blob, depending on its javascript type
(String, Number, Array, Buffer, respectively)
- An _OSC Bundle_ is represented as a javascript object with the following fields:
{
oscType : "bundle"
timetag : 7
elements : [element1, element]
}
oscType "bundle"
timetag is one of:
- Date - a JavaScript Date objectArray
- - [numberOfSecondsSince1900, fractionalSeconds]number
Both values are s. This gives full timing accuracy of 1/(2^32) seconds.
elements is an Array of either _OSC Message_ or _OSC Bundle_
There have been a few breaking changes from the 1.0 version:
- We now provide type declarations for typescript compatibility
- We always enable the previously optional "strict" errors
- Many explicit error messages for passing in data of the wrong type have been removed. We encourage you to use typescript to prevent these sorts of errors.
- Functions that used to return Buffer now return DataViewDate
- TimeTags must be specified as s or [number, number] arrays, and are always returned as [number, number] arrays. To convert between arrays and Dates, use dateToTimetag and timetagToDate.toBuffer` has been removed.
- The two-argument version of
Licensed under the terms found in COPYING (zlib license)