Fix bugs
[platform/core/uifw/stt.git] / test / test_main.c
1 /*
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.
12 */
13
14 #include <stdio.h>
15 #include <Ecore.h>
16 #include <dlog.h>
17
18 #include <stt.h>
19 #include <stt_file.h>
20 #include <unistd.h>
21
22 #define TAG_STT_TEST "stt_test"
23
24 static stt_h g_stt = NULL;
25
26 static Eina_Bool __stt_test_finalize(void *data)
27 {
28         int ret;
29         SLOG(LOG_DEBUG, TAG_STT_TEST, "Unset callbacks");
30         ret = stt_file_unset_recognition_result_cb();
31         if (STT_FILE_ERROR_NONE != ret) {
32                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to unset recognition cb");
33         }
34
35         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT file deinitialize");
36         ret = stt_file_deinitialize();
37         if (STT_FILE_ERROR_NONE != ret) {
38                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to deinitialize");
39         }
40
41         ecore_main_loop_quit();
42
43         return EINA_FALSE;
44 }
45
46 static void __stt_file_state_changed_cb(stt_file_state_e previous, stt_file_state_e current, void* user_data)
47 {
48 }
49
50 static bool __stt_file_supported_language_cb(const char* language, void* user_data)
51 {
52         if (NULL == language)
53                 return false;
54
55         SLOG(LOG_DEBUG, TAG_STT_TEST, "=== %s ===", language);
56         return true;
57 }
58
59 static bool __stt_file_result_time_cb(int index, stt_file_result_time_event_e event, const char* text,
60                                       long start_time, long end_time, void* user_data)
61 {
62         if (NULL != text) {
63                 SLOG(LOG_DEBUG, TAG_STT_TEST, "[%d] %s - %ld to %ld", index, text, start_time, end_time);
64                 return true;
65         }
66         return false;
67 }
68
69 static void __stt_file_recognition_result_cb(stt_file_result_event_e event, const char** data, int data_count, const char* msg, void *user_data)
70 {
71         SLOG(LOG_DEBUG, TAG_STT_TEST, "==== Result Callback ====");
72         if (STT_FILE_RESULT_EVENT_ERROR == event) {
73                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Error Result");
74                 ecore_timer_add(0, __stt_test_finalize, NULL);
75                 return;
76         }
77
78         if (NULL != data) {
79                 SLOG(LOG_DEBUG, TAG_STT_TEST, "( %s )", data[0]);
80         }
81
82
83         int ret;
84         ret = stt_file_foreach_detailed_result(__stt_file_result_time_cb, NULL);
85         if (STT_FILE_ERROR_NONE != ret) {
86                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to get result time");
87         }
88
89         if (STT_FILE_RESULT_EVENT_FINAL_RESULT == event) {
90                 ecore_timer_add(0, __stt_test_finalize, NULL);
91         }
92
93         return;
94 }
95
96 static bool __supported_engine_cb(const char* engine_id, const char* engine_name, void* user_data)
97 {
98         SLOG(LOG_DEBUG, TAG_STT_TEST, "Engine id(%s) name(%s)", engine_id, engine_name);
99
100         return true;
101 }
102
103 static Eina_Bool __get_engine_list(void *data)
104 {
105         int ret;
106         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT get engine list");
107         ret = stt_file_foreach_supported_engines(__supported_engine_cb, NULL);
108         if (STT_FILE_ERROR_NONE != ret) {
109                 SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to get engine list");
110         }
111
112         char *cur_engine;
113         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT get engine");
114         ret = stt_file_get_engine(&cur_engine);
115         if (STT_FILE_ERROR_NONE != ret) {
116                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to get engine");
117                 return EINA_FALSE;
118         }
119
120         if (NULL != cur_engine) {
121                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Current engine - %s", cur_engine);
122                 free(cur_engine);
123         }
124
125         return EINA_FALSE;
126 }
127
128 static Eina_Bool __file_start(void *data)
129 {
130         char *tmp = (char *)data;
131         int ret;
132         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT start file recognition");
133         ret = stt_file_start("en_US", "stt.recognition.type.FREE", tmp, STT_FILE_AUDIO_TYPE_RAW_S16, 16000);
134         if (STT_FILE_ERROR_NONE != ret) {
135                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to start");
136         }
137
138         return EINA_FALSE;
139 }
140
141 static Eina_Bool __stt_get_volume(void *data)
142 {
143         float volume = 0.0;
144         int ret = stt_get_recording_volume(g_stt, &volume);
145         if (STT_ERROR_NONE != ret) {
146                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to get volume");
147                 return EINA_FALSE;
148         }
149
150         SLOG(LOG_DEBUG, TAG_STT_TEST, "Get volume : %f", volume);
151
152         return EINA_TRUE;
153 }
154
155 static Eina_Bool __stt_start(void *data)
156 {
157         int ret;
158         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT set silence detection");
159         ret = stt_set_silence_detection(g_stt, STT_OPTION_SILENCE_DETECTION_TRUE);
160         if (STT_ERROR_NONE != ret) {
161                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to set silence detection");
162         }
163
164         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT start");
165         ret = stt_start(g_stt, "en_US", "stt.recognition.type.FREE");
166         if (STT_ERROR_NONE != ret) {
167                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to start");
168         }
169
170         ecore_timer_add(0.1, __stt_get_volume, NULL);
171
172         return EINA_FALSE;
173 }
174
175 static void __stt_state_changed_cb(stt_h stt, stt_state_e previous, stt_state_e current, void* user_data)
176 {
177         if (STT_STATE_CREATED == previous && STT_STATE_READY == current) {
178                 SLOG(LOG_DEBUG, TAG_STT_TEST, "State is Created -> Ready");
179                 ecore_timer_add(0, __stt_start, NULL);
180         }
181 }
182
183 static Eina_Bool __stt_finalize(void *data)
184 {
185         int ret;
186         SLOG(LOG_DEBUG, TAG_STT_TEST, "STT destroy");
187         ret = stt_destroy(g_stt);
188         if (STT_ERROR_NONE != ret) {
189                 SLOG(LOG_ERROR, TAG_STT_TEST, "[ERROR] Fail to stt destroy");
190         }
191
192         ecore_main_loop_quit();
193         return EINA_FALSE;
194 }
195
196 static void __stt_recognition_result_cb(stt_h stt, stt_result_event_e event, const char** data, int data_count, const char* msg, void *user_data)
197 {
198         SLOG(LOG_DEBUG, TAG_STT_TEST, "==== STT result cb ====");
199
200         if (NULL != data) {
201                 SLOG(LOG_DEBUG, TAG_STT_TEST, "( %s )", data[0]);
202         }
203
204         ecore_timer_add(0, __stt_finalize, NULL);
205 }
206
207 int main(int argc, char *argv[])
208 {
209         if (2 > argc) {
210                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Please check parameter");
211                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Ex> stt-test -f <file path>");
212                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Ex> stt-test -m");
213                 return 0;
214         }
215
216         if (0 != strcmp("-f", argv[1]) && 0 != strcmp("-m", argv[1])) {
217                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Please check parameter");
218                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Ex> stt-test -f <file path>");
219                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Ex> stt-test -m");
220                 return 0;
221         }
222
223         if (!strcmp("-f", argv[1])) {
224                 if (0 != access(argv[2], F_OK)) {
225                         SLOG(LOG_DEBUG, TAG_STT_TEST, "Please check filepath");
226                         return 0;
227                 }
228
229                 SLOG(LOG_DEBUG, TAG_STT_TEST, "===== STT File (%s) Sample start =====", argv[2]);
230
231                 if (!ecore_init()) {
232                         SLOG(LOG_ERROR, TAG_STT_TEST, "[Main ERROR] Fail ecore_init()");
233                         return 0;
234                 }
235
236                 int ret;
237                 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT File initialize");
238                 ret = stt_file_initialize();
239                 if (STT_FILE_ERROR_NONE != ret) {
240                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to initialize");
241                         return 0;
242                 }
243
244                 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT Set Callbacks");
245                 ret = stt_file_set_recognition_result_cb(__stt_file_recognition_result_cb, NULL);
246                 if (STT_FILE_ERROR_NONE != ret) {
247                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set recognition result cb");
248                         return 0;
249                 }
250
251                 ret = stt_file_set_state_changed_cb(__stt_file_state_changed_cb, NULL);
252                 if (STT_FILE_ERROR_NONE != ret) {
253                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set state changed cb");
254                         return 0;
255                 }
256
257                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Get supported langauge");
258                 ret = stt_file_foreach_supported_languages(__stt_file_supported_language_cb, NULL);
259                 if (STT_FILE_ERROR_NONE != ret) {
260                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to get supported language");
261                         return 0;
262                 }
263
264                 ecore_timer_add(1, __get_engine_list, NULL);
265
266                 char *tmp = strdup(argv[2]);
267                 ecore_timer_add(1, __file_start, tmp);
268         } else if (!strcmp("-m", argv[1])) {
269
270                 ecore_init();
271
272                 int ret;
273
274                 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT Create");
275                 ret = stt_create(&g_stt);
276                 if (STT_ERROR_NONE != ret) {
277                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to create");
278                         return 0;
279                 }
280
281                 SLOG(LOG_DEBUG, TAG_STT_TEST, "Set callback");
282                 ret = stt_set_state_changed_cb(g_stt, __stt_state_changed_cb, NULL);
283                 if (STT_ERROR_NONE != ret) {
284                         stt_destroy(g_stt);
285                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set state changed cb");
286                         return 0;
287                 }
288                 ret = stt_set_recognition_result_cb(g_stt, __stt_recognition_result_cb, NULL);
289                 if (STT_ERROR_NONE != ret) {
290                         stt_destroy(g_stt);
291                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set recognition result cb");
292                         return 0;
293                 }
294
295                 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT prepare");
296                 ret = stt_prepare(g_stt);
297                 if (STT_ERROR_NONE != ret) {
298                         stt_destroy(g_stt);
299                         SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to prepare");
300                         return 0;
301                 }
302         }
303
304         ecore_main_loop_begin();
305
306         ecore_shutdown();
307
308         SLOG(LOG_DEBUG, TAG_STT_TEST, "===== STT END =====");
309
310         return 0;
311 }