/
ugeninput.go
59 lines (52 loc) · 1.45 KB
/
ugeninput.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
package sc
// UgenInput creates a ugen suitable for use as an input to other ugens.
// It will return either a single-channel ugen or a multi-channel ugen.
func UgenInput(name string, rate int8, specialIndex int16, numOutputs int, inputs ...Input) Input {
expanded := expandInputs(inputs...)
l := len(expanded)
if l == 1 {
return NewUgenNode(name, rate, specialIndex, numOutputs, inputs...)
}
// return MultiNode
a := make([]*UgenNode, len(expanded))
for i := range a {
a[i] = NewUgenNode(name, rate, specialIndex, numOutputs, expanded[i]...)
}
return NewMultiNode(a...)
}
// expandInputs turns an array of inputs into a 2-dimensional array
// of inputs where the 1st dimension is the channel and
// the second is the array of inputs for each channel.
func expandInputs(inputs ...Input) [][]Input {
// first pass to determine how large each array needs to be
// this could probably be more efficient
sz := 0
for _, in := range inputs {
if multi, isMulti := in.(MultiInput); isMulti {
ins := multi.InputArray()
l := len(ins)
if l > sz {
sz = l
}
}
}
if sz == 0 {
// none were multi-channel inputs
return [][]Input{inputs}
}
n := len(inputs)
arr := make([][]Input, sz)
for i := range arr {
arr[i] = make([]Input, n)
for j := range arr[i] {
in := inputs[j]
if multi, isMulti := in.(MultiInput); isMulti {
ins := multi.InputArray()
arr[i][j] = ins[i%len(ins)]
} else {
arr[i][j] = in
}
}
}
return arr
}