| |
|
|
| """LibriSpeech Speaker Identification dataset.""" |
|
|
|
|
| import os |
| import textwrap |
| import datasets |
| import itertools |
| import typing as tp |
| from pathlib import Path |
|
|
| from ._librispeech import OFFICIAL_TRAIN, OFFICIAL_TEST |
|
|
| SAMPLE_RATE = 16_000 |
|
|
| _COMPRESSED_FILENAME = 'librispeech.tar.gz' |
|
|
| CLASSES = list(sorted(set([Path(audio_path).stem.split('-')[0] for audio_path in OFFICIAL_TRAIN]))) |
|
|
|
|
| class LibriSpeechConfig(datasets.BuilderConfig): |
| """BuilderConfig for LibriSpeech.""" |
| |
| def __init__(self, features, **kwargs): |
| super(LibriSpeechConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs) |
| self.features = features |
|
|
|
|
| class LibriSpeech(datasets.GeneratorBasedBuilder): |
|
|
| BUILDER_CONFIGS = [ |
| LibriSpeechConfig( |
| features=datasets.Features( |
| { |
| "file": datasets.Value("string"), |
| "audio": datasets.Audio(sampling_rate=SAMPLE_RATE), |
| "speaker_id": datasets.Value("string"), |
| "label": datasets.ClassLabel(names=CLASSES), |
| } |
| ), |
| name="librispeech", |
| description='', |
| ), |
| ] |
|
|
| def _info(self): |
| return datasets.DatasetInfo( |
| description="", |
| features=self.config.features, |
| supervised_keys=None, |
| homepage="", |
| citation="", |
| task_templates=None, |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| """Returns SplitGenerators.""" |
| archive_path = dl_manager.extract(_COMPRESSED_FILENAME) |
| return [ |
| datasets.SplitGenerator( |
| name=datasets.Split.TRAIN, gen_kwargs={"archive_path": archive_path, "split": "train"} |
| ), |
| datasets.SplitGenerator( |
| name=datasets.Split.TEST, gen_kwargs={"archive_path": archive_path, "split": "test"} |
| ), |
| ] |
|
|
| 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 if Path(fileid).name in OFFICIAL_TRAIN] |
| elif split == 'test': |
| _walker = [fileid for fileid in _walker if Path(fileid).name in OFFICIAL_TEST] |
|
|
| def default_find_classes(audio_path): |
| return Path(audio_path).stem.split('-')[0] |
|
|
| for guid, audio_path in enumerate(_walker): |
| yield guid, { |
| "id": str(guid), |
| "file": audio_path, |
| "audio": audio_path, |
| "speaker_id": default_find_classes(audio_path), |
| "label": default_find_classes(audio_path), |
| } |
|
|
|
|
| def fast_scandir(path: str, exts: tp.List[str], recursive: bool = False): |
| |
| |
| subfolders, files = [], [] |
|
|
| try: |
| for f in os.scandir(path): |
| try: |
| 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) |
|
|
| return subfolders, files |