-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.cpp
272 lines (237 loc) · 6.77 KB
/
main.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "private_include/util.hpp"
#include <fea/getopt/getopt.hpp>
#include <fea/terminal/pipe.hpp>
#include <fea/terminal/utf8.hpp>
#include <filesystem>
#include <format>
#include <iostream>
#include <string>
#include <wsay/engine.hpp>
#include <wsay/voice.hpp>
const std::wstring exit_cmd = L"!exit";
const std::wstring shutup_cmd = L"!stop";
int wmain(int argc, wchar_t** argv, wchar_t**) {
fea::fast_iostreams();
auto on_exit_reset_term = fea::utf8_terminal(true);
wsy::engine engine;
wsy::voice voice;
bool parsetext = true;
bool interactive_mode = false;
std::wstring speech_text = fea::wread_pipe_text();
fea::get_opt<wchar_t> opt;
opt.add_raw_option(
L"sentence",
[&](std::wstring&& str) {
speech_text = std::move(str);
return true;
},
L"Sentence to say. You can use speech xml.");
opt.add_flag_option(
L"list_voices",
[&]() {
const std::vector<std::wstring>& names = engine.voices();
for (size_t i = 0; i < names.size(); ++i) {
std::wcout << std::format(L"{} : {}\n", i + 1, names[i]);
}
std::wcout << L"\n";
return true;
},
L"Lists available voices.", L'l');
opt.add_required_arg_option(
L"voice",
[&](std::wstring&& str) {
voice.voice_idx = std::stoull(str);
if (voice.voice_idx == 0
|| voice.voice_idx > engine.voices().size()) {
std::wcerr << L"Beep boop, wrong voice selected.\n\n";
return false;
}
--voice.voice_idx;
return true;
},
L"Choose a different voice. Use the voice number printed "
"using --list_voices.",
L'v');
opt.add_required_arg_option(
L"input_text",
[&](std::wstring&& f) {
std::filesystem::path filepath = { std::move(f) };
return parse_text_file(filepath, speech_text);
},
L"Play text from '.txt' file. Supports speech xml.", L'i');
opt.add_optional_arg_option(
L"output_file",
[&](std::wstring f) {
std::filesystem::path filepath
= std::filesystem::current_path() / L"out.wav";
if (!f.empty()) {
filepath = { std::move(f) };
}
voice.add_output_file(filepath);
return true;
},
L"Outputs to wav file. Uses 'out.wav' if no filename is "
"provided.",
L'o');
opt.add_flag_option(
L"interactive",
[&]() {
interactive_mode = true;
return true;
},
L"Enter interactive mode. Type sentences, they will be "
"spoken when you press enter.\nUse 'ctrl+c' or "
"type '!exit' to quit.",
L'I');
opt.add_flag_option(
L"list_devices",
[&]() {
const std::vector<std::wstring>& dnames = engine.devices();
for (size_t i = 0; i < dnames.size(); ++i) {
std::wcout << std::format(L"{} : {}\n", i + 1, dnames[i]);
}
std::wcout << L"\tUse 'all' to select every device.\n\n";
return true;
},
L"List detected playback devices.", L'd');
opt.add_multi_arg_option(
L"playback_device",
[&](std::vector<std::wstring>&& arr) {
if (arr.size() == 1 && arr.front() == L"all") {
for (size_t i = 0; i < engine.devices().size(); ++i) {
voice.add_output_device(i);
}
return true;
}
for (size_t i = 0; i < arr.size(); ++i) {
unsigned chosen_device = std::stoul(arr[i]);
if (chosen_device == 0
|| chosen_device > engine.devices().size()) {
std::wcerr << L"Beep boop, wrong device "
"selected.\n\n";
return false;
}
voice.add_output_device(chosen_device - 1);
}
return true;
},
L"Specify a playback device. Use the number "
"provided by --list_devices.\nYou can provide more than 1 "
"playback device, seperate the numbers with spaces. You "
"can also mix output to file + playback.\nUse 'all' to select all "
"devices.",
L'p');
opt.add_required_arg_option(
L"volume",
[&](std::wstring&& f) {
voice.volume = uint8_t(std::stoul(f));
if (voice.volume > 100u) {
std::wcerr << "--volume only supports values from 0 to "
"100\n\n";
return false;
}
return true;
},
L"Sets the voice volume, from 0 to 100.", L'V');
opt.add_required_arg_option(
L"speed",
[&](std::wstring&& f) {
voice.speed = uint8_t(std::stoul(f));
if (voice.speed > 100u) {
std::wcerr << "--speed only supports values from 0 to "
"100\n\n";
return false;
}
return true;
},
L"Sets the voice speed, from 0 to 100. 50 is the default speed.",
L's');
opt.add_required_arg_option(
L"pitch",
[&](std::wstring&& f) {
voice.pitch = uint8_t(std::stoul(f));
if (voice.pitch > 20u) {
std::wcerr << "--pitch only supports values from 0 to "
"20\n\n";
return false;
}
return true;
},
L"Sets the voice pitch, from 0 to 20. 10 is the default "
L"pitch.",
L'P');
opt.add_flag_option(
L"clipboard",
[&]() {
speech_text = get_clipboard_text();
if (speech_text.empty()) {
return false;
}
std::wcout << std::format(
L"Playing clipboard text : '{}'\n", speech_text);
return true;
},
L"Speak currently copied text (the text in your clipboard).", L'c');
opt.add_flag_option(
L"nospeechxml",
[&]() {
voice.xml_parse = false;
parsetext = false;
return true;
},
L"Disable speech xml detection. Use this if the text contains "
L"special characters that aren't speech xml.\n",
L'X');
opt.add_required_arg_option(
L"fxradio",
[&](std::wstring&& f) {
size_t fx = std::stoul(f) - 1;
if (fx >= size_t(wsy::effect_e::count)) {
std::wcerr << std::format(L"--fxradio only supports values "
L"between 1 and {}.\n",
wsy::num_radio_fx());
return false;
}
voice.effect(wsy::effect_e(fx));
return true;
},
std::format(L"Degrades audio to make it sound like a "
L"radio.\nSupported values : 1 to {}.\n",
wsy::num_radio_fx()));
std::wstring help_outro = L"wsay\nversion ";
help_outro += WSAY_VERSION;
help_outro += L"\nhttps://github.com/p-groarke/wsay/releases\n";
help_outro += L"Philippe Groarke <hello@philippegroarke.com>";
opt.add_help_outro(help_outro);
if (!speech_text.empty()) {
// We have some text coming from pipe, or elsewhere.
opt.no_options_is_ok();
}
if (!opt.parse_options(argc, argv)) {
return -1;
}
if (interactive_mode) {
wsy::async_token tok = engine.make_async_token(voice);
std::wcout << L"[Info] Type sentences, press enter to speak them.\n";
std::wcout << std::format(
L"[Command] '{}' : Exit interactive mode.\n", exit_cmd);
std::wcout << std::format(
L"[Command] '{}' : Interrupt speaking.\n", shutup_cmd);
std::wcout << L"\n";
std::wstring wsentence;
while (std::getline(std::wcin, wsentence)) {
if (wsentence == exit_cmd) {
break;
}
if (wsentence == shutup_cmd) {
engine.stop(tok);
continue;
}
engine.speak_async(wsentence, tok);
}
return 0;
} else {
engine.speak(voice, speech_text);
}
return 0;
}