[Freeswitch-svn] [commit] r8986 - in freeswitch/trunk/src: . include mod/applications/mod_dptools

Freeswitch SVN anthm at freeswitch.org
Thu Jul 10 11:57:41 EDT 2008


Author: anthm
Date: Thu Jul 10 11:57:41 2008
New Revision: 8986

Modified:
   freeswitch/trunk/src/include/switch_ivr.h
   freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c
   freeswitch/trunk/src/switch_ivr_play_say.c

Log:
add wait_for_voice app for MC

Modified: freeswitch/trunk/src/include/switch_ivr.h
==============================================================================
--- freeswitch/trunk/src/include/switch_ivr.h	(original)
+++ freeswitch/trunk/src/include/switch_ivr.h	Thu Jul 10 11:57:41 2008
@@ -314,6 +314,9 @@
 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);
 
 /*!

Modified: freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c
==============================================================================
--- freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c	(original)
+++ freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c	Thu Jul 10 11:57:41 2008
@@ -2102,6 +2102,31 @@
 	switch_ivr_unhold_uuid(switch_core_session_get_uuid(session));
 }
 
+#define WAIT_FOR_SILENCE_SYNTAX "<tresh> <silence hits> <listen hits> [<file>]"
+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 @@
 				   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 */

Modified: freeswitch/trunk/src/switch_ivr_play_say.c
==============================================================================
--- freeswitch/trunk/src/switch_ivr_play_say.c	(original)
+++ freeswitch/trunk/src/switch_ivr_play_say.c	Thu Jul 10 11:57:41 2008
@@ -1195,6 +1195,125 @@
 	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,



More information about the Freeswitch-svn mailing list