forked from rhasspy/piper-phonemize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phonemize.cpp
190 lines (158 loc) · 5.94 KB
/
phonemize.cpp
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
#include <malloc.h>
#include <map>
#include <string>
#include <vector>
#include <espeak-ng/speak_lib.h>
#include <onnxruntime_cxx_api.h>
#include "phonemize.hpp"
#include "uni_algo.h"
#ifdef __GLIBC__
#define OFFSET_CLAUSE_TERMINATOR (8 + sizeof(int))
#else
#define OFFSET_CLAUSE_TERMINATOR sizeof(int)
#endif
typedef struct {
int clause_terminator() {
// get memory address of espeak::Translator->clause_terminator field
return *(int*)((unsigned long)(this + malloc_usable_size(this) - OFFSET_CLAUSE_TERMINATOR) & -8);
}
} Translator;
extern Translator *translator;
namespace piper {
// language -> phoneme -> [phoneme, ...]
std::map<std::string, PhonemeMap> DEFAULT_PHONEME_MAP = {
{"pt-br", {{U'c', {U'k'}}}}};
void phonemize_eSpeak(std::string text, eSpeakPhonemeConfig &config,
std::vector<std::vector<Phoneme>> &phonemes) {
auto voice = config.voice;
int result = espeak_SetVoiceByName(voice.c_str());
if (result != 0) {
throw std::runtime_error("Failed to set eSpeak-ng voice");
}
std::shared_ptr<PhonemeMap> phonemeMap;
if (config.phonemeMap) {
phonemeMap = config.phonemeMap;
} else if (DEFAULT_PHONEME_MAP.count(voice) > 0) {
phonemeMap = std::make_shared<PhonemeMap>(DEFAULT_PHONEME_MAP[voice]);
}
// Modified by eSpeak
std::string textCopy(text);
std::vector<Phoneme> *sentencePhonemes = nullptr;
const char *inputTextPointer = textCopy.c_str();
int terminator = 0;
while (inputTextPointer != NULL) {
// Modified espeak-ng API to get access to clause terminator
std::string clausePhonemes(espeak_TextToPhonemes(
(const void **)&inputTextPointer,
/*textmode*/ espeakCHARS_AUTO,
/*phonememode = IPA*/ 0x02));
terminator = translator->clause_terminator();
// Decompose, e.g. "ç" -> "c" + "̧"
auto phonemesNorm = una::norm::to_nfd_utf8(clausePhonemes);
auto phonemesRange = una::ranges::utf8_view{phonemesNorm};
if (!sentencePhonemes) {
// Start new sentence
phonemes.emplace_back();
sentencePhonemes = &phonemes[phonemes.size() - 1];
}
// Maybe use phoneme map
std::vector<Phoneme> mappedSentPhonemes;
if (phonemeMap) {
for (auto phoneme : phonemesRange) {
if (phonemeMap->count(phoneme) < 1) {
// No mapping for phoneme
mappedSentPhonemes.push_back(phoneme);
} else {
// Mapping for phoneme
auto mappedPhonemes = &(phonemeMap->at(phoneme));
mappedSentPhonemes.insert(mappedSentPhonemes.end(),
mappedPhonemes->begin(),
mappedPhonemes->end());
}
}
} else {
// No phoneme map
mappedSentPhonemes.insert(mappedSentPhonemes.end(), phonemesRange.begin(),
phonemesRange.end());
}
auto phonemeIter = mappedSentPhonemes.begin();
auto phonemeEnd = mappedSentPhonemes.end();
if (config.keepLanguageFlags) {
// No phoneme filter
sentencePhonemes->insert(sentencePhonemes->end(), phonemeIter,
phonemeEnd);
} else {
// Filter out (lang) switch (flags).
// These surround words from languages other than the current voice.
bool inLanguageFlag = false;
while (phonemeIter != phonemeEnd) {
if (inLanguageFlag) {
if (*phonemeIter == U')') {
// End of (lang) switch
inLanguageFlag = false;
}
} else if (*phonemeIter == U'(') {
// Start of (lang) switch
inLanguageFlag = true;
} else {
sentencePhonemes->push_back(*phonemeIter);
}
phonemeIter++;
}
}
// Add appropriate punctuation depending on terminator type
int punctuation = terminator & 0x000FFFFF;
if (punctuation == CLAUSE_PERIOD) {
sentencePhonemes->push_back(config.period);
} else if (punctuation == CLAUSE_QUESTION) {
sentencePhonemes->push_back(config.question);
} else if (punctuation == CLAUSE_EXCLAMATION) {
sentencePhonemes->push_back(config.exclamation);
} else if (punctuation == CLAUSE_COMMA) {
sentencePhonemes->push_back(config.comma);
} else if (punctuation == CLAUSE_COLON) {
sentencePhonemes->push_back(config.colon);
} else if (punctuation == CLAUSE_SEMICOLON) {
sentencePhonemes->push_back(config.semicolon);
}
if ((terminator & CLAUSE_TYPE_SENTENCE) == CLAUSE_TYPE_SENTENCE) {
// End of sentence
sentencePhonemes = nullptr;
}
} // while inputTextPointer != NULL
} /* phonemize_eSpeak */
// ----------------------------------------------------------------------------
void phonemize_codepoints(std::string text, CodepointsPhonemeConfig &config,
std::vector<std::vector<Phoneme>> &phonemes) {
if (config.casing == CASING_LOWER) {
text = una::cases::to_lowercase_utf8(text);
} else if (config.casing == CASING_UPPER) {
text = una::cases::to_uppercase_utf8(text);
} else if (config.casing == CASING_FOLD) {
text = una::cases::to_casefold_utf8(text);
}
// Decompose, e.g. "ç" -> "c" + "̧"
auto phonemesNorm = una::norm::to_nfd_utf8(text);
auto phonemesRange = una::ranges::utf8_view{phonemesNorm};
// No sentence boundary detection
phonemes.emplace_back();
auto sentPhonemes = &phonemes[phonemes.size() - 1];
if (config.phonemeMap) {
for (auto phoneme : phonemesRange) {
if (config.phonemeMap->count(phoneme) < 1) {
// No mapping for phoneme
sentPhonemes->push_back(phoneme);
} else {
// Mapping for phoneme
auto mappedPhonemes = &(config.phonemeMap->at(phoneme));
sentPhonemes->insert(sentPhonemes->end(), mappedPhonemes->begin(),
mappedPhonemes->end());
}
}
} else {
// No phoneme map
sentPhonemes->insert(sentPhonemes->end(), phonemesRange.begin(),
phonemesRange.end());
}
} // phonemize_text
} // namespace piper