~pbatch/patchwerk

patchwerk/examples/test_1.c -rw-r--r-- 1.7 KiB
9c265356 — paul plan9 additions from Sigrid 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdlib.h>
#include <soundpipe.h>
#include "patchwerk.h"
#include "sinosc.h"
#include "scaler.h"

#define NUM_NODES 3

typedef struct {
    pw_node *node[NUM_NODES];
    pw_cable *out;
    int pos;
} user_data;

static void process(sp_data *sp, void *ptr)
{
    int i;
    user_data *ud;
    ud = ptr;
    SPFLOAT out;

    if(ud->pos == 0) {
        for(i = 0; i < NUM_NODES; i++) {
            pw_node_compute(ud->node[i]);
        }
    }

    out = pw_cable_get(ud->out, ud->pos);
    ud->pos = (ud->pos + 1) % BLKSIZE;
    sp_out(sp, 0, out);
}

static SPFLOAT block[BLKSIZE];
static SPFLOAT block_2[BLKSIZE];

int main()
{
    user_data ud;
    sp_data *sp;
    int n;
    sinosc_d *sinosc_2;
    sinosc_d *sinosc_1;
    scaler_d *scaler;

    sp_create(&sp);

    ud.pos = 0;
    for(n = 0; n < NUM_NODES; n++) {
        ud.node[n] = malloc(pw_node_size());
        pw_node_init(ud.node[n], BLKSIZE);
        pw_node_set_id(ud.node[n], n);
    }

    node_sinosc(sp, ud.node[0], block);
    node_scaler(sp, ud.node[1], block);
    node_sinosc(sp, ud.node[2], block);

    sinosc_2 = pw_node_get_data(ud.node[2]);
    sinosc_1 = pw_node_get_data(ud.node[0]);
    scaler = pw_node_get_data(ud.node[1]);

    pw_cable_set_constant(scaler->min, 200);
    pw_cable_set_constant(scaler->max, 800);
    pw_cable_set_constant(sinosc_1->amp, 1);
    pw_cable_set_constant(sinosc_1->freq, 4);

    pw_cable_connect(sinosc_1->out, scaler->in);
    pw_cable_connect(scaler->out, sinosc_2->freq);

    ud.out = sinosc_2->out;
    sp_process(sp, &ud, process);

    for(n = 0; n < NUM_NODES; n++) {
        pw_node_destroy(ud.node[n]);
        free(ud.node[n]);
    }
    sp_destroy(&sp);
    return 0;
}