Merge "contrib/sip_to_pjsip: handle setvar in conversion"
[asterisk/asterisk.git] / funcs / func_speex.c
index fc4eb8e..1a88fae 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (C) 2008, Digium, Inc.
  *
  * Brian Degenhardt <bmd@digium.com>
- * Brett Bryant <bbryant@digium.com> 
+ * Brett Bryant <bbryant@digium.com>
  *
  * See http://www.asterisk.org for more information about
  * the Asterisk project. Please do not directly contact
  *
  * \brief Noise reduction and automatic gain control (AGC)
  *
- * \author Brian Degenhardt <bmd@digium.com> 
- * \author Brett Bryant <bbryant@digium.com> 
+ * \author Brian Degenhardt <bmd@digium.com>
+ * \author Brett Bryant <bbryant@digium.com>
  *
  * \ingroup functions
  *
- * \extref The Speex library - http://www.speex.org
+ * The Speex 1.2 library - http://www.speex.org
+ * \note Requires the 1.2 version of the Speex library (which might not be what you find in Linux packages)
  */
 
 /*** MODULEINFO
        <depend>speex</depend>
        <depend>speex_preprocess</depend>
-       <use>speexdsp</use>
+       <use type="external">speexdsp</use>
+       <support_level>core</support_level>
  ***/
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include <speex/speex_preprocess.h>
 #include "asterisk/module.h"
 #include "asterisk/channel.h"
@@ -48,6 +48,52 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #define DEFAULT_AGC_LEVEL 8000.0
 
+/*** DOCUMENTATION
+       <function name="AGC" language="en_US">
+               <synopsis>
+                       Apply automatic gain control to audio on a channel.
+               </synopsis>
+               <syntax>
+                       <parameter name="channeldirection" required="true">
+                               <para>This can be either <literal>rx</literal> or <literal>tx</literal></para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>The AGC function will apply automatic gain control to the audio on the
+                       channel that it is executed on. Using <literal>rx</literal> for audio received
+                       and <literal>tx</literal> for audio transmitted to the channel. When using this
+                       function you set a target audio level. It is primarily intended for use with
+                       analog lines, but could be useful for other channels as well. The target volume
+                       is set with a number between <literal>1-32768</literal>. The larger the number
+                       the louder (more gain) the channel will receive.</para>
+                       <para>Examples:</para>
+                       <para>exten => 1,1,Set(AGC(rx)=8000)</para>
+                       <para>exten => 1,2,Set(AGC(tx)=off)</para>
+               </description>
+       </function>
+       <function name="DENOISE" language="en_US">
+               <synopsis>
+                       Apply noise reduction to audio on a channel.
+               </synopsis>
+               <syntax>
+                       <parameter name="channeldirection" required="true">
+                               <para>This can be either <literal>rx</literal> or <literal>tx</literal>
+                               the values that can be set to this are either <literal>on</literal> and
+                               <literal>off</literal></para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>The DENOISE function will apply noise reduction to audio on the channel
+                       that it is executed on. It is very useful for noisy analog lines, especially
+                       when adjusting gains or using AGC. Use <literal>rx</literal> for audio received from the channel
+                       and <literal>tx</literal> to apply the filter to the audio being sent to the channel.</para>
+                       <para>Examples:</para>
+                       <para>exten => 1,1,Set(DENOISE(rx)=on)</para>
+                       <para>exten => 1,2,Set(DENOISE(tx)=off)</para>
+               </description>
+       </function>
+ ***/
+
 struct speex_direction_info {
        SpeexPreprocessState *state;    /*!< speex preprocess state object */
        int agc;                                                /*!< audio gain control is enabled or not */
@@ -58,10 +104,11 @@ struct speex_direction_info {
 
 struct speex_info {
        struct ast_audiohook audiohook;
+       int lastrate;
        struct speex_direction_info *tx, *rx;
 };
 
-static void destroy_callback(void *data) 
+static void destroy_callback(void *data)
 {
        struct speex_info *si = data;
 
@@ -96,36 +143,36 @@ static int speex_callback(struct ast_audiohook *audiohook, struct ast_channel *c
        struct ast_datastore *datastore = NULL;
        struct speex_direction_info *sdi = NULL;
        struct speex_info *si = NULL;
+       char source[80];
 
        /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
        if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE || frame->frametype != AST_FRAME_VOICE) {
-               return 0;
+               return -1;
        }
-       
-       ast_channel_lock(chan);
+
+       /* We are called with chan already locked */
        if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) {
-               ast_channel_unlock(chan);
-               return 0;
+               return -1;
        }
-       ast_channel_unlock(chan);
 
        si = datastore->data;
 
        sdi = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? si->rx : si->tx;
 
        if (!sdi) {
-               return 0;
+               return -1;
        }
 
-       if (sdi->samples != frame->samples) {
+       if ((sdi->samples != frame->samples) || (ast_format_get_sample_rate(frame->subclass.format) != si->lastrate)) {
+               si->lastrate = ast_format_get_sample_rate(frame->subclass.format);
                if (sdi->state) {
                        speex_preprocess_state_destroy(sdi->state);
                }
 
-               if (!(sdi->state = speex_preprocess_state_init((sdi->samples = frame->samples), 8000))) {
+               if (!(sdi->state = speex_preprocess_state_init((sdi->samples = frame->samples), si->lastrate))) {
                        return -1;
                }
-               
+
                speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_AGC, &sdi->agc);
 
                if (sdi->agc) {
@@ -135,7 +182,13 @@ static int speex_callback(struct ast_audiohook *audiohook, struct ast_channel *c
                speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_DENOISE, &sdi->denoise);
        }
 
-       speex_preprocess(sdi->state, frame->data, NULL);
+       speex_preprocess(sdi->state, frame->data.ptr, NULL);
+       snprintf(source, sizeof(source), "%s/speex", frame->src);
+       if (frame->mallocd & AST_MALLOCD_SRC) {
+               ast_free((char *) frame->src);
+       }
+       frame->src = ast_strdup(source);
+       frame->mallocd |= AST_MALLOCD_SRC;
 
        return 0;
 }
@@ -147,22 +200,32 @@ static int speex_write(struct ast_channel *chan, const char *cmd, char *data, co
        struct speex_direction_info **sdi = NULL;
        int is_new = 0;
 
+       if (!chan) {
+               ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
+               return -1;
+       }
+
+       if (strcasecmp(data, "rx") && strcasecmp(data, "tx")) {
+               ast_log(LOG_ERROR, "Invalid argument provided to the %s function\n", cmd);
+               return -1;
+       }
+
        ast_channel_lock(chan);
        if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) {
                ast_channel_unlock(chan);
 
-               if (!(datastore = ast_channel_datastore_alloc(&speex_datastore, NULL))) {
+               if (!(datastore = ast_datastore_alloc(&speex_datastore, NULL))) {
                        return 0;
                }
 
                if (!(si = ast_calloc(1, sizeof(*si)))) {
-                       ast_channel_datastore_free(datastore);
+                       ast_datastore_free(datastore);
                        return 0;
                }
 
-               ast_audiohook_init(&si->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "speex");
+               ast_audiohook_init(&si->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "speex", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
                si->audiohook.manipulate_callback = speex_callback;
-
+               si->lastrate = 8000;
                is_new = 1;
        } else {
                ast_channel_unlock(chan);
@@ -171,15 +234,8 @@ static int speex_write(struct ast_channel *chan, const char *cmd, char *data, co
 
        if (!strcasecmp(data, "rx")) {
                sdi = &si->rx;
-       } else if (!strcasecmp(data, "tx")) {
-               sdi = &si->tx;
        } else {
-               ast_log(LOG_ERROR, "Invalid argument provided to the %s function\n", cmd);
-
-               if (is_new) {
-                       ast_channel_datastore_free(datastore);
-                       return -1;
-               }
+               sdi = &si->tx;
        }
 
        if (!*sdi) {
@@ -193,15 +249,15 @@ static int speex_write(struct ast_channel *chan, const char *cmd, char *data, co
        }
 
        if (!strcasecmp(cmd, "agc")) {
-               if (!sscanf(value, "%f", &(*sdi)->agclevel))
+               if (!sscanf(value, "%30f", &(*sdi)->agclevel))
                        (*sdi)->agclevel = ast_true(value) ? DEFAULT_AGC_LEVEL : 0.0;
-       
+
                if ((*sdi)->agclevel > 32768.0) {
-                       ast_log(LOG_WARNING, "AGC(%s)=%.01f is greater than 32768... setting to 32768 instead\n", 
+                       ast_log(LOG_WARNING, "AGC(%s)=%.01f is greater than 32768... setting to 32768 instead\n",
                                        ((*sdi == si->rx) ? "rx" : "tx"), (*sdi)->agclevel);
                        (*sdi)->agclevel = 32768.0;
                }
-       
+
                (*sdi)->agc = !!((*sdi)->agclevel);
 
                if ((*sdi)->state) {
@@ -236,11 +292,11 @@ static int speex_write(struct ast_channel *chan, const char *cmd, char *data, co
                        ast_audiohook_remove(chan, &si->audiohook);
                        ast_audiohook_detach(&si->audiohook);
                }
-               
-               ast_channel_datastore_free(datastore);
+
+               ast_datastore_free(datastore);
        }
 
-       if (is_new) { 
+       if (is_new) {
                datastore->data = si;
                ast_channel_lock(chan);
                ast_channel_datastore_add(chan, datastore);
@@ -290,41 +346,16 @@ static int speex_read(struct ast_channel *chan, const char *cmd, char *data, cha
 
 static struct ast_custom_function agc_function = {
        .name = "AGC",
-       .synopsis = "Apply automatic gain control to audio on a channel",
-       .desc =
-       "  The AGC function will apply automatic gain control to audio on the channel\n"
-       "that this function is executed on.  Use rx for audio received from the channel\n"
-       "and tx to apply AGC to the audio being sent to the channel.  When using this\n"
-       "function, you set a target audio level.  It is primarily intended for use with\n"
-       "analog lines, but could be useful for other channels, as well.  The target volume\n"
-       "is set with a number between 1 and 32768.  Larger numbers are louder.\n"
-       "  Example Usage:\n"
-       "    Set(AGC(rx)=8000)\n"
-       "    Set(AGC(tx)=8000)\n"
-       "    Set(AGC(rx)=off)\n"
-       "    Set(AGC(tx)=off)\n"
-       "",
        .write = speex_write,
-       .read = speex_read
+       .read = speex_read,
+       .read_max = 22,
 };
 
 static struct ast_custom_function denoise_function = {
        .name = "DENOISE",
-       .synopsis = "Apply noise reduction to audio on a channel",
-       .desc =
-       "  The DENOISE function will apply noise reduction to audio on the channel\n"
-       "that this function is executed on.  It is especially useful for noisy analog\n"
-       "lines, especially when adjusting gains or using AGC.  Use rx for audio\n"
-       "received from the channel and tx to apply the filter to the audio being sent\n"
-       "to the channel.\n"
-       "  Example Usage:\n"
-       "    Set(DENOISE(rx)=on)\n"
-       "    Set(DENOISE(tx)=on)\n"
-       "    Set(DENOISE(rx)=off)\n"
-       "    Set(DENOISE(tx)=off)\n"
-       "",
        .write = speex_write,
-       .read = speex_read
+       .read = speex_read,
+       .read_max = 22,
 };
 
 static int unload_module(void)