File size: 3,915 Bytes
d5ee97c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

#include <iostream>
#include "Voice.h"
#define LOGF(txt) std::cout << txt <<  "\n"
#include "phonemizer.h"
#include "ext/ZCharScanner.h"
#include <algorithm>
#include <cctype>
#include <string>
#include "ext/cxxopts.hpp"

std::vector<std::string> GetTxtFile(const std::string& InFn) {
 
	std::vector<std::string> Ret;
    std::ifstream InFile(InFn);

	if (!InFile.good())
		return Ret;


    std::string Line;
    while (std::getline(InFile, Line)) 
    {
		Ret.push_back(Line);



    }
	InFile.close();

	return Ret;
 


    
}

std::vector<std::string> SuperWordSplit(const std::string& InStr, int MaxLen)
{
	ZStringDelimiter Del1(InStr);
	Del1.AddDelimiter(" ");
	
	std::vector<std::string> RawWords = Del1.GetTokens();
	int AmtWords = RawWords.size();

	int Idx = 0;
	std::string CurrentStr = "";

	std::vector<std::string> SplitStrs;

	while (Idx < AmtWords)
	{
		if (CurrentStr.size() > 0)
			CurrentStr.append(" ");

		std::string CuWord = RawWords[Idx];
		// phonetic input has to be uppercase
		if (CuWord.find("@") == std::string::npos) 
		{
			std::transform(CuWord.begin(), CuWord.end(), CuWord.begin(),
				[](unsigned char c) { return std::tolower(c); });
		} 


		CurrentStr.append(CuWord);

		if (CurrentStr.length() > MaxLen) {
			SplitStrs.push_back(CurrentStr);
			CurrentStr = "";

		}


		Idx += 1;

		// Add the last string
		if (Idx == AmtWords)
			SplitStrs.push_back(CurrentStr);






	}

	return SplitStrs;

}

int main(int argc, char* argv[])
{
	cxxopts::Options options("TFTTSInfer", "Inference with TensorflowTTS models in command line");
	options.add_options()
		("v,voice", "Path to the voice folder", cxxopts::value<std::string>()->default_value("LJ")) // a bool parameter
		("l,language", "Path to the language folder for G2P", cxxopts::value<std::string>()->default_value("g2p/English"))
		("o,output", "Name of .wav file output of all infers", cxxopts::value<std::string>()->default_value("AllAud.wav"))
		("m,maxlen", "Optional, max length of split for TTS. Default is 180", cxxopts::value<int>()->default_value("180"))
		;

	auto Args = options.parse(argc, argv);

	std::string Name = Args["voice"].as<std::string>();
	std::string Lang = Args["language"].as<std::string>();
	std::string OutputFileName = Args["output"].as<std::string>();
	int MaxLen = Args["maxlen"].as<int>();

	if (OutputFileName.find(".wav") == std::string::npos)
		OutputFileName += ".wav";



	LOGF("Loading voice...");

	// Load phonemizer
	Phonemizer StdPh;

	bool G2pInit = StdPh.Initialize(Lang);
	if (!G2pInit) {
		LOGF("Could not initialize language and/or G2P model! See if the path is correct and try again!");
		return -2;
	
	}

	// Load the voice itself
	Voice CurrentVox(Name,Name,&StdPh);
	std::vector<float> AllAud;

	// Begin interactive console
	bool Running = true;
	while (Running)
	{
		std::string Prompt = "";

		LOGF("Type a prompt, or type EXIT to exit ");

		std::getline(std::cin, Prompt);
		if (Prompt == "EXIT") {
			Running = false;
			break;
		}
		std::vector<float> Audata;

		// Split the prompt into chunks (if the user inputs like that)
		for (const auto& Spli : SuperWordSplit(Prompt, MaxLen)) {
			std::vector<float> ImmediateAudata = CurrentVox.Vocalize(Prompt + CurrentVox.GetInfo().EndPadding);
			// Insert the audio data to the end of the mid-level audata vector
			Audata.insert(Audata.end(), ImmediateAudata.begin(), ImmediateAudata.end());

		
		}




		std::string Filename = Prompt.substr(0, std::min(16, (int)Prompt.size())) + ".wav";

		VoxUtil::ExportWAV(Filename, Audata, CurrentVox.GetInfo().SampleRate);

		// Insert the audio into the AllAud vector
		AllAud.insert(AllAud.end(), Audata.begin(), Audata.end());
		
		LOGF("Saved to " + Filename);




	}


	// Export all the audio
	VoxUtil::ExportWAV(OutputFileName, AllAud, CurrentVox.GetInfo().SampleRate);
	LOGF("Saved ALL to " + OutputFileName);

	std::cout << "Hello TensorflowTTS!\n";
	return 0;

}