sanchit-gandhi
commited on
Commit
·
9bb4a91
1
Parent(s):
4ed3116
Update tedlium.py
Browse files- tedlium.py +5 -14
tedlium.py
CHANGED
@@ -19,7 +19,7 @@ import re
|
|
19 |
from pathlib import Path
|
20 |
|
21 |
import numpy as np
|
22 |
-
|
23 |
|
24 |
import datasets
|
25 |
from datasets.tasks import AutomaticSpeechRecognition
|
@@ -210,7 +210,7 @@ class TedLium(datasets.GeneratorBasedBuilder):
|
|
210 |
# the .sph speaker file almost always has the same file name as the .stm file
|
211 |
speaker_file = Path(file).stem
|
212 |
audio_file = os.path.join(sph_dir, speaker_file + ".sph")
|
213 |
-
segment =
|
214 |
with open(file) as f:
|
215 |
for line in f:
|
216 |
line = line.strip()
|
@@ -219,12 +219,12 @@ class TedLium(datasets.GeneratorBasedBuilder):
|
|
219 |
if speaker_file != fn:
|
220 |
# handle the case where the stm file does not have the same file name as the transcript
|
221 |
speaker_file = fn
|
222 |
-
segment =
|
223 |
samples = _extract_audio_segment(segment, int(channel), float(start), float(end))
|
224 |
|
225 |
key = "-".join([speaker, start, end, label])
|
226 |
example = {
|
227 |
-
"audio": {"path": file, "array": samples, "sampling_rate":
|
228 |
"text": transcript,
|
229 |
"speaker_id": speaker,
|
230 |
"gender": _parse_gender(label),
|
@@ -234,14 +234,6 @@ class TedLium(datasets.GeneratorBasedBuilder):
|
|
234 |
yield key, example
|
235 |
|
236 |
|
237 |
-
def _read_audio_segment(sph_path):
|
238 |
-
"""Reads segment of audio samples from given sph file path"""
|
239 |
-
with open(sph_path, "rb") as f:
|
240 |
-
segment = AudioSegment.from_file(f, format="nistsphere")
|
241 |
-
assert segment.channels == 1
|
242 |
-
return segment
|
243 |
-
|
244 |
-
|
245 |
def _maybe_trim_suffix(transcript):
|
246 |
# stm files for the TEDLIUM release 1 train split contain a key (enclosed in
|
247 |
# parens) at the end.
|
@@ -260,8 +252,7 @@ def _extract_audio_segment(segment, channel, start_sec, end_sec):
|
|
260 |
assert channel == 1
|
261 |
start_ms = int(start_sec * 1000)
|
262 |
end_ms = int(end_sec * 1000)
|
263 |
-
|
264 |
-
samples = np.array(segment.get_array_of_samples())
|
265 |
return samples
|
266 |
|
267 |
|
|
|
19 |
from pathlib import Path
|
20 |
|
21 |
import numpy as np
|
22 |
+
import soundfile as sf
|
23 |
|
24 |
import datasets
|
25 |
from datasets.tasks import AutomaticSpeechRecognition
|
|
|
210 |
# the .sph speaker file almost always has the same file name as the .stm file
|
211 |
speaker_file = Path(file).stem
|
212 |
audio_file = os.path.join(sph_dir, speaker_file + ".sph")
|
213 |
+
segment, sampling_rate = sf.read(audio_file, dtype=np.int16)
|
214 |
with open(file) as f:
|
215 |
for line in f:
|
216 |
line = line.strip()
|
|
|
219 |
if speaker_file != fn:
|
220 |
# handle the case where the stm file does not have the same file name as the transcript
|
221 |
speaker_file = fn
|
222 |
+
segment, sampling_rate = sf.read(audio_file, dtype=np.int16)
|
223 |
samples = _extract_audio_segment(segment, int(channel), float(start), float(end))
|
224 |
|
225 |
key = "-".join([speaker, start, end, label])
|
226 |
example = {
|
227 |
+
"audio": {"path": file, "array": samples, "sampling_rate": sampling_rate},
|
228 |
"text": transcript,
|
229 |
"speaker_id": speaker,
|
230 |
"gender": _parse_gender(label),
|
|
|
234 |
yield key, example
|
235 |
|
236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
237 |
def _maybe_trim_suffix(transcript):
|
238 |
# stm files for the TEDLIUM release 1 train split contain a key (enclosed in
|
239 |
# parens) at the end.
|
|
|
252 |
assert channel == 1
|
253 |
start_ms = int(start_sec * 1000)
|
254 |
end_ms = int(end_sec * 1000)
|
255 |
+
samples = segment[start_ms:end_ms]
|
|
|
256 |
return samples
|
257 |
|
258 |
|