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.
31 #include <gst/math-compat.h>
39 resample_sinc_window (double x, double halfwidth, double scale)
45 if (x < -halfwidth || x > halfwidth)
48 y = sin (x * M_PI * scale) / (x * M_PI * scale) * scale;
51 y *= (1 - x * x) * (1 - x * x);
57 resample_scale_ref (ResampleState * r)
60 RESAMPLE_DEBUG ("sample size %d", r->sample_size);
64 r->buffer_len = r->sample_size * r->filter_length;
65 r->buffer = malloc (r->buffer_len);
66 memset (r->buffer, 0, r->buffer_len);
69 r->i_inc = r->o_rate / r->i_rate;
70 r->o_inc = r->i_rate / r->o_rate;
71 RESAMPLE_DEBUG ("i_inc %g o_inc %g", r->i_inc, r->o_inc);
73 r->i_start = -r->i_inc * r->filter_length;
79 r->sinc_scale = r->i_inc;
80 if (r->sinc_scale == 0.5) {
81 /* strange things happen at integer multiples */
92 RESAMPLE_DEBUG ("asked to resample %d bytes", r->o_size);
93 RESAMPLE_DEBUG ("%d bytes in queue",
94 audioresample_buffer_queue_get_depth (r->queue));
96 while (r->o_size >= r->sample_size) {
101 midpoint = r->i_start + (r->filter_length - 1) * 0.5 * r->i_inc;
103 ("still need to output %d bytes, %d input left, i_start %g, midpoint %f",
104 r->o_size, audioresample_buffer_queue_get_depth (r->queue), r->i_start,
106 if (midpoint > 0.5 * r->i_inc) {
107 RESAMPLE_ERROR ("inconsistent state");
109 while (midpoint < -0.5 * r->i_inc) {
110 AudioresampleBuffer *buffer;
112 RESAMPLE_DEBUG ("midpoint %f < %f, r->i_inc %f", midpoint,
113 -0.5 * r->i_inc, r->i_inc);
114 buffer = audioresample_buffer_queue_pull (r->queue, r->sample_size);
115 if (buffer == NULL) {
116 /* FIXME: for the first buffer, this isn't necessarily an error,
117 * since because of the filter length we'll output less buffers.
118 * deal with that so we don't print to console */
119 RESAMPLE_ERROR ("buffer_queue_pull returned NULL");
123 r->i_start += r->i_inc;
124 RESAMPLE_DEBUG ("pulling (i_start = %g)", r->i_start);
126 midpoint += r->i_inc;
127 memmove (r->buffer, r->buffer + r->sample_size,
128 r->buffer_len - r->sample_size);
130 memcpy (r->buffer + r->buffer_len - r->sample_size, buffer->data,
132 r->buffer_filled = MIN (r->buffer_filled + r->sample_size, r->buffer_len);
134 audioresample_buffer_unref (buffer);
138 case RESAMPLE_FORMAT_S16:
139 for (i = 0; i < r->n_channels; i++) {
144 for (j = 0; j < r->filter_length; j++) {
145 offset = (r->i_start + j * r->i_inc) * r->o_inc;
146 x = *(int16_t *) (r->buffer + i * sizeof (int16_t) +
149 resample_sinc_window (offset, r->filter_length * 0.5,
157 *(int16_t *) (r->o_buf + i * sizeof (int16_t)) = rint (acc);
160 case RESAMPLE_FORMAT_S32:
161 for (i = 0; i < r->n_channels; i++) {
166 for (j = 0; j < r->filter_length; j++) {
167 offset = (r->i_start + j * r->i_inc) * r->o_inc;
168 x = *(int32_t *) (r->buffer + i * sizeof (int32_t) +
171 resample_sinc_window (offset, r->filter_length * 0.5,
174 if (acc < -2147483648.0)
176 if (acc > 2147483647.0)
179 *(int32_t *) (r->o_buf + i * sizeof (int32_t)) = rint (acc);
182 case RESAMPLE_FORMAT_F32:
183 for (i = 0; i < r->n_channels; i++) {
188 for (j = 0; j < r->filter_length; j++) {
189 offset = (r->i_start + j * r->i_inc) * r->o_inc;
190 x = *(float *) (r->buffer + i * sizeof (float) +
193 resample_sinc_window (offset, r->filter_length * 0.5,
197 *(float *) (r->o_buf + i * sizeof (float)) = acc;
200 case RESAMPLE_FORMAT_F64:
201 for (i = 0; i < r->n_channels; i++) {
206 for (j = 0; j < r->filter_length; j++) {
207 offset = (r->i_start + j * r->i_inc) * r->o_inc;
208 x = *(double *) (r->buffer + i * sizeof (double) +
211 resample_sinc_window (offset, r->filter_length * 0.5,
215 *(double *) (r->o_buf + i * sizeof (double)) = acc;
221 r->o_buf += r->sample_size;
222 r->o_size -= r->sample_size;