mirror of
https://github.com/jwetzell/osc-go.git
synced 2026-08-16 12:13:26 +00:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8ed1ab065c | |||
| d6d4ac4419 | |||
| a86a9b3121 | |||
| 216f84a2d9 | |||
| bd6a548e34 | |||
| d8dff0794a | |||
| 0c4d36b376 | |||
| a5ac419e3e | |||
| 3b66c38d18 | |||
| 4d8ba2a47b | |||
| 0caf1b02a7 |
@@ -18,7 +18,7 @@ jobs:
|
|||||||
run: go test -v -coverprofile=coverage.txt .
|
run: go test -v -coverprofile=coverage.txt .
|
||||||
|
|
||||||
- name: Upload coverage to Codecov
|
- name: Upload coverage to Codecov
|
||||||
uses: codecov/codecov-action@v5
|
uses: codecov/codecov-action@v6
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.CODECOV_TOKEN }}
|
token: ${{ secrets.CODECOV_TOKEN }}
|
||||||
files: coverage.txt
|
files: coverage.txt
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
[](https://codecov.io/gh/jwetzell/osc-go)
|
||||||
|
|
||||||
A mostly complete OSC implementation and collection of command line OSC utilities written in Go. Mainly an exercise in learning Go.
|
A mostly complete OSC implementation and collection of command line OSC utilities written in Go. Mainly an exercise in learning Go.
|
||||||
|
|
||||||
## Utilities
|
## Utilities
|
||||||
|
|||||||
@@ -8,10 +8,7 @@ func (b *OSCBundle) ToBytes() ([]byte, error) {
|
|||||||
|
|
||||||
bytes := stringToOSCBytes("#bundle")
|
bytes := stringToOSCBytes("#bundle")
|
||||||
|
|
||||||
timeTagBytes, err := timeTagToOSCBytes(b.TimeTag)
|
timeTagBytes := timeTagToOSCBytes(b.TimeTag)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
bytes = append(bytes, timeTagBytes...)
|
bytes = append(bytes, timeTagBytes...)
|
||||||
|
|
||||||
for _, packet := range b.Contents {
|
for _, packet := range b.Contents {
|
||||||
@@ -21,10 +18,7 @@ func (b *OSCBundle) ToBytes() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
packetLength := len(packetBytes)
|
packetLength := len(packetBytes)
|
||||||
|
|
||||||
packetLengthBytes, err := int32ToOSCBytes(int32(packetLength))
|
packetLengthBytes := int32ToOSCBytes(int32(packetLength))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
bytes = append(bytes, packetLengthBytes...)
|
bytes = append(bytes, packetLengthBytes...)
|
||||||
bytes = append(bytes, packetBytes...)
|
bytes = append(bytes, packetBytes...)
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-23
@@ -12,41 +12,37 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var Address string
|
|
||||||
var Args []string
|
|
||||||
var Types []string
|
|
||||||
var Slip bool
|
|
||||||
|
|
||||||
cmd := &cli.Command{
|
cmd := &cli.Command{
|
||||||
Name: "makeosc",
|
Name: "makeosc",
|
||||||
Usage: "make osc bytes",
|
Usage: "make osc bytes",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "address",
|
Name: "address",
|
||||||
Value: "",
|
Value: "",
|
||||||
Usage: "OSC address",
|
Usage: "OSC address",
|
||||||
Destination: &Address,
|
Required: true,
|
||||||
Required: true,
|
|
||||||
},
|
},
|
||||||
&cli.StringSliceFlag{
|
&cli.StringSliceFlag{
|
||||||
Name: "arg",
|
Name: "arg",
|
||||||
Usage: "OSC args",
|
Usage: "OSC args",
|
||||||
Destination: &Args,
|
|
||||||
},
|
},
|
||||||
&cli.StringSliceFlag{
|
&cli.StringSliceFlag{
|
||||||
Name: "type",
|
Name: "type",
|
||||||
Usage: "OSC types",
|
Usage: "OSC types",
|
||||||
Destination: &Types,
|
|
||||||
},
|
},
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "slip",
|
Name: "slip",
|
||||||
Value: false,
|
Value: false,
|
||||||
Usage: "whether to slip encode the OSC Message bytes",
|
Usage: "whether to slip encode the OSC Message bytes",
|
||||||
Destination: &Slip,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||||
make(Address, Args, Types, Slip)
|
address := cmd.String("address")
|
||||||
|
args := cmd.StringSlice("arg")
|
||||||
|
types := cmd.StringSlice("type")
|
||||||
|
slip := cmd.Bool("slip")
|
||||||
|
makeMsg(address, args, types, slip)
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -130,8 +126,7 @@ func argToTypedArg(rawArg string, oscType string) osc.OSCArg {
|
|||||||
Type: "N",
|
Type: "N",
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
fmt.Print("unhandled osc type: ")
|
fmt.Printf("unsupported OSC arg type: %s\n", oscType)
|
||||||
fmt.Printf("%s.\n", oscType)
|
|
||||||
// TODO(jwetzell): something better than this like actual nil, err thing
|
// TODO(jwetzell): something better than this like actual nil, err thing
|
||||||
return osc.OSCArg{}
|
return osc.OSCArg{}
|
||||||
}
|
}
|
||||||
@@ -159,7 +154,7 @@ func slipEncode(bytes []byte) []byte {
|
|||||||
return encodedBytes
|
return encodedBytes
|
||||||
}
|
}
|
||||||
|
|
||||||
func make(address string, args []string, types []string, slip bool) {
|
func makeMsg(address string, args []string, types []string, slip bool) {
|
||||||
|
|
||||||
oscMessage := osc.OSCMessage{
|
oscMessage := osc.OSCMessage{
|
||||||
Address: address,
|
Address: address,
|
||||||
|
|||||||
@@ -12,33 +12,25 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var IP string
|
|
||||||
var Port int32
|
|
||||||
var Protocol string
|
|
||||||
var Format string
|
|
||||||
var Slip bool
|
|
||||||
|
|
||||||
cmd := &cli.Command{
|
cmd := &cli.Command{
|
||||||
Name: "receiveosc",
|
Name: "receiveosc",
|
||||||
Usage: "receive OSC messages via UDP or TCP",
|
Usage: "receive OSC messages via UDP or TCP",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "ip",
|
Name: "ip",
|
||||||
Usage: "ip to receive OSC messages on",
|
Usage: "ip to receive OSC messages on",
|
||||||
Value: "0.0.0.0",
|
Value: "0.0.0.0",
|
||||||
Destination: &IP,
|
|
||||||
},
|
},
|
||||||
&cli.Int32Flag{
|
&cli.Int32Flag{
|
||||||
Name: "port",
|
Name: "port",
|
||||||
Usage: "port to receive OSC messages on",
|
Usage: "port to receive OSC messages on",
|
||||||
Destination: &Port,
|
Value: 8888,
|
||||||
Value: 8888,
|
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "protocol",
|
Name: "protocol",
|
||||||
Usage: "protocol to use to receive (tcp or udp)",
|
Usage: "protocol to use to receive (tcp or udp)",
|
||||||
Value: "udp",
|
Value: "udp",
|
||||||
Destination: &Protocol,
|
|
||||||
Validator: func(flag string) error {
|
Validator: func(flag string) error {
|
||||||
if flag != "udp" && flag != "tcp" {
|
if flag != "udp" && flag != "tcp" {
|
||||||
return fmt.Errorf("protocol must be either 'udp' or 'tcp'")
|
return fmt.Errorf("protocol must be either 'udp' or 'tcp'")
|
||||||
@@ -47,10 +39,9 @@ func main() {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "format",
|
Name: "format",
|
||||||
Usage: "format for messages to be output in ('json')",
|
Usage: "format for messages to be output in ('json')",
|
||||||
Value: "json",
|
Value: "json",
|
||||||
Destination: &Format,
|
|
||||||
Validator: func(flag string) error {
|
Validator: func(flag string) error {
|
||||||
if flag != "json" {
|
if flag != "json" {
|
||||||
return fmt.Errorf("format must be 'json'")
|
return fmt.Errorf("format must be 'json'")
|
||||||
@@ -59,22 +50,27 @@ func main() {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "slip",
|
Name: "slip",
|
||||||
Value: false,
|
Value: false,
|
||||||
Usage: "whether to slip encode the OSC Message bytes",
|
Usage: "whether to slip encode the OSC Message bytes",
|
||||||
Destination: &Slip,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||||
netAddress := fmt.Sprintf("%s:%d", IP, Port)
|
ip := cmd.String("ip")
|
||||||
switch Protocol {
|
port := cmd.Int32("port")
|
||||||
|
protocol := cmd.String("protocol")
|
||||||
|
format := cmd.String("format")
|
||||||
|
slip := cmd.Bool("slip")
|
||||||
|
|
||||||
|
netAddress := fmt.Sprintf("%s:%d", ip, port)
|
||||||
|
switch protocol {
|
||||||
case "udp":
|
case "udp":
|
||||||
listenUDP(netAddress, Format)
|
listenUDP(netAddress, format)
|
||||||
case "tcp":
|
case "tcp":
|
||||||
if !Slip {
|
if !slip {
|
||||||
return fmt.Errorf("OSC 1.0 over TCP is not supported yet")
|
return fmt.Errorf("OSC 1.0 over TCP is not supported yet")
|
||||||
}
|
}
|
||||||
listenTCP(netAddress, Slip, Format)
|
listenTCP(netAddress, slip, format)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
|||||||
+31
-39
@@ -15,35 +15,25 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var Host string
|
|
||||||
var Port int32
|
|
||||||
var Address string
|
|
||||||
var Protocol string
|
|
||||||
var Args []string
|
|
||||||
var Types []string
|
|
||||||
var Slip bool
|
|
||||||
|
|
||||||
cmd := &cli.Command{
|
cmd := &cli.Command{
|
||||||
Name: "sendosc",
|
Name: "sendosc",
|
||||||
Usage: "send OSC messages via UDP or TCP",
|
Usage: "send OSC messages via UDP or TCP",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "host",
|
Name: "host",
|
||||||
Usage: "host to send OSC message to",
|
Usage: "host to send OSC message to",
|
||||||
Destination: &Host,
|
Required: true,
|
||||||
Required: true,
|
|
||||||
},
|
},
|
||||||
&cli.Int32Flag{
|
&cli.Int32Flag{
|
||||||
Name: "port",
|
Name: "port",
|
||||||
Usage: "port to send OSC message to",
|
Usage: "port to send OSC message to",
|
||||||
Destination: &Port,
|
Required: true,
|
||||||
Required: true,
|
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "protocol",
|
Name: "protocol",
|
||||||
Usage: "protocol to use to send (tcp or udp)",
|
Usage: "protocol to use to send (tcp or udp)",
|
||||||
Value: "udp",
|
Value: "udp",
|
||||||
Destination: &Protocol,
|
|
||||||
Validator: func(flag string) error {
|
Validator: func(flag string) error {
|
||||||
if flag != "udp" && flag != "tcp" {
|
if flag != "udp" && flag != "tcp" {
|
||||||
return fmt.Errorf("protocol must be either 'udp' or 'tcp'")
|
return fmt.Errorf("protocol must be either 'udp' or 'tcp'")
|
||||||
@@ -52,32 +42,35 @@ func main() {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "address",
|
Name: "address",
|
||||||
Usage: "OSC address",
|
Usage: "OSC address",
|
||||||
Destination: &Address,
|
Required: true,
|
||||||
Required: true,
|
|
||||||
},
|
},
|
||||||
&cli.StringSliceFlag{
|
&cli.StringSliceFlag{
|
||||||
Name: "arg",
|
Name: "arg",
|
||||||
Usage: "OSC args",
|
Usage: "OSC args",
|
||||||
Value: []string{},
|
Value: []string{},
|
||||||
Destination: &Args,
|
|
||||||
},
|
},
|
||||||
&cli.StringSliceFlag{
|
&cli.StringSliceFlag{
|
||||||
Name: "type",
|
Name: "type",
|
||||||
Usage: "OSC types",
|
Usage: "OSC types",
|
||||||
Value: []string{},
|
Value: []string{},
|
||||||
Destination: &Types,
|
|
||||||
},
|
},
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "slip",
|
Name: "slip",
|
||||||
Value: false,
|
Value: false,
|
||||||
Usage: "whether to slip encode the OSC Message bytes",
|
Usage: "whether to slip encode the OSC Message bytes",
|
||||||
Destination: &Slip,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||||
send(Host, Port, Address, Args, Types, Protocol, Slip)
|
host := cmd.String("host")
|
||||||
|
port := cmd.Int32("port")
|
||||||
|
address := cmd.String("address")
|
||||||
|
args := cmd.StringSlice("arg")
|
||||||
|
types := cmd.StringSlice("type")
|
||||||
|
protocol := cmd.String("protocol")
|
||||||
|
slip := cmd.Bool("slip")
|
||||||
|
send(host, port, address, args, types, protocol, slip)
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -161,8 +154,7 @@ func argToTypedArg(rawArg string, oscType string) osc.OSCArg {
|
|||||||
Type: "N",
|
Type: "N",
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
fmt.Print("unhandled osc type: ")
|
fmt.Printf("unsupported OSC arg type: %s\n", oscType)
|
||||||
fmt.Printf("%s.\n", oscType)
|
|
||||||
// TODO(jwetzell): something better than this like actual nil, err thing
|
// TODO(jwetzell): something better than this like actual nil, err thing
|
||||||
return osc.OSCArg{}
|
return osc.OSCArg{}
|
||||||
}
|
}
|
||||||
@@ -223,7 +215,7 @@ func send(host string, port int32, address string, args []string, types []string
|
|||||||
oscMessageBuffer = append(sizeBytes, oscMessageBuffer...)
|
oscMessageBuffer = append(sizeBytes, oscMessageBuffer...)
|
||||||
}
|
}
|
||||||
|
|
||||||
netAddress := fmt.Sprintf("%s:%d", host, port)
|
netAddress := net.JoinHostPort(host, fmt.Sprintf("%d", port))
|
||||||
conn, err := net.Dial(protocol, netAddress)
|
conn, err := net.Dial(protocol, netAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Dial err %v", err)
|
fmt.Printf("Dial err %v", err)
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package osc
|
|||||||
|
|
||||||
// TODO(jwetzell): split things up
|
// TODO(jwetzell): split things up
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -18,7 +17,7 @@ func stringToOSCBytes(rawString string) []byte {
|
|||||||
|
|
||||||
padLength := 4 - (len(sb.String()) % 4)
|
padLength := 4 - (len(sb.String()) % 4)
|
||||||
if padLength < 4 {
|
if padLength < 4 {
|
||||||
for i := 0; i < padLength; i++ {
|
for range padLength {
|
||||||
sb.WriteString("\u0000")
|
sb.WriteString("\u0000")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -26,75 +25,64 @@ func stringToOSCBytes(rawString string) []byte {
|
|||||||
return []byte(sb.String())
|
return []byte(sb.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func int32ToOSCBytes(number int32) ([]byte, error) {
|
func int32ToOSCBytes(number int32) []byte {
|
||||||
var buf bytes.Buffer
|
bytes := make([]byte, 4)
|
||||||
err := binary.Write(&buf, binary.BigEndian, number)
|
bytes[0] = byte((number >> 24) & 0xFF)
|
||||||
if err != nil {
|
bytes[1] = byte((number >> 16) & 0xFF)
|
||||||
return nil, err
|
bytes[2] = byte((number >> 8) & 0xFF)
|
||||||
}
|
bytes[3] = byte(number & 0xFF)
|
||||||
return buf.Bytes(), nil
|
return bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
func int64ToOSCBytes(number int64) ([]byte, error) {
|
func int64ToOSCBytes(number int64) []byte {
|
||||||
var buf bytes.Buffer
|
bytes := make([]byte, 8)
|
||||||
err := binary.Write(&buf, binary.BigEndian, number)
|
bytes[0] = byte((number >> 56) & 0xFF)
|
||||||
if err != nil {
|
bytes[1] = byte((number >> 48) & 0xFF)
|
||||||
return nil, err
|
bytes[2] = byte((number >> 40) & 0xFF)
|
||||||
}
|
bytes[3] = byte((number >> 32) & 0xFF)
|
||||||
return buf.Bytes(), nil
|
bytes[4] = byte((number >> 24) & 0xFF)
|
||||||
|
bytes[5] = byte((number >> 16) & 0xFF)
|
||||||
|
bytes[6] = byte((number >> 8) & 0xFF)
|
||||||
|
bytes[7] = byte(number & 0xFF)
|
||||||
|
return bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
func float32ToOSCBytes(number float32) ([]byte, error) {
|
func float32ToOSCBytes(number float32) []byte {
|
||||||
var buf bytes.Buffer
|
bytes := make([]byte, 4)
|
||||||
err := binary.Write(&buf, binary.BigEndian, number)
|
binary.BigEndian.PutUint32(bytes, math.Float32bits(number))
|
||||||
if err != nil {
|
return bytes
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func float64ToOSCBytes(number float64) ([]byte, error) {
|
func float64ToOSCBytes(number float64) []byte {
|
||||||
var buf bytes.Buffer
|
bytes := make([]byte, 8)
|
||||||
err := binary.Write(&buf, binary.BigEndian, number)
|
binary.BigEndian.PutUint64(bytes, math.Float64bits(number))
|
||||||
if err != nil {
|
return bytes
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func byteArrayToOSCBytes(bytes []byte) ([]byte, error) {
|
func byteArrayToOSCBytes(bytes []byte) []byte {
|
||||||
oscBytes := []byte{}
|
oscBytes := []byte{}
|
||||||
|
|
||||||
bytesSize := len(bytes)
|
bytesSize := len(bytes)
|
||||||
bytesSizeBytes, err := int32ToOSCBytes(int32(bytesSize))
|
bytesSizeBytes := int32ToOSCBytes(int32(bytesSize))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
oscBytes = append(oscBytes, bytesSizeBytes...)
|
oscBytes = append(oscBytes, bytesSizeBytes...)
|
||||||
oscBytes = append(oscBytes, bytes...)
|
oscBytes = append(oscBytes, bytes...)
|
||||||
|
|
||||||
padLength := 4 - (bytesSize % 4)
|
padLength := 4 - (bytesSize % 4)
|
||||||
if padLength < 4 {
|
if padLength < 4 {
|
||||||
for i := 0; i < padLength; i++ {
|
for range padLength {
|
||||||
oscBytes = append(oscBytes, 0)
|
oscBytes = append(oscBytes, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return oscBytes, nil
|
return oscBytes
|
||||||
}
|
}
|
||||||
|
|
||||||
func timeTagToOSCBytes(timeTag OSCTimeTag) ([]byte, error) {
|
func timeTagToOSCBytes(timeTag OSCTimeTag) []byte {
|
||||||
timeTagBytes, err := int32ToOSCBytes(timeTag.seconds)
|
timeTagBytes := int32ToOSCBytes(timeTag.seconds)
|
||||||
if err != nil {
|
fractionalSecondsBytes := int32ToOSCBytes(timeTag.fractionalSeconds)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
fractionalSecondsBytes, err := int32ToOSCBytes(timeTag.fractionalSeconds)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
timeTagBytes = append(timeTagBytes, fractionalSecondsBytes...)
|
timeTagBytes = append(timeTagBytes, fractionalSecondsBytes...)
|
||||||
|
|
||||||
return timeTagBytes, nil
|
return timeTagBytes
|
||||||
}
|
}
|
||||||
|
|
||||||
func argsToBuffer(args []OSCArg) ([]byte, error) {
|
func argsToBuffer(args []OSCArg) ([]byte, error) {
|
||||||
@@ -102,7 +90,7 @@ func argsToBuffer(args []OSCArg) ([]byte, error) {
|
|||||||
var argBuffers = []byte{}
|
var argBuffers = []byte{}
|
||||||
|
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
switch oscType := arg.Type; oscType {
|
switch arg.Type {
|
||||||
case "s":
|
case "s":
|
||||||
if value, ok := arg.Value.(string); ok {
|
if value, ok := arg.Value.(string); ok {
|
||||||
argBuffers = append(argBuffers, stringToOSCBytes(value)...)
|
argBuffers = append(argBuffers, stringToOSCBytes(value)...)
|
||||||
@@ -111,60 +99,36 @@ func argsToBuffer(args []OSCArg) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
case "i":
|
case "i":
|
||||||
if value, ok := arg.Value.(int); ok {
|
if value, ok := arg.Value.(int); ok {
|
||||||
valueBytes, err := int32ToOSCBytes(int32(value))
|
valueBytes := int32ToOSCBytes(int32(value))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
argBuffers = append(argBuffers, valueBytes...)
|
argBuffers = append(argBuffers, valueBytes...)
|
||||||
} else if value, ok := arg.Value.(int32); ok {
|
} else if value, ok := arg.Value.(int32); ok {
|
||||||
valueBytes, err := int32ToOSCBytes(int32(value))
|
valueBytes := int32ToOSCBytes(int32(value))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
argBuffers = append(argBuffers, valueBytes...)
|
argBuffers = append(argBuffers, valueBytes...)
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("OSC arg had int32 type but non-number value")
|
return nil, errors.New("OSC arg had int32 type but non-number value")
|
||||||
}
|
}
|
||||||
case "f":
|
case "f":
|
||||||
if value, ok := arg.Value.(float32); ok {
|
if value, ok := arg.Value.(float32); ok {
|
||||||
valueBytes, err := float32ToOSCBytes(value)
|
valueBytes := float32ToOSCBytes(value)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
argBuffers = append(argBuffers, valueBytes...)
|
argBuffers = append(argBuffers, valueBytes...)
|
||||||
} else if value, ok := arg.Value.(float64); ok {
|
} else if value, ok := arg.Value.(float64); ok {
|
||||||
valueBytes, err := float32ToOSCBytes(float32(value))
|
valueBytes := float32ToOSCBytes(float32(value))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
argBuffers = append(argBuffers, valueBytes...)
|
argBuffers = append(argBuffers, valueBytes...)
|
||||||
} else if value, ok := arg.Value.(int); ok {
|
} else if value, ok := arg.Value.(int); ok {
|
||||||
valueBytes, err := float32ToOSCBytes(float32(value))
|
valueBytes := float32ToOSCBytes(float32(value))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
argBuffers = append(argBuffers, valueBytes...)
|
argBuffers = append(argBuffers, valueBytes...)
|
||||||
} else if value, ok := arg.Value.(int32); ok {
|
} else if value, ok := arg.Value.(int32); ok {
|
||||||
valueBytes, err := float32ToOSCBytes(float32(value))
|
valueBytes := float32ToOSCBytes(float32(value))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
argBuffers = append(argBuffers, valueBytes...)
|
argBuffers = append(argBuffers, valueBytes...)
|
||||||
} else if value, ok := arg.Value.(int64); ok {
|
} else if value, ok := arg.Value.(int64); ok {
|
||||||
valueBytes, err := float32ToOSCBytes(float32(value))
|
valueBytes := float32ToOSCBytes(float32(value))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
argBuffers = append(argBuffers, valueBytes...)
|
argBuffers = append(argBuffers, valueBytes...)
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("OSC arg had float32 type but non-number value")
|
return nil, errors.New("OSC arg had float32 type but non-number value")
|
||||||
}
|
}
|
||||||
case "b":
|
case "b":
|
||||||
if value, ok := arg.Value.([]byte); ok {
|
if value, ok := arg.Value.([]byte); ok {
|
||||||
valueBytes, err := byteArrayToOSCBytes(value)
|
valueBytes := byteArrayToOSCBytes(value)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
argBuffers = append(argBuffers, valueBytes...)
|
argBuffers = append(argBuffers, valueBytes...)
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("OSC arg had blob type but non-blob value")
|
return nil, errors.New("OSC arg had blob type but non-blob value")
|
||||||
@@ -188,62 +152,38 @@ func argsToBuffer(args []OSCArg) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
case "h":
|
case "h":
|
||||||
if value, ok := arg.Value.(int); ok {
|
if value, ok := arg.Value.(int); ok {
|
||||||
valueBytes, err := int64ToOSCBytes(int64(value))
|
valueBytes := int64ToOSCBytes(int64(value))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
argBuffers = append(argBuffers, valueBytes...)
|
argBuffers = append(argBuffers, valueBytes...)
|
||||||
} else if value, ok := arg.Value.(int32); ok {
|
} else if value, ok := arg.Value.(int32); ok {
|
||||||
valueBytes, err := int64ToOSCBytes(int64(value))
|
valueBytes := int64ToOSCBytes(int64(value))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
argBuffers = append(argBuffers, valueBytes...)
|
argBuffers = append(argBuffers, valueBytes...)
|
||||||
} else if value, ok := arg.Value.(int64); ok {
|
} else if value, ok := arg.Value.(int64); ok {
|
||||||
valueBytes, err := int64ToOSCBytes(value)
|
valueBytes := int64ToOSCBytes(value)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
argBuffers = append(argBuffers, valueBytes...)
|
argBuffers = append(argBuffers, valueBytes...)
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("OSC arg had int64 type but non-number value")
|
return nil, errors.New("OSC arg had int64 type but non-number value")
|
||||||
}
|
}
|
||||||
case "d":
|
case "d":
|
||||||
if value, ok := arg.Value.(float32); ok {
|
if value, ok := arg.Value.(float32); ok {
|
||||||
valueBytes, err := float64ToOSCBytes(float64(value))
|
valueBytes := float64ToOSCBytes(float64(value))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
argBuffers = append(argBuffers, valueBytes...)
|
argBuffers = append(argBuffers, valueBytes...)
|
||||||
} else if value, ok := arg.Value.(float64); ok {
|
} else if value, ok := arg.Value.(float64); ok {
|
||||||
valueBytes, err := float64ToOSCBytes(value)
|
valueBytes := float64ToOSCBytes(value)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
argBuffers = append(argBuffers, valueBytes...)
|
argBuffers = append(argBuffers, valueBytes...)
|
||||||
} else if value, ok := arg.Value.(int); ok {
|
} else if value, ok := arg.Value.(int); ok {
|
||||||
valueBytes, err := float64ToOSCBytes(float64(value))
|
valueBytes := float64ToOSCBytes(float64(value))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
argBuffers = append(argBuffers, valueBytes...)
|
argBuffers = append(argBuffers, valueBytes...)
|
||||||
} else if value, ok := arg.Value.(int32); ok {
|
} else if value, ok := arg.Value.(int32); ok {
|
||||||
valueBytes, err := float64ToOSCBytes(float64(value))
|
valueBytes := float64ToOSCBytes(float64(value))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
argBuffers = append(argBuffers, valueBytes...)
|
argBuffers = append(argBuffers, valueBytes...)
|
||||||
} else if value, ok := arg.Value.(int64); ok {
|
} else if value, ok := arg.Value.(int64); ok {
|
||||||
valueBytes, err := float64ToOSCBytes(float64(value))
|
valueBytes := float64ToOSCBytes(float64(value))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
argBuffers = append(argBuffers, valueBytes...)
|
argBuffers = append(argBuffers, valueBytes...)
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("OSC arg had float64 type but non-number value")
|
return nil, errors.New("OSC arg had float64 type but non-number value")
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupported OSC argument type: %s", oscType)
|
return nil, fmt.Errorf("unsupported OSC argument type: %s", arg.Type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return argBuffers, nil
|
return argBuffers, nil
|
||||||
@@ -307,7 +247,7 @@ func readOSCFloat32(bytes []byte) (float32, []byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func readOSCFloat64(bytes []byte) (float64, []byte, error) {
|
func readOSCFloat64(bytes []byte) (float64, []byte, error) {
|
||||||
if len(bytes) < 4 {
|
if len(bytes) < 8 {
|
||||||
return 0, bytes, errors.New("OSC float64 arg is not 8 bytes")
|
return 0, bytes, errors.New("OSC float64 arg is not 8 bytes")
|
||||||
}
|
}
|
||||||
bits := binary.BigEndian.Uint64(bytes[0:8])
|
bits := binary.BigEndian.Uint64(bytes[0:8])
|
||||||
@@ -321,6 +261,10 @@ func readOSCBlob(bytes []byte) ([]byte, []byte, error) {
|
|||||||
return []byte{}, bytes, errors.New("OSC blob arg size not valid: " + err.Error())
|
return []byte{}, bytes, errors.New("OSC blob arg size not valid: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if blobLength < 0 {
|
||||||
|
return []byte{}, bytes, errors.New("OSC blob arg size not valid: size cannot be negative")
|
||||||
|
}
|
||||||
|
|
||||||
if len(remainingBytes) < int(blobLength) {
|
if len(remainingBytes) < int(blobLength) {
|
||||||
return []byte{}, bytes, errors.New("OSC blob arg size not valid: size specified is larger than remaining bytes")
|
return []byte{}, bytes, errors.New("OSC blob arg size not valid: size specified is larger than remaining bytes")
|
||||||
}
|
}
|
||||||
@@ -331,6 +275,9 @@ func readOSCBlob(bytes []byte) ([]byte, []byte, error) {
|
|||||||
if blobLengthPadding < 4 {
|
if blobLengthPadding < 4 {
|
||||||
blobEnd = blobEnd + blobLengthPadding
|
blobEnd = blobEnd + blobLengthPadding
|
||||||
}
|
}
|
||||||
|
if int(blobEnd) > len(bytes) {
|
||||||
|
return []byte{}, bytes, errors.New("OSC blob arg size not valid: size specified is larger than remaining bytes when accounting for padding")
|
||||||
|
}
|
||||||
return bytes[4 : 4+blobLength], bytes[blobEnd:], nil
|
return bytes[4 : 4+blobLength], bytes[blobEnd:], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -443,8 +390,7 @@ func readOSCArg(bytes []byte, oscType string) (OSCArg, []byte, error) {
|
|||||||
oscArg.Value = argTimeTag
|
oscArg.Value = argTimeTag
|
||||||
remainingBytes = bytesLeft
|
remainingBytes = bytesLeft
|
||||||
default:
|
default:
|
||||||
fmt.Printf("unsupported osc type: %s\n", oscType)
|
return OSCArg{}, bytes, fmt.Errorf("unsupported OSC argument type: %s", oscType)
|
||||||
readArgError = errors.New("unsupported OSC argument type: " + oscType)
|
|
||||||
}
|
}
|
||||||
return oscArg, remainingBytes, readArgError
|
return oscArg, remainingBytes, readArgError
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user