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.
22 #define TAG_STT_TEST "stt_test"
24 static stt_h g_stt = NULL;
26 static Eina_Bool __stt_test_finalize(void *data)
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");
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");
41 ecore_main_loop_quit();
46 static void __stt_file_state_changed_cb(stt_file_state_e previous, stt_file_state_e current, void* user_data)
50 static bool __stt_file_supported_language_cb(const char* language, void* user_data)
55 SLOG(LOG_DEBUG, TAG_STT_TEST, "=== %s ===", language);
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)
63 SLOG(LOG_DEBUG, TAG_STT_TEST, "[%d] %s - %ld to %ld", index, text, start_time, end_time);
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)
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);
79 SLOG(LOG_DEBUG, TAG_STT_TEST, "( %s )", data[0]);
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");
89 if (STT_FILE_RESULT_EVENT_FINAL_RESULT == event) {
90 ecore_timer_add(0, __stt_test_finalize, NULL);
96 static bool __supported_engine_cb(const char* engine_id, const char* engine_name, void* user_data)
98 SLOG(LOG_DEBUG, TAG_STT_TEST, "Engine id(%s) name(%s)", engine_id, engine_name);
103 static Eina_Bool __get_engine_list(void *data)
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");
112 char *cur_engine = NULL;
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");
120 if (NULL != cur_engine) {
121 SLOG(LOG_DEBUG, TAG_STT_TEST, "Current engine - %s", cur_engine);
128 static Eina_Bool __file_start(void *data)
130 char *tmp = (char *)data;
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");
141 static Eina_Bool __stt_get_volume(void *data)
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");
150 SLOG(LOG_DEBUG, TAG_STT_TEST, "Get volume : %f", volume);
155 static Eina_Bool __stt_start(void *data)
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");
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");
170 ecore_timer_add(0.1, __stt_get_volume, NULL);
175 static void __stt_state_changed_cb(stt_h stt, stt_state_e previous, stt_state_e current, void* user_data)
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);
183 static Eina_Bool __stt_finalize(void *data)
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");
192 ecore_main_loop_quit();
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)
198 SLOG(LOG_DEBUG, TAG_STT_TEST, "==== STT result cb ====");
201 SLOG(LOG_DEBUG, TAG_STT_TEST, "( %s )", data[0]);
204 if (STT_RESULT_EVENT_FINAL_RESULT == event) {
205 ecore_timer_add(0, __stt_finalize, NULL);
209 int main(int argc, char *argv[])
212 SLOG(LOG_DEBUG, TAG_STT_TEST, "Please check parameter");
213 SLOG(LOG_DEBUG, TAG_STT_TEST, "Ex> stt-test -f <file path>");
214 SLOG(LOG_DEBUG, TAG_STT_TEST, "Ex> stt-test -m");
218 if (0 != strcmp("-f", argv[1]) && 0 != strcmp("-m", argv[1])) {
219 SLOG(LOG_DEBUG, TAG_STT_TEST, "Please check parameter");
220 SLOG(LOG_DEBUG, TAG_STT_TEST, "Ex> stt-test -f <file path>");
221 SLOG(LOG_DEBUG, TAG_STT_TEST, "Ex> stt-test -m");
225 if (!strcmp("-f", argv[1])) {
226 if (0 != access(argv[2], F_OK)) {
227 SLOG(LOG_DEBUG, TAG_STT_TEST, "Please check filepath");
231 SLOG(LOG_DEBUG, TAG_STT_TEST, "===== STT File (%s) Sample start =====", argv[2]);
234 SLOG(LOG_ERROR, TAG_STT_TEST, "[Main ERROR] Fail ecore_init()");
239 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT File initialize");
240 ret = stt_file_initialize();
241 if (STT_FILE_ERROR_NONE != ret) {
242 SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to initialize");
246 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT Set Callbacks");
247 ret = stt_file_set_recognition_result_cb(__stt_file_recognition_result_cb, NULL);
248 if (STT_FILE_ERROR_NONE != ret) {
249 SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set recognition result cb");
253 ret = stt_file_set_state_changed_cb(__stt_file_state_changed_cb, NULL);
254 if (STT_FILE_ERROR_NONE != ret) {
255 SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set state changed cb");
259 SLOG(LOG_DEBUG, TAG_STT_TEST, "Get supported language");
260 ret = stt_file_foreach_supported_languages(__stt_file_supported_language_cb, NULL);
261 if (STT_FILE_ERROR_NONE != ret) {
262 SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to get supported language");
266 ecore_timer_add(1, __get_engine_list, NULL);
268 char *tmp = strdup(argv[2]);
269 ecore_timer_add(1, __file_start, tmp);
270 } else if (!strcmp("-m", argv[1])) {
276 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT Create");
277 ret = stt_create(&g_stt);
278 if (STT_ERROR_NONE != ret) {
279 SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to create");
283 SLOG(LOG_DEBUG, TAG_STT_TEST, "Set callback");
284 ret = stt_set_state_changed_cb(g_stt, __stt_state_changed_cb, NULL);
285 if (STT_ERROR_NONE != ret) {
287 SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set state changed cb");
290 ret = stt_set_recognition_result_cb(g_stt, __stt_recognition_result_cb, NULL);
291 if (STT_ERROR_NONE != ret) {
293 SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set recognition result cb");
297 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT set credential");
298 ret = stt_set_credential(g_stt, "1Q2W3E4R5T");
299 if (STT_ERROR_NONE != ret) {
300 SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to set credential");
304 SLOG(LOG_DEBUG, TAG_STT_TEST, "STT prepare");
305 ret = stt_prepare(g_stt);
306 if (STT_ERROR_NONE != ret) {
308 SLOG(LOG_ERROR, TAG_STT_TEST, "Fail to prepare");
313 ecore_main_loop_begin();
317 SLOG(LOG_DEBUG, TAG_STT_TEST, "===== STT END =====");