Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
TensorSpeech
GitHub Repository: TensorSpeech/TensorFlowTTS
Path: blob/master/examples/cppwin/TensorflowTTSCppInference/TensorflowTTSCppInference.cpp
1559 views
1
2
#include <iostream>
3
#include "Voice.h"
4
#define LOGF(txt) std::cout << txt << "\n"
5
#include "phonemizer.h"
6
#include "ext/ZCharScanner.h"
7
#include <algorithm>
8
#include <cctype>
9
#include <string>
10
#include "ext/cxxopts.hpp"
11
12
std::vector<std::string> GetTxtFile(const std::string& InFn) {
13
14
std::vector<std::string> Ret;
15
std::ifstream InFile(InFn);
16
17
if (!InFile.good())
18
return Ret;
19
20
21
std::string Line;
22
while (std::getline(InFile, Line))
23
{
24
Ret.push_back(Line);
25
26
27
28
}
29
InFile.close();
30
31
return Ret;
32
33
34
35
36
}
37
38
std::vector<std::string> SuperWordSplit(const std::string& InStr, int MaxLen)
39
{
40
ZStringDelimiter Del1(InStr);
41
Del1.AddDelimiter(" ");
42
43
std::vector<std::string> RawWords = Del1.GetTokens();
44
int AmtWords = RawWords.size();
45
46
int Idx = 0;
47
std::string CurrentStr = "";
48
49
std::vector<std::string> SplitStrs;
50
51
while (Idx < AmtWords)
52
{
53
if (CurrentStr.size() > 0)
54
CurrentStr.append(" ");
55
56
std::string CuWord = RawWords[Idx];
57
// phonetic input has to be uppercase
58
if (CuWord.find("@") == std::string::npos)
59
{
60
std::transform(CuWord.begin(), CuWord.end(), CuWord.begin(),
61
[](unsigned char c) { return std::tolower(c); });
62
}
63
64
65
CurrentStr.append(CuWord);
66
67
if (CurrentStr.length() > MaxLen) {
68
SplitStrs.push_back(CurrentStr);
69
CurrentStr = "";
70
71
}
72
73
74
Idx += 1;
75
76
// Add the last string
77
if (Idx == AmtWords)
78
SplitStrs.push_back(CurrentStr);
79
80
81
82
83
84
85
}
86
87
return SplitStrs;
88
89
}
90
91
int main(int argc, char* argv[])
92
{
93
cxxopts::Options options("TFTTSInfer", "Inference with TensorflowTTS models in command line");
94
options.add_options()
95
("v,voice", "Path to the voice folder", cxxopts::value<std::string>()->default_value("LJ")) // a bool parameter
96
("l,language", "Path to the language folder for G2P", cxxopts::value<std::string>()->default_value("g2p/English"))
97
("o,output", "Name of .wav file output of all infers", cxxopts::value<std::string>()->default_value("AllAud.wav"))
98
("m,maxlen", "Optional, max length of split for TTS. Default is 180", cxxopts::value<int>()->default_value("180"))
99
;
100
101
auto Args = options.parse(argc, argv);
102
103
std::string Name = Args["voice"].as<std::string>();
104
std::string Lang = Args["language"].as<std::string>();
105
std::string OutputFileName = Args["output"].as<std::string>();
106
int MaxLen = Args["maxlen"].as<int>();
107
108
if (OutputFileName.find(".wav") == std::string::npos)
109
OutputFileName += ".wav";
110
111
112
113
LOGF("Loading voice...");
114
115
// Load phonemizer
116
Phonemizer StdPh;
117
118
bool G2pInit = StdPh.Initialize(Lang);
119
if (!G2pInit) {
120
LOGF("Could not initialize language and/or G2P model! See if the path is correct and try again!");
121
return -2;
122
123
}
124
125
// Load the voice itself
126
Voice CurrentVox(Name,Name,&StdPh);
127
std::vector<float> AllAud;
128
129
// Begin interactive console
130
bool Running = true;
131
while (Running)
132
{
133
std::string Prompt = "";
134
135
LOGF("Type a prompt, or type EXIT to exit ");
136
137
std::getline(std::cin, Prompt);
138
if (Prompt == "EXIT") {
139
Running = false;
140
break;
141
}
142
std::vector<float> Audata;
143
144
// Split the prompt into chunks (if the user inputs like that)
145
for (const auto& Spli : SuperWordSplit(Prompt, MaxLen)) {
146
std::vector<float> ImmediateAudata = CurrentVox.Vocalize(Prompt + CurrentVox.GetInfo().EndPadding);
147
// Insert the audio data to the end of the mid-level audata vector
148
Audata.insert(Audata.end(), ImmediateAudata.begin(), ImmediateAudata.end());
149
150
151
}
152
153
154
155
156
std::string Filename = Prompt.substr(0, std::min(16, (int)Prompt.size())) + ".wav";
157
158
VoxUtil::ExportWAV(Filename, Audata, CurrentVox.GetInfo().SampleRate);
159
160
// Insert the audio into the AllAud vector
161
AllAud.insert(AllAud.end(), Audata.begin(), Audata.end());
162
163
LOGF("Saved to " + Filename);
164
165
166
167
168
}
169
170
171
// Export all the audio
172
VoxUtil::ExportWAV(OutputFileName, AllAud, CurrentVox.GetInfo().SampleRate);
173
LOGF("Saved ALL to " + OutputFileName);
174
175
std::cout << "Hello TensorflowTTS!\n";
176
return 0;
177
178
}
179
180