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