diff --git a/src/include/switch_ivr.h b/src/include/switch_ivr.h index aaa0a5eeb9..fffa949bc3 100644 --- a/src/include/switch_ivr.h +++ b/src/include/switch_ivr.h @@ -314,6 +314,9 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_tone_detect_session(switch_core_sessi SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *session, switch_file_handle_t *fh, const char *file, switch_input_args_t *args); +SWITCH_DECLARE(switch_status_t) switch_ivr_wait_for_silence(switch_core_session_t *session, uint32_t thresh, uint32_t silence_hits, + uint32_t listen_hits, const char *file); + SWITCH_DECLARE(switch_status_t) switch_ivr_gentones(switch_core_session_t *session, char *script, int32_t loops, switch_input_args_t *args); /*! diff --git a/src/mod/applications/mod_dptools/mod_dptools.c b/src/mod/applications/mod_dptools/mod_dptools.c index d6320a6681..d734abacdd 100644 --- a/src/mod/applications/mod_dptools/mod_dptools.c +++ b/src/mod/applications/mod_dptools/mod_dptools.c @@ -2102,6 +2102,31 @@ SWITCH_STANDARD_APP(unhold_function) switch_ivr_unhold_uuid(switch_core_session_get_uuid(session)); } +#define WAIT_FOR_SILENCE_SYNTAX " []" +SWITCH_STANDARD_APP(wait_for_silence_function) +{ + char *argv[4] = { 0 }; + uint32_t thresh, silence_hits, listen_hits; + int argc; + char *lbuf = NULL; + + if (!switch_strlen_zero(data) && (lbuf = switch_core_session_strdup(session, data)) + && (argc = switch_separate_string(lbuf, ' ', argv, (sizeof(argv) / sizeof(argv[0])))) >= 3) { + thresh = atoi(argv[0]); + silence_hits = atoi(argv[1]); + listen_hits = atoi(argv[2]); + + if (thresh > 0 && silence_hits > 0 && listen_hits > 0) { + switch_ivr_wait_for_silence(session, thresh, silence_hits, listen_hits, argv[3]); + return; + } + + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Usage: %s\n", WAIT_FOR_SILENCE_SYNTAX); + +} + #define SPEAK_DESC "Speak text to a channel via the tts interface" #define DISPLACE_DESC "Displace audio from a file to the channels input" #define SESS_REC_DESC "Starts a background recording of the entire session" @@ -2226,6 +2251,8 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_dptools_load) SAF_SUPPORT_NOMEDIA); SWITCH_ADD_APP(app_interface, "say", "say", "say", say_function, SAY_SYNTAX, SAF_NONE); + SWITCH_ADD_APP(app_interface, "wait_for_silence", "wait_for_silence", "wait_for_silence", wait_for_silence_function, WAIT_FOR_SILENCE_SYNTAX, SAF_NONE); + SWITCH_ADD_DIALPLAN(dp_interface, "inline", inline_dialplan_hunt); /* indicate that the module should continue to be loaded */ diff --git a/src/switch_ivr_play_say.c b/src/switch_ivr_play_say.c index 42bd5f3686..e7bc645c5c 100644 --- a/src/switch_ivr_play_say.c +++ b/src/switch_ivr_play_say.c @@ -1195,6 +1195,125 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess return status; } +SWITCH_DECLARE(switch_status_t) switch_ivr_wait_for_silence(switch_core_session_t *session, uint32_t thresh, uint32_t silence_hits, uint32_t listen_hits, const char *file) +{ + uint32_t score, count = 0, j = 0; + double energy = 0; + switch_channel_t *channel = switch_core_session_get_channel(session); + int divisor = 0; + switch_codec_t *read_codec = switch_core_session_get_read_codec(session); + uint32_t org_silence_hits = silence_hits; + uint32_t channels; + switch_frame_t *read_frame; + switch_status_t status = SWITCH_STATUS_FALSE; + int16_t *data; + int listening = 0; + int countdown = 0; + switch_codec_t raw_codec = {0}; + int16_t *abuf = NULL; + switch_frame_t write_frame = {0}; + switch_file_handle_t fh = {0}; + + switch_assert(read_codec); + + if (file) { + if (switch_core_file_open(&fh, + file, + read_codec->implementation->number_of_channels, + read_codec->implementation->actual_samples_per_second, + SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) { + switch_core_session_reset(session, SWITCH_TRUE); + return SWITCH_STATUS_NOTFOUND; + } + switch_zmalloc(abuf, SWITCH_RECOMMENDED_BUFFER_SIZE); + write_frame.data = abuf; + write_frame.buflen = SWITCH_RECOMMENDED_BUFFER_SIZE; + } + + + if (switch_core_codec_init(&raw_codec, + "L16", + NULL, + read_codec->implementation->actual_samples_per_second, + read_codec->implementation->microseconds_per_frame / 1000, + 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, + NULL, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { + + status = SWITCH_STATUS_FALSE; + goto end; + } + + write_frame.codec = &raw_codec; + + divisor = read_codec->implementation->actual_samples_per_second / 8000; + channels = read_codec->implementation->number_of_channels; + + switch_core_session_set_read_codec(session, &raw_codec); + + while (switch_channel_ready(channel)) { + + status = switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0); + + if (!SWITCH_READ_ACCEPTABLE(status)) { + break; + } + + if (abuf) { + switch_size_t olen = raw_codec.implementation->samples_per_frame; + + if (switch_core_file_read(&fh, abuf, &olen) != SWITCH_STATUS_SUCCESS) { + break; + } + + write_frame.samples = olen; + write_frame.datalen = olen * sizeof(int16_t); + if ((status = switch_core_session_write_frame(session, &write_frame, SWITCH_IO_FLAG_NONE, 0)) != SWITCH_STATUS_SUCCESS) { + break; + } + } + + if (countdown) { + if (!--countdown) { + break; + } else { + continue; + } + } + + data = (int16_t *) read_frame->data; + + for (energy = 0, j = 0, count = 0; count < read_frame->samples; count++) { + energy += abs(data[j++]); + j += channels; + } + + score = (uint32_t) (energy / (read_frame->samples / divisor)); + + if (score >= thresh) { + listening++; + } + + if (listening > listen_hits && score < thresh) { + if (!--silence_hits) { + countdown = 25; + } + } else { + silence_hits = org_silence_hits; + } + } + + switch_core_session_set_read_codec(session, NULL); + switch_core_codec_destroy(&raw_codec); + + end: + + if (abuf) { + switch_core_file_close(&fh); + } + + return status; +} + SWITCH_DECLARE(switch_status_t) switch_ivr_read(switch_core_session_t *session, uint32_t min_digits, uint32_t max_digits,