-
Notifications
You must be signed in to change notification settings - Fork 1
/
max.go
665 lines (553 loc) · 13.7 KB
/
max.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
package max
// #cgo CFLAGS: -I${SRCDIR}/lib/max -I${SRCDIR}/lib/msp
// #cgo windows CFLAGS: -DWIN_VERSION=1
// #cgo darwin CFLAGS: -DMAC_VERSION=1
// #cgo darwin LDFLAGS: -Wl,-undefined,dynamic_lookup
// #cgo windows LDFLAGS: -L${SRCDIR}/lib/max/x64 -L${SRCDIR}/lib/msp/x64 -lMaxAPI -lMaxAudio
// #include "max.h"
import "C"
import (
"fmt"
"reflect"
"sync"
"sync/atomic"
"unsafe"
"github.com/kr/pretty"
)
/* Types */
// Type describes an inlet or outlet type.
type Type string
// The available inlet and outlet types.
const (
Bang Type = "bang"
Int Type = "int"
Float Type = "float"
List Type = "list"
Any Type = "any"
Signal Type = "signal"
)
func (t Type) enum() C.maxgo_type_e {
switch t {
case Bang:
return C.MAXGO_BANG
case Int:
return C.MAXGO_INT
case Float:
return C.MAXGO_FLOAT
case List:
return C.MAXGO_LIST
case Any:
return C.MAXGO_ANY
case Signal:
return C.MAXGO_SIGNAL
default:
panic("invalid type")
}
}
// Atom is a Max atom of type int64, float64 or string.
type Atom = interface{}
// Event describes an emitted event.
type Event struct {
Outlet *Outlet
Type Type
Msg string
Data []Atom
}
/* Basic */
// Log will print a message to the max console.
func Log(format string, args ...interface{}) {
C.maxgo_log(C.CString(fmt.Sprintf(format, args...))) // string freed by receiver
}
// Error will print an error to the max console.
func Error(format string, args ...interface{}) {
C.maxgo_error(C.CString(fmt.Sprintf(format, args...))) // string freed by receiver
}
// Alert will show an alert dialog.
func Alert(format string, args ...interface{}) {
C.maxgo_alert(C.CString(fmt.Sprintf(format, args...))) // string freed by receiver
}
// Pretty will pretty print and log the provided values.
func Pretty(a ...interface{}) {
Log(pretty.Sprint(a...))
}
var symbols sync.Map
func gensym(str string) *C.t_symbol {
// check cache
val, ok := symbols.Load(str)
if ok {
return val.(*C.t_symbol)
}
// get and cache symbol
sym := C.maxgo_gensym(C.CString(str)) // string freed by receiver
symbols.Store(str, sym)
return sym
}
/* Initialization */
// InitCallback is called to initialize objects.
type InitCallback func(obj *Object, atoms []Atom) bool
// HandleCallback is called to handle messages.
type HandleCallback func(obj *Object, inlet int, name string, atoms []Atom)
// ProcessCallback is called to process audio.
type ProcessCallback func(obj *Object, input, output []float64)
// FreeCallback is called to free objects.
type FreeCallback func(obj *Object)
var initCallback InitCallback
var handleCallback HandleCallback
var processCallback ProcessCallback
var freeCallback FreeCallback
var initMutex sync.Mutex
var initDone bool
//go:linkname mainMain main.main
func mainMain()
//export maxgoMain
func maxgoMain() {
// call main
mainMain()
// acquire mutex
initMutex.Lock()
defer initMutex.Unlock()
// check flag
if !initDone {
panic("not initialized")
}
}
// Init will initialize the Max class with the specified name using the provided
// callbacks to initialize and free objects. This function must be called from
// the main packages main() function.
//
// The provided callbacks are called to initialize and object, handle messages,
// process audio and free the object when it is not used anymore. The callbacks
// are usually called on the Max main thread. However, the handler may be called
// from an unknown thread in parallel to the other callbacks.
func Init(name string, init InitCallback, handle HandleCallback, process ProcessCallback, free FreeCallback) {
// ensure mutex
initMutex.Lock()
defer initMutex.Unlock()
// check flag
if initDone {
panic("already initialized")
}
// set callbacks
initCallback = init
handleCallback = handle
processCallback = process
freeCallback = free
// initialize
C.maxgo_init(C.CString(name)) // string freed by receiver
// set flag
initDone = true
}
/* Classes */
var counter uint64
var objects = map[uint64]*Object{}
var objectsMutex sync.Mutex
//export maxgoInit
func maxgoInit(ptr unsafe.Pointer, argc int64, argv *C.t_atom) (uint64, int, int) {
// decode atoms
atoms := decodeAtoms(argc, argv)
// get ref
ref := atomic.AddUint64(&counter, 1)
// prepare object
obj := &Object{
ref: ref,
ptr: ptr,
queue: make(chan Event, 100),
}
// store object
objectsMutex.Lock()
objects[ref] = obj
objectsMutex.Unlock()
// call init callback
ok := initCallback(obj, atoms)
if !ok {
return 0, 0, 0
}
// determine required proxies and signals
var proxies int
var signals int
for _, inlet := range obj.in {
if inlet.Type() == Signal {
signals++
} else {
proxies++
}
}
if signals == 0 && proxies > 0 {
proxies--
}
// create outlets in reverse order
for i := len(obj.out) - 1; i >= 0; i-- {
outlet := obj.out[i]
switch outlet.typ {
case Bang:
outlet.ptr = C.bangout(obj.ptr)
case Int:
outlet.ptr = C.intout(obj.ptr)
case Float:
outlet.ptr = C.floatout(obj.ptr)
case List:
outlet.ptr = C.listout(obj.ptr)
case Any:
outlet.ptr = C.outlet_new(obj.ptr, nil)
case Signal:
str := C.CString("signal")
outlet.ptr = C.outlet_new(obj.ptr, str)
C.free(unsafe.Pointer(str))
default:
panic("invalid outlet type")
}
}
return ref, proxies, signals
}
//export maxgoHandle
func maxgoHandle(ref uint64, msg *C.char, inlet int64, argc int64, argv *C.t_atom) {
// get object
objectsMutex.Lock()
obj, ok := objects[ref]
objectsMutex.Unlock()
if !ok {
return
}
// decode atoms
atoms := decodeAtoms(argc, argv)
// get name
name := C.GoString(msg)
// check inlet
if inlet >= 0 {
// get inlet
in := obj.in[inlet]
if in == nil {
return
}
// check signal
if in.typ == Signal {
Error("message received on signal inlet %d", inlet)
return
}
// check name
if in.typ != Any && Type(name) != in.typ {
Error("invalid message received on inlet %d", inlet)
return
}
// check atoms
if in.typ == Bang && len(atoms) != 0 || (in.typ == Int || in.typ == Float) && len(atoms) != 1 {
Error("unexpected input received on inlet %d", inlet)
return
}
// check types
switch in.typ {
case Int:
if _, ok := atoms[0].(int64); !ok {
Error("invalid input received on inlet %d", inlet)
return
}
case Float:
if _, ok := atoms[0].(float64); !ok {
Error("invalid input received on inlet %d", inlet)
return
}
}
}
// run callback if available
if handleCallback != nil {
handleCallback(obj, int(inlet), name, atoms)
}
}
//export maxgoProcess
func maxgoProcess(ref uint64, in, out *C.double, samples C.long) {
// get object
objectsMutex.Lock()
obj, ok := objects[ref]
objectsMutex.Unlock()
if !ok {
return
}
// prepare input and output
var input []float64
var output []float64
// convert input
if in != nil {
input = []float64{}
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&input))
sliceHeader.Cap = int(samples)
sliceHeader.Len = int(samples)
sliceHeader.Data = uintptr(unsafe.Pointer(in))
}
// convert output
if out != nil {
output = []float64{}
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&output))
sliceHeader.Cap = int(samples)
sliceHeader.Len = int(samples)
sliceHeader.Data = uintptr(unsafe.Pointer(out))
}
// run callback if available
if processCallback != nil {
processCallback(obj, input, output)
}
}
//export maxgoPop
func maxgoPop(ref uint64) (unsafe.Pointer, C.maxgo_type_e, *C.t_symbol, int64, *C.t_atom, bool) {
// get object
objectsMutex.Lock()
obj, ok := objects[ref]
objectsMutex.Unlock()
if !ok {
return nil, 0, nil, 0, nil, false
}
// get event
var evt Event
select {
case evt = <-obj.queue:
default:
return nil, 0, nil, 0, nil, false
}
// encode atoms
argc, argv := encodeAtoms(evt.Data)
// get symbol if available
var sym *C.t_symbol
if evt.Type == Any {
sym = gensym(evt.Msg)
}
// determine if there are more events
more := len(obj.queue) > 0
return evt.Outlet.ptr, evt.Type.enum(), sym, argc, argv, more
}
//export maxgoDescribe
func maxgoDescribe(ref uint64, io, i int64) (*C.char, bool) {
// get object
objectsMutex.Lock()
obj, ok := objects[ref]
objectsMutex.Unlock()
if !ok {
return nil, false
}
// return label
if io == 1 {
if int(i) < len(obj.in) {
label := fmt.Sprintf("%s (%s)", obj.in[i].label, obj.in[i].typ)
return C.CString(label), obj.in[i].hot // string freed by receiver
}
} else {
if int(i) < len(obj.out) {
label := fmt.Sprintf("%s (%s)", obj.out[i].label, obj.out[i].typ)
return C.CString(label), false // string freed by receiver
}
}
return nil, false
}
//export maxgoFree
func maxgoFree(ref uint64) {
// get and delete object
objectsMutex.Lock()
obj, ok := objects[ref]
delete(objects, ref)
objectsMutex.Unlock()
if !ok {
return
}
// run callback if available
if freeCallback != nil {
freeCallback(obj)
}
}
/* Objects */
// Object is single Max object.
type Object struct {
ref uint64
ptr unsafe.Pointer
in []*Inlet
out []*Outlet
queue chan Event
}
// Push will add the provided events to the objects queue.
func (o *Object) Push(events ...Event) {
// queue events
for _, evt := range events {
select {
case o.queue <- evt:
default:
Error("dropped event due to full queue")
}
}
// notify
C.maxgo_notify(o.ptr)
}
// Inlet is a single Max inlet.
type Inlet struct {
typ Type
label string
hot bool
}
// Inlet will declare an inlet. If no inlets are added to an object it will have
// a default inlet to receive messages.
func (o *Object) Inlet(typ Type, label string, hot bool) *Inlet {
// check signal
if typ == Signal && len(o.in) > 0 {
panic("signal only supported as the first inlet")
}
// create inlet
inlet := &Inlet{typ: typ, label: label, hot: hot}
// store inlet
o.in = append(o.in, inlet)
return inlet
}
// Type will return the inlets type.
func (i *Inlet) Type() Type {
return i.typ
}
// Label will return the inlets label.
func (i *Inlet) Label() string {
return i.label
}
// Outlet is a single MAx outlet.
type Outlet struct {
obj *Object
typ Type
label string
ptr unsafe.Pointer
}
// Outlet will declare an outlet.
func (o *Object) Outlet(typ Type, label string) *Outlet {
// check signal
if typ == Signal && len(o.out) > 0 {
panic("signal only supported as the first outlet")
}
// create outlet
outlet := &Outlet{obj: o, typ: typ, label: label}
// store outlet
o.out = append(o.out, outlet)
return outlet
}
// Type will return the outlets type.
func (o *Outlet) Type() Type {
return o.typ
}
// Label will return the outlets label.
func (o *Outlet) Label() string {
return o.label
}
// Bang will send a bang.
func (o *Outlet) Bang() {
if o.typ == Bang || o.typ == Any {
o.obj.Push(Event{Outlet: o, Type: Bang})
} else {
Error("bang sent to outlet of type %s", o.typ)
}
}
// Int will send and int.
func (o *Outlet) Int(n int64) {
if o.typ == Int || o.typ == Any {
o.obj.Push(Event{Outlet: o, Type: Int, Data: []Atom{n}})
} else {
Error("int sent to outlet of type %s", o.typ)
}
}
// Float will send a float.
func (o *Outlet) Float(n float64) {
if o.typ == Float || o.typ == Any {
o.obj.Push(Event{Outlet: o, Type: Float, Data: []Atom{n}})
} else {
Error("float sent to outlet of type %s", o.typ)
}
}
// List will send a list.
func (o *Outlet) List(atoms []Atom) {
if o.typ == List || o.typ == Any {
o.obj.Push(Event{Outlet: o, Type: List, Data: atoms})
} else {
Error("list sent to outlet of type %s", o.typ)
}
}
// Any will send any message.
func (o *Outlet) Any(msg string, atoms []Atom) {
if o.typ == Any {
o.obj.Push(Event{Outlet: o, Type: Any, Msg: msg, Data: atoms})
} else {
Error("any sent to outlet of type %s", o.typ)
}
}
/* Threads */
var queue = map[uint64]func(){}
var queueMutex sync.Mutex
// IsMainThread will return if the Max main thead is executing.
func IsMainThread() bool {
return C.systhread_ismainthread() == 1
}
//export maxgoYield
func maxgoYield(ref uint64) {
// get function
queueMutex.Lock()
fn := queue[ref]
delete(queue, ref)
queueMutex.Unlock()
// execute function
fn()
}
// Defer will run the provided function on the Max main thread.
func Defer(fn func()) {
// get reference
ref := atomic.AddUint64(&counter, 1)
// store function
queueMutex.Lock()
queue[ref] = fn
queueMutex.Unlock()
// defer call
C.maxgo_defer(C.ulonglong(ref))
}
/* Atoms */
func decodeAtoms(argc int64, argv *C.t_atom) []Atom {
// check empty
if argc == 0 {
return nil
}
// cast to slice
var list []C.t_atom
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&list))
sliceHeader.Cap = int(argc)
sliceHeader.Len = int(argc)
sliceHeader.Data = uintptr(unsafe.Pointer(argv))
// allocate result
atoms := make([]interface{}, len(list))
// add atoms
for i, item := range list {
switch item.a_type {
case C.A_LONG:
atoms[i] = int64(C.atom_getlong(&item))
case C.A_FLOAT:
atoms[i] = float64(C.atom_getfloat(&item))
case C.A_SYM:
atoms[i] = C.GoString(C.atom_getsym(&item).s_name)
default:
atoms[i] = nil
}
}
return atoms
}
// the receiver must arrange for the returned non-nil array to be freed
func encodeAtoms(atoms []Atom) (int64, *C.t_atom) {
// check length
if len(atoms) == 0 {
return 0, nil
}
// allocate atom array
array := (*C.t_atom)(unsafe.Pointer(C.getbytes(C.t_getbytes_size(len(atoms) * C.sizeof_t_atom))))
// cast to slice
var slice []C.t_atom
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&slice))
sliceHeader.Cap = len(atoms)
sliceHeader.Len = len(atoms)
sliceHeader.Data = uintptr(unsafe.Pointer(array))
// set atoms
for i, atom := range atoms {
switch atom := atom.(type) {
case int64:
C.atom_setlong(&slice[i], C.t_atom_long(atom))
case float64:
C.atom_setfloat(&slice[i], C.double(atom))
case string:
C.atom_setsym(&slice[i], gensym(atom))
}
}
return int64(len(atoms)), array
}