From 6a405a23230abdf80c89621ee955698d4d1b5883 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Thu, 5 Feb 2009 19:53:13 +0000 Subject: [PATCH] sound_test app git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@11658 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_ivr.h | 1 + .../applications/mod_dptools/mod_dptools.c | 6 ++ src/switch_ivr.c | 85 +++++++++++++++++++ 3 files changed, 92 insertions(+) diff --git a/src/include/switch_ivr.h b/src/include/switch_ivr.h index a721104f50..baf0dbd581 100644 --- a/src/include/switch_ivr.h +++ b/src/include/switch_ivr.h @@ -794,6 +794,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_set_terminator(sw SWITCH_DECLARE(switch_say_method_t) switch_ivr_get_say_method_by_name(const char *name); SWITCH_DECLARE(switch_say_type_t) switch_ivr_get_say_type_by_name(const char *name); SWITCH_DECLARE(switch_status_t) switch_ivr_set_user(switch_core_session_t *session, const char *data); + SWITCH_DECLARE(switch_status_t) switch_ivr_sound_test(switch_core_session_t *session); /** @} */ SWITCH_END_EXTERN_C diff --git a/src/mod/applications/mod_dptools/mod_dptools.c b/src/mod/applications/mod_dptools/mod_dptools.c index bbe28176ea..ae41c4bc3d 100644 --- a/src/mod/applications/mod_dptools/mod_dptools.c +++ b/src/mod/applications/mod_dptools/mod_dptools.c @@ -912,6 +912,11 @@ SWITCH_STANDARD_APP(info_function) } } +SWITCH_STANDARD_APP(sound_test_function) +{ + switch_ivr_sound_test(session); +} + SWITCH_STANDARD_APP(event_function) { switch_event_t *event; @@ -2581,6 +2586,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_dptools_load) SWITCH_ADD_APP(app_interface, "log", "Logs to the logger", LOG_LONG_DESC, log_function, " ", SAF_SUPPORT_NOMEDIA); SWITCH_ADD_APP(app_interface, "info", "Display Call Info", "Display Call Info", info_function, "", SAF_SUPPORT_NOMEDIA); SWITCH_ADD_APP(app_interface, "event", "Fire an event", "Fire an event", event_function, "", SAF_SUPPORT_NOMEDIA); + SWITCH_ADD_APP(app_interface, "sound_test", "Analyze Audio", "Analyze Audio", sound_test_function, "", SAF_NONE); SWITCH_ADD_APP(app_interface, "export", "Export a channel variable across a bridge", EXPORT_LONG_DESC, export_function, "=", SAF_SUPPORT_NOMEDIA); SWITCH_ADD_APP(app_interface, "set", "Set a channel variable", SET_LONG_DESC, set_function, "=", SAF_SUPPORT_NOMEDIA); diff --git a/src/switch_ivr.c b/src/switch_ivr.c index 43e15d382a..194cb4bded 100644 --- a/src/switch_ivr.c +++ b/src/switch_ivr.c @@ -38,6 +38,91 @@ #include #include "stfu.h" +SWITCH_DECLARE(switch_status_t) switch_ivr_sound_test(switch_core_session_t *session) +{ + + switch_codec_implementation_t imp = {0}; + switch_codec_t codec = { 0 }; + int16_t peak = 0; + int16_t *data; + switch_frame_t *read_frame = NULL; + int i; + switch_channel_t *channel = switch_core_session_get_channel(session); + switch_status_t status = SWITCH_STATUS_SUCCESS; + int64_t global_total = 0, global_sum = 0, period_sum = 0; + int period_total = 0; + int period_avg = 0, global_avg = 0; + int avg = 0; + int period_len; + + switch_core_session_get_read_impl(session, &imp); + + period_len = imp.actual_samples_per_second / imp.samples_per_packet; + + if (switch_core_codec_init(&codec, + "L16", + NULL, + imp.samples_per_second, + imp.microseconds_per_packet / 1000, + imp.number_of_channels, + SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, + switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Codec Error L16@%uhz %u channels %dms\n", + imp.samples_per_second, imp.number_of_channels, imp.microseconds_per_packet / 1000); + return SWITCH_STATUS_FALSE; + } + + 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 (switch_test_flag(read_frame, SFF_CNG) || !read_frame->samples) { + continue; + } + + + data = (int16_t *) read_frame->data; + peak = 0; + avg = 0; + for (i = 0; i < read_frame->samples; i++) { + const int16_t s = abs(data[i]); + if (s > peak) { + peak = s; + } + avg += s; + } + + avg /= read_frame->samples; + + period_sum += peak; + global_sum += peak; + + global_total++; + period_total++; + + period_avg = period_sum / period_total; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, + "\npacket_avg=%d packet_peak=%d period_avg=%d global_avg=%d\n\n", avg, peak, period_avg, global_avg); + + if (period_total >= period_len) { + global_avg = global_sum / global_total; + period_total = 0; + period_sum = 0; + } + + } + + + switch_core_codec_destroy(&codec); + + return SWITCH_STATUS_SUCCESS; + +} + SWITCH_DECLARE(switch_status_t) switch_ivr_sleep(switch_core_session_t *session, uint32_t ms, switch_bool_t sync, switch_input_args_t *args) { switch_channel_t *channel = switch_core_session_get_channel(session);