-
Notifications
You must be signed in to change notification settings - Fork 4
/
dispatch.go
89 lines (79 loc) · 1.59 KB
/
dispatch.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
package snd
import (
"sort"
"sync"
)
// TODO most of this probably doesn't need to be exposed
// but those details can be worked out once additional audio
// drivers are supported.
type Dispatcher struct{ sync.WaitGroup }
// Dispatch blocks until all inputs are prepared.
func (dp *Dispatcher) Dispatch(tc uint64, inps ...*Input) {
wt := inps[0].wt
for _, inp := range inps {
if inp.wt != wt {
dp.Wait()
wt = inp.wt
}
dp.Add(1)
go func(sd Sound, tc uint64) {
sd.Prepare(tc)
dp.Done()
}(inp.sd, tc)
}
dp.Wait()
}
type Input struct {
sd Sound
wt int
}
type ByWT []*Input
func (a ByWT) Len() int { return len(a) }
func (a ByWT) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByWT) Less(i, j int) bool { return a[i].wt > a[j].wt }
func (a ByWT) Slice() (sl [][]*Input) {
if len(a) == 0 {
return nil
}
wt := a[0].wt
i := 0
for j, p := range a {
if p.wt != wt {
sl = append(sl, a[i:j])
i = j
wt = p.wt
}
}
return append(sl, a[i:])
}
func GetInputs(sd Sound) []*Input {
inps := []*Input{{sd, 0}}
getinputs(sd, 1, &inps)
sort.Sort(ByWT(inps))
return inps
}
// TODO janky func
func getinputs(sd Sound, wt int, out *[]*Input) {
for _, in := range sd.Inputs() {
if in == nil { // TODO for !realtime || in.IsOff() {
continue
}
at := -1
for i, p := range *out {
if p.sd == in {
if p.wt >= wt {
return // object has or will be traversed on different path
}
at = i
break
}
}
if at != -1 {
(*out)[at].sd = in
(*out)[at].wt = wt
} else {
*out = append(*out, &Input{in, wt})
}
getinputs(in, wt+1, out)
}
}