~pbatch/sndkit

ff1e08e80e51557c9171f1e99a942d1e1ee3f405 — Paul Batchelor 7 months ago 57d250f master
added metrosync
M dsp/metro.org => dsp/metro.org +23 -0
@@ 121,6 121,29 @@ void sk_metro_freq(sk_metro *m, SKFLT freq)
    m->freq = freq;
}
#+END_SRC
* Reset
The metro object can be reset using =sk_metro_reset=.

When this is called, it will cause the the next call
to =sk_metro_tick= to produce a tick. The previously
set frequency value will be untouched.

To achieve a reset, the =init= flag is set, and
internal phase value =phs= reset to zero.

#+NAME: funcdefs
#+BEGIN_SRC c
void sk_metro_reset(sk_metro *m);
#+END_SRC

#+NAME: funcs
#+BEGIN_SRC c
void sk_metro_reset(sk_metro *m)
{
    m->init = 1;
    m->phs = 0;
}
#+END_SRC
* Compute
Compute a sample of audio with =sk_metro_tick=.


M nodes/metro/l_metro.c => nodes/metro/l_metro.c +20 -0
@@ 7,6 7,7 @@
#include "sklil.h"

int sk_node_metro(sk_core *core);
int sk_node_metrosync(sk_core *core);

static lil_value_t metro(lil_t lil, size_t argc, lil_value_t *argv)
{


@@ 24,7 25,26 @@ static lil_value_t metro(lil_t lil, size_t argc, lil_value_t *argv)
    return NULL;
}

static lil_value_t metrosync(lil_t lil, size_t argc, lil_value_t *argv)
{
    sk_core *core;
    int rc;
    core = lil_get_data(lil);

    SKLIL_ARITY_CHECK(lil, "metrosync", argc, 2);

    rc = sklil_param(core, argv[0]);
    SKLIL_PARAM_CHECK(lil, rc, "metrosync");
    rc = sklil_param(core, argv[1]);
    SKLIL_PARAM_CHECK(lil, rc, "metrosync");

    rc = sk_node_metrosync(core);
    SKLIL_ERROR_CHECK(lil, rc, "metrosync didn't work out.");
    return NULL;
}

void sklil_load_metro(lil_t lil)
{
    lil_register(lil, "metro", metro);
    lil_register(lil, "metrosync", metrosync);
}

M nodes/metro/metro.c => nodes/metro/metro.c +75 -8
@@ 8,6 8,7 @@

struct metro_n {
    gf_cable *freq;
    gf_cable *reset;
    gf_cable *out;
    sk_metro metro;
};


@@ 32,6 33,31 @@ static void compute(gf_node *node)
    }
}

static void compute_reset(gf_node *node)
{
    int blksize;
    int n;
    struct metro_n *metro;

    blksize = gf_node_blksize(node);

    metro = (struct metro_n *)gf_node_get_data(node);

    for (n = 0; n < blksize; n++) {
        GFFLT out, reset, freq;
        freq = gf_cable_get(metro->freq, n);
        reset = gf_cable_get(metro->reset, n);

        if (reset > 0) {
            sk_metro_reset(&metro->metro);
        }

        sk_metro_freq(&metro->metro, freq);
        out = sk_metro_tick(&metro->metro);
        gf_cable_set(metro->out, n, out);
    }
}

static void destroy(gf_node *node)
{
    gf_patch *patch;


@@ 44,16 70,21 @@ static void destroy(gf_node *node)
    gf_memory_free(patch, &ud);
}

int sk_node_metro(sk_core *core)
static int node_metro(sk_core *core, int sync)
{
    gf_patch *patch;
    gf_node *node;
    int rc;
    sk_param freq;
    sk_param freq, reset;
    void *ud;
    struct metro_n *metro;
    int sr;

    if (sync) {
        rc = sk_param_get(core, &reset);
        SK_ERROR_CHECK(rc);
    }

    rc = sk_param_get(core, &freq);
    SK_ERROR_CHECK(rc);



@@ 69,19 100,55 @@ int sk_node_metro(sk_core *core)
    rc = gf_patch_new_node(patch, &node);
    SK_GF_ERROR_CHECK(rc);

    rc = gf_node_cables_alloc(node, 2);
    SK_GF_ERROR_CHECK(rc);
    if (sync) {
        rc = gf_node_cables_alloc(node, 3);
        SK_GF_ERROR_CHECK(rc);
    } else {
        rc = gf_node_cables_alloc(node, 2);
        SK_GF_ERROR_CHECK(rc);
    }

    gf_node_set_block(node, 1);
    if (sync) {
        gf_node_set_block(node, 2);
    } else {
        gf_node_set_block(node, 1);
    }

    gf_node_get_cable(node, 0, &metro->freq);
    gf_node_get_cable(node, 1, &metro->out);

    if (sync) {
        gf_node_get_cable(node, 1, &metro->reset);
        gf_node_get_cable(node, 2, &metro->out);
    } else {
        gf_node_get_cable(node, 1, &metro->out);
    }

    gf_node_set_data(node, metro);
    gf_node_set_compute(node, compute);

    if (sync) {
        gf_node_set_compute(node, compute_reset);
    } else {
        gf_node_set_compute(node, compute);
    }

    gf_node_set_destroy(node, destroy);

    sk_param_set(core, node, &freq, 0);
    sk_param_out(core, node, 1);
    if (sync) {
        sk_param_set(core, node, &reset, 1);
        sk_param_out(core, node, 2);
    } else {
        sk_param_out(core, node, 1);
    }
    return 0;
}

int sk_node_metro(sk_core *core)
{
    return node_metro(core, 0);
}

int sk_node_metrosync(sk_core *core)
{
    return node_metro(core, 1);
}

M nodes/metro/test.lil => nodes/metro/test.lil +1 -1
@@ 1,3 1,3 @@
metro 4
wavout zz "test.wav"
computes 10
\ No newline at end of file
computes 10

A nodes/metro/test_metrosync.lil => nodes/metro/test_metrosync.lil +6 -0
@@ 0,0 1,6 @@
metrosync [param 4] [metro 1.5]
env zz 0.001 0.001 0.01
sine 1000 0.8
mul zz zz
wavout zz "test.wav"
computes 10

A test/t/metrosync.lil => test/t/metrosync.lil +5 -0
@@ 0,0 1,5 @@
metrosync [param 4] [metro 1.5]
env zz 0.001 0.001 0.01
sine 1000 0.8
mul zz zz
verify abcda2fe1fb15f56b1eb497e32cf5d7b

M test/test.sh => test/test.sh +1 -0
@@ 74,3 74,4 @@ check butbp
check gtick
check euclid
check tractxyv
check metrosync