pluck

Files: pluck.h, pluck.c

Karplus-Strong plucked string instrument.

Functions

sp_pluck_create(sp_pluck **pluck)
sp_pluck_init(sp_data *sp, sp_pluck *pluck, SPFLOAT ifreq)
sp_pluck_compute(sp_data *sp, sp_pluck *pluck, SPFLOAT *trigger, SPFLOAT *out)
sp_pluck_destroy(sp_pluck **pluck)

Mandatory Parameters

ifreq: Sets the initial frequency. This frequency is used to allocate all the buffers needed for the delay. This should be the lowest frequency you plan on using.
(Recommended value: 110)

Optional Parameters

freq: Variable frequency. Values less than the initial frequency (ifreq) will be doubled until it is greater than or equal to ifreq.
(Default value: ifreq)
amp: Amplitude
(Default value: 0.5)

Inputs

trigger: Trigger input. When non-zero, will reinitialize and pluck.

Outputs

out: Signal output.

Example Code

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

typedef struct {
    sp_pluck *pluck;
    sp_metro *met;
} UserData;

void process(sp_data *sp, void *udata) {
    UserData *ud = udata;
    SPFLOAT pluck = 0, met = 0;
    sp_metro_compute(sp, ud->met, NULL, &met);
    SPFLOAT notes[] = {60, 63, 67, 70, 74};
    if(met) {
        ud->pluck->freq = sp_midi2cps(notes[sp_rand(sp) % 5]);
    }
    sp_pluck_compute(sp, ud->pluck, &met, &pluck);
    sp->out[0] = pluck;
}

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

    sp_pluck_create(&ud.pluck);
    sp_metro_create(&ud.met);

    sp_pluck_init(sp, ud.pluck, 400);
    sp_metro_init(sp, ud.met);
    ud.met->freq = 4;

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

    sp_pluck_destroy(&ud.pluck);
    sp_metro_destroy(&ud.met);

    sp_destroy(&sp);
    return 0;
}