File size: 9,012 Bytes
2d86fb7 |
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 |
$silerolog=$ARGV[0];
$srcdir=$ARGV[1];
$dstdir=$ARGV[2];
$suff="";
$audioprocessing=1; # 0 to process quickly all json file to find warnings
if(! -d $dstdir){ mkdir $dstdir; }
# counters
$segmentstot=0;
$segmentstransferred=0;
$durationtot=0.0;
$durationtransferred=0.0;
$audiofilestot=0;
$audiofilesexisting=0;
$audiofilesmissing=0;
$audiofilessaved=0;
$audiofilesduplicated=0;
$lastaudioid="";
$offsetread=0.0; # number of seconds read (excluding 44 bytes header)
$openedfile=0; # flag, 1 if there are a pair of files (read, write) open
%processedaudiofiles=(); # to avoid reprocessing the same file twice in case of dirty logs
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)=("","","");
# {"audio_id": "c_BdUr5patI", "offset": 0.802, "duration": 10.46}
if($line=~m/^\{\"audio_id\": \"([^\"]+)\", \"offset\": ([0-9\.]+), \"duration\": ([0-9\.]+)\}\s*$/){
($aid,$off,$dur)=($1,$2,$3);
# print "1 aid: <$aid> off: <$off> dur: <$dur>\n";
}
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";
}
#print "read aid: <$aid> off: <$off> dur: <$dur>\n";
if($aid=~m/\S/){
if($aid eq $lastaudioid){ # new segment, old file
if($openedfile){
# check if the segments are in the correct order
if($off>=$offsetread){
$sil=($off-$offsetread);
$samplestoread=int($sil*1000)*16*2; # 2 bytes each sample
#printf "samplestoread %d %.3f \n",$samplestoread, $sil;
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);
}
# print AOUT $silence; # if I print everything, I got the same file unless final tail
}
$offsetread+=$sil; # seconds read (excluding 44 bytes header)
$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; # 2 bytes each sample
#printf "samplestoread %d %.3f \n",$samplestoread, $dur;
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; # seconds read (excluding 44 bytes header)
}
}
# for each segment:
# - reads file_id, offset and duration;
# - checks if the original audio file (.wav) is present in $srcdir/
# - if yes, writes a new audio file with the same name (+suffix $suff) in $dstdir/
# - appends the audio samples of the segment to the new audio file
#checks
}
else{ # new segment, new file
$audiofilestot++;
if($openedfile){ # first closes old file, if any
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;
#exit(1); # stops after the first audio file
# if($audiofilesexisting>2){ goto end; } # se >0 elabora un solo file
}
$localsegmentstot=0;
$localsegmentstransferred=0;
$localdurationtot=0.0;
$localdurationtransferred=0.0;
$localwarning=0;
$localerrors=0;
$offsetread=0.0; # number of samples read (excluding 44 bytes header )
if(defined($processedaudiofiles{$aid})){ # to avoid reprocessing the same file twice
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; # to avoid reprocessing the same file twice
$openfile="$dstdir/${aid}$suff";
$openedfile=1;
$offsetread=0.0; # number of samples read (excluding 44 bytes header)
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; # 2 bytes each sample
# printf "samplestoread %d %.3f \n",$samplestoread, $off;
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);
}
# print AOUT $header; # if I print everything, I got the same file unless final tail
# print AOUT $silence; # if I print everything, I got the same file unless final tail
}
$offsetread+=$off; # seconds read (excluding 44 bytes header)
$segmentstot++;
$durationtot+=$off;
$localsegmentstot++;
$localdurationtot+=$off;
}
$samplestoread=int($dur*1000)*16*2; # 2 bytes each sample
# printf "samplestoread %d %.3f \n",$samplestoread, $dur;
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; # seconds read (excluding 44 bytes header)
}
# read and discard silence, if > 0
# read and write speech, if >0
}
else{
$audiofilesmissing++;
print "WARNING: REPORT audio file $srcdir/$aid.wav missing\n";
}
# checks if the actual file was already processed
# open
}
$lastaudioid=$aid;
}
else{
print "WARNING: REPORT cannot parse <$line> - aid: <$aid> off: <$off> dur: <$dur>\n";
$localwarning++;
}
}
end:
close (SL);
if($openedfile){ # ends process last file, if any
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);
|