#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.
*
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);
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.
*
throw SttException(ret, ErrorString((stt_error_e)ret));
}
-
-
void SttManager::UnPrepare() {
-
/**
* UnPrepare stt service.
*
*/
int ret = stt_unprepare(handle);
- if(ret != STT_ERROR_NONE)
+ if (ret != STT_ERROR_NONE)
throw SttException(ret, ErrorString((stt_error_e)ret));
}
-
-
void SttManager::Start() {
-
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);
}
+ 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)) {
throw SttException((int) STT_ERROR_INVALID_STATE, "INVALID STATE - !STT_STATE_RECORDING");
}
throw SttException(ret, ErrorString((stt_error_e)ret));
}
-
-
void SttManager::Cancel() {
-
if(iscancelled) {
PRINTFUNC(DLOG_WARN, "iscancelled (%d)", iscancelled);
return;
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;
}
switch(cur) {
case STT_STATE_CREATED :
- if(state & CREATE) return true;
+ if (state & CREATE) return true;
break;
case STT_STATE_READY :
- if(state & READY) return true;
+ if (state & READY) return true;
break;
case STT_STATE_RECORDING :
- if(state & RECORDING) return true;
+ if (state & RECORDING) return true;
break;
case STT_STATE_PROCESSING :
- if(state & PROCESSING) return true;
+ if (state & PROCESSING) return true;
break;
default :
break;
return false;
}
-
-
void SttManager::Initialize() {
-
/** Todo. add routine to intialize */
}
result = "UNKNOWN";
break;
}
-
PRINTFUNC(DLOG_INFO, "result type : %s", result.c_str());
-
}
void SttManager::on_result(
int size,
const char* msg,
void *user_data) {
-
PrintResultState(event);
- if(!user_data) {
+ if (!user_data) {
PRINTFUNC(DLOG_ERROR, "user_data null");
throw SttException((int)STT_ERROR_INVALID_PARAMETER, "invalid self reference");
}
PRINTFUNC(DLOG_INFO, "result size : %d, msg : %s", size, msg);
- for(size_t i = 0; i < (size_t) size; i++) {
- if(data[i]) {
+ for (size_t i = 0; i < (size_t) size; i++) {
+ if (data[i]) {
results.push_back(std::string(data[i]));
}
- if(msg)
+ 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);
}
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);
- 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);
}
-
-
void SttManager::SetLanguage(std::string language) {
-
this->language = language;
}
-
-
void SttManager::EnableFeedback(bool enabled) {
-
int ret = STT_ERROR_NONE;
void *udata = static_cast<void *>(this);
- if(enabled) {
+ if (enabled) {
ret = stt_set_recognition_result_cb(handle, on_result, udata);
- if(STT_ERROR_NONE != ret)
+ 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)
+ 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)
+ if (STT_ERROR_NONE != ret)
throw SttException(ret, ErrorString((stt_error_e)ret));
- }
- else {
+ } else {
ret = stt_unset_error_cb(handle);
- if(STT_ERROR_NONE != ret)
+ 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)
+ 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)
+ if (STT_ERROR_NONE != ret)
throw SttException(ret, ErrorString((stt_error_e)ret));
}
}
-
-
const char* SttManager::ErrorString(int ecode) {
-
const char *str = NULL;
switch (ecode) {
str = (const char *) "STT_ERROR_NOT_SUPPORTED_FEATURE";
break;
}
-
return str;
}
-
-
void SttManager::SoundFeedback() {
-
int is_sound = 0;
int is_sound_vibe = 0;
- if(vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &is_sound)) {
+ 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)) {
+ 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) {
+ 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 {
+ } 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);
+ }
+}