add support for osc d type

This commit is contained in:
2024-10-13 21:45:03 -05:00
parent 1c68227aac
commit e5d559eb1a
3 changed files with 37 additions and 0 deletions
+23
View File
@@ -262,4 +262,27 @@ export const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
return [view.getBigInt64(0), buffer.subarray(8)];
},
},
d: {
toBuffer: (number) => {
if (typeof number === 'number') {
const view = new DataView(new ArrayBuffer(8));
view.setFloat64(0, number);
return new Uint8Array(view.buffer);
}
throw new TypeError('osc type d toBuffer called with non number value');
},
fromBuffer: (buffer) => {
if (buffer.length < 8) {
throw new Error('not enough bytes to read a osc float');
}
const view = new DataView(new ArrayBuffer(8));
buffer.subarray(0, 8).forEach((byte, index) => {
view.setUint8(index, byte);
});
const value = view.getFloat64(0);
return [value, buffer.subarray(8)];
},
},
};
@@ -58,6 +58,13 @@ const tests = [
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: 'simple address float64 arg',
bytes: new Uint8Array([
47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6,
]),
expected: { address: '/hello', args: [{ type: 'd', value: 12.7654763 }] },
},
{
description: 'osc 1.0 spec example 1',
bytes: new Uint8Array([
@@ -53,6 +53,13 @@ const tests = [
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: 'simple address float64 arg',
message: { address: '/hello', args: [{ type: 'd', value: 12.7654763 }] },
expected: new Uint8Array([
47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6,
]),
},
{
description: 'osc 1.0 spec example 1',
message: { address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] },