Add initial code for inputdelegator
[platform/core/uifw/inputdelegator.git] / src / SttFeedback.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 "Debug.h"
18 #include "SttFeedback.h"
19
20 #include "w-input-stt-engine.h"
21 #include "w-input-stt-voice.h"
22
23 using namespace is::stt;
24
25 SttFeedback::SttFeedback()
26 {
27         owner = NULL;
28 }
29
30
31
32 SttFeedback::~SttFeedback()
33 {
34
35 }
36
37
38
39 void SttFeedback::OnResult (
40         std::string asrtype,
41         stt_result_event_e event,
42         std::vector<std::string> results,
43         std::string msg) {
44
45         if(!owner) return;
46
47         VoiceData& vd = *((VoiceData *) owner);
48
49         if(vd.state == STT_STATE_VAL_TERMINATING) {
50                 PRINTFUNC(DLOG_WARN, "STT_STATE_VAL_TERMINATING");
51                 return;
52         }
53
54         // asrtype
55         // Partial result case : STT_RECOGNITION_TYPE_FREE_PARTIAL
56         // Normal result case : STT_RECOGNITION_TYPE_FREE
57
58         if(results.size() < 0) {
59                 PRINTFUNC(DLOG_ERROR, "Result size : %d", results.size());
60                 vd.state = STT_STATE_VAL_NOT_RECOGNISED;
61                 set_animation_state(&vd);
62         }
63         else {
64                 PRINTFUNC(DLOG_INFO, "Meaningful result : size (%d)", results.size());
65
66                 for(auto result : results) {
67                         PRINTFUNC(DLOG_INFO, "Results");
68                         if(!result.empty() && result.length() > 0) {
69                                 PRINTFUNC(DLOG_INFO,"%s\n", result.c_str());
70                                 switch(event){
71                                         case STT_RESULT_EVENT_FINAL_RESULT:
72                                                 PRINTFUNC(DLOG_DEBUG,"STT_RESULT_EVENT_FINAL_RESULT");
73                                                 vd.state = STT_STATE_VAL_INIT;
74                                                 set_animation_state(&vd);
75                                                 vd.result_type = STT_RESULT_EVENT_FINAL_RESULT;
76                                                 voice_get_string(result.c_str(), &vd);
77                                                 break;
78                                         case STT_RESULT_EVENT_PARTIAL_RESULT:
79                                                 PRINTFUNC(DLOG_DEBUG,"STT_RESULT_EVENT_PARTIAL_RESULT");
80                                                 vd.result_type = STT_RESULT_EVENT_PARTIAL_RESULT;
81                                                 voice_get_string(result.c_str(), &vd);
82                                                 break;
83                                         default:
84                                                 PRINTFUNC(DLOG_INFO, "");
85                                                 break;
86                                 }
87                                 return;
88                         }
89                         PRINTFUNC(DLOG_INFO, "Empty result");
90                 }
91
92                 PRINTFUNC(DLOG_INFO, "Need to specify this case");
93
94                 /**
95                  * Note.
96                  *
97                  * if recognized result doesn't have any data,
98                  * it will process as recognition fail.
99                  *
100                  */
101                 //vd.state = STT_STATE_VAL_NOT_RECOGNISED;
102                 vd.state = STT_STATE_VAL_INIT;
103                 set_animation_state(&vd);
104         }
105 }
106
107
108 void SttFeedback::AutoStart(void) {
109         PRINTFUNC(DLOG_DEBUG, "start");
110
111         if (elm_config_access_get()){
112                 PRINTFUNC(DLOG_DEBUG, "accessbility on: It will not start automatically.");
113         } else {
114                 start_by_press((VoiceData *) owner);
115         }
116 }
117
118
119 void SttFeedback::SttIdle(void)
120 {
121         if(!owner) {
122                 PRINTFUNC(DLOG_WARN, "no owner");
123                 return;
124         }
125
126         VoiceData& vd = *((VoiceData *) owner);
127
128         /**
129          * Note.
130          *
131          * When recognition is failed, do nothing.
132          * Because after result, it's called continuous. So it looks not natural.
133          *
134          * So in this case, we will not change as INIT state.
135          * using 2 sec timer, it will change as idle state.
136          *
137          */
138         if(vd.state == STT_STATE_VAL_NOT_RECOGNISED) {
139                 PRINTFUNC(DLOG_INFO, "Ignore when state was STT_STATE_VAL_NOT_RECOGNISED");
140                 return;
141         }
142
143         PRINTFUNC(DLOG_DEBUG, "UI will go to idle state");
144
145         vd.state = STT_STATE_VAL_INIT;
146         set_animation_state(&vd);
147 }
148
149
150
151 void SttFeedback::SttRecording(void)
152 {
153         if(!owner) {
154                 PRINTFUNC(DLOG_WARN, "no owner");
155                 return;
156         }
157
158         VoiceData& vd = *((VoiceData *) owner);
159
160         if(vd.partial_result){
161                 free(vd.partial_result);
162                 vd.partial_result = NULL;
163         }
164
165         PRINTFUNC(DLOG_DEBUG, "UI will go to listening state");
166
167         vd.state = STT_STATE_VAL_LISTENING;
168         set_animation_state(&vd);
169 }
170
171
172
173 void SttFeedback::SttProcessing(void)
174 {
175         if(!owner) {
176                 PRINTFUNC(DLOG_WARN, "no owner");
177                 return;
178         }
179
180         VoiceData& vd = *((VoiceData *) owner);
181
182         PRINTFUNC(DLOG_DEBUG, "UI will go to processing state");
183
184         vd.state = STT_STATE_VAL_PROCESSING;
185         set_animation_state(&vd);
186 }
187
188
189
190 void SttFeedback::OnError(stt_error_e reason)
191 {
192         if(!owner) {
193                 PRINTFUNC(DLOG_WARN, "no owner");
194                 return;
195         }
196
197         VoiceData& vd = *((VoiceData *) owner);
198
199         PRINTFUNC(DLOG_ERROR,"error = %d\n", reason);
200         vd.state = STT_STATE_VAL_INIT;
201         set_animation_state(&vd);
202         show_error_message(&vd, reason);
203 }
204
205
206 void SttFeedback::SetVoiceData(void *data) {
207
208         if(!data) {
209                 PRINTFUNC(DLOG_WARN, "no data");
210                 return;
211         }
212
213         owner = data;
214 }