File size: 9,950 Bytes
c596804
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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

_CITATION = """\
@ARTICLE{Koizumi2023-hs,
  title         = "{LibriTTS-R}: A restored multi-speaker text-to-speech corpus",
  author        = "Koizumi, Yuma and Zen, Heiga and Karita, Shigeki and Ding,
                   Yifan and Yatabe, Kohei and Morioka, Nobuyuki and Bacchiani,
                   Michiel and Zhang, Yu and Han, Wei and Bapna, Ankur",
  abstract      = "This paper introduces a new speech dataset called
                   ``LibriTTS-R'' designed for text-to-speech (TTS) use. It is
                   derived by applying speech restoration to the LibriTTS
                   corpus, which consists of 585 hours of speech data at 24 kHz
                   sampling rate from 2,456 speakers and the corresponding
                   texts. The constituent samples of LibriTTS-R are identical
                   to those of LibriTTS, with only the sound quality improved.
                   Experimental results show that the LibriTTS-R ground-truth
                   samples showed significantly improved sound quality compared
                   to those in LibriTTS. In addition, neural end-to-end TTS
                   trained with LibriTTS-R achieved speech naturalness on par
                   with that of the ground-truth samples. The corpus is freely
                   available for download from
                   \textbackslashurl\{http://www.openslr.org/141/\}.",
  month         =  may,
  year          =  2023,
  copyright     = "http://creativecommons.org/licenses/by-nc-nd/4.0/",
  archivePrefix = "arXiv",
  primaryClass  = "eess.AS",
  eprint        = "2305.18802"
}
"""

_DESCRIPTION = """\
LibriTTS-R [1] is a sound quality improved version of the LibriTTS corpus (http://www.openslr.org/60/) which is a 
multi-speaker English corpus of approximately 585 hours of read English speech at 24kHz sampling rate, 
published in 2019. The constituent samples of LibriTTS-R are identical to those of LibriTTS, with only the sound 
quality improved. To improve sound quality, a speech restoration model, Miipher proposed by Yuma Koizumi [2], was used.
"""

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

_LICENSE = "CC BY 4.0"

_DL_URL = "https://us.openslr.org/resources/141/"

_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_R_Dataset(datasets.GeneratorBasedBuilder):
    """
    LibriTTS-R [1] is a sound quality improved version of the LibriTTS corpus (http://www.openslr.org/60/) which is a
    multi-speaker English corpus of approximately 585 hours of read English speech at 24kHz sampling rate,
    published in 2019.
    """

    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-R 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_R',
                            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 has extra audio
                # print(f"[libritts_r {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_r {split_name}] Transcript without audio: {id_}")
                continue

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