spapi commited on
Commit
2d86fb7
·
verified ·
1 Parent(s): 7c75574

Create SplitAudioUsingSileroLog.pl

Browse files
Files changed (1) hide show
  1. scripts/SplitAudioUsingSileroLog.pl +252 -0
scripts/SplitAudioUsingSileroLog.pl ADDED
@@ -0,0 +1,252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ $silerolog=$ARGV[0];
2
+ $srcdir=$ARGV[1];
3
+ $dstdir=$ARGV[2];
4
+ $suff="";
5
+ $audioprocessing=1; # 0 to process quickly all json file to find warnings
6
+
7
+ if(! -d $dstdir){ mkdir $dstdir; }
8
+
9
+ # counters
10
+ $segmentstot=0;
11
+ $segmentstransferred=0;
12
+ $durationtot=0.0;
13
+ $durationtransferred=0.0;
14
+
15
+ $audiofilestot=0;
16
+ $audiofilesexisting=0;
17
+ $audiofilesmissing=0;
18
+ $audiofilessaved=0;
19
+ $audiofilesduplicated=0;
20
+
21
+ $lastaudioid="";
22
+ $offsetread=0.0; # number of seconds read (excluding 44 bytes header)
23
+ $openedfile=0; # flag, 1 if there are a pair of files (read, write) open
24
+
25
+ %processedaudiofiles=(); # to avoid reprocessing the same file twice in case of dirty logs
26
+
27
+ if($silerolog=~m/.gz/){
28
+ open (SL, "zcat $silerolog|") || die "cannot read (using zcat) Silero log file $silerolog";
29
+ }
30
+ else{
31
+ open (SL, "<$silerolog") || die "cannot read Silero log file $silerolog";
32
+ }
33
+ while($line=<SL>){
34
+ $line=~s/\r?\n//;
35
+ ($aid,$off,$dur)=("","","");
36
+ # {"audio_id": "c_BdUr5patI", "offset": 0.802, "duration": 10.46}
37
+ if($line=~m/^\{\"audio_id\": \"([^\"]+)\", \"offset\": ([0-9\.]+), \"duration\": ([0-9\.]+)\}\s*$/){
38
+ ($aid,$off,$dur)=($1,$2,$3);
39
+ # print "1 aid: <$aid> off: <$off> dur: <$dur>\n";
40
+ }
41
+ elsif($line=~m/\{\"audio_id\": \"([^\"]+)\", \"offset\": ([0-9\.]+), \"duration\": ([0-9\.]+)\}\s*$/){
42
+ ($aid,$off,$dur)=($1,$2,$3);
43
+ print "WARNING: something strange <$line>\nWARNING: extracted parameters $aid , $off , $dur\n";
44
+ }
45
+ #print "read aid: <$aid> off: <$off> dur: <$dur>\n";
46
+
47
+ if($aid=~m/\S/){
48
+ if($aid eq $lastaudioid){ # new segment, old file
49
+ if($openedfile){
50
+ # check if the segments are in the correct order
51
+ if($off>=$offsetread){
52
+ $sil=($off-$offsetread);
53
+ $samplestoread=int($sil*1000)*16*2; # 2 bytes each sample
54
+ #printf "samplestoread %d %.3f \n",$samplestoread, $sil;
55
+ if($samplestoread > 0){
56
+ if($audioprocessing){
57
+ $n=read AIN,$silence,$samplestoread;
58
+ unless($n == $samplestoread){
59
+ print "WARNING: cannot fully read silence, <$n> read instead of <$samplestoread> from audio file $srcdir/$aid.wav\n";
60
+ $localwarning++;
61
+ $localerrors++ if($n == 0);
62
+ }
63
+ # print AOUT $silence; # if I print everything, I got the same file unless final tail
64
+ }
65
+ $offsetread+=$sil; # seconds read (excluding 44 bytes header)
66
+ $segmentstot++;
67
+ $durationtot+=$sil;
68
+ $localsegmentstot++;
69
+ $localdurationtot+=$sil;
70
+ }
71
+ }
72
+ else{
73
+ printf "WARNING: offsetread $offsetread greater than off $off from audio file $srcdir/$aid.wav\n";
74
+ $localwarning++;
75
+ }
76
+ $samplestoread=int($dur*1000)*16*2; # 2 bytes each sample
77
+ #printf "samplestoread %d %.3f \n",$samplestoread, $dur;
78
+ if($samplestoread > 0){
79
+ if($audioprocessing){
80
+ $speech="";
81
+ $n=read AIN,$speech,$samplestoread;
82
+ unless($n == $samplestoread){
83
+ printf "WARNING: cannot fully read speech, <$n> read instead of <$samplestoread> from audio file $srcdir/$aid.wav\n";
84
+ $localwarning++;
85
+ $localerrors++ if($n == 0);
86
+ }
87
+ print AOUT $speech;
88
+ }
89
+ $localsegmentstot++;
90
+ $localsegmentstransferred++;
91
+ $localdurationtot+=$dur;
92
+ $localdurationtransferred+=$dur;
93
+ $segmentstot++;
94
+ $segmentstransferred++;
95
+ $durationtot+=$dur;
96
+ $durationtransferred+=$dur;
97
+ $offsetread+=$dur; # seconds read (excluding 44 bytes header)
98
+ }
99
+ }
100
+ # for each segment:
101
+ # - reads file_id, offset and duration;
102
+ # - checks if the original audio file (.wav) is present in $srcdir/
103
+ # - if yes, writes a new audio file with the same name (+suffix $suff) in $dstdir/
104
+ # - appends the audio samples of the segment to the new audio file
105
+ #checks
106
+ }
107
+ else{ # new segment, new file
108
+ $audiofilestot++;
109
+ if($openedfile){ # first closes old file, if any
110
+ if($audioprocessing){
111
+ close(AIN);
112
+ close(AOUT);
113
+ if($localerrors <= 0 && $localwarning <= 1){
114
+ system("sox -r 16000 -b 16 -c1 -e signed-integer $openfile.raw $openfile.wav");
115
+ $audiofilessaved++;
116
+ }
117
+ else{
118
+ printf("WARNING: REPORT file %s NOT SAVED: %d warning %d errors\n",$openfile,$localwarning,$localerrors);
119
+ }
120
+ unlink "$openfile.raw";
121
+ }
122
+ printf("REPORT file %s processed; %d segments from %d tot; %.1f seconds from %.1f seconds tot; %d warning %d errors\n",
123
+ $openfile,$localsegmentstransferred,$localsegmentstot,$localdurationtransferred,$localdurationtot,$localwarning,$localerrors);
124
+ $openedfile=0;
125
+ #exit(1); # stops after the first audio file
126
+
127
+ # if($audiofilesexisting>2){ goto end; } # se >0 elabora un solo file
128
+ }
129
+
130
+ $localsegmentstot=0;
131
+ $localsegmentstransferred=0;
132
+ $localdurationtot=0.0;
133
+ $localdurationtransferred=0.0;
134
+ $localwarning=0;
135
+ $localerrors=0;
136
+ $offsetread=0.0; # number of samples read (excluding 44 bytes header )
137
+
138
+ if(defined($processedaudiofiles{$aid})){ # to avoid reprocessing the same file twice
139
+ printf "WARNING: REPORT file $aid already processed, skipping\n";
140
+ $audiofilesduplicated++;
141
+ }
142
+ elsif (-f "$srcdir/$aid.wav"){
143
+ if($audioprocessing){
144
+ open(AIN, "<$srcdir/$aid.wav") || die "cannot read audio file $srcdir/$aid.wav";
145
+ open(AOUT, ">$dstdir/${aid}$suff.raw") || die "cannot write audio file $dstdir/${aid}$suff.raw";
146
+ }
147
+ $audiofilesexisting++;
148
+ $processedaudiofiles{$aid}=1; # to avoid reprocessing the same file twice
149
+ $openfile="$dstdir/${aid}$suff";
150
+ $openedfile=1;
151
+ $offsetread=0.0; # number of samples read (excluding 44 bytes header)
152
+ if($audioprocessing){
153
+ $n=read AIN,$header,44;
154
+ ($n == 44) || die "cannot read header <$n> from audio file $srcdir/$aid.wav";
155
+ }
156
+ $samplestoread=int($off*1000)*16*2; # 2 bytes each sample
157
+ # printf "samplestoread %d %.3f \n",$samplestoread, $off;
158
+ if($samplestoread > 0){
159
+ if($audioprocessing){
160
+ $n=read AIN,$silence,$samplestoread;
161
+ unless($n == $samplestoread){
162
+ printf "WARNING: cannot fully read silence, <$n> read instead of <$samplestoread> from audio file $srcdir/$aid.wav\n";
163
+ $localwarning++;
164
+ $localerrors++ if($n == 0);
165
+ }
166
+ # print AOUT $header; # if I print everything, I got the same file unless final tail
167
+ # print AOUT $silence; # if I print everything, I got the same file unless final tail
168
+ }
169
+ $offsetread+=$off; # seconds read (excluding 44 bytes header)
170
+ $segmentstot++;
171
+ $durationtot+=$off;
172
+ $localsegmentstot++;
173
+ $localdurationtot+=$off;
174
+ }
175
+ $samplestoread=int($dur*1000)*16*2; # 2 bytes each sample
176
+ # printf "samplestoread %d %.3f \n",$samplestoread, $dur;
177
+ if($samplestoread > 0){
178
+ if($audioprocessing){
179
+ $speech="";
180
+ $n=read AIN,$speech,$samplestoread;
181
+ unless($n == $samplestoread){
182
+ printf "WARNING: cannot fully read speech, <$n> read instead of <$samplestoread> from audio file $srcdir/$aid.wav\n";
183
+ $localwarning++;
184
+ $localerrors++ if($n == 0);
185
+ }
186
+ print AOUT $speech;
187
+ }
188
+ $localsegmentstot++;
189
+ $localsegmentstransferred++;
190
+ $localdurationtot+=$dur;
191
+ $localdurationtransferred+=$dur;
192
+ $segmentstot++;
193
+ $segmentstransferred++;
194
+ $durationtot+=$dur;
195
+ $durationtransferred+=$dur;
196
+ $offsetread+=$dur; # seconds read (excluding 44 bytes header)
197
+ }
198
+
199
+ # read and discard silence, if > 0
200
+ # read and write speech, if >0
201
+
202
+ }
203
+ else{
204
+ $audiofilesmissing++;
205
+ print "WARNING: REPORT audio file $srcdir/$aid.wav missing\n";
206
+ }
207
+ # checks if the actual file was already processed
208
+ # open
209
+ }
210
+ $lastaudioid=$aid;
211
+ }
212
+ else{
213
+ print "WARNING: REPORT cannot parse <$line> - aid: <$aid> off: <$off> dur: <$dur>\n";
214
+ $localwarning++;
215
+ }
216
+ }
217
+ end:
218
+ close (SL);
219
+
220
+ if($openedfile){ # ends process last file, if any
221
+ if($audioprocessing){
222
+ close(AIN);
223
+ close(AOUT);
224
+ if($localerrors <= 0 && $localwarning <= 1){
225
+ system("sox -r 16000 -b 16 -c1 -e signed-integer $openfile.raw $openfile.wav");
226
+ $audiofilessaved++;
227
+ }
228
+ else{
229
+ printf("WARNING: REPORT file %s NOT SAVED: %d warning %d errors\n",$openfile,$localwarning,$localerrors);
230
+ }
231
+ unlink "$openfile.raw";
232
+ }
233
+ printf("REPORT file %s processed; %d segments from %d tot; %.1f seconds from %.1f seconds tot; %d warning %d errors\n",
234
+ $openfile,$localsegmentstransferred,$localsegmentstot,$localdurationtransferred,$localdurationtot,$localwarning,$localerrors);
235
+ }
236
+
237
+ $durationtransferredh=int($durationtransferred/36)/100;
238
+ $durationtoth=int($durationtot/36)/100;
239
+
240
+ print
241
+ "REPORT segmentstot: $segmentstot\n".
242
+ "REPORT segmentstransferred: $segmentstransferred\n".
243
+ "REPORT durationtot: $durationtot ($durationtoth hours)\n".
244
+ "REPORT durationtransferred: $durationtransferred ($durationtransferredh hours)\n".
245
+ "REPORT audiofilestot: $audiofilestot\n".
246
+ "REPORT audiofilesexisting: $audiofilesexisting\n".
247
+ "REPORT audiofilessaved: $audiofilessaved\n".
248
+ "REPORT audiofilesmissing: $audiofilesmissing\n".
249
+ "REPORT audiofilesduplicated: $audiofilesduplicated\n";
250
+
251
+
252
+ exit(1);