resample_ref.c 6.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/* Resampling library
 * Copyright (C) <2001> David A. Schleef <ds@schleef.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif


#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <liboil/liboil.h>

32 33 34
#include "resample.h"
#include "buffer.h"
#include "debug.h"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65


static double
resample_sinc_window (double x, double halfwidth, double scale)
{
  double y;

  if (x == 0)
    return 1.0;
  if (x < -halfwidth || x > halfwidth)
    return 0.0;

  y = sin (x * M_PI * scale) / (x * M_PI * scale) * scale;

  x /= halfwidth;
  y *= (1 - x * x) * (1 - x * x);

  return y;
}

void
resample_scale_ref (ResampleState * r)
{
  if (r->need_reinit) {
    RESAMPLE_DEBUG ("sample size %d", r->sample_size);

    if (r->buffer)
      free (r->buffer);
    r->buffer_len = r->sample_size * r->filter_length;
    r->buffer = malloc (r->buffer_len);
    memset (r->buffer, 0, r->buffer_len);
66
    r->buffer_filled = 0;
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

    r->i_inc = r->o_rate / r->i_rate;
    r->o_inc = r->i_rate / r->o_rate;
    RESAMPLE_DEBUG ("i_inc %g o_inc %g", r->i_inc, r->o_inc);

    r->i_start = -r->i_inc * r->filter_length;

    r->need_reinit = 0;

#if 0
    if (r->i_inc < 1.0) {
      r->sinc_scale = r->i_inc;
      if (r->sinc_scale == 0.5) {
        /* strange things happen at integer multiples */
        r->sinc_scale = 1.0;
      }
    } else {
      r->sinc_scale = 1.0;
    }
#else
    r->sinc_scale = 1.0;
#endif
  }

91
  RESAMPLE_DEBUG ("asked to resample %d bytes", r->o_size);
92 93
  RESAMPLE_DEBUG ("%d bytes in queue",
      audioresample_buffer_queue_get_depth (r->queue));
94 95

  while (r->o_size >= r->sample_size) {
96 97 98 99 100
    double midpoint;
    int i;
    int j;

    midpoint = r->i_start + (r->filter_length - 1) * 0.5 * r->i_inc;
101 102 103 104
    RESAMPLE_DEBUG
        ("still need to output %d bytes, %d input left, i_start %g, midpoint %f",
        r->o_size, audioresample_buffer_queue_get_depth (r->queue), r->i_start,
        midpoint);
105 106 107 108 109 110
    if (midpoint > 0.5 * r->i_inc) {
      RESAMPLE_ERROR ("inconsistent state");
    }
    while (midpoint < -0.5 * r->i_inc) {
      AudioresampleBuffer *buffer;

111 112
      RESAMPLE_DEBUG ("midpoint %f < %f, r->i_inc %f", midpoint,
          -0.5 * r->i_inc, r->i_inc);
113 114
      buffer = audioresample_buffer_queue_pull (r->queue, r->sample_size);
      if (buffer == NULL) {
115 116 117
        /* FIXME: for the first buffer, this isn't necessarily an error,
         * since because of the filter length we'll output less buffers.
         * deal with that so we don't print to console */
118 119 120 121 122 123 124 125 126 127 128 129 130
        RESAMPLE_ERROR ("buffer_queue_pull returned NULL");
        return;
      }

      r->i_start += r->i_inc;
      RESAMPLE_DEBUG ("pulling (i_start = %g)", r->i_start);

      midpoint += r->i_inc;
      memmove (r->buffer, r->buffer + r->sample_size,
          r->buffer_len - r->sample_size);

      memcpy (r->buffer + r->buffer_len - r->sample_size, buffer->data,
          r->sample_size);
131 132
      r->buffer_filled = MIN (r->buffer_filled + r->sample_size, r->buffer_len);

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
      audioresample_buffer_unref (buffer);
    }

    switch (r->format) {
      case RESAMPLE_FORMAT_S16:
        for (i = 0; i < r->n_channels; i++) {
          double acc = 0;
          double offset;
          double x;

          for (j = 0; j < r->filter_length; j++) {
            offset = (r->i_start + j * r->i_inc) * r->o_inc;
            x = *(int16_t *) (r->buffer + i * sizeof (int16_t) +
                j * r->sample_size);
            acc +=
                resample_sinc_window (offset, r->filter_length * 0.5,
                r->sinc_scale) * x;
          }
          if (acc < -32768.0)
            acc = -32768.0;
          if (acc > 32767.0)
            acc = 32767.0;

          *(int16_t *) (r->o_buf + i * sizeof (int16_t)) = rint (acc);
        }
        break;
      case RESAMPLE_FORMAT_S32:
        for (i = 0; i < r->n_channels; i++) {
          double acc = 0;
          double offset;
          double x;

          for (j = 0; j < r->filter_length; j++) {
            offset = (r->i_start + j * r->i_inc) * r->o_inc;
            x = *(int32_t *) (r->buffer + i * sizeof (int32_t) +
                j * r->sample_size);
            acc +=
                resample_sinc_window (offset, r->filter_length * 0.5,
                r->sinc_scale) * x;
          }
          if (acc < -2147483648.0)
            acc = -2147483648.0;
          if (acc > 2147483647.0)
            acc = 2147483647.0;

          *(int32_t *) (r->o_buf + i * sizeof (int32_t)) = rint (acc);
        }
        break;
      case RESAMPLE_FORMAT_F32:
        for (i = 0; i < r->n_channels; i++) {
          double acc = 0;
          double offset;
          double x;

          for (j = 0; j < r->filter_length; j++) {
            offset = (r->i_start + j * r->i_inc) * r->o_inc;
            x = *(float *) (r->buffer + i * sizeof (float) +
                j * r->sample_size);
            acc +=
                resample_sinc_window (offset, r->filter_length * 0.5,
                r->sinc_scale) * x;
          }

          *(float *) (r->o_buf + i * sizeof (float)) = acc;
        }
        break;
      case RESAMPLE_FORMAT_F64:
        for (i = 0; i < r->n_channels; i++) {
          double acc = 0;
          double offset;
          double x;

          for (j = 0; j < r->filter_length; j++) {
            offset = (r->i_start + j * r->i_inc) * r->o_inc;
            x = *(double *) (r->buffer + i * sizeof (double) +
                j * r->sample_size);
            acc +=
                resample_sinc_window (offset, r->filter_length * 0.5,
                r->sinc_scale) * x;
          }

          *(double *) (r->o_buf + i * sizeof (double)) = acc;
        }
        break;
    }

    r->i_start -= 1.0;
    r->o_buf += r->sample_size;
    r->o_size -= r->sample_size;
  }
}