-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
183 lines (149 loc) · 3.23 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"fmt"
"log"
"net"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/hypebeast/go-osc/osc"
"github.com/jsimonetti/go-artnet/packet"
"github.com/pkg/errors"
)
type oscClient struct {
conn *net.UDPConn
}
func newOscClient(addr string) (*oscClient, error) {
c := &oscClient{}
remoteAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, err
}
conn, err := net.DialUDP("udp", nil, remoteAddr)
if err != nil {
return nil, err
}
c.conn = conn
return c, nil
}
func (c *oscClient) getChannel(ch int) (float32, error) {
msg := osc.NewMessage(fmt.Sprintf("/ch/%02d/mix/fader", ch))
data, err := msg.MarshalBinary()
if err != nil {
return 0, err
}
if _, err = c.conn.Write(data); err != nil {
return 0, err
}
data = make([]byte, 65535)
_, err = c.conn.Read(data)
if err != nil {
return 0, err
}
packet, err := osc.ParsePacket(string(data))
if err != nil {
return 0, err
}
switch packetMsg := packet.(type) {
case *osc.Message:
return packetMsg.Arguments[0].(float32), nil
default:
return 0, errors.Errorf("Unknown packet/message type %T", packetMsg)
}
}
func (c *oscClient) close() {
c.conn.Close()
}
type artnetController struct {
conn *net.UDPConn
remoteAddr *net.UDPAddr
channels [512]byte
universe uint8
channelOffset int
}
func newArtnetController(addr string, universe uint8, channelOffset int) (*artnetController, error) {
c := &artnetController{}
remoteAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, err
}
conn, err := net.DialUDP("udp", nil, remoteAddr)
if err != nil {
return nil, err
}
c.conn = conn
c.universe = universe
c.channelOffset = channelOffset
return c, nil
}
func (c *artnetController) setChannel(ch int, value byte) {
c.channels[ch+c.channelOffset] = value
}
func (c *artnetController) sendChannels() error {
p := &packet.ArtDMXPacket{
Sequence: 0x00,
SubUni: 0,
Net: 0,
Data: c.channels,
}
data, err := p.MarshalBinary()
if err != nil {
return err
}
_, err = c.conn.Write(data)
if err != nil {
return err
}
return nil
}
func (c *artnetController) close() {
c.conn.Close()
}
func main() {
if len(os.Args) < 4 {
log.Fatal("Usage: osc-to-artnet OSC_ADDR ARTNET_ADDR ARTNET_UNIVERSE ARTNET_CHANNEL_OFFSET\nExample: osc-to-artnet 192.168.1.65:10023 2.0.0.1:6454 0 0")
}
universe, err := strconv.Atoi(os.Args[2])
if err != nil {
log.Fatal(err)
}
channelOffset, err := strconv.Atoi(os.Args[3])
if err != nil {
log.Fatal(err)
}
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
client, err := newOscClient(os.Args[0])
if err != nil {
log.Fatal(err)
}
defer client.close()
controller, err := newArtnetController(os.Args[1], uint8(universe), channelOffset)
if err != nil {
log.Fatal(err)
}
defer controller.close()
loop:
for {
for i := 0; i < 32; i++ {
value, err := client.getChannel(i + 1)
if err != nil {
log.Fatal(err)
}
controller.setChannel(i, byte(value*255))
}
err = controller.sendChannels()
if err != nil {
log.Fatal(err)
}
select {
case sig := <-c:
log.Printf("Got signal %v, exiting.", sig)
break loop
default:
time.Sleep(16 * time.Millisecond)
}
}
}