mirror of
https://github.com/jwetzell/osc-js.git
synced 2026-08-23 15:59:01 +00:00
add support for osc h type
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
export type OSCType = 's' | 'i' | 'f' | 'b' | 'T' | 'F' | 't' | 'r' | 'N';
|
export type OSCType = 's' | 'i' | 'f' | 'b' | 'T' | 'F' | 't' | 'r' | 'N';
|
||||||
export type OSCArg = {
|
export type OSCArg = {
|
||||||
type: OSCType;
|
type: OSCType;
|
||||||
value: string | number | Uint8Array | boolean | OSCTimeTag | OSCColor | null;
|
value: string | number | Uint8Array | boolean | OSCTimeTag | OSCColor | bigint | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type OSCTimeTag = [number, number];
|
export type OSCTimeTag = [number, number];
|
||||||
@@ -24,8 +24,10 @@ export type OSCMessage = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type OSCTypeConverter = {
|
export type OSCTypeConverter = {
|
||||||
toBuffer: (value: string | number | Uint8Array | boolean | OSCTimeTag | OSCColor | null) => Uint8Array | undefined;
|
toBuffer: (
|
||||||
|
value: string | number | Uint8Array | boolean | OSCTimeTag | OSCColor | null | bigint
|
||||||
|
) => Uint8Array | undefined;
|
||||||
fromBuffer: (
|
fromBuffer: (
|
||||||
buffer: Uint8Array
|
buffer: Uint8Array
|
||||||
) => [string | number | Uint8Array | boolean | OSCTimeTag | OSCColor | null | undefined, Uint8Array];
|
) => [string | number | Uint8Array | boolean | OSCTimeTag | OSCColor | null | bigint | undefined, Uint8Array];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -236,4 +236,32 @@ export const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
|
|||||||
return [Number.MAX_SAFE_INTEGER, buffer];
|
return [Number.MAX_SAFE_INTEGER, buffer];
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
h: {
|
||||||
|
toBuffer: (number) => {
|
||||||
|
const view = new DataView(new ArrayBuffer(8));
|
||||||
|
|
||||||
|
if (typeof number === 'number') {
|
||||||
|
view.setBigInt64(0, BigInt(number));
|
||||||
|
} else if (typeof number === 'bigint') {
|
||||||
|
view.setBigInt64(0, number);
|
||||||
|
} else {
|
||||||
|
throw new TypeError('osc type h toBuffer called with non number or bigint value');
|
||||||
|
}
|
||||||
|
return new Uint8Array(view.buffer);
|
||||||
|
},
|
||||||
|
fromBuffer: (buffer) => {
|
||||||
|
if (buffer.length < 8) {
|
||||||
|
throw new Error('osc int64 must be at least 8 bytes');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const view = new DataView(new ArrayBuffer(8));
|
||||||
|
buffer.subarray(0,8).forEach((byte, index)=>{
|
||||||
|
view.setUint8(index,byte)
|
||||||
|
})
|
||||||
|
|
||||||
|
return [view.getBigInt64(0), buffer.subarray(8)];
|
||||||
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -53,6 +53,11 @@ const tests = [
|
|||||||
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 73, 0, 0]),
|
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 73, 0, 0]),
|
||||||
expected: { address: '/hello', args: [{ type: 'I', value: Number.MAX_SAFE_INTEGER }] },
|
expected: { address: '/hello', args: [{ type: 'I', value: Number.MAX_SAFE_INTEGER }] },
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: 'simple address int64 arg',
|
||||||
|
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255]),
|
||||||
|
expected: { address: '/hello', args: [{ type: 'h', value: 281474976710655n }] },
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: 'osc 1.0 spec example 1',
|
description: 'osc 1.0 spec example 1',
|
||||||
bytes: new Uint8Array([
|
bytes: new Uint8Array([
|
||||||
|
|||||||
@@ -48,6 +48,11 @@ const tests = [
|
|||||||
message: { address: '/hello', args: [{ type: 'N', value: null }] },
|
message: { address: '/hello', args: [{ type: 'N', value: null }] },
|
||||||
expected: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0]),
|
expected: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0]),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: 'simple address int64 arg',
|
||||||
|
message: { address: '/hello', args: [{ type: 'h', value: 281474976710655n }] },
|
||||||
|
expected: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255]),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: 'osc 1.0 spec example 1',
|
description: 'osc 1.0 spec example 1',
|
||||||
message: { address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] },
|
message: { address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] },
|
||||||
|
|||||||
Reference in New Issue
Block a user