mirror of
https://github.com/jwetzell/osc-js.git
synced 2026-08-09 17:23:42 +00:00
add T and F osc types
This commit is contained in:
+4
-4
@@ -1,7 +1,7 @@
|
||||
export type OSCType = 's' | 'i' | 'f' | 'b';
|
||||
export type OSCType = 's' | 'i' | 'f' | 'b' | 'T' | 'F';
|
||||
export type OSCArg = {
|
||||
type: OSCType;
|
||||
value: string | number | Buffer;
|
||||
value: string | number | Buffer | boolean;
|
||||
};
|
||||
|
||||
export type OSCMessage = {
|
||||
@@ -10,6 +10,6 @@ export type OSCMessage = {
|
||||
};
|
||||
|
||||
export type OSCTypeConverter = {
|
||||
toBuffer: (value: number | string | Buffer) => Buffer | undefined;
|
||||
fromString: (string: string) => number | string | Buffer;
|
||||
toBuffer: (value: string | number | Buffer | boolean) => Buffer | undefined;
|
||||
fromString: (string: string) => string | number | Buffer | boolean;
|
||||
};
|
||||
|
||||
+19
-24
@@ -1,4 +1,4 @@
|
||||
import { OSCType, OSCArg, OSCMessage, OSCTypeConverter } from './models';
|
||||
import { OSCType, OSCArg, OSCMessage, OSCTypeConverter, OSCPacket } from './models';
|
||||
|
||||
const oscTypeConverterMap: { [key in OSCType]: OSCTypeConverter } = {
|
||||
s: {
|
||||
@@ -51,6 +51,18 @@ const oscTypeConverterMap: { [key in OSCType]: OSCTypeConverter } = {
|
||||
},
|
||||
fromString: (string: string) => Buffer.from(string, 'hex'),
|
||||
},
|
||||
T: {
|
||||
toBuffer: () => {
|
||||
return Buffer.alloc(0)
|
||||
},
|
||||
fromString: (string: string) => true
|
||||
},
|
||||
F: {
|
||||
toBuffer: () => {
|
||||
return Buffer.alloc(0)
|
||||
},
|
||||
fromString: (string: string) => false
|
||||
}
|
||||
};
|
||||
|
||||
function argsToBuffer(args: OSCArg[]) {
|
||||
@@ -75,35 +87,18 @@ function argsToBuffer(args: OSCArg[]) {
|
||||
return Buffer.concat(argBuffers);
|
||||
}
|
||||
|
||||
function toBuffer({ address, args }: OSCMessage) {
|
||||
const addressBuffer = oscTypeConverterMap.s.toBuffer(address);
|
||||
export function messageToBuffer(message: OSCMessage) {
|
||||
|
||||
const addressBuffer = oscTypeConverterMap.s.toBuffer(message.address);
|
||||
if (addressBuffer === undefined) {
|
||||
throw new Error('problem encoding address');
|
||||
}
|
||||
|
||||
const typeString = args.map((arg) => arg.type).join('');
|
||||
const typeString = message.args.map((arg) => arg.type).join('');
|
||||
const typesBuffer = oscTypeConverterMap.s.toBuffer(`,${typeString}`);
|
||||
if (typesBuffer === undefined) {
|
||||
throw new Error('problem encoding types');
|
||||
}
|
||||
const argsBuffer = argsToBuffer(args);
|
||||
const argsBuffer = argsToBuffer(message.args);
|
||||
return Buffer.concat([addressBuffer, typesBuffer, argsBuffer]);
|
||||
}
|
||||
|
||||
function stringArgToTypedArg(rawArg: string, type: OSCType = '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);
|
||||
}
|
||||
|
||||
export default {
|
||||
toBuffer,
|
||||
stringArgToTypedArg,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user