forked from vdellamea/jamulus-server-remote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomix.php
379 lines (325 loc) · 10.1 KB
/
automix.php
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
<?php
// Jamulus Recording Remote
// v0.6 - 20210423
// Vincenzo Della Mea
//STANDALONE AUTOMIX
//TODO: https://www.php.net/manual/en/function.getopt.php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
if(isCommandLineInterface()) {
// Bandmates names to be used for "informed" automix
// 'name' => value,
// name exactly as in Jamulus profile,
// value: 1= left only, 0= right only, 0.5= center, etc
// Names not specified are automatically panned.
$BANDMATES=array(
'Jimi' =>0.85,
'Eric' =>0.45,
'Carol' =>0.53,
'Patti' =>0.5,
'Stevie'=> 0.11,
);
// File format for consolidated tracks
//TODO set bitrate
$CFORMAT="mp3"; //.wav, .opus ... any format managed by ffmpeg
//default dir is current dir for automix
$MIX="./";
$DEBUG=true;
$OPERATION='automix';
$DIRTYPE='single';
$AUDIONORMALIZATION=false;
//command line options
$optlist=array("automix","consolidate","single", "all","debug","format::","in:", "out:", "help", "normalize");
$options=getopt("",$optlist);
if(isset($options['debug'])) $DEBUG=true;
if(isset($options['consolidate'])) {$OPERATION='consolidate'; $CFORMAT='wav';}
if(isset($options['all'])) $DIRTYPE='all';
if(isset($options['format'])) $CFORMAT=$options['format'];
if(isset($options['in'])) $RECORDINGS=$options['in'];
if(isset($options['out'])) $MIX=$options['out'];
if(isset($options['normalize'])) $AUDIONORMALIZATION=true;
if($DEBUG) var_dump($options);
if(isset($options['help']) || !isset($RECORDINGS)) die(automix_help());
$today=date("Ymd");
//If you need to adapt the Remote to your server, check the following commands
if(!$DEBUG) $FFMPEG_LOG="-loglevel quiet ";
$COMMANDS=array(
"ffmpeg" => "ffmpeg $FFMPEG_LOG -hide_banner ",//-loglevel quiet
"checkstereo" => "ffmpeg $FFMPEG_LOG -hide_banner -i ",
"maxvolume" =>" $FFMPEG_LOG -hide_banner -i ",
"zipmix" => "zip mix.zip *.mp3 ; rm $MIX/*.mp3 ",
"ffprobe" => "ffprobe -hide_banner -show_entries stream=duration -of compact=p=0:nk=1 -v 0 ",
);
print("AUTOMIX 0.51 - part of Jamulus Recording Remote\n");
//if($argc<3) die("php automix.php single|all path_to_recordings\n");
if($OPERATION=='automix' && $DIRTYPE=='all') {
print("Generating automix for all the sessions in $RECORDINGS.\n");
$sessions=glob("$RECORDINGS/Jam-*",GLOB_ONLYDIR);
foreach($sessions as $s) {
print("- $s \n");
$from=$s;
$to="/var/tmp/".basename($from)."/";
mkdir($to);
consolidate_tracks($from,$to, ".wav");
$out=generate_mix($to);
if($DEBUG) print_r($out);
}
}
else if($OPERATION=='automix' && $DIRTYPE=='single'){
print("Generating automix for session $RECORDINGS.\n");
$to="/var/tmp/".basename($RECORDINGS)."/";
mkdir($to);
consolidate_tracks($RECORDINGS,$to, ".wav");
$out=generate_mix($to);
if($DEBUG) print_r($out);
}
else if($OPERATION=='consolidate' && $DIRTYPE=='single'){
print("Consolidating tracks for session $RECORDINGS to $MIX .\n");
mkdir($MIX);
consolidate_tracks($RECORDINGS,$MIX."/", $CFORMAT);
}
else if($OPERATION=='consolidate' && $DIRTYPE=='all'){
print("Consolidating tracks for all the sessions in $RECORDINGS .\n");
$sessions=glob("$RECORDINGS/Jam-*",GLOB_ONLYDIR);
foreach($sessions as $s) {
print("- $s \n");
$from=$s;
$to=$MIX."/".basename($from)."/";
if($DEBUG) print("TO: $MIX - $from - $to. \n");
mkdir($to);
consolidate_tracks($from,$to, $CFORMAT);
}
}
else die(automix_help());
}
//FUNCTIONS
function generate_mix($session) {
global $RECORDINGS;
global $MIX;
global $COMMANDS;
global $BANDMATES;
global $DEBUG;
$ffmpeg=$COMMANDS['ffmpeg'];
$checkstereo=$COMMANDS['checkstereo'];
$filename=basename($session);
$dir=glob($session."/*.wav");
$chantotal=0;
$unknowns=0;
//scan tracks, check known clients, check mono/stereo
foreach($dir as $t) {
//Is it a known client or not?
if(!known(basename($t))) $unknowns++;
exec($checkstereo.$t." 2>&1 | grep Guessed",$out,$rec);
$stereo=substr(trim($out[0]),-6)=="stereo";
$tracks[$t]=$stereo;
$chantotal1=$chantotal+1;
if($stereo) {
$channels[$t]=array("c".$chantotal,"c".$chantotal1);
$chantotal=$chantotal+2;
}
else
{
$channels[$t]=array("c".$chantotal,"c".$chantotal);
$chantotal=$chantotal+1;
}
unset($out);
}
$clients=sizeof($tracks);
//STEREO PANNING
if($unknowns>0) $offset=0.5/$unknowns;
$step=$offset*2;
$lmult=-0.5;
$numchans=0;
foreach($channels as $t=>$chans){
$inputs.="-i $t ";
//if client is known, use the value from config
if(known(basename($t))) {
$ltmp=$BANDMATES[known_name(basename($t))];
$rtmp=1-$ltmp;
}
else {
if($numchans==0)
$rtmp=$offset;
else
$rtmp=$rtmp+$step;
$ltmp=1-$rtmp;
$numchans++;
if($DEBUG) print("PANNING ".basename($t).": $ltmp - $rtmp \n");
}
$ltmp=round($ltmp,3);
$rtmp=round($rtmp,3);
$left.=$ltmp."*".$chans[0]."+";
$right.=$rtmp."*".$chans[1]."+";
}
// remove trailing '+'
$left=substr($left,0,-1);
$right=substr($right,0,-1);
$volumeplus=$clients;
$automixcommand=
"$ffmpeg $inputs -filter_complex \"amerge=inputs=". $clients.
",volume=$volumeplus"."dB,pan=stereo|FL<$left|FR<$right".
"[a]\" -map \"[a]\" $MIX"."$filename.mp3\n";
exec($automixcommand,$out,$ret);
if($DEBUG) {
print("AUTOMIX:".$automixcommand."\n");
print_r($out);
}
unset($out);
}
///////////////////////////////////////////
function consolidate_tracks($dir, $outdir, $format) {
global $AUDIONORMALIZATION;
global $COMMANDS;
global $DEBUG;
$ffprobe=$COMMANDS['ffprobe'];
$checkmaxvolume=$COMMANDS['maxvolume'];
// scan .lof file to read track offsets in seconds
$lof=file($dir."/".basename($dir).".lof");
foreach($lof as $f){
$tmp=explode(" ",trim($f));
$offset[str_replace("\"","",$tmp[1])]=$tmp[3];
}
$list=glob($dir."/*.wav");
$maxduration=0;//this will become the total length of the session
foreach($list as $t){
$tmp=explode("-",substr(basename($t),0,-4));
//check duration of each track
exec($ffprobe.$t,
$out);
$duration=$out[0];unset($out);
if($DEBUG) print("DURATION: ".$ffprobe.$t."\n");
//check max volume
$maxvolumecommand=$checkmaxvolume.$t.
' -af "volumedetect" -f null /dev/null 2>&1 |grep max_volume';
exec($maxvolumecommand,$outvol,$retvol);
if($DEBUG) print("VOLUME: ".$maxvolumecommand."\n");
$outvol2=explode(":",trim($outvol[0]));
$outvol2=explode(" ",substr($outvol2[1],0,-2));
$maxvolume=$outvol2[1];
unset($outvol);
unset($outvol2);
//if a client name is available, use it
if(!isset($tracks[$tmp[1]]['name'])) $tracks[$tmp[1]]['name']='____';
if($tmp[0]<>'____') $tracks[$tmp[1]]['name']=$tmp[0];
// real duration is offset + duration
$newdur=$offset[basename($t)]+$duration;
// look for the longest
if($newdur>$maxduration) $maxduration=$newdur;
$tracks[$tmp[1]]['segments'][$t]=
array( 'frame'=>$tmp[2],
'channels'=>$tmp[3],
'offset'=>$offset[basename($t)],
'duration'=>$duration,
'newdur'=>$newdur,
'maxvolume'=>$maxvolume,
);
}
if($DEBUG) print_r($tracks);
foreach ($tracks as $ip=>$t){
$trackdur=0;
$maxoffset=0;
$inputs=""; $delays=""; $amix="";
//$format contains the file extension: ffmpeg will generate in such format
//TODO set bitrate!
$outname=$outdir.$t['name']."-consolidated.".$format;
if($t['name']=='____') $outname=$outdir.$ip."-consolidated.".$format;
$c=0;
//sort by offset to reorder when many WAVs
//and decide whether the channel should be stereo or mono
$orderedwavs=array();
foreach($t['segments'] as $tr=>$s){
$orderedwavs[$tr]=$s['offset'];
$numberofchannels=$s['channels'];
}
asort($orderedwavs);
$monostereo='mono';if($numberofchannels==2) $monostereo='stereo';
$previousdur=0;
foreach($orderedwavs as $tr=>$o){
$s=$t['segments'][$tr];
$inputs.="-i $tr ";
$delay=round(1000*($s['offset']-$previousdur),0);
$maxvolume=-$s['maxvolume'];
//Volumes are all brought to 0dB
//this tries to save who had set the volume too low
$volumeincrease="";
if($maxvolume>0 && $maxvolume<7)
$volumeincrease=" ,volume=".$maxvolume."dB";
$delays.=
"[$c]aformat=sample_fmts=s16:sample_rates=48000".
":channel_layouts=$monostereo".
",adelay=".$delay."|".$delay.$volumeincrease."[a$c]; ";
$amix.="[a$c]";
$c++;
if($s['offset']>$maxoffset) {
$trackdur=($s['offset']+$s['duration']);
$maxoffset=$s['offset'];
}
$previousdur=$s['offset']+$s['duration'];
}
$total=$c;
$silence="";$silencedelay="";$silenceamix="";
$missingtime=round($maxduration-$previousdur,3);
$trackdur=round(1000*$trackdur,0);
// if the consolidated track is shorter than the maximum duration,
// add a silence track that lasts as the maximum
if($missingtime>0) {
$total=$c+1;
$silence=" -f lavfi -i anullsrc=r=48000 ";
$tracksilence=round($maxduration,3);
$silencedelay="[$c]atrim=duration=".$missingtime."[a$c];";
$silenceamix="[a$c]";
}
//Audio normalization: does not give reliable results
if($AUDIONORMALIZATION) $audionorm=", dynaudnorm=t=0.1 ";
// if($AUDIONORMALIZATION) $audionorm=", loudnorm=tp=0.0, aresample=48000";
$command="ffmpeg -hide_banner $inputs $silence".
" -filter_complex \"$delays $silencedelay $amix".
$silenceamix."concat=n=$total:v=0:a=1 $audionorm". "\" $outname\n";
exec($command, $outcommand);
if($DEBUG) {
print("CONSOLIDATE: ".$command."\n");
print_r($outcommand);
}
unset($outcommand);
}
}
function known($track) {
global $BANDMATES;
$ret=false;
foreach($BANDMATES as $m=>$n)
if($ret=(substr($track,0,strlen($m))==$m))
break;
return $ret;
}
function known_name($track) {
global $BANDMATES;
$ret=false;
foreach($BANDMATES as $m=>$n) {
$ret=substr($track,0,strlen($m));
if($ret==$m) break;
}
return $ret;
}
function isCommandLineInterface()
{
return (php_sapi_name() === 'cli');
}
function automix_help() {
?>
AUTOMIX v0.51 - part of Jamulus Recording Remote
Choose an operation:
--automix (default)
--consolidate
Is a full Recordings directory or a single session?
--single (default)
--all
Files:
--in path_to_recordings directory
--out path_to_generated (default: current dir)
Options:
--format (wav,mp3,opus) (default: mp3 for automix, wav for consolidate)
--normalize audio normalization, default off - not good yet
--debug add extra output
--help this one
<?php
}
?>