-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathengine.cc
651 lines (552 loc) · 23.5 KB
/
engine.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
#include <napi.h>
#include <algorithm>
#include <string>
#include "engine.h"
#include "engine/kana_parser.h"
#include "engine/user_dict.h"
#include "engine/nlohmann/json.hpp"
using namespace Napi;
Napi::Object EngineWrapper::NewInstance(Napi::Env env, const Napi::CallbackInfo& info)
{
Napi::EscapableHandleScope scope(env);
if (info.Length() < 5) {
Napi::TypeError::New(env, "missing arguments").ThrowAsJavaScriptException();
return Napi::Object::New(env);
}
if (!info[0].IsString() || !info[1].IsString() || !info[2].IsString() || !info[3].IsString() || !info[4].IsBoolean()) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return Napi::Object::New(env);
}
const std::initializer_list<napi_value> initArgList = { info[0], info[1], info[2], info[3], info[4] };
Napi::Object obj = env.GetInstanceData<Napi::FunctionReference>()->New(initArgList);
return scope.Escape(napi_value(obj)).ToObject();
}
Napi::Object EngineWrapper::Init(Napi::Env env, Napi::Object exports)
{
Napi::Function func = DefineClass(
env, "EngineWrapper", {
InstanceMethod("audio_query", &EngineWrapper::audio_query),
InstanceMethod("accent_phrases", &EngineWrapper::accent_phrases),
InstanceMethod("mora_data", &EngineWrapper::mora_data),
InstanceMethod("mora_length", &EngineWrapper::mora_length),
InstanceMethod("mora_pitch", &EngineWrapper::mora_pitch),
InstanceMethod("synthesis", &EngineWrapper::synthesis),
InstanceMethod("metas", &EngineWrapper::metas),
InstanceMethod("yukarin_s_forward", &EngineWrapper::yukarin_s_forward),
InstanceMethod("yukarin_sa_forward", &EngineWrapper::yukarin_sa_forward),
InstanceMethod("decode_forward", &EngineWrapper::decode_forward),
InstanceMethod("get_user_dict_words", &EngineWrapper::get_user_dict_words),
InstanceMethod("add_user_dict_word", &EngineWrapper::add_user_dict_word),
InstanceMethod("rewrite_user_dict_word", &EngineWrapper::rewrite_user_dict_word),
InstanceMethod("delete_user_dict_word", &EngineWrapper::delete_user_dict_word),
});
Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
env.SetInstanceData(constructor);
exports.Set("EngineWrapper", func);
return exports;
}
EngineWrapper::EngineWrapper(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<EngineWrapper>(info)
{
std::string openjtalk_dict = info[0].As<Napi::String>().Utf8Value();
std::string default_dict_path = info[1].As<Napi::String>().Utf8Value();
std::string user_dict_root = info[2].As<Napi::String>().Utf8Value();
std::string core_file_path = info[3].As<Napi::String>().Utf8Value();
bool use_gpu = info[4].As<Napi::Boolean>().Value();
try {
m_core = new Core(core_file_path, use_gpu);
m_openjtalk = new OpenJTalk(openjtalk_dict);
std::string user_dict_path = user_dict_root + "user_dict.json";
std::string compiled_dict_path = user_dict_root + "user.dic";
m_openjtalk->default_dict_path = default_dict_path;
m_openjtalk->user_dict_path = user_dict_path;
m_openjtalk->user_mecab = compiled_dict_path;
m_openjtalk = user_dict_startup_processing(m_openjtalk);
m_openjtalk = update_dict(m_openjtalk);
m_engine = new SynthesisEngine(m_core, m_openjtalk);
}
catch (std::exception& err) {
Napi::Error::New(info.Env(), err.what()).ThrowAsJavaScriptException();
}
}
EngineWrapper::~EngineWrapper()
{
m_core->finalize();
delete m_core;
m_core = nullptr;
}
void EngineWrapper::create_execute_error(Napi::Env env, const char* func_name)
{
std::string last_error = std::string(m_core->last_error_message());
std::string err = "failed to execute: " + std::string(func_name) + "\n" + last_error;
Napi::Error::New(env, err).ThrowAsJavaScriptException();
}
Napi::Value EngineWrapper::audio_query(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 2) {
Napi::TypeError::New(env, "missing arguments").ThrowAsJavaScriptException();
return env.Null();
}
if (!info[0].IsString() || !info[1].IsNumber()) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
Napi::Array accent_phrases;
try {
accent_phrases = m_engine->create_accent_phrases(env, info[0].As<Napi::String>(), info[1].As<Napi::Number>());
} catch (std::exception& err) {
Napi::Error::New(info.Env(), err.what()).ThrowAsJavaScriptException();
return env.Null();
}
Napi::String kana;
try {
kana = create_kana(env, accent_phrases);
}
catch (std::exception& err) {
Napi::Error::New(info.Env(), err.what()).ThrowAsJavaScriptException();
return env.Null();
}
Napi::Object audio_query = Napi::Object::New(env);
audio_query.Set("accent_phrases", accent_phrases);
audio_query.Set("speedScale", 1);
audio_query.Set("pitchScale", 0);
audio_query.Set("intonationScale", 1);
audio_query.Set("volumeScale", 1);
audio_query.Set("prePhonemeLength", 0.1);
audio_query.Set("postPhonemeLength", 0.1);
audio_query.Set("outputSamplingRate", m_engine->default_sampling_rate);
audio_query.Set("outputStereo", false);
audio_query.Set("kana", kana);
return audio_query;
}
Napi::Value EngineWrapper::accent_phrases(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 3) {
Napi::TypeError::New(env, "missing arguments").ThrowAsJavaScriptException();
return env.Null();
}
if (!info[0].IsString() || !info[1].IsNumber() || !info[2].IsBoolean()) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
Napi::Array accent_phrases;
if (info[2].As<Napi::Boolean>().Value()) {
try {
accent_phrases = parse_kana(env, info[0].As<Napi::String>().Utf8Value());
}
catch (std::exception& err) {
Napi::Error::New(info.Env(), err.what()).ThrowAsJavaScriptException();
return env.Null();
}
accent_phrases = m_engine->replace_mora_data(accent_phrases, info[1].As<Napi::Number>().Int64Value());
}
else {
accent_phrases = m_engine->create_accent_phrases(env, info[0].As<Napi::String>(), info[1].As<Napi::Number>());
}
return accent_phrases;
}
Napi::Value EngineWrapper::mora_data(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 2) {
Napi::TypeError::New(env, "missing arguments").ThrowAsJavaScriptException();
return env.Null();
}
// TODO: 厳密な型検査を行いたいが、パフォーマンスの低下が懸念
if (!info[0].IsArray() || !info[1].IsNumber()) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
return m_engine->replace_mora_data(info[0].As<Napi::Array>(), info[1].As<Napi::Number>().Int64Value());
}
Napi::Value EngineWrapper::mora_length(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 2) {
Napi::TypeError::New(env, "missing arguments").ThrowAsJavaScriptException();
return env.Null();
}
// TODO: 厳密な型検査
if (!info[0].IsArray() || !info[1].IsNumber()) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
return m_engine->replace_phoneme_length(info[0].As<Napi::Array>(), info[1].As<Napi::Number>().Int64Value());
}
Napi::Value EngineWrapper::mora_pitch(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 2) {
Napi::TypeError::New(env, "missing arguments").ThrowAsJavaScriptException();
return env.Null();
}
// TODO: 厳密な型検査
if (!info[0].IsArray() || !info[1].IsNumber()) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
return m_engine->replace_mora_pitch(info[0].As<Napi::Array>(), info[1].As<Napi::Number>().Int64Value());
}
Napi::Value EngineWrapper::synthesis(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 3) {
Napi::TypeError::New(env, "missing arguments").ThrowAsJavaScriptException();
return env.Null();
}
if (!info[0].IsObject() || !info[1].IsNumber() || !info[2].IsBoolean()) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
Napi::Object audio_query = info[0].As<Napi::Object>();
if (
!audio_query.Has("accent_phrases") ||
!audio_query.Has("speedScale") ||
!audio_query.Has("pitchScale") ||
!audio_query.Has("intonationScale") ||
!audio_query.Has("volumeScale") ||
!audio_query.Has("prePhonemeLength") ||
!audio_query.Has("postPhonemeLength") ||
!audio_query.Has("outputSamplingRate") ||
!audio_query.Has("outputStereo") ||
!audio_query.Has("kana")
) {
Napi::TypeError::New(env, "wrong audio query").ThrowAsJavaScriptException();
return env.Null();
}
// TODO: accent_phraseの厳密な型検査
Napi::Value accent_phrases = audio_query.Get("accent_phrases");
Napi::Value speed_scale = audio_query.Get("speedScale");
Napi::Value pitch_scale = audio_query.Get("pitchScale");
Napi::Value intonation_scale = audio_query.Get("intonationScale");
Napi::Value volume_scale = audio_query.Get("volumeScale");
Napi::Value pre_phoneme_length = audio_query.Get("prePhonemeLength");
Napi::Value post_phoneme_length = audio_query.Get("postPhonemeLength");
Napi::Value output_sampling_rate = audio_query.Get("outputSamplingRate");
Napi::Value output_stereo = audio_query.Get("outputStereo");
Napi::Value kana = audio_query.Get("kana");
if (!accent_phrases.IsArray() ||
!speed_scale.IsNumber() ||
!pitch_scale.IsNumber() ||
!intonation_scale.IsNumber() ||
!volume_scale.IsNumber() ||
!pre_phoneme_length.IsNumber() ||
!post_phoneme_length.IsNumber() ||
!output_sampling_rate.IsNumber() ||
!output_stereo.IsBoolean() ||
!kana.IsString()
) {
Napi::TypeError::New(env, "wrong audio query params").ThrowAsJavaScriptException();
return env.Null();
}
return m_engine->synthesis_wave_format(env, audio_query, info[1].As<Napi::Number>().Int64Value(), info[2].As<Napi::Boolean>().Value());
}
Napi::Value EngineWrapper::metas(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
Napi::String metas_string = Napi::String::New(env, m_core->metas());
return metas_string;
}
Napi::Value EngineWrapper::yukarin_s_forward(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
if (info.Length() < 2) {
Napi::TypeError::New(env, "missing arguments").ThrowAsJavaScriptException();
return env.Null();
}
if (!info[0].IsArray() || !info[1].IsNumber()) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
int length = (int)info[0].As<Napi::Array>().Length();
std::vector<long> phoneme_list(length);
for (int i = 0; i < length; i++) {
Napi::Value val = info[0].As<Napi::Array>()[i];
phoneme_list[i] = (long)val.As<Napi::Number>().Int64Value();
}
long speaker_id = info[1].As<Napi::Number>().Int64Value();
std::vector<float> output(length, 0);
bool success = m_core->yukarin_s_forward(length, phoneme_list.data(), &speaker_id, output.data());
if (!success) {
create_execute_error(env, __func__);
return env.Null();
}
Napi::Array output_array = Napi::Array::New(env, length);
for (int i = 0; i < length; i++) {
output_array[i] = Napi::Number::New(env, output[i]);
}
return output_array;
}
Napi::Value EngineWrapper::yukarin_sa_forward(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
if (info.Length() < 7) {
Napi::TypeError::New(env, "missing arguments").ThrowAsJavaScriptException();
return env.Null();
}
bool wrong_arg = false;
for (int i = 0; i < 6; i++) wrong_arg |= !info[i].IsArray();
if (wrong_arg || !info[6].IsNumber()) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
int length = info[0].As<Napi::Array>().Length();
std::vector<long> vowel_phoneme_list(length);
std::vector<long> consonant_phoneme_list(length);
std::vector<long> start_accent_list(length);
std::vector<long> end_accent_list(length);
std::vector<long> start_accent_phrase_list(length);
std::vector<long> end_accent_phrase_list(length);
for (int i = 0; i < length; i++) {
Napi::Value val0 = info[0].As<Napi::Array>()[i];
vowel_phoneme_list[i] = val0.As<Napi::Number>().Int32Value();
Napi::Value val1 = info[1].As<Napi::Array>()[i];
consonant_phoneme_list[i] = val1.As<Napi::Number>().Int32Value();
Napi::Value val2 = info[2].As<Napi::Array>()[i];
start_accent_list[i] = val2.As<Napi::Number>().Int32Value();
Napi::Value val3 = info[3].As<Napi::Array>()[i];
end_accent_list[i] = val3.As<Napi::Number>().Int32Value();
Napi::Value val4 = info[4].As<Napi::Array>()[i];
start_accent_phrase_list[i] = val4.As<Napi::Number>().Int32Value();
Napi::Value val5 = info[5].As<Napi::Array>()[i];
end_accent_phrase_list[i] = val5.As<Napi::Number>().Int32Value();
}
long speaker_id = info[6].As<Napi::Number>().Int64Value();
std::vector<float> output(length, 0);
if (!m_core->yukarin_sa_forward(
length,
vowel_phoneme_list.data(),
consonant_phoneme_list.data(),
start_accent_list.data(),
end_accent_list.data(),
start_accent_phrase_list.data(),
end_accent_phrase_list.data(),
&speaker_id,
output.data()
)) {
create_execute_error(env, __func__);
return env.Null();
}
Napi::Array output_array = Napi::Array::New(env, length);
for (int i = 0; i < length; i++) {
output_array[i] = Napi::Number::New(env, output[i]);
}
return output_array;
}
Napi::Value EngineWrapper::decode_forward(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
if (info.Length() < 3) {
Napi::TypeError::New(env, "missing arguments").ThrowAsJavaScriptException();
return env.Null();
}
if (!info[0].IsArray() || !info[1].IsArray() || !info[2].IsNumber()) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
Napi::Array f0_array = info[0].As<Napi::Array>();
Napi::Array phoneme_array = info[1].As<Napi::Array>();
if (!phoneme_array.IsArray()) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
Napi::Value phoneme_array_value = phoneme_array[(uint32_t)0];
Napi::Array phoneme_array_array = phoneme_array_value.As<Napi::Array>();
Napi::Value phoneme_value = phoneme_array_array[(uint32_t)0];
if (!phoneme_value.IsNumber()) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
int length = info[0].As<Napi::Array>().Length();
int phoneme_size = phoneme_array_array.Length();
std::vector<float> f0(length);
std::vector<float> phoneme(length * phoneme_size);
for (int i = 0; i < length; i++) {
Napi::Value val1 = f0_array[i];
f0[i] = val1.As<Napi::Number>().FloatValue();
phoneme_array_value = phoneme_array[i];
phoneme_array_array = phoneme_array_value.As<Napi::Array>();
for (int j = 0; j < phoneme_size; j++) {
Napi::Value val2 = phoneme_array_array[j];
phoneme[i * length + j] = val2.As<Napi::Number>().FloatValue();
}
}
long speaker_id = info[2].As<Napi::Number>().Int64Value();
int output_size = length * 256;
std::vector<float> output(output_size, 0.0);
if (!m_core->decode_forward(
length, phoneme_size, f0.data(), phoneme.data(), &speaker_id, output.data()
)) {
create_execute_error(env, __func__);
return env.Null();
}
Napi::Array output_array = Napi::Array::New(env, output_size);
for (int i = 0; i < output_size; i++) {
output_array[i] = Napi::Number::New(env, output[i]);
}
return output_array;
}
Napi::Value EngineWrapper::get_user_dict_words(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
json user_dict;
try {
user_dict = read_dict(m_openjtalk->user_dict_path);
} catch (std::exception& err) {
Napi::Error::New(info.Env(), err.what()).ThrowAsJavaScriptException();
return env.Null();
}
Napi::Object result = Napi::Object::New(env);
for (auto &item : user_dict.items()) {
std::string key = item.key();
json value = item.value();
Napi::Object result_child = Napi::Object::New(env);
result_child.Set("surface", value["surface"].get<std::string>());
result_child.Set("priority", value["priority"].get<int>());
result_child.Set("context_id", value["context_id"].get<int>());
result_child.Set("part_of_speech", value["part_of_speech"].get<std::string>());
result_child.Set("part_of_speech_detail_1", value["part_of_speech_detail_1"].get<std::string>());
result_child.Set("part_of_speech_detail_2", value["part_of_speech_detail_2"].get<std::string>());
result_child.Set("part_of_speech_detail_3", value["part_of_speech_detail_3"].get<std::string>());
result_child.Set("inflectional_type", value["inflectional_type"].get<std::string>());
result_child.Set("inflectional_form", value["inflectional_form"].get<std::string>());
result_child.Set("stem", value["stem"].get<std::string>());
result_child.Set("yomi", value["yomi"].get<std::string>());
result_child.Set("pronunciation", value["pronunciation"].get<std::string>());
result_child.Set("accent_type", value["accent_type"].get<int>());
result_child.Set("mora_count", value["mora_count"].get<int>());
result_child.Set("accent_associative_rule", value["accent_associative_rule"].get<std::string>());
result[key] = result_child;
}
return result;
}
Napi::Value EngineWrapper::add_user_dict_word(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 3) {
Napi::TypeError::New(env, "missing arguments").ThrowAsJavaScriptException();
return env.Null();
}
if (!info[0].IsString() || !info[1].IsString() || !info[2].IsNumber()) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
if (info.Length() >= 4 && !(info[3].IsUndefined() || info[3].IsString())) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
if (info.Length() >= 5 && !(info[4].IsUndefined() || info[4].IsNumber())) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
std::string surface = info[0].As<Napi::String>().Utf8Value();
std::string pronunciation = info[1].As<Napi::String>().Utf8Value();
int accent_type = info[2].As<Napi::Number>().Int32Value();
std::string *word_type = nullptr;
std::string word_type_value;
if (info[3].IsString()) {
word_type_value = info[3].As<Napi::String>().Utf8Value();
word_type = &word_type_value;
}
int *priority = nullptr;
int priority_value;
if (info[4].IsNumber()) {
priority_value = info[4].As<Napi::Number>().Int32Value();
priority = &priority_value;
}
std::string word_uuid;
try {
auto result = apply_word(
m_openjtalk,
surface,
pronunciation,
accent_type,
word_type,
priority
);
word_uuid = result.first;
m_openjtalk = result.second;
m_engine->update_openjtalk(m_openjtalk);
} catch (std::exception& err) {
Napi::Error::New(info.Env(), err.what()).ThrowAsJavaScriptException();
return env.Null();
}
return Napi::String::New(env, word_uuid);
}
Napi::Value EngineWrapper::rewrite_user_dict_word(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 4) {
Napi::TypeError::New(env, "missing arguments").ThrowAsJavaScriptException();
return env.Null();
}
if (!info[0].IsString() || !info[1].IsString() || !info[2].IsNumber() || !info[3].IsString()) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
if (info.Length() >= 5 && !(info[4].IsUndefined() || info[4].IsString())) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
if (info.Length() == 6 && !(info[5].IsUndefined() || info[5].IsNumber())) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
std::string surface = info[0].As<Napi::String>().Utf8Value();
std::string pronunciation = info[1].As<Napi::String>().Utf8Value();
int accent_type = info[2].As<Napi::Number>().Int32Value();
std::string word_uuid = info[3].As<Napi::String>().Utf8Value();
std::string *word_type = nullptr;
std::string word_type_value;
if (info[4].IsString()) {
word_type_value = info[4].As<Napi::String>().Utf8Value();
word_type = &word_type_value;
}
int *priority = nullptr;
int priority_value;
if (info[5].IsNumber()) {
priority_value = info[5].As<Napi::Number>().Int32Value();
priority = &priority_value;
}
try {
m_openjtalk = rewrite_word(
m_openjtalk,
word_uuid,
surface,
pronunciation,
accent_type,
word_type,
priority
);
m_engine->update_openjtalk(m_openjtalk);
} catch (std::exception& err) {
Napi::Error::New(info.Env(), err.what()).ThrowAsJavaScriptException();
return env.Null();
}
return env.Null();
}
Napi::Value EngineWrapper::delete_user_dict_word(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 1) {
Napi::TypeError::New(env, "missing arguments").ThrowAsJavaScriptException();
return env.Null();
}
if (!info[0].IsString()) {
Napi::TypeError::New(env, "wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
std::string word_uuid = info[0].As<Napi::String>().Utf8Value();
try {
m_openjtalk = delete_word(
m_openjtalk,
word_uuid
);
m_engine->update_openjtalk(m_openjtalk);
} catch (std::exception& err) {
Napi::Error::New(info.Env(), err.what()).ThrowAsJavaScriptException();
return env.Null();
}
return env.Null();
}
Napi::Object CreateObject(const Napi::CallbackInfo& info) {
return EngineWrapper::NewInstance(info.Env(), info);
}
Napi::Object Initialize(Napi::Env env, Napi::Object exports) {
Napi::Object new_exports = Napi::Function::New(env, CreateObject);
return EngineWrapper::Init(env, new_exports);
}
NODE_API_MODULE(core, Initialize)