add osc bundle encoding with proper timetag

This commit is contained in:
2024-12-22 14:17:14 -06:00
parent b71ee95364
commit 676644a457
2 changed files with 109 additions and 16 deletions
+77 -2
View File
@@ -1,12 +1,14 @@
package osc
import "encoding/binary"
import (
"errors"
)
func (b *OSCBundle) ToBytes() []byte {
bytes := stringToOSCBytes("#bundle")
bytes = binary.BigEndian.AppendUint64(bytes, b.TimeTag)
bytes = append(bytes, timeTagToOSCBytes(b.TimeTag)...)
for _, packet := range b.Contents {
packetBytes := packet.ToBytes()
@@ -18,3 +20,76 @@ func (b *OSCBundle) ToBytes() []byte {
return bytes
}
func BundleFromBytes(bytes []byte) (OSCBundle, []byte, error) {
if len(bytes) < 20 {
return OSCBundle{}, bytes, errors.New("bundle has to be at least 20 bytes")
}
if bytes[0] != 35 {
return OSCBundle{}, bytes, errors.New("bundle must start with a #")
}
bundleHeader, bytesAfterBundleHeader := readOSCString(bytes)
if bundleHeader != "#bundle" {
return OSCBundle{}, bytesAfterBundleHeader, errors.New("bundle must start with #bundle string")
}
timeTag, bytesAfterTimeTag, err := readOSCTimeTag(bytesAfterBundleHeader)
if err != nil {
return OSCBundle{}, bytesAfterBundleHeader, err
}
bundleContents := []OSCPacket{}
endOfBundle := false
remainingBytes := bytesAfterTimeTag
for !endOfBundle {
contentSize, bytesAfterContentSize, err := readOSCInt32(remainingBytes)
if err != nil {
return OSCBundle{}, remainingBytes, err
}
remainingBytes = bytesAfterContentSize
if len(remainingBytes) < int(contentSize) {
return OSCBundle{}, remainingBytes, errors.New("bundle doesn't have enough bytes for the content size it specifies")
}
bundleContentBytes := remainingBytes[0:contentSize]
if bundleContentBytes[0] == 35 {
content, _, err := BundleFromBytes(bundleContentBytes)
if err != nil {
return OSCBundle{}, remainingBytes, err
}
bundleContents = append(bundleContents, &content)
} else if bundleContentBytes[0] == 47 {
content, err := MessageFromBytes(bundleContentBytes)
if err != nil {
return OSCBundle{}, remainingBytes, err
}
bundleContents = append(bundleContents, &content)
} else {
return OSCBundle{}, remainingBytes, errors.New("bundle contents does not look a bundle or message")
}
remainingBytes = bytesAfterContentSize[contentSize:]
if len(remainingBytes) == 0 {
endOfBundle = true
}
}
return OSCBundle{
TimeTag: timeTag,
Contents: bundleContents,
},
remainingBytes,
nil
}
+32 -14
View File
@@ -10,13 +10,16 @@ func TestOSCBundleEncoding(t *testing.T) {
testCases := []struct {
description string
message OSCBundle
bundle OSCBundle
expected []byte
}{
{
"simple contents single message",
OSCBundle{
TimeTag: 0x0000002000000000,
TimeTag: OSCTimeTag{
seconds: 32,
fractionalSeconds: 0,
},
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,
@@ -29,7 +32,7 @@ func TestOSCBundleEncoding(t *testing.T) {
for _, testCase := range testCases {
actual := testCase.message.ToBytes()
actual := testCase.bundle.ToBytes()
if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to encode properly", testCase.description)
@@ -43,29 +46,44 @@ func TestOSCBundleEncoding(t *testing.T) {
func TestOSCBundleDecoding(t *testing.T) {
testCases := []struct {
description string
expected OSCBundle
bytes []byte
expected OSCMessage
}{}
}{
{
"simple contents single message",
OSCBundle{
TimeTag: OSCTimeTag{
seconds: 32,
fractionalSeconds: 0,
},
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, error := MessageFromBytes(testCase.bytes)
actual, remainingBytes, error := BundleFromBytes(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 len(remainingBytes) > 0 {
t.Errorf("Test '%s' should not have any remaining bytes", testCase.description)
}
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)
if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Test '%s' failed to encode bundle properly", testCase.description)
fmt.Printf("expected: %v\n", testCase.expected)
fmt.Printf("actual: %v\n", actual)
}
}
}