2 * Copyright (c) 2011-2016 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.
20 #define TTS_STRDUP(src) ((src != NULL) ? strdup(src) : NULL)
23 static char* g_text = NULL;
30 Eina_Bool __tts_test_destroy(void *data);
32 static bool __tts_test_get_text_from_file(const char* path, char** text)
39 if ((fp = fopen(path, "rb")) == NULL) {
40 SLOG(LOG_ERROR, tts_tag(), "Fail to open file (%s)", path);
44 if (0 != fseek(fp , 0 , SEEK_END)) {
45 SLOG(LOG_ERROR, tts_tag(), "Fail to fseek()");
50 int text_len = ftell(fp);
51 if (0 >= text_len || 0 > text_len + 1) {
52 SLOG(LOG_ERROR, tts_tag(), "File has no contents");
56 SLOG(LOG_ERROR, tts_tag(), "text_len(%d)", text_len);
60 temp = (char*)calloc(1, text_len + 1);
63 SLOG(LOG_ERROR, tts_tag(), "Fail to memory allocation");
70 result_len = fread(temp, sizeof(char), text_len, fp);
71 if (result_len != text_len) {
72 SLOG(LOG_ERROR, tts_tag(), "Fail to read : result(%d) text_len(%d)", result_len, text_len);
82 temp[result_len] = '\0';
90 Eina_Bool __tts_test_resume(void *data)
92 int ret = tts_play(g_tts);
93 if (TTS_ERROR_NONE != ret) {
94 SLOG(LOG_ERROR, tts_tag(), "Fail to resume");
95 ecore_timer_add(0, __tts_test_destroy, NULL);
102 Eina_Bool __tts_test_pause(void *data)
104 int ret = tts_pause(g_tts);
105 if (TTS_ERROR_NONE != ret) {
106 SLOG(LOG_ERROR, tts_tag(), "Fail to pause");
107 ecore_timer_add(0, __tts_test_destroy, NULL);
111 ecore_timer_add(3, __tts_test_resume, data);
116 Eina_Bool __tts_test_play(void *data)
124 ret = tts_add_text(g_tts, g_text, lang, TTS_VOICE_TYPE_AUTO, TTS_SPEED_AUTO, &utt_id);
125 if (TTS_ERROR_NONE != ret) {
126 SLOG(LOG_ERROR, tts_tag(), "Fail to add text");
127 ecore_timer_add(0, __tts_test_destroy, NULL);
131 SLOG(LOG_ERROR, tts_tag(), "Play : utt id(%d)", utt_id);
132 ret = tts_play(g_tts);
133 if (TTS_ERROR_NONE != ret) {
134 SLOG(LOG_ERROR, tts_tag(), "Fail to play");
135 ecore_timer_add(0, __tts_test_destroy, NULL);
139 // ecore_timer_add(1, __tts_test_pause, data);
144 Eina_Bool __tts_test_destroy(void *data)
147 SLOG(LOG_ERROR, tts_tag(), "Stop");
148 ret = tts_stop(g_tts);
149 if (TTS_ERROR_NONE != ret) {
150 SLOG(LOG_ERROR, tts_tag(), "Fail to stop");
153 SLOG(LOG_ERROR, tts_tag(), "Unprepare (Disconnection)");
154 ret = tts_unprepare(g_tts);
155 if (TTS_ERROR_NONE != ret) {
156 SLOG(LOG_ERROR, tts_tag(), "Fail to unprepare");
159 SLOG(LOG_ERROR, tts_tag(), "Destroy tts client");
160 ret = tts_destroy(g_tts);
161 if (TTS_ERROR_NONE != ret) {
162 SLOG(LOG_ERROR, tts_tag(), "Fail to destroy");
165 ecore_main_loop_quit();
170 static void __tts_test_state_changed_cb(tts_h tts, tts_state_e previous, tts_state_e current, void* user_data)
172 if (TTS_STATE_CREATED == previous && TTS_STATE_READY == current) {
173 SLOG(LOG_ERROR, tts_tag(), "State is ready after prepare");
174 ecore_timer_add(0, __tts_test_play, user_data);
180 static void __tts_test_utt_started_cb(tts_h tts, int utt_id, void* user_data)
182 SLOG(LOG_DEBUG, tts_tag(), "Utterance started : utt id(%d)", utt_id);
187 static void __tts_test_utt_completed_cb(tts_h tts, int utt_id, void* user_data)
189 SLOG(LOG_DEBUG, tts_tag(), "Utterance completed : utt id(%d)", utt_id);
190 ecore_timer_add(0, __tts_test_destroy, NULL);
195 int main(int argc, char *argv[])
197 if (1 == argc || 5 < argc) {
198 SLOG(LOG_DEBUG, tts_tag(), "Please check parameter");
199 SLOG(LOG_DEBUG, tts_tag(), "Ex> tts-test -t 'text'");
204 char* src_path = NULL;
208 while (NULL != argv[n]) {
210 if (!strcmp("-h", argv[n])) {
211 SLOG(LOG_DEBUG, tts_tag(), " ==========================================");
212 SLOG(LOG_DEBUG, tts_tag(), " TTS test usage");
213 SLOG(LOG_DEBUG, tts_tag(), " ==========================================");
214 SLOG(LOG_DEBUG, tts_tag(), " -t : Synthesize text");
215 SLOG(LOG_DEBUG, tts_tag(), " -l : Determine language to synthesize text, ex) en_US, ko_KR ...");
216 SLOG(LOG_DEBUG, tts_tag(), " -f : Determine file path which include text");
217 SLOG(LOG_DEBUG, tts_tag(), " ***************************************************");
218 SLOG(LOG_DEBUG, tts_tag(), " Example : #tts-test -l en_US -t \"1 2 3 4\" ");
219 SLOG(LOG_DEBUG, tts_tag(), " ***************************************************");
223 /* check language option */
224 if (!strcmp("-l", argv[n])) {
225 lang = TTS_STRDUP(argv[n+1]);
226 SLOG(LOG_ERROR, tts_tag(), "Language : %s", lang);
228 /* check text to synthesize */
229 else if (!strcmp("-t", argv[n])) {
230 g_text = TTS_STRDUP(argv[n+1]);
231 SLOG(LOG_ERROR, tts_tag(), "Text : %s", g_text);
233 /* check file path to synthesize */
234 else if (!strcmp("-f", argv[n])) {
235 src_path = TTS_STRDUP(argv[n+1]);
236 SLOG(LOG_ERROR, tts_tag(), "File path : %s", src_path);
237 if (!__tts_test_get_text_from_file(src_path, &g_text)) {
244 if (!g_text && !src_path) {
245 SLOG(LOG_ERROR, tts_tag(), "Invalid parameter, check help with command tts-test -h");
249 /*=================================== */
251 tts_mode_e mode = TTS_MODE_DEFAULT;
253 SLOG(LOG_DEBUG, tts_tag(), " ");
254 SLOG(LOG_DEBUG, tts_tag(), " ");
255 SLOG(LOG_DEBUG, tts_tag(), "===== TTS Sample start =====");
257 SLOG(LOG_DEBUG, tts_tag(), "Input text : %s", g_text ? g_text : "NULL");
258 SLOG(LOG_DEBUG, tts_tag(), "Input lang : %s", lang ? lang : "NULL");
259 SLOG(LOG_DEBUG, tts_tag(), "Input file path : %s", src_path ? src_path : "NULL");
262 SLOG(LOG_ERROR, tts_tag(), "[Main ERROR] Fail ecore_init()");
268 SLOG(LOG_DEBUG, tts_tag(), "Create tts client");
269 ret = tts_create(&g_tts);
270 if (TTS_ERROR_NONE != ret) {
271 SLOG(LOG_ERROR, tts_tag(), "Fail to create");
275 SLOG(LOG_DEBUG, tts_tag(), "Set tts mode - %d", mode);
276 ret = tts_set_mode(g_tts, mode);
277 if (TTS_ERROR_NONE != ret) {
278 SLOG(LOG_ERROR, tts_tag(), "Fail to set mode");
283 SLOG(LOG_DEBUG, tts_tag(), "Set Callback func");
284 ret = tts_set_state_changed_cb(g_tts, __tts_test_state_changed_cb, (void*)lang);
285 if (TTS_ERROR_NONE != ret) {
286 SLOG(LOG_ERROR, tts_tag(), "Fail to set state changed cb");
291 ret = tts_set_utterance_started_cb(g_tts, __tts_test_utt_started_cb, NULL);
292 if (TTS_ERROR_NONE != ret) {
293 SLOG(LOG_ERROR, tts_tag(), "Fail to set utt started cb");
298 ret = tts_set_utterance_completed_cb(g_tts, __tts_test_utt_completed_cb, NULL);
299 if (TTS_ERROR_NONE != ret) {
300 SLOG(LOG_ERROR, tts_tag(), "Fail to set utt completed cb");
305 SLOG(LOG_DEBUG, tts_tag(), "Prepare (Daemon Connection) asynchronously : Wait for ready state");
306 ret = tts_prepare(g_tts);
307 if (TTS_ERROR_NONE != ret) {
308 SLOG(LOG_ERROR, tts_tag(), "Fail to prepare");
313 ecore_main_loop_begin();
317 if (src_path) free(src_path);
318 if (lang) free(lang);
319 if (g_text) free(g_text);
321 SLOG(LOG_DEBUG, tts_tag(), "===== TTS END =====");