92075d59a4d7b723df47fd553d48804dbb314f82
[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_h g_stt;
42
43
44 const char * error_string(int ecode)
45 {
46         const char *str = NULL;
47         switch (ecode) {
48         case STT_ERROR_OUT_OF_MEMORY:
49                 str = "STT_ERROR_OUT_OF_MEMORY";
50                 break;
51         case STT_ERROR_IO_ERROR:
52                 str = "STT_ERROR_IO_ERROR";
53                 break;
54         case STT_ERROR_INVALID_PARAMETER:
55                 str = "STT_ERROR_INVALID_PARAMETER";
56                 break;
57         case STT_ERROR_TIMED_OUT:
58                 str = "STT_ERROR_TIMED_OUT";
59                 break;
60         case STT_ERROR_RECORDER_BUSY:
61                 str = "STT_ERROR_RECORDER_BUSY";
62                 break;
63         case STT_ERROR_OUT_OF_NETWORK:
64                 str = "STT_ERROR_OUT_OF_NETWORK";
65                 break;
66         case STT_ERROR_INVALID_STATE:
67                 str = " STT_ERROR_INVALID_STATE";
68                 break;
69         case STT_ERROR_INVALID_LANGUAGE:
70                 str = "STT_ERROR_INVALID_LANGUAGE";
71                 break;
72         case STT_ERROR_ENGINE_NOT_FOUND:
73                 str = "STT_ERROR_ENGINE_NOT_FOUND";
74                 break;
75         case STT_ERROR_OPERATION_FAILED:
76                 str = "STT_ERROR_OPERATION_FAILED";
77                 break;
78         case STT_ERROR_NOT_SUPPORTED_FEATURE:
79                 str = "STT_ERROR_NOT_SUPPORTED_FEATURE";
80                 break;
81         }
82         return str;
83 }
84
85 void voice_stt_set_silence_detection_func(bool bEnable)
86 {
87         int ret = STT_ERROR_NONE;
88
89         stt_option_silence_detection_e s_option;
90
91         if(bEnable)
92                 s_option = STT_OPTION_SILENCE_DETECTION_TRUE;
93         else
94                 s_option = STT_OPTION_SILENCE_DETECTION_FALSE;
95
96         ret = stt_set_silence_detection(g_stt, s_option);
97         if (STT_ERROR_NONE != ret) {
98                 PRINTFUNC(DLOG_ERROR, "stt_set_silence_detection Failed : error(%d) = %s", ret, error_string((stt_error_e)ret));
99         } else {
100                 PRINTFUNC(NO_PRINT, "stt_set_silence_detection Successful");
101         }
102 }
103
104
105 ////////////////////////////////////////////////////////////////////////////////
106 // STT Callback functions
107 ////////////////////////////////////////////////////////////////////////////////
108
109 void on_feedback(stt_h handle)
110 {
111         int is_sound = 0;
112         int is_sound_vibe = 0;
113
114         if(vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &is_sound)) {
115                 PRINTFUNC(DLOG_ERROR, "get sound status failed.");
116         }
117
118         if(vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &is_sound_vibe)) {
119                 PRINTFUNC(DLOG_ERROR, "get vibe status failed.");
120         }
121
122         if (is_sound || is_sound_vibe) {
123                 stt_set_start_sound(handle, "/usr/share/ise-voice-input/audio/voice_start.wav");
124                 stt_set_stop_sound(handle, "/usr/share/ise-voice-input/audio/voice_stop.wav");
125         } else {
126                 stt_unset_start_sound(handle);
127                 stt_unset_stop_sound(handle);
128         }
129 }
130
131 ////////////////////////////////////////////////////////////////////////////////
132 // STT APIs callers
133 ////////////////////////////////////////////////////////////////////////////////
134
135 bool _app_stt_initialize(VoiceData *voice_data)
136 {
137         PRINTFUNC(NO_PRINT, "_app_stt_initialize");
138         VoiceData *vd = (VoiceData *)voice_data;
139
140         try {
141                 if(vd->sttmanager) {
142                         vd->sttmanager->Cancel();
143                         delete vd->sttmanager;
144                         vd->sttmanager = NULL;
145                 }
146
147                 if(vd->sttfeedback) {
148                         delete vd->sttfeedback;
149                         vd->sttfeedback = NULL;
150                 }
151
152                 vd->sttfeedback = new is::stt::SttFeedback();
153                 vd->sttfeedback->SetVoiceData(vd);
154
155                 vd->sttmanager = new is::stt::SttManager(*(vd->sttfeedback));
156                 vd->sttmanager->Prepare();
157         } catch(std::exception &e) {
158                 PRINTFUNC(DLOG_ERROR, "%s", e.what());
159                 return false;
160         }
161
162         return true;
163 }
164
165