-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsapi_tts.cc
311 lines (270 loc) · 9.96 KB
/
sapi_tts.cc
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <node.h>
#include <math.h>
#include <Windows.h>
#include <sapi.h>
#pragma warning(disable:4996)
#include <sphelper.h>
#pragma warning(default: 4996)
#include <iostream>
#include <string>
namespace sapi_tts {
using v8::Context;
using v8::Local;
using v8::Persistent;
using v8::Local;
using v8::Isolate;
using v8::FunctionCallbackInfo;
using v8::Object;
using v8::HandleScope;
using v8::NewStringType;
using v8::String;
using v8::Boolean;
using v8::Array;
using v8::Number;
using v8::Value;
using v8::Null;
using v8::Function;
using node::AtExit;
static bool already_setup = false;
static double downloadPercent = 0;
static char * last_voice = 0;
static bool dll_ready = false;
static bool initialized = false;
std::wstring str_to_ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
ISpVoice * pVoice = NULL;
bool setup() {
printf("SAPI Setting Up...\n");
if (already_setup) {
printf("SAPI Already Set Up\n");
return true;
}
bool success = SUCCEEDED(::CoInitialize(NULL));
if(success) {
already_setup = true;
}
return success;
}
void jsStatus(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
bool ready = setup();
Local<Object> obj = Object::New(isolate);
obj->Set(context, String::NewFromUtf8(isolate, "ready", NewStringType::kNormal).ToLocalChecked(), Boolean::New(isolate, ready));
args.GetReturnValue().Set(obj);
}
bool closeVoice() {
printf("SAPI_Close %s\n", last_voice);
if (pVoice) {
pVoice->Release();
pVoice = NULL;
}
return true;
}
bool teardown() {
if (!already_setup) { return true; }
printf("SAPI Tearing down \n");
closeVoice();
bool success = true;
::CoUninitialize();
last_voice = 0;
already_setup = false;
return success;
}
Local<Array> listVoices(Isolate* isolate) {
teardown();
setup();
Local<Context> context = isolate->GetCurrentContext();
IEnumSpObjectTokens* cpEnum;
printf("SAPI Retrieving Voices...\n");
HRESULT hr = SpEnumTokens(SPCAT_VOICES, NULL, NULL, &cpEnum);
ULONG i = 0, ulCount = 0;
hr = cpEnum->GetCount(&ulCount);
Local<Array> result = Array::New(isolate, ulCount);
ISpObjectToken* tok;
CSpDynamicString szDesc;// = L"12345678901234567890123456789012345678901234567890";
LANGID lang(0);
char iso[5] = { 0 };
char ctry[5] = { 0 };
while (SUCCEEDED(hr) && i<ulCount)
{
hr = cpEnum->Next(1, &tok, NULL);
SpGetLanguageFromToken(tok, &lang);
GetLocaleInfo(lang, LOCALE_SISO639LANGNAME, iso, sizeof(iso) / sizeof(WCHAR));
GetLocaleInfo(lang, LOCALE_SISO3166CTRYNAME, ctry, sizeof(ctry) / sizeof(WCHAR));
CSpDynamicString dstrDefaultName;
SpGetDescription(tok, &dstrDefaultName);
SpGetDescription(tok, &szDesc);
LPWSTR lang_id;
tok->GetId(&lang_id);
std::string lang_id_str = CW2A(lang_id);
const char* id = lang_id_str.c_str();
CoTaskMemFree(lang_id);
char locale[20] = "en-US";
snprintf(locale, 20, "%s-%s", iso, ctry);
// const char* desc = CW2A(szDesc);
std::string desc_str = CW2A(szDesc);
const char* desc = desc_str.c_str();
Local<Object> obj = Object::New(isolate);
obj->Set(context, String::NewFromUtf8(isolate, "voice_id", NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, id, NewStringType::kNormal).ToLocalChecked());
obj->Set(context, String::NewFromUtf8(isolate, "name", NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, desc, NewStringType::kNormal).ToLocalChecked());
obj->Set(context, String::NewFromUtf8(isolate, "locale", NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, locale, NewStringType::kNormal).ToLocalChecked());
obj->Set(context, String::NewFromUtf8(isolate, "language", NewStringType::kNormal).ToLocalChecked(), String::NewFromUtf8(isolate, locale, NewStringType::kNormal).ToLocalChecked());
obj->Set(context, String::NewFromUtf8(isolate, "active", NewStringType::kNormal).ToLocalChecked(), Boolean::New(isolate, true));
result->Set(context, i, obj); // String::Utf8Value("asdf"));
printf("SAPI Voice: %s Speaker: %s Language: %s Version: %s\n", id, desc, locale, locale);
i++;
}
printf("SAPI Number of voice: %d\n", i);
cpEnum->Release();
return result;
}
bool openVoice(const char * voice_string) {
if (!already_setup) { setup(); }
if (last_voice != 0 && strcmp(last_voice, voice_string) == 0) {
return true;
} else if(last_voice != 0) {
printf("SAPI Closing last voice before opening new one %s %s\n", last_voice, voice_string);
closeVoice();
}
HRESULT hresult = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if(FAILED(hresult)) { return false; }
IEnumSpObjectTokens* cpEnum;
HRESULT hr = SpEnumTokens(SPCAT_VOICES, NULL, NULL, &cpEnum);
ULONG i = 0, ulCount = 0;
hr = cpEnum->GetCount(&ulCount);
ISpObjectToken* tok;
while (SUCCEEDED(hr) && i<ulCount)
{
hr = cpEnum->Next(1, &tok, NULL);
LPWSTR lang_id;
tok->GetId(&lang_id);
std::string lang_id_str = CW2A(lang_id);
const char *id = lang_id_str.c_str();
CoTaskMemFree(lang_id);
if (strcmp(voice_string, id) == 0) {
pVoice->SetVoice(tok);
last_voice = (char *)malloc(strlen(voice_string) + 1);
strcpy(last_voice, voice_string);
}
i++;
}
cpEnum->Release();
printf("SAP_Create %s, %s\n", last_voice, voice_string);
last_voice = (char *) malloc(strlen(voice_string) + 1);
strcpy(last_voice, voice_string);
return true;
}
static bool isSpeaking;
bool speakText(Isolate * isolate, Local<Object> opts) {
isSpeaking = true;
Local<Context> context = isolate->GetCurrentContext();
// String::Utf8Value string8(Local<String>::Cast(opts->Get(context, String::NewFromUtf8(isolate, "text", NewStringType::kNormal).ToLocalChecked())));
String::Utf8Value string8(opts->Get(context, String::NewFromUtf8(isolate, "text", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->ToString());
const char * text = *string8;
double speed = opts->Get(context, String::NewFromUtf8(isolate, "rate", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue();
double volume = opts->Get(context, String::NewFromUtf8(isolate, "volume", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue();
double pitch = opts->Get(context, String::NewFromUtf8(isolate, "pitch", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()->NumberValue();
printf("SAPI values: %G %G %G\n", speed, volume, pitch);
if (!speed || speed == 0 || isnan(speed)) {
speed = 100;
}
if (!volume || volume == 0 || isnan(volume)) {
volume = 100;
}
if (!pitch || pitch == 0 || isnan(pitch)) {
pitch = 100;
}
if (pVoice) {
pVoice->SetRate((((long) speed) - 100) / 100);
pVoice->SetVolume((short) volume);
// TODO: include pitch in XML markup, <prosody pitch="+5%">text</prosody>
std::string str(text);
std::wstring speak = str_to_ws(str);
HRESULT hresult = pVoice->Speak(speak.c_str(), SVSFlagsAsync, NULL);
} else {
printf("SAPI Cannot speak text, voice is not set");
}
Local<Function> func = Local<Function>::Cast(opts->Get(context, String::NewFromUtf8(isolate, "success", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked());
return true;
}
bool stopSpeakingText() {
if (pVoice) {
ULONG ref;
pVoice->Skip(L"SENTENCE", 100, &ref);
}
return true;
}
void jsSetup(const FunctionCallbackInfo<Value>& args) {
bool result = setup();
args.GetReturnValue().Set(result);
}
void jsTeardown(const FunctionCallbackInfo<Value>& args) {
bool result = teardown();
args.GetReturnValue().Set(result);
}
void jsSpeak(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Object> opts = Local<Object>::Cast(args[0]);
bool result = speakText(isolate, opts);
args.GetReturnValue().Set(result);
}
void jsStopSpeaking(const FunctionCallbackInfo<Value>& args) {
bool result = stopSpeakingText();
args.GetReturnValue().Set(result);
}
void jsSpeakCheck(const FunctionCallbackInfo<Value>& args) {
if (isSpeaking) {
if (pVoice) {
HANDLE handle = pVoice->SpeakCompleteEvent();
DWORD res = WaitForSingleObject(handle, 1);
if (res == WAIT_ABANDONED || res == WAIT_TIMEOUT) {
isSpeaking = true;
}
else if(res != WAIT_FAILED) {
isSpeaking = false;
}
}
else {
isSpeaking = false;
}
}
args.GetReturnValue().Set(isSpeaking);
}
void jsListVoices(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Array> result = listVoices(isolate);
args.GetReturnValue().Set(result);
}
void jsOpenVoice(const FunctionCallbackInfo<Value>& args) {
String::Utf8Value string(args[0]);
const char * voice_string = *string;
bool result = openVoice(voice_string);
args.GetReturnValue().Set(result);
}
void jsCloseVoice(const FunctionCallbackInfo<Value>& args) {
bool result = closeVoice();
args.GetReturnValue().Set(result);
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "status", jsStatus);
NODE_SET_METHOD(exports, "init", jsSetup);
NODE_SET_METHOD(exports, "teardown", jsTeardown);
NODE_SET_METHOD(exports, "speakText", jsSpeak);
NODE_SET_METHOD(exports, "stopSpeakingText", jsStopSpeaking);
NODE_SET_METHOD(exports, "isSpeaking", jsSpeakCheck);
NODE_SET_METHOD(exports, "getAvailableVoices", jsListVoices);
NODE_SET_METHOD(exports, "openVoice", jsOpenVoice);
NODE_SET_METHOD(exports, "closeVoice", jsCloseVoice);
}
NODE_MODULE(sapi_tts, init)
}