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>
38 resample_sinc_window (double x, double halfwidth, double scale)
44 if (x < -halfwidth || x > halfwidth)
47 y = sin (x * M_PI * scale) / (x * M_PI * scale) * scale;
50 y *= (1 - x * x) * (1 - x * x);
56 resample_scale_chunk (ResampleState * r)
59 r->sample_size = r->n_channels * resample_format_size (r->format);
60 RESAMPLE_DEBUG ("sample size %d", r->sample_size);
64 r->buffer_len = r->sample_size * 1000;
65 r->buffer = malloc (r->buffer_len);
66 memset (r->buffer, 0, r->buffer_len);
68 r->i_inc = r->o_rate / r->i_rate;
69 r->o_inc = r->i_rate / r->o_rate;
70 RESAMPLE_DEBUG ("i_inc %g o_inc %g", r->i_inc, r->o_inc);
72 r->i_start = -r->i_inc * r->filter_length;
78 r->sinc_scale = r->i_inc;
79 if (r->sinc_scale == 0.5) {
80 /* strange things happen at integer multiples */
91 while (r->o_size > 0) {
96 RESAMPLE_DEBUG ("i_start %g", r->i_start);
97 midpoint = r->i_start + (r->filter_length - 1) * 0.5 * r->i_inc;
98 if (midpoint > 0.5 * r->i_inc) {
99 RESAMPLE_ERROR ("inconsistent state");
101 while (midpoint < -0.5 * r->i_inc) {
102 AudioresampleBuffer *buffer;
104 buffer = audioresample_buffer_queue_pull (r->queue, r->sample_size);
105 if (buffer == NULL) {
106 RESAMPLE_ERROR ("buffer_queue_pull returned NULL");
110 r->i_start += r->i_inc;
111 RESAMPLE_DEBUG ("pulling (i_start = %g)", r->i_start);
113 midpoint += r->i_inc;
114 memmove (r->buffer, r->buffer + r->sample_size,
115 r->buffer_len - r->sample_size);
117 memcpy (r->buffer + r->buffer_len - r->sample_size, buffer->data,
119 audioresample_buffer_unref (buffer);
123 case RESAMPLE_FORMAT_S16:
124 for (i = 0; i < r->n_channels; i++) {
129 for (j = 0; j < r->filter_length; j++) {
130 offset = (r->i_start + j * r->i_inc) * r->o_inc;
131 x = *(int16_t *) (r->buffer + i * sizeof (int16_t) +
134 resample_sinc_window (offset, r->filter_length * 0.5,
142 *(int16_t *) (r->o_buf + i * sizeof (int16_t)) = rint (acc);
145 case RESAMPLE_FORMAT_S32:
146 for (i = 0; i < r->n_channels; i++) {
151 for (j = 0; j < r->filter_length; j++) {
152 offset = (r->i_start + j * r->i_inc) * r->o_inc;
153 x = *(int32_t *) (r->buffer + i * sizeof (int32_t) +
156 resample_sinc_window (offset, r->filter_length * 0.5,
159 if (acc < -2147483648.0)
161 if (acc > 2147483647.0)
164 *(int32_t *) (r->o_buf + i * sizeof (int32_t)) = rint (acc);
167 case RESAMPLE_FORMAT_F32:
168 for (i = 0; i < r->n_channels; i++) {
173 for (j = 0; j < r->filter_length; j++) {
174 offset = (r->i_start + j * r->i_inc) * r->o_inc;
175 x = *(float *) (r->buffer + i * sizeof (float) +
178 resample_sinc_window (offset, r->filter_length * 0.5,
182 *(float *) (r->o_buf + i * sizeof (float)) = acc;
185 case RESAMPLE_FORMAT_F64:
186 for (i = 0; i < r->n_channels; i++) {
191 for (j = 0; j < r->filter_length; j++) {
192 offset = (r->i_start + j * r->i_inc) * r->o_inc;
193 x = *(double *) (r->buffer + i * sizeof (double) +
196 resample_sinc_window (offset, r->filter_length * 0.5,
200 *(double *) (r->o_buf + i * sizeof (double)) = acc;
206 r->o_buf += r->sample_size;
207 r->o_size -= r->sample_size;