| import datasets |
| from pathlib import Path |
|
|
| _DESCRIPTION = "Custom dataset with audio (.wav) and phoneme (.txt) pairs, sharded by split." |
|
|
| class CustomAudioPhonemeDataset(datasets.GeneratorBasedBuilder): |
| VERSION = datasets.Version("1.1.0") |
|
|
| def _info(self): |
| return datasets.DatasetInfo( |
| description=_DESCRIPTION, |
| features=datasets.Features({ |
| "audio": datasets.Audio(sampling_rate=16000), |
| "phoneme": datasets.Sequence(datasets.Value("string")), |
| "speaker": datasets.Value("string"), |
| }), |
| supervised_keys=None, |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| |
| data_dir = Path(dl_manager.manual_dir) |
| return [ |
| datasets.SplitGenerator( |
| name=datasets.Split.TRAIN, |
| gen_kwargs={"split_dir": data_dir / "train"}, |
| ), |
| datasets.SplitGenerator( |
| name=datasets.Split.VALIDATION, |
| gen_kwargs={"split_dir": data_dir / "validation"}, |
| ), |
| datasets.SplitGenerator( |
| name=datasets.Split.TEST, |
| gen_kwargs={"split_dir": data_dir / "test"}, |
| ), |
| ] |
|
|
| def _generate_examples(self, split_dir): |
| |
| wav_dir = Path(split_dir) / "wav" |
| phoneme_dir = Path(split_dir) / "phonemized" |
|
|
| audio_files = sorted([p for p in wav_dir.rglob("*.wav") if not p.name.startswith("._")]) |
| phoneme_files = sorted([p for p in phoneme_dir.rglob("*.txt") if not p.name.startswith("._")]) |
|
|
| def get_speaker(path): |
| return path.parent.name |
|
|
| audio_map = {(get_speaker(p), p.stem): p for p in audio_files} |
| phoneme_map = {(get_speaker(p), p.stem): p for p in phoneme_files} |
|
|
| keys = set(audio_map.keys()) & set(phoneme_map.keys()) |
|
|
| for idx, (speaker, stem) in enumerate(sorted(keys)): |
| audio_path = str(audio_map[(speaker, stem)]) |
| phoneme_path = str(phoneme_map[(speaker, stem)]) |
|
|
| with open(phoneme_path, 'r', encoding='utf-8') as f: |
| phoneme = f.read().split() |
|
|
| yield idx, { |
| "audio": {"path": audio_path, "bytes": None}, |
| "phoneme": phoneme, |
| "speaker": speaker, |
| } |
|
|