2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * WaitForSilence Application by David C. Troy <dave@popvox.com>
7 * Version 1.00 2004-01-29
9 * Mark Spencer <markster@digium.com>
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2. See the LICENSE file
19 * at the top of the source tree.
24 * \brief Wait for Silence
25 * - Waits for up to 'x' milliseconds of silence, 'y' times \n
26 * - WaitForSilence(500,2) will wait for 1/2 second of silence, twice \n
27 * - WaitForSilence(1000,1) will wait for 1 second of silence, once \n
29 * \ingroup applications
39 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41 #include "asterisk/file.h"
42 #include "asterisk/logger.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/pbx.h"
45 #include "asterisk/dsp.h"
46 #include "asterisk/module.h"
47 #include "asterisk/options.h"
49 static char *tdesc = "Wait For Silence";
50 static char *app = "WaitForSilence";
51 static char *synopsis = "Waits for a specified amount of silence";
52 static char *descrip =
53 " WaitForSilence(x[|y]) Wait for Silence: Waits for up to 'x' \n"
54 "milliseconds of silence, 'y' times or 1 if omitted\n"
55 "Set the channel variable WAITSTATUS with to one of these values:"
56 "SILENCE - if silence of x ms was detected"
57 "TIMEOUT - if silence of x ms was not detected."
59 " - WaitForSilence(500|2) will wait for 1/2 second of silence, twice\n"
60 " - WaitForSilence(1000) will wait for 1 second of silence, once\n";
66 static int do_waiting(struct ast_channel *chan, int maxsilence) {
72 static int silencethreshold = 64;
75 struct ast_dsp *sildet; /* silence detector dsp */
79 rfmt = chan->readformat; /* Set to linear mode */
80 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
82 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
86 sildet = ast_dsp_new(); /* Create the silence detector */
88 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
91 ast_dsp_set_threshold(sildet, silencethreshold);
93 /* Await silence... */
96 res = ast_waitfor(chan, 2000);
98 ast_log(LOG_WARNING, "One waitfor failed, trying another\n");
99 /* Try one more time in case of masq */
100 res = ast_waitfor(chan, 2000);
102 ast_log(LOG_WARNING, "No audio available on %s??\n", chan->name);
114 if (f->frametype == AST_FRAME_VOICE) {
116 ast_dsp_silence(sildet, f, &dspsilence);
118 totalsilence = dspsilence;
124 if (totalsilence >= maxsilence) {
125 if (option_verbose > 2)
126 ast_verbose(VERBOSE_PREFIX_3 "Exiting with %dms silence > %dms required\n", totalsilence, maxsilence);
127 /* Ended happily with silence */
129 pbx_builtin_setvar_helper(chan, "WAITSTATUS", "SILENCE");
130 ast_log(LOG_DEBUG, "WAITSTATUS was set to SILENCE\n");
133 } else if ( difftime(time(&now),start) >= maxsilence/1000 ) {
134 pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
135 ast_log(LOG_DEBUG, "WAITSTATUS was set to TIMEOUT\n");
142 if (rfmt && ast_set_read_format(chan, rfmt)) {
143 ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
145 ast_dsp_free(sildet);
149 static int waitforsilence_exec(struct ast_channel *chan, void *data)
153 int maxsilence = 1000;
154 int iterations = 1, i;
158 res = ast_answer(chan); /* Answer the channel */
160 if (!data || ((sscanf(data, "%d|%d", &maxsilence, &iterations) != 2) &&
161 (sscanf(data, "%d", &maxsilence) != 1))) {
162 ast_log(LOG_WARNING, "Using default value of 1000ms, 1 iteration\n");
165 if (option_verbose > 2)
166 ast_verbose(VERBOSE_PREFIX_3 "Waiting %d time(s) for %d ms silence\n", iterations, maxsilence);
169 for (i=0; (i<iterations) && (res == 1); i++) {
170 res = do_waiting(chan, maxsilence);
172 LOCAL_USER_REMOVE(u);
178 int unload_module(void)
182 res = ast_unregister_application(app);
184 STANDARD_HANGUP_LOCALUSERS;
189 int load_module(void)
191 return ast_register_application(app, waitforsilence_exec, synopsis, descrip);
194 char *description(void)
202 STANDARD_USECOUNT(res);
208 return ASTERISK_GPL_KEY;