File size: 7,425 Bytes
cdc5412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from collections import defaultdict
import os
import json
import csv

import datasets

import torchaudio
import warnings

_NAME="samromur_children"
_VERSION="1.0.0"
_AUDIO_EXTENSIONS=".flac"

_DESCRIPTION = """
The Samrómur Children corpus contains more than 137000 validated speech-recordings uttered by Icelandic children. 
"""

_CITATION = """
@misc{menasamromurchildren2022,
      title={Samrómur Children Icelandic Speech 1.0}, 
      ldc_catalog_no={LDC2022S11},
      DOI={https://doi.org/10.35111/frrj-qd60},
      author={Hernández Mena, Carlos Daniel and Borsky, Michal and Mollberg, David Erik  and Guðmundsson, Smári Freyr and Hedström, Staffan and Pálsson, Ragnar and Jónsson, Ólafur Helgi and Þorsteinsdóttir, Sunneva and Guðmundsdóttir, Jóhanna Vigdís and Magnúsdóttir, Eydís Huld and Þórhallsdóttir, Ragnheiður and Guðnason, Jón},
      publisher={Reykjavík University}
      journal={Linguistic Data Consortium, Philadelphia},
      year={2019},
      url={https://catalog.ldc.upenn.edu/LDC2022S11},
}
"""

_HOMEPAGE = "https://catalog.ldc.upenn.edu/LDC2022S11"

_LICENSE = "CC-BY-4.0, See https://creativecommons.org/licenses/by/4.0/"

_BASE_DATA_DIR = "corpus/"
_METADATA_TRAIN =  os.path.join(_BASE_DATA_DIR,"files","metadata_train.tsv")
_METADATA_TEST  =  os.path.join(_BASE_DATA_DIR,"files", "metadata_test.tsv")
_METADATA_DEV   =  os.path.join(_BASE_DATA_DIR,"files",  "metadata_dev.tsv")

_TARS_TRAIN = os.path.join(_BASE_DATA_DIR,"files","tars_train.paths")
_TARS_TEST  = os.path.join(_BASE_DATA_DIR,"files", "tars_test.paths")
_TARS_DEV   = os.path.join(_BASE_DATA_DIR,"files",  "tars_dev.paths")

class SamromurChildrenConfig(datasets.BuilderConfig):
    """BuilderConfig for Samromur Children"""

    def __init__(self, name, **kwargs):
        name=_NAME
        super().__init__(name=name, **kwargs)

class SamromurChildren(datasets.GeneratorBasedBuilder):
    """Samrómur Children Icelandic Speech 1.0"""

    VERSION = datasets.Version(_VERSION)
    BUILDER_CONFIGS = [
        SamromurChildrenConfig(
            name=_NAME,
            version=datasets.Version(_VERSION),
        )
    ]

    def _info(self):
        features = datasets.Features(
            {
                "audio_id": datasets.Value("string"),
                "audio": datasets.Audio(sampling_rate=16000),
                "speaker_id": datasets.Value("string"),
                "gender": datasets.Value("string"),
                "age": datasets.Value("string"),
                "duration": datasets.Value("float32"),
                "normalized_text": datasets.Value("string"),
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
    
        metadata_train=dl_manager.download_and_extract(_METADATA_TRAIN)
        metadata_test=dl_manager.download_and_extract(_METADATA_TEST)
        metadata_dev=dl_manager.download_and_extract(_METADATA_DEV)   
        
        tars_train=dl_manager.download_and_extract(_TARS_TRAIN)
        tars_test=dl_manager.download_and_extract(_TARS_TEST)
        tars_dev=dl_manager.download_and_extract(_TARS_DEV)
        
        hash_tar_files=defaultdict(dict)
        with open(tars_train,'r') as f:
            hash_tar_files['train']=[path.replace('\n','') for path in f]

        with open(tars_test,'r') as f:
            hash_tar_files['test']=[path.replace('\n','') for path in f]
            
        with open(tars_dev,'r') as f:
            hash_tar_files['dev']=[path.replace('\n','') for path in f]
    
        hash_meta_paths={"train":metadata_train,"test":metadata_test,"dev":metadata_dev}
        audio_paths = dl_manager.download(hash_tar_files)
        
        splits=["train","dev","test"]
        local_extracted_audio_paths = (
            dl_manager.extract(audio_paths) if not dl_manager.is_streaming else
            {
                split:[None] * len(audio_paths[split]) for split in splits
            }
        )                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "audio_archives":[dl_manager.iter_archive(archive) for archive in audio_paths["train"]],
                    "local_extracted_archives_paths": local_extracted_audio_paths["train"],
                    "metadata_paths": hash_meta_paths["train"],
                }
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["dev"]],
                    "local_extracted_archives_paths": local_extracted_audio_paths["dev"],
                    "metadata_paths": hash_meta_paths["dev"],
                }
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "audio_archives": [dl_manager.iter_archive(archive) for archive in audio_paths["test"]],
                    "local_extracted_archives_paths": local_extracted_audio_paths["test"],
                    "metadata_paths": hash_meta_paths["test"],
                }
            ),
        ]

    def _generate_examples(self, audio_archives, local_extracted_archives_paths, metadata_paths):

        features = ["speaker_id","gender","age","duration","normalized_text"]
        
        with open(metadata_paths) as f:
            metadata = {x["audio_id"]: x for x in csv.DictReader(f, delimiter="\t")}

        for audio_archive, local_extracted_archive_path in zip(audio_archives, local_extracted_archives_paths):
            for audio_filename, audio_file in audio_archive:
                #audio_id = audio_filename.split(os.sep)[-1].split(_AUDIO_EXTENSIONS)[0]
                audio_id =os.path.splitext(os.path.basename(audio_filename))[0]
                path = os.path.join(local_extracted_archive_path, audio_filename) if local_extracted_archive_path else audio_filename

                # Load the audio file using torchaudio
                waveform, sample_rate = torchaudio.load(path)
            
                # Check if the waveform is empty
                if waveform.numel() == 0:
                    warnings.warn(f"Empty audio file: {str(audio_id)}")
                    continue
                                           
                yield audio_id, {
                    "audio_id": audio_id,
                    **{feature: metadata[audio_id][feature] for feature in features},
                    "audio": {"path": path, "bytes": audio_file.read()},
                }