-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
231 lines (190 loc) · 5.04 KB
/
module.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
package muse
type Module interface {
Control
Named(string) Module
AddTo(Patch) Module
Configuration() *Configuration
NumInputs() int
NumOutputs() int
InputAtIndex(int) *Socket
OutputAtIndex(int) *Socket
AddInputConnection(int, *Connection)
AddOutputConnection(int, *Connection)
RemoveInputConnection(int, Module, int)
RemoveOutputConnection(int, Module, int)
SetDidSynthesize(bool)
DidSynthesize() bool
MustSynthesize() bool
PrepareSynthesis()
Synthesize() bool
In(...any) Module
IConns([]*IConn) Module
Connect(int, Module, int)
Disconnect()
Exec(func(any)) Module
}
type BaseModule struct {
*BaseControl
Inputs []*Socket
Outputs []*Socket
Config *Configuration
didSynthesize bool
}
func NewBaseModule(numInputs int, numOutputs int) *BaseModule {
config := CurrentConfiguration()
inputs := make([]*Socket, numInputs)
for i := 0; i < numInputs; i++ {
inputs[i] = NewSocket(config.BufferSize)
}
outputs := make([]*Socket, numOutputs)
for i := 0; i < numOutputs; i++ {
outputs[i] = NewSocket(config.BufferSize)
}
return &BaseModule{
BaseControl: NewBaseControl(),
Inputs: inputs,
Outputs: outputs,
Config: config,
}
}
func (m *BaseModule) NumInputs() int {
return len(m.Inputs)
}
func (m *BaseModule) NumOutputs() int {
return len(m.Outputs)
}
func (m *BaseModule) InputAtIndex(index int) *Socket {
return m.Inputs[index]
}
func (m *BaseModule) OutputAtIndex(index int) *Socket {
return m.Outputs[index]
}
func (m *BaseModule) Configuration() *Configuration {
return m.Config
}
func (m *BaseModule) AddInputConnection(inputIndex int, conn *Connection) {
m.Inputs[inputIndex].AddConnection(conn)
}
func (m *BaseModule) AddOutputConnection(outputIndex int, conn *Connection) {
m.Outputs[outputIndex].AddConnection(conn)
}
func (m *BaseModule) RemoveInputConnection(inputIndex int, sender Module, outputIndex int) {
removeIndex := -1
conns := m.Inputs[inputIndex].Connections
for index, conn := range conns {
if conn.Index == outputIndex && conn.Module == sender {
removeIndex = index
break
}
}
if removeIndex > -1 {
m.Inputs[inputIndex].Connections = append(conns[:removeIndex], conns[removeIndex+1:]...)
}
}
func (m *BaseModule) RemoveOutputConnection(outputIndex int, receiver Module, inputIndex int) {
removeIndex := -1
conns := m.Outputs[outputIndex].Connections
for index, conn := range conns {
if conn.Index == inputIndex && conn.Module == receiver {
removeIndex = index
break
}
}
if removeIndex > -1 {
m.Outputs[outputIndex].Connections = append(conns[:removeIndex], conns[removeIndex+1:]...)
}
}
func (m *BaseModule) Named(name string) Module {
self := m.Self().(Module)
self.SetIdentifier(name)
return self
}
func (m *BaseModule) AddTo(p Patch) Module {
return p.AddModule(m.Self().(Module))
}
func (m *BaseModule) IConns(iConns []*IConn) Module {
self := m.Self().(Module)
for _, iConn := range iConns {
iConn.Object.(Module).Connect(iConn.OutIndex, self, iConn.InIndex)
}
return self
}
func (m *BaseModule) In(rawIconns ...any) Module {
return m.Self().(Module).IConns(IConns(rawIconns...))
}
func (m *BaseModule) Connect(outIndex int, to Module, inIndex int) {
from := m.Self().(Module)
p, ok := from.(Patch)
if ok {
if p.Contains(to) || p == to {
from = p.InputModuleAtIndex(outIndex)
outIndex = 0
}
}
p, ok = to.(Patch)
if ok {
if p.Contains(from) || p == from {
to = p.OutputModuleAtIndex(inIndex)
inIndex = 0
} else {
to = p.InputModuleAtIndex(inIndex)
inIndex = 0
}
}
from.AddOutputConnection(outIndex, &Connection{Module: to, Index: inIndex})
to.AddInputConnection(inIndex, &Connection{Module: from, Index: outIndex})
}
func (m *BaseModule) Disconnect() {
self := m.Self().(Module)
for inIndex, socket := range m.Inputs {
for _, conn := range socket.Connections {
conn.Module.RemoveOutputConnection(conn.Index, self, inIndex)
}
}
for outIndex, socket := range m.Outputs {
for _, conn := range socket.Connections {
conn.Module.RemoveInputConnection(conn.Index, self, outIndex)
}
}
}
func (m *BaseModule) DidSynthesize() bool {
return m.didSynthesize
}
func (m *BaseModule) SetDidSynthesize(didSynthesize bool) {
m.didSynthesize = didSynthesize
}
func (m *BaseModule) PrepareSynthesis() {
m.didSynthesize = false
// Clear input buffers first as we accumulate multiple inputs in one buffer
for _, input := range m.Inputs {
input.Buffer.Clear()
}
}
func (m *BaseModule) Synthesize() bool {
if m.didSynthesize {
return false
}
m.didSynthesize = true
for _, input := range m.Inputs {
// Accumulate connection outputs in single input
inputBuffer := input.Buffer
for _, conn := range input.Connections {
conn.Module.Synthesize()
for bufIndex, sample := range conn.Module.OutputAtIndex(conn.Index).Buffer {
inputBuffer[bufIndex] += sample
}
}
}
return true
}
func (m *BaseModule) MustSynthesize() bool {
return false
}
func (m *BaseModule) ReceiveMessage(msg any) []*Message {
// STUB
return nil
}
func (m *BaseModule) Exec(f func(any)) Module {
f(m.Self())
return m.Self().(Module)
}