2 * Copyright (C) <2001> David A. Schleef <ds@schleef.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
30 #include <liboil/liboil.h>
32 #include <audioresample/resample.h>
33 #include <audioresample/buffer.h>
34 #include <audioresample/debug.h>
37 func_sinc (double *fx, double *dfx, double x, void *closure)
39 //double scale = *(double *)closure;
50 *dfx = scale * (cos (x) - sin (x) / x) / x;
54 func_hanning (double *fx, double *dfx, double x, void *closure)
56 double width = *(double *) closure;
58 if (x < width && x > -width) {
60 *fx = (1 - x * x) * (1 - x * x);
61 *dfx = -2 * 2 * x / width * (1 - x * x);
70 resample_sinc_window (double x, double halfwidth, double scale)
76 if (x < -halfwidth || x > halfwidth)
79 y = sin (x * M_PI * scale) / (x * M_PI * scale) * scale;
82 y *= (1 - x * x) * (1 - x * x);
90 functable_test (Functable * ft, double halfwidth)
95 for (i = 0; i < 100; i++) {
97 printf ("%d %g %g\n", i, resample_sinc_window (x, halfwidth, 1.0),
98 functable_evaluate (ft, x));
107 resample_scale_functable (ResampleState * r)
109 if (r->need_reinit) {
110 double hanning_width;
112 r->sample_size = r->n_channels * resample_format_size (r->format);
113 RESAMPLE_DEBUG ("sample size %d", r->sample_size);
117 r->buffer_len = r->sample_size * r->filter_length;
118 r->buffer = malloc (r->buffer_len);
119 memset (r->buffer, 0, r->buffer_len);
121 r->i_inc = r->o_rate / r->i_rate;
122 r->o_inc = r->i_rate / r->o_rate;
123 RESAMPLE_DEBUG ("i_inc %g o_inc %g", r->i_inc, r->o_inc);
125 r->i_start = -r->i_inc * r->filter_length;
128 functable_free (r->ft);
130 r->ft = functable_new ();
131 functable_set_length (r->ft, r->filter_length * 16);
132 functable_set_offset (r->ft, -r->filter_length / 2);
133 functable_set_multiplier (r->ft, 1 / 16.0);
135 hanning_width = r->filter_length / 2;
136 functable_calculate (r->ft, func_sinc, NULL);
137 functable_calculate_multiply (r->ft, func_hanning, &hanning_width);
139 //functable_test(r->ft, 0.5 * r->filter_length);
141 if (r->i_inc < 1.0) {
142 r->sinc_scale = r->i_inc;
143 if (r->sinc_scale == 0.5) {
144 /* strange things happen at integer multiples */
157 while (r->o_size > 0) {
162 RESAMPLE_DEBUG ("i_start %g", r->i_start);
163 midpoint = r->i_start + (r->filter_length - 1) * 0.5 * r->i_inc;
164 if (midpoint > 0.5 * r->i_inc) {
165 RESAMPLE_ERROR ("inconsistent state");
167 while (midpoint < -0.5 * r->i_inc) {
168 AudioresampleBuffer *buffer;
170 buffer = audioresample_buffer_queue_pull (r->queue, r->sample_size);
171 if (buffer == NULL) {
172 RESAMPLE_ERROR ("buffer_queue_pull returned NULL");
176 r->i_start += r->i_inc;
177 RESAMPLE_DEBUG ("pulling (i_start = %g)", r->i_start);
179 midpoint += r->i_inc;
180 memmove (r->buffer, r->buffer + r->sample_size,
181 r->buffer_len - r->sample_size);
183 memcpy (r->buffer + r->buffer_len - r->sample_size, buffer->data,
185 audioresample_buffer_unref (buffer);
189 case RESAMPLE_FORMAT_S16:
190 for (i = 0; i < r->n_channels; i++) {
195 for (j = 0; j < r->filter_length; j++) {
196 offset = (r->i_start + j * r->i_inc) * r->o_inc;
197 x = *(int16_t *) (r->buffer + i * sizeof (int16_t) +
199 acc += functable_evaluate (r->ft, offset) * x;
200 //acc += resample_sinc_window (offset, r->filter_length * 0.5, r->sinc_scale) * x;
207 *(int16_t *) (r->o_buf + i * sizeof (int16_t)) = rint (acc);
210 case RESAMPLE_FORMAT_S32:
211 for (i = 0; i < r->n_channels; i++) {
216 for (j = 0; j < r->filter_length; j++) {
217 offset = (r->i_start + j * r->i_inc) * r->o_inc;
218 x = *(int32_t *) (r->buffer + i * sizeof (int32_t) +
220 acc += functable_evaluate (r->ft, offset) * x;
221 //acc += resample_sinc_window (offset, r->filter_length * 0.5, r->sinc_scale) * x;
223 if (acc < -2147483648.0)
225 if (acc > 2147483647.0)
228 *(int32_t *) (r->o_buf + i * sizeof (int32_t)) = rint (acc);
231 case RESAMPLE_FORMAT_F32:
232 for (i = 0; i < r->n_channels; i++) {
237 for (j = 0; j < r->filter_length; j++) {
238 offset = (r->i_start + j * r->i_inc) * r->o_inc;
239 x = *(float *) (r->buffer + i * sizeof (float) +
241 acc += functable_evaluate (r->ft, offset) * x;
242 //acc += resample_sinc_window (offset, r->filter_length * 0.5, r->sinc_scale) * x;
245 *(float *) (r->o_buf + i * sizeof (float)) = acc;
248 case RESAMPLE_FORMAT_F64:
249 for (i = 0; i < r->n_channels; i++) {
254 for (j = 0; j < r->filter_length; j++) {
255 offset = (r->i_start + j * r->i_inc) * r->o_inc;
256 x = *(double *) (r->buffer + i * sizeof (double) +
258 acc += functable_evaluate (r->ft, offset) * x;
259 //acc += resample_sinc_window (offset, r->filter_length * 0.5, r->sinc_scale) * x;
262 *(double *) (r->o_buf + i * sizeof (double)) = acc;
268 r->o_buf += r->sample_size;
269 r->o_size -= r->sample_size;