switch sendosc to osc library

This commit is contained in:
2024-10-09 18:46:38 -05:00
parent a5d155ac04
commit e997b784c0
5 changed files with 37 additions and 86 deletions
+1
View File
@@ -22,6 +22,7 @@
},
"license": "MIT",
"dependencies": {
"@jwetzell/osc": "0.2.2",
"commander": "^11.0.0",
"slip": "^1.0.2"
},
+4 -3
View File
@@ -4,7 +4,8 @@ const { Option, program } = require('commander');
const dgram = require('dgram');
const net = require('net');
const slip = require('slip');
const osc = require('./osc');
const osc = require('@jwetzell/osc');
const {argToTypedArg} = require('./utils')
const packageInfo = require('../package.json');
program.name(packageInfo.name);
@@ -25,11 +26,11 @@ program.action((options) => {
return {
type: argType,
value: osc.argToTypedArg(rawArg, argType),
value: argToTypedArg(rawArg, argType),
};
});
let oscMsgBuffer = osc.toBuffer({
let oscMsgBuffer = osc.messageToBuffer({
address,
args: typedArgs,
});
-83
View File
@@ -1,83 +0,0 @@
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,
};
+31
View File
@@ -0,0 +1,31 @@
const oscTypeConverterMap = {
s: {
fromString: (string) => string,
},
f: {
fromString: (string) => Number.parseFloat(string),
},
i: {
fromString: (string) => Number.parseInt(string, 10),
},
b: {
fromString: (string) => Buffer.from(string, 'hex'),
},
};
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 = {
argToTypedArg,
};
+1
View File
@@ -22,6 +22,7 @@
"version": "1.0.1",
"license": "MIT",
"dependencies": {
"@jwetzell/osc": "0.2.2",
"commander": "^11.0.0",
"slip": "^1.0.2"
},