sync codes to 2.4 spin
[platform/core/uifw/tts.git] / test / test_main.c
1 /*
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.
12 */
13
14 #include <stdio.h>
15 #include <Ecore.h>
16
17 #include <tts.h>
18 #include <dlog.h>
19
20 #define TTS_STRDUP(src)                 ((src != NULL) ? strdup(src) : NULL)
21
22 static tts_h g_tts;
23 static char* g_text = NULL;
24
25 const char* tts_tag()
26 {
27         return "ttstest";
28 }
29
30 Eina_Bool __tts_test_destroy(void *data);
31
32 static bool __tts_test_get_text_from_file(const char* path, char** text)
33 {
34         if (!path) return 0;
35         if (!text) return 0;
36
37         FILE *fp = NULL;
38
39         if ((fp = fopen(path, "rb")) == NULL) {
40                 SLOG(LOG_ERROR, tts_tag(), "Fail to open file (%s)", path);
41                 return 0;
42         }
43
44         fseek(fp , 0 , SEEK_END);
45
46         int text_len = ftell(fp);
47         if (0 >= text_len) {
48                 SLOG(LOG_ERROR, tts_tag(), "File has no contents");
49                 fclose(fp);
50                 return 0;
51         }
52         SLOG(LOG_ERROR, tts_tag(), "text_len(%d)", text_len);
53         rewind(fp);
54
55         char* temp = NULL;
56         temp = (char*)calloc(1, text_len+1);
57
58         if (temp == NULL) {
59                 SLOG(LOG_ERROR, tts_tag(), "Fail to memory allocation");                
60                 fclose(fp);
61                 return 0;
62         }
63
64         int result_len = 1;
65         while (!feof(fp)) {
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);
69                         fclose(fp);
70                         if (NULL != temp) {
71                                 free(temp);
72                                 temp = NULL;
73                         }
74                         return 0;
75                 }
76         }
77
78         temp[result_len] = '\0';
79
80         text = &temp;
81
82         fclose(fp);
83         return 1;
84 }
85
86 Eina_Bool __tts_test_resume(void *data)
87 {
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);
92                 return EINA_FALSE;
93         }
94
95         return EINA_FALSE;
96 }
97
98 Eina_Bool __tts_test_pause(void *data)
99 {
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);
104                 return EINA_FALSE;
105         }
106
107         ecore_timer_add(3, __tts_test_resume, data);
108
109         return EINA_FALSE;
110 }
111
112 Eina_Bool __tts_test_play(void *data)
113 {
114         int utt_id;
115         int ret;
116         char* lang = NULL;
117
118         lang = (char*)data;
119
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);
124                 return EINA_FALSE;
125         }
126
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);
132                 return EINA_FALSE;
133         }
134
135 //      ecore_timer_add(1, __tts_test_pause, data);
136
137         return EINA_FALSE;
138 }
139
140 Eina_Bool __tts_test_destroy(void *data)
141 {
142         int ret;
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");
147         }
148
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");
153         }
154
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");
159         }
160
161         ecore_main_loop_quit();
162
163         return EINA_FALSE;
164 }
165
166 static void __tts_test_state_changed_cb(tts_h tts, tts_state_e previous, tts_state_e current, void* user_data)
167 {
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);
171         }
172
173         return;
174 }
175
176 static void __tts_test_utt_started_cb(tts_h tts, int utt_id, void* user_data)
177 {
178         SLOG(LOG_DEBUG, tts_tag(), "Utterance started : utt id(%d)", utt_id);
179
180         return;
181 }
182
183 static void __tts_test_utt_completed_cb(tts_h tts, int utt_id, void* user_data)
184 {
185         SLOG(LOG_DEBUG, tts_tag(), "Utterance completed : utt id(%d)", utt_id);
186         ecore_timer_add(0, __tts_test_destroy, NULL);
187
188         return;
189 }
190
191 int main (int argc, char *argv[])
192 {
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'");
196                 return 0;
197         }
198
199         char* lang = NULL;
200         char* src_path = NULL;
201
202         int n = 0;
203
204         while (NULL != argv[n]) {
205
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(), " ***************************************************");
216                         return 0;
217                 }
218
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);
223                 }
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);
228                 }
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)) {
234                                 return 0;
235                         }
236                 }
237                 n++;
238         }
239
240         if (!g_text && !src_path) {
241                 SLOG(LOG_ERROR, tts_tag(), "Invalid parameter, check help with command tts-test -h");
242                 return 0;
243         }
244
245         /*=================================== */
246
247         tts_mode_e mode = TTS_MODE_DEFAULT;
248
249         SLOG(LOG_DEBUG, tts_tag(), "  ");
250         SLOG(LOG_DEBUG, tts_tag(), "  ");
251         SLOG(LOG_DEBUG, tts_tag(), "===== TTS Sample start =====");
252
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");
256
257         if (!ecore_init()) {
258                 SLOG(LOG_ERROR, tts_tag(), "[Main ERROR] Fail ecore_init()");
259                 return 0;
260         }
261
262         int ret;
263
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");
268                 return 0;
269         }
270
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");
275                 tts_destroy(g_tts);
276                 return 0;
277         }
278
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");
283                 tts_destroy(g_tts);
284                 return 0;
285         }
286
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");
290                 tts_destroy(g_tts);
291                 return 0;
292         }
293
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");
297                 tts_destroy(g_tts);
298                 return 0;
299         }
300
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");
305                 tts_destroy(g_tts);
306                 return 0;
307         }
308
309         ecore_main_loop_begin();
310
311         ecore_shutdown();
312
313         if (src_path) free(src_path);
314         if (lang) free(lang);
315         if (g_text) free(g_text);
316
317         SLOG(LOG_DEBUG, tts_tag(), "===== TTS END =====");
318
319         return 0;
320 }