12. Interpolator

Interpolates between two values given an input signal.

<<gestvm>>=
SKFLT cur, nxt;
<<init>>=
gvm->cur = 0;
gvm->nxt = 0;

The method of interpolation is known as behavior.

<<typedefs>>=
typedef SKFLT (*gestvm_behavior)(gestvm *, SKFLT);
<<gestvm>>=
gestvm_behavior behavior;
<<init>>=
gvm->behavior = b_linear;

Some behaviors include linear, step, and glissando.

<<static_funcdefs>>=
static SKFLT b_linear(gestvm *gvm, SKFLT a);
static SKFLT b_step(gestvm *gvm, SKFLT a);
static SKFLT b_gliss_medium(gestvm *gvm, SKFLT a);
<<funcs>>=
static SKFLT b_linear(gestvm *gvm, SKFLT a)
{
    return a;
}

static SKFLT b_step(gestvm *gvm, SKFLT a)
{
    return 0;
}

static SKFLT b_gliss_medium(gestvm *gvm, SKFLT a)
{
    if (a < 0.75) {
        a = 0;
    } else {
        a -= 0.75;
        if (a < 0) a = 0;
        a /= 0.25;
        a = a * a * a;
    }
    return a;
}

Some behaviors, such as exponential with variable slope, will require arguments. More thought required here. Maybe aux values that get tied to cur/nxt?

<<static_funcdefs>>=
static SKFLT interpolate(gestvm *gvm, SKFLT phs);
<<funcs>>=
static SKFLT interpolate(gestvm *gvm, SKFLT phs)
{
    SKFLT a;
    a = gvm->behavior(gvm, phs);

    return (1.0 - a)*gvm->cur + a*gvm->nxt;
}
<<static_funcdefs>>=
static gestvm_behavior find_behavior(int id);
<<funcs>>=
static gestvm_behavior find_behavior(int id)
{
    gestvm_behavior b;

    b = b_linear;

    switch (id) {
        case 0:
            b = b_linear;
            break;
        case 1:
            b = b_step;
            break;
        case 2:
            b = b_gliss_medium;
            break;
        default:
            break;
    }

    return b;
}



prev | home | next