rir-noise / rir-noise.py
yangwang825's picture
Update rir-noise.py
2e3fdc8 verified
# coding=utf-8
"""RIR-Noise dataset."""
import os
import textwrap
import datasets
import itertools
import typing as tp
from pathlib import Path
SAMPLE_RATE = 16_000
_RIR_NOISE_URL = 'https://www.openslr.org/resources/28/rirs_noises.zip'
_AUDIO_TYPES = ['pointsource_noises', 'real_rirs_isotropic_noises', 'simulated_rirs']
class RIRNoiseConfig(datasets.BuilderConfig):
"""BuilderConfig for RIR-Noise."""
def __init__(self, features, **kwargs):
super(RIRNoiseConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
self.features = features
class RIRNoise(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [
RIRNoiseConfig(
features=datasets.Features(
{
"file": datasets.Value("string"),
"audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
"label": datasets.ClassLabel(names=_AUDIO_TYPES),
}
),
name="rir-noise",
description=textwrap.dedent(
"""\
A database of simulated and real room impulse responses, isotropic and point-source noises.
"""
),
),
]
def _info(self):
return datasets.DatasetInfo(
description="A database of simulated and real room impulse responses, isotropic and point-source noises.",
features=self.config.features,
supervised_keys=None,
homepage="https://www.openslr.org/28",
citation="""
@inproceedings{ko2017study,
title={A study on data augmentation of reverberant speech for robust speech recognition},
author={Ko, Tom and Peddinti, Vijayaditya and Povey, Daniel and Seltzer, Michael L and Khudanpur, Sanjeev},
booktitle={2017 IEEE international conference on acoustics, speech and signal processing (ICASSP)},
pages={5220--5224},
year={2017},
organization={IEEE}
}
""",
task_templates=None,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
archive_path = dl_manager.download_and_extract(_RIR_NOISE_URL)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN, gen_kwargs={"archive_path": archive_path, "split": "train"}
),
]
def _generate_examples(self, archive_path, split=None):
extensions = ['.wav']
_, _walker = fast_scandir(archive_path, extensions, recursive=True)
if split == 'train':
_walker = [fileid for fileid in _walker]
for guid, audio_path in enumerate(_walker):
if 'pointsource_noises' in audio_path:
label = 'pointsource_noises'
elif 'real_rirs_isotropic_noises' in audio_path:
label = 'real_rirs_isotropic_noises'
elif 'simulated_rirs' in audio_path:
label = 'simulated_rirs'
yield guid, {
"id": str(guid),
"file": audio_path,
"audio": audio_path,
"label": label,
}
def fast_scandir(path: str, exts: tp.List[str], recursive: bool = False):
# Scan files recursively faster than glob
# From github.com/drscotthawley/aeiou/blob/main/aeiou/core.py
subfolders, files = [], []
try: # hope to avoid 'permission denied' by this try
for f in os.scandir(path):
try: # 'hope to avoid too many levels of symbolic links' error
if f.is_dir():
subfolders.append(f.path)
elif f.is_file():
if os.path.splitext(f.name)[1].lower() in exts:
files.append(f.path)
except Exception:
pass
except Exception:
pass
if recursive:
for path in list(subfolders):
sf, f = fast_scandir(path, exts, recursive=recursive)
subfolders.extend(sf)
files.extend(f) # type: ignore
return subfolders, files