Release version 0.1.41
[platform/core/uifw/stt.git] / server / sttp.h
1 /*
2 *  Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd All Rights Reserved 
3 *  Licensed under the Apache License, Version 2.0 (the "License");
4 *  you may not use this file except in compliance with the License.
5 *  You may obtain a copy of the License at
6 *  http://www.apache.org/licenses/LICENSE-2.0
7 *  Unless required by applicable law or agreed to in writing, software
8 *  distributed under the License is distributed on an "AS IS" BASIS,
9 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 *  See the License for the specific language governing permissions and
11 *  limitations under the License.
12 */
13
14 #ifndef __STTP_H__
15 #define __STTP_H__
16
17 #include <errno.h>
18 #include <stdbool.h>
19
20 /**
21 * @addtogroup STT_ENGINE_MODULE
22 * @{
23 */
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 /** 
30 * @brief Enumerations of error codes.
31 */
32 typedef enum {
33         STTP_ERROR_NONE                 =  0,           /**< Successful */
34         STTP_ERROR_OUT_OF_MEMORY        = -ENOMEM,      /**< Out of Memory */
35         STTP_ERROR_IO_ERROR             = -EIO,         /**< I/O error */
36         STTP_ERROR_INVALID_PARAMETER    = -EINVAL,      /**< Invalid parameter */
37         STTP_ERROR_OUT_OF_NETWORK       = -ENETDOWN,    /**< Out of network */
38         STTP_ERROR_INVALID_STATE        = -0x0100031,   /**< Invalid state */
39         STTP_ERROR_INVALID_LANGUAGE     = -0x0100032,   /**< Invalid language */
40         STTP_ERROR_OPERATION_FAILED     = -0x0100034,   /**< Operation failed */
41         STTP_ERROR_NOT_SUPPORTED_FEATURE= -0x0100035    /**< Not supported feature */
42 }sttp_error_e;
43
44 /**
45 * @brief Enumerations of audio type. 
46 */
47 typedef enum {
48         STTP_AUDIO_TYPE_PCM_S16_LE = 0, /**< Signed 16bit audio type, Little endian */
49         STTP_AUDIO_TYPE_PCM_U8          /**< Unsigned 8bit audio type */
50 }sttp_audio_type_e;
51
52 /**
53 * @brief Enumerations of callback event.
54 */
55 typedef enum {
56         STTP_RESULT_EVENT_SUCCESS = 0,  /**< Event when the recognition full result is ready  */
57         STTP_RESULT_EVENT_NO_RESULT,    /**< Event when the recognition result is not */
58         STTP_RESULT_EVENT_ERROR         /**< Event when the recognition has failed */
59 }sttp_result_event_e;
60
61 /** 
62 * @brief Recognition type : free form dictation or default type.
63 */
64 #define STTP_RECOGNITION_TYPE_FREE                      "stt.recognition.type.FREE"
65
66 /** 
67 * @brief Recognition type : web search. 
68 */
69 #define STTP_RECOGNITION_TYPE_WEB_SEARCH                "stt.recognition.type.WEB_SEARCH"
70
71 /** 
72 * @brief Result message : None message
73 */
74 #define STTP_RESULT_MESSAGE_NONE                "stt.result.message.none"
75
76 /** 
77 * @brief Result warning message : The speech has started too soon
78 */
79 #define STTP_RESULT_MESSAGE_WARNING_TOO_SOON    "stt.result.message.warning.too.soon"
80
81 /** 
82 * @brief Result warning message : The speech is too short
83 */
84 #define STTP_RESULT_MESSAGE_WARNING_TOO_SHORT   "stt.result.message.warning.too.short"
85
86 /** 
87 * @brief Result warning message : The speech is too long
88 */
89 #define STTP_RESULT_MESSAGE_WARNING_TOO_LONG    "stt.result.message.warning.too.long"
90
91 /** 
92 * @brief Result warning message : The speech is too quiet to listen
93 */
94 #define STTP_RESULT_MESSAGE_WARNING_TOO_QUIET   "stt.result.message.warning.too.quiet"
95
96 /** 
97 * @brief Result warning message : The speech is too loud to listen
98 */
99 #define STTP_RESULT_MESSAGE_WARNING_TOO_LOUD    "stt.result.message.warning.too.loud"
100
101 /** 
102 * @brief Result warning message : The speech is too fast to listen
103 */
104 #define STTP_RESULT_MESSAGE_WARNING_TOO_FAST    "stt.result.message.warning.too.fast"
105
106 /** 
107 * @brief Result error message : Recognition was failed because the speech started too soon
108 */
109 #define STTP_RESULT_MESSAGE_ERROR_TOO_SOON      "stt.result.message.error.too.soon"
110
111 /** 
112 * @brief Result error message : Recognition was failed because the speech started too short
113 */
114 #define STTP_RESULT_MESSAGE_ERROR_TOO_SHORT     "stt.result.message.error.too.short"
115
116 /** 
117 * @brief Result error message : Recognition was failed because the speech started too long
118 */
119 #define STTP_RESULT_MESSAGE_ERROR_TOO_LONG      "stt.result.message.error.too.long"
120
121 /** 
122 * @brief Result error message : Recognition was failed because the speech started too quiet to listen
123 */
124 #define STTP_RESULT_MESSAGE_ERROR_TOO_QUIET     "stt.result.message.error.too.quiet"
125
126 /** 
127 * @brief Result error message : Recognition was failed because the speech started too loud to listen 
128 */
129 #define STTP_RESULT_MESSAGE_ERROR_TOO_LOUD      "stt.result.message.error.too.loud"
130
131 /** 
132 * @brief Result error message : Recognition was failed because the speech started too fast to listen
133 */
134 #define STTP_RESULT_MESSAGE_ERROR_TOO_FAST      "stt.result.message.error.too.fast"
135
136 /** 
137 * @brief Called to get recognition result.
138
139 * @param[in] event A result event
140 * @param[in] type A recognition type (e.g. #STTP_RECOGNITION_TYPE_FREE, #STTP_RECOGNITION_TYPE_COMMAND)
141 * @param[in] data Result texts
142 * @param[in] data_count Result text count
143 * @param[in] msg Engine message (e.g. #STTP_RESULT_MESSAGE_WARNING_TOO_SOON, #STTP_RESULT_MESSAGE_ERROR_TOO_SHORT)
144 * @param[in] user_data  The user data passed from the start function
145 *
146 * @pre sttpe_stop() will invoke this callback.
147 *
148 * @see sttpe_start()
149 * @see sttpe_stop()
150 */
151 typedef void (*sttpe_result_cb)(sttp_result_event_e event, const char* type, 
152                                 const char** data, int data_count, const char* msg, void *user_data);
153
154 /** 
155 * @brief Called to get partial recognition result.
156
157 * @param[in] event A result event
158 * @param[in] data A result text
159 * @param[in] user_data  The user data passed from the start function.
160 *
161 * @pre sttpe_set_recording_data() will invoke this callback
162 *
163 * @see sttpe_set_recording_data()
164 */
165 typedef void (*sttpe_partial_result_cb)(sttp_result_event_e event, const char* data, void *user_data);
166
167 /** 
168 * @brief Called to detect silence from recording data.
169
170 * @param[in] user_data  The user data passed from the start function.
171 *
172 * @pre sttpe_set_recording_data() will invoke this callback.
173 *
174 * @see sttpe_set_recording_data()
175 */
176 typedef void (*sttpe_silence_detected_cb)(void *user_data);
177
178 /**
179 * @brief Called to retrieve the supported languages. 
180 *
181 * @param[in] language A language is specified as an ISO 3166 alpha-2 two letter country-code
182 *               followed by ISO 639-1 for the two-letter language code \n
183 *               For example, "ko_KR" for Korean, "en_US" for American English
184 * @param[in] user_data The user data passed from the foreach function
185 *
186 * @return @c true to continue with the next iteration of the loop \n @c false to break out of the loop
187 *
188 * @pre sttpe_foreach_supported_languages() will invoke this callback. 
189 *
190 * @see sttpe_foreach_supported_languages()
191 */
192 typedef bool (*sttpe_supported_language_cb)(const char* language, void* user_data);
193
194 /**
195 * @brief Called to retrieve the supported engine settings.
196 *
197 * @param[in] key A key
198 * @param[in] value A value
199 * @param[in] user_data The user data passed from the foreach function
200 *
201 * @return @c true to continue with the next iteration of the loop, \n @c false to break out of the loop.
202 * @pre sttpe_foreach_engine_settings() will invoke this callback. 
203 *
204 * @see sttpe_foreach_engine_settings()
205 */
206 typedef bool (*sttpe_engine_setting_cb)(const char* key, const char* value, void* user_data);
207
208 /**
209 * @brief Initializes the engine.
210 *
211 * @param[in] result_cb A callback function for recognition result
212 * @param[in] partial_result_cb A callback function for partial recognition result
213 * @param[in] silence_cb A callback function for silence detection
214 *
215 * @return 0 on success, otherwise a negative error value
216 * @retval #STTP_ERROR_NONE Successful
217 * @retval #STTP_ERROR_INVALID_PARAMETER Invalid parameter
218 * @retval #STTP_ERROR_INVALID_STATE Already initialized
219 * @retval #STTP_ERROR_OPERATION_FAILED Operation failed
220
221 * @see sttpe_deinitialize()
222 */
223 typedef int (* sttpe_initialize)(sttpe_result_cb result_cb, sttpe_partial_result_cb partial_result_cb, 
224                                  sttpe_silence_detected_cb silence_cb);
225
226 /**
227 * @brief Deinitializes the engine
228 *
229 * @return 0 on success, otherwise a negative error value
230 * @retval #STTP_ERROR_NONE Successful
231 * @retval #STTP_ERROR_INVALID_STATE Not initialized
232
233 * @see sttpe_initialize()
234 */
235 typedef int (* sttpe_deinitialize)(void);
236
237 /**
238 * @brief Retrieves all supported languages of the engine.
239 *
240 * @param[in] callback a callback function
241 * @param[in] user_data The user data to be passed to the callback function
242 *
243 * @return 0 on success, otherwise a negative error value
244 * @retval #STTP_ERROR_NONE Successful
245 * @retval #STTP_ERROR_INVALID_PARAMETER Invalid parameter
246 * @retval #STTP_ERROR_INVALID_STATE Not initialized
247 *
248 * @post This function invokes sttpe_supported_language_cb() repeatedly for getting supported languages. 
249 *
250 * @see sttpe_supported_language_cb()
251 */
252 typedef int (* sttpe_foreach_supported_languages)(sttpe_supported_language_cb callback, void* user_data);
253
254 /**
255 * @brief Checks whether a language is valid or not.
256 *
257 * @param[in] language A language
258 *
259 * @return 0 on success, otherwise a negative error value
260 * @retval #STTP_ERROR_NONE Successful
261 * @retval #STTP_ERROR_INVALID_PARAMETER Invalid parameter
262 * @retval #STTP_ERROR_INVALID_STATE Not initialized
263 *
264 * @see sttpe_foreach_supported_languages()
265 */
266 typedef bool (* sttpe_is_valid_language)(const char* language);
267
268 /**
269 * @brief Gets whether the engine supports silence detection. 
270 *
271 * @return 0 on success, otherwise a negative error value
272 * @retval #STTP_ERROR_NONE Successful
273 * @retval #STTP_ERROR_INVALID_STATE Not initialized
274 */
275 typedef bool (* sttpe_support_silence_detection)(void);
276
277 /**
278 * @brief Gets supporting partial result. 
279 *
280 * @return 0 on success, otherwise a negative error value
281 * @retval #STTP_ERROR_NONE Successful
282 * @retval #STTP_ERROR_INVALID_STATE Not initialized
283 */
284 typedef bool (* sttpe_support_partial_result)(void);
285
286 /**
287 * @brief Gets recording format of the engine. 
288 *
289 * @param[out] types The format used by the recorder.
290 * @param[out] rate The sample rate used by the recorder.
291 * @param[out] channels The number of channels used by the recorder.
292 *
293 * @return 0 on success, otherwise a negative error value
294 * @retval #STTP_ERROR_NONE Successful
295 * @retval #STTP_ERROR_INVALID_STATE Not initialized
296 */
297 typedef int (* sttpe_get_recording_format)(sttp_audio_type_e* types, int* rate, int* channels);
298
299 /**
300 * @brief Sets profanity filter option.
301
302 * @param[in] value A value
303 *
304 * @return 0 on success, otherwise a negative error value
305 * @retval #STTP_ERROR_NONE Successful
306 * @retval #STTP_ERROR_INVALID_STATE Not initialized
307 * @retval #STTP_ERROR_NOT_SUPPORTED_FEATURE Not supported feature
308 */
309 typedef int (* sttpe_set_profanity_filter)(bool value);
310
311 /**
312 * @brief Sets punctuation option.
313
314 * @param[in] value A value
315 *
316 * @return 0 on success, otherwise a negative error value
317 * @retval #STTP_ERROR_NONE Successful
318 * @retval #STTP_ERROR_INVALID_STATE Not initialized
319 * @retval #STTP_ERROR_NOT_SUPPORTED_FEATURE Not supported feature
320 */
321 typedef int (* sttpe_set_punctuation_override)(bool value);
322
323 /**
324 * @brief Sets silence detection option.
325
326 * @param[in] value A value
327 *
328 * @return 0 on success, otherwise a negative error value
329 * @retval #STTP_ERROR_NONE Successful
330 * @retval #STTP_ERROR_INVALID_STATE Not initialized
331 * @retval #STTP_ERROR_NOT_SUPPORTED_FEATURE Not supported feature
332 */
333 typedef int (* sttpe_set_silence_detection)(bool value);
334
335 /**
336 * @brief Start recognition.
337 *
338 * @param[in] language A language. 
339 * @param[in] type A recognition type. (e.g. #STTP_RECOGNITION_TYPE_FREE, #STTP_RECOGNITION_TYPE_WEB_SEARCH)
340 * @param[in] user_data The user data to be passed to the callback function. 
341 *
342 * @return 0 on success, otherwise a negative error value
343 * @retval #STTP_ERROR_NONE Successful
344 * @retval #STTP_ERROR_INVALID_PARAMETER Invalid parameter
345 * @retval #STTP_ERROR_INVALID_STATE Invalid state
346 * @retval #STTP_ERROR_INVALID_LANGUAGE Invalid language
347 * @retval #STTP_ERROR_OPERATION_FAILED Operation failed
348 * @retval #STTP_ERROR_OUT_OF_NETWORK Out of network
349 *
350 * @pre The engine is not in recognition processing.
351 *
352 * @see sttpe_set_recording_data()
353 * @see sttpe_stop()
354 * @see sttpe_cancel()
355 */
356 typedef int (* sttpe_start)(const char* language, const char* type, void *user_data);
357
358 /**
359 * @brief Sets recording data for speech recognition from recorder. 
360 *
361 * @remark This function should be returned immediately after recording data copy. 
362
363 * @param[in] data A recording data
364 * @param[in] length A length of recording data
365 *
366 * @return 0 on success, otherwise a negative error value
367 * @retval #STTP_ERROR_NONE Successful
368 * @retval #STTP_ERROR_INVALID_PARAMETER Invalid parameter
369 * @retval #STTP_ERROR_INVALID_STATE Invalid state
370 *
371 * @pre sttpe_start() should succeed.
372 * @post If the engine supports partial result, it will invoke sttpe_partial_result_cb().
373 *
374 * @see sttpe_start()
375 * @see sttpe_cancel()
376 * @see sttpe_stop()
377 * @see sttpe_partial_result_cb()
378 */
379 typedef int (* sttpe_set_recording_data)(const void* data, unsigned int length);
380
381 /**
382 * @brief Stops to set recording data.
383 *
384 * @return 0 on success, otherwise a negative error value
385 * @retval #STTP_ERROR_NONE Successful
386 * @retval #STTP_ERROR_INVALID_STATE Invalid state
387 * @retval #STTP_ERROR_OPERATION_FAILED Operation failed
388 * @retval #STTP_ERROR_OUT_OF_NETWORK Out of network
389 *
390 * @pre sttpe_start() should succeed.
391 * @post After processing of the engine, sttpe_result_cb() is called.
392 *
393 * @see sttpe_start()
394 * @see sttpe_set_recording_data()
395 * @see sttpe_result_cb()
396 * @see sttpe_cancel()
397 */
398 typedef int (* sttpe_stop)(void);
399
400 /**
401 * @brief Cancels the recognition process.
402 *
403 * @return 0 on success, otherwise a negative error value.
404 * @retval #STTP_ERROR_NONE Successful.
405 * @retval #STTP_ERROR_INVALID_STATE Invalid state.
406 * @pre STT engine is in recognition processing.
407 *
408 * @see sttpe_start()
409 * @see sttpe_stop()
410 */
411 typedef int (* sttpe_cancel)(void);
412
413 /**
414 * @brief Convert file to text.
415 *
416 * @param[in] filepath Sound file. 
417 * @param[in] language A language. 
418 * @param[in] type A recognition type. (e.g. #STTP_RECOGNITION_TYPE_FREE, #STTP_RECOGNITION_TYPE_WEB_SEARCH)
419 * @param[in] user_data The user data to be passed to the callback function. 
420 *
421 * @return 0 on success, otherwise a negative error value
422 * @retval #STTP_ERROR_NONE Successful
423 * @retval #STTP_ERROR_INVALID_PARAMETER Invalid parameter
424 * @retval #STTP_ERROR_INVALID_STATE Invalid state
425 * @retval #STTP_ERROR_INVALID_LANGUAGE Invalid language
426 * @retval #STTP_ERROR_OPERATION_FAILED Operation failed
427 * @retval #STTP_ERROR_OUT_OF_NETWORK Out of network
428 *
429 * @pre The engine is not in recognition processing.
430 *
431 * @see sttpe_result_cb()
432 */
433 typedef int (* sttpe_start_file_recognition)(const char* filepath, const char* language, const char* type, void *user_data);
434
435 /**
436 * @brief Gets setting information of the engine.
437 *
438 * @param[in] callback A callback function.
439 * @param[in] user_data The user data to be passed to the callback function.
440 *
441 * @return 0 on success, otherwise a negative error value
442 * @retval #STTP_ERROR_NONE Successful
443 * @retval #STTP_ERROR_INVALID_PARAMETER Invalid parameter
444 * @retval #STTP_ERROR_INVALID_STATE Not initialized
445 * @retval #STTP_ERROR_OPERATION_FAILED Operation failed
446 *
447 * @post This function invokes sttpe_engine_setting_cb() repeatedly for getting engine settings. 
448 *
449 * @see sttpe_engine_setting_cb()
450 */
451 typedef int (* sttpe_foreach_engine_settings)(sttpe_engine_setting_cb callback, void* user_data);
452
453 /**
454 * @brief Set engine specific information. 
455 *
456 * @param[in] key A key
457 * @param[in] value A value
458 *
459 * @return 0 on success, otherwise a negative error value
460 * @retval #STTP_ERROR_NONE Successful
461 * @retval #STTP_ERROR_INVALID_PARAMETER Invalid parameter
462 * @retval #STTP_ERROR_INVALID_STATE Not initialized
463 * @retval #STTP_ERROR_OPERATION_FAILED Operation failed
464 *
465 * @see sttpe_foreach_engine_settings()
466 */
467 typedef int (* sttpe_set_engine_setting)(const char* key, const char* value);
468
469
470 /**
471 * @brief A structure of the engine functions.
472 */
473 typedef struct {
474         int size;                                               /**< Size of structure */    
475         int version;                                            /**< Version */
476         
477         sttpe_initialize                initialize;             /**< Initialize engine */
478         sttpe_deinitialize              deinitialize;           /**< Shutdown engine */
479
480         /* Get engine information */
481         sttpe_foreach_supported_languages foreach_langs;        /**< Foreach language list */
482         sttpe_is_valid_language         is_valid_lang;          /**< Check language */
483         sttpe_support_silence_detection support_silence;        /**< Get silence detection support */
484         sttpe_support_partial_result    support_partial_result; /**< Get partial result support */
485         sttpe_get_recording_format      get_audio_format;       /**< Get audio format */
486         
487         /* Set engine information */
488         sttpe_set_profanity_filter      set_profanity_filter;   /**< Set profanity filter */
489         sttpe_set_punctuation_override  set_punctuation;        /**< Set punctuation override */
490         sttpe_set_silence_detection     set_silence_detection;  /**< Set silence detection */
491
492         /* Control recognition */
493         sttpe_start                     start;                  /**< Start recognition */
494         sttpe_set_recording_data        set_recording;          /**< Set recording data */
495         sttpe_stop                      stop;                   /**< Shutdown function */
496         sttpe_cancel                    cancel;                 /**< Cancel recognition or cancel thinking */
497         sttpe_start_file_recognition    start_file_recognition; /**< Start file recognition */
498         
499         /* Engine setting */
500         sttpe_foreach_engine_settings   foreach_engine_settings;/**< Foreach engine specific info */
501         sttpe_set_engine_setting        set_engine_setting;     /**< Set engine specific info */
502 } sttpe_funcs_s;
503
504 /**
505 * @brief A structure of the daemon functions.
506 */
507 typedef struct {
508         int size;                                               /**< size */
509         int version;                                            /**< version */
510
511 } sttpd_funcs_s;
512
513 /**
514 * @brief Loads the engine. 
515 *
516 * @param[in] pdfuncs The daemon functions
517 * @param[out] pefuncs The engine functions
518 *
519 * @return This function returns zero on success, or negative with error code on failure
520 * @retval #STTP_ERROR_NONE Successful
521 * @retval #STTP_ERROR_INVALID_PARAMETER Invalid parameter
522 * @retval #STTP_ERROR_OPERATION_FAILED Operation failed
523 *
524 * @pre The sttp_get_engine_info() should be successful.
525 * @post The daemon calls engine functions of sttpe_funcs_s.
526 *
527 * @see sttp_get_engine_info()
528 * @see sttp_unload_engine()
529 */
530 int sttp_load_engine(sttpd_funcs_s* pdfuncs, sttpe_funcs_s* pefuncs);
531
532 /**
533 * @brief Unloads this engine by the daemon. 
534 *
535 * @pre The sttp_load_engine() should be successful.
536 *
537 * @see sttp_load_engine()
538 */
539 void sttp_unload_engine(void);
540
541 /**
542 * @brief Called to get the engine base information.
543 *
544 * @param[in] engine_uuid The engine id
545 * @param[in] engine_name The engine name
546 * @param[in] setting_ug_name The setting ug name
547 * @param[in] use_network @c true to need network @c false not to need network.
548 * @param[in] user_data The User data passed from sttp_get_engine_info()
549 *
550 * @pre sttp_get_engine_info() will invoke this callback. 
551 *
552 * @see sttp_get_engine_info()
553 */
554 typedef void (*sttpe_engine_info_cb)(const char* engine_uuid, const char* engine_name, const char* setting_ug_name, 
555                                      bool use_network, void* user_data);
556
557 /**
558 * @brief Gets the engine base information before the engine is loaded by the daemon. 
559 *
560 * @param[in] callback Callback function
561 * @param[in] user_data User data to be passed to the callback function
562 *
563 * @return This function returns zero on success, or negative with error code on failure
564 * @retval #STTP_ERROR_NONE Successful
565 * @retval #STTP_ERROR_INVALID_PARAMETER Invalid parameter
566 * @retval #STTP_ERROR_OPERATION_FAILED Operation failed
567 *
568 * @post This function invokes sttpe_engine_info_cb() for getting engine information.
569 *
570 * @see sttpe_engine_info_cb()
571 * @see sttp_load_engine()
572 */
573 int sttp_get_engine_info(sttpe_engine_info_cb callback, void* user_data);
574
575 #ifdef __cplusplus
576 }
577 #endif
578
579 /**
580  * @}@}
581  */
582  
583 #endif /* __STTP_H__ */