src/audio/SDL_audiocvt.c
changeset 2716 f8f68f47285a
parent 2130 3ee59c43d784
child 2728 2768bd7281e0
     1.1 --- a/src/audio/SDL_audiocvt.c	Mon Aug 25 10:14:21 2008 +0000
     1.2 +++ b/src/audio/SDL_audiocvt.c	Mon Aug 25 15:08:59 2008 +0000
     1.3 @@ -20,12 +20,45 @@
     1.4      slouken@libsdl.org
     1.5  */
     1.6  #include "SDL_config.h"
     1.7 +#include <math.h>
     1.8  
     1.9  /* Functions for audio drivers to perform runtime conversion of audio format */
    1.10  
    1.11  #include "SDL_audio.h"
    1.12  #include "SDL_audio_c.h"
    1.13  
    1.14 +#define DEBUG_CONVERT
    1.15 +
    1.16 +/* These are fractional multiplication routines. That is, their inputs
    1.17 +   are two numbers in the range [-1, 1) and the result falls in that
    1.18 +   same range. The output is the same size as the inputs, i.e.
    1.19 +   32-bit x 32-bit = 32-bit.
    1.20 + */
    1.21 +
    1.22 +/* We hope here that the right shift includes sign extension */
    1.23 +#ifdef SDL_HAS_64BIT_Type
    1.24 +#define SDL_FixMpy32(a, b) ((((Sint64)a * (Sint64)b) >> 31) & 0xffffffff)
    1.25 +#else
    1.26 +/* If we don't have the 64-bit type, do something more complicated. See http://www.8052.com/mul16.phtml or http://www.cs.uaf.edu/~cs301/notes/Chapter5/node5.html */
    1.27 +#define SDL_FixMpy32(a, b) ((((Sint64)a * (Sint64)b) >> 31) & 0xffffffff)
    1.28 +#endif
    1.29 +#define SDL_FixMpy16(a, b) ((((Sint32)a * (Sint32)b) >> 14) & 0xffff)
    1.30 +#define SDL_FixMpy8(a, b) ((((Sint16)a * (Sint16)b) >> 7) & 0xff)
    1.31 +/* This macro just makes the floating point filtering code not have to be a special case. */
    1.32 +#define SDL_FloatMpy(a, b) (a * b)
    1.33 +
    1.34 +/* These macros take floating point numbers in the range [-1.0f, 1.0f) and
    1.35 +   represent them as fixed-point numbers in that same range. There's no
    1.36 +   checking that the floating point argument is inside the appropriate range.
    1.37 + */
    1.38 +
    1.39 +#define SDL_Make_1_7(a) (Sint8)(a * 128.0f)
    1.40 +#define SDL_Make_1_15(a) (Sint16)(a * 32768.0f)
    1.41 +#define SDL_Make_1_31(a) (Sint32)(a * 2147483648.0f)
    1.42 +#define SDL_Make_2_6(a) (Sint8)(a * 64.0f)
    1.43 +#define SDL_Make_2_14(a) (Sint16)(a * 16384.0f)
    1.44 +#define SDL_Make_2_30(a) (Sint32)(a * 1073741824.0f)
    1.45 +
    1.46  /* Effectively mix right and left channels into a single channel */
    1.47  static void SDLCALL
    1.48  SDL_ConvertMono(SDL_AudioCVT * cvt, SDL_AudioFormat format)
    1.49 @@ -1309,6 +1342,468 @@
    1.50      return 0;                   /* no conversion necessary. */
    1.51  }
    1.52  
    1.53 +/* Generate the necessary IIR lowpass coefficients for resampling.
    1.54 +   Assume that the SDL_AudioCVT struct is already set up with
    1.55 +   the correct values for len_mult and len_div, and use the
    1.56 +   type of dst_format. Also assume the buffer is allocated.
    1.57 +   Note the buffer needs to be 6 units long.
    1.58 +   For now, use RBJ's cookbook coefficients. It might be more
    1.59 +   optimal to create a Butterworth filter, but this is more difficult.
    1.60 +*/
    1.61 +int
    1.62 +SDL_BuildIIRLowpass(SDL_AudioCVT * cvt, SDL_AudioFormat format)
    1.63 +{
    1.64 +    float fc;                   /* cutoff frequency */
    1.65 +    float coeff[6];             /* floating point iir coefficients b0, b1, b2, a0, a1, a2 */
    1.66 +    float scale;
    1.67 +    float w0, alpha, cosw0;
    1.68 +    int i;
    1.69 +
    1.70 +    /* The higher Q is, the higher CUTOFF can be. Need to find a good balance to avoid aliasing */
    1.71 +    static const float Q = 5.0f;
    1.72 +    static const float CUTOFF = 0.4f;
    1.73 +
    1.74 +    fc = (cvt->len_mult >
    1.75 +          cvt->len_div) ? CUTOFF / (float) cvt->len_mult : CUTOFF /
    1.76 +        (float) cvt->len_div;
    1.77 +
    1.78 +    w0 = 2.0f * M_PI * fc;
    1.79 +    cosw0 = cosf(w0);
    1.80 +    alpha = sin(w0) / (2.0f * Q);
    1.81 +
    1.82 +    /* Compute coefficients, normalizing by a0 */
    1.83 +    scale = 1.0f / (1.0f + alpha);
    1.84 +
    1.85 +    coeff[0] = (1.0f - cosw0) / 2.0f * scale;
    1.86 +    coeff[1] = (1.0f - cosw0) * scale;
    1.87 +    coeff[2] = coeff[0];
    1.88 +
    1.89 +    coeff[3] = 1.0f;            /* a0 is normalized to 1 */
    1.90 +    coeff[4] = -2.0f * cosw0 * scale;
    1.91 +    coeff[5] = (1.0f - alpha) * scale;
    1.92 +
    1.93 +    /* Copy the coefficients to the struct. If necessary, convert coefficients to fixed point, using the range (-2.0, 2.0) */
    1.94 +#define convert_fixed(type, fix) { \
    1.95 +            type *cvt_coeff = (type *)cvt->coeff; \
    1.96 +            for(i = 0; i < 6; ++i) { \
    1.97 +                cvt_coeff[i] = fix(coeff[i]); \
    1.98 +            } \
    1.99 +        }
   1.100 +
   1.101 +    if (SDL_AUDIO_ISFLOAT(format) && SDL_AUDIO_BITSIZE(format) == 32) {
   1.102 +        float *cvt_coeff = (float *) cvt->coeff;
   1.103 +        for (i = 0; i < 6; ++i) {
   1.104 +            cvt_coeff[i] = coeff[i];
   1.105 +        }
   1.106 +    } else {
   1.107 +        switch (SDL_AUDIO_BITSIZE(format)) {
   1.108 +        case 8:
   1.109 +            convert_fixed(Uint8, SDL_Make_2_6);
   1.110 +            break;
   1.111 +        case 16:
   1.112 +            convert_fixed(Uint16, SDL_Make_2_14);
   1.113 +            break;
   1.114 +        case 32:
   1.115 +            convert_fixed(Uint32, SDL_Make_2_30);
   1.116 +            break;
   1.117 +        }
   1.118 +    }
   1.119 +
   1.120 +#ifdef DEBUG_CONVERT
   1.121 +#define debug_iir(type) { \
   1.122 +            type *cvt_coeff = (type *)cvt->coeff; \
   1.123 +            for(i = 0; i < 6; ++i) { \
   1.124 +                printf("coeff[%u] = %f = 0x%x\n", i, coeff[i], cvt_coeff[i]); \
   1.125 +            } \
   1.126 +        }
   1.127 +    if (SDL_AUDIO_ISFLOAT(format) && SDL_AUDIO_BITSIZE(format) == 32) {
   1.128 +        float *cvt_coeff = (float *) cvt->coeff;
   1.129 +        for (i = 0; i < 6; ++i) {
   1.130 +            printf("coeff[%u] = %f = %f\n", i, coeff[i], cvt_coeff[i]);
   1.131 +        }
   1.132 +    } else {
   1.133 +        switch (SDL_AUDIO_BITSIZE(format)) {
   1.134 +        case 8:
   1.135 +            debug_iir(Uint8);
   1.136 +            break;
   1.137 +        case 16:
   1.138 +            debug_iir(Uint16);
   1.139 +            break;
   1.140 +        case 32:
   1.141 +            debug_iir(Uint32);
   1.142 +            break;
   1.143 +        }
   1.144 +    }
   1.145 +#undef debug_iir
   1.146 +#endif
   1.147 +
   1.148 +    /* Initialize the state buffer to all zeroes, and set initial position */
   1.149 +    memset(cvt->state_buf, 0, 4 * SDL_AUDIO_BITSIZE(format) / 4);
   1.150 +    cvt->state_pos = 0;
   1.151 +#undef convert_fixed
   1.152 +}
   1.153 +
   1.154 +/* Apply the lowpass IIR filter to the given SDL_AudioCVT struct */
   1.155 +/* This was implemented because it would be much faster than the fir filter, 
   1.156 +   but it doesn't seem to have a steep enough cutoff so we'd need several
   1.157 +   cascaded biquads, which probably isn't a great idea. Therefore, this
   1.158 +   function can probably be discarded.
   1.159 +*/
   1.160 +static void
   1.161 +SDL_FilterIIR(SDL_AudioCVT * cvt, SDL_AudioFormat format)
   1.162 +{
   1.163 +    Uint32 i, n;
   1.164 +
   1.165 +    /* TODO: Check that n is calculated right */
   1.166 +    n = 8 * cvt->len_cvt / SDL_AUDIO_BITSIZE(format);
   1.167 +
   1.168 +    /* Note that the coefficients are 2_x and the input is 1_x. Do we need to shift left at the end here? The right shift temp = buf[n] >> 1 needs to depend on whether the type is signed or not for sign extension. */
   1.169 +    /* cvt->state_pos = 1: state[0] = x_n-1, state[1] = x_n-2, state[2] = y_n-1, state[3] - y_n-2 */
   1.170 +#define iir_fix(type, mult) {\
   1.171 +            type *coeff = (type *)cvt->coeff; \
   1.172 +            type *state = (type *)cvt->state_buf; \
   1.173 +            type *buf = (type *)cvt->buf; \
   1.174 +            type temp; \
   1.175 +            for(i = 0; i < n; ++i) { \
   1.176 +                    temp = buf[i] >> 1; \
   1.177 +                    if(cvt->state_pos) { \
   1.178 +                        buf[i] = mult(coeff[0], temp) + mult(coeff[1], state[0]) + mult(coeff[2], state[1]) - mult(coeff[4], state[2]) - mult(coeff[5], state[3]); \
   1.179 +                        state[1] = temp; \
   1.180 +                        state[3] = buf[i]; \
   1.181 +                        cvt->state_pos = 0; \
   1.182 +                    } else { \
   1.183 +                        buf[i] = mult(coeff[0], temp) + mult(coeff[1], state[1]) + mult(coeff[2], state[0]) - mult(coeff[4], state[3]) - mult(coeff[5], state[2]); \
   1.184 +                        state[0] = temp; \
   1.185 +                        state[2] = buf[i]; \
   1.186 +                        cvt->state_pos = 1; \
   1.187 +                    } \
   1.188 +                } \
   1.189 +        }
   1.190 +/* Need to test to see if the previous method or this one is faster */
   1.191 +/*#define iir_fix(type, mult) {\
   1.192 +            type *coeff = (type *)cvt->coeff; \
   1.193 +            type *state = (type *)cvt->state_buf; \
   1.194 +            type *buf = (type *)cvt->buf; \
   1.195 +            type temp; \
   1.196 +            for(i = 0; i < n; ++i) { \
   1.197 +                    temp = buf[i] >> 1; \
   1.198 +                    buf[i] = mult(coeff[0], temp) + mult(coeff[1], state[0]) + mult(coeff[2], state[1]) - mult(coeff[4], state[2]) - mult(coeff[5], state[3]); \
   1.199 +                    state[1] = state[0]; \
   1.200 +                    state[0] = temp; \
   1.201 +                    state[3] = state[2]; \
   1.202 +                    state[2] = buf[i]; \
   1.203 +                } \
   1.204 +        }*/
   1.205 +
   1.206 +    if (SDL_AUDIO_ISFLOAT(format) && SDL_AUDIO_BITSIZE(format) == 32) {
   1.207 +        float *coeff = (float *) cvt->coeff;
   1.208 +        float *state = (float *) cvt->state_buf;
   1.209 +        float *buf = (float *) cvt->buf;
   1.210 +        float temp;
   1.211 +
   1.212 +        for (i = 0; i < n; ++i) {
   1.213 +            /* y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] - a1 * y[n-1] - a[2] * y[n-2] */
   1.214 +            temp = buf[i];
   1.215 +            if (cvt->state_pos) {
   1.216 +                buf[i] =
   1.217 +                    coeff[0] * buf[n] + coeff[1] * state[0] +
   1.218 +                    coeff[2] * state[1] - coeff[4] * state[2] -
   1.219 +                    coeff[5] * state[3];
   1.220 +                state[1] = temp;
   1.221 +                state[3] = buf[i];
   1.222 +                cvt->state_pos = 0;
   1.223 +            } else {
   1.224 +                buf[i] =
   1.225 +                    coeff[0] * buf[n] + coeff[1] * state[1] +
   1.226 +                    coeff[2] * state[0] - coeff[4] * state[3] -
   1.227 +                    coeff[5] * state[2];
   1.228 +                state[0] = temp;
   1.229 +                state[2] = buf[i];
   1.230 +                cvt->state_pos = 1;
   1.231 +            }
   1.232 +        }
   1.233 +    } else {
   1.234 +        /* Treat everything as signed! */
   1.235 +        switch (SDL_AUDIO_BITSIZE(format)) {
   1.236 +        case 8:
   1.237 +            iir_fix(Sint8, SDL_FixMpy8);
   1.238 +            break;
   1.239 +        case 16:
   1.240 +            iir_fix(Sint16, SDL_FixMpy16);
   1.241 +            break;
   1.242 +        case 32:
   1.243 +            iir_fix(Sint32, SDL_FixMpy32);
   1.244 +            break;
   1.245 +        }
   1.246 +    }
   1.247 +#undef iir_fix
   1.248 +}
   1.249 +
   1.250 +/* Apply the windowed sinc FIR filter to the given SDL_AudioCVT struct.
   1.251 +*/
   1.252 +static void
   1.253 +SDL_FilterFIR(SDL_AudioCVT * cvt, SDL_AudioFormat format)
   1.254 +{
   1.255 +    int n = 8 * cvt->len_cvt / SDL_AUDIO_BITSIZE(format);
   1.256 +    int m = cvt->len_sinc;
   1.257 +    int i, j;
   1.258 +
   1.259 +    /* 
   1.260 +       Note: We can make a big optimization here by taking advantage
   1.261 +       of the fact that the signal is zero stuffed, so we can do
   1.262 +       significantly fewer multiplications and additions. However, this
   1.263 +       depends on the zero stuffing ratio, so it may not pay off. This would
   1.264 +       basically be a polyphase filter.
   1.265 +     */
   1.266 +    /* One other way to do this fast is to look at the fir filter from a different angle:
   1.267 +       After we zero stuff, we have input of all zeroes, except for every len_mult
   1.268 +       sample. If we choose a sinc length equal to len_mult, then the fir filter becomes
   1.269 +       much more simple: we're just taking a windowed sinc, shifting it to start at each
   1.270 +       len_mult sample, and scaling it by the value of that sample. If we do this, then
   1.271 +       we don't even need to worry about the sample histories, and the inner loop here is
   1.272 +       unnecessary. This probably sacrifices some quality but could really speed things up as well.
   1.273 +     */
   1.274 +    /* We only calculate the values of samples which are 0 (mod len_div) because
   1.275 +       those are the only ones used. All the other ones are discarded in the
   1.276 +       third step of resampling. This is a huge speedup. As a warning, though,
   1.277 +       if for some reason this is used elsewhere where there are no samples discarded,
   1.278 +       the output will not be corrrect if len_div is not 1. To make this filter a
   1.279 +       generic FIR filter, simply remove the if statement "if(i % cvt->len_div == 0)"
   1.280 +       around the inner loop so that every sample is processed.
   1.281 +     */
   1.282 +    /* This is basically just a FIR filter. i.e. for input x_n and m coefficients,
   1.283 +       y_n = x_n*sinc_0 + x_(n-1)*sinc_1 +  x_(n-2)*sinc_2 + ... + x_(n-m+1)*sinc_(m-1)
   1.284 +     */
   1.285 +#define filter_sinc(type, mult) { \
   1.286 +            type *sinc = (type *)cvt->coeff; \
   1.287 +            type *state = (type *)cvt->state_buf; \
   1.288 +            type *buf = (type *)cvt->buf; \
   1.289 +            for(i = 0; i < n; ++i) { \
   1.290 +                state[cvt->state_pos] = buf[i]; \
   1.291 +                buf[i] = 0; \
   1.292 +                if( i % cvt->len_div == 0 ) { \
   1.293 +                    for(j = 0; j < m;  ++j) { \
   1.294 +                        buf[i] += mult(sinc[j], state[(cvt->state_pos + j) % m]); \
   1.295 +                    } \
   1.296 +                }\
   1.297 +                cvt->state_pos = (cvt->state_pos + 1) % m; \
   1.298 +            } \
   1.299 +        }
   1.300 +
   1.301 +    if (SDL_AUDIO_ISFLOAT(format) && SDL_AUDIO_BITSIZE(format) == 32) {
   1.302 +        filter_sinc(float, SDL_FloatMpy);
   1.303 +    } else {
   1.304 +        switch (SDL_AUDIO_BITSIZE(format)) {
   1.305 +        case 8:
   1.306 +            filter_sinc(Sint8, SDL_FixMpy8);
   1.307 +            break;
   1.308 +        case 16:
   1.309 +            filter_sinc(Sint16, SDL_FixMpy16);
   1.310 +            break;
   1.311 +        case 32:
   1.312 +            filter_sinc(Sint32, SDL_FixMpy32);
   1.313 +            break;
   1.314 +        }
   1.315 +    }
   1.316 +
   1.317 +#undef filter_sinc
   1.318 +
   1.319 +}
   1.320 +
   1.321 +/* Generate the necessary windowed sinc filter for resampling.
   1.322 +   Assume that the SDL_AudioCVT struct is already set up with
   1.323 +   the correct values for len_mult and len_div, and use the
   1.324 +   type of dst_format. Also assume the buffer is allocated.
   1.325 +   Note the buffer needs to be m+1 units long.
   1.326 +*/
   1.327 +int
   1.328 +SDL_BuildWindowedSinc(SDL_AudioCVT * cvt, SDL_AudioFormat format,
   1.329 +                      unsigned int m)
   1.330 +{
   1.331 +    float fScale;               /* scale factor for fixed point */
   1.332 +    float *fSinc;               /* floating point sinc buffer, to be converted to fixed point */
   1.333 +    float fc;                   /* cutoff frequency */
   1.334 +    float two_pi_fc, two_pi_over_m, four_pi_over_m, m_over_two;
   1.335 +    float norm_sum, norm_fact;
   1.336 +    unsigned int i;
   1.337 +
   1.338 +    /* Check that the buffer is allocated */
   1.339 +    if (cvt->coeff == NULL) {
   1.340 +        return -1;
   1.341 +    }
   1.342 +
   1.343 +    /* Set the length */
   1.344 +    cvt->len_sinc = m + 1;
   1.345 +
   1.346 +    /* Allocate the floating point windowed sinc. */
   1.347 +    fSinc = (float *) malloc((m + 1) * sizeof(float));
   1.348 +    if (fSinc == NULL) {
   1.349 +        return -1;
   1.350 +    }
   1.351 +
   1.352 +    /* Set up the filter parameters */
   1.353 +    fc = (cvt->len_mult >
   1.354 +          cvt->len_div) ? 0.5f / (float) cvt->len_mult : 0.5f /
   1.355 +        (float) cvt->len_div;
   1.356 +#ifdef DEBUG_CONVERT
   1.357 +    printf("Lowpass cutoff frequency = %f\n", fc);
   1.358 +#endif
   1.359 +    two_pi_fc = 2.0f * M_PI * fc;
   1.360 +    two_pi_over_m = 2.0f * M_PI / (float) m;
   1.361 +    four_pi_over_m = 2.0f * two_pi_over_m;
   1.362 +    m_over_two = (float) m / 2.0f;
   1.363 +    norm_sum = 0.0f;
   1.364 +
   1.365 +    for (i = 0; i <= m; ++i) {
   1.366 +        if (i == m / 2) {
   1.367 +            fSinc[i] = two_pi_fc;
   1.368 +        } else {
   1.369 +            fSinc[i] =
   1.370 +                sinf(two_pi_fc * ((float) i - m_over_two)) / ((float) i -
   1.371 +                                                              m_over_two);
   1.372 +            /* Apply blackman window */
   1.373 +            fSinc[i] *=
   1.374 +                0.42f - 0.5f * cosf(two_pi_over_m * (float) i) +
   1.375 +                0.08f * cosf(four_pi_over_m * (float) i);
   1.376 +        }
   1.377 +        norm_sum += fabs(fSinc[i]);
   1.378 +    }
   1.379 +
   1.380 +    norm_fact = 1.0f / norm_sum;
   1.381 +
   1.382 +#define convert_fixed(type, fix) { \
   1.383 +        type *dst = (type *)cvt->coeff; \
   1.384 +        for( i = 0; i <= m; ++i ) { \
   1.385 +            dst[i] = fix(fSinc[i] * norm_fact); \
   1.386 +        } \
   1.387 +    }
   1.388 +
   1.389 +    /* If we're using floating point, we only need to normalize */
   1.390 +    if (SDL_AUDIO_ISFLOAT(format) && SDL_AUDIO_BITSIZE(format) == 32) {
   1.391 +        float *fDest = (float *) cvt->coeff;
   1.392 +        for (i = 0; i <= m; ++i) {
   1.393 +            fDest[i] = fSinc[i] * norm_fact;
   1.394 +        }
   1.395 +    } else {
   1.396 +        switch (SDL_AUDIO_BITSIZE(format)) {
   1.397 +        case 8:
   1.398 +            convert_fixed(Uint8, SDL_Make_1_7);
   1.399 +            break;
   1.400 +        case 16:
   1.401 +            convert_fixed(Uint16, SDL_Make_1_15);
   1.402 +            break;
   1.403 +        case 32:
   1.404 +            convert_fixed(Uint32, SDL_Make_1_31);
   1.405 +            break;
   1.406 +        }
   1.407 +    }
   1.408 +
   1.409 +    /* Initialize the state buffer to all zeroes, and set initial position */
   1.410 +    memset(cvt->state_buf, 0, cvt->len_sinc * SDL_AUDIO_BITSIZE(format) / 4);
   1.411 +    cvt->state_pos = 0;
   1.412 +
   1.413 +    /* Clean up */
   1.414 +#undef convert_fixed
   1.415 +    free(fSinc);
   1.416 +}
   1.417 +
   1.418 +/* This is used to reduce the resampling ratio */
   1.419 +inline int
   1.420 +SDL_GCD(int a, int b)
   1.421 +{
   1.422 +    int temp;
   1.423 +    while (b != 0) {
   1.424 +        temp = a % b;
   1.425 +        a = b;
   1.426 +        b = temp;
   1.427 +    }
   1.428 +    return a;
   1.429 +}
   1.430 +
   1.431 +/* Perform proper resampling. This is pretty slow but it's the best-sounding method. */
   1.432 +static void SDLCALL
   1.433 +SDL_Resample(SDL_AudioCVT * cvt, SDL_AudioFormat format)
   1.434 +{
   1.435 +    int i, j;
   1.436 +
   1.437 +#ifdef DEBUG_CONVERT
   1.438 +    printf("Converting audio rate via proper resampling (mono)\n");
   1.439 +#endif
   1.440 +
   1.441 +#define zerostuff_mono(type) { \
   1.442 +        const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
   1.443 +        type *dst = (type *) (cvt->buf + (cvt->len_cvt * cvt->len_mult)); \
   1.444 +        for (i = cvt->len_cvt / sizeof (type); i; --i) { \
   1.445 +            src--; \
   1.446 +            dst[-1] = src[0]; \
   1.447 +            for( j = -cvt->len_mult; j < -1; ++j ) { \
   1.448 +                dst[j] = 0; \
   1.449 +            } \
   1.450 +            dst -= cvt->len_mult; \
   1.451 +        } \
   1.452 +    }
   1.453 +
   1.454 +#define discard_mono(type) { \
   1.455 +        const type *src = (const type *) (cvt->buf); \
   1.456 +        type *dst = (type *) (cvt->buf); \
   1.457 +        for (i = 0; i < (cvt->len_cvt / sizeof(type)) / cvt->len_div; ++i) { \
   1.458 +            dst[0] = src[0]; \
   1.459 +            src += cvt->len_div; \
   1.460 +            ++dst; \
   1.461 +        } \
   1.462 +    }
   1.463 +
   1.464 +    /* Step 1: Zero stuff the conversion buffer. This upsamples by a factor of len_mult,
   1.465 +       creating aliasing at frequencies above the original nyquist frequency.
   1.466 +     */
   1.467 +#ifdef DEBUG_CONVERT
   1.468 +    printf("Zero-stuffing by a factor of %u\n", cvt->len_mult);
   1.469 +#endif
   1.470 +    switch (SDL_AUDIO_BITSIZE(format)) {
   1.471 +    case 8:
   1.472 +        zerostuff_mono(Uint8);
   1.473 +        break;
   1.474 +    case 16:
   1.475 +        zerostuff_mono(Uint16);
   1.476 +        break;
   1.477 +    case 32:
   1.478 +        zerostuff_mono(Uint32);
   1.479 +        break;
   1.480 +    }
   1.481 +
   1.482 +    cvt->len_cvt *= cvt->len_mult;
   1.483 +
   1.484 +    /* Step 2: Use a windowed sinc FIR filter (lowpass filter) to remove the alias
   1.485 +       frequencies. This is the slow part.
   1.486 +     */
   1.487 +    SDL_FilterFIR(cvt, format);
   1.488 +
   1.489 +    /* Step 3: Now downsample by discarding samples. */
   1.490 +
   1.491 +#ifdef DEBUG_CONVERT
   1.492 +    printf("Discarding samples by a factor of %u\n", cvt->len_div);
   1.493 +#endif
   1.494 +    switch (SDL_AUDIO_BITSIZE(format)) {
   1.495 +    case 8:
   1.496 +        discard_mono(Uint8);
   1.497 +        break;
   1.498 +    case 16:
   1.499 +        discard_mono(Uint16);
   1.500 +        break;
   1.501 +    case 32:
   1.502 +        discard_mono(Uint32);
   1.503 +        break;
   1.504 +    }
   1.505 +
   1.506 +#undef zerostuff_mono
   1.507 +#undef discard_mono
   1.508 +
   1.509 +    cvt->len_cvt /= cvt->len_div;
   1.510 +
   1.511 +    if (cvt->filters[++cvt->filter_index]) {
   1.512 +        cvt->filters[cvt->filter_index] (cvt, format);
   1.513 +    }
   1.514 +}
   1.515  
   1.516  
   1.517  /* Creates a set of audio filters to convert from one format to another.
   1.518 @@ -1399,6 +1894,17 @@
   1.519      }
   1.520  
   1.521      /* Do rate conversion */
   1.522 +    if (src_rate != dst_rate) {
   1.523 +        int rate_gcd;
   1.524 +        rate_gcd = SDL_GCD(src_rate, dst_rate);
   1.525 +        cvt->len_mult = dst_rate / rate_gcd;
   1.526 +        cvt->len_div = src_rate / rate_gcd;
   1.527 +        cvt->len_ratio = (double) cvt->len_mult / (double) cvt->len_div;
   1.528 +        cvt->filters[cvt->filter_index++] = SDL_Resample;
   1.529 +        SDL_BuildWindowedSinc(cvt, dst_fmt, 768);
   1.530 +    }
   1.531 +
   1.532 +/*
   1.533      cvt->rate_incr = 0.0;
   1.534      if ((src_rate / 100) != (dst_rate / 100)) {
   1.535          Uint32 hi_rate, lo_rate;
   1.536 @@ -1448,25 +1954,25 @@
   1.537              }
   1.538              len_mult = 2;
   1.539              len_ratio = 2.0;
   1.540 -        }
   1.541 -        /* If hi_rate = lo_rate*2^x then conversion is easy */
   1.542 -        while (((lo_rate * 2) / 100) <= (hi_rate / 100)) {
   1.543 -            cvt->filters[cvt->filter_index++] = rate_cvt;
   1.544 -            cvt->len_mult *= len_mult;
   1.545 -            lo_rate *= 2;
   1.546 -            cvt->len_ratio *= len_ratio;
   1.547 -        }
   1.548 -        /* We may need a slow conversion here to finish up */
   1.549 -        if ((lo_rate / 100) != (hi_rate / 100)) {
   1.550 -#if 1
   1.551 -            /* The problem with this is that if the input buffer is
   1.552 -               say 1K, and the conversion rate is say 1.1, then the
   1.553 -               output buffer is 1.1K, which may not be an acceptable
   1.554 -               buffer size for the audio driver (not a power of 2)
   1.555 -             */
   1.556 -            /* For now, punt and hope the rate distortion isn't great.
   1.557 -             */
   1.558 -#else
   1.559 +        }*/
   1.560 +    /* If hi_rate = lo_rate*2^x then conversion is easy */
   1.561 +    /*   while (((lo_rate * 2) / 100) <= (hi_rate / 100)) {
   1.562 +       cvt->filters[cvt->filter_index++] = rate_cvt;
   1.563 +       cvt->len_mult *= len_mult;
   1.564 +       lo_rate *= 2;
   1.565 +       cvt->len_ratio *= len_ratio;
   1.566 +       } */
   1.567 +    /* We may need a slow conversion here to finish up */
   1.568 +    /*    if ((lo_rate / 100) != (hi_rate / 100)) {
   1.569 +       #if 1 */
   1.570 +    /* The problem with this is that if the input buffer is
   1.571 +       say 1K, and the conversion rate is say 1.1, then the
   1.572 +       output buffer is 1.1K, which may not be an acceptable
   1.573 +       buffer size for the audio driver (not a power of 2)
   1.574 +     */
   1.575 +    /* For now, punt and hope the rate distortion isn't great.
   1.576 +     */
   1.577 +/*#else
   1.578              if (src_rate < dst_rate) {
   1.579                  cvt->rate_incr = (double) lo_rate / hi_rate;
   1.580                  cvt->len_mult *= 2;
   1.581 @@ -1478,7 +1984,7 @@
   1.582              cvt->filters[cvt->filter_index++] = SDL_RateSLOW;
   1.583  #endif
   1.584          }
   1.585 -    }
   1.586 +    }*/
   1.587  
   1.588      /* Set up the filter information */
   1.589      if (cvt->filter_index != 0) {
   1.590 @@ -1492,4 +1998,15 @@
   1.591      return (cvt->needed);
   1.592  }
   1.593  
   1.594 +#undef SDL_FixMpy8
   1.595 +#undef SDL_FixMpy16
   1.596 +#undef SDL_FixMpy32
   1.597 +#undef SDL_FloatMpy
   1.598 +#undef SDL_Make_1_7
   1.599 +#undef SDL_Make_1_15
   1.600 +#undef SDL_Make_1_31
   1.601 +#undef SDL_Make_2_6
   1.602 +#undef SDL_Make_2_14
   1.603 +#undef SDL_Make_2_30
   1.604 +
   1.605  /* vi: set ts=4 sw=4 expandtab: */