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";
39 else if (cur == STT_STATE_READY)
40 return (const char *) "STT_STATE_READY";
41 else if (cur == STT_STATE_RECORDING)
42 return (const char *) "STT_STATE_RECORDING";
43 else if (cur == STT_STATE_PROCESSING)
44 return (const char *) "STT_STATE_PROCESSING";
46 return (const char *) "ABNORMAL CASE";
49 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,
50 sound_stream_focus_change_reason_e reason_for_change, int sound_behavior, const char *extra_info, void *user_data)
54 SttManager::SttManager(ISttFeedback& feedback)
55 : ifeedback(feedback),
63 int ret = stt_create(&handle);
65 if (ret != STT_ERROR_NONE)
66 throw SttException(ret, ErrorString((stt_error_e)ret));
68 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_RECOGNITION, player_focus_state_cb, NULL, &g_stream_info_h);
69 if (SOUND_MANAGER_ERROR_NONE != ret) {
70 LOGW("Fail to create stream info. ret : %d", ret);
74 * Set default properties
79 catch(SttException &e) {
80 PRINTFUNC(DLOG_ERROR, "reason : %s", e.what());
85 SttManager::~SttManager() {
87 EnableFeedback(false);
92 catch(SttException &e) {
93 PRINTFUNC(DLOG_ERROR, "reason : %s", e.what());
100 sound_manager_destroy_stream_information(g_stream_info_h);
103 void SttManager::Prepare() {
105 * Prepare stt service.
108 int ret = stt_prepare(handle);
110 if (ret != STT_ERROR_NONE)
111 throw SttException(ret, ErrorString((stt_error_e)ret));
114 void SttManager::UnPrepare() {
116 * UnPrepare stt service.
119 int ret = stt_unprepare(handle);
121 if (ret != STT_ERROR_NONE)
122 throw SttException(ret, ErrorString((stt_error_e)ret));
125 void SttManager::Start() {
126 if (!Validate((int) READY)) {
127 throw SttException((int) STT_ERROR_INVALID_STATE, "INVALID STATE - !STT_STATE_READY");
130 PRINTFUNC(DLOG_DEBUG, "HERE");
138 asrtype = STT_RECOGNITION_TYPE_FREE_PARTIAL;
141 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);
142 if (SOUND_MANAGER_ERROR_NONE != ret) {
143 LOGW("Fail to acquire playback or recording focus. ret : %d, stream handle : %p", ret, g_stream_info_h);
146 ret = stt_start(handle, language.c_str(), asrtype.c_str());
148 if (ret != STT_ERROR_NONE)
149 throw SttException(ret, ErrorString((stt_error_e)ret));
152 void SttManager::Stop() {
153 if (!Validate((int) RECORDING)) {
154 throw SttException((int) STT_ERROR_INVALID_STATE, "INVALID STATE - !STT_STATE_RECORDING");
161 int ret = stt_stop(handle);
163 if (ret != STT_ERROR_NONE)
164 throw SttException(ret, ErrorString((stt_error_e)ret));
167 void SttManager::Cancel() {
169 PRINTFUNC(DLOG_WARN, "iscancelled (%d)", iscancelled);
173 if (!Validate((int) (RECORDING|PROCESSING))) {
174 throw SttException((int) STT_ERROR_INVALID_STATE, "INVALID STATE - !(STT_STATE_RECORDING or STT_STATE_PROCESSING)");
178 * Cancel stt service (recording, processing)
181 int ret = stt_cancel(handle);
183 if (ret != STT_ERROR_NONE)
184 throw SttException(ret, ErrorString((stt_error_e)ret));
187 PRINTFUNC(DLOG_INFO, "iscancelled (%d)", iscancelled);
192 bool SttManager::Validate(int state) {
195 int ret = stt_get_state(handle, &cur);
196 if (ret != STT_ERROR_NONE) {
200 PRINTFUNC(DLOG_DEBUG, "validate state - %d", state);
201 PRINTFUNC(DLOG_DEBUG, "stt deamon state - %s",
202 cur == STT_STATE_CREATED ? "STT_STATE_CREATED" :
203 cur == STT_STATE_READY ? "STT_STATE_READY" :
204 cur == STT_STATE_RECORDING ? "STT_STATE_RECORDING" :
205 cur == STT_STATE_PROCESSING ? "STT_STATE_PROCESSING" : "ABNORMAL");
208 case STT_STATE_CREATED:
209 if (state & CREATE) return true;
211 case STT_STATE_READY:
212 if (state & READY) return true;
214 case STT_STATE_RECORDING:
215 if (state & RECORDING) return true;
217 case STT_STATE_PROCESSING:
218 if (state & PROCESSING) return true;
227 void SttManager::Initialize() {
228 /** Todo. add routine to intialize */
231 void SttManager::PrintResultState(stt_result_event_e result_type)
235 switch (result_type) {
236 case STT_RESULT_EVENT_FINAL_RESULT:
237 result = "STT_RESULT_EVENT_FINAL_RESULT";
239 case STT_RESULT_EVENT_PARTIAL_RESULT:
240 result = "STT_RESULT_EVENT_PARTIAL_RESULT";
242 case STT_RESULT_EVENT_ERROR:
243 result = "STT_RESULT_EVENT_ERROR";
249 PRINTFUNC(DLOG_INFO, "result type : %s", result.c_str());
252 void SttManager::on_result(
254 stt_result_event_e event,
259 PrintResultState(event);
262 PRINTFUNC(DLOG_ERROR, "user_data null");
263 throw SttException((int)STT_ERROR_INVALID_PARAMETER, "invalid self reference");
266 SttManager& manager = *((SttManager *) user_data);
268 std::vector<std::string> results;
270 PRINTFUNC(DLOG_INFO, "result size : %d, msg : %s", size, msg);
272 for (size_t i = 0; i < (size_t) size; i++) {
274 results.push_back(std::string(data[i]));
278 manager.ifeedback.OnResult(manager.asrtype, event, results, std::string(msg));
280 manager.ifeedback.OnResult(manager.asrtype, event, results, std::string(""));
284 void SttManager::PrintState(stt_state_e previous, stt_state_e current)
290 case STT_STATE_READY :
291 prev = "STT_STATE_READY";
293 case STT_STATE_CREATED :
294 prev = "STT_STATE_CREATED";
296 case STT_STATE_RECORDING :
297 prev = "STT_STATE_RECORDING";
299 case STT_STATE_PROCESSING :
300 prev = "STT_STATE_PROCESSING";
308 case STT_STATE_READY :
309 curr = "STT_STATE_READY";
311 case STT_STATE_CREATED :
312 curr = "STT_STATE_CREATED";
314 case STT_STATE_RECORDING :
315 curr = "STT_STATE_RECORDING";
317 case STT_STATE_PROCESSING :
318 curr = "STT_STATE_PROCESSING";
324 PRINTFUNC(DLOG_INFO, "previous: %s(%d), current: %s(%d)", prev.c_str(), previous, curr.c_str(), current);
327 void SttManager::on_state_changed(
329 stt_state_e previous,
332 PrintState(previous, current);
335 throw SttException((int)STT_ERROR_INVALID_PARAMETER, "invalid self reference");
337 SttManager& manager = *((SttManager *) user_data);
339 if (current== STT_STATE_READY) {
340 if (previous == STT_STATE_RECORDING ||
341 previous == STT_STATE_PROCESSING) {
345 if (previous == STT_STATE_CREATED) {
346 manager.EnableSilenceDetection();
347 manager.ifeedback.AutoStart();
348 } else if (previous == STT_STATE_RECORDING) {
350 std::vector<std::string> results;
351 manager.ifeedback.OnResult(manager.asrtype, STT_RESULT_EVENT_ERROR, results, msg);
353 manager.ifeedback.SttIdle();
355 } else if (current == STT_STATE_RECORDING) {
356 manager.ifeedback.SttRecording();
357 } else if (current == STT_STATE_PROCESSING) {
358 if (!manager.iscancelled) {
359 PRINTFUNC(DLOG_INFO, "iscancelled (%d)", manager.iscancelled);
360 manager.ifeedback.SttProcessing();
362 manager.iscancelled = false;
363 PRINTFUNC(DLOG_INFO, "iscancelled (%d)", manager.iscancelled);
368 void SttManager::PrintErrorState(stt_error_e reason)
373 case STT_ERROR_OUT_OF_MEMORY:
374 res = "STT_ERROR_OUT_OF_MEMORY";
376 case STT_ERROR_IO_ERROR:
377 res = "STT_ERROR_IO_ERROR";
379 case STT_ERROR_INVALID_PARAMETER:
380 res = "STT_ERROR_INVALID_PARAMETER";
382 case STT_ERROR_TIMED_OUT:
383 res = "STT_ERROR_TIMED_OUT";
385 case STT_ERROR_RECORDER_BUSY:
386 res = "STT_ERROR_RECORDER_BUSY";
388 case STT_ERROR_OUT_OF_NETWORK:
389 res = "STT_ERROR_OUT_OF_NETWORK";
391 case STT_ERROR_PERMISSION_DENIED:
392 res = "STT_ERROR_PERMISSION_DENIED";
394 case STT_ERROR_NOT_SUPPORTED:
395 res = "STT_ERROR_NOT_SUPPORTED";
397 case STT_ERROR_INVALID_STATE:
398 res = "STT_ERROR_INVALID_STATE";
400 case STT_ERROR_INVALID_LANGUAGE:
401 res = "STT_ERROR_INVALID_LANGUAGE";
403 case STT_ERROR_ENGINE_NOT_FOUND:
404 res = "STT_ERROR_ENGINE_NOT_FOUND";
406 case STT_ERROR_OPERATION_FAILED:
407 res = "STT_ERROR_OPERATION_FAILED";
409 case STT_ERROR_NOT_SUPPORTED_FEATURE:
410 res = "STT_ERROR_NOT_SUPPORTED_FEATURE";
413 res = "UNKNOWN ERROR REASON";
416 PRINTFUNC(DLOG_INFO, "Error reason %s(%d)", res.c_str(), reason);
419 void SttManager::on_error(
423 PRINTFUNC(DLOG_INFO, "stt-daemon error (%d)", reason);
426 throw SttException((int)STT_ERROR_INVALID_PARAMETER, "invalid self reference");
428 SttManager& manager = *((SttManager *) user_data);
429 manager.ifeedback.OnError(reason);
432 void SttManager::SetLanguage(std::string language) {
433 this->language = language;
436 void SttManager::EnableFeedback(bool enabled) {
437 int ret = STT_ERROR_NONE;
439 void *udata = static_cast<void *>(this);
442 ret = stt_set_recognition_result_cb(handle, on_result, udata);
443 if (STT_ERROR_NONE != ret)
444 throw SttException(ret, ErrorString((stt_error_e)ret));
446 ret = stt_set_error_cb(handle, on_error, udata);
447 if (STT_ERROR_NONE != ret)
448 throw SttException(ret, ErrorString((stt_error_e)ret));
450 ret = stt_set_state_changed_cb(handle, on_state_changed, udata);
451 if (STT_ERROR_NONE != ret)
452 throw SttException(ret, ErrorString((stt_error_e)ret));
454 ret = stt_unset_error_cb(handle);
455 if (STT_ERROR_NONE != ret)
456 throw SttException(ret, ErrorString((stt_error_e)ret));
458 ret = stt_unset_state_changed_cb(handle);
459 if (STT_ERROR_NONE != ret)
460 throw SttException(ret, ErrorString((stt_error_e)ret));
462 ret = stt_unset_recognition_result_cb(handle);
463 if (STT_ERROR_NONE != ret)
464 throw SttException(ret, ErrorString((stt_error_e)ret));
468 const char* SttManager::ErrorString(int ecode) {
469 const char *str = NULL;
472 case STT_ERROR_OUT_OF_MEMORY:
473 str = (const char *) "STT_ERROR_OUT_OF_MEMORY";
475 case STT_ERROR_IO_ERROR:
476 str = (const char *) "STT_ERROR_IO_ERROR";
478 case STT_ERROR_INVALID_PARAMETER:
479 str = (const char *) "STT_ERROR_INVALID_PARAMETER";
481 case STT_ERROR_TIMED_OUT:
482 str = (const char *) "STT_ERROR_TIMED_OUT";
484 case STT_ERROR_RECORDER_BUSY:
485 str = (const char *) "STT_ERROR_RECORDER_BUSY";
487 case STT_ERROR_OUT_OF_NETWORK:
488 str = (const char *) "STT_ERROR_OUT_OF_NETWORK";
490 case STT_ERROR_INVALID_STATE:
491 str = (const char *) " STT_ERROR_INVALID_STATE";
493 case STT_ERROR_INVALID_LANGUAGE:
494 str = (const char *) "STT_ERROR_INVALID_LANGUAGE";
496 case STT_ERROR_ENGINE_NOT_FOUND:
497 str = (const char *) "STT_ERROR_ENGINE_NOT_FOUND";
499 case STT_ERROR_OPERATION_FAILED:
500 str = (const char *) "STT_ERROR_OPERATION_FAILED";
502 case STT_ERROR_NOT_SUPPORTED_FEATURE:
503 str = (const char *) "STT_ERROR_NOT_SUPPORTED_FEATURE";
509 void SttManager::SoundFeedback() {
511 int is_sound_vibe = 0;
513 if (vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &is_sound)) {
514 PRINTFUNC(DLOG_ERROR, "get sound status failed.");
517 if (vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &is_sound_vibe)) {
518 PRINTFUNC(DLOG_ERROR, "get vibe status failed.");
521 if (is_sound || is_sound_vibe) {
522 stt_set_start_sound(handle, "/usr/share/ise-voice-input/audio/voice_start.wav");
523 stt_set_stop_sound(handle, "/usr/share/ise-voice-input/audio/voice_stop.wav");
525 stt_unset_start_sound(handle);
526 stt_unset_stop_sound(handle);
530 void SttManager::EnableSilenceDetection(bool enabled) {
531 stt_option_silence_detection_e s_option;
534 s_option = STT_OPTION_SILENCE_DETECTION_TRUE;
536 s_option = STT_OPTION_SILENCE_DETECTION_FALSE;
538 int ret = stt_set_silence_detection(handle, s_option);
539 if (STT_ERROR_NONE != ret) {
544 ErrorString((stt_error_e) ret));
546 PRINTFUNC(NO_PRINT, "stt_set_silence_detection Successful");
550 void SttManager::ReleaseSoundFocus()
552 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);
553 if (SOUND_MANAGER_ERROR_NONE != ret) {
554 LOGW("Fail to release playback or recording focus. ret : %d", ret);