mirror of
https://github.com/jwetzell/osc-go.git
synced 2026-08-26 08:48:57 +00:00
bundle encoding
This commit is contained in:
@@ -0,0 +1,20 @@
|
|||||||
|
package osc
|
||||||
|
|
||||||
|
import "encoding/binary"
|
||||||
|
|
||||||
|
func (b *OSCBundle) ToBytes() []byte {
|
||||||
|
|
||||||
|
bytes := stringToOSCBytes("#bundle")
|
||||||
|
|
||||||
|
bytes = binary.BigEndian.AppendUint64(bytes, b.TimeTag)
|
||||||
|
|
||||||
|
for _, packet := range b.Contents {
|
||||||
|
packetBytes := packet.ToBytes()
|
||||||
|
packetLength := len(packet.ToBytes())
|
||||||
|
|
||||||
|
bytes = append(bytes, int32ToOSCBytes(int32(packetLength))...)
|
||||||
|
bytes = append(bytes, packetBytes...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package osc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestOSCBundleEncoding(t *testing.T) {
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
description string
|
||||||
|
message OSCBundle
|
||||||
|
expected []byte
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"simple contents single message",
|
||||||
|
OSCBundle{
|
||||||
|
TimeTag: 0x0000002000000000,
|
||||||
|
Contents: []OSCPacket{&OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: float32(440)}}}},
|
||||||
|
},
|
||||||
|
[]byte{35, 98, 117, 110, 100, 108, 101, 0, 0, 0, 0,
|
||||||
|
32, 0, 0, 0, 0, 0, 0, 0, 32, 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},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range testCases {
|
||||||
|
|
||||||
|
actual := testCase.message.ToBytes()
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(actual, testCase.expected) {
|
||||||
|
t.Errorf("Test '%s' failed to encode properly", testCase.description)
|
||||||
|
fmt.Printf("expected: %v\n", testCase.expected)
|
||||||
|
fmt.Printf("actual: %v\n", actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOSCBundleDecoding(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
description string
|
||||||
|
bytes []byte
|
||||||
|
expected OSCMessage
|
||||||
|
}{}
|
||||||
|
|
||||||
|
for _, testCase := range testCases {
|
||||||
|
|
||||||
|
actual, error := MessageFromBytes(testCase.bytes)
|
||||||
|
|
||||||
|
if error != nil {
|
||||||
|
fmt.Println(error)
|
||||||
|
t.Errorf("Test '%s' failed to encode properly", testCase.description)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(actual.Address, testCase.expected.Address) {
|
||||||
|
t.Errorf("Test '%s' failed to encode address properly", testCase.description)
|
||||||
|
fmt.Printf("expected: %v\n", testCase.expected.Address)
|
||||||
|
fmt.Printf("actual: %v\n", actual.Address)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(actual.Args, testCase.expected.Args) {
|
||||||
|
t.Errorf("Test '%s' failed to encode args properly", testCase.description)
|
||||||
|
fmt.Printf("expected: %v\n", testCase.expected.Args)
|
||||||
|
fmt.Printf("actual: %v\n", actual.Args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,10 @@ type OSCPacket interface {
|
|||||||
ToBytes() []byte
|
ToBytes() []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OSCBundle struct {
|
||||||
|
TimeTag uint64
|
||||||
|
Contents []OSCPacket
|
||||||
|
}
|
||||||
|
|
||||||
type OSCArg struct {
|
type OSCArg struct {
|
||||||
Type string
|
Type string
|
||||||
|
|||||||
Reference in New Issue
Block a user