2 * Copyright (c) 2011-2014 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
19 #include "ttsd_main.h"
20 #include "ttsd_engine_agent.h"
21 #include "ttsd_config.h"
23 #define ENGINE_PATH_SIZE 256
26 * Internal data structure
34 /* info for using engine load */
40 /* engine base setting */
46 ttspe_funcs_s* pefuncs;
47 ttspd_funcs_s* pdfuncs;
49 int (*ttsp_load_engine)(const ttspd_funcs_s* pdfuncs, ttspe_funcs_s* pefuncs);
50 int (*ttsp_unload_engine)();
57 char* setting_ug_path;
72 static bool g_agent_init;
74 /** TTS engine list */
75 static GList *g_engine_list;
77 /** Current engine information */
78 static ttsengine_s g_cur_engine;
80 /** Current voice information */
81 static GSList* g_cur_voices;
83 /** Result callback function */
84 static synth_result_callback g_result_cb;
87 /** Set current engine */
88 static int __internal_set_current_engine(const char* engine_uuid);
90 /** Check engine id */
91 static int __internal_check_engine_id(const char* engine_uuid);
93 /** Update engine list */
94 static int __internal_update_engine_list();
96 /** Get engine info */
97 static int __internal_get_engine_info(const char* filepath, ttsengine_info_s** info);
99 /** Callback function for result */
100 bool __result_cb(ttsp_result_event_e event, const void* data, unsigned int data_size,
101 ttsp_audio_type_e audio_type, int rate, void *user_data);
103 /** Callback function for voice list */
104 static bool __supported_voice_cb(const char* language, int type, void* user_data);
106 /** Free voice list */
107 static void __free_voice_list(GList* voice_list);
109 /** Callback function for engine info */
110 static void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name,
111 bool use_network, void* user_data);
115 static int ttsd_print_enginelist();
117 static int ttsd_print_voicelist();
119 static const char* __ttsd_get_engine_error_code(ttsp_error_e err)
122 case TTSP_ERROR_NONE: return "TTSP_ERROR_NONE";
123 case TTSP_ERROR_OUT_OF_MEMORY: return "TTSP_ERROR_OUT_OF_MEMORY";
124 case TTSP_ERROR_IO_ERROR: return "TTSP_ERROR_IO_ERROR";
125 case TTSP_ERROR_INVALID_PARAMETER: return "TTSP_ERROR_INVALID_PARAMETER";
126 case TTSP_ERROR_OUT_OF_NETWORK: return "TTSP_ERROR_OUT_OF_NETWORK";
127 case TTSP_ERROR_INVALID_STATE: return "TTSP_ERROR_INVALID_STATE";
128 case TTSP_ERROR_INVALID_VOICE: return "TTSP_ERROR_INVALID_VOICE";
129 case TTSP_ERROR_OPERATION_FAILED: return "TTSP_ERROR_OPERATION_FAILED";
131 return "Invalid error code";
135 int ttsd_engine_agent_init(synth_result_callback result_cb)
137 /* initialize static data */
138 if (result_cb == NULL) {
139 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] invalid parameter");
140 return TTSD_ERROR_INVALID_PARAMETER;
143 g_result_cb = result_cb;
145 g_cur_engine.engine_uuid = NULL;
146 g_cur_engine.engine_name = NULL;
147 g_cur_engine.engine_path = NULL;
149 g_cur_engine.is_set = false;
150 g_cur_engine.handle = NULL;
151 g_cur_engine.pefuncs = (ttspe_funcs_s*)calloc(1, sizeof(ttspe_funcs_s));
152 g_cur_engine.pdfuncs = (ttspd_funcs_s*)calloc(1, sizeof(ttspd_funcs_s));
156 if (0 != ttsd_config_get_default_voice(&(g_cur_engine.default_lang), &(g_cur_engine.default_vctype))) {
157 SLOG(LOG_WARN, get_tag(), "[Server WARNING] There is No default voice in config");
158 /* Set default voice */
159 g_cur_engine.default_lang = strdup(TTS_BASE_LANGUAGE);
160 g_cur_engine.default_vctype = TTSP_VOICE_TYPE_FEMALE;
163 if (0 != ttsd_config_get_default_speed(&(g_cur_engine.default_speed))) {
164 SLOG(LOG_WARN, get_tag(), "[Server WARNING] There is No default speed in config");
165 g_cur_engine.default_speed = TTS_SPEED_NORMAL;
168 if (0 != ttsd_config_get_default_pitch(&(g_cur_engine.default_pitch))) {
169 SLOG(LOG_WARN, get_tag(), "[Server WARNING] There is No default pitch in config");
170 g_cur_engine.default_pitch = TTS_PITCH_NORMAL;
173 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Initialize Engine Agent");
178 int ttsd_engine_agent_release()
180 if (false == g_agent_init) {
181 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
182 return TTSD_ERROR_OPERATION_FAILED;
185 /* unload current engine */
186 ttsd_engine_agent_unload_current_engine();
188 /* release engine list */
190 ttsengine_info_s *data = NULL;
192 if (g_list_length(g_engine_list) > 0) {
193 /* Get a first item */
194 iter = g_list_first(g_engine_list);
195 while (NULL != iter) {
196 /* Get data from item */
198 iter = g_list_remove(iter, data);
201 if (NULL != data->engine_uuid) free(data->engine_uuid);
202 if (NULL != data->engine_name) free(data->engine_name);
203 if (NULL != data->setting_ug_path) free(data->setting_ug_path);
204 if (NULL != data->engine_path) free(data->engine_path);
211 /* release current engine data */
212 if (g_cur_engine.engine_uuid != NULL) free(g_cur_engine.engine_uuid);
213 if (g_cur_engine.engine_name != NULL) free(g_cur_engine.engine_name);
214 if (g_cur_engine.engine_path != NULL) free(g_cur_engine.engine_path);
216 if (g_cur_engine.pefuncs != NULL) free(g_cur_engine.pefuncs);
217 if (g_cur_engine.pdfuncs != NULL) free(g_cur_engine.pdfuncs);
218 if (g_cur_engine.default_lang != NULL) free(g_cur_engine.default_lang);
220 g_agent_init = false;
222 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Release Engine Agent");
227 int ttsd_engine_agent_initialize_current_engine()
229 if (false == g_agent_init) {
230 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
231 return TTSD_ERROR_OPERATION_FAILED;
234 /* update engine list */
235 if (0 != __internal_update_engine_list()) {
236 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] No engine error");
237 return TTSD_ERROR_OPERATION_FAILED;
240 /* 2. get current engine from config */
241 char* cur_engine_uuid = NULL;
242 bool is_get_engineid_from_config = false;
244 if (0 != ttsd_config_get_default_engine(&cur_engine_uuid)) {
245 /*not set current engine */
246 /*set system default engine*/
248 ttsengine_info_s *data = NULL;
250 if (g_list_length(g_engine_list) > 0) {
251 iter = g_list_first(g_engine_list);
255 if (NULL != data->engine_uuid) {
256 cur_engine_uuid = strdup(data->engine_uuid);
257 ttsd_config_set_default_engine(cur_engine_uuid);
259 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Data of current engine is corrupt");
260 return TTSD_ERROR_OPERATION_FAILED;
264 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] Fail to set a engine of engine list");
265 return TTSD_ERROR_OPERATION_FAILED;
268 is_get_engineid_from_config = false;
270 is_get_engineid_from_config = true;
273 if (NULL == cur_engine_uuid) {
274 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Current engine id is NULL");
275 return TTSD_ERROR_OPERATION_FAILED;
278 /* check whether cur engine uuid is valid or not. */
279 if (0 != __internal_check_engine_id(cur_engine_uuid)) {
280 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] It is not valid engine id from config");
284 if (g_list_length(g_engine_list) > 0)
285 iter = g_list_first(g_engine_list);
287 SLOG(LOG_WARN, get_tag(), "[Engine Agent ERROR] NO TTS Engine !!");
288 if (NULL != cur_engine_uuid) free(cur_engine_uuid);
289 return TTSD_ERROR_OPERATION_FAILED;
292 if (cur_engine_uuid != NULL) free(cur_engine_uuid);
293 ttsengine_info_s *data = NULL;
296 cur_engine_uuid = strdup(data->engine_uuid);
298 is_get_engineid_from_config = false;
301 if (NULL != cur_engine_uuid)
302 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Current Engine Id : %s", cur_engine_uuid);
304 return TTSD_ERROR_OPERATION_FAILED;
306 /* set current engine */
307 if (0 != __internal_set_current_engine(cur_engine_uuid)) {
308 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to set current engine ");
309 if (NULL != cur_engine_uuid) free(cur_engine_uuid);
310 return TTSD_ERROR_OPERATION_FAILED;
313 if (false == is_get_engineid_from_config) {
314 if (0 != ttsd_config_set_default_engine(cur_engine_uuid)) {
315 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to set id to config");
319 if (NULL != cur_engine_uuid) free(cur_engine_uuid);
321 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set current engine");
326 static int __internal_check_engine_id(const char* engine_uuid)
329 ttsengine_s *data = NULL;
331 if (g_list_length(g_engine_list) > 0) {
332 iter = g_list_first(g_engine_list);
334 while (NULL != iter) {
337 if (0 == strncmp(engine_uuid, data->engine_uuid, strlen(data->engine_uuid)))
340 iter = g_list_next(iter);
347 void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name,
348 bool use_network, void* user_data)
350 ttsengine_info_s* temp = (ttsengine_info_s*)user_data;
352 if (NULL != engine_uuid)
353 temp->engine_uuid = strdup(engine_uuid);
355 if (NULL != engine_name)
356 temp->engine_name = strdup(engine_name);
358 if (NULL != setting_ug_name)
359 temp->setting_ug_path = strdup(setting_ug_name);
361 temp->use_network = use_network;
365 static int __internal_get_engine_info(const char* filepath, ttsengine_info_s** info)
370 handle = dlopen(filepath, RTLD_LAZY);
373 SECURE_SLOG(LOG_WARN, get_tag(), "[Engine Agent] Invalid engine : %s", filepath);
374 return TTSD_ERROR_OPERATION_FAILED;
377 /* link engine to daemon */
378 dlsym(handle, "ttsp_load_engine");
379 if ((error = dlerror()) != NULL) {
380 SECURE_SLOG(LOG_WARN, get_tag(), "[Engine Agent] Fail to open ttsp_load_engine : path(%s) message(%s)", filepath, error);
382 return TTSD_ERROR_OPERATION_FAILED;
385 dlsym(handle, "ttsp_unload_engine");
386 if ((error = dlerror()) != NULL) {
387 SECURE_SLOG(LOG_WARN, get_tag(), "[Engine Agent] Fail to open ttsp_unload_engine : path(%s) message(%s)", filepath, error);
389 return TTSD_ERROR_OPERATION_FAILED;
392 int (*get_engine_info)(ttsp_engine_info_cb callback, void* user_data);
394 get_engine_info = (int (*)(ttsp_engine_info_cb, void*))dlsym(handle, "ttsp_get_engine_info");
395 if (NULL != (error = dlerror()) || NULL == get_engine_info) {
396 SLOG(LOG_WARN, get_tag(), "[Engine Agent] Fail to open ttsp_get_engine_info() :path(%s) message(%s)", filepath, error);
398 return TTSD_ERROR_OPERATION_FAILED;
401 ttsengine_info_s* temp;
402 temp = (ttsengine_info_s*)calloc(1, sizeof(ttsengine_info_s));
404 SLOG(LOG_WARN, get_tag(), "[Engine Agent] Fail to alloc memory");
406 return TTSD_ERROR_OUT_OF_MEMORY;
409 /* get engine info */
410 if (0 != get_engine_info(&__engine_info_cb, (void*)temp)) {
411 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get engine info");
414 return TTSD_ERROR_OPERATION_FAILED;
420 if (TTSD_MODE_SCREEN_READER == ttsd_get_mode() && true == temp->use_network) {
421 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent WARNING] %s is invalid because of network based", temp->engine_name);
422 if (NULL != temp->engine_uuid) free(temp->engine_uuid);
423 if (NULL != temp->engine_name) free(temp->engine_name);
424 if (NULL != temp->setting_ug_path) free(temp->setting_ug_path);
426 return TTSD_ERROR_OPERATION_FAILED;
429 temp->engine_path = strdup(filepath);
431 SLOG(LOG_DEBUG, get_tag(), "----- Valid engine");
432 SECURE_SLOG(LOG_DEBUG, get_tag(), "Engine uuid : %s", temp->engine_uuid);
433 SECURE_SLOG(LOG_DEBUG, get_tag(), "Engine name : %s", temp->engine_name);
434 SECURE_SLOG(LOG_DEBUG, get_tag(), "Setting path : %s", temp->setting_ug_path);
435 SECURE_SLOG(LOG_DEBUG, get_tag(), "Engine path : %s", temp->engine_path);
436 SECURE_SLOG(LOG_DEBUG, get_tag(), "Use network : %s", temp->use_network ? "true" : "false");
437 SLOG(LOG_DEBUG, get_tag(), "-----");
438 SLOG(LOG_DEBUG, get_tag(), " ");
445 static int __internal_update_engine_list()
447 /* relsease engine list */
449 ttsengine_info_s *data = NULL;
451 if (g_list_length(g_engine_list) > 0) {
452 iter = g_list_first(g_engine_list);
454 while (NULL != iter) {
457 if (data != NULL) free(data);
458 g_engine_list = g_list_remove_link(g_engine_list, iter);
460 iter = g_list_first(g_engine_list);
464 /* get file name from engine directory and get engine information from each filename */
468 struct dirent *dirp = NULL;
469 dp = opendir(TTS_DEFAULT_ENGINE);
473 ret = readdir_r(dp, &entry, &dirp);
475 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to read directory");
480 ttsengine_info_s* info;
481 char* filepath = NULL;
484 file_size = strlen(TTS_DEFAULT_ENGINE) + strlen(dirp->d_name) + 5;
485 filepath = (char*)calloc(file_size, sizeof(char));
487 if (NULL != filepath) {
488 snprintf(filepath, file_size, "%s/%s", TTS_DEFAULT_ENGINE, dirp->d_name);
490 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not enough memory!!" );
494 /* get its info and update engine list */
495 if (0 == __internal_get_engine_info(filepath, &info)) {
496 /* add engine info to g_engine_list */
497 g_engine_list = g_list_append(g_engine_list, info);
500 if (NULL != filepath) free(filepath);
502 } while (NULL != dirp);
507 dp = opendir(TTS_DOWNLOAD_ENGINE);
511 ret = readdir_r(dp, &entry, &dirp);
513 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to read directory");
518 ttsengine_info_s* info;
519 char* filepath = NULL;
522 file_size = strlen(TTS_DOWNLOAD_ENGINE) + strlen(dirp->d_name) + 5;
523 filepath = (char*)calloc(file_size, sizeof(char));
525 if (NULL != filepath) {
526 snprintf(filepath, file_size, "%s/%s", TTS_DOWNLOAD_ENGINE, dirp->d_name);
528 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not enough memory!!" );
532 /* get its info and update engine list */
533 if (0 == __internal_get_engine_info(filepath, &info)) {
534 /* add engine info to g_engine_list */
535 g_engine_list = g_list_append(g_engine_list, info);
538 if (NULL != filepath) free(filepath);
540 } while (NULL != dirp);
545 if (g_list_length(g_engine_list) <= 0) {
546 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No Engine");
547 return TTSD_ERROR_OPERATION_FAILED;
550 #ifdef ENGINE_AGENT_DEBUG
551 ttsd_print_enginelist();
556 static int __internal_set_current_engine(const char* engine_uuid)
558 /* check whether engine id is valid or not. */
560 ttsengine_info_s *data = NULL;
563 if (g_list_length(g_engine_list) > 0) {
564 iter = g_list_first(g_engine_list);
566 while (NULL != iter) {
569 if (0 == strncmp(data->engine_uuid, engine_uuid, strlen(engine_uuid))) {
575 iter = g_list_next(iter);
579 /* If current engine does not exist, return error */
581 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Cannot find engine id");
582 return TTSD_ERROR_OPERATION_FAILED;
584 if (g_cur_engine.engine_uuid != NULL) {
585 /*compare current engine uuid */
586 if (0 == strncmp(g_cur_engine.engine_uuid, data->engine_uuid, strlen(engine_uuid))) {
587 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] tts engine has already been set");
593 /* set data from g_engine_list */
594 if (g_cur_engine.engine_uuid != NULL) free(g_cur_engine.engine_uuid);
595 if (g_cur_engine.engine_name != NULL) free(g_cur_engine.engine_name);
596 if (g_cur_engine.engine_path != NULL) free(g_cur_engine.engine_path);
598 if (NULL == data->engine_uuid || NULL == data->engine_name || NULL == data->engine_path) {
599 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Engine data is NULL");
600 return TTSD_ERROR_OPERATION_FAILED;
603 g_cur_engine.engine_uuid = strdup(data->engine_uuid);
604 g_cur_engine.engine_name = strdup(data->engine_name);
605 g_cur_engine.engine_path = strdup(data->engine_path);
607 g_cur_engine.handle = NULL;
608 g_cur_engine.is_loaded = false;
609 g_cur_engine.is_set = true;
610 g_cur_engine.need_network = data->use_network;
612 SLOG(LOG_DEBUG, get_tag(), "-----");
613 SECURE_SLOG(LOG_DEBUG, get_tag(), "Current engine uuid : %s", g_cur_engine.engine_uuid);
614 SECURE_SLOG(LOG_DEBUG, get_tag(), "Current engine name : %s", g_cur_engine.engine_name);
615 SECURE_SLOG(LOG_DEBUG, get_tag(), "Current engine path : %s", g_cur_engine.engine_path);
616 SLOG(LOG_DEBUG, get_tag(), "-----");
621 int __ttsd_get_mode(ttsp_mode_e* mode)
624 SLOG(LOG_ERROR, get_tag(), "[ERROR] Input parameter is null");
625 return TTSP_ERROR_INVALID_PARAMETER;
628 switch (ttsd_get_mode()) {
629 case TTSD_MODE_DEFAULT: *mode = TTSP_MODE_DEFAULT; break;
630 case TTSD_MODE_NOTIFICATION: *mode = TTSP_MODE_NOTIFICATION; break;
631 case TTSD_MODE_SCREEN_READER: *mode = TTSP_MODE_SCREEN_READER; break;
633 SLOG(LOG_ERROR, get_tag(), "[ERROR] tts mode is NOT valid");
639 int __ttsd_engine_agent_get_speed_range(int* min, int* normal, int* max)
641 if (NULL == min || NULL == normal || NULL == max) {
642 SLOG(LOG_ERROR, get_tag(), "[ERROR] Input parameter is null");
643 return TTSP_ERROR_INVALID_PARAMETER;
646 *min = TTS_SPEED_MIN;
647 *normal = TTS_SPEED_NORMAL;
648 *max = TTS_SPEED_MAX;
653 int __ttsd_engine_agent_get_pitch_range(int* min, int* normal, int* max)
655 if (NULL == min || NULL == normal || NULL == max) {
656 SLOG(LOG_ERROR, get_tag(), "[ERROR] Input parameter is null");
657 return TTSP_ERROR_INVALID_PARAMETER;
660 *min = TTS_PITCH_MIN;
661 *normal = TTS_PITCH_NORMAL;
662 *max = TTS_PITCH_MAX;
667 static bool __set_voice_info_cb(const char* language, int type, void* user_data)
669 if (NULL == language) {
670 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Input parameter is NULL in voice list callback!!!!");
674 ttsvoice_s* voice = calloc(1, sizeof(ttsvoice_s));
676 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to allocate memory");
679 voice->lang = strdup(language);
682 voice->client_ref_count = 0;
683 voice->is_loaded = false;
685 if (0 == strcmp(g_cur_engine.default_lang, language) && g_cur_engine.default_vctype == type) {
686 voice->is_default = true;
687 voice->is_loaded = true;
689 voice->is_default = false;
692 g_cur_voices = g_slist_append(g_cur_voices, voice);
697 static int __update_voice_list()
703 ret = g_cur_engine.pefuncs->foreach_voices(__set_voice_info_cb, NULL);
704 if (0 != ret || 0 >= g_slist_length(g_cur_voices)) {
705 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get voice list : result(%d)", ret);
709 #ifdef ENGINE_AGENT_DEBUG
710 ttsd_print_voicelist();
715 int ttsd_engine_agent_load_current_engine()
717 if (false == g_agent_init) {
718 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
719 return TTSD_ERROR_OPERATION_FAILED;
722 if (false == g_cur_engine.is_set) {
723 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No Current Engine ");
724 return TTSD_ERROR_OPERATION_FAILED;
727 /* check whether current engine is loaded or not */
728 if (true == g_cur_engine.is_loaded) {
729 SLOG(LOG_ERROR, get_tag(), "[Engine Agent] Engine has already been loaded ");
735 g_cur_engine.handle = dlopen(g_cur_engine.engine_path, RTLD_LAZY); /* RTLD_LAZY RTLD_NOW*/
737 if (NULL != (error = dlerror()) || NULL == g_cur_engine.handle) {
738 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to get current engine handle : %s", error);
742 g_cur_engine.ttsp_unload_engine = (int (*)())dlsym(g_cur_engine.handle, "ttsp_unload_engine");
743 if (NULL != (error = dlerror()) || NULL == g_cur_engine.ttsp_unload_engine) {
744 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to link daemon to ttsp_unload_engine() : %s", error);
748 g_cur_engine.ttsp_load_engine = (int (*)(const ttspd_funcs_s* , ttspe_funcs_s*))dlsym(g_cur_engine.handle, "ttsp_load_engine");
749 if (NULL != (error = dlerror()) || NULL == g_cur_engine.ttsp_load_engine) {
750 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to link daemon to ttsp_load_engine() : %s", error);
755 g_cur_engine.pdfuncs->version = 1;
756 g_cur_engine.pdfuncs->size = sizeof(ttspd_funcs_s);
757 g_cur_engine.pdfuncs->get_mode = __ttsd_get_mode;
758 g_cur_engine.pdfuncs->get_speed_range = __ttsd_engine_agent_get_speed_range;
759 g_cur_engine.pdfuncs->get_pitch_range = __ttsd_engine_agent_get_pitch_range;
762 ret = g_cur_engine.ttsp_load_engine(g_cur_engine.pdfuncs, g_cur_engine.pefuncs);
764 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to load engine - %s : result(%s)",
765 g_cur_engine.engine_path, __ttsd_get_engine_error_code(ret));
766 return TTSD_ERROR_OPERATION_FAILED;
769 /* engine error check */
770 if (g_cur_engine.pefuncs->size != sizeof(ttspe_funcs_s)) {
771 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] The size of engine function is not valid");
774 if (NULL == g_cur_engine.pefuncs->initialize ||
775 NULL == g_cur_engine.pefuncs->deinitialize ||
776 NULL == g_cur_engine.pefuncs->foreach_voices ||
777 NULL == g_cur_engine.pefuncs->is_valid_voice ||
778 NULL == g_cur_engine.pefuncs->start_synth ||
779 NULL == g_cur_engine.pefuncs->cancel_synth) {
780 SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] The engine functions are NOT valid");
781 return TTSD_ERROR_OPERATION_FAILED;
784 ret = g_cur_engine.pefuncs->initialize(__result_cb);
786 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to initialize current engine : %s", __ttsd_get_engine_error_code(ret));
787 return TTSD_ERROR_OPERATION_FAILED;
790 /* Get voice info of current engine */
791 ret = __update_voice_list();
793 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to set voice info : result(%d)", ret);
794 return TTSD_ERROR_OPERATION_FAILED;
797 /* Select default voice */
798 if (NULL != g_cur_engine.default_lang) {
799 if (true == g_cur_engine.pefuncs->is_valid_voice(g_cur_engine.default_lang, g_cur_engine.default_vctype)) {
800 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set origin default voice to current engine : lang(%s), type(%d)",
801 g_cur_engine.default_lang, g_cur_engine.default_vctype);
803 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] Fail set origin default voice : lang(%s), type(%d)",
804 g_cur_engine.default_lang, g_cur_engine.default_vctype);
806 return TTSD_ERROR_OPERATION_FAILED;
810 /* load default voice */
811 if (NULL != g_cur_engine.pefuncs->load_voice) {
812 ret = g_cur_engine.pefuncs->load_voice(g_cur_engine.default_lang, g_cur_engine.default_vctype);
814 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Load default voice : lang(%s), type(%d)",
815 g_cur_engine.default_lang, g_cur_engine.default_vctype);
817 SLOG(LOG_WARN, get_tag(), "[Engine Agent ERROR] Fail to load default voice : lang(%s), type(%d) result(%s)",
818 g_cur_engine.default_lang, g_cur_engine.default_vctype, __ttsd_get_engine_error_code(ret));
820 return TTSD_ERROR_OPERATION_FAILED;
824 /* set default pitch */
825 if (NULL != g_cur_engine.pefuncs->set_pitch) {
826 ret = g_cur_engine.pefuncs->set_pitch(g_cur_engine.default_pitch);
828 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to set pitch : pitch(%d), result(%s)",
829 g_cur_engine.default_pitch, __ttsd_get_engine_error_code(ret));
830 return TTSD_ERROR_OPERATION_FAILED;
832 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set default pitch : pitch(%d)", g_cur_engine.default_pitch);
836 if (false == set_voice) {
837 /* get language list */
839 GList* voice_list = NULL;
841 ret = g_cur_engine.pefuncs->foreach_voices(__supported_voice_cb, &voice_list);
843 if (0 == ret && 0 < g_list_length(voice_list)) {
845 voice_s* voice = NULL;
847 iter = g_list_first(voice_list);
850 while (NULL != iter) {
854 if (0 == strcmp("en_US", voice->language))
858 iter = g_list_next(iter);
861 SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] Fail to find voice in list");
862 return TTSD_ERROR_OPERATION_FAILED;
865 /* Set selected language and type */
866 if (true != g_cur_engine.pefuncs->is_valid_voice(voice->language, voice->type)) {
867 SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] Fail voice is NOT valid");
868 return TTSD_ERROR_OPERATION_FAILED;
871 ttsd_config_set_default_voice(voice->language, (int)voice->type);
873 g_cur_engine.default_lang = strdup(voice->language);
874 g_cur_engine.default_vctype = voice->type;
876 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Select default voice : lang(%s), type(%d)",
877 voice->language, voice->type);
879 __free_voice_list(voice_list);
881 SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] Fail to get language list : result(%d)", ret);
882 return TTSD_ERROR_OPERATION_FAILED;
886 g_cur_engine.is_loaded = true;
891 int ttsd_engine_agent_unload_current_engine()
893 if (false == g_agent_init) {
894 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
895 return TTSD_ERROR_OPERATION_FAILED;
898 if (false == g_cur_engine.is_set) {
899 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No Current Engine ");
900 return TTSD_ERROR_OPERATION_FAILED;
903 if (false == g_cur_engine.is_loaded) {
904 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Engine has already been unloaded ");
908 /* shutdown engine */
910 ret = g_cur_engine.pefuncs->deinitialize();
912 SLOG(LOG_ERROR, get_tag(), "[Engine Agent] Fail deinitialize() : %s", __ttsd_get_engine_error_code(ret));
916 g_cur_engine.ttsp_unload_engine();
918 dlclose(g_cur_engine.handle);
920 /* reset current engine data */
921 g_cur_engine.handle = NULL;
922 g_cur_engine.is_loaded = false;
925 ttsvoice_s* data = NULL;
927 iter = g_slist_nth(g_cur_voices, 0);
928 while (NULL != iter) {
932 if (NULL != data->lang) free(data->lang);
933 g_cur_voices = g_slist_remove(g_cur_voices, data);
938 iter = g_slist_nth(g_cur_voices, 0);
944 bool ttsd_engine_agent_need_network()
946 if (false == g_agent_init) {
947 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
948 return TTSD_ERROR_OPERATION_FAILED;
951 if (false == g_cur_engine.is_loaded) {
952 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
953 return TTSD_ERROR_OPERATION_FAILED;
956 return g_cur_engine.need_network;
959 bool ttsd_engine_select_valid_voice(const char* lang, int type, char** out_lang, int* out_type)
961 if (NULL == lang || NULL == out_lang || NULL == out_type) {
965 if (false == g_cur_engine.is_loaded) {
966 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] Not loaded engine");
970 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Select voice : input lang(%s), input type(%d), default lang(%s), default type(%d)",
971 lang, type, g_cur_engine.default_lang, g_cur_engine.default_vctype);
973 /* case 1 : Both are default */
974 if (0 == strncmp(lang, "default", strlen("default")) && 0 == type) {
975 *out_lang = strdup(g_cur_engine.default_lang);
976 *out_type = g_cur_engine.default_vctype;
981 GList* voice_list = NULL;
984 ret = g_cur_engine.pefuncs->foreach_voices(__supported_voice_cb, &voice_list);
985 if (0 != ret || 0 >= g_list_length(voice_list)) {
986 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get voice list : result(%d)", ret);
996 /* lang and type are not default type */
997 if (0 != strncmp(lang, "default", strlen("default")) && 0 != type) {
998 iter = g_list_first(voice_list);
1000 while (NULL != iter) {
1001 /* Get handle data from list */
1004 if (0 == strncmp(voice->language, lang, strlen(lang)) && voice->type == type) {
1005 *out_lang = strdup(voice->language);
1006 *out_type = voice->type;
1011 iter = g_list_next(iter);
1014 } else if (0 != strncmp(lang, "default", strlen("default")) && 0 == type) {
1015 /* Only type is default */
1016 if (0 == strncmp(lang, g_cur_engine.default_lang, strlen(g_cur_engine.default_lang))) {
1017 *out_lang = strdup(g_cur_engine.default_lang);
1018 *out_type = g_cur_engine.default_vctype;
1021 voice_s* voice_selected = NULL;
1022 iter = g_list_first(voice_list);
1023 while (NULL != iter) {
1024 /* Get handle data from list */
1027 if (0 == strncmp(voice->language, lang, strlen(lang))) {
1028 voice_selected = voice;
1029 if (voice->type == g_cur_engine.default_vctype) {
1030 voice_selected = voice;
1034 iter = g_list_next(iter);
1037 if (NULL != voice_selected) {
1038 *out_lang = strdup(voice_selected->language);
1039 *out_type = voice_selected->type;
1043 } else if (0 == strncmp(lang, "default", strlen("default")) && 0 != type) {
1044 /* Only lang is default */
1045 if (type == g_cur_engine.default_vctype) {
1046 *out_lang = strdup(g_cur_engine.default_lang);
1047 *out_type = g_cur_engine.default_vctype;
1050 voice_s* voice_selected = NULL;
1051 iter = g_list_first(voice_list);
1052 while (NULL != iter) {
1053 /* Get handle data from list */
1056 if (0 == strncmp(voice->language, g_cur_engine.default_lang, strlen(g_cur_engine.default_lang))) {
1057 voice_selected = voice;
1058 if (voice->type == type) {
1059 voice_selected = voice;
1063 iter = g_list_next(iter);
1066 if (NULL != voice_selected) {
1067 *out_lang = strdup(voice->language);
1068 *out_type = voice_selected->type;
1074 if (true == result) {
1075 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Selected voice : lang(%s), type(%d)", *out_lang, *out_type);
1078 __free_voice_list(voice_list);
1083 bool ttsd_engine_agent_is_same_engine(const char* engine_id)
1085 if (false == g_agent_init) {
1086 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1090 if (false == g_cur_engine.is_loaded) {
1091 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1095 /* compare current engine and engine id.*/
1096 if (0 == strncmp(g_cur_engine.engine_uuid, engine_id, strlen(engine_id))) {
1103 int ttsd_engine_agent_set_default_engine(const char* engine_id)
1105 if (false == g_agent_init) {
1106 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1107 return TTSD_ERROR_OPERATION_FAILED;
1110 /* compare current engine and new engine.*/
1111 if (0 == strncmp(g_cur_engine.engine_uuid, engine_id, strlen(engine_id))) {
1112 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] new engine(%s) is the same as current engine", engine_id);
1116 bool is_engine_loaded = false;
1117 char* tmp_uuid = NULL;
1118 tmp_uuid = strdup(g_cur_engine.engine_uuid);
1119 if (NULL == tmp_uuid) {
1120 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Current engine id is NULL");
1121 return TTSD_ERROR_OPERATION_FAILED;
1124 is_engine_loaded = g_cur_engine.is_loaded;
1126 if (true == is_engine_loaded) {
1128 if (0 != ttsd_engine_agent_unload_current_engine()) {
1129 SLOG(LOG_ERROR, get_tag(), "[Engine Agent Error] fail to unload current engine");
1133 /* change current engine */
1134 if (0 != __internal_set_current_engine(engine_id)) {
1135 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to set current engine. Recovery origin engine");
1137 /* roll back to old current engine. */
1138 __internal_set_current_engine(tmp_uuid);
1139 if (true == is_engine_loaded) {
1140 ttsd_engine_agent_load_current_engine();
1143 if (tmp_uuid != NULL) free(tmp_uuid);
1144 return TTSD_ERROR_OPERATION_FAILED;
1147 if (true == is_engine_loaded) {
1149 if (0 != ttsd_engine_agent_load_current_engine()) {
1150 SLOG(LOG_ERROR, get_tag(), "[Engine Agent Error] Fail to load new engine. Recovery origin engine");
1152 /* roll back to old current engine. */
1153 __internal_set_current_engine(tmp_uuid);
1154 if (true == is_engine_loaded) {
1155 ttsd_engine_agent_load_current_engine();
1158 if (tmp_uuid != NULL) free(tmp_uuid);
1159 return TTSD_ERROR_OPERATION_FAILED;
1161 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] load new engine : %s", engine_id);
1165 if (tmp_uuid != NULL) free(tmp_uuid);
1169 int ttsd_engine_agent_set_default_voice(const char* language, int vctype)
1171 if (false == g_agent_init) {
1172 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1173 return TTSD_ERROR_OPERATION_FAILED;
1176 if (false == g_cur_engine.is_loaded) {
1177 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1178 return TTSD_ERROR_OPERATION_FAILED;
1181 if (false == g_cur_engine.pefuncs->is_valid_voice(language, vctype)) {
1182 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Voice is NOT valid !!");
1183 return TTSD_ERROR_INVALID_VOICE;
1187 GSList *iter = NULL;
1188 ttsvoice_s* data = NULL;
1190 /* Update new default voice info */
1191 iter = g_slist_nth(g_cur_voices, 0);
1192 while (NULL != iter) {
1193 /* Get handle data from list */
1197 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Voice data is NULL");
1198 return TTSD_ERROR_OPERATION_FAILED;
1201 if (0 == strcmp(data->lang, language) && data->type == vctype) {
1202 data->is_default = true;
1203 if (0 == data->client_ref_count) {
1205 if (NULL != g_cur_engine.pefuncs->load_voice) {
1206 ret = g_cur_engine.pefuncs->load_voice(data->lang, data->type);
1208 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] load voice : lang(%s), type(%d)",
1209 data->lang, data->type);
1210 data->is_loaded = true;
1212 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to load voice : lang(%s), type(%d), result(%s)",
1213 data->lang, data->type, __ttsd_get_engine_error_code(ret));
1216 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent ERROR] Load voice of engine function is NULL");
1223 iter = g_slist_next(iter);
1226 #ifdef ENGINE_AGENT_DEBUG
1227 ttsd_print_voicelist();
1230 /* Update old default voice info */
1231 iter = g_slist_nth(g_cur_voices, 0);
1232 while (NULL != iter) {
1233 /*Get handle data from list*/
1237 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Voice data is NULL");
1238 return TTSD_ERROR_OPERATION_FAILED;
1241 if (0 == strcmp(data->lang, g_cur_engine.default_lang) && data->type == g_cur_engine.default_vctype) {
1242 data->is_default = false;
1243 if (0 == data->client_ref_count) {
1245 if (NULL != g_cur_engine.pefuncs->unload_voice) {
1246 ret = g_cur_engine.pefuncs->unload_voice(data->lang, data->type);
1248 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Unload voice : lang(%s), type(%d)",
1249 data->lang, data->type);
1250 data->is_loaded = false;
1252 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to unload voice : lang(%s), type(%d), result(%s)",
1253 data->lang, data->type, __ttsd_get_engine_error_code(ret));
1256 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent ERROR] Unload voice of engine function is NULL");
1263 iter = g_slist_next(iter);
1266 if (NULL != g_cur_engine.default_lang) free(g_cur_engine.default_lang);
1268 g_cur_engine.default_lang = strdup(language);
1269 g_cur_engine.default_vctype = vctype;
1271 #ifdef ENGINE_AGENT_DEBUG
1272 ttsd_print_voicelist();
1275 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set default voice : lang(%s), type(%d)",
1276 g_cur_engine.default_lang, g_cur_engine.default_vctype);
1281 int ttsd_engine_agent_set_default_speed(int speed)
1283 if (false == g_agent_init) {
1284 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1285 return TTSD_ERROR_OPERATION_FAILED;
1288 g_cur_engine.default_speed = speed;
1293 int ttsd_engine_agent_set_default_pitch(int pitch)
1295 if (false == g_agent_init) {
1296 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1297 return TTSD_ERROR_OPERATION_FAILED;
1300 if (false == g_cur_engine.is_loaded) {
1301 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1302 return TTSD_ERROR_OPERATION_FAILED;
1305 if (NULL == g_cur_engine.pefuncs->set_pitch) {
1306 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not support pitch");
1307 return TTSD_ERROR_OPERATION_FAILED;
1310 int ret = g_cur_engine.pefuncs->set_pitch(pitch);
1312 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to set pitch : pitch(%d), result(%s)",
1313 pitch, __ttsd_get_engine_error_code(ret));
1314 return TTSD_ERROR_OPERATION_FAILED;
1317 g_cur_engine.default_pitch = pitch;
1322 /******************************************************************************************
1323 * TTS Engine Interfaces for client
1324 *******************************************************************************************/
1326 int ttsd_engine_load_voice(const char* lang, const int vctype)
1328 /* 1. Find voice info */
1330 GSList *iter = NULL;
1331 ttsvoice_s* data = NULL;
1333 iter = g_slist_nth(g_cur_voices, 0);
1334 while (NULL != iter) {
1335 /*Get handle data from list*/
1339 if (0 == strcmp(data->lang, lang) && data->type == vctype) {
1340 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Find voice : default(%d) loaded(%d) ref(%d) lang(%s) type(%d)",
1341 data->is_default, data->is_loaded, data->client_ref_count, data->lang, data->type);
1347 iter = g_slist_next(iter);
1352 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] This voice is not supported voice : lang(%s) type(%d)", lang, vctype);
1353 return TTSD_ERROR_OPERATION_FAILED;
1356 /* 2. increse ref count */
1357 data->client_ref_count++;
1359 /* 3. if ref count change 0 to 1 and not default, load voice */
1360 if (1 == data->client_ref_count && false == data->is_default) {
1362 if (NULL != g_cur_engine.pefuncs->load_voice) {
1363 ret = g_cur_engine.pefuncs->load_voice(data->lang, data->type);
1365 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Load voice : lang(%s), type(%d)",
1366 data->lang, data->type);
1367 data->is_loaded = true;
1369 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to load voice : lang(%s), type(%d), result(%s)",
1370 data->lang, data->type, __ttsd_get_engine_error_code(ret));
1372 return TTSD_ERROR_OPERATION_FAILED;
1375 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent ERROR] Load voice of engine function is NULL");
1378 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Not load voice : default voice(%d) or ref count(%d)",
1379 data->is_default, data->client_ref_count);
1382 #ifdef ENGINE_AGENT_DEBUG
1383 ttsd_print_voicelist();
1389 int ttsd_engine_unload_voice(const char* lang, const int vctype)
1391 /* 1. Find voice info */
1393 GSList *iter = NULL;
1394 ttsvoice_s* data = NULL;
1396 iter = g_slist_nth(g_cur_voices, 0);
1397 while (NULL != iter) {
1398 /*Get handle data from list*/
1402 if (0 == strcmp(data->lang, lang) && data->type == vctype) {
1403 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Find voice : default(%d) loaded(%d) ref(%d) lang(%s) type(%d)",
1404 data->is_default, data->is_loaded, data->client_ref_count, data->lang, data->type);
1410 iter = g_slist_next(iter);
1415 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] This voice is not supported voice : lang(%s) type(%d)", lang, vctype);
1416 return TTSD_ERROR_OPERATION_FAILED;
1419 /* 2. Decrese ref count */
1420 data->client_ref_count--;
1422 /* 3. if ref count change 0 and not default, load voice */
1423 if (0 == data->client_ref_count && false == data->is_default) {
1425 if (NULL != g_cur_engine.pefuncs->unload_voice) {
1426 ret = g_cur_engine.pefuncs->unload_voice(data->lang, data->type);
1428 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Unload voice : lang(%s), type(%d)",
1429 data->lang, data->type);
1430 data->is_loaded = false;
1432 SECURE_SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to unload voice : lang(%s), type(%d), result(%s)",
1433 data->lang, data->type, __ttsd_get_engine_error_code(ret));
1435 return TTSD_ERROR_OPERATION_FAILED;
1438 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent ERROR] Unload voice of engine function is NULL");
1441 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Not unload voice : default voice(%d) or ref count(%d)",
1442 data->is_default, data->client_ref_count);
1445 #ifdef ENGINE_AGENT_DEBUG
1446 ttsd_print_voicelist();
1451 int ttsd_engine_start_synthesis(const char* lang, int vctype, const char* text, int speed, void* user_param)
1453 if (false == g_agent_init) {
1454 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1455 return TTSD_ERROR_OPERATION_FAILED;
1458 if (false == g_cur_engine.is_loaded) {
1459 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1460 return TTSD_ERROR_OPERATION_FAILED;
1463 /* select voice for default */
1464 char* temp_lang = NULL;
1466 if (true != ttsd_engine_select_valid_voice(lang, vctype, &temp_lang, &temp_type)) {
1467 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to select default voice");
1468 if (NULL != temp_lang) free(temp_lang);
1469 return TTSD_ERROR_INVALID_VOICE;
1471 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Start synthesis : language(%s), type(%d), speed(%d), text(%s)",
1472 temp_lang, temp_type, speed, text);
1475 if (NULL == g_cur_engine.pefuncs->start_synth) {
1476 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] start_synth() of engine is NULL!!");
1477 if (NULL != temp_lang) free(temp_lang);
1478 return TTSD_ERROR_OPERATION_FAILED;
1484 temp_speed = g_cur_engine.default_speed;
1489 /* synthesize text */
1491 ret = g_cur_engine.pefuncs->start_synth(temp_lang, temp_type, text, temp_speed, user_param);
1493 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] ***************************************");
1494 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] * synthesize error : %s *", __ttsd_get_engine_error_code(ret));
1495 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] ***************************************");
1496 if (NULL != temp_lang) free(temp_lang);
1497 return TTSD_ERROR_OPERATION_FAILED;
1500 if (NULL != temp_lang) free(temp_lang);
1504 int ttsd_engine_cancel_synthesis()
1506 if (false == g_agent_init) {
1507 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1508 return TTSD_ERROR_OPERATION_FAILED;
1511 if (false == g_cur_engine.is_loaded) {
1512 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1513 return TTSD_ERROR_OPERATION_FAILED;
1516 if (NULL == g_cur_engine.pefuncs->cancel_synth) {
1517 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] cancel_synth() of engine is NULL!!");
1518 return TTSD_ERROR_OPERATION_FAILED;
1521 /* stop synthesis */
1523 ret = g_cur_engine.pefuncs->cancel_synth();
1525 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail cancel synthesis : %s", __ttsd_get_engine_error_code(ret));
1526 return TTSD_ERROR_OPERATION_FAILED;
1532 bool __supported_voice_cb(const char* language, int type, void* user_data)
1534 GList** voice_list = (GList**)user_data;
1536 if (NULL == language || NULL == voice_list) {
1537 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Input parameter is NULL in voice list callback!!!!");
1541 voice_s* voice = calloc(1, sizeof(voice_s));
1542 if (NULL == voice) {
1543 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to allocate memory");
1546 voice->language = strdup(language);
1549 *voice_list = g_list_append(*voice_list, voice);
1554 int ttsd_engine_get_voice_list(GList** voice_list)
1556 if (false == g_agent_init) {
1557 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1558 return TTSD_ERROR_OPERATION_FAILED;
1561 if (false == g_cur_engine.is_loaded) {
1562 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1563 return TTSD_ERROR_OPERATION_FAILED;
1567 ret = g_cur_engine.pefuncs->foreach_voices(__supported_voice_cb, (void*)voice_list);
1569 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to get voice list : %s", __ttsd_get_engine_error_code(ret));
1570 return TTSD_ERROR_OPERATION_FAILED;
1576 int ttsd_engine_get_default_voice(char** lang, int* vctype)
1578 if (false == g_agent_init) {
1579 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
1580 return TTSD_ERROR_OPERATION_FAILED;
1583 if (false == g_cur_engine.is_loaded) {
1584 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1585 return TTSD_ERROR_OPERATION_FAILED;
1588 if (NULL == lang || NULL == vctype) {
1589 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] BAD Parameter");
1590 return TTSD_ERROR_INVALID_PARAMETER;
1593 if (NULL != g_cur_engine.default_lang) {
1594 *lang = strdup(g_cur_engine.default_lang);
1595 *vctype = g_cur_engine.default_vctype;
1597 SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine] Get default voice : language(%s), type(%d)", *lang, *vctype);
1599 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Default voice is NULL");
1600 return TTSD_ERROR_OPERATION_FAILED;
1606 void __free_voice_list(GList* voice_list)
1609 voice_s* data = NULL;
1611 /* if list have item */
1612 if (g_list_length(voice_list) > 0) {
1613 /* Get a first item */
1614 iter = g_list_first(voice_list);
1616 while (NULL != iter) {
1620 if (NULL != data->language) free(data->language);
1624 voice_list = g_list_remove_link(voice_list, iter);
1626 iter = g_list_first(voice_list);
1632 * TTS Engine Callback Functions ` *
1634 bool __result_cb(ttsp_result_event_e event, const void* data, unsigned int data_size, ttsp_audio_type_e audio_type, int rate, void *user_data)
1636 g_result_cb(event, data, data_size, audio_type, rate, user_data);
1640 /* function for debugging */
1641 int ttsd_print_enginelist()
1644 ttsengine_info_s *data = NULL;
1646 if (g_list_length(g_engine_list) > 0) {
1647 iter = g_list_first(g_engine_list);
1649 SLOG(LOG_DEBUG, get_tag(), "----- engine list -----");
1652 while (NULL != iter) {
1655 SECURE_SLOG(LOG_DEBUG, get_tag(), "[%dth]", i);
1656 SECURE_SLOG(LOG_DEBUG, get_tag(), "engine uuid : %s", data->engine_uuid);
1657 SECURE_SLOG(LOG_DEBUG, get_tag(), "engine name : %s", data->engine_name);
1658 SECURE_SLOG(LOG_DEBUG, get_tag(), "engine path : %s", data->engine_path);
1659 SECURE_SLOG(LOG_DEBUG, get_tag(), "setting ug path : %s", data->setting_ug_path);
1661 iter = g_list_next(iter);
1664 SLOG(LOG_DEBUG, get_tag(), "-----------------------");
1665 SLOG(LOG_DEBUG, get_tag(), " ");
1667 SLOG(LOG_DEBUG, get_tag(), "----- engine list -----");
1668 SLOG(LOG_DEBUG, get_tag(), "No Engine in directory");
1669 SLOG(LOG_DEBUG, get_tag(), "-----------------------");
1675 int ttsd_print_voicelist()
1678 GSList *iter = NULL;
1679 ttsvoice_s* data = NULL;
1681 SLOG(LOG_DEBUG, get_tag(), "=== Voice list ===");
1683 if (g_slist_length(g_cur_voices) > 0) {
1684 /* Get a first item */
1685 iter = g_slist_nth(g_cur_voices, 0);
1688 while (NULL != iter) {
1689 /*Get handle data from list*/
1692 if (NULL == data || NULL == data->lang) {
1693 SLOG(LOG_ERROR, get_tag(), "[ERROR] Data is invalid");
1697 SLOG(LOG_DEBUG, get_tag(), "[%dth] default(%d) loaded(%d) ref(%d) lang(%s) type(%d)",
1698 i, data->is_default, data->is_loaded, data->client_ref_count, data->lang, data->type);
1701 iter = g_slist_next(iter);
1706 SLOG(LOG_DEBUG, get_tag(), "==================");