mirror of
https://github.com/jwetzell/osc-go.git
synced 2026-07-26 10:28:42 +00:00
184 lines
3.3 KiB
Go
184 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
|
|
osc "github.com/jwetzell/osc-go"
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
func main() {
|
|
|
|
cmd := &cli.Command{
|
|
Name: "makeosc",
|
|
Usage: "make osc bytes",
|
|
Flags: []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "address",
|
|
Value: "",
|
|
Usage: "OSC address",
|
|
Required: true,
|
|
},
|
|
&cli.StringSliceFlag{
|
|
Name: "arg",
|
|
Usage: "OSC args",
|
|
},
|
|
&cli.StringSliceFlag{
|
|
Name: "type",
|
|
Usage: "OSC types",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "slip",
|
|
Value: false,
|
|
Usage: "whether to slip encode the OSC Message bytes",
|
|
},
|
|
},
|
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
|
address := cmd.String("address")
|
|
args := cmd.StringSlice("arg")
|
|
types := cmd.StringSlice("type")
|
|
slip := cmd.Bool("slip")
|
|
makeMsg(address, args, types, slip)
|
|
return nil
|
|
},
|
|
}
|
|
|
|
if err := cmd.Run(context.Background(), os.Args); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func argToTypedArg(rawArg string, oscType string) osc.OSCArg {
|
|
|
|
switch oscType {
|
|
case "s":
|
|
return osc.OSCArg{
|
|
Value: rawArg,
|
|
Type: "s",
|
|
}
|
|
case "i":
|
|
number, err := strconv.ParseInt(rawArg, 10, 32)
|
|
if err != nil {
|
|
// ... handle error
|
|
panic(err)
|
|
}
|
|
return osc.OSCArg{
|
|
Value: int32(number),
|
|
Type: "i",
|
|
}
|
|
case "f":
|
|
number, err := strconv.ParseFloat(rawArg, 32)
|
|
if err != nil {
|
|
// ... handle error
|
|
panic(err)
|
|
}
|
|
return osc.OSCArg{
|
|
Value: float32(number),
|
|
Type: "f",
|
|
}
|
|
case "b":
|
|
data, err := hex.DecodeString(rawArg)
|
|
if err != nil {
|
|
// ... handle error
|
|
panic(err)
|
|
}
|
|
return osc.OSCArg{
|
|
Value: data,
|
|
Type: "b",
|
|
}
|
|
case "h":
|
|
number, err := strconv.ParseInt(rawArg, 10, 64)
|
|
if err != nil {
|
|
// ... handle error
|
|
panic(err)
|
|
}
|
|
return osc.OSCArg{
|
|
Value: int64(number),
|
|
Type: "h",
|
|
}
|
|
case "d":
|
|
number, err := strconv.ParseFloat(rawArg, 64)
|
|
if err != nil {
|
|
// ... handle error
|
|
panic(err)
|
|
}
|
|
return osc.OSCArg{
|
|
Value: float64(number),
|
|
Type: "d",
|
|
}
|
|
case "T":
|
|
return osc.OSCArg{
|
|
Value: true,
|
|
Type: "T",
|
|
}
|
|
case "F":
|
|
return osc.OSCArg{
|
|
Value: false,
|
|
Type: "F",
|
|
}
|
|
case "N":
|
|
return osc.OSCArg{
|
|
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{}
|
|
}
|
|
}
|
|
|
|
func slipEncode(bytes []byte) []byte {
|
|
END := byte(0xc0)
|
|
ESC := byte(0xdb)
|
|
ESC_END := byte(0xdc)
|
|
ESC_ESC := byte(0xdd)
|
|
|
|
var encodedBytes = []byte{END}
|
|
|
|
for _, byteToEncode := range bytes {
|
|
switch byteToEncode {
|
|
case END:
|
|
encodedBytes = append(encodedBytes, ESC, ESC_END)
|
|
case ESC:
|
|
encodedBytes = append(encodedBytes, ESC, ESC_ESC)
|
|
default:
|
|
encodedBytes = append(encodedBytes, byteToEncode)
|
|
}
|
|
}
|
|
|
|
encodedBytes = append(encodedBytes, END)
|
|
return encodedBytes
|
|
}
|
|
|
|
func makeMsg(address string, args []string, types []string, slip bool) {
|
|
|
|
oscMessage := osc.OSCMessage{
|
|
Address: address,
|
|
Args: []osc.OSCArg{},
|
|
}
|
|
|
|
for index, arg := range args {
|
|
oscType := "s"
|
|
if len(types) > index {
|
|
oscType = types[index]
|
|
}
|
|
|
|
oscMessage.Args = append(oscMessage.Args, argToTypedArg(arg, oscType))
|
|
}
|
|
|
|
oscMessageBuffer, err := oscMessage.ToBytes()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if slip {
|
|
oscMessageBuffer = slipEncode(oscMessageBuffer)
|
|
}
|
|
os.Stdout.Write(oscMessageBuffer)
|
|
}
|