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 -- { */
40 #include "asterisk/module.h"
41 #include "asterisk/translate.h"
43 #include "libresample.h"
45 #include "slin_resample_ex.h"
47 #define RESAMPLER_QUALITY 0
49 #define OUTBUF_SIZE 8096
51 struct slin16_to_slin8_pvt {
53 double resample_factor;
56 struct slin8_to_slin16_pvt {
58 double resample_factor;
61 static int slin16_to_slin8_new(struct ast_trans_pvt *pvt)
63 struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
65 resamp_pvt->resample_factor = 0.5;
67 if (!(resamp_pvt->resampler = resample_open(RESAMPLER_QUALITY, 0.5, 0.5)))
73 static int slin8_to_slin16_new(struct ast_trans_pvt *pvt)
75 struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
77 resamp_pvt->resample_factor = 2.0;
79 if (!(resamp_pvt->resampler = resample_open(RESAMPLER_QUALITY, 2.0, 2.0)))
85 static void slin16_to_slin8_destroy(struct ast_trans_pvt *pvt)
87 struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
89 if (resamp_pvt->resampler)
90 resample_close(resamp_pvt->resampler);
93 static void slin8_to_slin16_destroy(struct ast_trans_pvt *pvt)
95 struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
97 if (resamp_pvt->resampler)
98 resample_close(resamp_pvt->resampler);
101 static int resample_frame(struct ast_trans_pvt *pvt,
102 void *resampler, double resample_factor, struct ast_frame *f)
104 int total_in_buf_used = 0;
105 int total_out_buf_used = 0;
106 int16_t *in_buf = (int16_t *) f->data;
107 int16_t *out_buf = (int16_t *) pvt->outbuf + pvt->samples;
108 float in_buf_f[f->samples];
109 float out_buf_f[2048];
113 for (i = 0; i < f->samples; i++)
114 in_buf_f[i] = in_buf[i] * (FLT_MAX / SHRT_MAX);
116 while (total_in_buf_used < f->samples) {
117 int in_buf_used, out_buf_used;
119 out_buf_used = resample_process(resampler, resample_factor,
120 &in_buf_f[total_in_buf_used], f->samples - total_in_buf_used,
122 &out_buf_f[total_out_buf_used], ARRAY_LEN(out_buf_f) - total_out_buf_used);
124 if (out_buf_used < 0)
127 total_out_buf_used += out_buf_used;
128 total_in_buf_used += in_buf_used;
130 if (total_out_buf_used == ARRAY_LEN(out_buf_f)) {
131 ast_log(LOG_ERROR, "Output buffer filled ... need to increase its size\n");
137 for (i = 0; i < total_out_buf_used; i++)
138 out_buf[i] = out_buf_f[i] * (SHRT_MAX / FLT_MAX);
140 pvt->samples += total_out_buf_used;
141 pvt->datalen += (total_out_buf_used * sizeof(int16_t));
146 static int slin16_to_slin8_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
148 struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
150 return resample_frame(pvt, resamp_pvt->resampler, resamp_pvt->resample_factor, f);
153 static int slin8_to_slin16_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
155 struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
157 return resample_frame(pvt, resamp_pvt->resampler, resamp_pvt->resample_factor, f);
160 static struct ast_frame *slin16_to_slin8_sample(void)
162 static struct ast_frame f = {
163 .frametype = AST_FRAME_VOICE,
164 .subclass = AST_FORMAT_SLINEAR16,
165 .datalen = sizeof(slin16_slin8_ex),
166 .samples = sizeof(slin16_slin8_ex) / sizeof(slin16_slin8_ex[0]),
167 .src = __PRETTY_FUNCTION__,
168 .data = slin16_slin8_ex,
174 static struct ast_frame *slin8_to_slin16_sample(void)
176 static struct ast_frame f = {
177 .frametype = AST_FRAME_VOICE,
178 .subclass = AST_FORMAT_SLINEAR,
179 .datalen = sizeof(slin8_slin16_ex),
180 .samples = sizeof(slin8_slin16_ex) / sizeof(slin8_slin16_ex[0]),
181 .src = __PRETTY_FUNCTION__,
182 .data = slin8_slin16_ex,
188 static struct ast_translator slin16_to_slin8 = {
189 .name = "slin16_to_slin8",
190 .srcfmt = AST_FORMAT_SLINEAR16,
191 .dstfmt = AST_FORMAT_SLINEAR,
192 .newpvt = slin16_to_slin8_new,
193 .destroy = slin16_to_slin8_destroy,
194 .framein = slin16_to_slin8_framein,
195 .sample = slin16_to_slin8_sample,
196 .desc_size = sizeof(struct slin16_to_slin8_pvt),
197 .buffer_samples = (OUTBUF_SIZE / sizeof(int16_t)),
198 .buf_size = OUTBUF_SIZE,
201 static struct ast_translator slin8_to_slin16 = {
202 .name = "slin8_to_slin16",
203 .srcfmt = AST_FORMAT_SLINEAR,
204 .dstfmt = AST_FORMAT_SLINEAR16,
205 .newpvt = slin8_to_slin16_new,
206 .destroy = slin8_to_slin16_destroy,
207 .framein = slin8_to_slin16_framein,
208 .sample = slin8_to_slin16_sample,
209 .desc_size = sizeof(struct slin8_to_slin16_pvt),
210 .buffer_samples = OUTBUF_SIZE,
211 .buf_size = OUTBUF_SIZE,
214 static int unload_module(void)
218 res |= ast_unregister_translator(&slin16_to_slin8);
219 res |= ast_unregister_translator(&slin8_to_slin16);
224 static int load_module(void)
228 res |= ast_register_translator(&slin16_to_slin8);
229 res |= ast_register_translator(&slin8_to_slin16);
231 return AST_MODULE_LOAD_SUCCESS;
234 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SLIN Resampling Codec");