saturator

Files: saturator.h, saturator.c

Soft clip saturating distortion, based on examples from Abel/Berners' Music 424 course at Stanford.

Functions

sp_saturator_create(sp_saturator **saturator)
sp_saturator_init(sp_data *sp, sp_saturator *saturator)
sp_saturator_compute(sp_data *sp, sp_saturator *saturator, SPFLOAT *in, SPFLOAT *out)
sp_saturator_destroy(sp_saturator **saturator)

Optional Parameters

drive: Input gain into the distortion section, in decibels. Controls overall amount of distortion.
(Default value: 1.0)
dcoffset: Constant linear offset applied to the signal. A small offset will introduce odd harmonics into the distoration spectrum, whereas a zero offset will have only even harmonics.
(Default value: 0.0)

Inputs

in: input.

Outputs

out: output.

Example Code

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

typedef struct {
    sp_saturator *saturator;
    sp_diskin *diskin;
    sp_dcblock *dcblk;
} UserData;

void process(sp_data *sp, void *udata) 
{
    UserData *ud = udata;
    SPFLOAT diskin = 0, saturator = 0, dcblk = 0;
    sp_diskin_compute(sp, ud->diskin, NULL, &diskin);
    sp_saturator_compute(sp, ud->saturator, &diskin, &saturator);
    sp_dcblock_compute(sp, ud->dcblk, &saturator, &dcblk);
    sp_out(sp, 0, dcblk);
}

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

    sp_saturator_create(&ud.saturator);
    sp_diskin_create(&ud.diskin);
    sp_dcblock_create(&ud.dcblk);

    sp_saturator_init(sp, ud.saturator);
    sp_diskin_init(sp, ud.diskin, "oneart.wav");
    sp_dcblock_init(sp, ud.dcblk);

    ud.saturator->drive = 10;
    ud.saturator->dcoffset = 4;
    sp->len = 44100 * 5;
    sp_process(sp, &ud, process);

    sp_saturator_destroy(&ud.saturator);
    sp_diskin_destroy(&ud.diskin);
    sp_dcblock_destroy(&ud.dcblk);

    sp_destroy(&sp);
    return 0;
}