Datasets:

Modalities:
Audio
Text
Formats:
parquet
Languages:
English
ArXiv:
Libraries:
Datasets
Dask
License:
File size: 11,987 Bytes
f9c2a6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1534e7
c9a9063
d1534e7
f9c2a6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# coding=utf-8
# Copyright 2024 blabble.io
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os

import datasets
from datasets import load_dataset
from datasets.features.features import require_decoding
from datasets.table import embed_table_storage
from datasets.utils.py_utils import convert_file_size_to_int
from tqdm import tqdm

_CITATION = """\
@ARTICLE{Zen2019-kz,
  title         = "{LibriTTS}: A corpus derived from {LibriSpeech} for
                   text-to-speech",
  author        = "Zen, Heiga and Dang, Viet and Clark, Rob and Zhang, Yu and
                   Weiss, Ron J and Jia, Ye and Chen, Zhifeng and Wu, Yonghui",
  abstract      = "This paper introduces a new speech corpus called
                   ``LibriTTS'' designed for text-to-speech use. It is derived
                   from the original audio and text materials of the
                   LibriSpeech corpus, which has been used for training and
                   evaluating automatic speech recognition systems. The new
                   corpus inherits desired properties of the LibriSpeech corpus
                   while addressing a number of issues which make LibriSpeech
                   less than ideal for text-to-speech work. The released corpus
                   consists of 585 hours of speech data at 24kHz sampling rate
                   from 2,456 speakers and the corresponding texts.
                   Experimental results show that neural end-to-end TTS models
                   trained from the LibriTTS corpus achieved above 4.0 in mean
                   opinion scores in naturalness in five out of six evaluation
                   speakers. The corpus is freely available for download from
                   http://www.openslr.org/60/.",
  month         =  apr,
  year          =  2019,
  copyright     = "http://arxiv.org/licenses/nonexclusive-distrib/1.0/",
  archivePrefix = "arXiv",
  primaryClass  = "cs.SD",
  eprint        = "1904.02882"
}
"""

_DESCRIPTION = """\
LibriTTS is a multi-speaker English corpus of approximately 585 hours of read English speech at 24kHz sampling rate, 
prepared by Heiga Zen with the assistance of Google Speech and Google Brain team members. The LibriTTS corpus is 
designed for TTS research. It is derived from the original materials (mp3 audio files from LibriVox and text files 
from Project Gutenberg) of the LibriSpeech corpus.
"""

_HOMEPAGE = "https://www.openslr.org/60/"

_LICENSE = "CC BY 4.0"

# EU mirror was much faster at time of writing
_DL_URL = "https://openslr.elda.org/resources/60/"
# _DL_URL = "https://us.openslr.org/resources/60/"

_DATA_URLS = {
    'dev.clean': _DL_URL + 'dev-clean.tar.gz',
    'dev.other': _DL_URL + 'dev-other.tar.gz',
    'test.clean': _DL_URL + 'test-clean.tar.gz',
    'test.other': _DL_URL + 'test-other.tar.gz',
    'train.clean.100': _DL_URL + 'train-clean-100.tar.gz',
    'train.clean.360': _DL_URL + 'train-clean-360.tar.gz',
    'train.other.500': _DL_URL + 'train-other-500.tar.gz',
}


def _generate_transcripts(transcript_csv_file):
    """Generates partial examples from transcript CSV file."""
    for line in transcript_csv_file:
        key, text_original, text_normalized = line.decode("utf-8").replace('\n', '').split("\t")
        speaker_id, chapter_id = [int(el) for el in key.split("_")[:2]]
        example = {
            "text_normalized": text_normalized,
            "text_original": text_original,
            "speaker_id": speaker_id,
            "chapter_id": chapter_id,
            "id_": key,
        }
        yield example


class LibriTTS_Dataset(datasets.GeneratorBasedBuilder):
    """
    LibriTTS is a multi-speaker English corpus of approximately 585 hours of read English speech at 24kHz sampling rate,
    prepared by Heiga Zen with the assistance of Google Speech and Google Brain team members.
    """

    VERSION = datasets.Version("1.0.0")

    DEFAULT_CONFIG_NAME = "all"
    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="dev", description="Only the 'dev.clean' split."),
        datasets.BuilderConfig(name="clean", description="'Clean' speech."),
        datasets.BuilderConfig(name="other", description="'Other', more challenging, speech."),
        datasets.BuilderConfig(name="all", description="Combined clean and other dataset."),
    ]

    def _info(self):
        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "audio": datasets.Audio(sampling_rate=24_000),
                    "text_normalized": datasets.Value("string"),
                    "text_original": datasets.Value("string"),
                    "speaker_id": datasets.Value("string"),
                    "path": datasets.Value("string"),
                    "chapter_id": datasets.Value("string"),
                    "id": datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        split_names = _DATA_URLS.keys()

        if self.config.name == "clean":
            split_names = [k for k in _DATA_URLS.keys() if 'clean' in k]
        elif self.config.name == "other":
            split_names = [k for k in _DATA_URLS.keys() if 'other' in k]

        archive_path = dl_manager.download({k: v for k, v in _DATA_URLS.items() if k in split_names})

        # (Optional) In non-streaming mode, we can extract the archive locally to have actual local audio files:
        local_extracted_archive = dl_manager.extract(archive_path) if not dl_manager.is_streaming else {}

        all_splits = [
            datasets.SplitGenerator(
                name=split_name,
                gen_kwargs={
                    "local_extracted_archive": local_extracted_archive.get(split_name),
                    "files": dl_manager.iter_archive(archive_path[split_name]),
                    "split_name": split_name
                },
            ) for split_name in split_names
        ]

        return all_splits

    def _generate_examples(self, split_name, files, local_extracted_archive):
        """Generate examples from a LibriTTS archive_path."""
        audio_extension = '.wav'

        key = 0
        all_audio_data = {}
        transcripts = {}

        def get_return_data(transcript, audio_data):
            nonlocal key

            audio = {"path": transcript["path"], "bytes": audio_data}
            key += 1

            return key, {"audio": audio, **transcript}

        for path, f in files:
            if path.endswith(audio_extension):
                id_ = path.split("/")[-1][: -len(audio_extension)]

                audio_data = f.read()

                # If we already have the transcript for this audio, yield it right away
                #   Otherwise, save it for when we get the transcript.
                transcript = transcripts.get(id_, None)

                if transcript is not None:
                    yield get_return_data(transcript, audio_data)
                    del transcripts[id_]
                else:
                    all_audio_data[id_] = f.read()

            elif path.endswith(".trans.tsv"):
                for example in _generate_transcripts(f):
                    example_id = example['id_']

                    audio_file = f"{example_id}{audio_extension}"

                    audio_file = (
                        os.path.join(
                            local_extracted_archive, 'LibriTTS',
                            split_name.replace('.', '-'),
                            str(example['speaker_id']), str(example['chapter_id']), audio_file)
                        if local_extracted_archive
                        else audio_file
                    )

                    transcript = {
                        "id": example_id,
                        "speaker_id": example['speaker_id'],
                        "chapter_id": example['chapter_id'],
                        "text_normalized": example['text_normalized'],
                        "text_original": example['text_original'],
                        "path": audio_file,
                    }

                    # If we already have the audio for this transcript, yield it right away
                    #   Otherwise, save it for when we get the audio.
                    audio_data = all_audio_data.get(example_id, None)
                    if audio_data is not None:
                        yield get_return_data(transcript, audio_data)
                        del all_audio_data[example_id]
                    else:
                        transcripts[example_id] = transcript

        for id_, audio_data in all_audio_data.items():
            transcript = transcripts.get(id_, None)

            if transcript is None:
                # for debugging, this dataset may extra audio
                # print(f"[libritts {split_name}] Audio without transcript: {id_}")
                continue

            else:
                yield get_return_data(transcript, audio_data)
                del transcripts[id_]

        for id_, transcript in transcripts.items():
            audio_data = all_audio_data.get(id_, None)

            if audio_data is None:
                # for debugging, this dataset has extra transcripts
                # print(f"[libritts {split_name}] Transcript without audio: {id_}")
                continue

            else:
                yield get_return_data(audio_data, transcript)
                # no del needed here


def to_parquet_with_audio(dataset, data_out_dir, split_name, max_shard_size='500MB'):
    from datasets import config

    # decodable_columns = (
    #     [k for k, v in dataset.features.items() if require_decoding(v, ignore_decode_attribute=True)]
    # )
    dataset_nbytes = dataset._estimate_nbytes()
    max_shard_size = convert_file_size_to_int(max_shard_size or config.MAX_SHARD_SIZE)
    num_shards = int(dataset_nbytes / max_shard_size) + 1
    num_shards = max(num_shards, 1)
    shards = (dataset.shard(num_shards=num_shards, index=i, contiguous=True) for i in range(num_shards))

    def shards_with_embedded_external_files(shards):
        for shard in shards:
            format = shard.format
            shard = shard.with_format("arrow")
            shard = shard.map(
                embed_table_storage,
                batched=True,
                batch_size=1000,
                keep_in_memory=True,
            )
            shard = shard.with_format(**format)
            yield shard

    shards = shards_with_embedded_external_files(shards)

    os.makedirs(data_out_dir, exist_ok=True)

    for index, shard in tqdm(
            enumerate(shards),
            desc="Save the dataset shards",
            total=num_shards,
    ):
        shard_path = f"{data_out_dir}/{split_name}-{index:05d}-of-{num_shards:05d}.parquet"
        shard.to_parquet(shard_path)


if __name__ == '__main__':
    file_path = os.path.abspath(
        os.path.realpath(__file__))

    file_dir = os.path.dirname(file_path)

    dataset_splits = load_dataset(file_path, "all")

    for split in dataset_splits:
        out_dir = f'{file_dir}/data/{split}/'
        os.makedirs(os.path.dirname(out_dir), exist_ok=True)

        to_parquet_with_audio(dataset_splits[split], out_dir, split)