#include <assert.h>
#include <vconf.h>
#include <vconf-keys.h>
+#include <sound_manager.h>
#include "Debug.h"
#include "SttManager.h"
CREATE = 0x1000
};
-static inline const char *stt_state_str(stt_state_e cur) {
+static sound_stream_info_h g_stream_info_h = NULL;
- if(cur == STT_STATE_CREATED)
+static inline const char *stt_state_str(stt_state_e cur) {
+ if (cur == STT_STATE_CREATED)
return (const char *) "STT_STATE_CREATED";
-
- else if(cur == STT_STATE_READY)
+ else if (cur == STT_STATE_READY)
return (const char *) "STT_STATE_READY";
-
- else if(cur == STT_STATE_RECORDING)
+ else if (cur == STT_STATE_RECORDING)
return (const char *) "STT_STATE_RECORDING";
-
- else if(cur == STT_STATE_PROCESSING)
+ else if (cur == STT_STATE_PROCESSING)
return (const char *) "STT_STATE_PROCESSING";
-
else
return (const char *) "ABNORMAL CASE";
}
+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,
+ sound_stream_focus_change_reason_e reason_for_change, int sound_behavior, const char *extra_info, void *user_data)
+{
+}
SttManager::SttManager(ISttFeedback& feedback)
: ifeedback(feedback),
iscancelled(false)
{
-
- try {
+ try {
/**
* Create stt handle.
*
*/
int ret = stt_create(&handle);
- if(ret != STT_ERROR_NONE)
+ if (ret != STT_ERROR_NONE)
throw SttException(ret, ErrorString((stt_error_e)ret));
+ ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_RECOGNITION, player_focus_state_cb, NULL, &g_stream_info_h);
+ if (SOUND_MANAGER_ERROR_NONE != ret) {
+ LOGW("Fail to create stream info. ret : %d", ret);
+ }
+
/**
* Set default properties
*
}
}
-
-
SttManager::~SttManager() {
-
try {
EnableFeedback(false);
}
catch(SttException &e) {
PRINTFUNC(DLOG_ERROR, "reason : %s", e.what());
+
stt_destroy(handle);
}
+
+ ReleaseSoundFocus();
+
+ sound_manager_destroy_stream_information(g_stream_info_h);
}
void SttManager::Prepare() {
-
/**
* Prepare stt service.
*
*/
int ret = stt_prepare(handle);
- if(ret != STT_ERROR_NONE)
+ if (ret != STT_ERROR_NONE)
throw SttException(ret, ErrorString((stt_error_e)ret));
}
-
-
void SttManager::UnPrepare() {
+ /**
+ * UnPrepare stt service.
+ *
+ */
+ int ret = stt_unprepare(handle);
- /**
- * UnPrepare stt service.
- *
- */
- int ret = stt_unprepare(handle);
-
- if(ret != STT_ERROR_NONE)
- throw SttException(ret, ErrorString((stt_error_e)ret));
+ if (ret != STT_ERROR_NONE)
+ throw SttException(ret, ErrorString((stt_error_e)ret));
}
-
-
void SttManager::Start() {
-
- if(!Validate((int) READY)) {
+ if (!Validate((int) READY)) {
throw SttException((int) STT_ERROR_INVALID_STATE, "INVALID STATE - !STT_STATE_READY");
}
asrtype = STT_RECOGNITION_TYPE_FREE_PARTIAL;
int ret;
- bool bval = false;
-
-// stt_is_samsung_asr(&bval);
-
- if( bval == true && !language.compare("en_GB")) {
- PRINTFUNC(DLOG_DEBUG, "en_GB requested, change to en_US");
- ret = stt_start(handle, "en_US", asrtype.c_str());
- } else {
- ret = stt_start(handle, language.c_str(), asrtype.c_str());
+ 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);
+ if (SOUND_MANAGER_ERROR_NONE != ret) {
+ LOGW("Fail to acquire playback or recording focus. ret : %d, stream handle : %p", ret, g_stream_info_h);
}
- if(ret != STT_ERROR_NONE)
+ ret = stt_start(handle, language.c_str(), asrtype.c_str());
+
+ if (ret != STT_ERROR_NONE)
throw SttException(ret, ErrorString((stt_error_e)ret));
}
-
-
void SttManager::Stop() {
-
- if(!Validate((int) RECORDING)) {
+ if (!Validate((int) RECORDING)) {
throw SttException((int) STT_ERROR_INVALID_STATE, "INVALID STATE - !STT_STATE_RECORDING");
}
*/
int ret = stt_stop(handle);
- if(ret != STT_ERROR_NONE)
+ if (ret != STT_ERROR_NONE)
throw SttException(ret, ErrorString((stt_error_e)ret));
}
-
-
void SttManager::Cancel() {
-
- if(iscancelled) {
+ if (iscancelled) {
PRINTFUNC(DLOG_WARN, "iscancelled (%d)", iscancelled);
return;
}
- if(!Validate((int) (RECORDING|PROCESSING))) {
+ if (!Validate((int) (RECORDING|PROCESSING))) {
throw SttException((int) STT_ERROR_INVALID_STATE, "INVALID STATE - !(STT_STATE_RECORDING or STT_STATE_PROCESSING)");
}
*/
int ret = stt_cancel(handle);
- if(ret != STT_ERROR_NONE)
+ if (ret != STT_ERROR_NONE)
throw SttException(ret, ErrorString((stt_error_e)ret));
iscancelled = true;
ifeedback.SttIdle();
}
-
bool SttManager::Validate(int state) {
-
stt_state_e cur;
int ret = stt_get_state(handle, &cur);
- if(ret != STT_ERROR_NONE) {
+ if (ret != STT_ERROR_NONE) {
return false;
}
cur == STT_STATE_PROCESSING ? "STT_STATE_PROCESSING" : "ABNORMAL");
switch(cur) {
- case STT_STATE_CREATED :
- if(state & CREATE) return true;
+ case STT_STATE_CREATED:
+ if (state & CREATE) return true;
break;
- case STT_STATE_READY :
- if(state & READY) return true;
+ case STT_STATE_READY:
+ if (state & READY) return true;
break;
- case STT_STATE_RECORDING :
- if(state & RECORDING) return true;
+ case STT_STATE_RECORDING:
+ if (state & RECORDING) return true;
break;
- case STT_STATE_PROCESSING :
- if(state & PROCESSING) return true;
+ case STT_STATE_PROCESSING:
+ if (state & PROCESSING) return true;
break;
- default :
+ default:
break;
}
return false;
}
-
-
void SttManager::Initialize() {
-
/** Todo. add routine to intialize */
}
std::string result;
switch (result_type) {
- case STT_RESULT_EVENT_FINAL_RESULT :
+ case STT_RESULT_EVENT_FINAL_RESULT:
result = "STT_RESULT_EVENT_FINAL_RESULT";
break;
- case STT_RESULT_EVENT_PARTIAL_RESULT :
+ case STT_RESULT_EVENT_PARTIAL_RESULT:
result = "STT_RESULT_EVENT_PARTIAL_RESULT";
break;
- case STT_RESULT_EVENT_ERROR :
+ case STT_RESULT_EVENT_ERROR:
result = "STT_RESULT_EVENT_ERROR";
break;
- default :
+ default:
result = "UNKNOWN";
break;
}
-
PRINTFUNC(DLOG_INFO, "result type : %s", result.c_str());
-
}
void SttManager::on_result(
- stt_h handle,
- stt_result_event_e event,
- const char** data,
- int size,
- const char* msg,
- void *user_data) {
-
- PrintResultState(event);
-
- if(!user_data) {
+ stt_h handle,
+ stt_result_event_e event,
+ const char** data,
+ int size,
+ const char* msg,
+ void *user_data) {
+ PrintResultState(event);
+
+ if (!user_data) {
PRINTFUNC(DLOG_ERROR, "user_data null");
throw SttException((int)STT_ERROR_INVALID_PARAMETER, "invalid self reference");
- }
+ }
- SttManager& manager = *((SttManager *) user_data);
+ SttManager& manager = *((SttManager *) user_data);
- std::vector<std::string> results;
+ std::vector<std::string> results;
- PRINTFUNC(DLOG_INFO, "result size : %d, msg : %s", size, msg);
+ PRINTFUNC(DLOG_INFO, "result size : %d, msg : %s", size, msg);
- for(size_t i = 0; i < (size_t) size; i++) {
- if(data[i]) {
- results.push_back(std::string(data[i]));
- }
-
- if(msg)
- manager.ifeedback.OnResult(manager.asrtype, event, results, std::string(msg));
- else
- manager.ifeedback.OnResult(manager.asrtype, event, results, std::string(""));
- }
+ for (size_t i = 0; i < (size_t) size; i++) {
+ if (data[i]) {
+ results.push_back(std::string(data[i]));
+ }
+ if (msg)
+ manager.ifeedback.OnResult(manager.asrtype, event, results, std::string(msg));
+ else
+ manager.ifeedback.OnResult(manager.asrtype, event, results, std::string(""));
+ }
}
void SttManager::PrintState(stt_state_e previous, stt_state_e current)
curr = "UNKNOWN";
break;
}
-
PRINTFUNC(DLOG_INFO, "previous: %s(%d), current: %s(%d)", prev.c_str(), previous, curr.c_str(), current);
-
}
void SttManager::on_state_changed(
stt_state_e previous,
stt_state_e current,
void *user_data) {
-
PrintState(previous, current);
- if(!user_data)
+ if (!user_data)
throw SttException((int)STT_ERROR_INVALID_PARAMETER, "invalid self reference");
SttManager& manager = *((SttManager *) user_data);
- if(current== STT_STATE_READY) {
- if(previous == STT_STATE_CREATED) {
+ if (current== STT_STATE_READY) {
+ if (previous == STT_STATE_RECORDING ||
+ previous == STT_STATE_PROCESSING) {
+ ReleaseSoundFocus();
+ }
+
+ if (previous == STT_STATE_CREATED) {
manager.EnableSilenceDetection();
manager.ifeedback.AutoStart();
- }
- else if(previous == STT_STATE_RECORDING) {
+ } else if (previous == STT_STATE_RECORDING) {
std::string msg;
std::vector<std::string> results;
manager.ifeedback.OnResult(manager.asrtype, STT_RESULT_EVENT_ERROR, results, msg);
- }
- else {
+ } else {
manager.ifeedback.SttIdle();
}
- }
- else if(current == STT_STATE_RECORDING) {
+ } else if (current == STT_STATE_RECORDING) {
manager.ifeedback.SttRecording();
- }
- else if(current == STT_STATE_PROCESSING) {
- if(!manager.iscancelled) {
+ } else if (current == STT_STATE_PROCESSING) {
+ if (!manager.iscancelled) {
PRINTFUNC(DLOG_INFO, "iscancelled (%d)", manager.iscancelled);
manager.ifeedback.SttProcessing();
- }
- else {
+ } else {
manager.iscancelled = false;
PRINTFUNC(DLOG_INFO, "iscancelled (%d)", manager.iscancelled);
}
std::string res;
switch (reason) {
- case STT_ERROR_OUT_OF_MEMORY :
+ case STT_ERROR_OUT_OF_MEMORY:
res = "STT_ERROR_OUT_OF_MEMORY";
break;
- case STT_ERROR_IO_ERROR :
+ case STT_ERROR_IO_ERROR:
res = "STT_ERROR_IO_ERROR";
break;
- case STT_ERROR_INVALID_PARAMETER :
+ case STT_ERROR_INVALID_PARAMETER:
res = "STT_ERROR_INVALID_PARAMETER";
break;
- case STT_ERROR_TIMED_OUT :
+ case STT_ERROR_TIMED_OUT:
res = "STT_ERROR_TIMED_OUT";
break;
- case STT_ERROR_RECORDER_BUSY :
+ case STT_ERROR_RECORDER_BUSY:
res = "STT_ERROR_RECORDER_BUSY";
break;
- case STT_ERROR_OUT_OF_NETWORK :
+ case STT_ERROR_OUT_OF_NETWORK:
res = "STT_ERROR_OUT_OF_NETWORK";
break;
- case STT_ERROR_PERMISSION_DENIED :
+ case STT_ERROR_PERMISSION_DENIED:
res = "STT_ERROR_PERMISSION_DENIED";
break;
- case STT_ERROR_NOT_SUPPORTED :
+ case STT_ERROR_NOT_SUPPORTED:
res = "STT_ERROR_NOT_SUPPORTED";
break;
- case STT_ERROR_INVALID_STATE :
+ case STT_ERROR_INVALID_STATE:
res = "STT_ERROR_INVALID_STATE";
break;
- case STT_ERROR_INVALID_LANGUAGE :
+ case STT_ERROR_INVALID_LANGUAGE:
res = "STT_ERROR_INVALID_LANGUAGE";
break;
- case STT_ERROR_ENGINE_NOT_FOUND :
+ case STT_ERROR_ENGINE_NOT_FOUND:
res = "STT_ERROR_ENGINE_NOT_FOUND";
break;
- case STT_ERROR_OPERATION_FAILED :
+ case STT_ERROR_OPERATION_FAILED:
res = "STT_ERROR_OPERATION_FAILED";
break;
- case STT_ERROR_NOT_SUPPORTED_FEATURE :
+ case STT_ERROR_NOT_SUPPORTED_FEATURE:
res = "STT_ERROR_NOT_SUPPORTED_FEATURE";
break;
- default :
+ default:
res = "UNKNOWN ERROR REASON";
break;
}
-
PRINTFUNC(DLOG_INFO, "Error reason %s(%d)", res.c_str(), reason);
-
}
-
void SttManager::on_error(
- stt_h handle,
- stt_error_e reason,
- void *user_data) {
-
- PRINTFUNC(DLOG_INFO, "stt-daemon error (%d)", reason);
+ stt_h handle,
+ stt_error_e reason,
+ void *user_data) {
+ PRINTFUNC(DLOG_INFO, "stt-daemon error (%d)", reason);
- if(!user_data)
+ if (!user_data)
throw SttException((int)STT_ERROR_INVALID_PARAMETER, "invalid self reference");
- SttManager& manager = *((SttManager *) user_data);
- manager.ifeedback.OnError(reason);
+ SttManager& manager = *((SttManager *) user_data);
+ manager.ifeedback.OnError(reason);
}
-
-
void SttManager::SetLanguage(std::string language) {
-
- this->language = language;
+ this->language = language;
}
-
-
void SttManager::EnableFeedback(bool enabled) {
+ int ret = STT_ERROR_NONE;
- int ret = STT_ERROR_NONE;
+ void *udata = static_cast<void *>(this);
- void *udata = static_cast<void *>(this);
-
- if(enabled) {
- ret = stt_set_recognition_result_cb(handle, on_result, udata);
- if(STT_ERROR_NONE != ret)
- throw SttException(ret, ErrorString((stt_error_e)ret));
+ if (enabled) {
+ ret = stt_set_recognition_result_cb(handle, on_result, udata);
+ if (STT_ERROR_NONE != ret)
+ throw SttException(ret, ErrorString((stt_error_e)ret));
- ret = stt_set_error_cb(handle, on_error, udata);
- if(STT_ERROR_NONE != ret)
- throw SttException(ret, ErrorString((stt_error_e)ret));
+ ret = stt_set_error_cb(handle, on_error, udata);
+ if (STT_ERROR_NONE != ret)
+ throw SttException(ret, ErrorString((stt_error_e)ret));
- ret = stt_set_state_changed_cb(handle, on_state_changed, udata);
- if(STT_ERROR_NONE != ret)
- throw SttException(ret, ErrorString((stt_error_e)ret));
- }
- else {
- ret = stt_unset_error_cb(handle);
- if(STT_ERROR_NONE != ret)
- throw SttException(ret, ErrorString((stt_error_e)ret));
+ ret = stt_set_state_changed_cb(handle, on_state_changed, udata);
+ if (STT_ERROR_NONE != ret)
+ throw SttException(ret, ErrorString((stt_error_e)ret));
+ } else {
+ ret = stt_unset_error_cb(handle);
+ if (STT_ERROR_NONE != ret)
+ throw SttException(ret, ErrorString((stt_error_e)ret));
- ret = stt_unset_state_changed_cb(handle);
- if(STT_ERROR_NONE != ret)
- throw SttException(ret, ErrorString((stt_error_e)ret));
+ ret = stt_unset_state_changed_cb(handle);
+ if (STT_ERROR_NONE != ret)
+ throw SttException(ret, ErrorString((stt_error_e)ret));
- ret = stt_unset_recognition_result_cb(handle);
- if(STT_ERROR_NONE != ret)
- throw SttException(ret, ErrorString((stt_error_e)ret));
- }
+ ret = stt_unset_recognition_result_cb(handle);
+ if (STT_ERROR_NONE != ret)
+ throw SttException(ret, ErrorString((stt_error_e)ret));
+ }
}
-
-
const char* SttManager::ErrorString(int ecode) {
+ const char *str = NULL;
- const char *str = NULL;
-
- switch (ecode) {
- case STT_ERROR_OUT_OF_MEMORY:
- str = (const char *) "STT_ERROR_OUT_OF_MEMORY";
- break;
- case STT_ERROR_IO_ERROR:
- str = (const char *) "STT_ERROR_IO_ERROR";
- break;
- case STT_ERROR_INVALID_PARAMETER:
- str = (const char *) "STT_ERROR_INVALID_PARAMETER";
- break;
- case STT_ERROR_TIMED_OUT:
- str = (const char *) "STT_ERROR_TIMED_OUT";
- break;
- case STT_ERROR_RECORDER_BUSY:
- str = (const char *) "STT_ERROR_RECORDER_BUSY";
- break;
- case STT_ERROR_OUT_OF_NETWORK:
- str = (const char *) "STT_ERROR_OUT_OF_NETWORK";
- break;
- case STT_ERROR_INVALID_STATE:
- str = (const char *) " STT_ERROR_INVALID_STATE";
- break;
- case STT_ERROR_INVALID_LANGUAGE:
- str = (const char *) "STT_ERROR_INVALID_LANGUAGE";
- break;
- case STT_ERROR_ENGINE_NOT_FOUND:
- str = (const char *) "STT_ERROR_ENGINE_NOT_FOUND";
- break;
- case STT_ERROR_OPERATION_FAILED:
- str = (const char *) "STT_ERROR_OPERATION_FAILED";
- break;
- case STT_ERROR_NOT_SUPPORTED_FEATURE:
- str = (const char *) "STT_ERROR_NOT_SUPPORTED_FEATURE";
- break;
- }
-
- return str;
+ switch (ecode) {
+ case STT_ERROR_OUT_OF_MEMORY:
+ str = (const char *) "STT_ERROR_OUT_OF_MEMORY";
+ break;
+ case STT_ERROR_IO_ERROR:
+ str = (const char *) "STT_ERROR_IO_ERROR";
+ break;
+ case STT_ERROR_INVALID_PARAMETER:
+ str = (const char *) "STT_ERROR_INVALID_PARAMETER";
+ break;
+ case STT_ERROR_TIMED_OUT:
+ str = (const char *) "STT_ERROR_TIMED_OUT";
+ break;
+ case STT_ERROR_RECORDER_BUSY:
+ str = (const char *) "STT_ERROR_RECORDER_BUSY";
+ break;
+ case STT_ERROR_OUT_OF_NETWORK:
+ str = (const char *) "STT_ERROR_OUT_OF_NETWORK";
+ break;
+ case STT_ERROR_INVALID_STATE:
+ str = (const char *) " STT_ERROR_INVALID_STATE";
+ break;
+ case STT_ERROR_INVALID_LANGUAGE:
+ str = (const char *) "STT_ERROR_INVALID_LANGUAGE";
+ break;
+ case STT_ERROR_ENGINE_NOT_FOUND:
+ str = (const char *) "STT_ERROR_ENGINE_NOT_FOUND";
+ break;
+ case STT_ERROR_OPERATION_FAILED:
+ str = (const char *) "STT_ERROR_OPERATION_FAILED";
+ break;
+ case STT_ERROR_NOT_SUPPORTED_FEATURE:
+ str = (const char *) "STT_ERROR_NOT_SUPPORTED_FEATURE";
+ break;
+ }
+ return str;
}
-
-
void SttManager::SoundFeedback() {
+ int is_sound = 0;
+ int is_sound_vibe = 0;
- int is_sound = 0;
- int is_sound_vibe = 0;
-
- if(vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &is_sound)) {
- PRINTFUNC(DLOG_ERROR, "get sound status failed.");
- }
-
- if(vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &is_sound_vibe)) {
- PRINTFUNC(DLOG_ERROR, "get vibe status failed.");
- }
-
- if(is_sound || is_sound_vibe) {
- stt_set_start_sound(handle, "/usr/share/ise-voice-input/audio/voice_start.wav");
- stt_set_stop_sound(handle, "/usr/share/ise-voice-input/audio/voice_stop.wav");
- }
- else {
- stt_unset_start_sound(handle);
- stt_unset_stop_sound(handle);
- }
-}
+ if (vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &is_sound)) {
+ PRINTFUNC(DLOG_ERROR, "get sound status failed.");
+ }
+ if (vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &is_sound_vibe)) {
+ PRINTFUNC(DLOG_ERROR, "get vibe status failed.");
+ }
+ if (is_sound || is_sound_vibe) {
+ stt_set_start_sound(handle, "/usr/share/ise-voice-input/audio/voice_start.wav");
+ stt_set_stop_sound(handle, "/usr/share/ise-voice-input/audio/voice_stop.wav");
+ } else {
+ stt_unset_start_sound(handle);
+ stt_unset_stop_sound(handle);
+ }
+}
void SttManager::EnableSilenceDetection(bool enabled) {
-
stt_option_silence_detection_e s_option;
- if(enabled)
+ if (enabled)
s_option = STT_OPTION_SILENCE_DETECTION_TRUE;
else
s_option = STT_OPTION_SILENCE_DETECTION_FALSE;
DLOG_ERROR,
"error(%d) = %s",
ret,
- ErrorString((stt_error_e) ret)
- );
- }
- else {
+ ErrorString((stt_error_e) ret));
+ } else {
PRINTFUNC(NO_PRINT, "stt_set_silence_detection Successful");
}
}
+void SttManager::ReleaseSoundFocus()
+{
+ 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);
+ if (SOUND_MANAGER_ERROR_NONE != ret) {
+ LOGW("Fail to release playback or recording focus. ret : %d", ret);
+ }
+}