$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=){ $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);