2008-05-01 16:57:19 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008, Digium, Inc.
|
|
|
|
*
|
|
|
|
* Brian Degenhardt <bmd@digium.com>
|
|
|
|
* Brett Bryant <bbryant@digium.com>
|
|
|
|
*
|
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file
|
|
|
|
*
|
|
|
|
* \brief Noise reduction and automatic gain control (AGC)
|
|
|
|
*
|
|
|
|
* \author Brian Degenhardt <bmd@digium.com>
|
|
|
|
* \author Brett Bryant <bbryant@digium.com>
|
|
|
|
*
|
|
|
|
* \ingroup functions
|
|
|
|
*
|
2012-09-21 17:14:59 +00:00
|
|
|
* The Speex 1.2 library - http://www.speex.org
|
2009-11-03 16:56:48 +00:00
|
|
|
* \note Requires the 1.2 version of the Speex library (which might not be what you find in Linux packages)
|
2008-05-01 16:57:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*** MODULEINFO
|
|
|
|
<depend>speex</depend>
|
2008-05-05 22:13:57 +00:00
|
|
|
<depend>speex_preprocess</depend>
|
2011-07-05 22:11:40 +00:00
|
|
|
<use type="external">speexdsp</use>
|
2011-07-14 20:28:54 +00:00
|
|
|
<support_level>core</support_level>
|
2008-05-01 16:57:19 +00:00
|
|
|
***/
|
|
|
|
|
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
#include <speex/speex_preprocess.h>
|
|
|
|
#include "asterisk/module.h"
|
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/pbx.h"
|
|
|
|
#include "asterisk/utils.h"
|
|
|
|
#include "asterisk/audiohook.h"
|
|
|
|
|
|
|
|
#define DEFAULT_AGC_LEVEL 8000.0
|
|
|
|
|
2008-11-01 21:10:07 +00:00
|
|
|
/*** 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
|
2008-11-02 02:50:33 +00:00
|
|
|
channel that it is executed on. Using <literal>rx</literal> for audio received
|
2008-11-01 21:10:07 +00:00
|
|
|
and <literal>tx</literal> for audio transmitted to the channel. When using this
|
2008-11-02 02:50:33 +00:00
|
|
|
function you set a target audio level. It is primarily intended for use with
|
2008-11-01 21:10:07 +00:00
|
|
|
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
|
2008-11-02 02:50:33 +00:00
|
|
|
the louder (more gain) the channel will receive.</para>
|
2008-11-01 21:10:07 +00:00
|
|
|
<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>
|
|
|
|
***/
|
|
|
|
|
2008-05-01 16:57:19 +00:00
|
|
|
struct speex_direction_info {
|
|
|
|
SpeexPreprocessState *state; /*!< speex preprocess state object */
|
|
|
|
int agc; /*!< audio gain control is enabled or not */
|
|
|
|
int denoise; /*!< denoise is enabled or not */
|
|
|
|
int samples; /*!< n of 8Khz samples in last frame */
|
|
|
|
float agclevel; /*!< audio gain control level [1.0 - 32768.0] */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct speex_info {
|
|
|
|
struct ast_audiohook audiohook;
|
Media Project Phase2: SILK 8khz-24khz, SLINEAR 8khz-192khz, SPEEX 32khz, hd audio ConfBridge, and other stuff
-Functional changes
1. Dynamic global format list build by codecs defined in codecs.conf
2. SILK 8khz, 12khz, 16khz, and 24khz with custom attributes defined in codecs.conf
3. Negotiation of SILK attributes in chan_sip.
4. SPEEX 32khz with translation
5. SLINEAR 8khz, 12khz, 24khz, 32khz, 44.1khz, 48khz, 96khz, 192khz with translation
using codec_resample.c
6. Various changes to RTP code required to properly handle the dynamic format list
and formats with attributes.
7. ConfBridge now dynamically jumps to the best possible sample rate. This allows
for conferences to take advantage of HD audio (Which sounds awesome)
8. Audiohooks are no longer limited to 8khz audio, and most effects have been
updated to take advantage of this such as Volume, DENOISE, PITCH_SHIFT.
9. codec_resample now uses its own code rather than depending on libresample.
-Organizational changes
Global format list is moved from frame.c to format.c
Various format specific functions moved from frame.c to format.c
Review: https://reviewboard.asterisk.org/r/1104/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@308582 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2011-02-22 23:04:49 +00:00
|
|
|
int lastrate;
|
2008-05-01 16:57:19 +00:00
|
|
|
struct speex_direction_info *tx, *rx;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void destroy_callback(void *data)
|
|
|
|
{
|
|
|
|
struct speex_info *si = data;
|
|
|
|
|
|
|
|
ast_audiohook_destroy(&si->audiohook);
|
|
|
|
|
|
|
|
if (si->rx && si->rx->state) {
|
|
|
|
speex_preprocess_state_destroy(si->rx->state);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (si->tx && si->tx->state) {
|
|
|
|
speex_preprocess_state_destroy(si->tx->state);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (si->rx) {
|
|
|
|
ast_free(si->rx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (si->tx) {
|
|
|
|
ast_free(si->tx);
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_free(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct ast_datastore_info speex_datastore = {
|
|
|
|
.type = "speex",
|
|
|
|
.destroy = destroy_callback
|
|
|
|
};
|
|
|
|
|
|
|
|
static int speex_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
|
|
|
|
{
|
|
|
|
struct ast_datastore *datastore = NULL;
|
|
|
|
struct speex_direction_info *sdi = NULL;
|
|
|
|
struct speex_info *si = NULL;
|
2009-10-20 22:09:07 +00:00
|
|
|
char source[80];
|
2008-05-01 16:57:19 +00:00
|
|
|
|
|
|
|
/* 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) {
|
2009-10-20 22:09:07 +00:00
|
|
|
return -1;
|
2008-05-01 16:57:19 +00:00
|
|
|
}
|
2009-10-20 22:09:07 +00:00
|
|
|
|
|
|
|
/* We are called with chan already locked */
|
2008-05-01 16:57:19 +00:00
|
|
|
if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) {
|
2009-10-20 22:09:07 +00:00
|
|
|
return -1;
|
2008-05-01 16:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
si = datastore->data;
|
|
|
|
|
|
|
|
sdi = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? si->rx : si->tx;
|
|
|
|
|
|
|
|
if (!sdi) {
|
2009-10-20 22:09:07 +00:00
|
|
|
return -1;
|
2008-05-01 16:57:19 +00:00
|
|
|
}
|
|
|
|
|
media formats: re-architect handling of media for performance improvements
In the old times media formats were represented using a bit field. This was
fast but had a few limitations.
1. Asterisk was limited in how many formats it could handle.
2. Formats, being a bit field, could not include any attribute information.
A format was strictly its type, e.g., "this is ulaw".
This was changed in Asterisk 10 (see
https://wiki.asterisk.org/wiki/display/AST/Media+Architecture+Proposal for
notes on that work) which led to the creation of the ast_format structure.
This structure allowed Asterisk to handle attributes and bundle information
with a format.
Additionally, ast_format_cap was created to act as a container for multiple
formats that, together, formed the capability of some entity. Another
mechanism was added to allow logic to be registered which performed format
attribute negotiation. Everywhere throughout the codebase Asterisk was
changed to use this strategy.
Unfortunately, in software, there is no free lunch. These new capabilities
came at a cost.
Performance analysis and profiling showed that we spend an inordinate
amount of time comparing, copying, and generally manipulating formats and
their related structures. Basic prototyping has shown that a reasonably
large performance improvement could be made in this area. This patch is the
result of that project, which overhauled the media format architecture
and its usage in Asterisk to improve performance.
Generally, the new philosophy for handling formats is as follows:
* The ast_format structure is reference counted. This removed a large amount
of the memory allocations and copying that was done in prior versions.
* In order to prevent race conditions while keeping things performant, the
ast_format structure is immutable by convention and lock-free. Violate this
tenet at your peril!
* Because formats are reference counted, codecs are also reference counted.
The Asterisk core generally provides built-in codecs and caches the
ast_format structures created to represent them. Generally, to prevent
inordinate amounts of module reference bumping, codecs and formats can be
added at run-time but cannot be removed.
* All compatibility with the bit field representation of codecs/formats has
been moved to a compatibility API. The primary user of this representation
is chan_iax2, which must continue to maintain its bit-field usage of formats
for interoperability concerns.
* When a format is negotiated with attributes, or when a format cannot be
represented by one of the cached formats, a new format object is created or
cloned from an existing format. That format may have the same codec
underlying it, but is a different format than a version of the format with
different attributes or without attributes.
* While formats are reference counted objects, the reference count maintained
on the format should be manipulated with care. Formats are generally cached
and will persist for the lifetime of Asterisk and do not explicitly need
to have their lifetime modified. An exception to this is when the user of a
format does not know where the format came from *and* the user may outlive
the provider of the format. This occurs, for example, when a format is read
from a channel: the channel may have a format with attributes (hence,
non-cached) and the user of the format may last longer than the channel (if
the reference to the channel is released prior to the format's reference).
For more information on this work, see the API design notes:
https://wiki.asterisk.org/wiki/display/AST/Media+Format+Rewrite
Finally, this work was the culmination of a large number of developer's
efforts. Extra thanks goes to Corey Farrell, who took on a large amount of the
work in the Asterisk core, chan_sip, and was an invaluable resource in peer
reviews throughout this project.
There were a substantial number of patches contributed during this work; the
following issues/patch names simply reflect some of the work (and will cause
the release scripts to give attribution to the individuals who work on them).
Reviews:
https://reviewboard.asterisk.org/r/3814
https://reviewboard.asterisk.org/r/3808
https://reviewboard.asterisk.org/r/3805
https://reviewboard.asterisk.org/r/3803
https://reviewboard.asterisk.org/r/3801
https://reviewboard.asterisk.org/r/3798
https://reviewboard.asterisk.org/r/3800
https://reviewboard.asterisk.org/r/3794
https://reviewboard.asterisk.org/r/3793
https://reviewboard.asterisk.org/r/3792
https://reviewboard.asterisk.org/r/3791
https://reviewboard.asterisk.org/r/3790
https://reviewboard.asterisk.org/r/3789
https://reviewboard.asterisk.org/r/3788
https://reviewboard.asterisk.org/r/3787
https://reviewboard.asterisk.org/r/3786
https://reviewboard.asterisk.org/r/3784
https://reviewboard.asterisk.org/r/3783
https://reviewboard.asterisk.org/r/3778
https://reviewboard.asterisk.org/r/3774
https://reviewboard.asterisk.org/r/3775
https://reviewboard.asterisk.org/r/3772
https://reviewboard.asterisk.org/r/3761
https://reviewboard.asterisk.org/r/3754
https://reviewboard.asterisk.org/r/3753
https://reviewboard.asterisk.org/r/3751
https://reviewboard.asterisk.org/r/3750
https://reviewboard.asterisk.org/r/3748
https://reviewboard.asterisk.org/r/3747
https://reviewboard.asterisk.org/r/3746
https://reviewboard.asterisk.org/r/3742
https://reviewboard.asterisk.org/r/3740
https://reviewboard.asterisk.org/r/3739
https://reviewboard.asterisk.org/r/3738
https://reviewboard.asterisk.org/r/3737
https://reviewboard.asterisk.org/r/3736
https://reviewboard.asterisk.org/r/3734
https://reviewboard.asterisk.org/r/3722
https://reviewboard.asterisk.org/r/3713
https://reviewboard.asterisk.org/r/3703
https://reviewboard.asterisk.org/r/3689
https://reviewboard.asterisk.org/r/3687
https://reviewboard.asterisk.org/r/3674
https://reviewboard.asterisk.org/r/3671
https://reviewboard.asterisk.org/r/3667
https://reviewboard.asterisk.org/r/3665
https://reviewboard.asterisk.org/r/3625
https://reviewboard.asterisk.org/r/3602
https://reviewboard.asterisk.org/r/3519
https://reviewboard.asterisk.org/r/3518
https://reviewboard.asterisk.org/r/3516
https://reviewboard.asterisk.org/r/3515
https://reviewboard.asterisk.org/r/3512
https://reviewboard.asterisk.org/r/3506
https://reviewboard.asterisk.org/r/3413
https://reviewboard.asterisk.org/r/3410
https://reviewboard.asterisk.org/r/3387
https://reviewboard.asterisk.org/r/3388
https://reviewboard.asterisk.org/r/3389
https://reviewboard.asterisk.org/r/3390
https://reviewboard.asterisk.org/r/3321
https://reviewboard.asterisk.org/r/3320
https://reviewboard.asterisk.org/r/3319
https://reviewboard.asterisk.org/r/3318
https://reviewboard.asterisk.org/r/3266
https://reviewboard.asterisk.org/r/3265
https://reviewboard.asterisk.org/r/3234
https://reviewboard.asterisk.org/r/3178
ASTERISK-23114 #close
Reported by: mjordan
media_formats_translation_core.diff uploaded by kharwell (License 6464)
rb3506.diff uploaded by mjordan (License 6283)
media_format_app_file.diff uploaded by kharwell (License 6464)
misc-2.diff uploaded by file (License 5000)
chan_mild-3.diff uploaded by file (License 5000)
chan_obscure.diff uploaded by file (License 5000)
jingle.diff uploaded by file (License 5000)
funcs.diff uploaded by file (License 5000)
formats.diff uploaded by file (License 5000)
core.diff uploaded by file (License 5000)
bridges.diff uploaded by file (License 5000)
mf-codecs-2.diff uploaded by file (License 5000)
mf-app_fax.diff uploaded by file (License 5000)
mf-apps-3.diff uploaded by file (License 5000)
media-formats-3.diff uploaded by file (License 5000)
ASTERISK-23715
rb3713.patch uploaded by coreyfarrell (License 5909)
rb3689.patch uploaded by mjordan (License 6283)
ASTERISK-23957
rb3722.patch uploaded by mjordan (License 6283)
mf-attributes-3.diff uploaded by file (License 5000)
ASTERISK-23958
Tested by: jrose
rb3822.patch uploaded by coreyfarrell (License 5909)
rb3800.patch uploaded by jrose (License 6182)
chan_sip.diff uploaded by mjordan (License 6283)
rb3747.patch uploaded by jrose (License 6182)
ASTERISK-23959 #close
Tested by: sgriepentrog, mjordan, coreyfarrell
sip_cleanup.diff uploaded by opticron (License 6273)
chan_sip_caps.diff uploaded by mjordan (License 6283)
rb3751.patch uploaded by coreyfarrell (License 5909)
chan_sip-3.diff uploaded by file (License 5000)
ASTERISK-23960 #close
Tested by: opticron
direct_media.diff uploaded by opticron (License 6273)
pjsip-direct-media.diff uploaded by file (License 5000)
format_cap_remove.diff uploaded by opticron (License 6273)
media_format_fixes.diff uploaded by opticron (License 6273)
chan_pjsip-2.diff uploaded by file (License 5000)
ASTERISK-23966 #close
Tested by: rmudgett
rb3803.patch uploaded by rmudgetti (License 5621)
chan_dahdi.diff uploaded by file (License 5000)
ASTERISK-24064 #close
Tested by: coreyfarrell, mjordan, opticron, file, rmudgett, sgriepentrog, jrose
rb3814.patch uploaded by rmudgett (License 5621)
moh_cleanup.diff uploaded by opticron (License 6273)
bridge_leak.diff uploaded by opticron (License 6273)
translate.diff uploaded by file (License 5000)
rb3795.patch uploaded by rmudgett (License 5621)
tls_fix.diff uploaded by mjordan (License 6283)
fax-mf-fix-2.diff uploaded by file (License 5000)
rtp_transfer_stuff uploaded by mjordan (License 6283)
rb3787.patch uploaded by rmudgett (License 5621)
media-formats-explicit-translate-format-3.diff uploaded by file (License 5000)
format_cache_case_fix.diff uploaded by opticron (License 6273)
rb3774.patch uploaded by rmudgett (License 5621)
rb3775.patch uploaded by rmudgett (License 5621)
rtp_engine_fix.diff uploaded by opticron (License 6273)
rtp_crash_fix.diff uploaded by opticron (License 6273)
rb3753.patch uploaded by mjordan (License 6283)
rb3750.patch uploaded by mjordan (License 6283)
rb3748.patch uploaded by rmudgett (License 5621)
media_format_fixes.diff uploaded by opticron (License 6273)
rb3740.patch uploaded by mjordan (License 6283)
rb3739.patch uploaded by mjordan (License 6283)
rb3734.patch uploaded by mjordan (License 6283)
rb3689.patch uploaded by mjordan (License 6283)
rb3674.patch uploaded by coreyfarrell (License 5909)
rb3671.patch uploaded by coreyfarrell (License 5909)
rb3667.patch uploaded by coreyfarrell (License 5909)
rb3665.patch uploaded by mjordan (License 6283)
rb3625.patch uploaded by coreyfarrell (License 5909)
rb3602.patch uploaded by coreyfarrell (License 5909)
format_compatibility-2.diff uploaded by file (License 5000)
core.diff uploaded by file (License 5000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@419044 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2014-07-20 22:06:33 +00:00
|
|
|
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);
|
2008-05-01 16:57:19 +00:00
|
|
|
if (sdi->state) {
|
|
|
|
speex_preprocess_state_destroy(sdi->state);
|
|
|
|
}
|
|
|
|
|
Media Project Phase2: SILK 8khz-24khz, SLINEAR 8khz-192khz, SPEEX 32khz, hd audio ConfBridge, and other stuff
-Functional changes
1. Dynamic global format list build by codecs defined in codecs.conf
2. SILK 8khz, 12khz, 16khz, and 24khz with custom attributes defined in codecs.conf
3. Negotiation of SILK attributes in chan_sip.
4. SPEEX 32khz with translation
5. SLINEAR 8khz, 12khz, 24khz, 32khz, 44.1khz, 48khz, 96khz, 192khz with translation
using codec_resample.c
6. Various changes to RTP code required to properly handle the dynamic format list
and formats with attributes.
7. ConfBridge now dynamically jumps to the best possible sample rate. This allows
for conferences to take advantage of HD audio (Which sounds awesome)
8. Audiohooks are no longer limited to 8khz audio, and most effects have been
updated to take advantage of this such as Volume, DENOISE, PITCH_SHIFT.
9. codec_resample now uses its own code rather than depending on libresample.
-Organizational changes
Global format list is moved from frame.c to format.c
Various format specific functions moved from frame.c to format.c
Review: https://reviewboard.asterisk.org/r/1104/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@308582 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2011-02-22 23:04:49 +00:00
|
|
|
if (!(sdi->state = speex_preprocess_state_init((sdi->samples = frame->samples), si->lastrate))) {
|
2008-05-01 16:57:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2009-10-20 22:09:07 +00:00
|
|
|
|
2008-05-01 16:57:19 +00:00
|
|
|
speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_AGC, &sdi->agc);
|
|
|
|
|
|
|
|
if (sdi->agc) {
|
|
|
|
speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_AGC_LEVEL, &sdi->agclevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_DENOISE, &sdi->denoise);
|
|
|
|
}
|
|
|
|
|
2008-05-22 17:10:53 +00:00
|
|
|
speex_preprocess(sdi->state, frame->data.ptr, NULL);
|
2009-10-20 22:09:07 +00:00
|
|
|
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;
|
2008-05-01 16:57:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int speex_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
|
|
|
|
{
|
|
|
|
struct ast_datastore *datastore = NULL;
|
|
|
|
struct speex_info *si = NULL;
|
|
|
|
struct speex_direction_info **sdi = NULL;
|
|
|
|
int is_new = 0;
|
|
|
|
|
2014-03-27 19:21:44 +00:00
|
|
|
if (!chan) {
|
|
|
|
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
Resolve FORWARD_NULL static analysis warnings
This resolves core findings from ASTERISK-19650 numbers 0-2, 6, 7, 9-11, 14-20,
22-24, 28, 30-32, 34-36, 42-56, 82-84, 87, 89-90, 93-102, 104, 105, 109-111,
and 115. Finding numbers 26, 33, and 29 were already resolved. Those skipped
were either extended/deprecated or in areas of code that shouldn't be
disturbed.
(Closes issue ASTERISK-19650)
........
Merged revisions 366167 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........
Merged revisions 366168 from http://svn.asterisk.org/svn/asterisk/branches/10
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@366169 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2012-05-10 20:56:09 +00:00
|
|
|
if (strcasecmp(data, "rx") && strcasecmp(data, "tx")) {
|
|
|
|
ast_log(LOG_ERROR, "Invalid argument provided to the %s function\n", cmd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-05-01 16:57:19 +00:00
|
|
|
ast_channel_lock(chan);
|
|
|
|
if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) {
|
|
|
|
ast_channel_unlock(chan);
|
|
|
|
|
2008-08-05 16:56:11 +00:00
|
|
|
if (!(datastore = ast_datastore_alloc(&speex_datastore, NULL))) {
|
2008-05-01 16:57:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(si = ast_calloc(1, sizeof(*si)))) {
|
2008-08-05 16:56:11 +00:00
|
|
|
ast_datastore_free(datastore);
|
2008-05-01 16:57:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
Media Project Phase2: SILK 8khz-24khz, SLINEAR 8khz-192khz, SPEEX 32khz, hd audio ConfBridge, and other stuff
-Functional changes
1. Dynamic global format list build by codecs defined in codecs.conf
2. SILK 8khz, 12khz, 16khz, and 24khz with custom attributes defined in codecs.conf
3. Negotiation of SILK attributes in chan_sip.
4. SPEEX 32khz with translation
5. SLINEAR 8khz, 12khz, 24khz, 32khz, 44.1khz, 48khz, 96khz, 192khz with translation
using codec_resample.c
6. Various changes to RTP code required to properly handle the dynamic format list
and formats with attributes.
7. ConfBridge now dynamically jumps to the best possible sample rate. This allows
for conferences to take advantage of HD audio (Which sounds awesome)
8. Audiohooks are no longer limited to 8khz audio, and most effects have been
updated to take advantage of this such as Volume, DENOISE, PITCH_SHIFT.
9. codec_resample now uses its own code rather than depending on libresample.
-Organizational changes
Global format list is moved from frame.c to format.c
Various format specific functions moved from frame.c to format.c
Review: https://reviewboard.asterisk.org/r/1104/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@308582 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2011-02-22 23:04:49 +00:00
|
|
|
ast_audiohook_init(&si->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "speex", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
|
2008-05-01 16:57:19 +00:00
|
|
|
si->audiohook.manipulate_callback = speex_callback;
|
Media Project Phase2: SILK 8khz-24khz, SLINEAR 8khz-192khz, SPEEX 32khz, hd audio ConfBridge, and other stuff
-Functional changes
1. Dynamic global format list build by codecs defined in codecs.conf
2. SILK 8khz, 12khz, 16khz, and 24khz with custom attributes defined in codecs.conf
3. Negotiation of SILK attributes in chan_sip.
4. SPEEX 32khz with translation
5. SLINEAR 8khz, 12khz, 24khz, 32khz, 44.1khz, 48khz, 96khz, 192khz with translation
using codec_resample.c
6. Various changes to RTP code required to properly handle the dynamic format list
and formats with attributes.
7. ConfBridge now dynamically jumps to the best possible sample rate. This allows
for conferences to take advantage of HD audio (Which sounds awesome)
8. Audiohooks are no longer limited to 8khz audio, and most effects have been
updated to take advantage of this such as Volume, DENOISE, PITCH_SHIFT.
9. codec_resample now uses its own code rather than depending on libresample.
-Organizational changes
Global format list is moved from frame.c to format.c
Various format specific functions moved from frame.c to format.c
Review: https://reviewboard.asterisk.org/r/1104/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@308582 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2011-02-22 23:04:49 +00:00
|
|
|
si->lastrate = 8000;
|
2008-05-01 16:57:19 +00:00
|
|
|
is_new = 1;
|
|
|
|
} else {
|
|
|
|
ast_channel_unlock(chan);
|
|
|
|
si = datastore->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(data, "rx")) {
|
|
|
|
sdi = &si->rx;
|
|
|
|
} else {
|
Resolve FORWARD_NULL static analysis warnings
This resolves core findings from ASTERISK-19650 numbers 0-2, 6, 7, 9-11, 14-20,
22-24, 28, 30-32, 34-36, 42-56, 82-84, 87, 89-90, 93-102, 104, 105, 109-111,
and 115. Finding numbers 26, 33, and 29 were already resolved. Those skipped
were either extended/deprecated or in areas of code that shouldn't be
disturbed.
(Closes issue ASTERISK-19650)
........
Merged revisions 366167 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........
Merged revisions 366168 from http://svn.asterisk.org/svn/asterisk/branches/10
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@366169 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2012-05-10 20:56:09 +00:00
|
|
|
sdi = &si->tx;
|
2008-05-01 16:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!*sdi) {
|
|
|
|
if (!(*sdi = ast_calloc(1, sizeof(**sdi)))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Right now, the audiohooks API will _only_ provide us 8 kHz slinear
|
|
|
|
* audio. When it supports 16 kHz (or any other sample rates, we will
|
|
|
|
* have to take that into account here. */
|
|
|
|
(*sdi)->samples = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(cmd, "agc")) {
|
2009-08-10 19:20:57 +00:00
|
|
|
if (!sscanf(value, "%30f", &(*sdi)->agclevel))
|
2008-05-01 16:57:19 +00:00
|
|
|
(*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",
|
|
|
|
((*sdi == si->rx) ? "rx" : "tx"), (*sdi)->agclevel);
|
|
|
|
(*sdi)->agclevel = 32768.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*sdi)->agc = !!((*sdi)->agclevel);
|
|
|
|
|
|
|
|
if ((*sdi)->state) {
|
|
|
|
speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_AGC, &(*sdi)->agc);
|
|
|
|
if ((*sdi)->agc) {
|
|
|
|
speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_AGC_LEVEL, &(*sdi)->agclevel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!strcasecmp(cmd, "denoise")) {
|
2008-05-01 18:28:38 +00:00
|
|
|
(*sdi)->denoise = (ast_true(value) != 0);
|
2008-05-01 16:57:19 +00:00
|
|
|
|
|
|
|
if ((*sdi)->state) {
|
|
|
|
speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_DENOISE, &(*sdi)->denoise);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(*sdi)->agc && !(*sdi)->denoise) {
|
|
|
|
if ((*sdi)->state)
|
|
|
|
speex_preprocess_state_destroy((*sdi)->state);
|
|
|
|
|
|
|
|
ast_free(*sdi);
|
|
|
|
*sdi = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!si->rx && !si->tx) {
|
|
|
|
if (is_new) {
|
|
|
|
is_new = 0;
|
|
|
|
} else {
|
|
|
|
ast_channel_lock(chan);
|
|
|
|
ast_channel_datastore_remove(chan, datastore);
|
|
|
|
ast_channel_unlock(chan);
|
|
|
|
ast_audiohook_remove(chan, &si->audiohook);
|
|
|
|
ast_audiohook_detach(&si->audiohook);
|
|
|
|
}
|
|
|
|
|
2008-08-05 16:56:11 +00:00
|
|
|
ast_datastore_free(datastore);
|
2008-05-01 16:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_new) {
|
|
|
|
datastore->data = si;
|
|
|
|
ast_channel_lock(chan);
|
|
|
|
ast_channel_datastore_add(chan, datastore);
|
|
|
|
ast_channel_unlock(chan);
|
|
|
|
ast_audiohook_attach(chan, &si->audiohook);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-01 18:28:38 +00:00
|
|
|
static int speex_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
struct ast_datastore *datastore = NULL;
|
|
|
|
struct speex_info *si = NULL;
|
|
|
|
struct speex_direction_info *sdi = NULL;
|
|
|
|
|
|
|
|
if (!chan) {
|
|
|
|
ast_log(LOG_ERROR, "%s cannot be used without a channel!\n", cmd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_channel_lock(chan);
|
|
|
|
if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) {
|
|
|
|
ast_channel_unlock(chan);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ast_channel_unlock(chan);
|
|
|
|
|
|
|
|
si = datastore->data;
|
|
|
|
|
|
|
|
if (!strcasecmp(data, "tx"))
|
|
|
|
sdi = si->tx;
|
|
|
|
else if (!strcasecmp(data, "rx"))
|
|
|
|
sdi = si->rx;
|
|
|
|
else {
|
|
|
|
ast_log(LOG_ERROR, "%s(%s) must either \"tx\" or \"rx\"\n", cmd, data);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(cmd, "agc"))
|
|
|
|
snprintf(buf, len, "%.01f", sdi ? sdi->agclevel : 0.0);
|
|
|
|
else
|
|
|
|
snprintf(buf, len, "%d", sdi ? sdi->denoise : 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-01 16:57:19 +00:00
|
|
|
static struct ast_custom_function agc_function = {
|
|
|
|
.name = "AGC",
|
|
|
|
.write = speex_write,
|
2009-04-29 18:53:01 +00:00
|
|
|
.read = speex_read,
|
|
|
|
.read_max = 22,
|
2008-05-01 16:57:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct ast_custom_function denoise_function = {
|
|
|
|
.name = "DENOISE",
|
|
|
|
.write = speex_write,
|
2009-04-29 18:53:01 +00:00
|
|
|
.read = speex_read,
|
|
|
|
.read_max = 22,
|
2008-05-01 16:57:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int unload_module(void)
|
|
|
|
{
|
|
|
|
ast_custom_function_unregister(&agc_function);
|
|
|
|
ast_custom_function_unregister(&denoise_function);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int load_module(void)
|
|
|
|
{
|
2008-05-13 17:42:17 +00:00
|
|
|
if (ast_custom_function_register(&agc_function)) {
|
|
|
|
return AST_MODULE_LOAD_DECLINE;
|
|
|
|
}
|
2008-05-01 16:57:19 +00:00
|
|
|
|
2008-05-13 17:42:17 +00:00
|
|
|
if (ast_custom_function_register(&denoise_function)) {
|
|
|
|
ast_custom_function_unregister(&agc_function);
|
|
|
|
return AST_MODULE_LOAD_DECLINE;
|
|
|
|
}
|
2008-05-01 16:57:19 +00:00
|
|
|
|
2008-05-13 17:42:17 +00:00
|
|
|
return AST_MODULE_LOAD_SUCCESS;
|
2008-05-01 16:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Noise reduction and Automatic Gain Control (AGC)");
|