-
Notifications
You must be signed in to change notification settings - Fork 1
/
Spleeter.js
522 lines (398 loc) · 13.9 KB
/
Spleeter.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
author: zak45
date: 07/02/2023
version:1.3.0
Chataigne Module for Deezer /spleeter
This will separate audio file into stems ( 2 / 4 / 5 )
work with mp3 file.
Mainly used to separate vocals from a song.
Need python / tensorflow // ffmpeg ffprobe
*/
// Main module parameters
var shouldProcessSpl = false;
var homeDIR = "";
var winHOME = "";
var sequence = "";
var targetFile = "";
var model = "";
var newAudio = "";
// spleeter cmd file name (required)
var spleeterCMDName = "spleeter.cmd";
// check spleeter
var spleeterIsRunning = false;
// CMD options
var splOutputOptions = " separate -c mp3 ";
// verbose
var verbose = " -verbose";
// Output folder location, if blank, set it to audio_output
var splOutputFolder = "audio_output";
// stems
var splStems = "spleeter:2stems";
// TEMP dir (required)
var tempDIR = "";
// init test
var isInit = true;
//SCAnalyzer
var SCAexist = root.modules.getItemWithName("SCAnalyzer");
// increase timeout
script.setExecutionTimeout(300);
//We create necessary entries in modules & sequences. We need OS / sound card and Sequence with Trigger / Audio.
function init()
{
script.log("-- Custom command called init()");
// we check required TMP folder
spTmpPath();
script.log("Temp folder : "+tempDIR);
if (tempDIR == "")
{
util.showMessageBox("Spleeter !", "TMP, TMPDIR or TEMP env variable not found", "warning", "OK");
}
var infos = util.getOSInfos();
script.log("Hello "+infos.username);
script.log("We run under : "+infos.name);
if ( infos.name.contains("Win") )
{
homeDIR = util.getEnvironmentVariable("USERPROFILE") + "/Documents";
winHOME = util.getEnvironmentVariable("USERPROFILE");
} else {
homeDIR = util.getEnvironmentVariable("$HOME");
}
//
script.setUpdateRate(1);
}
function update()
{
// init once
if (isInit)
{
isInit = false;
// modules test
var OSexist = root.modules.getItemWithName("OS");
var SCexist = root.modules.getItemWithName("Sound Card");
var SPLexist = root.modules.getItemWithName("Spleeter");
if (SCexist.name == "soundCard" || SPLexist.name == "spleeter")
{
script.log("Module Sound Card exist");
} else {
var newSCModule = root.modules.addItem("Sound Card");
}
if (OSexist.name == "os")
{
script.log("Module OS exist");
} else {
var newOSModule = root.modules.addItem("OS");
}
// test sequence
var SQexist = root.sequences.getItemWithName("Sequence");
if (SQexist.name == "sequence")
{
script.log("Sequences Sequence exist");
} else {
var newSequence = root.sequences.addItem();
var newTSequence = newSequence.layers.addItem("Trigger");
var newASequence = newSequence.layers.addItem("Audio");
}
local.parameters.spleeterParams.spleeterCommand.set(homeDIR+"/Chataigne/modules/Spleeter/spleeter.cmd");
if (SCAexist.name == "sCAnalyzer")
{
script.log("SCAnalyzer present");
// avoid Chataigne Crash
//root.modules.sCAnalyzer.scripts.sCAnalyzer.reload.trigger();
var Spleetercontainer = SCAexist.parameters.getChild("Spleeter Params");
Spleetercontainer.setCollapsed(false);
} else {
script.log('No SCAnalyzer found');
}
} else {
// start long pocess on it's own thread to run in blocking mode but not block the main UI
if (shouldProcessSpl)
{
shouldProcessSpl = false;
runSpleeter (sequence, targetFile, model);
}
}
}
function scriptParameterChanged (param)
{
script.log("Script Param changed : "+param.name);
}
function moduleParameterChanged (param)
{
script.log("Module Param changed : "+param.name);
if (param.name == "spleeterInfo") {
util.gotoURL('https://research.deezer.com/projects/spleeter.html');
}
}
function moduleValueChanged (value)
{
script.log("Module value changed : "+value);
}
// check to see if something to do
function Spleeter (insequence, intargetFile, inmodel)
{
if (insequence == "" && intargetFile == "" )
{
script.log('Nothing to do');
} else {
sequence = insequence;
targetFile = intargetFile;
model = inmodel;
util.showMessageBox("Spleeter", "This could take a while ...", "warning", "ok");
shouldProcessSpl = true;
}
}
// Run Spleeter: wait and read output folder files to create audio entry into sequence
function runSpleeter (sequence, targetFile, model)
{
// Spleeter command
spleeterCMDName = local.parameters.spleeterParams.spleeterCommand.getAbsolutePath();
// check to see if Spleeter exe exist
if (util.fileExists(spleeterCMDName) == 1)
{
spleeterIsRunning = true;
// options to add
splOutputOptions = local.parameters.spleeterParams.spleeterOptions.get();
// result output folder
splOutputFolder = local.parameters.spleeterParams.outputFolder.get();
// if output folder not set, we set it as default
if (splOutputFolder == "")
{
splOutputFolder = tempDIR + "/" + "audio_output";
}
splStems = model;
// we have some work to do, target first else file
if (sequence.contains("/"))
{
script.log('Retreive data from sequence : ' + sequence);
var splitseq = sequence.split("/");
var sequenceName = splitseq[2];
var audioName = splitseq[4];
// set newSequence to existing one
var newSequence = root.sequences.getItemWithName(sequenceName);
// set newAudio to existing one
newAudio = newSequence.layers.getItemWithName(audioName);
// targetFile
var targetFile = newAudio.clips.audioClip.filePath.getAbsolutePath();
script.log('Target file from sequence : ' + targetFile);
} else if (targetFile != ""){
script.log('Create new sequence from filename :' + targetFile);
// create new sequence / audio clip
var newSequence = root.sequences.addItem('Sequence');
newAudio = newSequence.layers.addItem('Audio');
var newAudioClip = newAudio.clips.addItem('audioClip');
newAudioClip.filePath.set(targetFile);
} else {
script.log('Not able to define start point');
script.logError("Something wrong with Spleeter....");
spleeterIsRunning = false;
return;
}
// get audio name
var songname = getFilename(targetFile).replace(".mp3","");
// check to see if separation files already exist
var vocals = util.fileExists(splOutputFolder + "/" + songname + "/vocals.mp3");
if (model.contains("spleeter:2stems"))
{
script.log("Check files for 2 stems");
var accompaniment = util.fileExists(splOutputFolder + "/" + songname + "/accompaniment.mp3");
} else if ( model.contains("spleeter:4stems") || model.contains("spleeter:5stems")){
script.log("Check files for 4/5 stems");
var drums = util.fileExists(splOutputFolder + "/" + songname + "/drums.mp3");
var bass = util.fileExists(splOutputFolder + "/" + songname + "/bass.mp3");
var other = util.fileExists(splOutputFolder + "/" + songname + "/other.mp3");
}
if ( model.contains("spleeter:5stems") )
{
script.log("Check files for 5 stems");
var piano = util.fileExists(splOutputFolder + "/" + songname + "/piano.mp3");
}
// run spleeter only on Force or missing file, this will execute ffprobe also
if ((local.parameters.spleeterParams.force.get() == 1) ||
(model.contains("spleeter:2stems") && (vocals == 0 || accompaniment == 0)) ||
(model.contains("spleeter:4stems") && (vocals == 0 || drums == 0 || bass == 0 || other == 0)) ||
(model.contains("spleeter:5stems") && (vocals == 0 || drums == 0 || bass == 0 || other == 0 || piano == 0))
)
{
// run Spleeter
if (local.parameters.spleeterParams.verbose.get() == 1)
{
verbose = "--verbose";
} else {
verbose = "";
}
var exeOPT = " " + splOutputOptions + " -o " + '"' + splOutputFolder + '" -p ' + splStems+' "' + targetFile + '" ' + verbose;
script.log('command to run : '+ spleeterCMDName + exeOPT);
// we execute the Spleeter command in blocking mode to wait end of execution;
var launchresult = root.modules.os.launchProcess(spleeterCMDName + exeOPT, true);
script.log(launchresult);
} else {
splOutputOptions = "ffprobeOnly for info";
var exeOPT = " " + splOutputOptions + " -o " + '"' + splOutputFolder + '" -p ' + splStems+' "' + targetFile + '" ' + verbose;
script.log('command to run : '+ spleeterCMDName + exeOPT);
// we execute the Spleeter command file for ffprobe only
var launchresult = root.modules.os.launchProcess(spleeterCMDName + exeOPT, true);
script.log(launchresult);
}
// set sequence to artist name /audio to title: info come from ffprobe
newSequence.setName(readMp3Tags("artist"));
newAudio.setName(readMp3Tags("title"));
// read the result
script.log("we read from : " + splOutputFolder);
var newtargetFile = splOutputFolder + "/" + songname + "/vocals.mp3";
// vocals is common file to all stems
var newvocals = util.fileExists(newtargetFile);
if (newvocals)
{
script.log('vocal file exist:' + newtargetFile);
newAudio = newSequence.layers.addItem('Audio');
var newAudioClip = newAudio.clips.addItem('audioClip');
newAudioClip.filePath.set(newtargetFile);
newAudio.setName("vocals");
} else {
script.log('vocal file do not exist :' + newtargetFile);
script.logError("Something wrong with Spleeter....");
}
if (model.contains("spleeter:2stems"))
{
script.log("Check files for 2 stems");
// instrumental file
var newtargetFile = splOutputFolder + "/" + songname + "/accompaniment.mp3";
var newaccompaniment = util.fileExists(newtargetFile);
if (newaccompaniment)
{
script.log('accompaniment file exist:' + newtargetFile);
newAudio = newSequence.layers.addItem('Audio');
var newAudioClip = newAudio.clips.addItem('audioClip');
newAudioClip.filePath.set(newtargetFile);
newAudio.setName("accompaniment");
} else {
script.log('accompaniment file do not exist :' + newtargetFile);
}
} else if (model.contains("spleeter:4stems") || model.contains("spleeter:5stems")) {
script.log("Check files for 4/5 stems");
// bass common for 4 or 5
var newtargetFile = splOutputFolder + "/" + songname + "/bass.mp3";
var newbass = util.fileExists(newtargetFile);
if (newbass)
{
script.log('bass file exist:' + newtargetFile);
newAudio = newSequence.layers.addItem('Audio');
var newAudioClip = newAudio.clips.addItem('audioClip');
newAudioClip.filePath.set(newtargetFile);
newAudio.setName("bass");
} else {
script.log('bass file do not exist :' +newtargetFile);
}
// drums common for 4 or 5
var newtargetFile = splOutputFolder + "/" + songname + "/drums.mp3";
var newdrums = util.fileExists(newtargetFile);
if (newdrums)
{
script.log('drums file exist:' +newtargetFile);
newAudio = newSequence.layers.addItem('Audio');
var newAudioClip = newAudio.clips.addItem('audioClip');
newAudioClip.filePath.set(newtargetFile);
newAudio.setName("drums");
} else {
script.log('drums file do not exist :' + newtargetFile);
}
// other common for 4 or 5
var newtargetFile = splOutputFolder + "/" + songname + "/other.mp3";
var newother = util.fileExists(newtargetFile);
if (newother)
{
script.log('other file exist:' + newtargetFile);
newAudio = newSequence.layers.addItem('Audio');
var newAudioClip = newAudio.clips.addItem('audioClip');
newAudioClip.filePath.set(newtargetFile);
newAudio.setName("other");
} else {
script.log('other file do not exist :' + newtargetFile);
}
}
if ( model.contains("spleeter:5stems"))
{
script.log("Check files for 5 stems");
// piano for 5 stems
var newtargetFile = splOutputFolder + "/" + songname + "/piano.mp3";
var newpiano = util.fileExists(newtargetFile);
if (newpiano)
{
script.log('piano file exist:' + newtargetFile);
newAudio = newSequence.layers.addItem('Audio');
var newAudioClip = newAudio.clips.addItem('audioClip');
newAudioClip.filePath.set(newtargetFile);
newAudio.setName("piano");
} else {
script.log('piano file do not exist :' + newtargetFile);
}
}
util.showMessageBox("Spleeter !", "Process finished for : " + newSequence.name, "information", "OK");
} else {
spleeterIsRunning = false;
script.log("Spleeter cmd not found");
util.showMessageBox("Spleeter !", "spleeter not found", "warning", "OK");
script.logError("Something wrong with Spleeter....");
}
spleeterIsRunning = false;
}
// retreive temp location
function spTmpPath ()
{
script.log("Retreive temp folder");
// TMP, TMPDIR, and TEMP environment variables
var pathTMP = util.getEnvironmentVariable("TMP");
var pathTMPDIR = util.getEnvironmentVariable("TMPDIR");
var pathTEMP = util.getEnvironmentVariable("TEMP");
if (pathTMP != "")
{
tempDIR = pathTMP;
return tempDIR;
} else if (pathTMPDIR != ""){
tempDIR = pathTMPDIR;
return tempDIR;
} else if (pathTEMP != ""){
tempDIR = pathTEMP;
return tempDIR;
}
script.log('ERROR temp directory not found');
script.logError("Something wrong with Spleeter....");
return tempDIR;
}
// retreive file name from absolute path
function getFilename(songname)
{
var splitcount = songname.split("/");
var filename = splitcount[splitcount.length-1];
return filename
}
// retreive tags from json file (generated by ffprobe)
function readMp3Tags(type)
{
var tag = "";
if (util.fileExists(tempDIR + "/ffprobefileinfo.json"))
{
var SCAJSONContent = util.readFile(tempDIR + "/ffprobefileinfo.json",true);
if ( type == "artist" )
{
tag = SCAJSONContent.format.tags.artist;
} else if ( type == "album" ){
tag = SCAJSONContent.format.tags.album;
} else if ( type == "title" ){
tag = SCAJSONContent.format.tags.title;
} else if ( type == "date" ){
tag = SCAJSONContent.format.tags.date;
} else {
tag = "undefined";
}
} else {
tag = "nofile";
script.log('File does not exist : ' + tempDIR + "/ffprobefileinfo.json");
}
return tag;
}
// used for value/expression testing .......
function testScript(songname)
{
script.log("test");
}