resample.c 9.8 KB
Newer Older
Michael Smith's avatar
Michael Smith committed
1 2
/* resample.c: see resample.h for interesting stuff */

3 4 5 6
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Michael Smith's avatar
Michael Smith committed
7
#include <math.h>
8
#include <stdlib.h>
Michael Smith's avatar
Michael Smith committed
9 10 11 12 13 14
#include <string.h>
#include <stdarg.h>
#include <assert.h>

#include "resample.h"

15 16 17 18
/* Some systems don't define this */
#ifndef M_PI
#define M_PI       3.14159265358979323846 
#endif
Michael Smith's avatar
Michael Smith committed
19 20 21

static int hcf(int arg1, int arg2)
{
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    int mult = 1;

    while (~(arg1 | arg2) & 1)
        arg1 >>= 1, arg2 >>= 1, mult <<= 1;

    while (arg1 > 0)
    {
        if (~(arg1 & arg2) & 1)
        {
            arg1 >>= (~arg1 & 1);
            arg2 >>= (~arg2 & 1);
        }
        else if (arg1 < arg2)
            arg2 = (arg2 - arg1) >> 1;
        else
            arg1 = (arg1 - arg2) >> 1;
    }

    return arg2 * mult;
Michael Smith's avatar
Michael Smith committed
41 42 43 44 45
}


static void filt_sinc(float *dest, int N, int step, double fc, double gain, int width)
{
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    double s = fc / step;
    int mid, x;
    float *endpoint = dest + N,
        *base = dest,
        *origdest = dest;
    
    assert(width <= N);

    if ((N & 1) == 0)
    {
        *dest = 0.0;
        dest += width;
        if (dest >= endpoint)
            dest = ++base;
        N--;
    }

    mid = N / 2;
    x = -mid;

    while (N--)
    {
        *dest = (x ? sin(x * M_PI * s) / (x * M_PI) * step : fc) * gain;
        x++;
        dest += width;
        if (dest >= endpoint)
            dest = ++base;
    }
    assert(dest == origdest + width);
Michael Smith's avatar
Michael Smith committed
75 76 77 78 79
}


static double I_zero(double x)
{
80 81 82 83 84 85 86 87 88 89 90 91 92 93
    int n = 0;
    double u = 1.0,
        s = 1.0,
        t;

    do
    {
        n += 2;
        t = x / n;
        u *= t * t;
        s += u;
    } while (u > 1e-21 * s);

    return s;
Michael Smith's avatar
Michael Smith committed
94 95 96 97 98
}


static void win_kaiser(float *dest, int N, double alpha, int width)
{
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    double I_alpha, midsq;
    int x;
    float *endpoint = dest + N,
        *base = dest,
        *origdest = dest;

    assert(width <= N);

    if ((N & 1) == 0)
    {
        *dest = 0.0;
        dest += width;
        if (dest >= endpoint)
            dest = ++base;
        N--;
    }

    x = -(N / 2);
    midsq = (double)(x - 1) * (double)(x - 1);
    I_alpha = I_zero(alpha);

    while (N--)
    {
        *dest *= I_zero(alpha * sqrt(1.0 - ((double)x * (double)x) / midsq)) / I_alpha;
        x++;
        dest += width;
        if (dest >= endpoint)
            dest = ++base;
    }
    assert(dest == origdest + width);
Michael Smith's avatar
Michael Smith committed
129 130 131 132 133
}


int res_init(res_state *state, int channels, int outfreq, int infreq, res_parameter op1, ...)
{
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
    double beta = 16.0,
        cutoff = 0.80,
        gain = 1.0;
    int taps = 45;

    int factor;

    assert(state);
    assert(channels > 0);
    assert(outfreq > 0);
    assert(infreq > 0);
    assert(taps > 0);

    if (state == NULL || channels <= 0 || outfreq <= 0 || infreq <= 0 || taps <= 0)
        return -1;

    if (op1 != RES_END)
    {
        va_list argp;
        va_start(argp, op1);
        do
        {
            switch (op1)
            {
            case RES_GAIN:
                gain = va_arg(argp, double);
                break;

            case RES_CUTOFF:
                cutoff = va_arg(argp, double);
                assert(cutoff > 0.01 && cutoff <= 1.0);
                break;

            case RES_TAPS:
                taps = va_arg(argp, int);
                assert(taps > 2 && taps < 1000);
                break;
                
            case RES_BETA:
                beta = va_arg(argp, double);
                assert(beta > 2.0);
                break;
            default:
                assert("arglist" == "valid");
                return -1;
            }
            op1 = va_arg(argp, res_parameter);
        } while (op1 != RES_END);
        va_end(argp);
    }

    factor = hcf(infreq, outfreq);
    outfreq /= factor;
    infreq /= factor;

    /* adjust to rational values for downsampling */
    if (outfreq < infreq)
    {
        /* push the cutoff frequency down to the output frequency */
        cutoff = cutoff * outfreq / infreq; 
Michael Smith's avatar
Michael Smith committed
194

195 196 197
        /* compensate for the sharper roll-off requirement
         * by using a bigger hammer */
        taps = taps * infreq/outfreq;
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    }

    assert(taps >= (infreq + outfreq - 1) / outfreq);

    if ((state->table = calloc(outfreq * taps, sizeof(float))) == NULL)
        return -1;
    if ((state->pool = calloc(channels * taps, sizeof(SAMPLE))) == NULL)
    {
        free(state->table);
        state->table = NULL;
        return -1;
    }

    state->poolfill = taps / 2 + 1;
    state->channels = channels;
    state->outfreq = outfreq;
    state->infreq = infreq;
    state->taps = taps;
    state->offset = 0;

    filt_sinc(state->table, outfreq * taps, outfreq, cutoff, gain, taps);
    win_kaiser(state->table, outfreq * taps, beta, taps);

    return 0;
Michael Smith's avatar
Michael Smith committed
222 223 224 225 226
}


static SAMPLE sum(float const *scale, int count, SAMPLE const *source, SAMPLE const *trigger, SAMPLE const *reset, int srcstep)
{
227
    float total = 0.0;
Michael Smith's avatar
Michael Smith committed
228

229 230 231
    while (count--)
    {
        total += *source * *scale;
Michael Smith's avatar
Michael Smith committed
232

233 234 235 236 237
        if (source == trigger)
            source = reset, srcstep = 1;
        source -= srcstep;
        scale++;
    }
Michael Smith's avatar
Michael Smith committed
238

239
    return total;
Michael Smith's avatar
Michael Smith committed
240 241 242 243 244
}


static int push(res_state const * const state, SAMPLE *pool, int * const poolfill, int * const offset, SAMPLE *dest, int dststep, SAMPLE const *source, int srcstep, size_t srclen)
{
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    SAMPLE    * const destbase = dest,
        *poolhead = pool + *poolfill,
        *poolend = pool + state->taps,
        *newpool = pool;
    SAMPLE const *refill, *base, *endpoint;
    int    lencheck;


    assert(state);
    assert(pool);
    assert(poolfill);
    assert(dest);
    assert(source);

    assert(state->poolfill != -1);
    
    lencheck = res_push_check(state, srclen);

    /* fill the pool before diving in */
    while (poolhead < poolend && srclen > 0)
    {
        *poolhead++ = *source;
        source += srcstep;
        srclen--;
    }

    if (srclen <= 0)
        return 0;

    base = source;
    endpoint = source + srclen * srcstep;

    while (source < endpoint)
    {
        *dest = sum(state->table + *offset * state->taps, state->taps, source, base, poolend, srcstep);
        dest += dststep;
        *offset += state->infreq;
        while (*offset >= state->outfreq)
        {
            *offset -= state->outfreq;
            source += srcstep;
        }
    }

    assert(dest == destbase + lencheck * dststep);

    /* pretend that source has that underrun data we're not going to get */
    srclen += (source - endpoint) / srcstep;

    /* if we didn't get enough to completely replace the pool, then shift things about a bit */
    if (srclen < state->taps)
    {
        refill = pool + srclen;
        while (refill < poolend)
            *newpool++ = *refill++;

        refill = source - srclen * srcstep;
    }
    else
        refill = source - state->taps * srcstep;

    /* pull in fresh pool data */
    while (refill < endpoint)
    {
        *newpool++ = *refill;
        refill += srcstep;
    }

    assert(newpool > pool);
    assert(newpool <= poolend);

    *poolfill = newpool - pool;

    return (dest - destbase) / dststep;
Michael Smith's avatar
Michael Smith committed
319 320 321 322 323
}


int res_push_max_input(res_state const * const state, size_t maxoutput)
{
324
    return maxoutput * state->infreq / state->outfreq;
Michael Smith's avatar
Michael Smith committed
325 326 327 328 329
}


int res_push_check(res_state const * const state, size_t srclen)
{
330 331
    if (state->poolfill < state->taps)
        srclen -= state->taps - state->poolfill;
Michael Smith's avatar
Michael Smith committed
332

333
    return (srclen * state->outfreq - state->offset + state->infreq - 1) / state->infreq;
Michael Smith's avatar
Michael Smith committed
334 335 336 337 338
}


int res_push(res_state *state, SAMPLE **dstlist, SAMPLE const **srclist, size_t srclen)
{
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    int result = -1, poolfill = -1, offset = -1, i;

    assert(state);
    assert(dstlist);
    assert(srclist);
    assert(state->poolfill >= 0);

    for (i = 0; i < state->channels; i++)
    {
        poolfill = state->poolfill;
        offset = state->offset;
        result = push(state, state->pool + i * state->taps, &poolfill, &offset, dstlist[i], 1, srclist[i], 1, srclen);
    }
    state->poolfill = poolfill;
    state->offset = offset;

    return result;
Michael Smith's avatar
Michael Smith committed
356 357 358 359 360
}


int res_push_interleaved(res_state *state, SAMPLE *dest, SAMPLE const *source, size_t srclen)
{
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    int result = -1, poolfill = -1, offset = -1, i;
    
    assert(state);
    assert(dest);
    assert(source);
    assert(state->poolfill >= 0);

    for (i = 0; i < state->channels; i++)
    {
        poolfill = state->poolfill;
        offset = state->offset;
        result = push(state, state->pool + i * state->taps, &poolfill, &offset, dest + i, state->channels, source + i, state->channels, srclen);
    }
    state->poolfill = poolfill;
    state->offset = offset;

    return result;
Michael Smith's avatar
Michael Smith committed
378 379 380 381 382
}


int res_drain(res_state *state, SAMPLE **dstlist)
{
383 384
    SAMPLE *tail;
    int result = -1, poolfill = -1, offset = -1, i;
Michael Smith's avatar
Michael Smith committed
385

386 387 388
    assert(state);
    assert(dstlist);
    assert(state->poolfill >= 0);
Michael Smith's avatar
Michael Smith committed
389

390 391
    if ((tail = calloc(state->taps, sizeof(SAMPLE))) == NULL)
        return -1;
Michael Smith's avatar
Michael Smith committed
392

393 394 395 396 397 398 399 400
    for (i = 0; i < state->channels; i++)
    {
        poolfill = state->poolfill;
        offset = state->offset;
        result = push(state, state->pool + i * state->taps, &poolfill, &offset, dstlist[i], 1, tail, 1, state->taps / 2 - 1);
    }
        
    free(tail);
Michael Smith's avatar
Michael Smith committed
401

402
    state->poolfill = -1;
Michael Smith's avatar
Michael Smith committed
403

404
    return result;
Michael Smith's avatar
Michael Smith committed
405 406 407 408 409
}


int res_drain_interleaved(res_state *state, SAMPLE *dest)
{
410 411
    SAMPLE *tail;
    int result = -1, poolfill = -1, offset = -1, i;
Michael Smith's avatar
Michael Smith committed
412

413 414 415
    assert(state);
    assert(dest);
    assert(state->poolfill >= 0);
Michael Smith's avatar
Michael Smith committed
416

417 418
    if ((tail = calloc(state->taps, sizeof(SAMPLE))) == NULL)
        return -1;
Michael Smith's avatar
Michael Smith committed
419

420 421 422 423 424 425 426 427
    for (i = 0; i < state->channels; i++)
    {
        poolfill = state->poolfill;
        offset = state->offset;
        result = push(state, state->pool + i * state->taps, &poolfill, &offset, dest + i, state->channels, tail, 1, state->taps / 2 - 1);
    }
        
    free(tail);
Michael Smith's avatar
Michael Smith committed
428

429
    state->poolfill = -1;
Michael Smith's avatar
Michael Smith committed
430

431
    return result;
Michael Smith's avatar
Michael Smith committed
432 433 434 435 436
}


void res_clear(res_state *state)
{
437 438 439
    assert(state);
    assert(state->table);
    assert(state->pool);
Michael Smith's avatar
Michael Smith committed
440

441 442 443
    free(state->table);
    free(state->pool);
    memset(state, 0, sizeof(*state));
Michael Smith's avatar
Michael Smith committed
444 445
}