2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, Digium, Inc.
6 * Russell Bryant <russell@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
22 * \brief Resample slinear audio
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 /* These are for SHRT_MAX and FLT_MAX -- { */
32 #if defined(__Darwin__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
40 #include "asterisk/module.h"
41 #include "asterisk/translate.h"
42 #include "asterisk/libresample.h"
44 #include "slin_resample_ex.h"
46 #define RESAMPLER_QUALITY 0
48 #define OUTBUF_SIZE 8096
50 struct slin16_to_slin8_pvt {
52 double resample_factor;
55 struct slin8_to_slin16_pvt {
57 double resample_factor;
60 static int slin16_to_slin8_new(struct ast_trans_pvt *pvt)
62 struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
64 resamp_pvt->resample_factor = 0.5;
66 if (!(resamp_pvt->resampler = resample_open(RESAMPLER_QUALITY, 0.5, 0.5)))
72 static int slin8_to_slin16_new(struct ast_trans_pvt *pvt)
74 struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
76 resamp_pvt->resample_factor = 2.0;
78 if (!(resamp_pvt->resampler = resample_open(RESAMPLER_QUALITY, 2.0, 2.0)))
84 static void slin16_to_slin8_destroy(struct ast_trans_pvt *pvt)
86 struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
88 if (resamp_pvt->resampler)
89 resample_close(resamp_pvt->resampler);
92 static void slin8_to_slin16_destroy(struct ast_trans_pvt *pvt)
94 struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
96 if (resamp_pvt->resampler)
97 resample_close(resamp_pvt->resampler);
100 static int resample_frame(struct ast_trans_pvt *pvt,
101 void *resampler, double resample_factor, struct ast_frame *f)
103 int total_in_buf_used = 0;
104 int total_out_buf_used = 0;
105 int16_t *in_buf = (int16_t *) f->data;
106 int16_t *out_buf = (int16_t *) pvt->outbuf + pvt->samples;
107 float in_buf_f[f->samples];
108 float out_buf_f[2048];
112 for (i = 0; i < f->samples; i++)
113 in_buf_f[i] = in_buf[i] * (FLT_MAX / SHRT_MAX);
115 while (total_in_buf_used < f->samples) {
116 int in_buf_used, out_buf_used;
118 out_buf_used = resample_process(resampler, resample_factor,
119 &in_buf_f[total_in_buf_used], f->samples - total_in_buf_used,
121 &out_buf_f[total_out_buf_used], ARRAY_LEN(out_buf_f) - total_out_buf_used);
123 if (out_buf_used < 0)
126 total_out_buf_used += out_buf_used;
127 total_in_buf_used += in_buf_used;
129 if (total_out_buf_used == ARRAY_LEN(out_buf_f)) {
130 ast_log(LOG_ERROR, "Output buffer filled ... need to increase its size\n");
136 for (i = 0; i < total_out_buf_used; i++)
137 out_buf[i] = out_buf_f[i] * (SHRT_MAX / FLT_MAX);
139 pvt->samples += total_out_buf_used;
140 pvt->datalen += (total_out_buf_used * sizeof(int16_t));
145 static int slin16_to_slin8_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
147 struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
149 return resample_frame(pvt, resamp_pvt->resampler, resamp_pvt->resample_factor, f);
152 static int slin8_to_slin16_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
154 struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
156 return resample_frame(pvt, resamp_pvt->resampler, resamp_pvt->resample_factor, f);
159 static struct ast_frame *slin16_to_slin8_sample(void)
161 static struct ast_frame f = {
162 .frametype = AST_FRAME_VOICE,
163 .subclass = AST_FORMAT_SLINEAR16,
164 .datalen = sizeof(slin16_slin8_ex),
165 .samples = sizeof(slin16_slin8_ex) / sizeof(slin16_slin8_ex[0]),
166 .src = __PRETTY_FUNCTION__,
167 .data = slin16_slin8_ex,
173 static struct ast_frame *slin8_to_slin16_sample(void)
175 static struct ast_frame f = {
176 .frametype = AST_FRAME_VOICE,
177 .subclass = AST_FORMAT_SLINEAR,
178 .datalen = sizeof(slin8_slin16_ex),
179 .samples = sizeof(slin8_slin16_ex) / sizeof(slin8_slin16_ex[0]),
180 .src = __PRETTY_FUNCTION__,
181 .data = slin8_slin16_ex,
187 static struct ast_translator slin16_to_slin8 = {
188 .name = "slin16_to_slin8",
189 .srcfmt = AST_FORMAT_SLINEAR16,
190 .dstfmt = AST_FORMAT_SLINEAR,
191 .newpvt = slin16_to_slin8_new,
192 .destroy = slin16_to_slin8_destroy,
193 .framein = slin16_to_slin8_framein,
194 .sample = slin16_to_slin8_sample,
195 .desc_size = sizeof(struct slin16_to_slin8_pvt),
196 .buffer_samples = (OUTBUF_SIZE / sizeof(int16_t)),
197 .buf_size = OUTBUF_SIZE,
200 static struct ast_translator slin8_to_slin16 = {
201 .name = "slin8_to_slin16",
202 .srcfmt = AST_FORMAT_SLINEAR,
203 .dstfmt = AST_FORMAT_SLINEAR16,
204 .newpvt = slin8_to_slin16_new,
205 .destroy = slin8_to_slin16_destroy,
206 .framein = slin8_to_slin16_framein,
207 .sample = slin8_to_slin16_sample,
208 .desc_size = sizeof(struct slin8_to_slin16_pvt),
209 .buffer_samples = OUTBUF_SIZE,
210 .buf_size = OUTBUF_SIZE,
213 static int unload_module(void)
217 res |= ast_unregister_translator(&slin16_to_slin8);
218 res |= ast_unregister_translator(&slin8_to_slin16);
223 static int load_module(void)
227 res |= ast_register_translator(&slin16_to_slin8);
228 res |= ast_register_translator(&slin8_to_slin16);
230 return AST_MODULE_LOAD_SUCCESS;
233 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SLIN Resampling Codec");