forked from speechbrain/speechbrain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_data.py
208 lines (184 loc) · 6.32 KB
/
prepare_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""
The functions to create the .csv files for LibriMix
Author
* Cem Subakan 2020
"""
import csv
import os
def prepare_librimix(
datapath,
savepath,
n_spks=2,
skip_prep=False,
librimix_addnoise=False,
fs=8000,
):
"""
Prepare .csv files for librimix
Arguments:
----------
datapath (str) : path for the wsj0-mix dataset.
savepath (str) : path where we save the csv file.
n_spks (int): number of speakers
skip_prep (bool): If True, skip data preparation
librimix_addnoise: If True, add whamnoise to librimix datasets
"""
if skip_prep:
return
if "Libri" in datapath:
# Libri 2/3Mix datasets
if n_spks == 2:
assert (
"Libri2Mix" in datapath
), "Inconsistent number of speakers and datapath"
create_libri2mix_csv(datapath, savepath, addnoise=librimix_addnoise)
elif n_spks == 3:
assert (
"Libri3Mix" in datapath
), "Inconsistent number of speakers and datapath"
create_libri3mix_csv(datapath, savepath, addnoise=librimix_addnoise)
else:
raise ValueError("Unsupported Number of Speakers")
else:
raise ValueError("Unsupported Dataset")
def create_libri2mix_csv(
datapath,
savepath,
addnoise=False,
version="wav8k/min/",
set_types=["train-360", "dev", "test"],
):
"""
This functions creates the .csv file for the libri2mix dataset
"""
for set_type in set_types:
if addnoise:
mix_path = os.path.join(datapath, version, set_type, "mix_both/")
else:
mix_path = os.path.join(datapath, version, set_type, "mix_clean/")
s1_path = os.path.join(datapath, version, set_type, "s1/")
s2_path = os.path.join(datapath, version, set_type, "s2/")
noise_path = os.path.join(datapath, version, set_type, "noise/")
files = os.listdir(mix_path)
mix_fl_paths = [mix_path + fl for fl in files]
s1_fl_paths = [s1_path + fl for fl in files]
s2_fl_paths = [s2_path + fl for fl in files]
noise_fl_paths = [noise_path + fl for fl in files]
csv_columns = [
"ID",
"duration",
"mix_wav",
"mix_wav_format",
"mix_wav_opts",
"s1_wav",
"s1_wav_format",
"s1_wav_opts",
"s2_wav",
"s2_wav_format",
"s2_wav_opts",
"noise_wav",
"noise_wav_format",
"noise_wav_opts",
]
with open(savepath + "/libri2mix_" + set_type + ".csv", "w") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for i, (mix_path, s1_path, s2_path, noise_path) in enumerate(
zip(mix_fl_paths, s1_fl_paths, s2_fl_paths, noise_fl_paths)
):
row = {
"ID": i,
"duration": 1.0,
"mix_wav": mix_path,
"mix_wav_format": "wav",
"mix_wav_opts": None,
"s1_wav": s1_path,
"s1_wav_format": "wav",
"s1_wav_opts": None,
"s2_wav": s2_path,
"s2_wav_format": "wav",
"s2_wav_opts": None,
"noise_wav": noise_path,
"noise_wav_format": "wav",
"noise_wav_opts": None,
}
writer.writerow(row)
def create_libri3mix_csv(
datapath,
savepath,
addnoise=False,
version="wav8k/min/",
set_types=["train-360", "dev", "test"],
):
"""
This functions creates the .csv file for the libri3mix dataset
"""
for set_type in set_types:
if addnoise:
mix_path = os.path.join(datapath, version, set_type, "mix_both/")
else:
mix_path = os.path.join(datapath, version, set_type, "mix_clean/")
s1_path = os.path.join(datapath, version, set_type, "s1/")
s2_path = os.path.join(datapath, version, set_type, "s2/")
s3_path = os.path.join(datapath, version, set_type, "s3/")
noise_path = os.path.join(datapath, version, set_type, "noise/")
files = os.listdir(mix_path)
mix_fl_paths = [mix_path + fl for fl in files]
s1_fl_paths = [s1_path + fl for fl in files]
s2_fl_paths = [s2_path + fl for fl in files]
s3_fl_paths = [s3_path + fl for fl in files]
noise_fl_paths = [noise_path + fl for fl in files]
csv_columns = [
"ID",
"duration",
"mix_wav",
"mix_wav_format",
"mix_wav_opts",
"s1_wav",
"s1_wav_format",
"s1_wav_opts",
"s2_wav",
"s2_wav_format",
"s2_wav_opts",
"s3_wav",
"s3_wav_format",
"s3_wav_opts",
"noise_wav",
"noise_wav_format",
"noise_wav_opts",
]
with open(savepath + "/libri3mix_" + set_type + ".csv", "w") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for (
i,
(mix_path, s1_path, s2_path, s3_path, noise_path),
) in enumerate(
zip(
mix_fl_paths,
s1_fl_paths,
s2_fl_paths,
s3_fl_paths,
noise_fl_paths,
)
):
row = {
"ID": i,
"duration": 1.0,
"mix_wav": mix_path,
"mix_wav_format": "wav",
"mix_wav_opts": None,
"s1_wav": s1_path,
"s1_wav_format": "wav",
"s1_wav_opts": None,
"s2_wav": s2_path,
"s2_wav_format": "wav",
"s2_wav_opts": None,
"s3_wav": s3_path,
"s3_wav_format": "wav",
"s3_wav_opts": None,
"noise_wav": noise_path,
"noise_wav_format": "wav",
"noise_wav_opts": None,
}
writer.writerow(row)