osc

Files: osc.h, osc.c

Table-lookup oscilator with linear interpolation

Functions

sp_osc_create(sp_osc **osc)
sp_osc_init(sp_data *sp, sp_osc *osc, sp_ftbl *tbl, SPFLOAT phase)
sp_osc_compute(sp_data *sp, sp_osc *osc, SPFLOAT *dummy, SPFLOAT *out)
sp_osc_destroy(sp_osc **osc)

Mandatory Parameters

tbl: Wavetable to read from. Note: the size of this table must be a power of 2.
(Recommended value: N/A)
phase: Initial phase of waveform, expects a value 0-1
(Recommended value: 0)

Optional Parameters

freq: Frequency (in Hz)
(Default value: 440)
amp: Amplitude (typically a value between 0 and 1).
(Default value: 0.2)

Outputs

out: Signal output.

Example Code

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

typedef struct {
    sp_osc *osc;
    sp_ftbl *ft; 
    int counter;
} UserData;

void write_osc(sp_data *sp, void *udata) {
    UserData *ud = udata;
    SPFLOAT osc = 0;
    if(ud->counter == 0){
        ud->osc->freq = 500 + rand() % 2000;
    }
    sp_osc_compute(sp, ud->osc, NULL, &osc);
    ud->counter = (ud->counter + 1) % 4410;
    sp_out(sp, 0, osc);
}

int main() {
    srand(time(NULL));
    UserData ud;
    ud.counter = 0;
    sp_data *sp;
    sp_create(&sp);
    sp_ftbl_create(sp, &ud.ft, 2048);
    sp_osc_create(&ud.osc);
    
    sp_gen_sine(sp, ud.ft);
    sp_osc_init(sp, ud.osc, ud.ft, 0);
    ud.osc->freq = 500;
    sp->len = 44100 * 5;
    sp_process(sp, &ud, write_osc);

    sp_ftbl_destroy(&ud.ft);
    sp_osc_destroy(&ud.osc);
    sp_destroy(&sp);
    return 0;
}