-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsox_convert.js
359 lines (307 loc) · 14.6 KB
/
sox_convert.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
/**
* Copyright 2020 Johannes Kropf
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
module.exports = function(RED) {
const { spawn } = require("child_process");
const { exec } = require("child_process");
const { execSync } = require("child_process");
const fs = require("fs");
function SoxConvertNode(config) {
RED.nodes.createNode(this,config);
this.statusTimer = false;
this.statusTimer2 = false;
this.argArr = [];
this.argArr1 = [];
this.argArr2 = [];
this.conversionType = config.conversionType;
this.wavMore = config.wavMore;
this.wavByteOrder = config.wavByteOrder;
this.wavEncoding = config.wavEncoding;
this.wavChannels = config.wavChannels;
this.wavRate = config.wavRate;
this.wavBits = config.wavBits;
this.flacMore = config.flacMore;
this.flacCompression = config.flacCompression;
this.flacChannels = config.flacChannels;
this.flacRate = config.flacRate;
this.flacBits = config.flacBits;
this.mp3More = config.mp3More;
this.mp3Channels = config.mp3Channels;
this.mp3Rate = config.mp3Rate;
this.mp3BitRate = config.mp3BitRate;
this.oggMore = config.oggMore;
this.oggCompression = config.oggCompression;
this.oggChannels = config.oggChannels;
this.oggRate = config.oggRate;
this.fileId = "";
this.filePath = "";
this.inputFilePath = "";
this.debugOutput = config.debugOutput;
this.outputToFile = config.outputToFile;
this.manualPath = config.manualPath;
this.partialPath = "";
this.checkPath = true;
this.system = true;
this.testFormat;
this.altPath = false;
var node = this;
function node_status(state1 = [], timeout = 0, state2 = []){
if (state1.length !== 0) {
node.status({fill:state1[1],shape:state1[2],text:state1[0]});
} else {
node.status({});
}
if (node.statusTimer !== false) {
clearTimeout(node.statusTimer);
node.statusTimer = false;
}
if (timeout !== 0) {
node.statusTimer = setTimeout(() => {
if (state2.length !== 0) {
node.status({fill:state2[1],shape:state2[2],text:state2[0]});
} else {
node.status({});
}
node.statusTimer = false;
},timeout);
}
}
function guessFormat(input){
const formats = [
[["aiff"],[0x46,0x4F,0x52,0x4D,0x00]],
[["wav"],[0x52,0x49,0x46,0x46]],
[["flac"],[0x66,0x4C,0x61,0x43]],
[["ogg"],[0x4F,0x67,0x67,0x53,0x00,0x02,0x00,0x00]],
[["mp3"],[0x49,0x44,0x33]],
[["mp3"],[0xFF,0xFB]]
];
const result = formats.filter(element => input.includes(Buffer.from(element[1])));
if (result.length === 0) { return false; }
return result[0][0];
}
function spawnConvert(msg, send, done){
let msg2 = {};
try{
node.soxConvert = spawn("sox",node.argArr);
}
catch (error) {
node_status(["error starting conversion command","red","ring"],1500);
(done) ? done(error) : node.error(error);
return;
}
node_status(["converting","blue","dot"]);
node.soxConvert.stderr.on('data', (data)=>{
msg2.payload = data.toString();
(send) ? send([null,msg2]) : node.send([null,msg2]);
});
node.soxConvert.on('close', function (code,signal) {
if (node.outputToFile === "buffer") {
msg.format = node.conversionType;
try {
msg.payload = fs.readFileSync(node.filePath);
} catch (error) {
node_status(["error","red","dot"],1500);
delete node.soxConvert;
(done) ? done("couldnt get tmp file after conversion") : node.error("couldnt get tmp file after conversion");
return;
}
} else {
msg.format = node.conversionType;
msg.payload = (node.altPath) ? node.altPath : node.filePath;
}
(send) ? send([msg,null]) : node.send([msg,null]);
(send) ? send([null,{payload:"complete"}]) : node.send([null,{payload:"complete"}]);
node_status(["finished","green","dot"],1500);
node.altPath = false;
delete node.soxConvert;
if (done) { done(); }
return;
});
node.soxConvert.stdout.on('data', (data)=>{
});
return;
}
node_status();
if (process.platform === 'linux') {
node.partialPath = (fs.existsSync('/dev/shm')) ?
"/dev/shm/" :
"/tmp/";
} else if (process.platform === "darwin") {
node.warn("im a mac")
node.partialPath = execSync('echo $TMPDIR');
node.partialPath = String(node.partialPath).trim();
} else {
node.system = false;
}
node.fileId = node.id.replace(/\./g,"");
if (node.outputToFile === "file" && !node.manualPath) {
node.error("did you forget to enter a file path? bing bing");
node_status(["file path error","red","dot"]);
node.checkPath = false;
} else if (node.outputToFile === "file") {
node.filePath = node.manualPath;
} else {
node.filePath = node.partialPath + node.fileId;
}
switch (node.conversionType) {
case "wav":
node.filePath += ".wav";
if (node.wavMore) {
node.argArr2 = [node.wavByteOrder,"-e",node.wavEncoding,"-c",node.wavChannels,"-r",node.wavRate,"-b",node.wavBits,node.filePath];
} else {
node.argArr2 = [node.filePath];
}
break;
case "flac":
node.filePath += ".flac";
if (node.flacMore) {
node.argArr2 = ["-C",node.flacCompression,"-c",node.flacChannels,"-r",node.flacRate,"-b",node.flacBits,node.filePath];
} else {
node.argArr2 = ["-C",node.flacCompression,node.filePath];
}
break;
case "mp3":
node.filePath += ".mp3";
if (node.mp3More) {
node.argArr2 = ["-c",node.mp3Channels,"-r",node.mp3Rate,"-C",node.mp3BitRate,node.filePath];
} else {
node.argArr2 = ["-C",node.mp3BitRate,node.filePath];
}
break;
case "ogg":
node.filePath += ".ogg";
if (node.oggMore) {
node.argArr2 = ["-c",node.oggChannels,"-r",node.oggRate,"-C",node.oggCompression,node.filePath];
} else {
node.argArr2 = ["-C",node.oggCompression,node.filePath];
}
break;
}
node.on('input', function(msg, send, done) {
if (!node.system) {
(done) ? done("Error. This node only works on Linux with ALSA and Sox.") : node.error("Error. This node only works on Linux with ALSA and Sox.");
node_status(["platform error","red","ring"]);
return;
}
if (node.soxConvert) {
node.warn("already converting, ignoring new input");
if (done) { done(); }
return;
}
if (!node.checkPath) {
(done) ? done("no file path") : node.error("no file path");
return;
}
if (typeof msg.payload === "string" && msg.payload.length === 0) {
(done) ? done("String input was empty") : node.error("String input was empty");
return;
}
if (msg.hasOwnProperty("filename")) {
if (typeof msg.filename !== "string") {
node.warn("ignoring msg.filename as it is not a string, msg.filename should be a legal path to a file in string format");
} else if (node.outputToFile !== "file") {
node.warn("ignoring msg.filename as it can only overwrite destination if in file output mode");
} else if (!msg.filename.match(node.conversionType)) {
node.warn("ignoring msg.filename as extension of msg.filepath has to match conversion type");
} else {
let swap = node.argArr2.length - 1;
node.argArr2.splice(swap, 1, msg.filename);
node.altPath = msg.filename;
}
}
node.inputFilePath = "";
if (Buffer.isBuffer(msg.payload)) {
if (msg.payload.length === 0) {
node_status(["error","red","dot"],1500);
(done) ? done("empty buffer") : node.error("empty buffer");
return;
}
const testBuffer = msg.payload.slice(0,8);
node.testFormat = guessFormat(testBuffer);
if (!node.testFormat) {
if (!msg.hasOwnProperty("format")) {
node_status(["error","red","dot"],1500);
(done) ? done("msg with a buffer payload also needs to have a coresponding msg.format property") : node.error("msg with a buffer payload also needs to have a coresponding msg.format property");
node_status(["error starting conversion command","red","ring"],1500);
return;
}
node.testFormat = msg.format;
}
node.inputFilePath = node.partialPath + node.fileId + "input." + node.testFormat;
try {
fs.writeFileSync(node.inputFilePath, msg.payload);
} catch (error) {
(done) ? done("couldnt write tmp file") : node.error("couldnt write tmp file");
node_status(["error","red","dot"],1500)
}
} else if (typeof msg.payload === "string") {
if (!fs.existsSync(msg.payload)) {
(done) ? done("this file doesnt exist") : node.error("this file doesnt exist");
node_status(["error","red","dot"],1500);
return;
}
node.inputFilePath = msg.payload;
}
node.argArr1 = [];
if (node.debugOutput) { node.argArr1.push("-V3"); }
if (node.testFormat === "raw") {
if (!msg.rate || !msg.encoding || !msg.bits || !msg.channels) {
(done) ? done("when converting raw audio you need to provide the following as properties of the incoming msg: the sample rate as msg.rate, the encoding as msg.encoding, the bits per sample as msg.bits and the channels of the audio as msg.channels") : node.error("when converting raw audio you need to provide the following as properties of the incoming msg: the sample rate as msg.rate, the encoding as msg.encoding, the bits per sample as msg.bits and the channels of the audio as msg.channels");
return;
}
let rawArr = ["-r",msg.rate,"-e",msg.encoding,"-b",msg.bits,"-c",msg.channels];
node.argArr1 = node.argArr1.concat(rawArr);
}
node.argArr1.push(node.inputFilePath);
if (msg.hasOwnProperty("options")) {
if (typeof msg.options !== "string") {
(done) ? done("options should be send as a single string including the additional arguments as per the sox commandline documentation.") : node.error("options should be send as a single string including the additional arguments as per the sox commandline documentation.");
return;
}
let options = msg.options.trim().split(" ");
node.argArr = node.argArr1.concat(node.argArr2, options);
delete msg.options;
} else {
node.argArr = node.argArr1.concat(node.argArr2);
}
spawnConvert(msg, send, done);
});
node.on("close",function() {
node_status();
if (node.system) {
const checkDir = node.partialPath;
try {
const files = fs.readdirSync(checkDir)
} catch (error) {
node.error(`clean up error: ${error}`);
}
files.forEach(file => {
if (file.match(node.fileId)) {
try {
fs.unlinkSync(checkDir + file);
} catch (error) {
node.error("couldnt delete leftover " + file);
}
}
return;
});
}
if(node.soxConvert) {
node.soxConvert.kill();
}
});
}
RED.nodes.registerType("sox-convert",SoxConvertNode);
}