mirror of
https://github.com/signalwire/freeswitch.git
synced 2026-07-24 05:02:10 +00:00
[Core] Add partial Asr support
This commit is contained in:
@@ -68,6 +68,7 @@ typedef struct {
|
||||
char *grammar;
|
||||
char *channel_uuid;
|
||||
switch_vad_t *vad;
|
||||
int partial;
|
||||
} test_asr_t;
|
||||
|
||||
|
||||
@@ -268,12 +269,17 @@ static switch_status_t test_asr_get_results(switch_asr_handle_t *ah, char **resu
|
||||
}
|
||||
|
||||
if (switch_test_flag(context, ASRFLAG_RESULT)) {
|
||||
int is_partial = context->partial-- > 0 ? 1 : 0;
|
||||
|
||||
*resultstr = switch_mprintf("{\"grammar\": \"%s\", \"text\": \"%s\", \"confidence\": %f}", context->grammar, context->result_text, context->result_confidence);
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(context->channel_uuid), SWITCH_LOG_ERROR, "Result: %s\n", *resultstr);
|
||||
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(context->channel_uuid), SWITCH_LOG_NOTICE, "%sResult: %s\n", is_partial ? "Partial " : "Final ", *resultstr);
|
||||
|
||||
status = SWITCH_STATUS_SUCCESS;
|
||||
if (is_partial) {
|
||||
status = SWITCH_STATUS_MORE_DATA;
|
||||
} else {
|
||||
status = SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
} else if (switch_test_flag(context, ASRFLAG_NOINPUT_TIMEOUT)) {
|
||||
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(context->channel_uuid), SWITCH_LOG_DEBUG, "Result: NO INPUT\n");
|
||||
|
||||
@@ -361,6 +367,9 @@ static void test_asr_text_param(switch_asr_handle_t *ah, char *param, const char
|
||||
} else if (!strcasecmp("confidence", param) && fval >= 0.0) {
|
||||
context->result_confidence = fval;
|
||||
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(context->channel_uuid), SWITCH_LOG_DEBUG, "confidence = %f\n", fval);
|
||||
} else if (!strcasecmp("partial", param) && switch_true(val)) {
|
||||
context->partial = 3;
|
||||
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(context->channel_uuid), SWITCH_LOG_DEBUG, "partial = %d\n", context->partial);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -1055,7 +1055,7 @@ SWITCH_DECLARE(char *) CoreSession::playAndDetectSpeech(char *file, char *engine
|
||||
|
||||
char *result = NULL;
|
||||
|
||||
switch_status_t status = switch_ivr_play_and_detect_speech(session, file, engine, grammar, &result, 0, NULL);
|
||||
switch_status_t status = switch_ivr_play_and_detect_speech(session, file, engine, grammar, &result, 0, ap);
|
||||
if (status == SWITCH_STATUS_SUCCESS) {
|
||||
// good
|
||||
} else if (status == SWITCH_STATUS_GENERR) {
|
||||
@@ -1063,12 +1063,12 @@ SWITCH_DECLARE(char *) CoreSession::playAndDetectSpeech(char *file, char *engine
|
||||
} else if (status == SWITCH_STATUS_NOT_INITALIZED) {
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "ASR INIT ERROR\n");
|
||||
} else {
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "ERROR\n");
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "ERROR status = %d\n", status);
|
||||
}
|
||||
|
||||
end_allow_threads();
|
||||
|
||||
return result; // remeber to free me
|
||||
return result ? strdup(result) : NULL; // remeber to free me
|
||||
}
|
||||
|
||||
SWITCH_DECLARE(void) CoreSession::say(const char *tosay, const char *module_name, const char *say_type, const char *say_method, const char *say_gender)
|
||||
|
||||
+31
-14
@@ -4696,8 +4696,16 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_bind_dtmf_meta_session(switch_core_se
|
||||
typedef struct {
|
||||
int done;
|
||||
char *result;
|
||||
switch_input_args_t *original_args;
|
||||
} play_and_detect_speech_state_t;
|
||||
|
||||
static void deliver_asr_event(switch_core_session_t *session, switch_event_t *event, switch_input_args_t *args)
|
||||
{
|
||||
if (args && args->input_callback) {
|
||||
args->input_callback(session, (void *)event, SWITCH_INPUT_TYPE_EVENT, args->buf, args->buflen);
|
||||
}
|
||||
}
|
||||
|
||||
static switch_status_t play_and_detect_input_callback(switch_core_session_t *session, void *input, switch_input_type_t input_type, void *data, unsigned int len)
|
||||
{
|
||||
play_and_detect_speech_state_t *state = (play_and_detect_speech_state_t *)data;
|
||||
@@ -4708,7 +4716,10 @@ static switch_status_t play_and_detect_input_callback(switch_core_session_t *ses
|
||||
event = (switch_event_t *)input;
|
||||
if (event->event_id == SWITCH_EVENT_DETECTED_SPEECH) {
|
||||
const char *speech_type = switch_event_get_header(event, "Speech-Type");
|
||||
|
||||
if (!zstr(speech_type)) {
|
||||
deliver_asr_event(session, event, state->original_args);
|
||||
|
||||
if (!strcasecmp(speech_type, "detected-speech")) {
|
||||
const char *result;
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "(%s) DETECTED SPEECH\n", switch_channel_get_name(channel));
|
||||
@@ -4718,8 +4729,11 @@ static switch_status_t play_and_detect_input_callback(switch_core_session_t *ses
|
||||
} else {
|
||||
state->result = "";
|
||||
}
|
||||
state->original_args = NULL;
|
||||
state->done = PLAY_AND_DETECT_DONE_RECOGNIZING;
|
||||
return SWITCH_STATUS_BREAK;
|
||||
} else if (!strcasecmp(speech_type, "detected-partial-speech")) {
|
||||
// ok
|
||||
} else if (!strcasecmp(speech_type, "begin-speaking")) {
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "(%s) START OF SPEECH\n", switch_channel_get_name(channel));
|
||||
return SWITCH_STATUS_BREAK;
|
||||
@@ -4727,6 +4741,8 @@ static switch_status_t play_and_detect_input_callback(switch_core_session_t *ses
|
||||
state->done = PLAY_AND_DETECT_DONE_RECOGNIZING;
|
||||
state->result = "";
|
||||
return SWITCH_STATUS_BREAK;
|
||||
} else {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "unhandled speech type %s\n", speech_type);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4765,7 +4781,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_and_detect_speech(switch_core_se
|
||||
switch_status_t status = SWITCH_STATUS_FALSE;
|
||||
int recognizing = 0;
|
||||
switch_input_args_t myargs = { 0 };
|
||||
play_and_detect_speech_state_t state = { 0, "" };
|
||||
play_and_detect_speech_state_t state = { 0, "", NULL };
|
||||
switch_channel_t *channel = switch_core_session_get_channel(session);
|
||||
|
||||
arg_recursion_check_start(args);
|
||||
@@ -4776,10 +4792,6 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_and_detect_speech(switch_core_se
|
||||
|
||||
if (!input_timeout) input_timeout = 5000;
|
||||
|
||||
if (!args) {
|
||||
args = &myargs;
|
||||
}
|
||||
|
||||
/* start speech detection */
|
||||
if ((status = switch_ivr_detect_speech(session, mod_name, grammar, "", NULL, NULL)) != SWITCH_STATUS_SUCCESS) {
|
||||
/* map SWITCH_STATUS_FALSE to SWITCH_STATUS_GENERR to indicate grammar load failed
|
||||
@@ -4792,12 +4804,19 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_and_detect_speech(switch_core_se
|
||||
recognizing = 1;
|
||||
|
||||
/* play the prompt, looking for detection result */
|
||||
args->input_callback = play_and_detect_input_callback;
|
||||
args->buf = &state;
|
||||
args->buflen = sizeof(state);
|
||||
status = switch_ivr_play_file(session, NULL, file, args);
|
||||
|
||||
if (args->dmachine && switch_ivr_dmachine_last_ping(args->dmachine) != SWITCH_STATUS_SUCCESS) {
|
||||
if (args) {
|
||||
state.original_args = args;
|
||||
myargs.dmachine = args->dmachine;
|
||||
}
|
||||
|
||||
myargs.input_callback = play_and_detect_input_callback;
|
||||
myargs.buf = &state;
|
||||
myargs.buflen = sizeof(state);
|
||||
|
||||
status = switch_ivr_play_file(session, NULL, file, &myargs);
|
||||
|
||||
if (args && args->dmachine && switch_ivr_dmachine_last_ping(args->dmachine) != SWITCH_STATUS_SUCCESS) {
|
||||
state.done |= PLAY_AND_DETECT_DONE;
|
||||
goto done;
|
||||
}
|
||||
@@ -4812,9 +4831,9 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_and_detect_speech(switch_core_se
|
||||
switch_ivr_detect_speech_start_input_timers(session);
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "(%s) WAITING FOR RESULT\n", switch_channel_get_name(channel));
|
||||
while (!state.done && switch_channel_ready(channel)) {
|
||||
status = switch_ivr_sleep(session, input_timeout, SWITCH_FALSE, args);
|
||||
status = switch_ivr_sleep(session, input_timeout, SWITCH_FALSE, &myargs);
|
||||
|
||||
if (args->dmachine && switch_ivr_dmachine_last_ping(args->dmachine) != SWITCH_STATUS_SUCCESS) {
|
||||
if (args && args->dmachine && switch_ivr_dmachine_last_ping(args->dmachine) != SWITCH_STATUS_SUCCESS) {
|
||||
state.done |= PLAY_AND_DETECT_DONE;
|
||||
goto done;
|
||||
}
|
||||
@@ -4826,8 +4845,6 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_and_detect_speech(switch_core_se
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
done:
|
||||
if (recognizing && !(state.done & PLAY_AND_DETECT_DONE_RECOGNIZING)) {
|
||||
switch_ivr_pause_detect_speech(session);
|
||||
|
||||
+31
-18
@@ -3192,8 +3192,16 @@ typedef struct {
|
||||
char terminator;
|
||||
switch_time_t last_digit_time;
|
||||
switch_bool_t is_speech;
|
||||
switch_input_args_t *original_args;
|
||||
} switch_collect_input_state_t;
|
||||
|
||||
static void deliver_asr_event(switch_core_session_t *session, switch_event_t *event, switch_input_args_t *args)
|
||||
{
|
||||
if (args && args->input_callback) {
|
||||
args->input_callback(session, (void *)event, SWITCH_INPUT_TYPE_EVENT, args->buf, args->buflen);
|
||||
}
|
||||
}
|
||||
|
||||
static switch_status_t switch_collect_input_callback(switch_core_session_t *session, void *input, switch_input_type_t input_type, void *data, unsigned int len)
|
||||
{
|
||||
switch_collect_input_state_t *state = (switch_collect_input_state_t *)data;
|
||||
@@ -3209,13 +3217,15 @@ static switch_status_t switch_collect_input_callback(switch_core_session_t *sess
|
||||
|
||||
if (zstr(speech_type)) return SWITCH_STATUS_SUCCESS;
|
||||
|
||||
deliver_asr_event(session, event, state->original_args);
|
||||
|
||||
if (!strcasecmp(speech_type, "detected-speech")) {
|
||||
const char *result = switch_event_get_body(event);
|
||||
|
||||
/* stop waiting for speech */
|
||||
switch_set_flag(state, SWITCH_COLLECT_INPUT_SPEECH_DONE);
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "(%s) DETECTED SPEECH\n", switch_channel_get_name(channel));
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "(%s) DETECTED SPEECH %s\n", switch_channel_get_name(channel), speech_type);
|
||||
|
||||
if (!zstr(result)) {
|
||||
state->recognition_result = cJSON_Parse(result);
|
||||
@@ -3230,15 +3240,12 @@ static switch_status_t switch_collect_input_callback(switch_core_session_t *sess
|
||||
}
|
||||
}
|
||||
return SWITCH_STATUS_BREAK;
|
||||
}
|
||||
|
||||
if (!strcasecmp("closed", speech_type)) {
|
||||
} else if (!strcasecmp(speech_type, "detected-partial-speech")) {
|
||||
} else if (!strcasecmp("closed", speech_type)) {
|
||||
/* stop waiting for speech */
|
||||
switch_set_flag(state, SWITCH_COLLECT_INPUT_SPEECH_DONE);
|
||||
return SWITCH_STATUS_BREAK;
|
||||
}
|
||||
|
||||
if (!strcasecmp(speech_type, "begin-speaking")) {
|
||||
} else if (!strcasecmp(speech_type, "begin-speaking")) {
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "(%s) START OF SPEECH\n", switch_channel_get_name(channel));
|
||||
state->is_speech = SWITCH_TRUE;
|
||||
|
||||
@@ -3246,6 +3253,8 @@ static switch_status_t switch_collect_input_callback(switch_core_session_t *sess
|
||||
/* barge in on prompt */
|
||||
return SWITCH_STATUS_BREAK;
|
||||
}
|
||||
} else {
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Unhandled Speech-Type %s\n", speech_type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3360,10 +3369,6 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_and_collect_input(switch_core_se
|
||||
}
|
||||
}
|
||||
|
||||
if (!args) {
|
||||
args = &myargs;
|
||||
}
|
||||
|
||||
/* start speech recognition, if enabled */
|
||||
if (recognizer_grammar && recognizer_mod_name) {
|
||||
if ((status = switch_ivr_detect_speech(session, recognizer_mod_name, recognizer_grammar, "", NULL, NULL)) != SWITCH_STATUS_SUCCESS) {
|
||||
@@ -3380,14 +3385,22 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_and_collect_input(switch_core_se
|
||||
}
|
||||
|
||||
/* play the prompt, looking for input result */
|
||||
args->input_callback = switch_collect_input_callback;
|
||||
args->buf = &state;
|
||||
args->buflen = sizeof(state);
|
||||
|
||||
if (args) {
|
||||
state.original_args = args;
|
||||
myargs.dmachine = args->dmachine;
|
||||
}
|
||||
|
||||
myargs.input_callback = switch_collect_input_callback;
|
||||
myargs.buf = &state;
|
||||
myargs.buflen = sizeof(state);
|
||||
|
||||
|
||||
switch_set_flag(&state, SWITCH_COLLECT_INPUT_PROMPT);
|
||||
status = switch_ivr_play_file(session, NULL, prompt, args);
|
||||
status = switch_ivr_play_file(session, NULL, prompt, &myargs);
|
||||
switch_clear_flag(&state, SWITCH_COLLECT_INPUT_PROMPT);
|
||||
|
||||
if (args->dmachine && switch_ivr_dmachine_last_ping(args->dmachine) != SWITCH_STATUS_SUCCESS) {
|
||||
if (args && args->dmachine && switch_ivr_dmachine_last_ping(args->dmachine) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_set_flag(&state, SWITCH_COLLECT_INPUT_DIGITS_DONE);
|
||||
switch_set_flag(&state, SWITCH_COLLECT_INPUT_SPEECH_DONE);
|
||||
status = SWITCH_STATUS_SUCCESS;
|
||||
@@ -3412,9 +3425,9 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_and_collect_input(switch_core_se
|
||||
while ((!switch_test_flag(&state, SWITCH_COLLECT_INPUT_DIGITS_DONE) || !switch_test_flag(&state, SWITCH_COLLECT_INPUT_SPEECH_DONE))
|
||||
&& switch_channel_ready(channel)) {
|
||||
|
||||
status = switch_ivr_sleep(session, sleep_time, SWITCH_FALSE, args);
|
||||
status = switch_ivr_sleep(session, sleep_time, SWITCH_FALSE, &myargs);
|
||||
|
||||
if (args->dmachine && switch_ivr_dmachine_last_ping(args->dmachine) != SWITCH_STATUS_SUCCESS) {
|
||||
if (args && args->dmachine && switch_ivr_dmachine_last_ping(args->dmachine) != SWITCH_STATUS_SUCCESS) {
|
||||
// dmachine done
|
||||
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "(%s) DMACHINE DONE\n", switch_channel_get_name(channel));
|
||||
switch_set_flag(&state, SWITCH_COLLECT_INPUT_DIGITS_DONE);
|
||||
|
||||
Reference in New Issue
Block a user