-
Notifications
You must be signed in to change notification settings - Fork 1
/
Spleeter.js
646 lines (491 loc) · 17.6 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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/*
author: zak45
date: 07/02/2023
version:2.1.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
Repo: https://github.com/zak-45/SpleeterGUI-Chataigne-Module
Portable Spleeter version available here : https://github.com/zak-45/SpleeterGUI-Chataigne-Module/releases/tag/0.0.0.0
*/
// Main module parameters
var shouldProcessSpl = false;
var shouldProcessInstall = false;
var installIsRunning = false;
var fileToTest = "";
var dirToTest = "";
var spOS = "";
var homeDIR = "";
var winHOME = "";
var moduleDIR = "";
var sequence = "";
var targetFile = "";
var model = "";
var newAudio = "";
var infos = util.getOSInfos();
// spleeter cmd file name (required)
var spleeterCMDName = "run_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()");
script.log("Hello "+infos.username);
script.log("We run under : "+infos.name);
spOS = infos.name;
if ( spOS.contains("Win") )
{
homeDIR = util.getEnvironmentVariable("USERPROFILE") + "/Documents/Chataigne";
winHOME = util.getEnvironmentVariable("USERPROFILE");
fileToTest = homeDIR + "/xtra/PySp3.10/Scripts/spleeter.exe";
// we check required TMP folder
spTmpPath();
} else {
homeDIR = util.getEnvironmentVariable("HOME") + "/Documents/Chataigne";
fileToTest = homeDIR + "/xtra/PySp3.10/bin/spleeter";
// we check required TMP folder
spTmpPath();
if (tempDIR == "")
{
tempDIR="/tmp";
}
}
dirToTest = homeDIR + "/xtra/PySp3.10/share";
moduleDIR = homeDIR + "/modules/SpleeterGUI-Chataigne-Module-main";
script.log("Temp folder : "+tempDIR);
if (tempDIR == "")
{
util.showMessageBox("Spleeter !", "TMP, TMPDIR or TEMP env variable not found", "warning", "OK");
}
}
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");
}
if (infos.name.contains("Win"))
{
local.parameters.spleeterParams.spleeterCommand.set(moduleDIR+"/xtra/win/run_spleeter.cmd");
} else if (infos.name.contains("Linux")) {
local.parameters.spleeterParams.spleeterCommand.set(moduleDIR+"/xtra/linux/run_spleeter.sh");
make_executable(moduleDIR+"/xtra/linux/run_spleeter.sh");
make_executable(moduleDIR+"/xtra/linux/install.sh");
} else if (infos.name.contains("macOS")) {
local.parameters.spleeterParams.spleeterCommand.set(moduleDIR+"/xtra/mac/run_spleeter.sh");
make_executable(moduleDIR+"/xtra/mac/run_spleeter.sh);
make_executable(moduleDIR+"/xtra/mac/install.sh);
}
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);
}
if (shouldProcessInstall && !installIsRunning)
{
shouldProcessInstall = false;
runInstall();
}
if (installIsRunning)
{
if (util.fileExists(fileToTest) && util.directoryExists(dirToTest)) {
installIsRunning = false;
util.showMessageBox("Spleeter", "install finished", "information", "ok");
}
}
}
}
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');
}
if (param.name == "install") {
if (installIsRunning == true) {
util.showMessageBox("Spleeter", "Installation is already running ....", "warning", "ok");
} else {
util.showYesNoCancelBox("install", "Spleeter", "proceed with installation", ["question"] );
}
}
if (param.name == "spleeterCommandLine") {
// Spleeter command
spleeterCMDName = local.parameters.spleeterParams.spleeterCommand.getAbsolutePath();
exeOPT = " cmd";
// check to see if Spleeter exe exist
if (util.fileExists(spleeterCMDName) == 1) {
// we execute the Spleeter command in non blocking mode
var launchresult = root.modules.os.launchProcess(spleeterCMDName + exeOPT, true);
script.log("Result : " + launchresult);
} else {
script.log("file not exist");
}
}
}
function messageBoxCallback(id, value) {
script.log('message box callback' + id + value );
if (id == "install" && value == 1) {
if (util.fileExists(fileToTest)) {
util.showMessageBox("Spleeter", "Already installed", "info", "ok");
} else {
shouldProcessInstall = true;
}
}
}
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 != ""){
var targetFile = root.modules.spleeter.commandTester.command.fileName.getAbsolutePath();
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 extract_tags 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 = "mp3tags 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 extract_tags only
var launchresult = root.modules.os.launchProcess(spleeterCMDName + exeOPT, true);
script.log(launchresult);
}
// set sequence to artist name /audio to title: info come from extract_tags
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, "info", "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 extract_tags)
function readMp3Tags(type)
{
var tag = "";
if (util.fileExists(moduleDIR + "/xtra/song_tags_info.json"))
{
var JSONContent = util.readFile(moduleDIR + "/xtra/song_tags_info.json",true);
if ( type == "artist" )
{
tag = JSONContent.ARTIST[0];
} else if ( type == "album" ){
tag = JSONContent.ALBUM[0];
} else if ( type == "title" ){
tag = JSONContent.TITLE[0];
} else if ( type == "date" ){
tag = JSONContent.DATE[0];
} else {
tag = "undefined";
}
} else {
tag = "nofile";
script.log('File does not exist : ' + moduleDIR + "/xtra/song_tags_info.json");
}
return tag;
}
function runInstall() {
installIsRunning = true;
util.showMessageBox("Spleeter !", "Installation is running... this will take a while, coffee time ! ", "info", "OK");
script.log('installation for os : '+ spOS);
if ( spOS.contains("Win") )
{
var launchresult = root.modules.os.launchApp(moduleDIR + "/xtra/win/install.cmd", "");
script.log(launchresult);
} else if ( spOS.contains("Linux") ) {
var launchresult = root.modules.os.launchApp(moduleDIR + "/xtra/linux/install.sh", "");
script.log(launchresult);
} else if ( spOS.contains("macOS") ) {
var launchresult = root.modules.os.launchApp(moduleDIR + "/xtra/mac/install.sh", "");
script.log(launchresult);
}
}
function make_executable(name)
{
script.log('set +x to : ' + name);
var launchresult = root.modules.os.launchProcess('chmod u+x ' + name, true);
script.log(launchresult);
}
// used for value/expression testing .......
function testScript(songname)
{
script.log("test");
}