adsr

Files: adsr.h, adsr.c

ADSR generator This is an ADSR generator whose curves are created using a one-pole low pass filter.

Functions

sp_adsr_create(sp_adsr **adsr)
sp_adsr_init(sp_data *sp, sp_adsr *adsr)
sp_adsr_compute(sp_data *sp, sp_adsr *adsr, SPFLOAT *gate, SPFLOAT *out)
sp_adsr_destroy(sp_adsr **adsr)

Optional Parameters

atk: Attack time (in seconds)
(Default value: 0.1)
dec: Decay time (in seconds)
(Default value: 0.1)
sus: Sustain (in range 0-1)
(Default value: 0.5)
rel: Release time (in seconds)
(Default value: 0.3)

Inputs

gate: Gate signal. This should be a steady state signal that jumps from 0 to 1.

Outputs

out: ADSR envelope.

Example Code

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "soundpipe.h"

typedef struct {
    sp_adsr *adsr;
    sp_osc *osc;
    sp_ftbl *ft; 
} UserData;

void process(sp_data *sp, void *udata) {
    UserData *ud = udata;
    SPFLOAT osc = 0, adsr = 0, gate = 0;
    if(sp->pos < sp->sr * 3) {
        gate = 1;
    }
    sp_adsr_compute(sp, ud->adsr, &gate, &adsr);
    sp_osc_compute(sp, ud->osc, NULL, &osc);
    sp->out[0] = adsr * osc;
}

int main() {
    srand(1234567);
    UserData ud;
    sp_data *sp;
    sp_create(&sp);

    sp_adsr_create(&ud.adsr);
    sp_osc_create(&ud.osc);
    sp_ftbl_create(sp, &ud.ft, 2048);

    sp_adsr_init(sp, ud.adsr);
    sp_gen_sine(sp, ud.ft);
    sp_osc_init(sp, ud.osc, ud.ft, 0);
    ud.osc->amp = 0.5;

    sp->len = 44100 * 5;
    sp_process(sp, &ud, process);

    sp_adsr_destroy(&ud.adsr);
    sp_ftbl_destroy(&ud.ft);
    sp_osc_destroy(&ud.osc);

    sp_destroy(&sp);
    return 0;
}