yangwang825 commited on
Commit
30b86ca
·
verified ·
1 Parent(s): f5365e2

Create librispeech-script.py

Browse files
Files changed (1) hide show
  1. librispeech-script.py +115 -0
librispeech-script.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+
3
+ """LibriSpeech Speaker Identification dataset."""
4
+
5
+
6
+ import os
7
+ import textwrap
8
+ import datasets
9
+ import itertools
10
+ import typing as tp
11
+ from pathlib import Path
12
+
13
+ from ._librispeech import OFFICIAL_TRAIN, OFFICIAL_TEST
14
+
15
+ SAMPLE_RATE = 16_000
16
+
17
+ _COMPRESSED_FILENAME = 'librispeech.tar.gz'
18
+
19
+ CLASSES = list(sorted(set([Path(audio_path).stem.split('-')[0] for audio_path in OFFICIAL_TRAIN])))
20
+
21
+
22
+ class LibriSpeechConfig(datasets.BuilderConfig):
23
+ """BuilderConfig for LibriSpeech."""
24
+
25
+ def __init__(self, features, **kwargs):
26
+ super(LibriSpeechConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
27
+ self.features = features
28
+
29
+
30
+ class LibriSpeech(datasets.GeneratorBasedBuilder):
31
+
32
+ BUILDER_CONFIGS = [
33
+ LibriSpeechConfig(
34
+ features=datasets.Features(
35
+ {
36
+ "file": datasets.Value("string"),
37
+ "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
38
+ "speaker_id": datasets.Value("string"),
39
+ "label": datasets.ClassLabel(names=CLASSES),
40
+ }
41
+ ),
42
+ name="librispeech",
43
+ description='',
44
+ ),
45
+ ]
46
+
47
+ def _info(self):
48
+ return datasets.DatasetInfo(
49
+ description="",
50
+ features=self.config.features,
51
+ supervised_keys=None,
52
+ homepage="",
53
+ citation="",
54
+ task_templates=None,
55
+ )
56
+
57
+ def _split_generators(self, dl_manager):
58
+ """Returns SplitGenerators."""
59
+ archive_path = dl_manager.extract(_COMPRESSED_FILENAME)
60
+ return [
61
+ datasets.SplitGenerator(
62
+ name=datasets.Split.TRAIN, gen_kwargs={"archive_path": archive_path, "split": "train"}
63
+ ),
64
+ datasets.SplitGenerator(
65
+ name=datasets.Split.TEST, gen_kwargs={"archive_path": archive_path, "split": "test"}
66
+ ),
67
+ ]
68
+
69
+ def _generate_examples(self, archive_path, split=None):
70
+ extensions = ['.wav']
71
+ _, _walker = fast_scandir(archive_path, extensions, recursive=True)
72
+
73
+ if split == 'train':
74
+ _walker = [fileid for fileid in _walker if Path(fileid).stem in OFFICIAL_TRAIN]
75
+ elif split == 'test':
76
+ _walker = [fileid for fileid in _walker if Path(fileid).stem in OFFICIAL_TEST]
77
+
78
+ def default_find_classes(audio_path):
79
+ return Path(audio_path).stem.split('-')[0]
80
+
81
+ for guid, audio_path in enumerate(_walker):
82
+ yield guid, {
83
+ "id": str(guid),
84
+ "file": audio_path,
85
+ "audio": audio_path,
86
+ "speaker_id": default_find_classes(audio_path),
87
+ "label": default_find_classes(audio_path),
88
+ }
89
+
90
+
91
+ def fast_scandir(path: str, exts: tp.List[str], recursive: bool = False):
92
+ # Scan files recursively faster than glob
93
+ # From github.com/drscotthawley/aeiou/blob/main/aeiou/core.py
94
+ subfolders, files = [], []
95
+
96
+ try: # hope to avoid 'permission denied' by this try
97
+ for f in os.scandir(path):
98
+ try: # 'hope to avoid too many levels of symbolic links' error
99
+ if f.is_dir():
100
+ subfolders.append(f.path)
101
+ elif f.is_file():
102
+ if os.path.splitext(f.name)[1].lower() in exts:
103
+ files.append(f.path)
104
+ except Exception:
105
+ pass
106
+ except Exception:
107
+ pass
108
+
109
+ if recursive:
110
+ for path in list(subfolders):
111
+ sf, f = fast_scandir(path, exts, recursive=recursive)
112
+ subfolders.extend(sf)
113
+ files.extend(f) # type: ignore
114
+
115
+ return subfolders, files