Create speech_segment.py
Browse files- speech_segment.py +101 -0
speech_segment.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Lint as: python3
|
| 2 |
+
"""Speech Segment dataset.
|
| 3 |
+
"""
|
| 4 |
+
import os
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
import datasets
|
| 8 |
+
import torchaudio
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class SpeechSegmentConfig(datasets.BuilderConfig):
|
| 12 |
+
"""BuilderConfig for Speech Segment.
|
| 13 |
+
For long audio files, segment them into smaller segments of fixed length.
|
| 14 |
+
For short audio files, return the whole audio file.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
def __init__(self, segment_length, **kwargs):
|
| 18 |
+
super(SpeechSegmentConfig, self).__init__(**kwargs)
|
| 19 |
+
self.segment_length = segment_length
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class SpeechSegment(datasets.GeneratorBasedBuilder):
|
| 23 |
+
"""Speech Segment dataset."""
|
| 24 |
+
|
| 25 |
+
BUILDER_CONFIGS = [
|
| 26 |
+
SpeechSegmentConfig(name="all", segment_length=60.0,),
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
@property
|
| 30 |
+
def manual_download_instructions(self):
|
| 31 |
+
return (
|
| 32 |
+
"Specify the data_dir as the path to the folder, will recursively search for .flac and .wav files. "
|
| 33 |
+
"`datasets.load_dataset('subatomicseer/speech_segment', data_dir='path/to/folder/folder_name')`"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
def _info(self):
|
| 37 |
+
features = datasets.Features(
|
| 38 |
+
{
|
| 39 |
+
"id": datasets.Value("string"),
|
| 40 |
+
"file": datasets.Value("string"),
|
| 41 |
+
'sample_rate': datasets.Value('int64'),
|
| 42 |
+
'offset': datasets.Value('int64'),
|
| 43 |
+
'num_frames': datasets.Value('int64'),
|
| 44 |
+
}
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
return datasets.DatasetInfo(
|
| 48 |
+
features=features,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
def _split_generators(self, dl_manager):
|
| 52 |
+
base_data_dir = os.path.abspath(os.path.expanduser(dl_manager.manual_dir))
|
| 53 |
+
if not os.path.exists(base_data_dir):
|
| 54 |
+
raise FileNotFoundError(
|
| 55 |
+
f"{base_data_dir} does not exist. Manual download instructions: {self.manual_download_instructions}"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
data_dirs = [str(p) for p in Path(base_data_dir).rglob('*') if p.suffix in ['.flac', '.wav']]
|
| 59 |
+
print(f"Found {len(data_dirs)} audio files in {base_data_dir}")
|
| 60 |
+
return [
|
| 61 |
+
datasets.SplitGenerator(
|
| 62 |
+
name=datasets.Split.TRAIN,
|
| 63 |
+
gen_kwargs={"data_dirs": data_dirs},
|
| 64 |
+
),
|
| 65 |
+
]
|
| 66 |
+
|
| 67 |
+
def _generate_examples(self, data_dirs):
|
| 68 |
+
for key, path in enumerate(data_dirs):
|
| 69 |
+
path_split = path.split("/")
|
| 70 |
+
id_ = '/'.join(path_split[-4:]).replace(".flac", "")
|
| 71 |
+
|
| 72 |
+
audio_metadata = torchaudio.info(path)
|
| 73 |
+
segment_length = int(self.config.segment_length * audio_metadata.sample_rate)
|
| 74 |
+
total_length = audio_metadata.num_frames
|
| 75 |
+
|
| 76 |
+
if total_length <= segment_length:
|
| 77 |
+
yield id_, {
|
| 78 |
+
"id": id_,
|
| 79 |
+
"file": path,
|
| 80 |
+
'sample_rate': audio_metadata.sample_rate,
|
| 81 |
+
'offset': 0,
|
| 82 |
+
'num_frames': total_length,
|
| 83 |
+
}
|
| 84 |
+
else:
|
| 85 |
+
# generate non-overlapping segments of segment_length
|
| 86 |
+
offsets = list(range(0, total_length, segment_length))
|
| 87 |
+
if total_length - offsets[-1] < 1 * audio_metadata.sample_rate:
|
| 88 |
+
# if the last segment is less than 2 seconds, discard it
|
| 89 |
+
offsets.pop()
|
| 90 |
+
|
| 91 |
+
for segment_id, start in enumerate(offsets):
|
| 92 |
+
end = start + segment_length - 1
|
| 93 |
+
if end > total_length:
|
| 94 |
+
end = total_length
|
| 95 |
+
yield f'{id_}_{segment_id}', {
|
| 96 |
+
"id": f'{id_}_{segment_id}',
|
| 97 |
+
"file": path,
|
| 98 |
+
'sample_rate': audio_metadata.sample_rate,
|
| 99 |
+
'offset': start,
|
| 100 |
+
'num_frames': end-start+1,
|
| 101 |
+
}
|