bring in sendosc and convert to npm workspaces

This commit is contained in:
2024-10-09 17:28:49 -05:00
parent efce70806f
commit 7b681be033
26 changed files with 3572 additions and 162 deletions
+61
View File
@@ -0,0 +1,61 @@
#!/usr/bin/env node
const { Option, program } = require('commander');
const dgram = require('dgram');
const net = require('net');
const slip = require('slip');
const osc = require('./osc');
const packageInfo = require('../package.json');
program.name(packageInfo.name);
program.version(packageInfo.version);
program.description('simple util to sendosc');
program.addOption(new Option('--protocol <protocol>', 'Network protocol').choices(['tcp', 'udp']).default('udp'));
program.addOption(new Option('--host <host>', 'the host to send osc to').makeOptionMandatory());
program.addOption(new Option('--port <port>', 'the port to send osc to').makeOptionMandatory());
program.addOption(new Option('--address <address>', 'OSC address').makeOptionMandatory());
program.addOption(new Option('--args <args...>', 'osc args').default([]));
program.addOption(new Option('--slip', 'slip encode message').default(false));
program.addOption(new Option('--types <types...>', 'osc arg types').choices(['s', 'i', 'f', 'b']).default([]));
program.action((options) => {
const { host, port, address, args, types } = options;
const typedArgs = args?.map((rawArg, index) => {
const argType = types[index] || 's';
return {
type: argType,
value: osc.argToTypedArg(rawArg, argType),
};
});
let oscMsgBuffer = osc.toBuffer({
address,
args: typedArgs,
});
if (options.slip) {
oscMsgBuffer = slip.encode(oscMsgBuffer);
}
if (options.protocol === 'tcp') {
const client = net.Socket();
client.on('error', (error) => {
console.error(error);
});
client.connect(port, host, () => {
client.write(oscMsgBuffer, () => {
client.destroy();
});
});
} else if (options.protocol === 'udp') {
const client = dgram.createSocket('udp4');
client.send(oscMsgBuffer, port, host, (error) => {
if (error) {
console.error(error);
}
client.close();
});
}
});
program.parse();
+83
View File
@@ -0,0 +1,83 @@
const oscTypeConverterMap = {
s: {
toBuffer: (string) => {
let oscString = `${string}\u0000`;
const padSize = 4 - (oscString.length % 4);
if (padSize < 4) {
oscString = oscString.padEnd(oscString.length + padSize, '\u0000');
}
return Buffer.from(oscString, 'ascii');
},
fromString: (string) => string,
},
f: {
toBuffer: (number) => {
const buffer = Buffer.alloc(4);
buffer.writeFloatBE(number);
return buffer;
},
fromString: (string) => Number.parseFloat(string),
},
i: {
toBuffer: (number) => {
const buffer = Buffer.alloc(4);
buffer.writeInt32BE(number);
return buffer;
},
fromString: (string) => Number.parseInt(string, 10),
},
b: {
toBuffer: (data) => {
const sizeBuffer = oscTypeConverterMap.i.toBuffer(data.length);
const padSize = 4 - (data.length % 4);
const padBuffer = Buffer.from(Array(padSize).fill(0));
return Buffer.concat([sizeBuffer, data, padBuffer]);
},
fromString: (string) => Buffer.from(string, 'hex'),
},
};
function argsToBuffer(args) {
const argBuffers = [];
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
const typeConverter = oscTypeConverterMap[arg.type];
if (typeConverter === undefined) {
throw new Error('osc type error: unknown type '.concat(arg.type));
}
if (typeConverter.fromString === undefined) {
throw new Error('osc type error: no string converter for type '.concat(arg.type));
}
argBuffers.push(typeConverter.toBuffer(arg.value));
}
return Buffer.concat(argBuffers);
}
function toBuffer({ address, args }) {
const addressBuffer = oscTypeConverterMap.s.toBuffer(address);
const typeString = args.map((arg) => arg.type).join('');
const typesBuffer = oscTypeConverterMap.s.toBuffer(`,${typeString}`);
const argsBuffer = argsToBuffer(args);
return Buffer.concat([addressBuffer, typesBuffer, argsBuffer]);
}
function argToTypedArg(rawArg, type = 's') {
const typeConverter = oscTypeConverterMap[type];
if (typeConverter === undefined) {
throw new Error('osc type error: unknown type '.concat(type));
}
if (typeConverter.fromString === undefined) {
throw new Error('osc type error: no string converter for type '.concat(type));
}
return typeConverter.fromString(rawArg);
}
module.exports = {
toBuffer,
argToTypedArg,
};