File size: 2,428 Bytes
8d7d253
145e867
 
6c4ff3d
8d7d253
145e867
6c4ff3d
8d7d253
 
 
 
 
 
 
145e867
8d7d253
145e867
8d7d253
 
554e7c2
6c4ff3d
 
145e867
 
 
6c4ff3d
145e867
 
 
6c4ff3d
145e867
 
 
6c4ff3d
145e867
 
 
6c4ff3d
 
 
 
145e867
6c4ff3d
 
145e867
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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):
        # Each split is a folder: train/, validation/, test/
        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):
        # Folders inside split_dir: wav/ and phonemized/
        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,
            }