blsquare

Files: blsquare.h, blsquare.c

Bandlimited square oscillator This is a bandlimited square oscillator ported from the "squaretooth" function from the Faust programming language.

Functions

sp_blsquare_create(sp_blsquare **blsquare)
sp_blsquare_init(sp_data *sp, sp_blsquare *blsquare)
sp_blsquare_compute(sp_data *sp, sp_blsquare *blsquare, SPFLOAT *dummy, SPFLOAT *out)
sp_blsquare_destroy(sp_blsquare **blsquare)

Optional Parameters

freq: Frequency, (range 0-20000)
(Default value: 440)
amp: Amplitude (range 0-1).
(Default value: 1.0)
width: Duty cycle width (range 0-1).
(Default value: 0.5)

Outputs

out: Signal output.

Example Code

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

typedef struct {
    sp_blsquare *blsquare;
    int counter;
} UserData;

void process(sp_data *sp, void *udata) {
    UserData *ud = udata;
    if(ud->counter == 0){
        *ud->blsquare->freq = 500 + rand() % 2000;
    }
    sp_blsquare_compute(sp, ud->blsquare, NULL, &sp->out[0]);
    ud->counter = (ud->counter + 1) % 4410;
}

int main() {
    srand(time(NULL));
    UserData ud;
    ud.counter = 0;
    sp_data *sp;
    sp_create(&sp);

    sp_blsquare_create(&ud.blsquare);

    sp_blsquare_init(sp, ud.blsquare);
    *ud.blsquare->freq = 500.0;
    *ud.blsquare->amp = 0.9;
    *ud.blsquare->width = 0.5;
    sp->len = 44100 * 5;
    sp_process(sp, &ud, process);

    sp_blsquare_destroy(&ud.blsquare);
    sp_destroy(&sp);
    return 0;
}