/
synthi.js
222 lines (182 loc) · 5.25 KB
/
synthi.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
function ensureAudioContextRunning() {
var ctx = fluid.staticEnvironment.webAudioContextWrapper.context;
if (ctx.state !== 'running') {
ctx.resume();
}
}
var environment = flock.init({
numBuses: 48,
bufferSize: 4096
// TODO: Change bufferSize back to 1024 when Flocking adds support for
// WebAudio API native nodes.
// (see https://github.com/colinbdclark/Flocking/issues/146#issuecomment-226057461)
});
environment.start();
var buses = {
0: 39, // (unused bus)
1: 31,
2: 33,
3: 3,
4: 5,
5: 7,
6: 9,
7: 11,
8: 13,
9: 15,
10: 17,
11: 19,
12: 21,
13: 23,
14: 25,
15: 27,
16: 29
}
var components = [];
var currentBank = 0;
function Component(inputPins, outputPin, synthDef) {
if (synthDef.id === undefined) { synthDef.id = "main"; }
var enclosingDef = {
id: "output",
ugen: "flock.ugen.out",
sources: synthDef
};
if (outputPin !== null) { enclosingDef.bus = buses[outputPin]; }
this.init = function () {
this.inputPins = inputPins;
this.connectedPins = {};
this.synth = flock.synth({
synthDef: enclosingDef
});
components.push(this);
};
this.restart = function () {
this.synth.destroy();
this.synth = flock.synth({
synthDef: enclosingDef
});
this.moveToFront();
};
this.getAllConnectedPins = function () {
var allConnectedPins = [];
for (inputPin in this.connectedPins) {
allConnectedPins = allConnectedPins.concat(this.connectedPins[inputPin]);
}
return allConnectedPins;
};
this.set = function (property, value) {
this.synth.set("main."+property, value);
};
this.get = function (property) {
return this.synth.get("main."+property);
};
this.connect = function (inputPin, connectedPins) {
this.connectedPins[inputPin] = connectedPins;
var inputBuses = connectedPins.map(function (p) {return buses[p];});
this.synth.set("input-"+inputPin+".bus", inputBuses);
this.moveToFront();
};
this.moveToFront = function () {
environment.remove(this.synth);
environment.tail(this.synth);
components.filter(function (c) {return c.getAllConnectedPins().indexOf(outputPin) > -1})
.forEach(function (c) {c.moveToFront();});
};
this.init();
}
function connectPin(pin) {
ensureAudioContextRunning();
var inputPin = pin.attr("data-in");
var outputPin = pin.attr("data-out");
var allConnectionsToInput = $("#patches [data-in="+inputPin+"]:checked").map(function (i, x) {return parseInt($(this).attr("data-out"));}).get();
// console.log("Hooking up [" + allConnectionsToInput + "] to " + inputPin);
try {
components.filter(function (c) {return c.inputPins.indexOf(inputPin) > -1;})
.forEach(function (c) {c.connect(inputPin, allConnectionsToInput)});
} catch (e) {
// disconnect
pin.attr("checked", false);
// this was probably caused by a circular dependency issue (TODO: just disable pins that could cause circular dependencies)
if (e.message.indexOf("Maximum call stack size exceeded") > -1) {
throw "Circular dependency";
} else {
throw e;
}
}
}
function disconnectAllPins() {
$("#patches input").each(function (i, pin) {
if ($(pin).is(':checked')) {
$(pin).prop('checked', false);
connectPin($(pin));
}
});
}
function saveState() {
var dialValues = {};
$(".dial").each(function (i, x) {
dialValues[$(x).attr("id")] = parseInt($(".knob", x).attr("data-value"));
});
var pinValues = {};
$("#patches input").each(function (i,x) {
if ($(x).is(':checked')) {
pinValues[$(x).attr("data-in") + "-" + $(x).attr("data-out")] = true
}
});
return LZString.compressToBase64(JSON.stringify({
dials: dialValues,
pins: pinValues
}));
}
function loadState(state) {
console.log("Loading " + state + " ...");
var parsed = JSON.parse(LZString.decompressFromBase64(state));
// Dials
var dials = parsed.dials;
for (dialName in dials) {
var dialValue = dials[dialName];
($(".dial#" + dialName + " .knob").data("set"))(dialValue);
}
// Pins
var pins = parsed.pins;
disconnectAllPins(); // reset state of patchboard first!
for (pinName in pins) {
var pinIn = pinName.split("-")[0];
var pinOut = pinName.split("-")[1];
var checked = pins[pinName];
if (checked) {
var pin = $("#patches input[data-in="+pinIn+"][data-out="+pinOut+"]");
pin.prop('checked', true);
connectPin(pin);
}
}
}
function refreshPatchboard() {
// Used after manually triggering Envelope to clean up state.
var pins = {};
$("#patches input").each(function (i,x) {
if ($(x).is(':checked')) {
pins[$(x).attr("data-in") + "-" + $(x).attr("data-out")] = true
}
});
disconnectAllPins();
for (pinName in pins) {
var pinIn = pinName.split("-")[0];
var pinOut = pinName.split("-")[1];
var checked = pins[pinName];
if (checked) {
var pin = $("#patches input[data-in="+pinIn+"][data-out="+pinOut+"]");
pin.prop('checked', true);
connectPin(pin);
}
}
}
function storeToBank() {
localStorage["bank" + currentBank] = saveState();
}
function loadFromBank() {
ensureAudioContextRunning();
loadState(presetBanks[currentBank] || localStorage["bank" + currentBank]);
}
function clearBank() {
localStorage.removeItem("bank" + currentBank);
}