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>
20 #include <sound_manager.h>
23 #include "SttManager.h"
25 using namespace is::stt;
34 static sound_stream_info_h g_stream_info_h = NULL;
36 static inline const char *stt_state_str(stt_state_e cur) {
37 if (cur == STT_STATE_CREATED)
38 return (const char *) "STT_STATE_CREATED";
40 else if (cur == STT_STATE_READY)
41 return (const char *) "STT_STATE_READY";
43 else if (cur == STT_STATE_RECORDING)
44 return (const char *) "STT_STATE_RECORDING";
46 else if (cur == STT_STATE_PROCESSING)
47 return (const char *) "STT_STATE_PROCESSING";
50 return (const char *) "ABNORMAL CASE";
53 static void player_focus_state_cb(sound_stream_info_h stream_info, sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state,
54 sound_stream_focus_change_reason_e reason_for_change, int sound_behaviors, const char *extra_info, void *user_data)
58 SttManager::SttManager(ISttFeedback& feedback)
59 : ifeedback(feedback),
67 int ret = stt_create(&handle);
69 if(ret != STT_ERROR_NONE)
70 throw SttException(ret, ErrorString((stt_error_e)ret));
72 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_RECOGNITION, player_focus_state_cb, NULL, &g_stream_info_h);
73 if (SOUND_MANAGER_ERROR_NONE != ret) {
74 LOGW("Fail to create stream info. ret : %d", ret);
78 * Set default properties
83 catch(SttException &e) {
84 PRINTFUNC(DLOG_ERROR, "reason : %s", e.what());
89 SttManager::~SttManager() {
91 EnableFeedback(false);
96 catch(SttException &e) {
97 PRINTFUNC(DLOG_ERROR, "reason : %s", e.what());
103 sound_manager_destroy_stream_information(g_stream_info_h);
106 void SttManager::Prepare() {
108 * Prepare stt service.
111 int ret = stt_prepare(handle);
113 if(ret != STT_ERROR_NONE)
114 throw SttException(ret, ErrorString((stt_error_e)ret));
117 void SttManager::UnPrepare() {
119 * UnPrepare stt service.
122 int ret = stt_unprepare(handle);
124 if (ret != STT_ERROR_NONE)
125 throw SttException(ret, ErrorString((stt_error_e)ret));
128 void SttManager::Start() {
129 if(!Validate((int) READY)) {
130 throw SttException((int) STT_ERROR_INVALID_STATE, "INVALID STATE - !STT_STATE_READY");
133 PRINTFUNC(DLOG_DEBUG, "HERE");
141 asrtype = STT_RECOGNITION_TYPE_FREE_PARTIAL;
144 ret = sound_manager_acquire_focus(g_stream_info_h, (sound_stream_focus_mask_e)(SOUND_STREAM_FOCUS_FOR_PLAYBACK | SOUND_STREAM_FOCUS_FOR_RECORDING), SOUND_BEHAVIOR_NONE, NULL);
145 if (SOUND_MANAGER_ERROR_NONE != ret) {
146 LOGW("Fail to acquire playback or recording focus. ret : %d, stream handle : %p", ret, g_stream_info_h);
149 ret = stt_start(handle, language.c_str(), asrtype.c_str());
151 if(ret != STT_ERROR_NONE)
152 throw SttException(ret, ErrorString((stt_error_e)ret));
155 void SttManager::Stop() {
156 if(!Validate((int) RECORDING)) {
157 throw SttException((int) STT_ERROR_INVALID_STATE, "INVALID STATE - !STT_STATE_RECORDING");
164 int ret = stt_stop(handle);
166 if(ret != STT_ERROR_NONE)
167 throw SttException(ret, ErrorString((stt_error_e)ret));
170 void SttManager::Cancel() {
172 PRINTFUNC(DLOG_WARN, "iscancelled (%d)", iscancelled);
176 if(!Validate((int) (RECORDING|PROCESSING))) {
177 throw SttException((int) STT_ERROR_INVALID_STATE, "INVALID STATE - !(STT_STATE_RECORDING or STT_STATE_PROCESSING)");
181 * Cancel stt service (recording, processing)
184 int ret = stt_cancel(handle);
186 if(ret != STT_ERROR_NONE)
187 throw SttException(ret, ErrorString((stt_error_e)ret));
190 PRINTFUNC(DLOG_INFO, "iscancelled (%d)", iscancelled);
196 bool SttManager::Validate(int state) {
199 int ret = stt_get_state(handle, &cur);
200 if (ret != STT_ERROR_NONE) {
204 PRINTFUNC(DLOG_DEBUG, "validate state - %d", state);
205 PRINTFUNC(DLOG_DEBUG, "stt deamon state - %s",
206 cur == STT_STATE_CREATED ? "STT_STATE_CREATED" :
207 cur == STT_STATE_READY ? "STT_STATE_READY" :
208 cur == STT_STATE_RECORDING ? "STT_STATE_RECORDING" :
209 cur == STT_STATE_PROCESSING ? "STT_STATE_PROCESSING" : "ABNORMAL");
212 case STT_STATE_CREATED :
213 if (state & CREATE) return true;
215 case STT_STATE_READY :
216 if (state & READY) return true;
218 case STT_STATE_RECORDING :
219 if (state & RECORDING) return true;
221 case STT_STATE_PROCESSING :
222 if (state & PROCESSING) return true;
231 void SttManager::Initialize() {
232 /** Todo. add routine to intialize */
235 void SttManager::PrintResultState(stt_result_event_e result_type)
239 switch (result_type) {
240 case STT_RESULT_EVENT_FINAL_RESULT :
241 result = "STT_RESULT_EVENT_FINAL_RESULT";
243 case STT_RESULT_EVENT_PARTIAL_RESULT :
244 result = "STT_RESULT_EVENT_PARTIAL_RESULT";
246 case STT_RESULT_EVENT_ERROR :
247 result = "STT_RESULT_EVENT_ERROR";
253 PRINTFUNC(DLOG_INFO, "result type : %s", result.c_str());
256 void SttManager::on_result(
258 stt_result_event_e event,
263 PrintResultState(event);
266 PRINTFUNC(DLOG_ERROR, "user_data null");
267 throw SttException((int)STT_ERROR_INVALID_PARAMETER, "invalid self reference");
270 SttManager& manager = *((SttManager *) user_data);
272 std::vector<std::string> results;
274 PRINTFUNC(DLOG_INFO, "result size : %d, msg : %s", size, msg);
276 for (size_t i = 0; i < (size_t) size; i++) {
278 results.push_back(std::string(data[i]));
282 manager.ifeedback.OnResult(manager.asrtype, event, results, std::string(msg));
284 manager.ifeedback.OnResult(manager.asrtype, event, results, std::string(""));
288 void SttManager::PrintState(stt_state_e previous, stt_state_e current)
294 case STT_STATE_READY :
295 prev = "STT_STATE_READY";
297 case STT_STATE_CREATED :
298 prev = "STT_STATE_CREATED";
300 case STT_STATE_RECORDING :
301 prev = "STT_STATE_RECORDING";
303 case STT_STATE_PROCESSING :
304 prev = "STT_STATE_PROCESSING";
312 case STT_STATE_READY :
313 curr = "STT_STATE_READY";
315 case STT_STATE_CREATED :
316 curr = "STT_STATE_CREATED";
318 case STT_STATE_RECORDING :
319 curr = "STT_STATE_RECORDING";
321 case STT_STATE_PROCESSING :
322 curr = "STT_STATE_PROCESSING";
328 PRINTFUNC(DLOG_INFO, "previous: %s(%d), current: %s(%d)", prev.c_str(), previous, curr.c_str(), current);
331 void SttManager::on_state_changed(
333 stt_state_e previous,
336 PrintState(previous, current);
339 throw SttException((int)STT_ERROR_INVALID_PARAMETER, "invalid self reference");
341 SttManager& manager = *((SttManager *) user_data);
343 if (current== STT_STATE_READY) {
344 if (previous == STT_STATE_RECORDING ||
345 previous == STT_STATE_PROCESSING) {
349 if (previous == STT_STATE_CREATED) {
350 manager.EnableSilenceDetection();
351 manager.ifeedback.AutoStart();
352 } else if (previous == STT_STATE_RECORDING) {
354 std::vector<std::string> results;
355 manager.ifeedback.OnResult(manager.asrtype, STT_RESULT_EVENT_ERROR, results, msg);
357 manager.ifeedback.SttIdle();
359 } else if (current == STT_STATE_RECORDING) {
360 manager.ifeedback.SttRecording();
361 } else if (current == STT_STATE_PROCESSING) {
362 if (!manager.iscancelled) {
363 PRINTFUNC(DLOG_INFO, "iscancelled (%d)", manager.iscancelled);
364 manager.ifeedback.SttProcessing();
366 manager.iscancelled = false;
367 PRINTFUNC(DLOG_INFO, "iscancelled (%d)", manager.iscancelled);
372 void SttManager::PrintErrorState(stt_error_e reason)
377 case STT_ERROR_OUT_OF_MEMORY :
378 res = "STT_ERROR_OUT_OF_MEMORY";
380 case STT_ERROR_IO_ERROR :
381 res = "STT_ERROR_IO_ERROR";
383 case STT_ERROR_INVALID_PARAMETER :
384 res = "STT_ERROR_INVALID_PARAMETER";
386 case STT_ERROR_TIMED_OUT :
387 res = "STT_ERROR_TIMED_OUT";
389 case STT_ERROR_RECORDER_BUSY :
390 res = "STT_ERROR_RECORDER_BUSY";
392 case STT_ERROR_OUT_OF_NETWORK :
393 res = "STT_ERROR_OUT_OF_NETWORK";
395 case STT_ERROR_PERMISSION_DENIED :
396 res = "STT_ERROR_PERMISSION_DENIED";
398 case STT_ERROR_NOT_SUPPORTED :
399 res = "STT_ERROR_NOT_SUPPORTED";
401 case STT_ERROR_INVALID_STATE :
402 res = "STT_ERROR_INVALID_STATE";
404 case STT_ERROR_INVALID_LANGUAGE :
405 res = "STT_ERROR_INVALID_LANGUAGE";
407 case STT_ERROR_ENGINE_NOT_FOUND :
408 res = "STT_ERROR_ENGINE_NOT_FOUND";
410 case STT_ERROR_OPERATION_FAILED :
411 res = "STT_ERROR_OPERATION_FAILED";
413 case STT_ERROR_NOT_SUPPORTED_FEATURE :
414 res = "STT_ERROR_NOT_SUPPORTED_FEATURE";
417 res = "UNKNOWN ERROR REASON";
420 PRINTFUNC(DLOG_INFO, "Error reason %s(%d)", res.c_str(), reason);
423 void SttManager::on_error(
427 PRINTFUNC(DLOG_INFO, "stt-daemon error (%d)", reason);
430 throw SttException((int)STT_ERROR_INVALID_PARAMETER, "invalid self reference");
432 SttManager& manager = *((SttManager *) user_data);
433 manager.ifeedback.OnError(reason);
436 void SttManager::SetLanguage(std::string language) {
437 this->language = language;
440 void SttManager::EnableFeedback(bool enabled) {
441 int ret = STT_ERROR_NONE;
443 void *udata = static_cast<void *>(this);
446 ret = stt_set_recognition_result_cb(handle, on_result, udata);
447 if (STT_ERROR_NONE != ret)
448 throw SttException(ret, ErrorString((stt_error_e)ret));
450 ret = stt_set_error_cb(handle, on_error, udata);
451 if (STT_ERROR_NONE != ret)
452 throw SttException(ret, ErrorString((stt_error_e)ret));
454 ret = stt_set_state_changed_cb(handle, on_state_changed, udata);
455 if (STT_ERROR_NONE != ret)
456 throw SttException(ret, ErrorString((stt_error_e)ret));
458 ret = stt_unset_error_cb(handle);
459 if (STT_ERROR_NONE != ret)
460 throw SttException(ret, ErrorString((stt_error_e)ret));
462 ret = stt_unset_state_changed_cb(handle);
463 if (STT_ERROR_NONE != ret)
464 throw SttException(ret, ErrorString((stt_error_e)ret));
466 ret = stt_unset_recognition_result_cb(handle);
467 if (STT_ERROR_NONE != ret)
468 throw SttException(ret, ErrorString((stt_error_e)ret));
472 const char* SttManager::ErrorString(int ecode) {
473 const char *str = NULL;
476 case STT_ERROR_OUT_OF_MEMORY:
477 str = (const char *) "STT_ERROR_OUT_OF_MEMORY";
479 case STT_ERROR_IO_ERROR:
480 str = (const char *) "STT_ERROR_IO_ERROR";
482 case STT_ERROR_INVALID_PARAMETER:
483 str = (const char *) "STT_ERROR_INVALID_PARAMETER";
485 case STT_ERROR_TIMED_OUT:
486 str = (const char *) "STT_ERROR_TIMED_OUT";
488 case STT_ERROR_RECORDER_BUSY:
489 str = (const char *) "STT_ERROR_RECORDER_BUSY";
491 case STT_ERROR_OUT_OF_NETWORK:
492 str = (const char *) "STT_ERROR_OUT_OF_NETWORK";
494 case STT_ERROR_INVALID_STATE:
495 str = (const char *) " STT_ERROR_INVALID_STATE";
497 case STT_ERROR_INVALID_LANGUAGE:
498 str = (const char *) "STT_ERROR_INVALID_LANGUAGE";
500 case STT_ERROR_ENGINE_NOT_FOUND:
501 str = (const char *) "STT_ERROR_ENGINE_NOT_FOUND";
503 case STT_ERROR_OPERATION_FAILED:
504 str = (const char *) "STT_ERROR_OPERATION_FAILED";
506 case STT_ERROR_NOT_SUPPORTED_FEATURE:
507 str = (const char *) "STT_ERROR_NOT_SUPPORTED_FEATURE";
513 void SttManager::SoundFeedback() {
515 int is_sound_vibe = 0;
517 if (vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &is_sound)) {
518 PRINTFUNC(DLOG_ERROR, "get sound status failed.");
521 if (vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &is_sound_vibe)) {
522 PRINTFUNC(DLOG_ERROR, "get vibe status failed.");
525 if (is_sound || is_sound_vibe) {
526 stt_set_start_sound(handle, "/usr/share/ise-voice-input/audio/voice_start.wav");
527 stt_set_stop_sound(handle, "/usr/share/ise-voice-input/audio/voice_stop.wav");
529 stt_unset_start_sound(handle);
530 stt_unset_stop_sound(handle);
536 void SttManager::EnableSilenceDetection(bool enabled) {
537 stt_option_silence_detection_e s_option;
540 s_option = STT_OPTION_SILENCE_DETECTION_TRUE;
542 s_option = STT_OPTION_SILENCE_DETECTION_FALSE;
544 int ret = stt_set_silence_detection(handle, s_option);
545 if (STT_ERROR_NONE != ret) {
550 ErrorString((stt_error_e) ret));
552 PRINTFUNC(NO_PRINT, "stt_set_silence_detection Successful");
556 void SttManager::ReleaseSoundFocus()
558 int ret = sound_manager_release_focus(g_stream_info_h, (sound_stream_focus_mask_e)(SOUND_STREAM_FOCUS_FOR_PLAYBACK | SOUND_STREAM_FOCUS_FOR_RECORDING), SOUND_BEHAVIOR_NONE, NULL);
559 if (SOUND_MANAGER_ERROR_NONE != ret) {
560 LOGW("Fail to release playback or recording focus. ret : %d", ret);