add initial version of loader script
Browse files- nst_swedish_tts.py +154 -0
nst_swedish_tts.py
ADDED
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2021 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
|
3 |
+
# Copyright 2023 Jim O'Regan for Språkbanken Tal
|
4 |
+
#
|
5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6 |
+
# you may not use this file except in compliance with the License.
|
7 |
+
# You may obtain a copy of the License at
|
8 |
+
#
|
9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
#
|
11 |
+
# Unless required by applicable law or agreed to in writing, software
|
12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 |
+
# See the License for the specific language governing permissions and
|
15 |
+
# limitations under the License.
|
16 |
+
|
17 |
+
# Lint as: python3
|
18 |
+
"""Datasets loader for NST Swedish TTS data"""
|
19 |
+
|
20 |
+
import soundfile as sf
|
21 |
+
import os
|
22 |
+
from pathlib import Path
|
23 |
+
import datasets
|
24 |
+
from datasets.tasks import AutomaticSpeechRecognition
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
_HEADER = b'PCM44 \x00\x00\x00\x00\x00\x00\x00S'
|
29 |
+
|
30 |
+
_AUDIO_URL = "https://www.nb.no/sbfil/talesyntese/sve.ibm.talesyntese.tar.gz"
|
31 |
+
|
32 |
+
_DESCRIPTION = """
|
33 |
+
Database for Swedish speech synthesis, originally produced by Nordic Language Technology AS (NST).
|
34 |
+
"""
|
35 |
+
|
36 |
+
_URL = "https://www.nb.no/sprakbanken/en/resource-catalogue/oai-nb-no-sbr-18/"
|
37 |
+
|
38 |
+
|
39 |
+
def is_pcm(filename) -> bool:
|
40 |
+
"""
|
41 |
+
Check the header of a .pcm file
|
42 |
+
|
43 |
+
Args:
|
44 |
+
filename: the file to check
|
45 |
+
|
46 |
+
Returns:
|
47 |
+
True is header is present, False otherwise
|
48 |
+
"""
|
49 |
+
with open(filename, "rb") as pcm:
|
50 |
+
pcm.seek(0)
|
51 |
+
cond = (pcm.read(16) == _HEADER)
|
52 |
+
# reset location, just in case
|
53 |
+
pcm.seek(0)
|
54 |
+
return cond
|
55 |
+
|
56 |
+
|
57 |
+
IGNORE_SENT = [
|
58 |
+
"stod man på torget kunde man se huset och det var ingen tvekan om att det dominerade sin omgivning och det rådde knappast heller något tvivel om att det förr i tiden hade väckt en hel del avund känslor som någon enstaka gång fortfarande kunde framkallas hos de äldre",
|
59 |
+
"viktor hade skickat ut det innan novell sålde unixware till sco",
|
60 |
+
"det gläder oss självklart"
|
61 |
+
]
|
62 |
+
IGNORE_ID = [
|
63 |
+
"4913",
|
64 |
+
]
|
65 |
+
# MAYBE_FIX = {
|
66 |
+
# "4913": "en annan gång tar vi ett annat grepp"
|
67 |
+
# }
|
68 |
+
|
69 |
+
def read_with_soundfile(filename):
|
70 |
+
return sf.read(filename, channels=2, samplerate=44100, endian="BIG",
|
71 |
+
dtype="int16", format="RAW", subtype="PCM_16", start=16)
|
72 |
+
|
73 |
+
|
74 |
+
class NSTDataset(datasets.GeneratorBasedBuilder):
|
75 |
+
|
76 |
+
VERSION = datasets.Version("1.1.0")
|
77 |
+
|
78 |
+
BUILDER_CONFIGS = [
|
79 |
+
datasets.BuilderConfig(name="speech", version=VERSION, description="Data for speech recognition"),
|
80 |
+
]
|
81 |
+
|
82 |
+
def _info(self):
|
83 |
+
features = datasets.Features(
|
84 |
+
{
|
85 |
+
"audio": datasets.Audio(sampling_rate=44_100),
|
86 |
+
"pitch_tracker": datasets.Audio(sampling_rate=44_100),
|
87 |
+
"text": datasets.Value("string"),
|
88 |
+
}
|
89 |
+
)
|
90 |
+
|
91 |
+
return datasets.DatasetInfo(
|
92 |
+
description=_DESCRIPTION,
|
93 |
+
features=features,
|
94 |
+
supervised_keys=None,
|
95 |
+
homepage=_URL,
|
96 |
+
task_templates=[
|
97 |
+
AutomaticSpeechRecognition(audio_column="audio", transcription_column="text")
|
98 |
+
],
|
99 |
+
)
|
100 |
+
|
101 |
+
def _split_generators(self, dl_manager):
|
102 |
+
if hasattr(dl_manager, 'manual_dir') and dl_manager.manual_dir is not None:
|
103 |
+
data_dir = os.path.abspath(os.path.expanduser(dl_manager.manual_dir))
|
104 |
+
AUDIO_FILE = os.path.join(data_dir, _AUDIO_URL.split("/")[-1])
|
105 |
+
audio_dir = dl_manager.extract(AUDIO_FILE)
|
106 |
+
else:
|
107 |
+
audio_dir = dl_manager.download_and_extract(_AUDIO_URL)
|
108 |
+
return [
|
109 |
+
datasets.SplitGenerator(
|
110 |
+
name=datasets.Split.TRAIN,
|
111 |
+
gen_kwargs={
|
112 |
+
"split": "train",
|
113 |
+
"audio_dir": audio_dir,
|
114 |
+
},
|
115 |
+
),
|
116 |
+
]
|
117 |
+
|
118 |
+
def _generate_examples(
|
119 |
+
self, split, audio_dir
|
120 |
+
):
|
121 |
+
filepath = Path(audio_dir) / "sw_pcms" / "mf"
|
122 |
+
textpath = Path(audio_dir) / "sw_pcms" / "scripts" / "mf" / "sw_all"
|
123 |
+
transcripts = {}
|
124 |
+
counter = 1
|
125 |
+
with open(str(textpath), encoding="latin1") as text:
|
126 |
+
for line in text.readlines():
|
127 |
+
line = line.strip()
|
128 |
+
if line in IGNORE_SENT:
|
129 |
+
continue
|
130 |
+
else:
|
131 |
+
id = f"sw_all_mf_01_{counter:04d}"
|
132 |
+
if str(id) not in IGNORE_ID:
|
133 |
+
transcripts[id] = line
|
134 |
+
counter += 1
|
135 |
+
for file in filepath.glob("*.pcm"):
|
136 |
+
stem = file.stem
|
137 |
+
if is_pcm(str(file)):
|
138 |
+
data, _ = read_with_soundfile(str(file))
|
139 |
+
yield stem, {
|
140 |
+
"audio": {
|
141 |
+
"array": data[:, 1],
|
142 |
+
"sampling_rate": 44_100,
|
143 |
+
"path": str(file),
|
144 |
+
"id": stem,
|
145 |
+
},
|
146 |
+
"pitch_tracker": {
|
147 |
+
"array": data[:, 0],
|
148 |
+
"sampling_rate": 44_100,
|
149 |
+
"path": str(file),
|
150 |
+
"id": stem,
|
151 |
+
},
|
152 |
+
"text": transcripts[stem],
|
153 |
+
}
|
154 |
+
|