forked from scgolang/sendosc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (63 loc) · 1.56 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"flag"
"log"
"net"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/scgolang/osc"
)
func main() {
var (
host = flag.String("h", "127.0.0.1", "host")
port = flag.Int("p", 57120, "port")
address = flag.String("a", "/foo", "OSC address")
)
flag.Parse()
msg := osc.Message{Address: *address}
if err := addArgs(&msg, flag.Args()); err != nil {
log.Fatal(err)
}
// Send the message.
addr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(*host, strconv.Itoa(*port)))
if err != nil {
log.Fatal(err)
}
conn, err := osc.DialUDP("udp", nil, addr)
if err != nil {
log.Fatal(err)
}
if err := conn.Send(msg); err != nil {
log.Fatal(err)
}
}
// addArgs adds arguments to an osc message.
func addArgs(msg *osc.Message, args []string) error {
for _, arg := range args {
if err := addArg(msg, arg); err != nil {
return errors.Wrapf(err, "could not add arg '%s' to msg", arg)
}
}
return nil
}
const argsep = ":"
// addArg adds a single argument to an osc message.
// It expects osc message arguments to be formatted as <typetag>:<data>
func addArg(msg *osc.Message, arg string) error {
pieces := strings.Split(arg, argsep)
typetag, data := pieces[0], pieces[1]
switch typetag {
default:
return errors.New("unsupported typetag: " + typetag)
case "s":
msg.Arguments = append(msg.Arguments, osc.String(data))
case "i":
i, err := strconv.ParseInt(data, 10, 64)
if err != nil {
return errors.Wrapf(err, "could not parse integer from %s", data)
}
msg.Arguments = append(msg.Arguments, osc.Int(i))
}
return nil
}