-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode_open_jtalk.cc
367 lines (316 loc) · 13.8 KB
/
node_open_jtalk.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <nan.h>
#include <node_buffer.h>
#include <sstream>
#include "HTS_engine.h"
#include "node_open_jtalk.h"
using namespace std;
using namespace v8;
using namespace node;
/*HTS_fwrite_little_endian*/
/* HTS_fwrite_little_endian: fwrite with byteswap */
inline void ensureLittleEndian(const void *buf, size_t size, size_t n)
{
#ifdef WORDS_BIGENDIAN
HTS_byte_swap(buf, size, n * size);
#endif /* WORDS_BIGENDIAN */
}
RiffHeader::RiffHeader(HTS_Engine& engine)
{
data_05_08 = HTS_GStreamSet_get_total_nsamples(&engine.gss) * sizeof(short) + 36;
data_25_28 = engine.condition.sampling_frequency;
data_29_32 = engine.condition.sampling_frequency * sizeof(short);
data_41_44 = HTS_GStreamSet_get_total_nsamples(&engine.gss) * sizeof(short);
ensureLittleEndian(data_01_04, sizeof(char), 4);
ensureLittleEndian(&data_05_08, sizeof(int), 1);
ensureLittleEndian(data_09_12, sizeof(char), 4);
ensureLittleEndian(data_13_16, sizeof(char), 4);
ensureLittleEndian(&data_17_20, sizeof(int), 1);
ensureLittleEndian(&data_21_22, sizeof(short), 1);
ensureLittleEndian(&data_23_24, sizeof(short), 1);
ensureLittleEndian(&data_25_28, sizeof(int), 1);
ensureLittleEndian(&data_29_32, sizeof(int), 1);
ensureLittleEndian(&data_33_34, sizeof(short), 1);
ensureLittleEndian(&data_35_36, sizeof(short), 1);
ensureLittleEndian(data_37_40, sizeof(char), 4);
ensureLittleEndian(&data_41_44, sizeof(int), 1);
}
RiffHeader HTSEngineClient::createHeader() {
return RiffHeader(engine);
}
/* HTS_Engine_save_riff */
std::vector<short> HTSEngineClient::writeRiffToStream() {
size_t i;
double x;
short temp;
std::vector<short> buffer;
/* write data */
HTS_GStreamSet gss = engine.gss;
for (i = 0; i < HTS_GStreamSet_get_total_nsamples(&gss); i++) {
x = HTS_GStreamSet_get_speech(&gss, i);
if (x > 32767.0)
temp = 32767;
else if (x < -32768.0)
temp = -32768;
else
temp = (short) x;
ensureLittleEndian(&temp, sizeof(short), 1);
buffer.push_back(temp);
}
return buffer;
}
HTSEngineClient::HTSEngineClient(HTS_Engine& hts_engine): engine(hts_engine) {
}
void OpenJTalkOption::UpdateFromV8Object(v8::Local<v8::Object>& object) {
Local<String> sampling_frequency_key = NanNew<String>("samplingFrequency");
Local<String> fperiod_key = NanNew<String>("fPeriod");
Local<String> audio_buff_size_key = NanNew<String>("audioBuffSize");
Local<String> volume_key = NanNew<String>("volume");
Local<String> msd_threshold_key = NanNew<String>("msdThreshold");
Local<String> gv_weight_0_key = NanNew<String>("gvWeightForSpectrum");
Local<String> gv_weight_1_key = NanNew<String>("gvWeightForLogF0");
Local<String> alpha_key = NanNew<String>("alpha");
Local<String> beta_key = NanNew<String>("beta");
Local<String> speed_key = NanNew<String>("speed");
Local<String> add_half_tone_key = NanNew<String>("addHalfTone");
if (object->Has(sampling_frequency_key)) {
sampling_frequency = object->Get(sampling_frequency_key).As<Number>()->Value();
}
if (object->Has(fperiod_key)) {
fperiod = object->Get(fperiod_key).As<Number>()->Value();
}
if (object->Has(audio_buff_size_key)) {
audio_buff_size = object->Get(audio_buff_size_key).As<Number>()->Value();
}
if (object->Has(volume_key)) {
volume = object->Get(volume_key).As<Number>()->Value();
}
if (object->Has(msd_threshold_key)) {
msd_threshold = object->Get(msd_threshold_key).As<Number>()->Value();
}
if (object->Has(gv_weight_0_key)) {
gv_weight_0 = object->Get(gv_weight_0_key).As<Number>()->Value();
}
if (object->Has(gv_weight_1_key)) {
gv_weight_0 = object->Get(gv_weight_1_key).As<Number>()->Value();
}
if (object->Has(alpha_key)) {
alpha = object->Get(alpha_key).As<Number>()->Value();
}
if (object->Has(beta_key)) {
beta = object->Get(beta_key).As<Number>()->Value();
}
if (object->Has(speed_key)) {
speed = object->Get(speed_key).As<Number>()->Value();
}
if (object->Has(add_half_tone_key)) {
add_half_tone = object->Get(add_half_tone_key).As<Number>()->Value();
}
}
void OpenJTalkOption::ReflectToV8Object(v8::Local<v8::Object>& object) {
object->Set(NanNew<String>("samplingFrequency"), NanNew<Uint32>(static_cast<int>(sampling_frequency)));
object->Set(NanNew<String>("fPeriod"), NanNew<Uint32>(static_cast<int>(fperiod)));
object->Set(NanNew<String>("alpha"), NanNew<Number>(alpha));
object->Set(NanNew<String>("beta"), NanNew<Number>(beta));
object->Set(NanNew<String>("speed"), NanNew<Number>(speed));
object->Set(NanNew<String>("addHalfTone"), NanNew<Number>(add_half_tone));
object->Set(NanNew<String>("msdThreshold"), NanNew<Number>(msd_threshold));
object->Set(NanNew<String>("gvWeightForSpectrum"), NanNew<Number>(gv_weight_0));
object->Set(NanNew<String>("gvWeightForLogF0"), NanNew<Number>(gv_weight_1));
object->Set(NanNew<String>("volume"), NanNew<Number>(volume));
object->Set(NanNew<String>("audioBuffSize"), NanNew<Uint32>(static_cast<int>(audio_buff_size)));
}
OpenJTalkWrapper::OpenJTalkWrapper() {
Open_JTalk_initialize(&open_jtalk);
}
OpenJTalkWrapper::~OpenJTalkWrapper() {
Open_JTalk_clear(&open_jtalk);
}
void OpenJTalkWrapper::Load(const std::string& dn_dict, const std::string& fn_voice) {
Open_JTalk_load(&open_jtalk, dn_dict.c_str(), fn_voice.c_str());
}
RiffHeader OpenJTalkWrapper::Synthesis(std::vector<short>& vec, const char *txt) {
int result = 0;
char buff[MAXBUFLEN];
RiffHeader header;
HTSEngineClient htsEngineClient = HTSEngineClient(open_jtalk.engine);
text2mecab(buff, txt);
Mecab_analysis(&open_jtalk.mecab, buff);
mecab2njd(&open_jtalk.njd, Mecab_get_feature(&open_jtalk.mecab),
Mecab_get_size(&open_jtalk.mecab));
njd_set_pronunciation(&open_jtalk.njd);
njd_set_digit(&open_jtalk.njd);
njd_set_accent_phrase(&open_jtalk.njd);
njd_set_accent_type(&open_jtalk.njd);
njd_set_unvoiced_vowel(&open_jtalk.njd);
njd_set_long_vowel(&open_jtalk.njd);
njd2jpcommon(&open_jtalk.jpcommon, &open_jtalk.njd);
JPCommon_make_label(&open_jtalk.jpcommon);
if (JPCommon_get_label_size(&open_jtalk.jpcommon) > 2) {
if (HTS_Engine_synthesize_from_strings
(&open_jtalk.engine, JPCommon_get_label_feature(&open_jtalk.jpcommon),
JPCommon_get_label_size(&open_jtalk.jpcommon)) == TRUE)
result = 1;
vec = htsEngineClient.writeRiffToStream();
header = htsEngineClient.createHeader();
HTS_Engine_refresh(&open_jtalk.engine);
}
JPCommon_refresh(&open_jtalk.jpcommon);
NJD_refresh(&open_jtalk.njd);
Mecab_refresh(&open_jtalk.mecab);
return header;
}
void OpenJTalkWrapper::Update(const OpenJTalkOption& option) {
Open_JTalk_set_sampling_frequency(&open_jtalk, option.sampling_frequency);
Open_JTalk_set_fperiod(&open_jtalk, option.fperiod);
Open_JTalk_set_alpha(&open_jtalk, option.alpha);
Open_JTalk_set_beta(&open_jtalk, option.beta);
Open_JTalk_set_speed(&open_jtalk, option.speed);
Open_JTalk_add_half_tone(&open_jtalk, option.add_half_tone);
Open_JTalk_set_msd_threshold(&open_jtalk, 1, option.msd_threshold);
Open_JTalk_set_gv_weight(&open_jtalk, 0, option.gv_weight_0);
Open_JTalk_set_gv_weight(&open_jtalk, 1, option.gv_weight_1);
Open_JTalk_set_volume(&open_jtalk, option.volume);
Open_JTalk_set_audio_buff_size(&open_jtalk, option.audio_buff_size);
}
OpenJTalkOption OpenJTalkWrapper::GetOption() {
OpenJTalkOption ojt_option(open_jtalk.engine);
return ojt_option;
}
SynthesisWorkerDynamic::SynthesisWorkerDynamic(NanCallback* callback, OpenJTalkWrapper* open_jtalk, const std::string txt) : NanAsyncWorker(callback), open_jtalk(open_jtalk), text(txt) {
}
void SynthesisWorkerDynamic::Execute() {
header = open_jtalk->Synthesis(data, text.c_str());
if (data.size() == 0) {
SetErrorMessage("Failed to synthesize voice.");
}
}
void SynthesisWorkerDynamic::HandleOKCallback() {
NanScope();
std::stringstream stream;
stream.write(reinterpret_cast<char*>(&header), sizeof(header));
stream.write(reinterpret_cast<char*>(data.data()), data.size() * sizeof(short));
Local<Object> jsbuffer = NanNewBufferHandle(stream.str().c_str(), sizeof(header) + data.size() * sizeof(short));
Local<Value> argv[2] = {NanNull(), jsbuffer};
callback->Call(2, argv);
delete open_jtalk;
}
SynthesisWorker::SynthesisWorker(NanCallback* callback, const string txt, const string fn_voice_) : NanAsyncWorker(callback), text(txt), fn_voice(fn_voice_) {
}
void SynthesisWorker::Execute() {
OpenJTalkWrapper open_jtalk;
open_jtalk.Load(DN_DICT, fn_voice);
header = open_jtalk.Synthesis(data, text.c_str());
if (data.size() == 0) {
SetErrorMessage("Failed to synthesize voice.");
}
}
void SynthesisWorker::HandleOKCallback() {
NanScope();
std::stringstream stream;
stream.write(reinterpret_cast<char*>(&header), sizeof(header));
stream.write(reinterpret_cast<char*>(data.data()), data.size() * sizeof(short));
Local<Object> jsbuffer = NanNewBufferHandle(stream.str().c_str(), sizeof(header) + data.size() * sizeof(short));
Local<Value> argv[2] = {NanNull(), jsbuffer};
callback->Call(2, argv);
}
NAN_METHOD(OpenJTalk::Synthesize) {
NanScope();
Local<String> buf = args[0].As<String>();
OpenJTalk* obj = ObjectWrap::Unwrap<OpenJTalk>(args.Holder());
if (args.Length() == 3) {
Local<Function> optionHook = args[1].As<Function>();
Local<Function> callback = args[2].As<Function>();
Local<Object> arg = NanNew<Object>();
OpenJTalkWrapper* open_jtalk = new OpenJTalkWrapper();
open_jtalk->Load(DN_DICT, obj->fn_voice);
OpenJTalkOption option = open_jtalk->GetOption();
option.ReflectToV8Object(arg);
Local<Value> argv[1] = {arg};
Local<Value> new_option = NanMakeCallback(NanGetCurrentContext()->Global(), optionHook, 1, argv);
if (new_option->IsObject()) {
Local<Object> new_option_ = new_option.As<Object>();
option.UpdateFromV8Object(new_option_);
open_jtalk->Update(option);
}
NanCallback* nanCallback = new NanCallback(callback);
SynthesisWorkerDynamic* worker = new SynthesisWorkerDynamic(nanCallback, open_jtalk, string(*String::Utf8Value(buf)));
Local<v8::Object> _this = args.This();
worker->SaveToPersistent("OpenJTalk", _this);
NanAsyncQueueWorker(worker);
} else if (args.Length() == 2) {
Local<Function> callback = args[1].As<Function>();
NanCallback* nanCallback = new NanCallback(callback);
SynthesisWorker* worker = new SynthesisWorker(nanCallback, string(*String::Utf8Value(buf)), obj->fn_voice.c_str());
Local<v8::Object> _this = args.This();
worker->SaveToPersistent("OpenJTalk", _this);
NanAsyncQueueWorker(worker);
}
NanReturnUndefined();
}
NAN_METHOD(OpenJTalk::SynthesizeSync) {
NanScope();
stringstream stream;
vector<short> data;
OpenJTalk* obj = ObjectWrap::Unwrap<OpenJTalk>(args.Holder());
Local<String> buf = args[0].As<String>();
OpenJTalkWrapper open_jtalk;
open_jtalk.Load(DN_DICT, obj->fn_voice.c_str());
if (!args[1].IsEmpty() && args[1]->IsFunction()) {
Local<Function> callback = args[1].As<Function>();
Local<Object> arg = NanNew<Object>();
OpenJTalkOption option = open_jtalk.GetOption();
option.ReflectToV8Object(arg);
Local<Value> argv[1] = {arg};
Local<Value> new_option = NanMakeCallback(NanGetCurrentContext()->Global(), callback, 1, argv);
if (new_option->IsObject()) {
Local<Object> new_option_ = new_option.As<Object>();
option.UpdateFromV8Object(new_option_);
open_jtalk.Update(option);
}
}
RiffHeader header = open_jtalk.Synthesis(data, *String::Utf8Value(buf));
stream.write(reinterpret_cast<char*>(&header), sizeof(header));
stream.write(reinterpret_cast<char*>(data.data()), data.size() * sizeof(short));
Local<Object> jsbuffer = NanNewBufferHandle(stream.str().c_str(), sizeof(header) + data.size() * sizeof(short));
NanReturnValue(jsbuffer);
}
Persistent<FunctionTemplate> OpenJTalk::constructor;
OpenJTalk::OpenJTalk() {
}
OpenJTalk::~OpenJTalk() {
}
void OpenJTalk::Init(Handle<Object> exports) {
NanScope();
// Prepare constructor template
Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New);
tpl->SetClassName(NanNew("OpenJTalk"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
NODE_SET_PROTOTYPE_METHOD(tpl, "synthesize", OpenJTalk::Synthesize);
NODE_SET_PROTOTYPE_METHOD(tpl, "synthesizeSync", OpenJTalk::SynthesizeSync);
NanAssignPersistent(constructor, tpl);
exports->Set(NanNew("OpenJTalk"), tpl->GetFunction());
}
NAN_METHOD(OpenJTalk::New) {
NanScope();
if (args.IsConstructCall()) {
Local<Object> optionsObj = args[0]->ToObject();
Local<String> fn_voice_key = NanNew<String>("voice");
if (optionsObj.IsEmpty() || !optionsObj->Has(fn_voice_key)) {
return NanThrowError("Please specify a voice to use.");
}
Local<String> fn_voice_ = optionsObj->Get(fn_voice_key)->ToString();
// Invoked as constructor: `new OpenJTalk(...)`
OpenJTalk* obj = new OpenJTalk();
obj->fn_voice = *String::Utf8Value(fn_voice_);
obj->Wrap(args.This());
NanReturnValue(args.This());
} else {
// Invoked as plain function `OpenJTalk(...)`, turn into construct call.
// const int argc = 1;
// Local<Value> argv[argc] = { args[0] };
// Local<FunctionTemplate> cons = NanNew<FunctionTemplate>(constructor);
// NanReturnValue(cons->NewInstance(argc, argv));
return NanThrowTypeError("Use the new operator to create new objects");
}
}