Merge pull request #31 from jwetzell/chore/remove-type-prefix

remove OSC prefix from types
This commit is contained in:
Joel Wetzell
2026-09-10 08:35:01 -05:00
committed by GitHub
10 changed files with 165 additions and 165 deletions
+4 -4
View File
@@ -4,7 +4,7 @@ import (
"errors"
)
func (b *OSCBundle) ToBytes() ([]byte, error) {
func (b *Bundle) ToBytes() ([]byte, error) {
bytes := stringToOSCBytes("#bundle")
@@ -26,7 +26,7 @@ func (b *OSCBundle) ToBytes() ([]byte, error) {
return bytes, nil
}
func BundleFromBytes(bytes []byte) (*OSCBundle, []byte, error) {
func BundleFromBytes(bytes []byte) (*Bundle, []byte, error) {
if len(bytes) < 20 {
return nil, bytes, errors.New("OSC Bundle has to be at least 20 bytes")
}
@@ -47,7 +47,7 @@ func BundleFromBytes(bytes []byte) (*OSCBundle, []byte, error) {
timeTag, bytesAfterTimeTag, _ := readOSCTimeTag(bytesAfterBundleHeader)
bundleContents := []OSCPacket{}
bundleContents := []Packet{}
remainingBytes := bytesAfterTimeTag
@@ -83,7 +83,7 @@ func BundleFromBytes(bytes []byte) (*OSCBundle, []byte, error) {
}
remainingBytes = bytesAfterContentSize[contentSize:]
return &OSCBundle{
return &Bundle{
TimeTag: timeTag,
Contents: bundleContents,
},
+17 -17
View File
@@ -9,17 +9,17 @@ func TestGoodOSCBundleEncoding(t *testing.T) {
testCases := []struct {
name string
bundle *OSCBundle
bundle *Bundle
expected []byte
}{
{
name: "simple contents single message",
bundle: &OSCBundle{
TimeTag: OSCTimeTag{
bundle: &Bundle{
TimeTag: TimeTag{
seconds: 32,
fractionalSeconds: 0,
},
Contents: []OSCPacket{&OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: float32(440)}}}},
Contents: []Packet{&Message{Address: "/oscillator/4/frequency", Args: []Arg{{Type: "f", Value: float32(440)}}}},
},
expected: []byte{35, 98, 117, 110, 100, 108, 101, 0, 0, 0, 0,
32, 0, 0, 0, 0, 0, 0, 0, 32, 47, 111,
@@ -49,17 +49,17 @@ func TestBadOSCBundleEncoding(t *testing.T) {
testCases := []struct {
name string
bundle *OSCBundle
bundle *Bundle
errorString string
}{
{
name: "bundle contains message with bad address",
bundle: &OSCBundle{
TimeTag: OSCTimeTag{
bundle: &Bundle{
TimeTag: TimeTag{
seconds: 32,
fractionalSeconds: 0,
},
Contents: []OSCPacket{&OSCMessage{Address: "hello", Args: []OSCArg{}}},
Contents: []Packet{&Message{Address: "hello", Args: []Arg{}}},
},
errorString: "OSC Message address must start with /",
},
@@ -84,17 +84,17 @@ func TestBadOSCBundleEncoding(t *testing.T) {
func TestGoodOSCBundleDecoding(t *testing.T) {
testCases := []struct {
name string
expected *OSCBundle
expected *Bundle
bytes []byte
}{
{
name: "simple contents single message",
expected: &OSCBundle{
TimeTag: OSCTimeTag{
expected: &Bundle{
TimeTag: TimeTag{
seconds: 32,
fractionalSeconds: 0,
},
Contents: []OSCPacket{&OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: float32(440)}}}},
Contents: []Packet{&Message{Address: "/oscillator/4/frequency", Args: []Arg{{Type: "f", Value: float32(440)}}}},
},
bytes: []byte{35, 98, 117, 110, 100, 108, 101, 0, 0, 0, 0,
32, 0, 0, 0, 0, 0, 0, 0, 32, 47, 111,
@@ -104,17 +104,17 @@ func TestGoodOSCBundleDecoding(t *testing.T) {
},
{
name: "simple contents nested bundle",
expected: &OSCBundle{
TimeTag: OSCTimeTag{
expected: &Bundle{
TimeTag: TimeTag{
seconds: 32,
fractionalSeconds: 0,
},
Contents: []OSCPacket{&OSCBundle{
TimeTag: OSCTimeTag{
Contents: []Packet{&Bundle{
TimeTag: TimeTag{
seconds: 64,
fractionalSeconds: 0,
},
Contents: []OSCPacket{&OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: float32(440)}}}},
Contents: []Packet{&Message{Address: "/oscillator/4/frequency", Args: []Arg{{Type: "f", Value: float32(440)}}}},
}},
},
bytes: []byte{35, 98, 117, 110, 100, 108, 101, 0, // #bundle
+13 -13
View File
@@ -52,11 +52,11 @@ func main() {
}
}
func argToTypedArg(rawArg string, oscType string) osc.OSCArg {
func argToTypedArg(rawArg string, oscType string) osc.Arg {
switch oscType {
case "s":
return osc.OSCArg{
return osc.Arg{
Value: rawArg,
Type: "s",
}
@@ -66,7 +66,7 @@ func argToTypedArg(rawArg string, oscType string) osc.OSCArg {
// ... handle error
panic(err)
}
return osc.OSCArg{
return osc.Arg{
Value: int32(number),
Type: "i",
}
@@ -76,7 +76,7 @@ func argToTypedArg(rawArg string, oscType string) osc.OSCArg {
// ... handle error
panic(err)
}
return osc.OSCArg{
return osc.Arg{
Value: float32(number),
Type: "f",
}
@@ -86,7 +86,7 @@ func argToTypedArg(rawArg string, oscType string) osc.OSCArg {
// ... handle error
panic(err)
}
return osc.OSCArg{
return osc.Arg{
Value: data,
Type: "b",
}
@@ -96,7 +96,7 @@ func argToTypedArg(rawArg string, oscType string) osc.OSCArg {
// ... handle error
panic(err)
}
return osc.OSCArg{
return osc.Arg{
Value: int64(number),
Type: "h",
}
@@ -106,29 +106,29 @@ func argToTypedArg(rawArg string, oscType string) osc.OSCArg {
// ... handle error
panic(err)
}
return osc.OSCArg{
return osc.Arg{
Value: float64(number),
Type: "d",
}
case "T":
return osc.OSCArg{
return osc.Arg{
Value: true,
Type: "T",
}
case "F":
return osc.OSCArg{
return osc.Arg{
Value: false,
Type: "F",
}
case "N":
return osc.OSCArg{
return osc.Arg{
Value: nil,
Type: "N",
}
default:
fmt.Printf("unsupported OSC arg type: %s\n", oscType)
// TODO(jwetzell): something better than this like actual nil, err thing
return osc.OSCArg{}
return osc.Arg{}
}
}
@@ -157,9 +157,9 @@ func slipEncode(bytes []byte) []byte {
func makeMsg(address string, args []string, types []string, slip bool) {
oscMessage := osc.OSCMessage{
oscMessage := osc.Message{
Address: address,
Args: []osc.OSCArg{},
Args: []osc.Arg{},
}
for index, arg := range args {
+7 -7
View File
@@ -102,7 +102,7 @@ func listenTCP(netAddress string, useSLIP bool, format string) {
type SLIP struct {
pendingBytes []byte
Packets chan osc.OSCPacket
Packets chan osc.Packet
}
func (s *SLIP) decode(bytes []byte) {
@@ -155,7 +155,7 @@ func handleSLIP(slip SLIP, format string) {
func handleTCPConnection(conn net.Conn, useSLIP bool, format string) {
slip := SLIP{
pendingBytes: []byte{},
Packets: make(chan osc.OSCPacket),
Packets: make(chan osc.Packet),
}
go handleSLIP(slip, format)
@@ -177,17 +177,17 @@ func handleTCPConnection(conn net.Conn, useSLIP bool, format string) {
}
}
func handlePacket(message osc.OSCPacket, format string) {
if bundle, ok := message.(*osc.OSCBundle); ok {
func handlePacket(message osc.Packet, format string) {
if bundle, ok := message.(*osc.Bundle); ok {
handleBundle(bundle, format)
} else if msg, ok := message.(*osc.OSCMessage); ok {
} else if msg, ok := message.(*osc.Message); ok {
handleMessage(msg, format)
} else {
fmt.Println("Received unknown OSC Packet type")
}
}
func handleMessage(message *osc.OSCMessage, format string) {
func handleMessage(message *osc.Message, format string) {
if format == "json" {
jsonData, _ := json.Marshal(message)
fmt.Println(string(jsonData))
@@ -196,7 +196,7 @@ func handleMessage(message *osc.OSCMessage, format string) {
}
}
func handleBundle(bundle *osc.OSCBundle, format string) {
func handleBundle(bundle *osc.Bundle, format string) {
for _, packet := range bundle.Contents {
handlePacket(packet, format)
}
+13 -13
View File
@@ -80,11 +80,11 @@ func main() {
}
}
func argToTypedArg(rawArg string, oscType string) osc.OSCArg {
func argToTypedArg(rawArg string, oscType string) osc.Arg {
switch oscType {
case "s":
return osc.OSCArg{
return osc.Arg{
Value: rawArg,
Type: "s",
}
@@ -94,7 +94,7 @@ func argToTypedArg(rawArg string, oscType string) osc.OSCArg {
// ... handle error
panic(err)
}
return osc.OSCArg{
return osc.Arg{
Value: int32(number),
Type: "i",
}
@@ -104,7 +104,7 @@ func argToTypedArg(rawArg string, oscType string) osc.OSCArg {
// ... handle error
panic(err)
}
return osc.OSCArg{
return osc.Arg{
Value: float32(number),
Type: "f",
}
@@ -114,7 +114,7 @@ func argToTypedArg(rawArg string, oscType string) osc.OSCArg {
// ... handle error
panic(err)
}
return osc.OSCArg{
return osc.Arg{
Value: data,
Type: "b",
}
@@ -124,7 +124,7 @@ func argToTypedArg(rawArg string, oscType string) osc.OSCArg {
// ... handle error
panic(err)
}
return osc.OSCArg{
return osc.Arg{
Value: int64(number),
Type: "h",
}
@@ -134,29 +134,29 @@ func argToTypedArg(rawArg string, oscType string) osc.OSCArg {
// ... handle error
panic(err)
}
return osc.OSCArg{
return osc.Arg{
Value: float64(number),
Type: "d",
}
case "T":
return osc.OSCArg{
return osc.Arg{
Value: true,
Type: "T",
}
case "F":
return osc.OSCArg{
return osc.Arg{
Value: false,
Type: "F",
}
case "N":
return osc.OSCArg{
return osc.Arg{
Value: nil,
Type: "N",
}
default:
fmt.Printf("unsupported OSC arg type: %s\n", oscType)
// TODO(jwetzell): something better than this like actual nil, err thing
return osc.OSCArg{}
return osc.Arg{}
}
}
@@ -185,9 +185,9 @@ func slipEncode(bytes []byte) []byte {
func send(host string, port int32, address string, args []string, types []string, protocol string, slip bool) {
oscMessage := osc.OSCMessage{
oscMessage := osc.Message{
Address: address,
Args: []osc.OSCArg{},
Args: []osc.Arg{},
}
for index, arg := range args {
+4 -4
View File
@@ -5,7 +5,7 @@ import (
"strings"
)
func (m *OSCMessage) ToBytes() ([]byte, error) {
func (m *Message) ToBytes() ([]byte, error) {
if len(m.Address) == 0 {
return nil, errors.New("OSC Message must have an address")
@@ -36,7 +36,7 @@ func (m *OSCMessage) ToBytes() ([]byte, error) {
return oscBuffer, nil
}
func MessageFromBytes(bytes []byte) (*OSCMessage, error) {
func MessageFromBytes(bytes []byte) (*Message, error) {
if len(bytes) == 0 {
return nil, errors.New("cannot create OSC Message from empty byte array")
}
@@ -50,9 +50,9 @@ func MessageFromBytes(bytes []byte) (*OSCMessage, error) {
return nil, err
}
oscMessage := OSCMessage{
oscMessage := Message{
Address: address,
Args: []OSCArg{},
Args: []Arg{},
}
if len(typeAndArgBytes) == 0 {
+56 -56
View File
@@ -10,22 +10,22 @@ func TestGoodOSCMessageEncoding(t *testing.T) {
testCases := []struct {
name string
message *OSCMessage
message *Message
expected []byte
}{
{
name: "simple hello",
message: &OSCMessage{
message: &Message{
Address: "/hello",
Args: []OSCArg{},
Args: []Arg{},
},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 0, 0, 0},
},
{
name: "simple address string arg",
message: &OSCMessage{
message: &Message{
Address: "/hello",
Args: []OSCArg{
Args: []Arg{
{
Type: "s",
Value: "arg1",
@@ -36,47 +36,47 @@ func TestGoodOSCMessageEncoding(t *testing.T) {
},
{
name: "simple address integer arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "i", Value: 35}}},
message: &Message{Address: "/hello", Args: []Arg{{Type: "i", Value: 35}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35},
},
{
name: "simple address float arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "f", Value: 34.5}}},
message: &Message{Address: "/hello", Args: []Arg{{Type: "f", Value: 34.5}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 102, 0, 0, 66, 10, 0, 0},
},
{
name: "simple address blob arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "b", Value: []byte{98, 108, 111, 98}}}},
message: &Message{Address: "/hello", Args: []Arg{{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},
},
{
name: "simple address True arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "T", Value: true}}},
message: &Message{Address: "/hello", Args: []Arg{{Type: "T", Value: true}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 84, 0, 0},
},
{
name: "simple address False arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "F", Value: false}}},
message: &Message{Address: "/hello", Args: []Arg{{Type: "F", Value: false}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0},
},
{
name: "simple address color arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "r", Value: OSCColor{r: 20, g: 21, b: 22, a: 10}}}},
message: &Message{Address: "/hello", Args: []Arg{{Type: "r", Value: Color{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},
},
{
name: "simple address nil arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "N", Value: nil}}},
message: &Message{Address: "/hello", Args: []Arg{{Type: "N", Value: nil}}},
expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0},
},
{
name: "simple address int64 arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "h", Value: 281474976710655}}},
message: &Message{Address: "/hello", Args: []Arg{{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},
},
{
name: "simple address float64 arg",
message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "d", Value: 12.7654763}}},
message: &Message{Address: "/hello", Args: []Arg{{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,
},
@@ -100,7 +100,7 @@ func TestGoodOSCMessageEncoding(t *testing.T) {
// },
{
name: "osc 1.0 spec example 1",
message: &OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: 440}}},
message: &Message{Address: "/oscillator/4/frequency", Args: []Arg{{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,
@@ -108,9 +108,9 @@ func TestGoodOSCMessageEncoding(t *testing.T) {
},
{
name: "osc 1.0 spec example 2",
message: &OSCMessage{
message: &Message{
Address: "/foo",
Args: []OSCArg{
Args: []Arg{
{Type: "i", Value: 1000},
{Type: "i", Value: -1},
{Type: "s", Value: "hello"},
@@ -145,80 +145,80 @@ func TestGoodOSCMessageEncoding(t *testing.T) {
func TestBadOSCMessageEncoding(t *testing.T) {
testCases := []struct {
name string
message *OSCMessage
message *Message
errorString string
}{
{
name: "empty message",
message: &OSCMessage{},
message: &Message{},
errorString: "OSC Message must have an address",
},
{
name: "address does not start with /",
message: &OSCMessage{Address: "hello"},
message: &Message{Address: "hello"},
errorString: "OSC Message address must start with /",
},
{
name: "arg with unsupported type",
message: &OSCMessage{
message: &Message{
Address: "/hello",
Args: []OSCArg{{Type: "x", Value: "unsupported"}},
Args: []Arg{{Type: "x", Value: "unsupported"}},
},
errorString: "unsupported OSC argument type: x",
},
{
name: "string arg that is not a string",
message: &OSCMessage{
message: &Message{
Address: "/hello",
Args: []OSCArg{{Type: "s", Value: 123}},
Args: []Arg{{Type: "s", Value: 123}},
},
errorString: "OSC arg had string type but non-string value",
},
{
name: "int32 arg that is not a number",
message: &OSCMessage{
message: &Message{
Address: "/hello",
Args: []OSCArg{{Type: "i", Value: "not an int"}},
Args: []Arg{{Type: "i", Value: "not an int"}},
},
errorString: "OSC arg had int32 type but non-number value",
},
{
name: "float32 arg that is not a number",
message: &OSCMessage{
message: &Message{
Address: "/hello",
Args: []OSCArg{{Type: "f", Value: "not a float"}},
Args: []Arg{{Type: "f", Value: "not a float"}},
},
errorString: "OSC arg had float32 type but non-number value",
},
{
name: "int64 arg that is not a number",
message: &OSCMessage{
message: &Message{
Address: "/hello",
Args: []OSCArg{{Type: "h", Value: "not an int"}},
Args: []Arg{{Type: "h", Value: "not an int"}},
},
errorString: "OSC arg had int64 type but non-number value",
},
{
name: "float64 arg that is not a number",
message: &OSCMessage{
message: &Message{
Address: "/hello",
Args: []OSCArg{{Type: "d", Value: "not a float"}},
Args: []Arg{{Type: "d", Value: "not a float"}},
},
errorString: "OSC arg had float64 type but non-number value",
},
{
name: "blob arg that is not a byte array",
message: &OSCMessage{
message: &Message{
Address: "/hello",
Args: []OSCArg{{Type: "b", Value: "not a blob"}},
Args: []Arg{{Type: "b", Value: "not a blob"}},
},
errorString: "OSC arg had blob type but non-blob value",
},
{
name: "color arg that is not an OSCColor",
message: &OSCMessage{
message: &Message{
Address: "/hello",
Args: []OSCArg{{Type: "r", Value: "not a color"}},
Args: []Arg{{Type: "r", Value: "not a color"}},
},
errorString: "OSC arg had color type but non-color value",
},
@@ -243,69 +243,69 @@ func TestGoodOSCMessageDecoding(t *testing.T) {
testCases := []struct {
name string
bytes []byte
expected OSCMessage
expected Message
}{
{
name: "simple address no args",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 0, 0, 0},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{}},
expected: Message{Address: "/hello", Args: []Arg{}},
},
{
name: "simple address string arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 115, 0, 0, 97, 114, 103, 49, 0, 0, 0, 0},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "s", Value: "arg1"}}},
expected: Message{Address: "/hello", Args: []Arg{{Type: "s", Value: "arg1"}}},
},
{
name: "simple address integer arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "i", Value: int32(35)}}},
expected: Message{Address: "/hello", Args: []Arg{{Type: "i", Value: int32(35)}}},
},
{
name: "simple address float arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 102, 0, 0, 66, 10, 0, 0},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "f", Value: float32(34.5)}}},
expected: Message{Address: "/hello", Args: []Arg{{Type: "f", Value: float32(34.5)}}},
},
{
name: "simple address blob arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, 4, 98, 108, 111, 98},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "b", Value: []byte{98, 108, 111, 98}}}},
expected: Message{Address: "/hello", Args: []Arg{{Type: "b", Value: []byte{98, 108, 111, 98}}}},
},
{
name: "simple address True arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 84, 0, 0},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "T", Value: true}}},
expected: Message{Address: "/hello", Args: []Arg{{Type: "T", Value: true}}},
},
{
name: "simple address False arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "F", Value: false}}},
expected: Message{Address: "/hello", Args: []Arg{{Type: "F", Value: false}}},
},
{
name: "simple address color arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "r", Value: OSCColor{r: 20, g: 21, b: 22, a: 10}}}},
expected: Message{Address: "/hello", Args: []Arg{{Type: "r", Value: Color{r: 20, g: 21, b: 22, a: 10}}}},
},
{
name: "simple address nil arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "N", Value: nil}}},
expected: Message{Address: "/hello", Args: []Arg{{Type: "N", Value: nil}}},
},
{
name: "simple address Inifinitum arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 73, 0, 0},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "I", Value: math.MaxInt32}}},
expected: Message{Address: "/hello", Args: []Arg{{Type: "I", Value: math.MaxInt32}}},
},
{
name: "simple address int64 arg",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "h", Value: int64(281474976710655)}}},
expected: Message{Address: "/hello", Args: []Arg{{Type: "h", Value: int64(281474976710655)}}},
},
{
name: "simple address float64 arg",
bytes: []byte{
47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6,
},
expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "d", Value: float64(12.7654763)}}},
expected: Message{Address: "/hello", Args: []Arg{{Type: "d", Value: float64(12.7654763)}}},
},
// TODO(jwetzell): support OSC array
// {
@@ -327,9 +327,9 @@ func TestGoodOSCMessageDecoding(t *testing.T) {
{
name: "simple address no type string",
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0},
expected: OSCMessage{
expected: Message{
Address: "/hello",
Args: []OSCArg{},
Args: []Arg{},
},
},
{
@@ -338,7 +338,7 @@ func TestGoodOSCMessageDecoding(t *testing.T) {
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,
},
expected: OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: float32(440)}}},
expected: Message{Address: "/oscillator/4/frequency", Args: []Arg{{Type: "f", Value: float32(440)}}},
},
{
name: "osc 1.0 spec example 2",
@@ -346,9 +346,9 @@ func TestGoodOSCMessageDecoding(t *testing.T) {
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,
},
expected: OSCMessage{
expected: Message{
Address: "/foo",
Args: []OSCArg{
Args: []Arg{
{Type: "i", Value: int32(1000)},
{Type: "i", Value: int32(-1)},
{Type: "s", Value: "hello"},
@@ -513,9 +513,9 @@ func TestBadOSCMessageDecoding(t *testing.T) {
}
func BenchmarkMessageToBytes(b *testing.B) {
message := &OSCMessage{
message := &Message{
Address: "/hello",
Args: []OSCArg{
Args: []Arg{
{Type: "i", Value: 35},
},
}
+15 -15
View File
@@ -77,7 +77,7 @@ func byteArrayToOSCBytes(bytes []byte) []byte {
return oscBytes
}
func timeTagToOSCBytes(timeTag OSCTimeTag) []byte {
func timeTagToOSCBytes(timeTag TimeTag) []byte {
timeTagBytes := int32ToOSCBytes(timeTag.seconds)
fractionalSecondsBytes := int32ToOSCBytes(timeTag.fractionalSeconds)
timeTagBytes = append(timeTagBytes, fractionalSecondsBytes...)
@@ -85,7 +85,7 @@ func timeTagToOSCBytes(timeTag OSCTimeTag) []byte {
return timeTagBytes
}
func argsToBuffer(args []OSCArg) ([]byte, error) {
func argsToBuffer(args []Arg) ([]byte, error) {
//TODO(jwetzell): add error handling
var argBuffers = []byte{}
@@ -142,7 +142,7 @@ func argsToBuffer(args []OSCArg) ([]byte, error) {
case "I":
argBuffers = append(argBuffers, make([]byte, 0)...)
case "r":
color, ok := arg.Value.(OSCColor)
color, ok := arg.Value.(Color)
if !ok {
return nil, errors.New("OSC arg had color type but non-color value")
}
@@ -281,11 +281,11 @@ func readOSCBlob(bytes []byte) ([]byte, []byte, error) {
return bytes[4 : 4+blobLength], bytes[blobEnd:], nil
}
func readOSCColor(bytes []byte) (OSCColor, []byte, error) {
func readOSCColor(bytes []byte) (Color, []byte, error) {
if len(bytes) < 4 {
return OSCColor{0, 0, 0, 0}, bytes, errors.New("OSC color arg is not 4 bytes")
return Color{0, 0, 0, 0}, bytes, errors.New("OSC color arg is not 4 bytes")
}
oscColor := OSCColor{
oscColor := Color{
r: bytes[0],
g: bytes[1],
b: bytes[2],
@@ -294,17 +294,17 @@ func readOSCColor(bytes []byte) (OSCColor, []byte, error) {
return oscColor, bytes[4:], nil
}
func readOSCTimeTag(bytes []byte) (OSCTimeTag, []byte, error) {
func readOSCTimeTag(bytes []byte) (TimeTag, []byte, error) {
seconds, bytesAfterSeconds, err := readOSCInt32(bytes)
if err != nil {
return OSCTimeTag{}, bytes, fmt.Errorf("OSC time tag seconds are not valid: %s", err)
return TimeTag{}, bytes, fmt.Errorf("OSC time tag seconds are not valid: %s", err)
}
fractionalSeconds, remainingBytes, err := readOSCInt32(bytesAfterSeconds)
if err != nil {
return OSCTimeTag{}, bytes, fmt.Errorf("OSC time tag fractional seconds are not valid: %s", err)
return TimeTag{}, bytes, fmt.Errorf("OSC time tag fractional seconds are not valid: %s", err)
}
return OSCTimeTag{
return TimeTag{
seconds: seconds,
fractionalSeconds: fractionalSeconds,
},
@@ -312,10 +312,10 @@ func readOSCTimeTag(bytes []byte) (OSCTimeTag, []byte, error) {
nil
}
func readOSCArg(bytes []byte, oscType string) (OSCArg, []byte, error) {
func readOSCArg(bytes []byte, oscType string) (Arg, []byte, error) {
var readArgError error
oscArg := OSCArg{}
oscArg := Arg{}
oscArg.Type = oscType
var remainingBytes []byte
@@ -324,7 +324,7 @@ func readOSCArg(bytes []byte, oscType string) (OSCArg, []byte, error) {
case "s":
argString, bytesLeft, err := readOSCString(bytes)
if err != nil {
return OSCArg{}, bytes, err
return Arg{}, bytes, err
}
oscArg.Value = argString
remainingBytes = bytesLeft
@@ -390,12 +390,12 @@ func readOSCArg(bytes []byte, oscType string) (OSCArg, []byte, error) {
oscArg.Value = argTimeTag
remainingBytes = bytesLeft
default:
return OSCArg{}, bytes, fmt.Errorf("unsupported OSC argument type: %s", oscType)
return Arg{}, bytes, fmt.Errorf("unsupported OSC argument type: %s", oscType)
}
return oscArg, remainingBytes, readArgError
}
func PacketFromBytes(bytes []byte) (OSCPacket, []byte, error) {
func PacketFromBytes(bytes []byte) (Packet, []byte, error) {
if len(bytes) == 0 {
return nil, bytes, errors.New("cannot create OSC Packet from empty byte array")
}
+26 -26
View File
@@ -9,12 +9,12 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
testCases := []struct {
name string
args []OSCArg
args []Arg
expected []byte
}{
{
name: "int arg",
args: []OSCArg{
args: []Arg{
{
Type: "i",
Value: int(123),
@@ -24,7 +24,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "int32 arg",
args: []OSCArg{
args: []Arg{
{
Type: "i",
Value: int32(123),
@@ -34,7 +34,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "float32 arg",
args: []OSCArg{
args: []Arg{
{
Type: "f",
Value: float32(123),
@@ -44,7 +44,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "float32 arg with int value",
args: []OSCArg{
args: []Arg{
{
Type: "f",
Value: int(123),
@@ -54,7 +54,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "float32 arg with int32 value",
args: []OSCArg{
args: []Arg{
{
Type: "f",
Value: int32(123),
@@ -64,7 +64,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "float32 arg with int64 value",
args: []OSCArg{
args: []Arg{
{
Type: "f",
Value: int64(123),
@@ -74,7 +74,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "float64 arg",
args: []OSCArg{
args: []Arg{
{
Type: "d",
Value: float64(123),
@@ -84,7 +84,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "float64 arg with float32 value",
args: []OSCArg{
args: []Arg{
{
Type: "d",
Value: float32(123),
@@ -94,7 +94,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "float64 arg with int value",
args: []OSCArg{
args: []Arg{
{
Type: "d",
Value: int(123),
@@ -104,7 +104,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "float64 arg with int32 value",
args: []OSCArg{
args: []Arg{
{
Type: "d",
Value: int32(123),
@@ -114,7 +114,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "float64 arg with int64 value",
args: []OSCArg{
args: []Arg{
{
Type: "d",
Value: int64(123),
@@ -124,7 +124,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "int64 arg",
args: []OSCArg{
args: []Arg{
{
Type: "h",
Value: int64(123),
@@ -134,7 +134,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "int64 arg with int32 value",
args: []OSCArg{
args: []Arg{
{
Type: "h",
Value: int32(123),
@@ -144,7 +144,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "blob arg",
args: []OSCArg{
args: []Arg{
{
Type: "b",
Value: []byte{1, 2, 3},
@@ -154,7 +154,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "true arg",
args: []OSCArg{
args: []Arg{
{
Type: "T",
Value: true,
@@ -164,7 +164,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "false arg",
args: []OSCArg{
args: []Arg{
{
Type: "F",
Value: false,
@@ -174,7 +174,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "nil arg",
args: []OSCArg{
args: []Arg{
{
Type: "N",
Value: nil,
@@ -184,7 +184,7 @@ func TestGoodOSCArgsToBuffer(t *testing.T) {
},
{
name: "inifinitum arg",
args: []OSCArg{
args: []Arg{
{
Type: "I",
Value: nil,
@@ -214,7 +214,7 @@ func TestBadOSCArgsToBuffer(t *testing.T) {
testCases := []struct {
name string
args []OSCArg
args []Arg
errorString string
}{}
@@ -238,25 +238,25 @@ func TestGoodPacketFromBytes(t *testing.T) {
testCases := []struct {
name string
expected OSCPacket
expected Packet
bytes []byte
}{
{
name: "message with no args",
expected: &OSCMessage{
expected: &Message{
Address: "/hello",
Args: []OSCArg{},
Args: []Arg{},
},
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0},
},
{
name: "bundle with one message with no args",
expected: &OSCBundle{
TimeTag: OSCTimeTag{
expected: &Bundle{
TimeTag: TimeTag{
seconds: 32,
fractionalSeconds: 0,
},
Contents: []OSCPacket{&OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: float32(440)}}}},
Contents: []Packet{&Message{Address: "/oscillator/4/frequency", Args: []Arg{{Type: "f", Value: float32(440)}}}},
},
bytes: []byte{35, 98, 117, 110, 100, 108, 101, 0, 0, 0, 0,
32, 0, 0, 0, 0, 0, 0, 0, 32, 47, 111,
+10 -10
View File
@@ -1,32 +1,32 @@
package osc
type OSCPacket interface {
type Packet interface {
ToBytes() ([]byte, error)
}
type OSCBundle struct {
Contents []OSCPacket `json:"contents"`
TimeTag OSCTimeTag `json:"timeTag"`
type Bundle struct {
Contents []Packet `json:"contents"`
TimeTag TimeTag `json:"timeTag"`
}
type OSCArg struct {
type Arg struct {
Value any `json:"value"`
Type string `json:"type"`
}
type OSCMessage struct {
Address string `json:"address"`
Args []OSCArg `json:"args"`
type Message struct {
Address string `json:"address"`
Args []Arg `json:"args"`
}
type OSCColor struct {
type Color struct {
r uint8
g uint8
b uint8
a uint8
}
type OSCTimeTag struct {
type TimeTag struct {
seconds int32
fractionalSeconds int32
}