From 7d9e3c577a008a62aa26d0e7d4d562e9927ad1d5 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthony.minessale@gmail.com>
Date: Sat, 24 Nov 2007 21:48:25 +0000
Subject: [PATCH] fix energy stuff to work better on 16khz calls

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@6395 d0543943-73ff-0310-b7d9-9358b9ac24b2
---
 src/mod/applications/mod_conference/mod_conference.c | 10 ++++++++--
 src/switch_ivr_play_say.c                            |  9 +++++++--
 src/switch_rtp.c                                     |  9 +++++++--
 3 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/src/mod/applications/mod_conference/mod_conference.c b/src/mod/applications/mod_conference/mod_conference.c
index 00d933c928..274ce1b710 100644
--- a/src/mod/applications/mod_conference/mod_conference.c
+++ b/src/mod/applications/mod_conference/mod_conference.c
@@ -1382,17 +1382,23 @@ static void *SWITCH_THREAD_FUNC conference_loop_input(switch_thread_t * thread,
 		if (switch_test_flag(member, MFLAG_CAN_SPEAK) && energy_level) {
 			uint32_t energy = 0, i = 0, samples = 0, j = 0, score = 0;
 			int16_t *data;
+			int divisor = 0;
 
 			data = read_frame->data;
+
+			if (!(divisor = read_codec->implementation->actual_samples_per_second / 8000)) {
+				divisor = 1;
+			}
+			
 			if ((samples = read_frame->datalen / sizeof(*data))) {
 
 				for (i = 0; i < samples; i++) {
 					energy += abs(data[j]);
 					j += read_codec->implementation->number_of_channels;
 				}
-				score = energy / samples;
+				score = energy / (samples / divisor);
 			}
-
+			
 			if (score > energy_level) {
 				uint32_t diff = score - energy_level;
 				if (hangover_hits) {
diff --git a/src/switch_ivr_play_say.c b/src/switch_ivr_play_say.c
index e0dabb986f..d3b3f5383c 100644
--- a/src/switch_ivr_play_say.c
+++ b/src/switch_ivr_play_say.c
@@ -516,13 +516,18 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_record_file(switch_core_session_t *se
 			uint32_t samples = read_frame->datalen / sizeof(*fdata);
 			uint32_t score, count = 0, j = 0;
 			double energy = 0;
-
+			int divisor = 0;
+			
 			for (count = 0; count < samples; count++) {
 				energy += abs(fdata[j]);
 				j += read_codec->implementation->number_of_channels;
 			}
 
-			score = (uint32_t) (energy / samples);
+			if (!(divisor = read_codec->implementation->actual_samples_per_second / 8000)) {
+				divisor = 1;
+			}
+			
+			score = (uint32_t) (energy / (samples / divisor));
 			if (score < fh->thresh) {
 				if (!--fh->silence_hits) {
 					break;
diff --git a/src/switch_rtp.c b/src/switch_rtp.c
index 21231492bd..51dee6023a 100644
--- a/src/switch_rtp.c
+++ b/src/switch_rtp.c
@@ -1375,8 +1375,13 @@ static int rtp_common_write(switch_rtp_t *rtp_session, void *data, uint32_t data
 			uint32_t energy = 0;
 			uint32_t x, y = 0, z = len / sizeof(int16_t);
 			uint32_t score = 0;
-
+			int divisor = 0;
 			if (z) {
+				
+				if (!(divisor = rtp_session->vad_data.read_codec->implementation->actual_samples_per_second / 8000)) {
+					divisor = 1;
+				}
+
 				for (x = 0; x < z; x++) {
 					energy += abs(decoded[y]);
 					y += rtp_session->vad_data.read_codec->implementation->number_of_channels;
@@ -1385,7 +1390,7 @@ static int rtp_common_write(switch_rtp_t *rtp_session, void *data, uint32_t data
 				if (++rtp_session->vad_data.start_count < rtp_session->vad_data.start) {
 					send = 1;
 				} else {
-					score = energy / z;
+					score = (energy / (z / divisor));
 					if (score && (rtp_session->vad_data.bg_count < rtp_session->vad_data.bg_len)) {
 						rtp_session->vad_data.bg_level += score;
 						if (++rtp_session->vad_data.bg_count == rtp_session->vad_data.bg_len) {