add encoding for all types except array

This commit is contained in:
2024-12-15 22:51:56 -06:00
parent bbdb193d65
commit a742219efa
3 changed files with 152 additions and 8 deletions
+69 -7
View File
@@ -26,7 +26,7 @@ func stringToOSCBytes(rawString string) []byte {
return []byte(sb.String())
}
func integerToOSCBytes(number int32) []byte {
func int32ToOSCBytes(number int32) []byte {
var buf bytes.Buffer
err := binary.Write(&buf, binary.BigEndian, number)
if err != nil {
@@ -35,7 +35,25 @@ func integerToOSCBytes(number int32) []byte {
return buf.Bytes()
}
func floatToOSCBytes(number float32) []byte {
func int64ToOSCBytes(number int64) []byte {
var buf bytes.Buffer
err := binary.Write(&buf, binary.BigEndian, number)
if err != nil {
panic(err)
}
return buf.Bytes()
}
func float32ToOSCBytes(number float32) []byte {
var buf bytes.Buffer
err := binary.Write(&buf, binary.BigEndian, number)
if err != nil {
panic(err)
}
return buf.Bytes()
}
func float64ToOSCBytes(number float64) []byte {
var buf bytes.Buffer
err := binary.Write(&buf, binary.BigEndian, number)
if err != nil {
@@ -48,7 +66,7 @@ func byteArrayToOSCBytes(bytes []byte) []byte {
oscBytes := []byte{}
bytesSize := len(bytes)
oscBytes = append(oscBytes, integerToOSCBytes(int32(bytesSize))...)
oscBytes = append(oscBytes, int32ToOSCBytes(int32(bytesSize))...)
oscBytes = append(oscBytes, bytes...)
padLength := 4 - (bytesSize % 4)
@@ -75,17 +93,23 @@ func argsToBuffer(args []OSCArg) []byte {
}
case "i":
if value, ok := arg.Value.(int); ok {
argBuffers = append(argBuffers, integerToOSCBytes(int32(value))...)
argBuffers = append(argBuffers, int32ToOSCBytes(int32(value))...)
} else if value, ok := arg.Value.(int32); ok {
argBuffers = append(argBuffers, integerToOSCBytes(value)...)
argBuffers = append(argBuffers, int32ToOSCBytes(value)...)
} else {
fmt.Println("OSC arg had integer type but non-integer value.")
}
case "f":
if value, ok := arg.Value.(float32); ok {
argBuffers = append(argBuffers, floatToOSCBytes(float32(value))...)
argBuffers = append(argBuffers, float32ToOSCBytes(float32(value))...)
} else if value, ok := arg.Value.(float64); ok {
argBuffers = append(argBuffers, floatToOSCBytes(float32(value))...)
argBuffers = append(argBuffers, float32ToOSCBytes(float32(value))...)
} else if value, ok := arg.Value.(int); ok {
argBuffers = append(argBuffers, float32ToOSCBytes(float32(value))...)
} else if value, ok := arg.Value.(int32); ok {
argBuffers = append(argBuffers, float32ToOSCBytes(float32(value))...)
} else if value, ok := arg.Value.(int64); ok {
argBuffers = append(argBuffers, float32ToOSCBytes(float32(value))...)
} else {
fmt.Println("OSC arg had float type but non-float value.")
}
@@ -95,6 +119,44 @@ func argsToBuffer(args []OSCArg) []byte {
} else {
fmt.Println("OSC arg had blob type but non-blob value.")
}
case "T":
argBuffers = append(argBuffers, make([]byte, 0)...)
case "F":
argBuffers = append(argBuffers, make([]byte, 0)...)
case "N":
argBuffers = append(argBuffers, make([]byte, 0)...)
case "I":
argBuffers = append(argBuffers, make([]byte, 0)...)
case "r":
color, ok := arg.Value.(OSCColor)
if ok {
colorBytes := []byte{color.r, color.g, color.b, color.a}
argBuffers = append(argBuffers, colorBytes...)
}
case "h":
if value, ok := arg.Value.(int); ok {
argBuffers = append(argBuffers, int64ToOSCBytes(int64(value))...)
} else if value, ok := arg.Value.(int32); ok {
argBuffers = append(argBuffers, int64ToOSCBytes(int64(value))...)
} else if value, ok := arg.Value.(int64); ok {
argBuffers = append(argBuffers, int64ToOSCBytes(value)...)
} else {
fmt.Println("OSC arg had integer type but non-integer value.")
}
case "d":
if value, ok := arg.Value.(float32); ok {
argBuffers = append(argBuffers, float64ToOSCBytes(float64(value))...)
} else if value, ok := arg.Value.(float64); ok {
argBuffers = append(argBuffers, float64ToOSCBytes(float64(value))...)
} else if value, ok := arg.Value.(int); ok {
argBuffers = append(argBuffers, float64ToOSCBytes(float64(value))...)
} else if value, ok := arg.Value.(int32); ok {
argBuffers = append(argBuffers, float64ToOSCBytes(float64(value))...)
} else if value, ok := arg.Value.(int64); ok {
argBuffers = append(argBuffers, float64ToOSCBytes(float64(value))...)
} else {
fmt.Println("OSC arg had float type but non-float value.")
}
default:
fmt.Printf("unhandled osc type: %s.\n", oscType)
}
+76 -1
View File
@@ -48,9 +48,84 @@ func TestOSCEncoding(t *testing.T) {
},
{
description: "simple address blob arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{OSCArg{Type: "b", Value: []byte{98, 108, 111, 98}}}},
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "b", Value: []byte{98, 108, 111, 98}}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, 4, 98, 108, 111, 98},
},
{
description: "simple address True arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{OSCArg{Type: "T", Value: true}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 84, 0, 0},
},
{
description: "simple address False arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "F", Value: false}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0},
},
{
description: "simple address color arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "r", Value: OSCColor{r: 20, g: 21, b: 22, a: 10}}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10},
},
{
description: "simple address nil arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "N", Value: nil}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0},
},
{
description: "simple address int64 arg",
message: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "h", Value: 281474976710655}}},
expected: []byte{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: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "d", Value: 12.7654763}}},
expected: []byte{
47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6,
},
},
// TODO(jwetzell): get array args working working
// {
// description: "simple address array arg",
// message: OSCMessage{
// Address: "/hello",
// Args: []OSCArg{
// []OSCArg{
// {Type: "d", Value: 12.7654763},
// {Type: "i", Value: 1000},
// },
// },
// },
// expected: []byte{
// 47, 104, 101, 108, 108, 111, 0, 0, 44, 91, 100, 105, 93, 0, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6,
// 0, 0, 3, 232,
// },
// },
{
description: "osc 1.0 spec example 1",
message: OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: 440}}},
expected: []byte{
47, 111, 115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52, 47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0, 44,
102, 0, 0, 67, 220, 0, 0,
},
},
{
description: "osc 1.0 spec example 2",
message: OSCMessage{
Address: "/foo",
Args: []OSCArg{
{Type: "i", Value: 1000},
{Type: "i", Value: -1},
{Type: "s", Value: "hello"},
// thanks IEEE 754
{Type: "f", Value: 1.2339999675750732421875},
{Type: "f", Value: 5.677999973297119140625},
},
},
expected: []byte{
47, 102, 111, 111, 0, 0, 0, 0, 44, 105, 105, 115, 102, 102, 0, 0, 0, 0, 3, 232, 255, 255, 255, 255, 104, 101, 108,
108, 111, 0, 0, 0, 63, 157, 243, 182, 64, 181, 178, 45,
},
},
}
for _, testCase := range testCases {
+7
View File
@@ -9,3 +9,10 @@ type OSCMessage struct {
Address string
Args []OSCArg
}
type OSCColor struct {
r uint8
g uint8
b uint8
a uint8
}