2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2011, Digium, Inc.
6 * Russell Bryant <russell@digium.com>
7 * David Vossel <dvossel@digium.com>
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.
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.
23 * \brief Resample slinear audio
29 <depend>resample</depend>
30 <support_level>core</support_level>
34 #include "speex/speex_resampler.h"
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include "asterisk/module.h"
39 #include "asterisk/translate.h"
40 #include "asterisk/slin.h"
42 #define OUTBUF_SIZE 8096
44 static struct ast_translator *translators;
45 static int trans_size;
46 static int id_list[] = {
55 AST_FORMAT_SLINEAR192,
58 static int resamp_new(struct ast_trans_pvt *pvt)
62 if (!(pvt->pvt = speex_resampler_init(1, ast_format_rate(&pvt->t->src_format), ast_format_rate(&pvt->t->dst_format), 5, &err))) {
69 static void resamp_destroy(struct ast_trans_pvt *pvt)
71 SpeexResamplerState *resamp_pvt = pvt->pvt;
72 speex_resampler_destroy(resamp_pvt);
75 static int resamp_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
77 SpeexResamplerState *resamp_pvt = pvt->pvt;
78 unsigned int out_samples = (OUTBUF_SIZE / sizeof(int16_t)) - pvt->samples;
79 unsigned int in_samples;
84 in_samples = f->datalen / 2;
86 speex_resampler_process_int(resamp_pvt,
90 pvt->outbuf.i16 + pvt->samples,
93 pvt->samples += out_samples;
94 pvt->datalen += out_samples * 2;
99 static int unload_module(void)
104 for (idx = 0; idx < trans_size; idx++) {
105 res |= ast_unregister_translator(&translators[idx]);
107 ast_free(translators);
112 static int load_module(void)
117 trans_size = ARRAY_LEN(id_list) * ARRAY_LEN(id_list);
118 if (!(translators = ast_calloc(1, sizeof(struct ast_translator) * trans_size))) {
119 return AST_MODULE_LOAD_FAILURE;
122 for (x = 0; x < ARRAY_LEN(id_list); x++) {
123 for (y = 0; y < ARRAY_LEN(id_list); y++) {
127 translators[idx].newpvt = resamp_new;
128 translators[idx].destroy = resamp_destroy;
129 translators[idx].framein = resamp_framein;
130 translators[idx].desc_size = 0;
131 translators[idx].buffer_samples = (OUTBUF_SIZE / sizeof(int16_t));
132 translators[idx].buf_size = OUTBUF_SIZE;
133 ast_format_set(&translators[idx].src_format, id_list[x], 0);
134 ast_format_set(&translators[idx].dst_format, id_list[y], 0);
135 snprintf(translators[idx].name, sizeof(translators[idx].name), "slin %dkhz -> %dkhz",
136 ast_format_rate(&translators[idx].src_format), ast_format_rate(&translators[idx].dst_format));
137 res |= ast_register_translator(&translators[idx]);
143 return AST_MODULE_LOAD_SUCCESS;
146 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SLIN Resampling Codec");