Asterisk - The Open Source Telephony Project GIT-master-70eff7f
Loading...
Searching...
No Matches
func_speex.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2008, Digium, Inc.
5 *
6 * Brian Degenhardt <bmd@digium.com>
7 * Brett Bryant <bbryant@digium.com>
8 *
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
14 *
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
18 */
19
20/*! \file
21 *
22 * \brief Noise reduction and automatic gain control (AGC)
23 *
24 * \author Brian Degenhardt <bmd@digium.com>
25 * \author Brett Bryant <bbryant@digium.com>
26 *
27 * \ingroup functions
28 *
29 * The Speex 1.2 library - http://www.speex.org
30 * \note Requires the 1.2 version of the Speex library (which might not be what you find in Linux packages)
31 */
32
33/*** MODULEINFO
34 <depend>speex</depend>
35 <depend>speex_preprocess</depend>
36 <use type="external">speexdsp</use>
37 <support_level>core</support_level>
38 ***/
39
40#include "asterisk.h"
41
42#include <speex/speex_preprocess.h>
43#include "asterisk/module.h"
44#include "asterisk/channel.h"
45#include "asterisk/pbx.h"
46#include "asterisk/utils.h"
47#include "asterisk/audiohook.h"
48
49#define DEFAULT_AGC_LEVEL 8000.0
50
51/*** DOCUMENTATION
52 <function name="AGC" language="en_US">
53 <since>
54 <version>1.6.1.0</version>
55 </since>
56 <synopsis>
57 Apply automatic gain control to audio on a channel.
58 </synopsis>
59 <syntax>
60 <parameter name="channeldirection" required="true">
61 <para>This can be either <literal>rx</literal> or <literal>tx</literal></para>
62 </parameter>
63 </syntax>
64 <description>
65 <para>The AGC function will apply automatic gain control to the audio on the
66 channel that it is executed on. Using <literal>rx</literal> for audio received
67 and <literal>tx</literal> for audio transmitted to the channel. When using this
68 function you set a target audio level. It is primarily intended for use with
69 analog lines, but could be useful for other channels as well. The target volume
70 is set with a number between <literal>1-32768</literal>. The larger the number
71 the louder (more gain) the channel will receive.</para>
72 <example title="Apply automatic gain control">
73 exten => 1,1,Set(AGC(rx)=8000)
74 exten => 1,2,Set(AGC(tx)=off)
75 </example>
76 </description>
77 </function>
78 <function name="DENOISE" language="en_US">
79 <since>
80 <version>1.6.1.0</version>
81 </since>
82 <synopsis>
83 Apply noise reduction to audio on a channel.
84 </synopsis>
85 <syntax>
86 <parameter name="channeldirection" required="true">
87 <para>This can be either <literal>rx</literal> or <literal>tx</literal>
88 the values that can be set to this are either <literal>on</literal> and
89 <literal>off</literal></para>
90 </parameter>
91 </syntax>
92 <description>
93 <para>The DENOISE function will apply noise reduction to audio on the channel
94 that it is executed on. It is very useful for noisy analog lines, especially
95 when adjusting gains or using AGC. Use <literal>rx</literal> for audio received from the channel
96 and <literal>tx</literal> to apply the filter to the audio being sent to the channel.</para>
97 <example title="Apply noise reduction">
98 exten => 1,1,Set(DENOISE(rx)=on)
99 exten => 1,2,Set(DENOISE(tx)=off)
100 </example>
101 </description>
102 </function>
103 ***/
104
106 SpeexPreprocessState *state; /*!< speex preprocess state object */
107 int agc; /*!< audio gain control is enabled or not */
108 int denoise; /*!< denoise is enabled or not */
109 int samples; /*!< n of 8Khz samples in last frame */
110 float agclevel; /*!< audio gain control level [1.0 - 32768.0] */
111};
112
118
119static void destroy_callback(void *data)
120{
121 struct speex_info *si = data;
122
124
125 if (si->rx && si->rx->state) {
126 speex_preprocess_state_destroy(si->rx->state);
127 }
128
129 if (si->tx && si->tx->state) {
130 speex_preprocess_state_destroy(si->tx->state);
131 }
132
133 if (si->rx) {
134 ast_free(si->rx);
135 }
136
137 if (si->tx) {
138 ast_free(si->tx);
139 }
140
141 ast_free(data);
142};
143
144static const struct ast_datastore_info speex_datastore = {
145 .type = "speex",
146 .destroy = destroy_callback
147};
148
149static int speex_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
150{
151 struct ast_datastore *datastore = NULL;
152 struct speex_direction_info *sdi = NULL;
153 struct speex_info *si = NULL;
154 char source[80];
155
156 /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
158 return -1;
159 }
160
161 /* We are called with chan already locked */
162 if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) {
163 return -1;
164 }
165
166 si = datastore->data;
167
168 sdi = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? si->rx : si->tx;
169
170 if (!sdi) {
171 return -1;
172 }
173
174 if ((sdi->samples != frame->samples) || (ast_format_get_sample_rate(frame->subclass.format) != si->lastrate)) {
176 if (sdi->state) {
177 speex_preprocess_state_destroy(sdi->state);
178 }
179
180 if (!(sdi->state = speex_preprocess_state_init((sdi->samples = frame->samples), si->lastrate))) {
181 return -1;
182 }
183
184 speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_AGC, &sdi->agc);
185
186 if (sdi->agc) {
187 speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_AGC_LEVEL, &sdi->agclevel);
188 }
189
190 speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_DENOISE, &sdi->denoise);
191 }
192
193 speex_preprocess(sdi->state, frame->data.ptr, NULL);
194 snprintf(source, sizeof(source), "%s/speex", frame->src);
195 if (frame->mallocd & AST_MALLOCD_SRC) {
196 ast_free((char *) frame->src);
197 }
198 frame->src = ast_strdup(source);
199 frame->mallocd |= AST_MALLOCD_SRC;
200
201 return 0;
202}
203
204static int speex_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
205{
206 struct ast_datastore *datastore = NULL;
207 struct speex_info *si = NULL;
208 struct speex_direction_info **sdi = NULL;
209 int is_new = 0;
210
211 if (!chan) {
212 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
213 return -1;
214 }
215
216 if (strcasecmp(data, "rx") && strcasecmp(data, "tx")) {
217 ast_log(LOG_ERROR, "Invalid argument provided to the %s function\n", cmd);
218 return -1;
219 }
220
221 ast_channel_lock(chan);
222 if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) {
223
224 if (!(datastore = ast_datastore_alloc(&speex_datastore, NULL))) {
225 ast_channel_unlock(chan);
226 return 0;
227 }
228
229 if (!(si = ast_calloc(1, sizeof(*si)))) {
230 ast_channel_unlock(chan);
231 ast_datastore_free(datastore);
232 return 0;
233 }
234
237 si->lastrate = 8000;
238 is_new = 1;
239
240 } else {
241 si = datastore->data;
242 }
243
244 if (!strcasecmp(data, "rx")) {
245 sdi = &si->rx;
246 } else {
247 sdi = &si->tx;
248 }
249
250 if (!*sdi) {
251 if (!(*sdi = ast_calloc(1, sizeof(**sdi)))) {
252 if (is_new) {
253 datastore->data = si;
254 ast_channel_unlock(chan);
255 ast_datastore_free(datastore);
256 } else {
257 ast_channel_unlock(chan);
258 }
259 return 0;
260 }
261 /* Right now, the audiohooks API will _only_ provide us 8 kHz slinear
262 * audio. When it supports 16 kHz (or any other sample rates, we will
263 * have to take that into account here. */
264 (*sdi)->samples = -1;
265 }
266
267 if (!strcasecmp(cmd, "agc")) {
268 if (!sscanf(value, "%30f", &(*sdi)->agclevel))
269 (*sdi)->agclevel = ast_true(value) ? DEFAULT_AGC_LEVEL : 0.0;
270
271 if ((*sdi)->agclevel > 32768.0) {
272 ast_log(LOG_WARNING, "AGC(%s)=%.01f is greater than 32768... setting to 32768 instead\n",
273 ((*sdi == si->rx) ? "rx" : "tx"), (*sdi)->agclevel);
274 (*sdi)->agclevel = 32768.0;
275 }
276
277 (*sdi)->agc = !!((*sdi)->agclevel);
278
279 if ((*sdi)->state) {
280 speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_AGC, &(*sdi)->agc);
281 if ((*sdi)->agc) {
282 speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_AGC_LEVEL, &(*sdi)->agclevel);
283 }
284 }
285 } else if (!strcasecmp(cmd, "denoise")) {
286 (*sdi)->denoise = (ast_true(value) != 0);
287
288 if ((*sdi)->state) {
289 speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_DENOISE, &(*sdi)->denoise);
290 }
291 }
292
293 if (!(*sdi)->agc && !(*sdi)->denoise) {
294 if ((*sdi)->state)
295 speex_preprocess_state_destroy((*sdi)->state);
296
297 ast_free(*sdi);
298 *sdi = NULL;
299 }
300
301 if (!si->rx && !si->tx) {
302 if (is_new) {
303 datastore->data = si;
304 is_new = 0;
305 ast_channel_unlock(chan);
306 } else {
307 ast_channel_datastore_remove(chan, datastore);
309 ast_channel_unlock(chan);
311 }
312 ast_datastore_free(datastore);
313 return 0;
314 }
315
316 if (is_new) {
317 datastore->data = si;
318 ast_channel_datastore_add(chan, datastore);
319 ast_channel_unlock(chan);
321 } else {
322 ast_channel_unlock(chan);
323 }
324
325 return 0;
326}
327
328static int speex_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
329{
330 struct ast_datastore *datastore = NULL;
331 struct speex_info *si = NULL;
332 struct speex_direction_info *sdi = NULL;
333
334 if (!chan) {
335 ast_log(LOG_ERROR, "%s cannot be used without a channel!\n", cmd);
336 return -1;
337 }
338
339 ast_channel_lock(chan);
340 if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) {
341 ast_channel_unlock(chan);
342 return -1;
343 }
344
345 si = datastore->data;
346
347 if (!strcasecmp(data, "tx"))
348 sdi = si->tx;
349 else if (!strcasecmp(data, "rx"))
350 sdi = si->rx;
351 else {
352 ast_channel_unlock(chan);
353 ast_log(LOG_ERROR, "%s(%s) must either \"tx\" or \"rx\"\n", cmd, data);
354 return -1;
355 }
356
357 if (!strcasecmp(cmd, "agc"))
358 snprintf(buf, len, "%.01f", sdi ? sdi->agclevel : 0.0);
359 else
360 snprintf(buf, len, "%d", sdi ? sdi->denoise : 0);
361
362 ast_channel_unlock(chan);
363 return 0;
364}
365
367 .name = "AGC",
368 .write = speex_write,
369 .read = speex_read,
370 .read_max = 22,
371};
372
374 .name = "DENOISE",
375 .write = speex_write,
376 .read = speex_read,
377 .read_max = 22,
378};
379
386
400
401AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Noise reduction and Automatic Gain Control (AGC)");
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition astmm.h:241
#define ast_calloc(num, len)
A wrapper for calloc()
Definition astmm.h:202
#define ast_log
Definition astobj2.c:42
Audiohooks Architecture.
@ AST_AUDIOHOOK_MANIPULATE_ALL_RATES
Definition audiohook.h:75
int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source, enum ast_audiohook_init_flags flags)
Initialize an audiohook structure.
Definition audiohook.c:324
int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook)
Remove an audiohook from a specified channel.
Definition audiohook.c:992
ast_audiohook_direction
Definition audiohook.h:48
@ AST_AUDIOHOOK_DIRECTION_READ
Definition audiohook.h:49
int ast_audiohook_detach(struct ast_audiohook *audiohook)
Detach audiohook from channel.
Definition audiohook.c:821
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
Definition audiohook.c:745
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
Definition audiohook.c:348
@ AST_AUDIOHOOK_TYPE_MANIPULATE
Definition audiohook.h:38
@ AST_AUDIOHOOK_STATUS_DONE
Definition audiohook.h:45
General Asterisk PBX channel definitions.
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition channel.c:2376
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
Remove a datastore from a channel.
Definition channel.c:2385
#define ast_channel_lock(chan)
Definition channel.h:2983
#define ast_channel_unlock(chan)
Definition channel.h:2984
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition channel.c:2390
#define ast_datastore_alloc(info, uid)
Definition datastore.h:85
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition datastore.c:68
char buf[BUFSIZE]
Definition eagi_proxy.c:66
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition format.c:379
direction
static int speex_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition func_speex.c:328
static struct ast_custom_function agc_function
Definition func_speex.c:366
#define DEFAULT_AGC_LEVEL
Definition func_speex.c:49
static struct ast_custom_function denoise_function
Definition func_speex.c:373
static int speex_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Definition func_speex.c:204
static void destroy_callback(void *data)
Definition func_speex.c:119
static int load_module(void)
Definition func_speex.c:387
static int speex_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
Definition func_speex.c:149
static int unload_module(void)
Definition func_speex.c:380
static const struct ast_datastore_info speex_datastore
Definition func_speex.c:144
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define AST_MALLOCD_SRC
#define LOG_ERROR
#define LOG_WARNING
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition module.h:78
Core PBX routines and definitions.
#define ast_custom_function_register(acf)
Register a custom function.
Definition pbx.h:1563
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
#define NULL
Definition resample.c:96
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition utils.c:2233
ast_audiohook_manipulate_callback manipulate_callback
Definition audiohook.h:118
enum ast_audiohook_status status
Definition audiohook.h:108
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition pbx.h:118
const char * name
Definition pbx.h:119
Structure for a data store type.
Definition datastore.h:31
const char * type
Definition datastore.h:32
Structure for a data store object.
Definition datastore.h:64
void * data
Definition datastore.h:66
struct ast_format * format
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
enum ast_frame_type frametype
union ast_frame::@237 data
SpeexPreprocessState * state
Definition func_speex.c:106
struct speex_direction_info * rx
Definition func_speex.c:116
struct ast_audiohook audiohook
Definition func_speex.c:114
struct speex_direction_info * tx
Definition func_speex.c:116
int value
Definition syslog.c:37
Utility functions.