~pbatch/patchwerk

patchwerk/examples/test_3.c -rw-r--r-- 3.8 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdlib.h>
#include <soundpipe.h>
#include "patchwerk.h"
#include "sine.h"
#include "biscale.h"
#include "sinosc.h"
#include "scaler.h"

#define NUM_NODES 5

enum {
    LFO_FREQ,
    BISCALE_FREQ,
    LFO_AMP,
    BISCALE_AMP,
    SINE
};

typedef struct {
    pw_patch *patch;
    pw_cable *out;
    int pos;
} user_data;

static void traverse_node(pw_node *node, FILE *fp);
static void traverse_cable(pw_cable *cable, FILE *fp, int p);

/* labels associated with ugen_list.h */
static const char *labels[] = 
{
#define UGEN(T,S) #S,
#include "ugen_list.h"
#undef UGEN
};

static SPFLOAT block[BLKSIZE];

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

    if(ud->pos == 0) {
        pw_patch_compute(ud->patch);
    }

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

static void traverse_cable(pw_cable *cable, FILE *fp, int p)
{
    int id1;
    int id2;
    
    id1 = pw_node_get_id(cable->node);
    id2 = pw_node_get_id(cable->pcable->node);

    if(pw_cable_is_constant(cable)) {
        fprintf(fp, "\"p%d: %g\" -> %s_%d;\n", 
            p, pw_cable_get(cable, 0),
            labels[pw_node_get_type(cable->node)],
            id1
            );
        return;
    }

    if(id1 != id2) {
        fprintf(fp, "%s_%d -> %s_%d [label=\"p%d\"];\n", 
                labels[pw_node_get_type(cable->pcable->node)],
                id2, 
                labels[pw_node_get_type(cable->node)],
                id1,
                p
                );
        traverse_node(cable->pcable->node, fp);
    }
    
}

static void traverse_node(pw_node *node, FILE *fp)
{
    int ncables; int c;
    pw_cable *cable;

    ncables = pw_node_get_ncables(node);

    for(c = 0; c < ncables; c++) {
        pw_node_get_cable(node, c, &cable);
        traverse_cable(cable, fp, c);
    }
    
}

static void write_graph(pw_patch *patch, pw_cable *out, const char *filename)
{
    FILE *fp;

    fp = fopen(filename, "w");
    fprintf(fp, "digraph G {\n"); 

    if(out->node != NULL) {
        fprintf(fp, "%s_%d -> dac;\n", 
                labels[pw_node_get_type(out->node)],
                pw_node_get_id(out->node));
        traverse_node(out->pcable->node, fp);
    }

    fprintf(fp, "}\n");
    fclose(fp);
}

int main(int argc, char *argv[])
{
    sp_data *sp;
    user_data ud;
    pw_node *node;
    pw_patch *patch;
    sinosc_d *lfo;
    sinosc_d *sinosc_1;
    scaler_d *scaler;

    sp_create(&sp);

    ud.pos = 0;
    ud.patch = malloc(pw_patch_size());
    /* TODO: where is the uninitialized value? */
    pw_patch_init(ud.patch, BLKSIZE); 
    patch = ud.patch;

    /* Set up connections */

    /* LFO */
    pw_patch_new_node(patch, &node);

    node_sinosc(sp, node, block);
    lfo = pw_node_get_data(node);
    pw_cable_set_constant(lfo->amp, 1);
    pw_cable_set_constant(lfo->freq, 4);
    
    /* BISCALE */
    pw_patch_new_node(patch, &node);

    node_scaler(sp, node, block);
    scaler = pw_node_get_data(node);
    pw_cable_set_constant(scaler->min, 100);
    pw_cable_set_constant(scaler->max, 800);

    /* connect LFO to Biscale input */
    pw_cable_connect(lfo->out, scaler->in);

    /* Sine node */
    pw_patch_new_node(patch, &node);

    node_sinosc(sp, node, block);
    sinosc_1 = pw_node_get_data(node);
    pw_cable_set_constant(sinosc_1->amp, 0.5);
    pw_cable_set_constant(sinosc_1->freq, 880);
    
    /* connect biscale out to frequency */

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

    ud.out = sinosc_1->out;
    sp_process(sp, &ud, process);
    write_graph(ud.patch, ud.out, "test_3.dot");
    printf("wrote dot file test_3.dot to disk.\n");
    printf("run 'dot -Tpng test_3.dot > test_3.png' to render it.\n");

    pw_patch_destroy(patch);
    pw_patch_free_nodes(patch);

    free(ud.patch);
    sp_destroy(&sp);
    return 0;
}