2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <vconf-keys.h>
22 #include "SttManager.h"
24 using namespace is::stt;
33 static inline const char *stt_state_str(stt_state_e cur) {
34 if (cur == STT_STATE_CREATED)
35 return (const char *) "STT_STATE_CREATED";
37 else if (cur == STT_STATE_READY)
38 return (const char *) "STT_STATE_READY";
40 else if (cur == STT_STATE_RECORDING)
41 return (const char *) "STT_STATE_RECORDING";
43 else if (cur == STT_STATE_PROCESSING)
44 return (const char *) "STT_STATE_PROCESSING";
47 return (const char *) "ABNORMAL CASE";
50 SttManager::SttManager(ISttFeedback& feedback)
51 : ifeedback(feedback),
59 int ret = stt_create(&handle);
61 if(ret != STT_ERROR_NONE)
62 throw SttException(ret, ErrorString((stt_error_e)ret));
65 * Set default properties
70 catch(SttException &e) {
71 PRINTFUNC(DLOG_ERROR, "reason : %s", e.what());
76 SttManager::~SttManager() {
78 EnableFeedback(false);
83 catch(SttException &e) {
84 PRINTFUNC(DLOG_ERROR, "reason : %s", e.what());
89 void SttManager::Prepare() {
91 * Prepare stt service.
94 int ret = stt_prepare(handle);
96 if(ret != STT_ERROR_NONE)
97 throw SttException(ret, ErrorString((stt_error_e)ret));
100 void SttManager::UnPrepare() {
102 * UnPrepare stt service.
105 int ret = stt_unprepare(handle);
107 if (ret != STT_ERROR_NONE)
108 throw SttException(ret, ErrorString((stt_error_e)ret));
111 void SttManager::Start() {
112 if(!Validate((int) READY)) {
113 throw SttException((int) STT_ERROR_INVALID_STATE, "INVALID STATE - !STT_STATE_READY");
116 PRINTFUNC(DLOG_DEBUG, "HERE");
124 asrtype = STT_RECOGNITION_TYPE_FREE_PARTIAL;
126 ret = stt_start(handle, language.c_str(), asrtype.c_str());
128 if(ret != STT_ERROR_NONE)
129 throw SttException(ret, ErrorString((stt_error_e)ret));
132 void SttManager::Stop() {
133 if(!Validate((int) RECORDING)) {
134 throw SttException((int) STT_ERROR_INVALID_STATE, "INVALID STATE - !STT_STATE_RECORDING");
141 int ret = stt_stop(handle);
143 if(ret != STT_ERROR_NONE)
144 throw SttException(ret, ErrorString((stt_error_e)ret));
147 void SttManager::Cancel() {
149 PRINTFUNC(DLOG_WARN, "iscancelled (%d)", iscancelled);
153 if(!Validate((int) (RECORDING|PROCESSING))) {
154 throw SttException((int) STT_ERROR_INVALID_STATE, "INVALID STATE - !(STT_STATE_RECORDING or STT_STATE_PROCESSING)");
158 * Cancel stt service (recording, processing)
161 int ret = stt_cancel(handle);
163 if(ret != STT_ERROR_NONE)
164 throw SttException(ret, ErrorString((stt_error_e)ret));
167 PRINTFUNC(DLOG_INFO, "iscancelled (%d)", iscancelled);
173 bool SttManager::Validate(int state) {
176 int ret = stt_get_state(handle, &cur);
177 if (ret != STT_ERROR_NONE) {
181 PRINTFUNC(DLOG_DEBUG, "validate state - %d", state);
182 PRINTFUNC(DLOG_DEBUG, "stt deamon state - %s",
183 cur == STT_STATE_CREATED ? "STT_STATE_CREATED" :
184 cur == STT_STATE_READY ? "STT_STATE_READY" :
185 cur == STT_STATE_RECORDING ? "STT_STATE_RECORDING" :
186 cur == STT_STATE_PROCESSING ? "STT_STATE_PROCESSING" : "ABNORMAL");
189 case STT_STATE_CREATED :
190 if (state & CREATE) return true;
192 case STT_STATE_READY :
193 if (state & READY) return true;
195 case STT_STATE_RECORDING :
196 if (state & RECORDING) return true;
198 case STT_STATE_PROCESSING :
199 if (state & PROCESSING) return true;
208 void SttManager::Initialize() {
209 /** Todo. add routine to intialize */
212 void SttManager::PrintResultState(stt_result_event_e result_type)
216 switch (result_type) {
217 case STT_RESULT_EVENT_FINAL_RESULT :
218 result = "STT_RESULT_EVENT_FINAL_RESULT";
220 case STT_RESULT_EVENT_PARTIAL_RESULT :
221 result = "STT_RESULT_EVENT_PARTIAL_RESULT";
223 case STT_RESULT_EVENT_ERROR :
224 result = "STT_RESULT_EVENT_ERROR";
230 PRINTFUNC(DLOG_INFO, "result type : %s", result.c_str());
233 void SttManager::on_result(
235 stt_result_event_e event,
240 PrintResultState(event);
243 PRINTFUNC(DLOG_ERROR, "user_data null");
244 throw SttException((int)STT_ERROR_INVALID_PARAMETER, "invalid self reference");
247 SttManager& manager = *((SttManager *) user_data);
249 std::vector<std::string> results;
251 PRINTFUNC(DLOG_INFO, "result size : %d, msg : %s", size, msg);
253 for (size_t i = 0; i < (size_t) size; i++) {
255 results.push_back(std::string(data[i]));
259 manager.ifeedback.OnResult(manager.asrtype, event, results, std::string(msg));
261 manager.ifeedback.OnResult(manager.asrtype, event, results, std::string(""));
265 void SttManager::PrintState(stt_state_e previous, stt_state_e current)
271 case STT_STATE_READY :
272 prev = "STT_STATE_READY";
274 case STT_STATE_CREATED :
275 prev = "STT_STATE_CREATED";
277 case STT_STATE_RECORDING :
278 prev = "STT_STATE_RECORDING";
280 case STT_STATE_PROCESSING :
281 prev = "STT_STATE_PROCESSING";
289 case STT_STATE_READY :
290 curr = "STT_STATE_READY";
292 case STT_STATE_CREATED :
293 curr = "STT_STATE_CREATED";
295 case STT_STATE_RECORDING :
296 curr = "STT_STATE_RECORDING";
298 case STT_STATE_PROCESSING :
299 curr = "STT_STATE_PROCESSING";
305 PRINTFUNC(DLOG_INFO, "previous: %s(%d), current: %s(%d)", prev.c_str(), previous, curr.c_str(), current);
308 void SttManager::on_state_changed(
310 stt_state_e previous,
313 PrintState(previous, current);
316 throw SttException((int)STT_ERROR_INVALID_PARAMETER, "invalid self reference");
318 SttManager& manager = *((SttManager *) user_data);
320 if (current== STT_STATE_READY) {
321 if (previous == STT_STATE_CREATED) {
322 manager.EnableSilenceDetection();
323 manager.ifeedback.AutoStart();
324 } else if (previous == STT_STATE_RECORDING) {
326 std::vector<std::string> results;
327 manager.ifeedback.OnResult(manager.asrtype, STT_RESULT_EVENT_ERROR, results, msg);
329 manager.ifeedback.SttIdle();
331 } else if (current == STT_STATE_RECORDING) {
332 manager.ifeedback.SttRecording();
333 } else if (current == STT_STATE_PROCESSING) {
334 if (!manager.iscancelled) {
335 PRINTFUNC(DLOG_INFO, "iscancelled (%d)", manager.iscancelled);
336 manager.ifeedback.SttProcessing();
338 manager.iscancelled = false;
339 PRINTFUNC(DLOG_INFO, "iscancelled (%d)", manager.iscancelled);
344 void SttManager::PrintErrorState(stt_error_e reason)
349 case STT_ERROR_OUT_OF_MEMORY :
350 res = "STT_ERROR_OUT_OF_MEMORY";
352 case STT_ERROR_IO_ERROR :
353 res = "STT_ERROR_IO_ERROR";
355 case STT_ERROR_INVALID_PARAMETER :
356 res = "STT_ERROR_INVALID_PARAMETER";
358 case STT_ERROR_TIMED_OUT :
359 res = "STT_ERROR_TIMED_OUT";
361 case STT_ERROR_RECORDER_BUSY :
362 res = "STT_ERROR_RECORDER_BUSY";
364 case STT_ERROR_OUT_OF_NETWORK :
365 res = "STT_ERROR_OUT_OF_NETWORK";
367 case STT_ERROR_PERMISSION_DENIED :
368 res = "STT_ERROR_PERMISSION_DENIED";
370 case STT_ERROR_NOT_SUPPORTED :
371 res = "STT_ERROR_NOT_SUPPORTED";
373 case STT_ERROR_INVALID_STATE :
374 res = "STT_ERROR_INVALID_STATE";
376 case STT_ERROR_INVALID_LANGUAGE :
377 res = "STT_ERROR_INVALID_LANGUAGE";
379 case STT_ERROR_ENGINE_NOT_FOUND :
380 res = "STT_ERROR_ENGINE_NOT_FOUND";
382 case STT_ERROR_OPERATION_FAILED :
383 res = "STT_ERROR_OPERATION_FAILED";
385 case STT_ERROR_NOT_SUPPORTED_FEATURE :
386 res = "STT_ERROR_NOT_SUPPORTED_FEATURE";
389 res = "UNKNOWN ERROR REASON";
392 PRINTFUNC(DLOG_INFO, "Error reason %s(%d)", res.c_str(), reason);
395 void SttManager::on_error(
399 PRINTFUNC(DLOG_INFO, "stt-daemon error (%d)", reason);
402 throw SttException((int)STT_ERROR_INVALID_PARAMETER, "invalid self reference");
404 SttManager& manager = *((SttManager *) user_data);
405 manager.ifeedback.OnError(reason);
408 void SttManager::SetLanguage(std::string language) {
409 this->language = language;
412 void SttManager::EnableFeedback(bool enabled) {
413 int ret = STT_ERROR_NONE;
415 void *udata = static_cast<void *>(this);
418 ret = stt_set_recognition_result_cb(handle, on_result, udata);
419 if (STT_ERROR_NONE != ret)
420 throw SttException(ret, ErrorString((stt_error_e)ret));
422 ret = stt_set_error_cb(handle, on_error, udata);
423 if (STT_ERROR_NONE != ret)
424 throw SttException(ret, ErrorString((stt_error_e)ret));
426 ret = stt_set_state_changed_cb(handle, on_state_changed, udata);
427 if (STT_ERROR_NONE != ret)
428 throw SttException(ret, ErrorString((stt_error_e)ret));
430 ret = stt_unset_error_cb(handle);
431 if (STT_ERROR_NONE != ret)
432 throw SttException(ret, ErrorString((stt_error_e)ret));
434 ret = stt_unset_state_changed_cb(handle);
435 if (STT_ERROR_NONE != ret)
436 throw SttException(ret, ErrorString((stt_error_e)ret));
438 ret = stt_unset_recognition_result_cb(handle);
439 if (STT_ERROR_NONE != ret)
440 throw SttException(ret, ErrorString((stt_error_e)ret));
444 const char* SttManager::ErrorString(int ecode) {
445 const char *str = NULL;
448 case STT_ERROR_OUT_OF_MEMORY:
449 str = (const char *) "STT_ERROR_OUT_OF_MEMORY";
451 case STT_ERROR_IO_ERROR:
452 str = (const char *) "STT_ERROR_IO_ERROR";
454 case STT_ERROR_INVALID_PARAMETER:
455 str = (const char *) "STT_ERROR_INVALID_PARAMETER";
457 case STT_ERROR_TIMED_OUT:
458 str = (const char *) "STT_ERROR_TIMED_OUT";
460 case STT_ERROR_RECORDER_BUSY:
461 str = (const char *) "STT_ERROR_RECORDER_BUSY";
463 case STT_ERROR_OUT_OF_NETWORK:
464 str = (const char *) "STT_ERROR_OUT_OF_NETWORK";
466 case STT_ERROR_INVALID_STATE:
467 str = (const char *) " STT_ERROR_INVALID_STATE";
469 case STT_ERROR_INVALID_LANGUAGE:
470 str = (const char *) "STT_ERROR_INVALID_LANGUAGE";
472 case STT_ERROR_ENGINE_NOT_FOUND:
473 str = (const char *) "STT_ERROR_ENGINE_NOT_FOUND";
475 case STT_ERROR_OPERATION_FAILED:
476 str = (const char *) "STT_ERROR_OPERATION_FAILED";
478 case STT_ERROR_NOT_SUPPORTED_FEATURE:
479 str = (const char *) "STT_ERROR_NOT_SUPPORTED_FEATURE";
485 void SttManager::SoundFeedback() {
487 int is_sound_vibe = 0;
489 if (vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &is_sound)) {
490 PRINTFUNC(DLOG_ERROR, "get sound status failed.");
493 if (vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &is_sound_vibe)) {
494 PRINTFUNC(DLOG_ERROR, "get vibe status failed.");
497 if (is_sound || is_sound_vibe) {
498 stt_set_start_sound(handle, "/usr/share/ise-voice-input/audio/voice_start.wav");
499 stt_set_stop_sound(handle, "/usr/share/ise-voice-input/audio/voice_stop.wav");
501 stt_unset_start_sound(handle);
502 stt_unset_stop_sound(handle);
508 void SttManager::EnableSilenceDetection(bool enabled) {
509 stt_option_silence_detection_e s_option;
512 s_option = STT_OPTION_SILENCE_DETECTION_TRUE;
514 s_option = STT_OPTION_SILENCE_DETECTION_FALSE;
516 int ret = stt_set_silence_detection(handle, s_option);
517 if (STT_ERROR_NONE != ret) {
522 ErrorString((stt_error_e) ret));
524 PRINTFUNC(NO_PRINT, "stt_set_silence_detection Successful");