shithub: soundpipe

ref: 775d674bf2fea68eb44212ba9ee3ca82d93de451
dir: /modules/delay.c/

View raw version
/* This code is placed in the public domain. */

#include <stdlib.h>
#include <math.h>
#include "soundpipe.h"

int sp_delay_create(sp_delay **p)
{
    *p = malloc(sizeof(sp_delay));
    return SP_OK;
}

int sp_delay_destroy(sp_delay **p)
{
    free((*p)->buf);
    free(*p);
    return SP_OK;
}

int sp_delay_init(sp_data *sp, sp_delay *p, SPFLOAT time)
{
    p->time = time;
    p->bufsize = round(time * sp->sr);
    p->buf = calloc(1, p->bufsize * sizeof(SPFLOAT));
    p->bufpos = 0;
    p->feedback = 0;
    p->last = 0;
    return SP_OK;
}

int sp_delay_compute(sp_data *sp, sp_delay *p, SPFLOAT *in, SPFLOAT *out)
{
    SPFLOAT delay = 0, sig = 0;
    SPFLOAT *buf = p->buf;
    delay = buf[p->bufpos];
    sig = (delay * p->feedback) + *in;
    buf[p->bufpos] = sig;
    p->bufpos = (p->bufpos + 1) % p->bufsize;
    *out = delay;
    return SP_OK;
}