[openbeos] Re: Resampling algorithms for audio mixer

> What about "interpolating remaining one sample behind the input
> buffer" (so that you cannot have to interpolate values beyond  the
> innput buffer) and passing the last 3 or 4 samples of the previous
> input buffer through a struct, as you suggested, so that the values
> you now have to inteprolate before the input buffer are well defined?

Exactly; that's pretty much what I was proposing. Preserving
state should clear up any potential concerns about sound quality
with very small buffer sizes (as an extreme example, one-sample
buffers!).

It's been a while since I wrote any resampling code, but the
last one I wrote worked like this: (this is from memory, not
actual cut+paste code, so it probably won't work, and certainly
isn't optimized!)

typedef struct RESAMPLER {
    int32 cur, prev;
    uint32 pos; /* Current fractional position */
} RESAMPLER;

/*
 * Produce an output buffer at the desired sample rate,
 * reading the input stream as required.
 * 'out_rate' is the output sample rate relative to the input sample
 * rate in 16:16 fixed point format. e.g., with a 44100Hz output
 * rate and an 88200Hz input rate, 'out_rate' must be 32768.
 */
void resample(
    RESAMPLER *state,
    int16 *out_buf,
    size_t out_len,
    uint32 out_rate
    ) {
    for( ; out_len-- ; ) {
        *out_buf++ = (int16) (( state->cur * state->pos + state->prev *
 (~state->pos) ) >> 16);
        state->pos += out_rate;
        while( state->pos & 0xffff0000 ) {
            /* The position has overflowed into the next sample --
               read a new sample */
            state->pos -= 0x10000;
            state->prev = state->cur;
            state->cur = READ_SAMPLE(); /* This returns an int16 */
        }
    }
}

The architecture is pretty different given that it uses an 'out_rate' 
value
rather than using the relative sizes of the two buffers, and reads the
input stream internally rather than having a separate input buffer.
Though the idea of state preservation doesn't depend on that, of
course.

- Cyan

Other related posts: