Coding rule check
[platform/core/uifw/inputdelegator.git] / src / w-input-stt-engine.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <Elementary.h>
18 #include <gmodule.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <vconf.h>
22 #include <feedback.h>
23 #include <wav_player.h>
24 #include <vconf-internal-keys.h>
25 #include <app.h>
26
27 #include "w-input-selector.h"
28 #include "w-input-stt-voice.h"
29 #include "w-input-stt-engine.h"
30 #include "Debug.h"
31
32 #define FREE(ptr)       \
33                 do { \
34                         if (ptr != NULL) { \
35                                 free(ptr); \
36                                 ptr = NULL; \
37                         } \
38                 } while (0);
39
40
41 static stt_state_e static_current_stt_state = STT_STATE_CREATED;
42 static stt_h g_stt;
43
44 static void _on_state_change_cb(stt_h stt, stt_state_e previous, stt_state_e current, void* voice_data);
45 static void _on_error_cb(stt_h stt, stt_error_e reason, void *voice_data);
46 static void _on_result_cb(stt_h stt, stt_result_event_e event, const char** data, int data_count, const char* msg, void *voice_data);
47
48 static bool is_auto_spacing(void *voice_data)
49 {
50         VoiceData *voicedata = (VoiceData *)voice_data;
51         int lang = 0, ret = 0;
52 #if 0
53         ret = preference_get_int(PREFERENCE_ISE_STT_LANGUAGE, &lang);
54         if(PREFERENCE_ERROR_NONE != ret){
55                 PRINTFUNC(DLOG_ERROR, "preference_get_int error!(%d)", ret);
56         }
57 #else
58         ret = vconf_get_int(VCONFKEY_ISE_STT_LANGUAGE, &lang);
59         if (ret !=0) {
60                 PRINTFUNC(DLOG_ERROR, "Vconf_get_int error!(%d)", ret);
61         }
62 #endif
63         switch(lang) {
64         case 0 :
65                 if((strcmp(voicedata->kbd_lang, "zh_CN") == 0) || (strcmp(voicedata->kbd_lang, "ja_JP") == 0))
66                         return false;
67                 else
68                         return true;
69         case 7: // Japanese
70         case 10: // Chinese
71                 return false;
72         default :
73                 return true;
74         }
75
76         return true;
77 }
78
79 char * error_string(int ecode)
80 {
81         char *str = NULL;
82         switch (ecode) {
83         case STT_ERROR_OUT_OF_MEMORY:
84                 str = "STT_ERROR_OUT_OF_MEMORY";
85                 break;
86         case STT_ERROR_IO_ERROR:
87                 str = "STT_ERROR_IO_ERROR";
88                 break;
89         case STT_ERROR_INVALID_PARAMETER:
90                 str = "STT_ERROR_INVALID_PARAMETER";
91                 break;
92         case STT_ERROR_TIMED_OUT:
93                 str = "STT_ERROR_TIMED_OUT";
94                 break;
95         case STT_ERROR_RECORDER_BUSY:
96                 str = "STT_ERROR_RECORDER_BUSY";
97                 break;
98         case STT_ERROR_OUT_OF_NETWORK:
99                 str = "STT_ERROR_OUT_OF_NETWORK";
100                 break;
101         case STT_ERROR_INVALID_STATE:
102                 str = " STT_ERROR_INVALID_STATE";
103                 break;
104         case STT_ERROR_INVALID_LANGUAGE:
105                 str = "STT_ERROR_INVALID_LANGUAGE";
106                 break;
107         case STT_ERROR_ENGINE_NOT_FOUND:
108                 str = "STT_ERROR_ENGINE_NOT_FOUND";
109                 break;
110         case STT_ERROR_OPERATION_FAILED:
111                 str = "STT_ERROR_OPERATION_FAILED";
112                 break;
113         case STT_ERROR_NOT_SUPPORTED_FEATURE:
114                 str = "STT_ERROR_NOT_SUPPORTED_FEATURE";
115                 break;
116         }
117         return str;
118 }
119
120 void voice_stt_set_silence_detection_func(bool bEnable)
121 {
122         int ret = STT_ERROR_NONE;
123
124         stt_option_silence_detection_e s_option;
125
126         if(bEnable)
127                 s_option = STT_OPTION_SILENCE_DETECTION_TRUE;
128         else
129                 s_option = STT_OPTION_SILENCE_DETECTION_FALSE;
130
131         ret = stt_set_silence_detection(g_stt, s_option);
132         if (STT_ERROR_NONE != ret) {
133                 PRINTFUNC(DLOG_ERROR, "stt_set_silence_detection Failed : error(%d) = %s", ret, error_string((stt_error_e)ret));
134         } else {
135                 PRINTFUNC(NO_PRINT, "stt_set_silence_detection Successful");
136         }
137 }
138
139
140 ////////////////////////////////////////////////////////////////////////////////
141 // STT Callback functions
142 ////////////////////////////////////////////////////////////////////////////////
143
144 void on_feedback(stt_h handle)
145 {
146         int is_sound = 0;
147         int is_sound_vibe = 0;
148
149         if(vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &is_sound)) {
150                 PRINTFUNC(DLOG_ERROR, "get sound status failed.");
151         }
152
153         if(vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &is_sound_vibe)) {
154                 PRINTFUNC(DLOG_ERROR, "get vibe status failed.");
155         }
156
157         if (is_sound || is_sound_vibe) {
158                 stt_set_start_sound(handle, "/usr/share/ise-voice-input/audio/voice_start.wav");
159                 stt_set_stop_sound(handle, "/usr/share/ise-voice-input/audio/voice_stop.wav");
160         } else {
161                 stt_unset_start_sound(handle);
162                 stt_unset_stop_sound(handle);
163         }
164 }
165
166 ////////////////////////////////////////////////////////////////////////////////
167 // STT APIs callers
168 ////////////////////////////////////////////////////////////////////////////////
169
170 bool _app_stt_initialize(VoiceData *voice_data)
171 {
172         PRINTFUNC(NO_PRINT, "_app_stt_initialize");
173         VoiceData *vd = (VoiceData *)voice_data;
174
175         try {
176                 if(vd->sttmanager) {
177                         vd->sttmanager->Cancel();
178                         delete vd->sttmanager;
179                         vd->sttmanager = NULL;
180                 }
181
182                 if(vd->sttfeedback) {
183                         delete vd->sttfeedback;
184                         vd->sttfeedback = NULL;
185                 }
186
187                 vd->sttfeedback = new is::stt::SttFeedback();
188                 vd->sttfeedback->SetVoiceData(vd);
189
190                 vd->sttmanager = new is::stt::SttManager(*(vd->sttfeedback));
191                 vd->sttmanager->Prepare();
192         } catch(std::exception &e) {
193                 PRINTFUNC(DLOG_ERROR, "%s", e.what());
194                 return false;
195         }
196
197         return true;
198 }
199
200