-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
309 lines (253 loc) · 7.9 KB
/
index.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
const { exec, spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const osc = require('osc');
const EventEmitter = require('events');
const wrapperBegin = `
#N canvas 0 23 450 300 10;
#X obj 10 10 r __to_pd__;
`;
const wrapperEnd = `
#X obj 10 110 list trim;
#X obj 10 130 s __from_pd__;
#X connect 0 0 1 0;
#X connect 1 0 2 0;
#X connect 2 0 3 0;
#X connect 3 0 4 0;
#X connect 4 0 5 0;
`;
function isNumeric(x) { return (typeof x == 'number' && !isNaN(x)); }
function isInteger(x) { return (isNumeric(x) && Number.isInteger(x)); }
////////// PURE DATA PATCH WRAPPER CLASS //////////
class Patch extends EventEmitter {
constructor(id, pd) {
super();
this.id = id;
this.pd = pd;
}
send(...args) {
let sendString = `send ${this.id}`;
args.forEach((arg) => { sendString += ` ${arg}`; });
sendString += ';\n';
this.pd.pdsend.stdin.write(sendString);
}
sendUdp(...args) {
if (!this.pd.useUdp) return;
const typedArgs = [{ type: 's', value: this.id }];
args.forEach((arg) => {
let typedArg = { value: arg };
if (isNumeric(arg)) {
if (isInteger(args)) { typedArg.type = 'i'; }
else { typedArg.type = 'f'; }
} else if (typeof arg == 'string' ) {
typedArg.type = 's';
}
typedArgs.push(typedArg);
});
this.pd.osc.send({ address: '/all', args: typedArgs });
}
}
const defaultPdParameters = {
pdOptions: [],
useUdp: false,
pdsendPort: 3030,
pdreceivePort: 3031,
udpsendPort: 8000,
udpreceivePort: 8001,
tmpDir: 'tmp',
};
////////// PURE DATA MAIN WRAPPER CLASS //////////
class Pd {
static async init(binFolder, opts = {}) {
const {
pdOptions,
useUdp,
pdsendPort,
pdreceivePort,
udpsendPort,
udpreceivePort,
tmpDir
} = Object.assign(defaultPdParameters, opts);
this.binFolder = binFolder;
this.pdOptions = pdOptions;
this.pdsendPort = pdsendPort;
this.pdreceivePort = pdreceivePort;
this.useUdp = useUdp;
this.udpsendPort = udpsendPort;
this.udpreceivePort = udpreceivePort;
this.osc = null;
this.pd = null;
this.pdsend = null;
this.pdreceive = null;
this.patches = {};
this.tmpDir = path.join(__dirname, tmpDir);
this.uuid = 0;
if (!fs.existsSync(this.tmpDir)) {
fs.mkdirSync(this.tmpDir);
}
const files = fs.readdirSync(this.tmpDir);
files.forEach((file) => { fs.unlinkSync(path.join(this.tmpDir, file)); });
await this.start();
}
static async start() {
return new Promise(async (resolve, reject) => {
this._start()
.then(() => {
//////////////////// PDSEND PROCESS
this.pdsend = spawn(path.join(this.binFolder, 'pdsend'), [`${this.pdsendPort}`]);
resolve();
}, async () => {
await this._kill('pdsend');
await this._kill('pd');
await this._kill('pdreceive');
if (this.osc !== null) {
this.osc.close();
delete this.osc;
this.osc = null;
}
await this._sleep(5000);
await this.start();
resolve();
});
});
}
static async _start() {
if (this.useUdp) {
await this._startUdp();
}
await this._startPdReceive();
}
/**
* Start osc udp class to send udp messages to pd
* (and check for recurring EADDRINUSE errors)
*/
static async _startUdp() {
return new Promise((resolve, reject) => {
this.osc = new osc.UDPPort({
localAddress: '0.0.0.0',
localPort: this.udpreceivePort,
remoteAddress: '127.0.0.1',
remotePort: this.udpsendPort,
});
// todo (backwards udp communication) :
// this.osc.on('bundle', (oscBundle, timeTag, info) => {});
this.osc.on('ready', () => { /* console.log('udp ready'); */ resolve(); });
this.osc.on('error', (err) => { /* console.log('udp error'); */ reject(); });
this.osc.open();
});
}
/**
* Start pd process
*/
static _startPd() {
const _netsendPath = path.join(__dirname, '_pd-netsend');
const _netreceivePath = path.join(__dirname, '_pd-netreceive');
const netsendPath = this._createSendReceiveWrapper(_netsendPath, 'sender', `${this.pdreceivePort}`);
const netreceivePath = this._createSendReceiveWrapper(_netreceivePath, 'receiver', `${this.pdsendPort}`, `${this.udpsendPort}`);
const options = this.pdOptions.concat([
'-nogui', '-noprefs',
'-open', `${netsendPath}`, `${netreceivePath}`,
]);
this.pd = spawn(path.join(this.binFolder, 'pd'), options);
// print console output (yes, pd's console output goes to stderr !)
this.pd.stderr.on('data', (data) => { console.log(`pd : ${data}`); });
}
/**
* Start pdsend binary
*/
static _startPdSend() {
this.pdsend = spawn(path.join(this.binFolder, 'pdsend'), [`${this.pdsendPort}`]);
}
/**
* Start pdreceive binary
* (and check for recurrent "Address already in use" errors)
*/
static async _startPdReceive() {
return new Promise((resolve, reject) => {
this.pdreceive = spawn(path.join(this.binFolder, 'pdreceive'), [`${this.pdreceivePort}`]);
this.pdreceive.stdout.on('data', (data) => {
if (`${data}` === 'initialized;\n') {
resolve();
} else {
this._routeMessage(data);
}
});
this.pdreceive.stderr.on('data', async (data) => {
console.log(`pdreceive stderr : ${data}`);
reject();
});
this._startPd();
});
}
/**
* Open a patch
* todo : make async and resolve on patch loaded acknowledgement ?
* (early udp messages are missed because sent too fast)
*/
static open(patchPath, ...args) {
const id = `id-${this.uuid}`; // `id-${Date.now()}`;
this.uuid++;
const fullPath = this._createPatchWrapper(patchPath, id, ...args);
const filedir = path.dirname(fullPath);
const filename = path.basename(fullPath);
this.patches[id] = new Patch(id, this);
this.pdsend.stdin.write(`open ${filename} ${filedir};\n`);
// todo ?
// this.patches[id].on('message', () => { /* receive load ack */ });
return this.patches[id];
}
/**
* Close a patch
*/
static close(patch) {
const id = patch.id;
this.pdsend.stdin.write(`close ${id}.pd;\n`);
}
////////// PRIVATE METHODS
static _routeMessage(msg) {
let msgs = `${msg}`.split(';\n').map(atoms => atoms.split(' '));
msgs.forEach((atoms) => {
const id = atoms.splice(0, 1)[0];
if (this.patches[id]) {
this.patches[id].emit('message', atoms);
}
});
}
static _createSendReceiveWrapper(originalPatchPath, id, ...args) {
let patchContents = '#N canvas 50 50 400 300 10;\n#X obj 10 10';
patchContents += ` ${originalPatchPath}`;
args.forEach((arg) => { patchContents += ` ${arg}`; });
return this._createWrapperFile(id, patchContents);
}
static _createPatchWrapper(originalPatchPath, id, ...args) {
let patchContents = wrapperBegin;
patchContents += `#X obj 10 30 route ${id};\n`
patchContents += `#X obj 10 50 ${originalPatchPath} ${id}`;
args.forEach((arg) => { patchContents += ` ${arg}`; });
patchContents += ';\n';
patchContents += `#X obj 10 90 list prepend ${id};`;
patchContents += wrapperEnd;
return this._createWrapperFile(id, patchContents);
}
static _createWrapperFile(id, contents) {
const patchPath = path.join(this.tmpDir, `${id}.pd`);
fs.writeFileSync(patchPath, contents);
return patchPath;
}
static _deleteWrapperFile(id) {
const patchPath = path.join(__dirname, this.tmpDir, `${id}.pd`);
fs.unlinkSync(patchPath);
}
static async _kill(procName) {
return new Promise((resolve, reject) => {
const e = exec(`killall ${procName}`);
e.on('exit', () => { resolve(); });
});
}
static async _sleep(duration) {
return new Promise((resolve, reject) => {
setTimeout(() => { resolve(); }, duration);
});
}
}
module.exports = Pd;