9. Patchwerk Nodes

9.1. Funcdefs and Funcs

Worgle structure stuff mostly. If SEQVM_NO_PATCHWERKis defined, it will compile seqvm without these components.

<<funcdefs>>=
#ifndef SEQVM_NO_PATCHWERK
<<gf_funcdefs>>
#endif
<<funcs>>=
#ifndef SEQVM_NO_PATCHWERK
<<gf_funcs>>
#endif

9.2. seqvm mother node

<<gf_funcdefs>>=
seqvm * node_seqvm(gf_node *node, int pool_size);
<<gf_funcs>>=
static void n_seqvm_compute(gf_node *node)
{
    int blksize;
    int n;
    gf_cable *in;
    seqvm *svm;

    svm = gf_node_get_data(node);
    blksize = gf_node_blksize(node);

    gf_node_get_cable(node, 0, &in);

    seqvm_reset(svm);
    for (n = 0; n < blksize; n++) {
        PWFLT clk;
        clk = gf_cable_get(in, n);

        if (clk != 0) {
            seqvm_tick(svm, n);
        }
    }
}

static void n_seqvm_destroy(gf_node *node)
{
    void *ud;
    int rc;
    seqvm_cell *pool;
    seqvm *svm;
    gf_patch *patch;

    gf_node_cables_free(node);
    rc = gf_node_get_patch(node, &patch);
    if (rc != PW_OK) return;

    svm = gf_node_get_data(node);

    pool = seqvm_get_pool(svm);

    ud = pool;
    rc = gf_memory_free(patch, &ud);
    if (rc != PW_OK) return;

    ud = svm;
    rc = gf_memory_free(patch, &ud);
    if (rc != PW_OK) return;

}

seqvm * node_seqvm(gf_node *node, int pool_size)
{
    seqvm *svm;
    seqvm_cell *pool;
    int rc;
    void *ud;
    gf_patch *patch;

    svm = NULL;

    rc = gf_node_get_patch(node, &patch);

    if (rc != PW_OK) return NULL;

    rc = gf_memory_alloc(patch, sizeof(seqvm), &ud);
    if (rc != PW_OK) return NULL;
    svm = ud;

    rc = gf_memory_alloc(patch, sizeof(seqvm_cell) * pool_size, &ud);
    if (rc != PW_OK) return NULL;
    pool = ud;

    seqvm_init(svm, pool, pool_size);

    gf_node_set_compute(node, n_seqvm_compute);
    gf_node_set_destroy(node, n_seqvm_destroy);
    gf_node_set_data(node, svm);

    gf_node_cables_alloc(node, 1);

    return svm;
}

9.3. val

<<gf_funcdefs>>=
int node_seqvm_val(gf_node *node, seqvm *svm);
<<gf_funcs>>=
static void n_chan_val(gf_node *node)
{
    int blksize;
    int n;
    gf_cable *chan;
    seqvm *svm;
    gf_cable *out;

    blksize = gf_node_blksize(node);
    svm = gf_node_get_data(node);
    gf_node_get_cable(node, 0, &chan);
    gf_node_get_cable(node, 1, &out);

    for (n = 0; n < blksize; n++) {
        int c;
        PWFLT o;
        seqvm_chan *sc;

        c = gf_cable_get(chan, n);
        sc = seqvm_get_chan(svm, c, n);

        if (sc != NULL) o = sc->val;
        else o = 0;

        gf_cable_set(out, n, o);
    }
}

int node_seqvm_val(gf_node *node, seqvm *svm)
{
    gf_node_cables_alloc(node, 2);
    gf_node_set_block(node, 1);
    gf_node_set_compute(node, n_chan_val);
    gf_node_set_data(node, svm);
    return PW_OK;
}

9.4. tick

<<gf_funcdefs>>=
int node_seqvm_tick(gf_node *node, seqvm *svm);
<<gf_funcs>>=
static void n_chan_tick(gf_node *node)
{
    int blksize;
    int n;
    gf_cable *chan;
    seqvm *svm;
    gf_cable *out;

    blksize = gf_node_blksize(node);
    svm = gf_node_get_data(node);
    gf_node_get_cable(node, 0, &chan);
    gf_node_get_cable(node, 1, &out);

    for (n = 0; n < blksize; n++) {
        int c;
        PWFLT o;

        c = gf_cable_get(chan, n);

        o = 0;

        if (n == svm->blkpos) {
            seqvm_chan *sc;
            sc = seqvm_get_chan(svm, c, n);
            if (sc != NULL) {
                o = sc->tick;
            }
        }

        gf_cable_set(out, n, o);
    }
}

int node_seqvm_tick(gf_node *node, seqvm *svm)
{
    gf_node_cables_alloc(node, 2);
    gf_node_set_block(node, 1);
    gf_node_set_compute(node, n_chan_tick);
    gf_node_set_data(node, svm);
    return PW_OK;
}

9.5. retick

<<gf_funcdefs>>=
int node_seqvm_retick(gf_node *node, seqvm *svm, int start);
<<gf_funcs>>=
struct retick_n {
    seqvm *svm;
    int pos;
};

static void retick_compute(gf_node *node)
{
    int blksize;
    int n;
    struct retick_n *rt;
    seqvm *svm;

    rt = gf_node_get_data(node);
    svm = rt->svm;
    blksize = gf_node_blksize(node);

    for (n = 0; n < blksize; n++) {
        if (svm->blkpos == n) {
            seqvm_retick(svm, &rt->pos);
        }
    }
}

static void retick_destroy(gf_node *node)
{
    void *ud;
    gf_patch *patch;
    int rc;

    gf_node_cables_free(node);
    rc = gf_node_get_patch(node, &patch);

    if (rc != PW_OK) return;
    ud = gf_node_get_data(node);
    gf_memory_free(patch, &ud);
}

int node_seqvm_retick(gf_node *node, seqvm *svm, int start)
{
    struct retick_n *rt;
    void *ud;
    gf_patch *patch;
    int rc;

    rc = gf_node_get_patch(node, &patch);

    if (rc != PW_OK) return rc;

    rc = gf_memory_alloc(patch, sizeof(struct retick_n), &ud);
    if (rc != PW_OK) return rc;

    rt = ud;

    rt->pos = start;
    rt->svm = svm;
    gf_node_set_compute(node, retick_compute);
    gf_node_set_destroy(node, retick_destroy);
    gf_node_set_data(node, rt);
    return PW_OK;
}

9.6. gate

<<gf_funcdefs>>=
int node_seqvm_gate(gf_node *node, seqvm *svm);
<<gf_funcs>>=
static void n_chan_gate(gf_node *node)
{
    int blksize;
    int n;
    gf_cable *chan;
    seqvm *svm;
    gf_cable *out;

    blksize = gf_node_blksize(node);
    svm = gf_node_get_data(node);
    gf_node_get_cable(node, 0, &chan);
    gf_node_get_cable(node, 1, &out);

    for (n = 0; n < blksize; n++) {
        int c;
        PWFLT o;
        seqvm_chan *sc;

        c = gf_cable_get(chan, n);
        sc = seqvm_get_chan(svm, c, n);

        if (sc != NULL) o = sc->gate;
        else o = 0;

        gf_cable_set(out, n, o);
    }
}

int node_seqvm_gate(gf_node *node, seqvm *svm)
{
    gf_node_cables_alloc(node, 2);
    gf_node_set_block(node, 1);
    gf_node_set_compute(node, n_chan_gate);
    gf_node_set_data(node, svm);
    return PW_OK;
}



prev | home | next