mirror of
https://github.com/jwetzell/osc-js.git
synced 2026-08-10 01:33:42 +00:00
switch sendosc to osc library
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
},
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@jwetzell/osc": "0.2.2",
|
||||
"commander": "^11.0.0",
|
||||
"slip": "^1.0.2"
|
||||
},
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
@@ -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,
|
||||
};
|
||||
Generated
+1
@@ -22,6 +22,7 @@
|
||||
"version": "1.0.1",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@jwetzell/osc": "0.2.2",
|
||||
"commander": "^11.0.0",
|
||||
"slip": "^1.0.2"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user