add support for osc nil type

This commit is contained in:
2024-10-13 17:15:02 -05:00
parent f75b663812
commit 4ec6bae5fe
4 changed files with 26 additions and 4 deletions
+4 -4
View File
@@ -1,7 +1,7 @@
export type OSCType = 's' | 'i' | 'f' | 'b' | 'T' | 'F' | 't' | 'r';
export type OSCType = 's' | 'i' | 'f' | 'b' | 'T' | 'F' | 't' | 'r' | 'N';
export type OSCArg = {
type: OSCType;
value: string | number | Uint8Array | boolean | OSCTimeTag | OSCColor;
value: string | number | Uint8Array | boolean | OSCTimeTag | OSCColor | null;
};
export type OSCTimeTag = [number, number];
@@ -24,8 +24,8 @@ export type OSCMessage = {
};
export type OSCTypeConverter = {
toBuffer: (value: string | number | Uint8Array | boolean | OSCTimeTag | OSCColor) => Uint8Array | undefined;
toBuffer: (value: string | number | Uint8Array | boolean | OSCTimeTag | OSCColor | null) => Uint8Array | undefined;
fromBuffer: (
buffer: Uint8Array
) => [string | number | Uint8Array | boolean | OSCTimeTag | OSCColor | undefined, Uint8Array];
) => [string | number | Uint8Array | boolean | OSCTimeTag | OSCColor | null | undefined, Uint8Array];
};
+12
View File
@@ -173,6 +173,10 @@ export const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
},
r: {
toBuffer: (color) => {
if (color === undefined || color === null){
throw new TypeError('osc type r called with undefined or null value');
}
if (typeof color === 'object' && 'r' in color && 'g' in color && 'b' in color && 'a' in color) {
const view = new DataView(new ArrayBuffer(4));
@@ -216,4 +220,12 @@ export const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
throw new Error('problem converting osc ');
},
},
N: {
toBuffer: () => {
return new Uint8Array(0);
},
fromBuffer: (buffer) => {
return [null, buffer];
},
},
};
@@ -43,6 +43,11 @@ const tests = [
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10]),
expected: { address: '/hello', args: [{ type: 'r', value: { r: 20, g: 21, b: 22, a: 10 } }] },
},
{
description: 'simple address nil arg',
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0]),
expected: { address: '/hello', args: [{ type: 'N', value: null }] },
},
{
description: 'osc 1.0 spec example 1',
bytes: new Uint8Array([
@@ -43,6 +43,11 @@ const tests = [
message: { address: '/hello', args: [{ type: 'r', value: { r: 20, g: 21, b: 22, a: 10 } }] },
expected: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10]),
},
{
description: 'simple address nil arg',
message: { address: '/hello', args: [{ type: 'N', value: null }] },
expected: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0]),
},
{
description: 'osc 1.0 spec example 1',
message: { address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] },