-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjack_client.js
331 lines (298 loc) · 10.6 KB
/
jack_client.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
/**
* Query JACK info and status. uses https://github.com/ajboni/jack-audio-tools
* @module jack_client
*/
let store = require("./store");
const Layout = require("./layout");
const PubSub = require("pubsub-js");
let jack;
PubSub.subscribe("jack", function (jackStatus) {
jack = jackStatus;
});
/**
* Initialize. Set up polling.
*
*/
function init() {
// queryJack();
updateStatus();
poll();
}
/**
* Poll JACK for info.
* @see settings
*/
function poll() {
if (store.getJackStatus().JACK_STATUS.status === "running") {
updateStatus();
} else {
throw "JACK IS NOT RUNNING";
}
}
function updateStatus() {
store.setJackStatus(queryJack(), queryTransport(), getAvailableJackPorts());
}
/**
* Get several Jack Statuses.
* @returns Returns a json object with each property: status, cpu_load, block_size, realtime, sample_rate
*/
function queryJack() {
const cp = require("child_process");
const status = cp.execSync(
"python3 ./py/calvo_cli_tools/jack_tools/client.py -c calvo-poll query",
{ encoding: "utf8" }
);
return JSON.parse(status);
}
/**
* Queries JACK transport to get status.
*
* @returns Returns a JSON object with the JACK transport status.
*/
function queryTransport() {
const cp = require("child_process");
const status = cp.execSync(
`python3 ./py/calvo_cli_tools/jack_tools/transporter.py -c calvo-poll query`,
{ encoding: "utf8" }
);
return JSON.parse(status);
}
function setTransportStatus(status) {
// choices=['query', 'rewind', 'start', 'status', 'stop', 'toggle'],
}
/**
* Get all available JACK audio/midi input/output ports.
*
* @returns Returns a JSON object with all available ports.
*/
function getAvailableJackPorts() {
const cp = require("child_process");
const info = cp.execSync(
`python3 ./py/calvo_cli_tools/jack_tools/client.py -c calvo-poll port-info`,
{ encoding: "utf8" }
);
return JSON.parse(info);
}
const queue = {
connectPorts: [],
};
function addCommandToQueue(prop, command) {
if (!queue[prop]) {
queue[prop] = [];
}
queue[prop].push(command);
}
function processQueue(prop) {
if (!queue[prop]) {
store.wlogError(`JACK: Queue ${prop} does not exist.`);
return;
}
const items = queue[prop];
const command = items.join(" & ");
const cp = require("child_process");
try {
const status = cp.execSync(command, { encoding: "utf8" });
queue[prop] = [];
} catch (error) {
store.wlogError(error);
}
// items.slice().forEach((element) => {});
}
/**
* connect <origin_port> to <destination_port>
* connect two jack ports together. Both ports must be same type.
* @param {string} src Origin port: Must be an output port.
* @param {string} dst Destination port: Must be an Input port
*/
async function connectPorts(
src,
dst,
disconnect = false,
quiet = true,
addToQueue = false
) {
const cp = require("child_process");
const rm = disconnect ? "--disconnect" : "";
const q = quiet ? "--quiet" : "";
try {
const command = `python3 ./py/calvo_cli_tools/jack_tools/port_tools.py connect "${src}" "${dst}" ${rm} ${q}`;
if (addToQueue) {
addCommandToQueue("connectPorts", command);
} else {
const status = cp.execSync(command, { encoding: "utf8" });
}
} catch (error) {
store.wlogError(error);
}
}
/**
*
* It will (dis) connect two plugins, dealing with mono/stereo conversions.
* Use 'input|output' to use a mono/stero I/O ports as src/dst.
* @param {plugin} src Source plugin instance. It should exist on the RACK. It will use the plugin's output ports.
* @param {plugin} dst Destination plugin.It should exist on the RACK. It will use the plugin's input ports.
* @param {boolean} [disconnect=false] Disconnect plugins
* @param {boolean} [addToQueue=false] If true, add it to the 'connectPlugins' queue instead of inmediately firing. Queue should be processed manually after.
*/
function connectPlugins(src, dst, disconnect = false, addToQueue = false) {
const il = store.app.SETTINGS.INPUT_L;
const ir = store.app.SETTINGS.INPUT_R;
const im = store.app.SETTINGS.INPUT_MODE;
const ol = store.app.SETTINGS.OUTPUT_L;
const or = store.app.SETTINGS.OUTPUT_R;
const om = store.app.SETTINGS.OUTPUT_MODE;
const instanceName = `calvo_${store.app.APP_ID}`;
// Direct Monitor not supported right now (It may conflict on multi instance mode.)
if (src === "input" && dst === "output") {
store.wlogError(
"Direct Monitor not supported right now (It may conflict on multi instance mode.)"
);
return;
}
// Input => Plugin
if (src === "input") {
if (!dst.ports.audio.input || dst.ports.audio.input.length === 0) {
store.wlogWarning(
`${dst.name} is breaking the audio chain as it does not have any input audio port.`
);
return;
}
const dstMode = dst.ports.audio.input.length === 1 ? "mono" : "stereo";
const dl = `${instanceName}_${dst.info.instanceNumber}_${dst.info.safeName}:${dst.ports.audio.input[0].symbol}`;
const dr =
dstMode === "stereo"
? `${instanceName}_${dst.info.instanceNumber}_${dst.info.safeName}:${dst.ports.audio.input[1].symbol}`
: "";
if (im === "mono" && dstMode === "mono") {
connectPorts(il, dl, disconnect, true, addToQueue);
} else if (im === "mono" && dstMode === "stereo") {
connectPorts(il, dl, disconnect, true, addToQueue);
connectPorts(il, dr, disconnect, true, addToQueue);
} else if (im === "stereo" && dstMode === "mono") {
connectPorts(il, dl, disconnect, true, addToQueue);
connectPorts(ir, dl, disconnect, true, addToQueue);
} else if (im === "stereo" && dstMode === "stereo") {
connectPorts(il, dl, disconnect, true, addToQueue);
connectPorts(ir, dr, disconnect, true, addToQueue);
}
}
// Plugin => Output
else if (dst === "output") {
if (!src.ports.audio.output || src.ports.audio.output.length === 0) {
store.wlogWarning(
`${src.name} is breaking the audio chain as it does not have any output audio port.`
);
return;
}
const srcMode = src.ports.audio.output.length === 1 ? "mono" : "stereo";
const sl = `${instanceName}_${src.info.instanceNumber}_${src.info.safeName}:${src.ports.audio.output[0].symbol}`;
const sr =
srcMode === "stereo"
? `${instanceName}_${src.info.instanceNumber}_${src.info.safeName}:${src.ports.audio.output[1].symbol}`
: "";
if (srcMode === "mono" && om === "mono") {
connectPorts(sl, ol, disconnect, true, addToQueue);
} else if (srcMode === "mono" && om === "stereo") {
connectPorts(sl, ol, disconnect, true, addToQueue);
connectPorts(sl, or, disconnect, true, addToQueue);
} else if (srcMode === "stereo" && om === "mono") {
connectPorts(sl, ol, disconnect, true, addToQueue);
connectPorts(sr, ol, disconnect, true, addToQueue);
} else if (srcMode === "stereo" && om === "stereo") {
connectPorts(sl, ol, disconnect, true, addToQueue);
connectPorts(sr, or, disconnect, true, addToQueue);
}
}
// Plugin => Plugin
else {
if (!src.ports.audio.output || src.ports.audio.output.length === 0) {
store.wlogWarning(
`${src.name} ]is breaking the audio chain as it does not have any output audio port.`
);
return;
}
if (!dst.ports.audio.input || dst.ports.audio.input.length === 0) {
store.wlogWarning(
`${dst.name} is breaking the audio chain as it does not have any output audio port.`
);
return;
}
const srcMode = src.ports.audio.output.length === 1 ? "mono" : "stereo";
const sl = `${instanceName}_${src.info.instanceNumber}_${src.info.safeName}:${src.ports.audio.output[0].symbol}`;
const sr =
srcMode === "stereo"
? `${instanceName}_${src.info.instanceNumber}_${src.info.safeName}:${src.ports.audio.output[1].symbol}`
: "";
const dstMode = dst.ports.audio.input.length === 1 ? "mono" : "stereo";
const dl = `${instanceName}_${dst.info.instanceNumber}_${dst.info.safeName}:${dst.ports.audio.input[0].symbol}`;
const dr =
dstMode === "stereo"
? `${instanceName}_${dst.info.instanceNumber}_${dst.info.safeName}:${dst.ports.audio.input[1].symbol}`
: "";
if (srcMode === "mono" && dstMode === "mono") {
connectPorts(sl, dl, disconnect, true, addToQueue);
} else if (srcMode === "mono" && dstMode === "stereo") {
connectPorts(sl, dl, disconnect, true, addToQueue);
connectPorts(sl, dr, disconnect, true, addToQueue);
} else if (srcMode === "stereo" && dstMode === "mono") {
connectPorts(sl, dl, disconnect, true, addToQueue);
connectPorts(sr, dl, disconnect, true, addToQueue);
} else if (srcMode === "stereo" && dstMode === "stereo") {
connectPorts(sl, dl, disconnect, true, addToQueue);
connectPorts(sr, dr, disconnect, true, addToQueue);
}
}
}
/**
* Alias for connect(src, dst, { disconnect: true })
* It will (dis) connect two plugins, dealing with mono/stereo conversions.
* Use 'input|output' to use a mono/stero I/O ports as src/dst.
* @param {plugin} src Source plugin instance. It should exist on the RACK. It will use the plugin's output ports.
* @param {plugin} dst Destination plugin.It should exist on the RACK. It will use the plugin's input ports.
* @param {boolean} [disconnect=true]
*/
function disconnectPlugins(src, dst, addToQueue = false) {
connectPlugins(src, dst, true, addToQueue);
}
/**
* Disconnect all plugin ports from any target.
*
* @param {plugin} plugin Plugin Instance
* @param {string} direction [all|input|output] What ports to disconnect.
*
*/
function clearPluginPorts(plugin, direction = "all", addToQueue = false) {
const cp = require("child_process");
const arr = [];
if (direction === "all" || direction === "input") {
arr.push(...plugin.ports.audio.input);
}
if (direction === "all" || direction === "output") {
arr.push(...plugin.ports.audio.output);
}
const instanceName = `calvo_${store.app.APP_ID}`;
const ports = arr
.map(
(port) =>
`${instanceName}_${plugin.info.instanceNumber}_${plugin.info.safeName}:${port.symbol}`
)
.join(",");
// store.wlog(ports);
try {
command = `python3 ./py/calvo_cli_tools/jack_tools/port_tools.py clear ${ports}`;
if (addToQueue) {
addCommandToQueue("clearPluginPorts", command);
} else {
const status = cp.execSync(command, { encoding: "utf8" });
}
} catch (error) {
store.wlogError(error.toString());
}
}
exports.init = init;
exports.poll = poll;
exports.queryTransport = queryTransport;
exports.connectPlugins = connectPlugins;
exports.disconnectPlugins = disconnectPlugins;
exports.clearPluginPorts = clearPluginPorts;
exports.processQueue = processQueue;