Create rir-noise.py
Browse files- rir-noise.py +116 -0
rir-noise.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import textwrap
|
| 3 |
+
import datasets
|
| 4 |
+
import itertools
|
| 5 |
+
import typing as tp
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
SAMPLE_RATE = 16_000
|
| 10 |
+
|
| 11 |
+
_RIR_NOISE_URL = 'https://www.openslr.org/resources/28/rirs_noises.zip'
|
| 12 |
+
_AUDIO_TYPES = ['pointsource_noises', 'real_rirs_isotropic_noises', 'simulated_rirs']
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def fast_scandir(path: str, exts: tp.List[str], recursive: bool = False):
|
| 16 |
+
# Scan files recursively faster than glob
|
| 17 |
+
# From github.com/drscotthawley/aeiou/blob/main/aeiou/core.py
|
| 18 |
+
subfolders, files = [], []
|
| 19 |
+
|
| 20 |
+
try: # hope to avoid 'permission denied' by this try
|
| 21 |
+
for f in os.scandir(path):
|
| 22 |
+
try: # 'hope to avoid too many levels of symbolic links' error
|
| 23 |
+
if f.is_dir():
|
| 24 |
+
subfolders.append(f.path)
|
| 25 |
+
elif f.is_file():
|
| 26 |
+
if os.path.splitext(f.name)[1].lower() in exts:
|
| 27 |
+
files.append(f.path)
|
| 28 |
+
except Exception:
|
| 29 |
+
pass
|
| 30 |
+
except Exception:
|
| 31 |
+
pass
|
| 32 |
+
|
| 33 |
+
if recursive:
|
| 34 |
+
for path in list(subfolders):
|
| 35 |
+
sf, f = fast_scandir(path, exts, recursive=recursive)
|
| 36 |
+
subfolders.extend(sf)
|
| 37 |
+
files.extend(f) # type: ignore
|
| 38 |
+
|
| 39 |
+
return subfolders, files
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class RIRNoiseConfig(datasets.BuilderConfig):
|
| 43 |
+
"""BuilderConfig for RIR-Noise."""
|
| 44 |
+
|
| 45 |
+
def __init__(self, features, **kwargs):
|
| 46 |
+
super(RIRNoiseConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
|
| 47 |
+
self.features = features
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class RIRNoise(datasets.GeneratorBasedBuilder):
|
| 51 |
+
|
| 52 |
+
BUILDER_CONFIGS = [
|
| 53 |
+
RIRNoiseConfig(
|
| 54 |
+
features=datasets.Features(
|
| 55 |
+
{
|
| 56 |
+
"file": datasets.Value("string"),
|
| 57 |
+
"audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
|
| 58 |
+
"label": datasets.ClassLabel(names=_AUDIO_TYPES),
|
| 59 |
+
}
|
| 60 |
+
),
|
| 61 |
+
name="rir-noise",
|
| 62 |
+
description=textwrap.dedent(
|
| 63 |
+
"""\
|
| 64 |
+
A database of simulated and real room impulse responses, isotropic and point-source noises.
|
| 65 |
+
"""
|
| 66 |
+
),
|
| 67 |
+
),
|
| 68 |
+
]
|
| 69 |
+
|
| 70 |
+
def _info(self):
|
| 71 |
+
return datasets.DatasetInfo(
|
| 72 |
+
description="A database of simulated and real room impulse responses, isotropic and point-source noises.",
|
| 73 |
+
features=self.config.features,
|
| 74 |
+
supervised_keys=None,
|
| 75 |
+
homepage="https://www.openslr.org/28",
|
| 76 |
+
citation="""
|
| 77 |
+
@inproceedings{ko2017study,
|
| 78 |
+
title={A study on data augmentation of reverberant speech for robust speech recognition},
|
| 79 |
+
author={Ko, Tom and Peddinti, Vijayaditya and Povey, Daniel and Seltzer, Michael L and Khudanpur, Sanjeev},
|
| 80 |
+
booktitle={2017 IEEE international conference on acoustics, speech and signal processing (ICASSP)},
|
| 81 |
+
pages={5220--5224},
|
| 82 |
+
year={2017},
|
| 83 |
+
organization={IEEE}
|
| 84 |
+
}
|
| 85 |
+
""",
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
def _split_generators(self, dl_manager):
|
| 89 |
+
"""Returns SplitGenerators."""
|
| 90 |
+
archive_path = dl_manager.download_and_extract(_RIR_NOISE_URL)
|
| 91 |
+
return [
|
| 92 |
+
datasets.SplitGenerator(
|
| 93 |
+
name=datasets.Split.TRAIN, gen_kwargs={"archive_path": archive_path, "split": "train"}
|
| 94 |
+
),
|
| 95 |
+
]
|
| 96 |
+
|
| 97 |
+
def _generate_examples(self, archive_path, split=None):
|
| 98 |
+
extensions = ['.wav']
|
| 99 |
+
_, _walker = fast_scandir(archive_path, extensions, recursive=True)
|
| 100 |
+
|
| 101 |
+
if split == 'train':
|
| 102 |
+
_walker = [fileid for fileid in _walker]
|
| 103 |
+
|
| 104 |
+
for guid, audio_path in enumerate(_walker):
|
| 105 |
+
if 'pointsource_noises' in audio_path:
|
| 106 |
+
label = 'pointsource_noises'
|
| 107 |
+
elif 'real_rirs_isotropic_noises' in audio_path:
|
| 108 |
+
label = 'real_rirs_isotropic_noises'
|
| 109 |
+
elif 'simulated_rirs' in audio_path:
|
| 110 |
+
label = 'simulated_rirs'
|
| 111 |
+
yield guid, {
|
| 112 |
+
"id": str(guid),
|
| 113 |
+
"file": audio_path,
|
| 114 |
+
"audio": audio_path,
|
| 115 |
+
"label": label,
|
| 116 |
+
}
|