Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
TensorSpeech
GitHub Repository: TensorSpeech/TensorFlowTTS
Path: blob/master/tensorflow_tts/processor/thorsten.py
1558 views
1
# -*- coding: utf-8 -*-
2
# Copyright 2020 TensorFlowTTS Team.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
# http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
"""Perform preprocessing and raw feature extraction for LJSpeech dataset."""
16
17
import os
18
import re
19
20
import numpy as np
21
import soundfile as sf
22
from dataclasses import dataclass
23
from tensorflow_tts.processor import BaseProcessor
24
from tensorflow_tts.utils import cleaners
25
from tensorflow_tts.utils.utils import PROCESSOR_FILE_NAME
26
27
_pad = "pad"
28
_eos = "eos"
29
_punctuation = "!'(),.? "
30
_special = "-"
31
_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
32
33
# Export all symbols:
34
THORSTEN_SYMBOLS = (
35
[_pad] + list(_special) + list(_punctuation) + list(_letters) + [_eos]
36
)
37
38
# Regular expression matching text enclosed in curly braces:
39
_curly_re = re.compile(r"(.*?)\{(.+?)\}(.*)")
40
41
42
@dataclass
43
class ThorstenProcessor(BaseProcessor):
44
"""Thorsten processor."""
45
46
cleaner_names: str = "german_cleaners"
47
positions = {
48
"wave_file": 0,
49
"text_norm": 1,
50
}
51
train_f_name: str = "metadata.csv"
52
53
def create_items(self):
54
if self.data_dir:
55
with open(
56
os.path.join(self.data_dir, self.train_f_name), encoding="utf-8"
57
) as f:
58
self.items = [self.split_line(self.data_dir, line, "|") for line in f]
59
60
def split_line(self, data_dir, line, split):
61
parts = line.strip().split(split)
62
wave_file = parts[self.positions["wave_file"]]
63
text_norm = parts[self.positions["text_norm"]]
64
wav_path = os.path.join(data_dir, "wavs", f"{wave_file}.wav")
65
speaker_name = "thorsten"
66
return text_norm, wav_path, speaker_name
67
68
def setup_eos_token(self):
69
return _eos
70
71
def save_pretrained(self, saved_path):
72
os.makedirs(saved_path, exist_ok=True)
73
self._save_mapper(os.path.join(saved_path, PROCESSOR_FILE_NAME), {})
74
75
def get_one_sample(self, item):
76
text, wav_path, speaker_name = item
77
78
# normalize audio signal to be [-1, 1], soundfile already norm.
79
audio, rate = sf.read(wav_path)
80
audio = audio.astype(np.float32)
81
82
# convert text to ids
83
text_ids = np.asarray(self.text_to_sequence(text), np.int32)
84
85
sample = {
86
"raw_text": text,
87
"text_ids": text_ids,
88
"audio": audio,
89
"utt_id": os.path.split(wav_path)[-1].split(".")[0],
90
"speaker_name": speaker_name,
91
"rate": rate,
92
}
93
94
return sample
95
96
def text_to_sequence(self, text):
97
sequence = []
98
# Check for curly braces and treat their contents as ARPAbet:
99
while len(text):
100
m = _curly_re.match(text)
101
if not m:
102
sequence += self._symbols_to_sequence(
103
self._clean_text(text, [self.cleaner_names])
104
)
105
break
106
sequence += self._symbols_to_sequence(
107
self._clean_text(m.group(1), [self.cleaner_names])
108
)
109
sequence += self._arpabet_to_sequence(m.group(2))
110
text = m.group(3)
111
112
# add eos tokens
113
sequence += [self.eos_id]
114
return sequence
115
116
def _clean_text(self, text, cleaner_names):
117
for name in cleaner_names:
118
cleaner = getattr(cleaners, name)
119
if not cleaner:
120
raise Exception("Unknown cleaner: %s" % name)
121
text = cleaner(text)
122
return text
123
124
def _symbols_to_sequence(self, symbols):
125
return [self.symbol_to_id[s] for s in symbols if self._should_keep_symbol(s)]
126
127
def _arpabet_to_sequence(self, text):
128
return self._symbols_to_sequence(["@" + s for s in text.split()])
129
130
def _should_keep_symbol(self, s):
131
return s in self.symbol_to_id and s != "_" and s != "~"
132
133