randh

Files: randh.h, randh.c

Random number generator with hold time. Randh is loosely based off of the Csound opcode randomh. The design is equivalent to scaled noise sent through a classic sample and hold module.

Functions

sp_randh_create(sp_randh **randh)
sp_randh_init(sp_data *sp, sp_randh *randh)
sp_randh_compute(sp_data *sp, sp_randh *randh, SPFLOAT *out)
sp_randh_destroy(sp_randh **randh)

Optional Parameters

min: Minimum value to use.
(Default value: 0)
max: Maximum value to use.
(Default value: 1)
freq: Frequency of randomization (in Hz)
(Default value: 10)

Outputs

out: Signal output.

Example Code

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

typedef struct {
    sp_randh *randh;
    sp_osc *osc;
    sp_ftbl *ft; 
} UserData;

void process(sp_data *sp, void *udata) {
    UserData *ud = udata;
    SPFLOAT osc = 0, randh = 0;
    sp_randh_compute(sp, ud->randh, NULL, &randh);
    ud->osc->freq = randh;
    sp_osc_compute(sp, ud->osc, NULL, &osc);
    sp->out[0] = osc;
}

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

    sp_randh_create(&ud.randh);
    sp_osc_create(&ud.osc);
    sp_ftbl_create(sp, &ud.ft, 2048);

    sp_randh_init(sp, ud.randh);
    ud.randh->freq = 10;
    ud.randh->min = 40;
    ud.randh->max = 1000;

    sp_gen_sine(sp, ud.ft);
    sp_osc_init(sp, ud.osc, ud.ft, 0);

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

    sp_randh_destroy(&ud.randh);
    sp_ftbl_destroy(&ud.ft);
    sp_osc_destroy(&ud.osc);

    sp_destroy(&sp);
    return 0;
}