-
Notifications
You must be signed in to change notification settings - Fork 2
/
Synesthesia.Graph.js
380 lines (298 loc) · 11.5 KB
/
Synesthesia.Graph.js
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
/*
Synesthesia:Graph
This module handles the internal representation of the graph. This
describes how nodes are connected, the ways those connections are
managed and the mechanism by which each of those connections has
the attributes associated with it propagated through the graph.
Propagation methods depend upon the type of information and which
node is oriented as the source or destination for that type.
This module should not have an associated CSS file.
No part of this file should make reference to any type of display mechanism or properties.
*/
module.declare("Synesthesia:Graph",
["Utilities"],
function () {
var Utilities = module.require("Utilities");
var Graph = {};
Graph.Endpoint = (function () {
function Endpoint (params) {
this.params = (typeof params !== "undefined" ? params : {});
this.node = this.params.node;
if (!Utilities.conforms(Graph.Node, this.node)) {
throw new Error("Graph.Endpoint: Given object is not a valid node.");
}
this.name = this.params.name;
this.type = this.params.type;
if (Endpoint.Types.indexOf(this.type) == -1) {
throw new Error("Graph.Endpoint: Invalid type '" + this.type + "'.");
}
this.accepted_types = this.params.accepted_types;
for (var i = 0; i < this.accepted_types.length; i++) {
if (Endpoint.Types.indexOf(this.accepted_types[i]) == -1) {
throw new Error("Graph.Endpoint: Invalid accepted type '" + this.accpeted_types[i] + "'.");
}
}
this.direction = this.params.direction;
if (Endpoint.Directions.indexOf(this.direction) == -1) {
throw new Error("Graph.Endpoint: Invalid direction.");
}
this.max_connections = this.params.max_connections || Infinity;
this.connections = [];
}
Endpoint.Types = [
"notes",
"AudioNode",
"AudioParam",
"canvas" // Do audio first!
];
Endpoint.Directions = [
"input",
"output"
];
Endpoint.prototype.getNode = function () {
return this.node;
};
Endpoint.prototype.getName = function () {
return this.name;
};
Endpoint.prototype.getType = function () {
return this.type;
};
Endpoint.prototype.getAcceptedTypes = function () {
return this.accepted_types;
};
Endpoint.prototype.getMaxConnections = function () {
return this.max_connections;
};
Endpoint.prototype.getDirection = function () {
return this.direction;
};
Endpoint.prototype.getConnections = function () {
return [].concat(this.connections);
};
Endpoint.prototype.canConnectTo = function (other_endpoint) {
// Can't be the same direction.
if (this.direction == other_endpoint.getDirection()) return false;
// Other endpoint must accept this endpoint type.
if (other_endpoint.getAcceptedTypes().indexOf(this.type) == -1) return false;
// Other endpoint can't be maxed out.
if (other_endpoint.getConnections().length >= other_endpoint.getMaxConnections()) return false;
// This endpoint must accept other endpoint type.
if (this.accepted_types.indexOf(other_endpoint.getType()) == -1) return false;
// This endpoint can't be maxed out.
if (this.connections.length >= this.max_connections) return false;
return true;
};
Endpoint.prototype.informConnected = function (new_connection) {
this.connections.push(new_connection);
this.node.informConnected(this, new_connection);
};
Endpoint.prototype.informDisconnected = function (rm_connection) {
if (this.connections.indexOf(rm_connection) != -1) {
this.connections.splice(
this.connections.indexOf(rm_connection),
1
);
}
this.node.informDisconnected(this, rm_connection);
};
return Endpoint;
})();
Graph.Connection = (function () {
function Connection (params) {
this.params = (typeof params !== "undefined" ? params : {});
this.from_endpoint = this.params.from_endpoint;
this.to_endpoint = this.params.to_endpoint;
/*
console.log("from");
console.log(this.from_endpoint);
console.log("to");
console.log(this.to_endpoint);
*/
}
Connection.prototype.setFromEndpoint = function (from_endpoint) {
this.from_endpoint = from_endpoint;
};
Connection.prototype.setToEndpoint = function (to_endpoint) {
this.to_endpoint = to_endpoint;
};
Connection.prototype.getOppositeEndpoint = function (endpoint) {
if (endpoint == this.from_endpoint) {
return this.to_endpoint;
} else if (endpoint == this.to_endpoint) {
return this.from_endpoint;
} else {
return null;
}
};
return Connection;
})();
Graph.Node = (function () {
function Node (params) {
if (!params) return; // INTERFACE
this.input_descriptors = {};
this.output_descriptors = {};
}
Node.prototype.setInputDescriptors = function (descriptors) {
this.input_descriptors = descriptors;
};
Node.prototype.setOutputDescriptors = function (descriptors) {
this.output_descriptors = descriptors;
};
/*
Requests an array containing objects describing the inputs / outputs.
The objects in this array are of type Graph.EndpointDescriptor
*/
Node.prototype.getInputDescriptors = function () {
var o = new Object();
Utilities.copy_properties(this.input_descriptors, o);
return o;
};
Node.prototype.getOutputDescriptors = function () {
var o = new Object();
Utilities.copy_properties(this.output_descriptors, o);
return o;
};
Node.prototype.getInputDescriptorsArray = function () {
var descriptors = [];
for (var descriptor_name in this.input_descriptors) {
if (!this.input_descriptors.hasOwnProperty(descriptor_name)) continue;
descriptors.push(this.input_descriptors[descriptor_name]);
}
return descriptors;
};
Node.prototype.getOutputDescriptorsArray = function () {
var descriptors = [];
for (var descriptor_name in this.output_descriptors) {
if (!this.output_descriptors.hasOwnProperty(descriptor_name)) continue;
descriptors.push(this.output_descriptors[descriptor_name]);
}
return descriptors;
};
// Informs the node that a given connection has been connected to / disconnected from the given endpoint.
Node.prototype.informConnected = function (endpoint, connection) {
throw new Error("Graph.Node(.informConnected): Not implemented.");
};
Node.prototype.informDisconnected = function (endpoint, connection) {
throw new Error("Graph.Node(.informDisconnected): Not implemented.");
};
return Node;
})();
Graph.Node.NoteSourceNode = (function () {
function NoteSourceNode (params) {
if (!params) return;
Graph.Node.apply(this, arguments);
if (!Utilities.overrides(Graph.Node.NoteSourceNode, this)) {
console.error("Graph.Node.NoteSourceNode: Subclass does not override.");
}
}
NoteSourceNode.GlobalConnectionMap = new Utilities.Map();
NoteSourceNode.prototype = Utilities.extend(
new Graph.Node()
);
NoteSourceNode.prototype.connectToNoteDestination = function (destination_node) {
var connections = NoteSourceNode.GlobalConnectionMap.get(this);
if (!connections) {
connections = [];
NoteSourceNode.GlobalConnectionMap.set(this, connections);
}
if (connections.indexOf(destination_node) == -1) {
connections.push(destination_node);
} else {
console.warn("Graph.Node.NoteSourceNode(.connectToNoteDestination): Already connected to the requested node.");
}
};
NoteSourceNode.prototype.disconnectFromNoteDestination = function (destination_node) {
var connections = NoteSourceNode.GlobalConnectionMap.get(this);
if (!connections) {
throw new Error("Graph.Node.NoteSourceNode(.disconnectFromNoteDestination): Disconnect requested on a node that was not known to be connected.");
}
while (connections.indexOf(destination_node) != -1) {
connections.splice(connections.indexOf(destination_node), 1);
}
};
NoteSourceNode.prototype.distributeNotes = function (notes) {
var connections = NoteSourceNode.GlobalConnectionMap.get(this);
if (!connections) return;
for (var i = 0; i < connections.length; i++) {
connections[i].handleNotes(notes);
}
};
return NoteSourceNode;
})();
Graph.Node.NoteDestinationNode = (function () {
function NoteDestinationNode (params) {
if (!params) return;
if(!Utilities.overrides(Graph.Node.NoteDestinationNode, this)) {
console.error("Graph.Node.NoteDestinationNode: Subclass does not override.");
}
}
NoteDestinationNode.prototype.handleNotes = function (notes) {
throw new Error("Graph.Node.NoteDestinationNode(.handleNotes): Not implemented.");
};
return NoteDestinationNode;
})();
Graph.Node.AudioNode = (function () {
function AudioNode () {
Graph.Node.apply(this, arguments);
}
AudioNode.prototype = Utilities.extend(
new Graph.Node()
);
return AudioNode;
})();
Graph.Node.AudioSourceNode = (function () {
function AudioSourceNode () {
Graph.Node.AudioNode.apply(this, arguments);
this.src_dest_map = new Utilities.Map();
}
AudioSourceNode.prototype = Utilities.extend(
new Graph.Node.AudioNode()
);
AudioSourceNode.GlobalConnectionMap = new Utilities.Map();
AudioSourceNode.prototype.connectSourceToDestination = function (source_node, destination_node) {
var connections = AudioSourceNode.GlobalConnectionMap.get(source_node);
if (!connections) {
connections = [];
AudioSourceNode.GlobalConnectionMap.set(source_node, connections);
}
// Prevent copies of connections.
if (connections.indexOf(destination_node) != -1) {
return;
}
connections.push(destination_node);
source_node.connect(destination_node);
};
AudioSourceNode.prototype.disconnectSourceFromDestination = function (source_node, destination_node) {
var connections = AudioSourceNode.GlobalConnectionMap.get(source_node);
if (!connections || connections.length == 0) {
throw new Error("Graph.Node.AudioSourceNode(.disconnectSourceFromDestination): Disconnect requested on a node that was not known to be connected.");
}
// Remove all copies (only one expected) of the connection.
while (connections.indexOf(destination_node) != -1) {
connections.splice(connections.indexOf(destination_node), 1);
}
// Disconnect the source node (from all).
source_node.disconnect();
// Reconnect all other nodes.
for (var i = 0; i < connections.length; i++) {
source_node.connect(connections[i]);
}
};
return AudioSourceNode;
})();
Graph.Node.AudioDestinationNode = (function () {
function AudioDestinationNode () {
Graph.Node.AudioNode.apply(this, arguments);
this.destination_map = new Utilities.Map();
}
AudioDestinationNode.prototype = Utilities.extend(
new Graph.Node.AudioNode()
);
AudioDestinationNode.prototype.getDestinationForInput = function (input_endpoint) {
throw new Error("Graph.Node.AudioDestinationNode(.getDestinationForInput): Not implemented.");
};
return AudioDestinationNode;
})();
return Graph;
});