yangwang825 commited on
Commit
e43bd05
·
verified ·
1 Parent(s): d2f9bd0

Create musan.py

Browse files
Files changed (1) hide show
  1. musan.py +115 -0
musan.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+
3
+ """MUSAN 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
+
14
+ SAMPLE_RATE = 16_000
15
+
16
+ _MUSAN_URL = 'https://www.openslr.org/resources/17/musan.tar.gz'
17
+ _AUDIO_TYPES = ['music', 'noise', 'speech']
18
+
19
+
20
+ class MusanConfig(datasets.BuilderConfig):
21
+ """BuilderConfig for MUSAN."""
22
+
23
+ def __init__(self, features, **kwargs):
24
+ super(MusanConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
25
+ self.features = features
26
+
27
+
28
+ class MUSAN(datasets.GeneratorBasedBuilder):
29
+
30
+ BUILDER_CONFIGS = [
31
+ MusanConfig(
32
+ features=datasets.Features(
33
+ {
34
+ "file": datasets.Value("string"),
35
+ "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
36
+ "label": datasets.ClassLabel(names=_AUDIO_TYPES),
37
+ }
38
+ ),
39
+ name="gtzan",
40
+ description=textwrap.dedent(
41
+ """\
42
+ MUSAN is a corpus of music, speech, and noise recordings.
43
+ """
44
+ ),
45
+ ),
46
+ ]
47
+
48
+ def _info(self):
49
+ return datasets.DatasetInfo(
50
+ description="MUSAN is a corpus of music, speech, and noise recordings.",
51
+ features=self.config.features,
52
+ supervised_keys=None,
53
+ homepage="https://www.openslr.org/17",
54
+ citation="""
55
+ @misc{musan2015,
56
+ author = {David Snyder and Guoguo Chen and Daniel Povey},
57
+ title = {{MUSAN}: {A} {M}usic, {S}peech, and {N}oise {C}orpus},
58
+ year = {2015},
59
+ eprint = {1510.08484},
60
+ note = {arXiv:1510.08484v1}
61
+ }
62
+ """,
63
+ task_templates=None,
64
+ )
65
+
66
+ def _split_generators(self, dl_manager):
67
+ """Returns SplitGenerators."""
68
+ archive_path = dl_manager.download_and_extract(_MUSAN_URL)
69
+ return [
70
+ datasets.SplitGenerator(
71
+ name=datasets.Split.TRAIN, gen_kwargs={"archive_path": archive_path, "split": "train"}
72
+ ),
73
+ ]
74
+
75
+ def _generate_examples(self, archive_path, split=None):
76
+ extensions = ['.wav']
77
+ _, _walker = fast_scandir(archive_path, extensions, recursive=True)
78
+
79
+ if split == 'train':
80
+ _walker = [fileid for fileid in _walker]
81
+
82
+ for guid, audio_path in enumerate(_walker):
83
+ yield guid, {
84
+ "id": str(guid),
85
+ "file": audio_path,
86
+ "audio": audio_path,
87
+ "label": Path(audio_path).parent.parent.stem,
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