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");
52 SLOG(LOG_ERROR, tts_tag(), "text_len(%d)", text_len);
56 temp = (char*)calloc(1, text_len+1);
59 SLOG(LOG_ERROR, tts_tag(), "Fail to memory allocation");
66 result_len = fread(temp, sizeof(char), text_len, fp);
67 if (result_len != text_len) {
68 SLOG(LOG_ERROR, tts_tag(), "Fail to read : result(%d) text_len(%d)", result_len, text_len);
78 temp[result_len] = '\0';
86 Eina_Bool __tts_test_resume(void *data)
88 int ret = tts_play(g_tts);
89 if (TTS_ERROR_NONE != ret) {
90 SLOG(LOG_ERROR, tts_tag(), "Fail to resume");
91 ecore_timer_add(0, __tts_test_destroy, NULL);
98 Eina_Bool __tts_test_pause(void *data)
100 int ret = tts_pause(g_tts);
101 if (TTS_ERROR_NONE != ret) {
102 SLOG(LOG_ERROR, tts_tag(), "Fail to pause");
103 ecore_timer_add(0, __tts_test_destroy, NULL);
107 ecore_timer_add(3, __tts_test_resume, data);
112 Eina_Bool __tts_test_play(void *data)
120 ret = tts_add_text(g_tts, g_text, lang, TTS_VOICE_TYPE_AUTO, TTS_SPEED_AUTO, &utt_id);
121 if (TTS_ERROR_NONE != ret) {
122 SLOG(LOG_ERROR, tts_tag(), "Fail to add text");
123 ecore_timer_add(0, __tts_test_destroy, NULL);
127 SLOG(LOG_ERROR, tts_tag(), "Play : utt id(%d)", utt_id);
128 ret = tts_play(g_tts);
129 if (TTS_ERROR_NONE != ret) {
130 SLOG(LOG_ERROR, tts_tag(), "Fail to play");
131 ecore_timer_add(0, __tts_test_destroy, NULL);
135 // ecore_timer_add(1, __tts_test_pause, data);
140 Eina_Bool __tts_test_destroy(void *data)
143 SLOG(LOG_ERROR, tts_tag(), "Stop");
144 ret = tts_stop(g_tts);
145 if (TTS_ERROR_NONE != ret) {
146 SLOG(LOG_ERROR, tts_tag(), "Fail to stop");
149 SLOG(LOG_ERROR, tts_tag(), "Unprepare (Disconnection)");
150 ret = tts_unprepare(g_tts);
151 if (TTS_ERROR_NONE != ret) {
152 SLOG(LOG_ERROR, tts_tag(), "Fail to unprepare");
155 SLOG(LOG_ERROR, tts_tag(), "Destory tts client");
156 ret = tts_destroy(g_tts);
157 if (TTS_ERROR_NONE != ret) {
158 SLOG(LOG_ERROR, tts_tag(), "Fail to destroy");
161 ecore_main_loop_quit();
166 static void __tts_test_state_changed_cb(tts_h tts, tts_state_e previous, tts_state_e current, void* user_data)
168 if (TTS_STATE_CREATED == previous && TTS_STATE_READY == current) {
169 SLOG(LOG_ERROR, tts_tag(), "State is ready after prepare");
170 ecore_timer_add(0, __tts_test_play, user_data);
176 static void __tts_test_utt_started_cb(tts_h tts, int utt_id, void* user_data)
178 SLOG(LOG_DEBUG, tts_tag(), "Utterance started : utt id(%d)", utt_id);
183 static void __tts_test_utt_completed_cb(tts_h tts, int utt_id, void* user_data)
185 SLOG(LOG_DEBUG, tts_tag(), "Utterance completed : utt id(%d)", utt_id);
186 ecore_timer_add(0, __tts_test_destroy, NULL);
191 int main (int argc, char *argv[])
193 if (1 == argc || 5 < argc) {
194 SLOG(LOG_DEBUG, tts_tag(), "Please check parameter");
195 SLOG(LOG_DEBUG, tts_tag(), "Ex> tts-test -t 'text'");
200 char* src_path = NULL;
204 while (NULL != argv[n]) {
206 if(!strcmp("-h", argv[n])) {
207 SLOG(LOG_DEBUG, tts_tag(), " ==========================================");
208 SLOG(LOG_DEBUG, tts_tag(), " TTS test usage");
209 SLOG(LOG_DEBUG, tts_tag(), " ==========================================");
210 SLOG(LOG_DEBUG, tts_tag(), " -t : Synthesize text");
211 SLOG(LOG_DEBUG, tts_tag(), " -l : Determine langage to synthesize text, ex) en_US, ko_KR ...");
212 SLOG(LOG_DEBUG, tts_tag(), " -f : Determine file path which include text");
213 SLOG(LOG_DEBUG, tts_tag(), " ***************************************************");
214 SLOG(LOG_DEBUG, tts_tag(), " Example : #tts-test -l en_US -t \"1 2 3 4\" ");
215 SLOG(LOG_DEBUG, tts_tag(), " ***************************************************");
219 /* check langage option */
220 if (!strcmp("-l", argv[n])) {
221 lang = TTS_STRDUP(argv[n+1]);
222 SLOG(LOG_ERROR, tts_tag(), "Language : %s", lang);
224 /* check text to synthesize */
225 else if (!strcmp("-t", argv[n])) {
226 g_text = TTS_STRDUP(argv[n+1]);
227 SLOG(LOG_ERROR, tts_tag(), "Text : %s", g_text);
229 /* check file path to synthesize */
230 else if (!strcmp("-f", argv[n])) {
231 src_path = TTS_STRDUP(argv[n+1]);
232 SLOG(LOG_ERROR, tts_tag(), "File path : %s", src_path);
233 if(!__tts_test_get_text_from_file(src_path, &g_text)) {
240 if (!g_text && !src_path) {
241 SLOG(LOG_ERROR, tts_tag(), "Invalid parameter, check help with command tts-test -h");
245 /*=================================== */
247 tts_mode_e mode = TTS_MODE_DEFAULT;
249 SLOG(LOG_DEBUG, tts_tag(), " ");
250 SLOG(LOG_DEBUG, tts_tag(), " ");
251 SLOG(LOG_DEBUG, tts_tag(), "===== TTS Sample start =====");
253 SLOG(LOG_DEBUG, tts_tag(), "Input text : %s", g_text ? g_text : "NULL");
254 SLOG(LOG_DEBUG, tts_tag(), "Input lang : %s", lang ? lang : "NULL");
255 SLOG(LOG_DEBUG, tts_tag(), "Input file path : %s", src_path ? src_path : "NULL");
258 SLOG(LOG_ERROR, tts_tag(), "[Main ERROR] Fail ecore_init()");
264 SLOG(LOG_DEBUG, tts_tag(), "Create tts client");
265 ret = tts_create(&g_tts);
266 if (TTS_ERROR_NONE != ret) {
267 SLOG(LOG_ERROR, tts_tag(), "Fail to create");
271 SLOG(LOG_DEBUG, tts_tag(), "Set tts mode - %d", mode);
272 ret = tts_set_mode(g_tts, mode);
273 if (TTS_ERROR_NONE != ret) {
274 SLOG(LOG_ERROR, tts_tag(), "Fail to set mode");
279 SLOG(LOG_DEBUG, tts_tag(), "Set Callback func");
280 ret = tts_set_state_changed_cb(g_tts, __tts_test_state_changed_cb, (void*)lang);
281 if (TTS_ERROR_NONE != ret) {
282 SLOG(LOG_ERROR, tts_tag(), "Fail to set state changed cb");
287 ret = tts_set_utterance_started_cb(g_tts, __tts_test_utt_started_cb, NULL);
288 if (TTS_ERROR_NONE != ret) {
289 SLOG(LOG_ERROR, tts_tag(), "Fail to set utt started cb");
294 ret = tts_set_utterance_completed_cb(g_tts, __tts_test_utt_completed_cb, NULL);
295 if (TTS_ERROR_NONE != ret) {
296 SLOG(LOG_ERROR, tts_tag(), "Fail to set utt completed cb");
301 SLOG(LOG_DEBUG, tts_tag(), "Prepare (Daemon Connection) asynchronously : Wait for ready state");
302 ret = tts_prepare(g_tts);
303 if (TTS_ERROR_NONE != ret) {
304 SLOG(LOG_ERROR, tts_tag(), "Fail to prepare");
309 ecore_main_loop_begin();
313 if (src_path) free(src_path);
314 if (lang) free(lang);
315 if (g_text) free(g_text);
317 SLOG(LOG_DEBUG, tts_tag(), "===== TTS END =====");