-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.go
534 lines (485 loc) · 13.5 KB
/
client.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package sc
import (
"errors"
"fmt"
"net"
"sync"
"sync/atomic"
"time"
"github.com/scgolang/osc"
)
// OSC addresses.
// See http://doc.sccode.org/Reference/Server-Command-Reference.html.
const (
bufferAllocAddress = "/b_alloc"
bufferGenAddress = "/b_gen"
bufferInfoAddress = "/b_info"
bufferQueryAddress = "/b_query"
bufferReadAddress = "/b_allocRead"
bufferReadChannelAddress = "/b_allocReadChannel"
doneOscAddress = "/done"
dumpOscAddress = "/dumpOSC"
groupDeepFreeAddress = "/g_deepFree"
groupDumpTreeAddress = "/g_dumpTree"
groupFreeAllAddress = "/g_freeAll"
groupHeadAddress = "/g_head"
groupNewAddress = "/g_new"
groupQueryTreeAddress = "/g_queryTree"
groupQueryTreeReplyAddress = "/g_queryTree.reply"
groupTailAddress = "/g_tail"
nodeFreeAddress = "/n_free"
nodeFillAddress = "/n_fill"
nodeMapAddress = "/n_map"
nodeMapnAddress = "/n_mapn"
nodeMapaAddress = "/n_mapa"
nodeMapanAddress = "/n_mapan"
nodeRunAddress = "/n_run"
nodeSetAddress = "/n_set"
nodeSetnAddress = "/n_setn"
statusAddress = "/status"
statusReplyAddress = "/status.reply"
synthNewAddress = "/s_new"
synthdefReceiveAddress = "/d_recv"
)
// Arguments to dumpOSC command.
// See http://doc.sccode.org/Reference/Server-Command-Reference.html#/dumpOSC
const (
DumpOff = 0
DumpParsed = 1
DumpContents = 2
DumpAll = 3
)
// Arguments to s_new command.
// See http://doc.sccode.org/Reference/Server-Command-Reference.html#/s_new
const (
AddToHead = int32(0)
AddToTail = int32(1)
AddBefore = int32(2)
AddAfter = int32(3)
AddReplace = int32(4)
)
const (
// RootNodeID is what sclang uses as the root node ID. See http://doc.sccode.org/Classes/RootNode.html.
RootNodeID = int32(0)
// DefaultGroupID is what sclang uses for the default group ID. See http://doc.sccode.org/Reference/default_group.html.
DefaultGroupID = int32(1)
// DefaultLocalAddr is the listening address for DefaultClient.
DefaultLocalAddr = "0.0.0.0:57110"
// DefaultScsynthAddr is the remote address for DefaultClient.
DefaultScsynthAddr = "0.0.0.0:57120"
// DefaultConnectTimeout is the default timeout for connecting to scsynth.
DefaultConnectTimeout = time.Second
)
// Common errors.
var (
ErrTimeout = errors.New("timeout error")
)
// Client manages all communication with scsynth
type Client struct {
// errChan is a channel that emits errors from
// the goroutine that runs the OSC server that is
// used to receive messages from scsynth
errChan chan error
closeMutex sync.Mutex
closed int32
addr *net.UDPAddr
oscConn osc.Conn
bufferInfoChan chan osc.Message // bufferInfoChan relays /b_info messages
doneChan chan osc.Message // doneChan relays /done messages
statusChan chan osc.Message // statusChan relays /status.reply messages
gqueryTreeChan chan osc.Message // gqueryTreeChan relays /done messages
nextSynthID int32 // next synth node ID
}
// number of concurrent handlers for /done messages.
const numDoneHandlers = 8
// NewClient creates a new SuperCollider client.
// The client will bind to the provided address and port
// to receive messages from scsynth.
func NewClient(network, local, scsynth string, timeout time.Duration) (*Client, error) {
addr, err := net.ResolveUDPAddr(network, local)
if err != nil {
return nil, err
}
c := &Client{
errChan: make(chan error),
bufferInfoChan: make(chan osc.Message),
doneChan: make(chan osc.Message, numDoneHandlers),
gqueryTreeChan: make(chan osc.Message),
statusChan: make(chan osc.Message),
addr: addr,
nextSynthID: 1000,
}
if err := c.Connect(scsynth, timeout); err != nil {
return nil, err
}
return c, nil
}
var (
defaultClient *Client
defaultGroup *GroupNode
)
// DefaultClient returns the default sc client.
func DefaultClient() (*Client, error) {
var err error
if defaultClient == nil {
defaultClient, err = NewClient("udp", DefaultLocalAddr, DefaultScsynthAddr, DefaultConnectTimeout)
if err != nil {
return nil, err
}
defaultGroup, err = defaultClient.AddDefaultGroup()
if err != nil {
return nil, err
}
}
return defaultClient, nil
}
// AddDefaultGroup adds the default group.
func (c *Client) AddDefaultGroup() (*GroupNode, error) {
return c.Group(DefaultGroupID, AddToTail, RootNodeID)
}
// Connect connects to an scsynth instance via UDP.
func (c *Client) Connect(addr string, timeout time.Duration) error {
raddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return err
}
// Attempt connection with a timeout.
var (
start = time.Now()
timedOut = true
)
for time.Now().Sub(start) < timeout {
oscConn, err := osc.DialUDP("udp", c.addr, raddr)
if err != nil {
time.Sleep(100 * time.Millisecond)
continue
}
c.oscConn = oscConn
timedOut = false
break
}
if timedOut {
return errors.New("connection timeout")
}
// listen for OSC messages
go func(errChan chan error) {
var (
start = time.Now()
err error
)
for time.Now().Sub(start) < timeout {
err = c.oscConn.Serve(8, c.oscHandlers()) // Arbitrary number of worker routines.
if err != nil {
time.Sleep(100 * time.Second)
continue
}
}
if err != nil {
errChan <- err
}
}(c.errChan)
return nil
}
// DumpOSC sends a /dumpOSC message to scsynth
// level should be DumpOff, DumpParsed, DumpContents, DumpAll
func (c *Client) DumpOSC(level int32) error {
return c.oscConn.Send(osc.Message{
Address: dumpOscAddress,
Arguments: osc.Arguments{
osc.Int(level),
},
})
}
// FreeAll frees all nodes in a group
func (c *Client) FreeAll(gids ...int32) error {
msg := osc.Message{
Address: groupFreeAllAddress,
}
for _, gid := range gids {
msg.Arguments = append(msg.Arguments, osc.Int(gid))
}
return c.oscConn.Send(msg)
}
// Group creates a group.
func (c *Client) Group(id, action, target int32) (*GroupNode, error) {
msg := osc.Message{
Address: groupNewAddress,
Arguments: osc.Arguments{
osc.Int(id),
osc.Int(action),
osc.Int(target),
},
}
if err := c.oscConn.Send(msg); err != nil {
return nil, err
}
return newGroup(c, id), nil
}
// NextSynthID gets the next available ID for creating a synth
func (c *Client) NextSynthID() int32 {
return atomic.AddInt32(&c.nextSynthID, 1)
}
// NodeFree stops a node abruptly, removes it from its group, and frees its memory.
// Using this method can cause a click if the node is not silent at the time it is freed.
func (c *Client) NodeFree(id int32) error {
return c.oscConn.Send(osc.Message{
Address: nodeFreeAddress,
Arguments: osc.Arguments{osc.Int(id)},
})
}
// NodeMap causes controls of a node to be read from a control bus.
// The first argument is the node ID.
// The second argument is a map from control names to control bus indices.
func (c *Client) NodeMap(id int32, m map[string]int32) error {
msg := osc.Message{
Address: nodeMapAddress,
Arguments: osc.Arguments{
osc.Int(id),
},
}
for k, v := range m {
msg.Arguments = append(msg.Arguments, osc.String(k))
msg.Arguments = append(msg.Arguments, osc.Int(v))
}
return c.oscConn.Send(msg)
}
// NodeMapa causes controls of a node to be read from an audio bus.
// The first argument is the node ID.
// The second argument is a map from control names to audio bus indices.
func (c *Client) NodeMapa(id int32, m map[string]int32) error {
msg := osc.Message{
Address: nodeMapaAddress,
Arguments: osc.Arguments{
osc.Int(id),
},
}
for k, v := range m {
msg.Arguments = append(msg.Arguments, osc.String(k))
msg.Arguments = append(msg.Arguments, osc.Int(v))
}
return c.oscConn.Send(msg)
}
// NodeSet sets a control value on a node.
func (c *Client) NodeSet(id int32, ctls map[string]float32) error {
msg := osc.Message{
Address: nodeSetAddress,
Arguments: osc.Arguments{
osc.Int(id),
},
}
for k, v := range ctls {
msg.Arguments = append(msg.Arguments, osc.String(k))
msg.Arguments = append(msg.Arguments, osc.Float(v))
}
return c.oscConn.Send(msg)
}
// QueryGroup g_queryTree for a particular group.
func (c *Client) QueryGroup(id int32) (*GroupNode, error) {
if err := c.oscConn.Send(osc.Message{
Address: groupQueryTreeAddress,
Arguments: osc.Arguments{
osc.Int(id),
osc.Int(1),
},
}); err != nil {
return nil, err
}
// wait for response
var resp osc.Message
select {
case resp = <-c.gqueryTreeChan:
case <-time.After(2 * time.Second):
return nil, errors.New("timeout waiting for response")
}
if numArgs := len(resp.Arguments); numArgs < 3 {
return nil, fmt.Errorf("expected 3 arguments for message, got %d", numArgs)
}
// Throw away the flag that tells us we want to include synth controls in the reply.
// We already know we requested that!
resp.Arguments = resp.Arguments[1:]
return c.parseGroup(resp)
}
// SendAllDefs sends all the synthdefs that have been registered with RegisterSynthdef.
func (c *Client) SendAllDefs() error {
// If you add to this map, please keep the keys in alphabetical order.
for name, f := range map[string]UgenFunc{
"grainbuf_mono": defGrainBuf(1),
"grainbuf_stereo": defGrainBuf(2),
"in": defIn,
"jpverb": defJPverb,
"lfo": defLFO,
"lfpulse": defLFPulse,
"lfsaw": defLFSaw,
"sine_a": defSineA,
"sine_c": defSineC,
} {
if err := c.SendDef(NewSynthdef(name, f)); err != nil {
return err
}
}
return nil
}
// SendDef sends a synthdef to scsynth.
// This method blocks until a /done message is received
// indicating that the synthdef was loaded
func (c *Client) SendDef(def *Synthdef) error {
db, err := def.Bytes()
if err != nil {
return err
}
msg := osc.Message{
Address: synthdefReceiveAddress,
Arguments: osc.Arguments{
osc.Blob(db),
},
}
if err := c.oscConn.Send(msg); err != nil {
return err
}
var done osc.Message
select {
case done = <-c.doneChan:
goto ParseMessage
case err = <-c.errChan:
return err
}
ParseMessage:
// error if this message was not an ack of the synthdef
errmsg := "expected /done with /d_recv argument"
if len(done.Arguments) != 1 {
return fmt.Errorf(errmsg)
}
addr, err := done.Arguments[0].ReadString()
if err != nil {
return err
}
if addr != synthdefReceiveAddress {
return errors.New(errmsg)
}
return nil
}
// Status gets the status of scsynth with a timeout.
// If the status request times out it returns ErrTimeout.
func (c *Client) Status(timeout time.Duration) (*ServerStatus, error) {
statusReq := osc.Message{
Address: statusAddress,
}
if err := c.oscConn.Send(statusReq); err != nil {
return nil, err
}
after := time.After(timeout)
select {
case _ = <-after:
return nil, ErrTimeout
case msg := <-c.statusChan:
return newStatus(msg)
case err := <-c.errChan:
return nil, err
}
}
// Synth creates a synth node.
func (c *Client) Synth(defName string, id, action, target int32, ctls map[string]float32) (*Synth, error) {
msg := osc.Message{
Address: synthNewAddress,
Arguments: osc.Arguments{
osc.String(defName),
osc.Int(id),
osc.Int(action),
osc.Int(target),
},
}
if ctls != nil {
for k, v := range ctls {
msg.Arguments = append(msg.Arguments, osc.String(k))
msg.Arguments = append(msg.Arguments, osc.Float(v))
}
}
if err := c.oscConn.Send(msg); err != nil {
return nil, err
}
return newSynth(c, defName, id), nil
}
// SynthArgs contains the arguments necessary to create a synth that is part of a group.
type SynthArgs struct {
DefName string
ID int32
Action int32
Target int32
Ctls map[string]float32
}
// Synths creates multiple synth nodes at once with an OSC bundle.
func (c *Client) Synths(args []SynthArgs) error {
bun := osc.Bundle{
Packets: make([]osc.Packet, len(args)),
}
for i, arg := range args {
msg := osc.Message{
Address: synthNewAddress,
Arguments: osc.Arguments{
osc.String(arg.DefName),
osc.Int(arg.ID),
osc.Int(arg.Action),
osc.Int(arg.Target),
},
}
for k, v := range arg.Ctls {
msg.Arguments = append(msg.Arguments, osc.String(k))
msg.Arguments = append(msg.Arguments, osc.Float(v))
}
bun.Packets[i] = msg
}
return c.oscConn.Send(bun)
}
// addOscHandlers adds OSC handlers
func (c *Client) oscHandlers() osc.Dispatcher {
return map[string]osc.MessageHandler{
bufferInfoAddress: osc.Method(func(msg osc.Message) error {
c.bufferInfoChan <- msg
return nil
}),
statusReplyAddress: osc.Method(func(msg osc.Message) error {
c.statusChan <- msg
return nil
}),
doneOscAddress: osc.Method(func(msg osc.Message) error {
c.doneChan <- msg
return nil
}),
groupQueryTreeReplyAddress: osc.Method(func(msg osc.Message) error {
c.gqueryTreeChan <- msg
return nil
}),
}
}
// PlayDef plays a synthdef by sending the synthdef using
// DefaultClient, then immediately creating a synth node from the def.
func PlayDef(def *Synthdef) (*Synth, error) {
c, err := DefaultClient()
if err != nil {
return nil, err
}
if err := c.SendDef(def); err != nil {
return nil, err
}
synthID := c.NextSynthID()
return defaultGroup.Synth(def.Name, synthID, AddToTail, nil)
}
// Close closes the client.
func (c *Client) Close() error {
if c.isClosed() {
return nil
}
atomic.StoreInt32(&c.closed, 1)
if err := c.oscConn.Close(); err != nil {
return err
}
close(c.errChan)
close(c.doneChan)
close(c.statusChan)
close(c.gqueryTreeChan)
return nil
}
// isClosed says whether or not the client is closed.
func (c *Client) isClosed() bool {
return atomic.LoadInt32(&c.closed) == int32(1)
}