Add initial code for inputdelegator
[platform/core/uifw/inputdelegator.git] / inc / SttManager.h
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
18 #pragma once
19
20 #include <string>
21 #include <vector>
22 #include <stdexcept>
23
24
25 extern "C" {
26         #include <stt.h>
27 }
28
29 namespace is {
30 namespace stt {
31
32 class SttException : public std::runtime_error {
33
34         public :
35                 SttException(int code, const char *reason)
36                         : std::runtime_error(reason)
37                         , ecode(code) { };
38
39                 int GetEcode() {
40
41                         return ecode;
42                 };
43
44                 int ecode;
45 };
46
47 class ISttFeedback
48 {
49         public :
50                 virtual ~ISttFeedback() = 0;
51                 /**
52                  * Result event which can be overloaded
53                  *
54                  * @param event
55                  * @param results
56                  * @param msg
57                  */
58                 virtual void OnResult(
59                         std::string asrtype,
60                         stt_result_event_e event,
61                         std::vector<std::string> results,
62                         std::string msg) = 0;
63
64                 virtual void AutoStart(void) = 0;
65
66                 virtual void SttIdle(void) = 0;
67
68                 virtual void SttRecording(void) = 0;
69
70                 virtual void SttProcessing(void) = 0;
71
72                 /**
73                  * Error event which can be overloaded
74                  *
75                  * @param reason
76                  */
77                 virtual void OnError(stt_error_e reason) = 0;
78 };
79 inline ISttFeedback::~ISttFeedback() { };
80
81 class SttManager
82 {
83         private :
84                 stt_h handle;
85                 std::string language;
86
87         public :
88                 ISttFeedback& ifeedback;
89                 bool iscancelled;
90                 std::string asrtype;
91
92         public :
93
94                 SttManager(ISttFeedback& feedback);
95
96                 ~SttManager();
97
98                 /**
99                  * Prepare stt service.
100                  *
101                  */
102                 void Prepare();
103
104                 /**
105                  * Unprepare stt service
106                  *
107                  */
108                 void UnPrepare();
109
110                 /**
111                  * Start stt service
112                  *
113                  */
114                 void Start();
115
116                 /**
117                  * Stop stt service
118                  *
119                  */
120                 void Stop();
121
122                 /**
123                  * Cancle stt service
124                  *
125                  */
126                 void Cancel();
127
128                 /**
129                  * Initialize
130                  *
131                  */
132                 void Initialize();
133
134                 bool Validate(int state);
135
136
137                 /**
138                  * result cb for tizen stt api.
139                  *
140                  * @param handle
141                  * @param event
142                  * @param data
143                  * @param data_count
144                  * @param msg
145                  * @param user_data
146                  */
147                 static void on_result(
148                         stt_h handle,
149                         stt_result_event_e event,
150                         const char **data,
151                         int data_count,
152                         const char *msg,
153                         void *user_data);
154
155                 /**
156                  * state changed cb for tizen stt api.
157                  *
158                  * @param handle
159                  * @param previous
160                  * @param current
161                  * @param user_data
162                  */
163                 static void on_state_changed(
164                         stt_h handle,
165                         stt_state_e previous,
166                         stt_state_e current,
167                         void *user_data);
168
169                 /**
170                  * error cb for tizen stt api.
171                  *
172                  * @param handle
173                  * @param reason
174                  * @param user_data
175                  */
176                 static void on_error(stt_h handle, stt_error_e reason, void *user_data);
177
178                 /**
179                  * Setter language property
180                  *
181                  * @param language
182                  */
183                 void SetLanguage(std::string language);
184
185                 /**
186                  * Enable cb event or not
187                  *
188                  * @param enabled
189                  */
190                 void EnableFeedback(bool enabled = true);
191
192                 /**
193                  * Enable silence detect
194                  *
195                  * @param enabled
196                  */
197                 void EnableSilenceDetection(bool enabled = false);
198
199                 /**
200                  * Sound feedback
201                  *
202                  */
203                 void SoundFeedback();
204
205                 /**
206                  * Convert erroro code as string
207                  *
208                  * @param errocode
209                  * @return human readable string about error code.
210                  */
211                 const char *ErrorString(int ecode);
212
213                 stt_state_e GetCurrent(void) {
214
215                         stt_state_e cur;
216                         stt_get_state(handle, &cur);
217
218                         return cur;
219                 };
220
221                 stt_h GetSttHandle() {  return handle;  }
222
223         private :
224                 static void PrintErrorState(stt_error_e reason);
225                 static void PrintState(stt_state_e previous, stt_state_e current);
226                 static void PrintResultState(stt_result_event_e result_type);
227 };
228
229 }} /** end of is::stt */