-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.go
200 lines (160 loc) · 4.89 KB
/
control.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
package muse
type ControlMessage map[string]any
type ControlReceiver interface {
ReceiveControlValue(any, int)
}
type ControlSender interface {
SendControlValue(any, int)
}
type Control interface {
Selfie
Identifiable
MessageReceiver
ControlReceiver
ControlSender
CtrlNamed(string) Control
CtrlAddTo(Patch) Control
AddControlInputConnection(int, Control, int)
AddControlOutputConnection(int, Control, int)
RemoveControlInputConnection(int, Control, int)
RemoveControlOutputConnection(int, Control, int)
CtrlIConns([]*IConn) Control
CtrlIn(...any) Control
CtrlConnect(int, Control, int)
CtrlDisconnect()
Tick(int64, *Configuration)
}
type ControlConnection struct {
Control Control
Index int
}
// BaseControl acts at control rate (once every audio frame) instead of audio rate
type BaseControl struct {
identifier string
self any
inConnections map[int][]*ControlConnection
outConnections map[int][]*ControlConnection
}
func NewBaseControl() *BaseControl {
bc := &BaseControl{
inConnections: map[int][]*ControlConnection{},
outConnections: map[int][]*ControlConnection{},
}
bc.self = bc
return bc
}
func (c *BaseControl) Identifier() string {
return c.identifier
}
func (c *BaseControl) SetIdentifier(id string) {
c.identifier = id
}
func (c *BaseControl) CtrlNamed(name string) Control {
self := c.Self().(Control)
self.SetIdentifier(name)
return self
}
func (c *BaseControl) Self() any {
return c.self
}
func (c *BaseControl) SetSelf(self any) {
c.self = self
}
func (c *BaseControl) Tick(timestamp int64, config *Configuration) {
// STUB: do anything with Tick in embedding struct
}
func (c *BaseControl) ReceiveControlValue(value any, index int) {
// STUB: do anything with the value received in embedding struct
}
func (c *BaseControl) ReceiveMessage(msg any) []*Message {
// STUB: do anything with the message received in embedding struct
return nil
}
func (c *BaseControl) SendControlValue(value any, index int) {
connections := c.outConnections[index]
for _, connection := range connections {
connection.Control.ReceiveControlValue(value, connection.Index)
}
}
func (c *BaseControl) AddControlInputConnection(inputIndex int, sender Control, outputIndex int) {
connections := c.inConnections[inputIndex]
if connections == nil {
connections = []*ControlConnection{}
}
connections = append(connections, &ControlConnection{Control: sender, Index: outputIndex})
c.inConnections[inputIndex] = connections
}
func (c *BaseControl) AddControlOutputConnection(outputIndex int, receiver Control, inputIndex int) {
connections := c.outConnections[outputIndex]
if connections == nil {
connections = []*ControlConnection{}
}
connections = append(connections, &ControlConnection{Control: receiver, Index: inputIndex})
c.outConnections[outputIndex] = connections
}
func (c *BaseControl) RemoveControlInputConnection(inputIndex int, sender Control, outputIndex int) {
conns := c.inConnections[inputIndex]
removeIndex := -1
for index, conn := range conns {
if conn.Control == sender && conn.Index == outputIndex {
removeIndex = index
break
}
}
if removeIndex > -1 {
c.inConnections[inputIndex] = append(conns[:removeIndex], conns[removeIndex+1:]...)
}
}
func (c *BaseControl) RemoveControlOutputConnection(outputIndex int, receiver Control, inputIndex int) {
conns := c.outConnections[outputIndex]
removeIndex := -1
for index, conn := range conns {
if conn.Control == receiver && conn.Index == inputIndex {
removeIndex = index
break
}
}
if removeIndex > -1 {
c.outConnections[outputIndex] = append(conns[:removeIndex], conns[removeIndex+1:]...)
}
}
func (c *BaseControl) CtrlAddTo(p Patch) Control {
return p.AddControl(c.Self().(Control))
}
func (c *BaseControl) CtrlIConns(iConns []*IConn) Control {
self := c.Self().(Control)
for _, iConn := range iConns {
iConn.Object.(Control).CtrlConnect(iConn.OutIndex, self, iConn.InIndex)
}
return self
}
func (c *BaseControl) CtrlIn(rawIconns ...any) Control {
return c.Self().(Control).CtrlIConns(IConns(rawIconns...))
}
func (c *BaseControl) CtrlConnect(outIndex int, receiver Control, inIndex int) {
self := c.Self().(Control)
self.AddControlOutputConnection(outIndex, receiver, inIndex)
receiver.AddControlInputConnection(inIndex, self, outIndex)
}
func (c *BaseControl) CtrlDisconnect() {
self := c.Self().(Control)
for inIndex, inConns := range c.inConnections {
for _, inConn := range inConns {
inConn.Control.RemoveControlOutputConnection(inConn.Index, self, inIndex)
}
}
for outIndex, outConns := range c.outConnections {
for _, outConn := range outConns {
outConn.Control.RemoveControlInputConnection(outConn.Index, self, outIndex)
}
}
}
type ControlThru struct {
*BaseControl
}
func NewControlThru() *ControlThru {
return &ControlThru{BaseControl: NewBaseControl()}
}
func (ct *ControlThru) ReceiveControlValue(value any, index int) {
ct.SendControlValue(value, index)
}