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.
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 fseek(fp , 0 , SEEK_END);
46 int text_len = ftell(fp);
48 SLOG(LOG_ERROR, tts_tag(), "File has no contents\n");
52 SLOG(LOG_ERROR, tts_tag(), "text_len(%d)\n", text_len);
55 *text = (char*)calloc(1, text_len+1);
58 SLOG(LOG_ERROR, tts_tag(), "Fail to memory allocation\n");
64 result_len = fread(*text, sizeof(char), text_len, fp);
65 if (result_len != text_len) {
66 SLOG(LOG_ERROR, tts_tag(), "Fail to read\n");
71 *text[result_len] = '\0';
77 Eina_Bool __tts_test_play(void *data)
85 ret = tts_add_text(g_tts, g_text, lang, TTS_VOICE_TYPE_AUTO, TTS_SPEED_AUTO, &utt_id);
86 if (TTS_ERROR_NONE != ret) {
87 SLOG(LOG_ERROR, tts_tag(), "Fail to add text\n");
88 ecore_timer_add(0, __tts_test_destroy, NULL);
92 SLOG(LOG_ERROR, tts_tag(), "Play : utt id(%d)\n", utt_id);
93 ret = tts_play(g_tts);
94 if (TTS_ERROR_NONE != ret) {
95 SLOG(LOG_ERROR, tts_tag(), "Fail to play\n");
96 ecore_timer_add(0, __tts_test_destroy, NULL);
103 Eina_Bool __tts_test_destroy(void *data)
106 SLOG(LOG_ERROR, tts_tag(), "Stop\n");
107 ret = tts_stop(g_tts);
108 if (TTS_ERROR_NONE != ret) {
109 SLOG(LOG_ERROR, tts_tag(), "Fail to stop\n");
112 SLOG(LOG_ERROR, tts_tag(), "Unprepare (Disconnection)\n");
113 ret = tts_unprepare(g_tts);
114 if (TTS_ERROR_NONE != ret) {
115 SLOG(LOG_ERROR, tts_tag(), "Fail to unprepare\n");
118 SLOG(LOG_ERROR, tts_tag(), "Destory tts client\n");
119 ret = tts_destroy(g_tts);
120 if (TTS_ERROR_NONE != ret) {
121 SLOG(LOG_ERROR, tts_tag(), "Fail to destroy\n");
124 ecore_main_loop_quit();
129 static void __tts_test_state_changed_cb(tts_h tts, tts_state_e previous, tts_state_e current, void* user_data)
131 if (TTS_STATE_CREATED == previous && TTS_STATE_READY == current) {
132 SLOG(LOG_ERROR, tts_tag(), "State is ready after prepare\n");
133 ecore_timer_add(0, __tts_test_play, user_data);
139 static void __tts_test_utt_started_cb(tts_h tts, int utt_id, void* user_data)
141 SLOG(LOG_DEBUG, tts_tag(), "Utterance started : utt id(%d) \n", utt_id);
146 static void __tts_test_utt_completed_cb(tts_h tts, int utt_id, void* user_data)
148 SLOG(LOG_DEBUG, tts_tag(), "Utterance completed : utt id(%d) \n", utt_id);
149 ecore_timer_add(0, __tts_test_destroy, NULL);
154 int main (int argc, char *argv[])
156 if (1 == argc || 5 < argc) {
157 SLOG(LOG_DEBUG, tts_tag(), "Please check parameter\n");
158 SLOG(LOG_DEBUG, tts_tag(), "Ex> tts-test 'text'\n");
159 SLOG(LOG_DEBUG, tts_tag(), "Specific mode> tts-test 'text' '-sr || -noti'\n");
164 char* src_path = NULL;
168 while(NULL != argv[n]) {
170 if(!strcmp("-h", argv[n])) {
171 SLOG(LOG_DEBUG, tts_tag(), "\n");
172 SLOG(LOG_DEBUG, tts_tag(), " ==========================================\n");
173 SLOG(LOG_DEBUG, tts_tag(), " TTS test usage\n");
174 SLOG(LOG_DEBUG, tts_tag(), " ==========================================\n\n");
175 SLOG(LOG_DEBUG, tts_tag(), " -t : Synthesize text \n");
176 SLOG(LOG_DEBUG, tts_tag(), " -l : Determine langage to synthesize text, ex) en_US, ko_KR ...\n");
177 SLOG(LOG_DEBUG, tts_tag(), " -f : Determine file path which include text\n\n");
178 SLOG(LOG_DEBUG, tts_tag(), " ***************************************************\n");
179 SLOG(LOG_DEBUG, tts_tag(), " Example : #tts-test -l en_US -t \"1 2 3 4\" \n");
180 SLOG(LOG_DEBUG, tts_tag(), " ***************************************************\n");
181 SLOG(LOG_DEBUG, tts_tag(), "\n");
185 // check langage option
186 if(!strcmp("-l", argv[n])) {
187 lang = TTS_STRDUP(argv[n+1]);
188 SLOG(LOG_ERROR, tts_tag(), "Language : %s\n", lang);
190 // check text to synthesize
191 else if (!strcmp("-t", argv[n])) {
192 g_text = TTS_STRDUP(argv[n+1]);
193 SLOG(LOG_ERROR, tts_tag(), "Text : %s\n", g_text);
195 // check file path to synthesize
196 else if (!strcmp("-f", argv[n])) {
197 src_path = TTS_STRDUP(argv[n+1]);
198 SLOG(LOG_ERROR, tts_tag(), "File path : %s\n", src_path);
199 if(!__tts_test_get_text_from_file(src_path, &g_text)) {
206 if(!g_text && !src_path) {
207 SLOG(LOG_ERROR, tts_tag(), "Invalid parameter, check help with command tts-test -h");
211 //===================================
213 tts_mode_e mode = TTS_MODE_DEFAULT;
215 SLOG(LOG_DEBUG, tts_tag(), " ");
216 SLOG(LOG_DEBUG, tts_tag(), " ");
217 SLOG(LOG_DEBUG, tts_tag(), "===== TTS Sample start =====\n");
219 SLOG(LOG_DEBUG, tts_tag(), "Input text : %s\n", g_text ? g_text : "NULL");
220 SLOG(LOG_DEBUG, tts_tag(), "Input lang : %s\n", lang ? lang : "NULL");
221 SLOG(LOG_DEBUG, tts_tag(), "Input file path : %s\n", src_path ? src_path : "NULL");
224 SLOG(LOG_ERROR, tts_tag(), "[Main ERROR] Fail ecore_init()\n");
230 SLOG(LOG_DEBUG, tts_tag(), "Create tts client\n");
231 ret = tts_create(&g_tts);
232 if (TTS_ERROR_NONE != ret) {
233 SLOG(LOG_ERROR, tts_tag(), "Fail to create\n");
237 SLOG(LOG_DEBUG, tts_tag(), "Set tts mode - %d\n", mode);
238 ret = tts_set_mode(g_tts, mode);
239 if (TTS_ERROR_NONE != ret) {
240 SLOG(LOG_ERROR, tts_tag(), "Fail to set mode\n");
245 SLOG(LOG_DEBUG, tts_tag(), "Set Callback func\n");
246 ret = tts_set_state_changed_cb(g_tts, __tts_test_state_changed_cb, (void*)lang);
247 if (TTS_ERROR_NONE != ret) {
248 SLOG(LOG_ERROR, tts_tag(), "Fail to set state changed cb\n");
253 ret = tts_set_utterance_started_cb(g_tts, __tts_test_utt_started_cb, NULL);
254 if (TTS_ERROR_NONE != ret) {
255 SLOG(LOG_ERROR, tts_tag(), "Fail to set utt started cb\n");
260 ret = tts_set_utterance_completed_cb(g_tts, __tts_test_utt_completed_cb, NULL);
261 if (TTS_ERROR_NONE != ret) {
262 SLOG(LOG_ERROR, tts_tag(), "Fail to set utt completed cb\n");
267 SLOG(LOG_DEBUG, tts_tag(), "Prepare (Daemon Connection) asynchronously : Wait for ready state \n");
268 ret = tts_prepare(g_tts);
269 if (TTS_ERROR_NONE != ret) {
270 SLOG(LOG_ERROR, tts_tag(), "Fail to prepare\n");
275 ecore_main_loop_begin();
279 if(src_path) free(src_path);
281 if(g_text) free(g_text);
283 SLOG(LOG_DEBUG, tts_tag(), "===== TTS END =====\n\n\n");