shithub: soundpipe

ref: 25a9fcc24ad00c6a406042818b9e6f2e7bba0dd1
dir: soundpipe/modules/smoother.c

View raw version
/*
 * Smoother
 *
 * Smoother is a one-pole smoothing filter, typically used on
 * control signals to create a "smootheramenteau" effect.
 *
 * This filter design uses the difference equation:
 *
 * y(n) = b0*x(n) - a1*y(n - 1)
 *
 * Where a1 is (0.5^(1/(t * sr))), and b0 is (1 - a1).
 *
 * More information on one-pole smoothers can be found here:
 * https://ccrma.stanford.edu/~jos/filters/One_Pole.html
 */


#include <math.h>
#include <stdint.h>
#include <stdlib.h>

#include "soundpipe.h"

int sp_smoother_create(sp_smoother **p)
{
    *p = malloc(sizeof(sp_smoother));
    return SP_OK;
}

int sp_smoother_destroy(sp_smoother **p)
{
    free(*p);
    return SP_OK;
}

int sp_smoother_init(sp_data *sp, sp_smoother *p)
{
    p->y0 = 0;
    p->b0 = 0;
    p->a1 = 0;
    p->psmooth = -100.0;
    p->smooth = 0.01;

    /* using this constant shaves off a multiply operation */
    p->onedsr = 1.0/sp->sr;
    return SP_OK;
}

int sp_smoother_compute(sp_data *sp, sp_smoother *p, SPFLOAT *in, SPFLOAT *out)
{
    USED(sp);

    if (p->psmooth != p->smooth) {
        p->a1 = pow(0.5, p->onedsr/p->smooth);
        p->b0 = 1.0 - p->a1;
        p->psmooth = p->smooth;
    }

    p->y0 = p->b0 * (*in) + p->a1 * p->y0;
    *out = p->y0;
    return SP_OK;
}

int sp_smoother_reset(sp_data *sp, sp_smoother *p, SPFLOAT *in)
{
    USED(sp);

    p->y0 = *in;
    return SP_OK;
}