diff --git a/src/include/switch_module_interfaces.h b/src/include/switch_module_interfaces.h index 9d137e63a6..4e9d2902ea 100644 --- a/src/include/switch_module_interfaces.h +++ b/src/include/switch_module_interfaces.h @@ -313,6 +313,8 @@ struct switch_file_handle { void *private_info; int64_t pos; switch_buffer_t *audio_buffer; + uint32_t thresh; + uint32_t silence_hits; }; /*! \brief Abstract interface to an asr module */ diff --git a/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c b/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c index 657a644b61..ea6f189a49 100644 --- a/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c +++ b/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c @@ -828,7 +828,7 @@ static JSBool session_recordfile(JSContext *cx, JSObject *obj, uintN argc, jsval int len = 0; switch_input_callback_function_t dtmf_func = NULL; struct input_callback_state cb_state = {0}; - switch_file_handle_t fh; + switch_file_handle_t fh = {0}; JSFunction *function; int32 limit = 0; @@ -860,9 +860,21 @@ static JSBool session_recordfile(JSContext *cx, JSObject *obj, uintN argc, jsval if (argc > 3) { JS_ValueToInt32(cx, argv[3], &limit); } + + if (argc > 4) { + int32_t thresh; + JS_ValueToInt32(cx, argv[4], &thresh); + fh.thresh = thresh; + } + + if (argc > 5) { + int32_t silence_hits; + JS_ValueToInt32(cx, argv[5], &silence_hits); + fh.silence_hits = silence_hits; + } } - memset(&fh, 0, sizeof(fh)); + cb_state.extra = &fh; cb_state.ret = BOOLEAN_TO_JSVAL( JS_FALSE ); switch_ivr_record_file(jss->session, &fh, file_name, dtmf_func, bp, len, limit); @@ -925,7 +937,7 @@ static JSBool session_streamfile(JSContext *cx, JSObject *obj, uintN argc, jsval int len = 0; switch_input_callback_function_t dtmf_func = NULL; struct input_callback_state cb_state = {0}; - switch_file_handle_t fh; + switch_file_handle_t fh = {0}; JSFunction *function; channel = switch_core_session_get_channel(jss->session); @@ -963,12 +975,10 @@ static JSBool session_streamfile(JSContext *cx, JSObject *obj, uintN argc, jsval if (argc > 4) { int32 samps; - uint32_t pos = 0; JS_ValueToInt32(cx, argv[4], &samps); - switch_core_file_seek(&fh, &pos, samps, SEEK_CUR); + fh.samples = samps; } - memset(&fh, 0, sizeof(fh)); cb_state.extra = &fh; cb_state.ret = BOOLEAN_TO_JSVAL( JS_FALSE ); switch_ivr_play_file(jss->session, &fh, file_name, timer_name, dtmf_func, bp, len); diff --git a/src/switch_event.c b/src/switch_event.c index bfce256124..e6e53aebe1 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -606,7 +606,9 @@ SWITCH_DECLARE(switch_status_t) switch_event_serialize(switch_event_t *event, ch *str = NULL; - if (!(buf = malloc(blocksize))) { + dlen = blocksize * 2; + + if (!(buf = malloc(dlen))) { return SWITCH_STATUS_MEMERR; } @@ -615,9 +617,7 @@ SWITCH_DECLARE(switch_status_t) switch_event_serialize(switch_event_t *event, ch return SWITCH_STATUS_MEMERR; } - dlen = blocksize; - - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "hit serialze!.\n"); + //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "hit serialze!.\n"); for (hp = event->headers; hp; hp = hp->next) { /* * grab enough memory to store 3x the string (url encode takes one char and turns it into %XX) @@ -628,7 +628,7 @@ SWITCH_DECLARE(switch_status_t) switch_event_serialize(switch_event_t *event, ch */ if(encode_len < ((strlen(hp->value) * 3) + 1)) { char* tmp; - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Allocing %d was %d.\n", ((strlen(hp->value) * 3) + 1), encode_len); + //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Allocing %d was %d.\n", ((strlen(hp->value) * 3) + 1), encode_len); /* we can use realloc for initial alloc as well, if encode_buf is zero it treats it as a malloc */ if(!(tmp = realloc(encode_buf, ((strlen(hp->value) * 3) + 1)))) { /* oh boy, ram's gone, give back what little we grabbed and bail */ diff --git a/src/switch_ivr.c b/src/switch_ivr.c index 08065956d9..986f37ae80 100644 --- a/src/switch_ivr.c +++ b/src/switch_ivr.c @@ -428,6 +428,12 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se start = time(NULL); } + if (fh->thresh) { + if (!fh->silence_hits) { + fh->silence_hits = 20; + } + } + while(switch_channel_ready(channel)) { switch_size_t len; switch_event_t *event; @@ -477,6 +483,26 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se if (!SWITCH_READ_ACCEPTABLE(status)) { break; } + + if (fh->thresh) { + int16_t *fdata = (int16_t *) read_frame->data; + uint32_t samples = read_frame->datalen / sizeof(*fdata); + uint32_t score, count = 0, j = 0; + double energy = 0; + + for (count = 0; count < samples; count++) { + energy += abs(fdata[j]); + j += read_codec->implementation->number_of_channels; + } + + score = energy / samples; + if (score < fh->thresh) { + if (!--fh->silence_hits) { + break; + } + } + } + if (!switch_test_flag(fh, SWITCH_FILE_PAUSE)) { len = (switch_size_t) read_frame->datalen / 2; switch_core_file_write(fh, read_frame->data, &len); @@ -983,6 +1009,10 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess write_frame.data = abuf; write_frame.buflen = sizeof(abuf); + if (fh->samples) { + uint32_t pos = 0; + switch_core_file_seek(fh, &pos, fh->samples, SEEK_CUR); + } if (switch_core_file_get_string(fh, SWITCH_AUDIO_COL_STR_TITLE, &p) == SWITCH_STATUS_SUCCESS) { title = (char *) switch_core_session_strdup(session, (char *)p);