forked from speechbrain/speechbrain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdns_download.py
More file actions
601 lines (533 loc) · 24.9 KB
/
Copy pathdns_download.py
File metadata and controls
601 lines (533 loc) · 24.9 KB
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#!/usr/bin/env/python3
"""
Recipe for downloading DNS-4 dataset- training,
baseline DEV noisyset, blind testset
Source:
https://github.com/microsoft/DNS-Challenge
https://github.com/microsoft/DNS-Challenge/blob/master/download-dns-challenge-4.sh
Disk-space (compressed): 550 GB
Disk-space (decompressed): 1 TB
NOTE:
1. Some of the azure links provided by Microsoft are not perfect and data
download may stop mid-way through the download process. Hence we validate
download size of each of the file.
2. Instead of using the impulse response files provided in the challenge,
we opt to download them from OPENSLR. OPENSLR offers both real and synthetic
RIRs, while the challenge offers only real RIRs.
Authors
* Sangeet Sagar 2022
"""
import argparse
import fileinput
import os
import shutil
import ssl
import tarfile
import urllib.request
import zipfile
from concurrent.futures import ThreadPoolExecutor
import certifi
import requests
from tqdm.auto import tqdm
BLOB_NAMES = [
"clean_fullband/datasets_fullband.clean_fullband.VocalSet_48kHz_mono_000_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.emotional_speech_000_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.french_speech_000_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.french_speech_001_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.french_speech_002_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.french_speech_003_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.french_speech_004_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.french_speech_005_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.french_speech_006_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.french_speech_007_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.french_speech_008_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_000_0.00_3.47.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_001_3.47_3.64.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_002_3.64_3.74.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_003_3.74_3.81.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_004_3.81_3.86.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_005_3.86_3.91.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_006_3.91_3.96.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_007_3.96_4.00.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_008_4.00_4.04.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_009_4.04_4.08.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_010_4.08_4.12.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_011_4.12_4.16.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_012_4.16_4.21.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_013_4.21_4.26.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_014_4.26_4.33.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_015_4.33_4.43.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_016_4.43_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_017_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_018_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_019_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_020_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_021_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_022_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_023_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_024_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_025_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_026_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_027_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_028_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_029_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_030_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_031_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_032_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_033_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_034_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_035_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_036_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_037_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_038_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_039_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_040_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_041_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.german_speech_042_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.italian_speech_000_0.00_3.98.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.italian_speech_001_3.98_4.21.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.italian_speech_002_4.21_4.40.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.italian_speech_003_4.40_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.italian_speech_004_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.italian_speech_005_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_000_0.00_3.75.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_001_3.75_3.88.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_002_3.88_3.96.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_003_3.96_4.02.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_004_4.02_4.06.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_005_4.06_4.10.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_006_4.10_4.13.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_007_4.13_4.16.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_008_4.16_4.19.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_009_4.19_4.21.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_010_4.21_4.24.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_011_4.24_4.26.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_012_4.26_4.29.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_013_4.29_4.31.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_014_4.31_4.33.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_015_4.33_4.35.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_016_4.35_4.38.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_017_4.38_4.40.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_018_4.40_4.42.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_019_4.42_4.45.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_020_4.45_4.48.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_021_4.48_4.52.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_022_4.52_4.57.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_023_4.57_4.67.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_024_4.67_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_025_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_026_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_027_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_028_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_029_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_030_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_031_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_032_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_033_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_034_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_035_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_036_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_037_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_038_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.read_speech_039_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.russian_speech_000_0.00_4.31.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.russian_speech_001_4.31_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.spanish_speech_000_0.00_4.09.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.spanish_speech_001_4.09_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.spanish_speech_002_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.spanish_speech_003_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.spanish_speech_004_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.spanish_speech_005_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.spanish_speech_006_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.spanish_speech_007_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.spanish_speech_008_NA_NA.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.vctk_wav48_silence_trimmed_000.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.vctk_wav48_silence_trimmed_001.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.vctk_wav48_silence_trimmed_002.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.vctk_wav48_silence_trimmed_003.tar.bz2",
"clean_fullband/datasets_fullband.clean_fullband.vctk_wav48_silence_trimmed_004.tar.bz2",
"noise_fullband/datasets_fullband.noise_fullband.audioset_000.tar.bz2",
"noise_fullband/datasets_fullband.noise_fullband.audioset_001.tar.bz2",
"noise_fullband/datasets_fullband.noise_fullband.audioset_002.tar.bz2",
"noise_fullband/datasets_fullband.noise_fullband.audioset_003.tar.bz2",
"noise_fullband/datasets_fullband.noise_fullband.audioset_004.tar.bz2",
"noise_fullband/datasets_fullband.noise_fullband.audioset_005.tar.bz2",
"noise_fullband/datasets_fullband.noise_fullband.audioset_006.tar.bz2",
"noise_fullband/datasets_fullband.noise_fullband.freesound_000.tar.bz2",
"noise_fullband/datasets_fullband.noise_fullband.freesound_001.tar.bz2",
"datasets_fullband.dev_testset_000.tar.bz2",
]
AZURE_URL = "https://dns4public.blob.core.windows.net/dns4archive/datasets_fullband" # noqa ignore-url-check
# Impulse response and Blind testset
OTHER_URLS = {
"impulse_responses": [
"https://www.openslr.org/resources/26/sim_rir_16k.zip",
"https://www.openslr.org/resources/28/rirs_noises.zip",
],
"blind_testset": [
"https://dns4public.blob.core.windows.net/dns4archive/blind_testset_bothtracks.zip"
],
}
RIR_table_simple_URL = "https://raw.githubusercontent.com/microsoft/DNS-Challenge/0443a12f5e6e7bec310f453cf0d9637ca28e0eea/datasets/acoustic_params/RIR_table_simple.csv"
SPLIT_LIST = [
"dev_testset",
"impulse_responses",
"noise_fullband",
"emotional_speech",
"french_speech",
"german_speech",
"italian_speech",
"read_speech",
"russian_speech",
"spanish_speech",
"vctk_wav48_silence_trimmed",
"VocalSet_48kHz_mono",
]
def prepare_download():
"""
Downloads and prepares various data files and resources. It
downloads real-time DNS track data files (train set and dev
noisy set).
"""
# Real-time DNS track (train set + dev noisy set)
for file_url in BLOB_NAMES:
for split in SPLIT_LIST:
if split in file_url:
split_name = split
split_path = os.path.join(COMPRESSED_PATH, split_name)
if not os.path.exists(split_path):
os.makedirs(split_path)
if not os.path.exists(DECOMPRESSED_PATH):
os.makedirs(DECOMPRESSED_PATH)
filename = file_url.split("/")[-1]
download_path = os.path.join(split_path, filename)
download_url = AZURE_URL + "/" + file_url
if not validate_file(download_url, download_path):
if os.path.exists(download_path):
resume_byte_pos = os.path.getsize(download_path)
else:
resume_byte_pos = None
download_file(
download_url,
download_path,
split_name,
filename,
resume_byte_pos=resume_byte_pos,
)
else:
print(", \tDownload complete. Skipping")
decompress_file(download_path, DECOMPRESSED_PATH, split_name)
# Download RIR (impulse response) & BLIND testset
rir_blind_test_download()
def rir_blind_test_download():
"""
Download the RIRs (room impulse responses), and the blind
test set.
"""
# RIR (impulse response) & BLIND testset
for split_name, download_urls in OTHER_URLS.items():
for file_url in download_urls:
split_path = os.path.join(COMPRESSED_PATH, split_name)
if not os.path.exists(split_path):
os.makedirs(split_path)
filename = file_url.split("/")[-1]
download_path = os.path.join(split_path, filename)
if not validate_file(file_url, download_path):
if os.path.exists(download_path):
resume_byte_pos = os.path.getsize(download_path)
else:
resume_byte_pos = None
download_file(
file_url,
download_path,
split_name,
filename,
resume_byte_pos=resume_byte_pos,
)
else:
print(", \tDownload complete. Skipping")
decompress_file(
download_path,
os.path.join(DECOMPRESSED_PATH, split_name),
split_name,
)
# Download RIRs simple table
file_path = os.path.join(
DECOMPRESSED_PATH, "impulse_responses", "RIR_table_simple.csv"
)
response = requests.get(RIR_table_simple_URL)
if response.status_code == 200:
with open(file_path, "wb") as file:
file.write(response.content)
print("\nRIR_simple_table downloaded successfully.")
else:
print(
f"\nFailed to download RIR_simple_table. Status code: {response.status_code}"
)
def download_file(
download_url, download_path, split_name, filename, resume_byte_pos=None
):
"""
Download file from given URL
Arguments
---------
download_url : str
URL of file being downloaded
download_path : str
Full path of the file that is to be downloaded
(or already downloaded)
split_name : str
Split name of the file being downloaded
e.g. read_speech
filename : str
Filename of the file being downloaded
resume_byte_pos: (int, optional)
Starting byte position for resuming the download.
Default is None, which means a fresh download.
Returns
-------
bool
If True, the file need not be downloaded again.
Else the download might have failed or is incomplete.
"""
print("Downloading:", split_name, "=>", filename)
resume_header = (
{"Range": f"bytes={resume_byte_pos}-"} if resume_byte_pos else None
)
response = requests.get(download_url, headers=resume_header, stream=True)
file_size = int(response.headers.get("Content-Length"))
mode = "ab" if resume_byte_pos else "wb"
initial_pos = resume_byte_pos if resume_byte_pos else 0
with open(download_path, mode, encoding="utf-8") as f:
with tqdm(
total=file_size,
unit="B",
unit_scale=True,
unit_divisor=1024,
initial=initial_pos,
miniters=1,
) as pbar:
for chunk in response.iter_content(32 * 1024):
f.write(chunk)
pbar.update(len(chunk))
# Validate downloaded file
if validate_file(download_url, download_path):
return True
else:
print("Download failed. Moving on.")
return False
def download_file_parallel(args):
"""
Downloads a file in parallel using the provided arguments. It
makes use of `download_file` function to download the required file.
Arguments
---------
args : tuple
Tuple containing the download URL, download path, split
name, filename, and required bytes to be downloaded.
"""
download_url, download_path, split_name, filename, resume_byte_pos = args
download_file(
download_url,
download_path,
split_name,
filename,
resume_byte_pos=resume_byte_pos,
)
def parallel_download():
"""
Perform parallel download of files using `using ThreadPoolExecutor`.
"""
with ThreadPoolExecutor() as executor:
futures = []
for file_url in BLOB_NAMES:
for split in SPLIT_LIST:
if split in file_url:
split_name = split
split_path = os.path.join(COMPRESSED_PATH, split_name)
if not os.path.exists(split_path):
os.makedirs(split_path)
if not os.path.exists(DECOMPRESSED_PATH):
os.makedirs(DECOMPRESSED_PATH)
filename = file_url.split("/")[-1]
download_path = os.path.join(split_path, filename)
download_url = AZURE_URL + "/" + file_url
if not validate_file(download_url, download_path):
if os.path.exists(download_path):
resume_byte_pos = os.path.getsize(download_path)
else:
resume_byte_pos = None
args = (
download_url,
download_path,
split_name,
filename,
resume_byte_pos,
)
futures.append(executor.submit(download_file_parallel, args))
# download_file(download_url, download_path, split_name, filename)
# decompress_file(download_path, DECOMPRESSED_PATH)
else:
print(", \tDownload complete. Skipping")
decompress_file(download_path, DECOMPRESSED_PATH, split_name)
for future in futures:
future.result()
# Download RIR (impulse response) & BLIND testset
rir_blind_test_download()
def decompress_file(file, decompress_path, split_name):
"""
Decompress the downloaded file if the target folder does not exist.
Arguments
---------
file : str
Path to the compressed downloaded file
decompress_path : str
Path to store the decompressed audio files
split_name : str
The portion of the data to decompress
Returns
-------
True if decompression skipped.
"""
for _, dirs, _ in os.walk(decompress_path):
if split_name in dirs:
print("\tDecompression skipped. Folder already exists.")
return True
if "sim_rir_16k" in file:
slr26_dir = os.path.join(decompress_path, "SLR26")
if os.path.exists(slr26_dir):
print("\tDecompression skipped. Folder already exists.")
return True
if "rirs_noises" in file:
slr28_dir = os.path.join(decompress_path, "SLR28")
if os.path.exists(slr28_dir):
print("\tDecompression skipped. Folder already exists.")
return True
print("\tDecompressing...")
file_extension = os.path.splitext(file)[-1].lower()
if file_extension == ".zip":
zip = zipfile.ZipFile(file, "r")
zip.extractall(decompress_path)
rename_rirs(decompress_path)
elif file_extension == ".bz2":
tar = tarfile.open(file, "r:bz2")
tar.extractall(decompress_path)
tar.close()
else:
print("Unsupported file format. Only zip and bz2 files are supported.")
def rename_rirs(decompress_path):
"""
Rename directories containing simulated room impulse responses
(RIRs).
Arguments
---------
decompress_path : str
The path to the directory containing the RIRs
"""
try:
os.rename(
os.path.join(decompress_path, "simulated_rirs_16k"),
os.path.join(decompress_path, "SLR26"),
)
except Exception:
pass
try:
os.rename(
os.path.join(decompress_path, "RIRS_NOISES"),
os.path.join(decompress_path, "SLR28"),
)
except Exception:
pass
def validate_file(download_url, download_path):
"""
Validate the downloaded file and resume the download if needed.
Arguments
---------
download_url : str
URL of the file being downloaded
download_path : str
Full path of the file that is to be downloaded
(or already downloaded)
Returns
-------
bool
If True, the file need not be downloaded again.
Else, either the file is not yet downloaded or
partially downloaded, thus resume the download.
"""
if not os.path.isfile(download_path):
# File not yet downloaded
return False
# Get file size in MB
actual_size = urllib.request.urlopen(
download_url,
context=ssl.create_default_context(cafile=certifi.where()),
).length
download_size = os.path.getsize(download_path)
print(
"File: {}, \t downloaded {} MB out of {} MB".format(
download_path.split("/")[-1],
download_size // (1024 * 1024),
actual_size // (1024 * 1024),
),
end="",
)
# Set a margin of 100 MB. We skip re-downloading the file if downloaded
# size differs from actual size by max 100 MB. More than this margin,
# re-download is to attempted.
if actual_size - download_size < 100 * 1024 * 1024:
return True
else:
print(", \tIncomplete download. Resuming...")
return False
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Download and extract DNS dataset."
)
parser.add_argument(
"--compressed_path",
type=str,
default="DNS-compressed",
help="Path to store the compressed data.",
)
parser.add_argument(
"--decompressed_path",
type=str,
default="DNS-dataset",
help="Path to store the decompressed data.",
)
parser.add_argument(
"--parallel_download",
action="store_true",
help="Use parallel download.",
)
args = parser.parse_args()
COMPRESSED_PATH = args.compressed_path
DECOMPRESSED_PATH = args.decompressed_path
if args.parallel_download:
parallel_download()
else:
prepare_download()
# Modify contents inside RIR_simple_table.csv
file_path = os.path.join(
DECOMPRESSED_PATH, "impulse_responses", "RIR_table_simple.csv"
)
full_path = os.path.abspath(os.path.dirname(file_path))
replacements = {
"datasets/impulse_responses/SLR26/simulated_rirs_16k": os.path.join(
full_path, "SLR26"
),
"datasets/impulse_responses/SLR28/RIRS_NOISES": os.path.join(
full_path, "SLR28"
),
}
# Perform the replacements directly in the file using fileinput module
with fileinput.FileInput(file_path, inplace=True) as file:
for line in file:
for original, replacement in replacements.items():
line = line.replace(original, replacement)
print(line, end="")
if not os.path.exists(
os.path.join("noisyspeech_synthesizer", "RIR_table_simple.csv")
):
shutil.move(file_path, "noisyspeech_synthesizer")