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>
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_ref (ResampleState * r)
59 RESAMPLE_DEBUG ("sample size %d", r->sample_size);
63 r->buffer_len = r->sample_size * r->filter_length;
64 r->buffer = malloc (r->buffer_len);
65 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 RESAMPLE_DEBUG ("asked to resample %d bytes", r->o_size);
92 RESAMPLE_DEBUG ("%d bytes in queue",
93 audioresample_buffer_queue_get_depth (r->queue));
95 while (r->o_size >= r->sample_size) {
100 midpoint = r->i_start + (r->filter_length - 1) * 0.5 * r->i_inc;
102 ("still need to output %d bytes, %d input left, i_start %g, midpoint %f",
103 r->o_size, audioresample_buffer_queue_get_depth (r->queue), r->i_start,
105 if (midpoint > 0.5 * r->i_inc) {
106 RESAMPLE_ERROR ("inconsistent state");
108 while (midpoint < -0.5 * r->i_inc) {
109 AudioresampleBuffer *buffer;
111 RESAMPLE_DEBUG ("midpoint %f < %f, r->i_inc %f", midpoint,
112 -0.5 * r->i_inc, r->i_inc);
113 buffer = audioresample_buffer_queue_pull (r->queue, r->sample_size);
114 if (buffer == NULL) {
115 /* FIXME: for the first buffer, this isn't necessarily an error,
116 * since because of the filter length we'll output less buffers.
117 * deal with that so we don't print to console */
118 RESAMPLE_ERROR ("buffer_queue_pull returned NULL");
122 r->i_start += r->i_inc;
123 RESAMPLE_DEBUG ("pulling (i_start = %g)", r->i_start);
125 midpoint += r->i_inc;
126 memmove (r->buffer, r->buffer + r->sample_size,
127 r->buffer_len - r->sample_size);
129 memcpy (r->buffer + r->buffer_len - r->sample_size, buffer->data,
131 r->buffer_filled = MIN (r->buffer_filled + r->sample_size, r->buffer_len);
133 audioresample_buffer_unref (buffer);
137 case RESAMPLE_FORMAT_S16:
138 for (i = 0; i < r->n_channels; i++) {
143 for (j = 0; j < r->filter_length; j++) {
144 offset = (r->i_start + j * r->i_inc) * r->o_inc;
145 x = *(int16_t *) (r->buffer + i * sizeof (int16_t) +
148 resample_sinc_window (offset, r->filter_length * 0.5,
156 *(int16_t *) (r->o_buf + i * sizeof (int16_t)) = rint (acc);
159 case RESAMPLE_FORMAT_S32:
160 for (i = 0; i < r->n_channels; i++) {
165 for (j = 0; j < r->filter_length; j++) {
166 offset = (r->i_start + j * r->i_inc) * r->o_inc;
167 x = *(int32_t *) (r->buffer + i * sizeof (int32_t) +
170 resample_sinc_window (offset, r->filter_length * 0.5,
173 if (acc < -2147483648.0)
175 if (acc > 2147483647.0)
178 *(int32_t *) (r->o_buf + i * sizeof (int32_t)) = rint (acc);
181 case RESAMPLE_FORMAT_F32:
182 for (i = 0; i < r->n_channels; i++) {
187 for (j = 0; j < r->filter_length; j++) {
188 offset = (r->i_start + j * r->i_inc) * r->o_inc;
189 x = *(float *) (r->buffer + i * sizeof (float) +
192 resample_sinc_window (offset, r->filter_length * 0.5,
196 *(float *) (r->o_buf + i * sizeof (float)) = acc;
199 case RESAMPLE_FORMAT_F64:
200 for (i = 0; i < r->n_channels; i++) {
205 for (j = 0; j < r->filter_length; j++) {
206 offset = (r->i_start + j * r->i_inc) * r->o_inc;
207 x = *(double *) (r->buffer + i * sizeof (double) +
210 resample_sinc_window (offset, r->filter_length * 0.5,
214 *(double *) (r->o_buf + i * sizeof (double)) = acc;
220 r->o_buf += r->sample_size;
221 r->o_size -= r->sample_size;