Merge with Tizen 2.3
[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\n");
49                 fclose(fp);
50                 return 0;
51         }
52         SLOG(LOG_ERROR, tts_tag(), "text_len(%d)\n", text_len);
53         rewind(fp);
54
55         *text = (char*)calloc(1, text_len+1);
56
57         if (text == NULL) {
58                 SLOG(LOG_ERROR, tts_tag(), "Fail to memory allocation\n");              
59                 return 0;
60         }
61         
62         int result_len = 1;
63         while (!feof(fp)) {
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");
67                         return 0;
68                 }
69         }
70
71         *text[result_len] = '\0';
72
73         fclose(fp);
74         return 1;
75 }
76
77 Eina_Bool __tts_test_play(void *data)
78 {
79         int utt_id;
80         int ret;
81         char* lang = NULL;
82
83         lang = (char*)data;
84         
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);
89                 return EINA_FALSE;
90         }
91
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);
97                 return EINA_FALSE;
98         }
99
100         return EINA_FALSE;
101 }
102
103 Eina_Bool __tts_test_destroy(void *data)
104 {
105         int ret;
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");
110         }
111
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");
116         }
117
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");
122         }
123
124         ecore_main_loop_quit();
125
126         return EINA_FALSE;
127 }
128
129 static void __tts_test_state_changed_cb(tts_h tts, tts_state_e previous, tts_state_e current, void* user_data)
130 {
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);
134         }
135
136         return;
137 }
138
139 static void __tts_test_utt_started_cb(tts_h tts, int utt_id, void* user_data)
140 {
141         SLOG(LOG_DEBUG, tts_tag(), "Utterance started : utt id(%d) \n", utt_id);
142
143         return;
144 }
145
146 static void __tts_test_utt_completed_cb(tts_h tts, int utt_id, void* user_data)
147 {
148         SLOG(LOG_DEBUG, tts_tag(), "Utterance completed : utt id(%d) \n", utt_id);
149         ecore_timer_add(0, __tts_test_destroy, NULL);
150
151         return;
152 }
153
154 int main (int argc, char *argv[])
155 {
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");
160                 return 0;
161         }
162
163         char* lang = NULL;
164         char* src_path = NULL;
165
166         int n = 0;
167
168         while(NULL != argv[n]) {
169
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");
182                         return 0;
183                 }
184                 
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);
189                 }
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);
194                 }
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)) {
200                                 return 0;
201                         }
202                 }
203                 n++;
204         }
205         
206         if(!g_text && !src_path) {
207                 SLOG(LOG_ERROR, tts_tag(), "Invalid parameter, check help with command tts-test -h");
208                 return 0;
209         }
210
211 //===================================
212
213         tts_mode_e mode = TTS_MODE_DEFAULT;
214
215         SLOG(LOG_DEBUG, tts_tag(), "  ");
216         SLOG(LOG_DEBUG, tts_tag(), "  ");
217         SLOG(LOG_DEBUG, tts_tag(), "===== TTS Sample start =====\n");
218
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");
222
223         if (!ecore_init()) {
224                 SLOG(LOG_ERROR, tts_tag(), "[Main ERROR] Fail ecore_init()\n");
225                 return 0;
226         }
227
228         int ret;
229
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");
234                 return 0;
235         }
236
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");
241                 tts_destroy(g_tts);
242                 return 0;
243         }
244
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");
249                 tts_destroy(g_tts);
250                 return 0;
251         }
252
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");
256                 tts_destroy(g_tts);
257                 return 0;
258         }
259
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");
263                 tts_destroy(g_tts);
264                 return 0;
265         }
266
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");
271                 tts_destroy(g_tts);
272                 return 0;
273         }
274
275         ecore_main_loop_begin();
276
277         ecore_shutdown();
278
279         if(src_path) free(src_path);
280         if(lang) free(lang);
281         if(g_text) free(g_text);
282
283         SLOG(LOG_DEBUG, tts_tag(), "===== TTS END =====\n\n\n");
284
285         return 0;
286 }