add makeosc utility

This commit is contained in:
2024-10-09 18:48:40 -05:00
parent e997b784c0
commit c404299e3e
8 changed files with 220 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
dist
+41
View File
@@ -0,0 +1,41 @@
![npm](https://img.shields.io/npm/v/makeosc)
# makeosc
Simple NodeJS script for generating OSC bytes output to stdout.
## Usage
```
Usage: makeosc [options]
simple util to make osc buffers
Options:
-V, --version output the version number
--address <address> OSC address
--args <args...> osc args (default: [])
--slip slip encode message (default: false)
--types <types...> osc arg types (choices: "s", "i", "f", "b", default: [])
-h, --help display help for command
```
- using npx `npx makeosc@latest --address /hello`
- install using `npm install -g makeosc@latest` and run `makeosc --address /hello`
## Notes
- `--types` option is a space seperate list of type characters that will determine what the corresponding argument type will be set to
- optional
- uses type codes from the OSC spec
- `--args` option is a space-separated list of arguments. If a corresponding type is not found in the `--types` option it will default to string (`s`).
- blobs (`b`) are to be entered as hex string representing the buffer to be sent so the ASCII string `hello` would be `68656c6c6f`
- the default protocol is UDP but can be changed to TCP using the `--protocol` flag
## Examples
- `makeosc --address /test --args 1.0`
- `makeosc --address /this/is/sent/via/tcp`
- `makeosc --address /test/with/types --args 1 2.0 three --types i f`
- note that `types` is not the same length as `args` so the remaining argument (`three`) will default to string
- `makeosc --address /blob --args 68656c6c6f --types b`
+33
View File
@@ -0,0 +1,33 @@
{
"name": "makeosc",
"version": "0.0.1",
"description": "Simple NodeJS utility for generating osc buffers",
"main": "dist/main.js",
"bin": {
"makeosc": "dist/main.js"
},
"scripts": {
"prebuild": "rimraf dist",
"build": "esbuild src/main.js --bundle --platform=node --outfile=dist/main.js",
"prepack": "npm run build"
},
"author": {
"name": "Joel Wetzell",
"email": "me@jwetzell.com",
"url": "https://jwetzell.com"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jwetzell/osc-js.git"
},
"license": "MIT",
"dependencies": {
"@jwetzell/osc": "0.2.2",
"commander": "^11.0.0",
"slip": "^1.0.2"
},
"devDependencies": {
"esbuild": "0.24.0",
"rimraf": "6.0.1"
}
}
+45
View File
@@ -0,0 +1,45 @@
#!/usr/bin/env node
const { Option, program } = require('commander');
const slip = require('slip');
const { argToTypedArg } = require('./utils');
const osc = require('@jwetzell/osc');
const packageInfo = require('../package.json');
program.name(packageInfo.name);
program.version(packageInfo.version);
program.description('simple util to make osc buffers');
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 { address, args, types } = options;
const typedArgs = args?.map((rawArg, index) => {
const argType = types[index] || 's';
return {
type: argType,
value: argToTypedArg(rawArg, argType),
};
});
let oscMsgBuffer = osc.messageToBuffer({
address,
args: typedArgs,
});
if (options.slip) {
oscMsgBuffer = slip.encode(oscMsgBuffer);
}
process.stdout.write(oscMsgBuffer);
});
program.parse();
process.stdout.on('error', (err) => {
if (err.code === 'EPIPE') {
process.exit(0);
}
});
+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,
};