-
Notifications
You must be signed in to change notification settings - Fork 20
/
EnglishPhoneticProcessor.cpp
166 lines (96 loc) · 3.23 KB
/
EnglishPhoneticProcessor.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
#include "EnglishPhoneticProcessor.h"
#include "VoxCommon.hpp"
using namespace std;
bool EnglishPhoneticProcessor::Initialize(Phonemizer* InPhn, ESpeakPhonemizer *InENGPh)
{
Phoner = InPhn;
Tokenizer.SetAllowedChars(Phoner->GetGraphemeChars());
ENG_Phonemizer = InENGPh;
return true;
}
std::string EnglishPhoneticProcessor::ProcessTextPhonetic(const std::string& InText, const std::vector<u32string> &InPhonemes, const std::vector<DictEntry>& InDict, ETTSLanguageType::Enum InLanguageType, bool IsTac)
{
if (!Phoner)
return "ERROR";
vector<string> Words = Tokenizer.Tokenize(InText,IsTac);
string Assemble = "";
if (InLanguageType == ETTSLanguageType::Char)
{
for (size_t w = 0; w < Words.size();w++)
{
Assemble.append(Words[w]);
if (w > 0)
Assemble.append(" ");
}
if (Assemble[Assemble.size() - 1] == ' ')
Assemble.pop_back();
return Assemble;
}
// Make a copy of the dict passed.
std::vector<DictEntry> CurrentDict = InDict;
for (size_t w = 0; w < Words.size();w++)
{
const string& Word = Words[w];
if (Word.size() > 22)
continue;
// Double email symbol indicates Tacotron punctuation handling
if (Word.find("@@") != std::string::npos)
{
std::string AddPonct = Word.substr(2); // Remove the @@
Assemble.append(" ");
Assemble.append(AddPonct);
Assemble.append(" ");
continue;
}
if (Word.find("@") != std::string::npos){
std::u32string AddPh = VoxUtil::StrToU32(Word.substr(1)); // Remove the @
size_t OutId = 0;
if (VoxUtil::FindInVec(AddPh,InPhonemes,OutId))
{
Assemble.append(VoxUtil::U32ToStr(InPhonemes[OutId]));
Assemble.append(" ");
}
continue;
}
size_t OverrideIdx = 0;
if (!ENG_Phonemizer && VoxUtil::FindInVec2<std::string,DictEntry>(Word,InDict,OverrideIdx))
{
Assemble.append(InDict[OverrideIdx].PhSpelling);
Assemble.append(" ");
continue;
}
std::string Res = Word;
if (!ENG_Phonemizer){
Res = Phoner->ProcessWord(Word,0.001f);
CurrentDict.push_back({Word,Res,""});
}
// Cache the word in the override dict so next time we don't have to research it
Assemble.append(Res);
Assemble.append(" ");
}
// eSpeak phonemizer takes in whole thing
if (ENG_Phonemizer){
Assemble = ENG_Phonemizer->Phonemize(Assemble);
}
// Delete last space if there is
if (Assemble[Assemble.size() - 1] == ' ')
Assemble.pop_back();
return Assemble;
}
EnglishPhoneticProcessor::EnglishPhoneticProcessor()
{
Phoner = nullptr;
ENG_Phonemizer = nullptr;
}
EnglishPhoneticProcessor::EnglishPhoneticProcessor(Phonemizer *InPhn, ESpeakPhonemizer *InENGPh)
{
Initialize(InPhn,InENGPh);
}
EnglishPhoneticProcessor::~EnglishPhoneticProcessor()
{
// Causes annoying crash on exit. It's also irrelevant because the OS frees what little memory this had.
/*
if (Phoner)
delete Phoner;
*/
}