|
|
$silerolog=$ARGV[0]; |
|
|
$srcdir=$ARGV[1]; |
|
|
$dstdir=$ARGV[2]; |
|
|
$suff=""; |
|
|
$audioprocessing=1; |
|
|
|
|
|
if(! -d $dstdir){ mkdir $dstdir; } |
|
|
|
|
|
|
|
|
$segmentstot=0; |
|
|
$segmentstransferred=0; |
|
|
$durationtot=0.0; |
|
|
$durationtransferred=0.0; |
|
|
|
|
|
$audiofilestot=0; |
|
|
$audiofilesexisting=0; |
|
|
$audiofilesmissing=0; |
|
|
$audiofilessaved=0; |
|
|
$audiofilesduplicated=0; |
|
|
|
|
|
$lastaudioid=""; |
|
|
$offsetread=0.0; |
|
|
$openedfile=0; |
|
|
|
|
|
%processedaudiofiles=(); |
|
|
|
|
|
if($silerolog=~m/.gz/){ |
|
|
open (SL, "zcat $silerolog|") || die "cannot read (using zcat) Silero log file $silerolog"; |
|
|
} |
|
|
else{ |
|
|
open (SL, "<$silerolog") || die "cannot read Silero log file $silerolog"; |
|
|
} |
|
|
while($line=<SL>){ |
|
|
$line=~s/\r?\n//; |
|
|
($aid,$off,$dur)=("","",""); |
|
|
|
|
|
if($line=~m/^\{\"audio_id\": \"([^\"]+)\", \"offset\": ([0-9\.]+), \"duration\": ([0-9\.]+)\}\s*$/){ |
|
|
($aid,$off,$dur)=($1,$2,$3); |
|
|
|
|
|
} |
|
|
elsif($line=~m/\{\"audio_id\": \"([^\"]+)\", \"offset\": ([0-9\.]+), \"duration\": ([0-9\.]+)\}\s*$/){ |
|
|
($aid,$off,$dur)=($1,$2,$3); |
|
|
print "WARNING: something strange <$line>\nWARNING: extracted parameters $aid , $off , $dur\n"; |
|
|
} |
|
|
|
|
|
|
|
|
if($aid=~m/\S/){ |
|
|
if($aid eq $lastaudioid){ |
|
|
if($openedfile){ |
|
|
|
|
|
if($off>=$offsetread){ |
|
|
$sil=($off-$offsetread); |
|
|
$samplestoread=int($sil*1000)*16*2; |
|
|
|
|
|
if($samplestoread > 0){ |
|
|
if($audioprocessing){ |
|
|
$n=read AIN,$silence,$samplestoread; |
|
|
unless($n == $samplestoread){ |
|
|
print "WARNING: cannot fully read silence, <$n> read instead of <$samplestoread> from audio file $srcdir/$aid.wav\n"; |
|
|
$localwarning++; |
|
|
$localerrors++ if($n == 0); |
|
|
} |
|
|
|
|
|
} |
|
|
$offsetread+=$sil; |
|
|
$segmentstot++; |
|
|
$durationtot+=$sil; |
|
|
$localsegmentstot++; |
|
|
$localdurationtot+=$sil; |
|
|
} |
|
|
} |
|
|
else{ |
|
|
printf "WARNING: offsetread $offsetread greater than off $off from audio file $srcdir/$aid.wav\n"; |
|
|
$localwarning++; |
|
|
} |
|
|
$samplestoread=int($dur*1000)*16*2; |
|
|
|
|
|
if($samplestoread > 0){ |
|
|
if($audioprocessing){ |
|
|
$speech=""; |
|
|
$n=read AIN,$speech,$samplestoread; |
|
|
unless($n == $samplestoread){ |
|
|
printf "WARNING: cannot fully read speech, <$n> read instead of <$samplestoread> from audio file $srcdir/$aid.wav\n"; |
|
|
$localwarning++; |
|
|
$localerrors++ if($n == 0); |
|
|
} |
|
|
print AOUT $speech; |
|
|
} |
|
|
$localsegmentstot++; |
|
|
$localsegmentstransferred++; |
|
|
$localdurationtot+=$dur; |
|
|
$localdurationtransferred+=$dur; |
|
|
$segmentstot++; |
|
|
$segmentstransferred++; |
|
|
$durationtot+=$dur; |
|
|
$durationtransferred+=$dur; |
|
|
$offsetread+=$dur; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
else{ |
|
|
$audiofilestot++; |
|
|
if($openedfile){ |
|
|
if($audioprocessing){ |
|
|
close(AIN); |
|
|
close(AOUT); |
|
|
if($localerrors <= 0 && $localwarning <= 1){ |
|
|
system("sox -r 16000 -b 16 -c1 -e signed-integer $openfile.raw $openfile.wav"); |
|
|
$audiofilessaved++; |
|
|
} |
|
|
else{ |
|
|
printf("WARNING: REPORT file %s NOT SAVED: %d warning %d errors\n",$openfile,$localwarning,$localerrors); |
|
|
} |
|
|
unlink "$openfile.raw"; |
|
|
} |
|
|
printf("REPORT file %s processed; %d segments from %d tot; %.1f seconds from %.1f seconds tot; %d warning %d errors\n", |
|
|
$openfile,$localsegmentstransferred,$localsegmentstot,$localdurationtransferred,$localdurationtot,$localwarning,$localerrors); |
|
|
$openedfile=0; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
$localsegmentstot=0; |
|
|
$localsegmentstransferred=0; |
|
|
$localdurationtot=0.0; |
|
|
$localdurationtransferred=0.0; |
|
|
$localwarning=0; |
|
|
$localerrors=0; |
|
|
$offsetread=0.0; |
|
|
|
|
|
if(defined($processedaudiofiles{$aid})){ |
|
|
printf "WARNING: REPORT file $aid already processed, skipping\n"; |
|
|
$audiofilesduplicated++; |
|
|
} |
|
|
elsif (-f "$srcdir/$aid.wav"){ |
|
|
if($audioprocessing){ |
|
|
open(AIN, "<$srcdir/$aid.wav") || die "cannot read audio file $srcdir/$aid.wav"; |
|
|
open(AOUT, ">$dstdir/${aid}$suff.raw") || die "cannot write audio file $dstdir/${aid}$suff.raw"; |
|
|
} |
|
|
$audiofilesexisting++; |
|
|
$processedaudiofiles{$aid}=1; |
|
|
$openfile="$dstdir/${aid}$suff"; |
|
|
$openedfile=1; |
|
|
$offsetread=0.0; |
|
|
if($audioprocessing){ |
|
|
$n=read AIN,$header,44; |
|
|
($n == 44) || die "cannot read header <$n> from audio file $srcdir/$aid.wav"; |
|
|
} |
|
|
$samplestoread=int($off*1000)*16*2; |
|
|
|
|
|
if($samplestoread > 0){ |
|
|
if($audioprocessing){ |
|
|
$n=read AIN,$silence,$samplestoread; |
|
|
unless($n == $samplestoread){ |
|
|
printf "WARNING: cannot fully read silence, <$n> read instead of <$samplestoread> from audio file $srcdir/$aid.wav\n"; |
|
|
$localwarning++; |
|
|
$localerrors++ if($n == 0); |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
$offsetread+=$off; |
|
|
$segmentstot++; |
|
|
$durationtot+=$off; |
|
|
$localsegmentstot++; |
|
|
$localdurationtot+=$off; |
|
|
} |
|
|
$samplestoread=int($dur*1000)*16*2; |
|
|
|
|
|
if($samplestoread > 0){ |
|
|
if($audioprocessing){ |
|
|
$speech=""; |
|
|
$n=read AIN,$speech,$samplestoread; |
|
|
unless($n == $samplestoread){ |
|
|
printf "WARNING: cannot fully read speech, <$n> read instead of <$samplestoread> from audio file $srcdir/$aid.wav\n"; |
|
|
$localwarning++; |
|
|
$localerrors++ if($n == 0); |
|
|
} |
|
|
print AOUT $speech; |
|
|
} |
|
|
$localsegmentstot++; |
|
|
$localsegmentstransferred++; |
|
|
$localdurationtot+=$dur; |
|
|
$localdurationtransferred+=$dur; |
|
|
$segmentstot++; |
|
|
$segmentstransferred++; |
|
|
$durationtot+=$dur; |
|
|
$durationtransferred+=$dur; |
|
|
$offsetread+=$dur; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
else{ |
|
|
$audiofilesmissing++; |
|
|
print "WARNING: REPORT audio file $srcdir/$aid.wav missing\n"; |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
$lastaudioid=$aid; |
|
|
} |
|
|
else{ |
|
|
print "WARNING: REPORT cannot parse <$line> - aid: <$aid> off: <$off> dur: <$dur>\n"; |
|
|
$localwarning++; |
|
|
} |
|
|
} |
|
|
end: |
|
|
close (SL); |
|
|
|
|
|
if($openedfile){ |
|
|
if($audioprocessing){ |
|
|
close(AIN); |
|
|
close(AOUT); |
|
|
if($localerrors <= 0 && $localwarning <= 1){ |
|
|
system("sox -r 16000 -b 16 -c1 -e signed-integer $openfile.raw $openfile.wav"); |
|
|
$audiofilessaved++; |
|
|
} |
|
|
else{ |
|
|
printf("WARNING: REPORT file %s NOT SAVED: %d warning %d errors\n",$openfile,$localwarning,$localerrors); |
|
|
} |
|
|
unlink "$openfile.raw"; |
|
|
} |
|
|
printf("REPORT file %s processed; %d segments from %d tot; %.1f seconds from %.1f seconds tot; %d warning %d errors\n", |
|
|
$openfile,$localsegmentstransferred,$localsegmentstot,$localdurationtransferred,$localdurationtot,$localwarning,$localerrors); |
|
|
} |
|
|
|
|
|
$durationtransferredh=int($durationtransferred/36)/100; |
|
|
$durationtoth=int($durationtot/36)/100; |
|
|
|
|
|
print |
|
|
"REPORT segmentstot: $segmentstot\n". |
|
|
"REPORT segmentstransferred: $segmentstransferred\n". |
|
|
"REPORT durationtot: $durationtot ($durationtoth hours)\n". |
|
|
"REPORT durationtransferred: $durationtransferred ($durationtransferredh hours)\n". |
|
|
"REPORT audiofilestot: $audiofilestot\n". |
|
|
"REPORT audiofilesexisting: $audiofilesexisting\n". |
|
|
"REPORT audiofilessaved: $audiofilessaved\n". |
|
|
"REPORT audiofilesmissing: $audiofilesmissing\n". |
|
|
"REPORT audiofilesduplicated: $audiofilesduplicated\n"; |
|
|
|
|
|
|
|
|
exit(1); |
|
|
|