~pbatch/patchwerk

patchwerk/examples/test_2.c -rw-r--r-- 2.3 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
#include <stdlib.h>
#include <soundpipe.h>
#include "patchwerk.h"
#include "sinosc.h"
#include "scaler.h"

#define NUM_NODES 3

enum {
    SINE_1,
    BISCALE,
    SINE_2
};

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

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 connect(
    pw_patch *patch, 
    pw_node *node1, int param1, 
    pw_node *node2, int param2
) {
    pw_cable *cable[2];

    pw_node_get_cable(node1, param1, &cable[0]);

    pw_node_get_cable(node2, param2, &cable[1]);

    pw_cable_connect(cable[0], cable[1]);
}

static void constant(pw_patch *patch, pw_node *node, int param, PWFLT val)
{
    pw_cable *cable;
    pw_node_get_cable(node, param, &cable);
    pw_cable_set_constant(cable, val);
}

SPFLOAT block[BLKSIZE];

int main(int argc, char *argv[])
{
    sp_data *sp;
    user_data ud;
    pw_node *node;
    pw_node *n[3];
    pw_cable *cable;

    sp_create(&sp);

    ud.patch = malloc(pw_patch_size());

    ud.pos = 0;
    pw_patch_init(ud.patch, BLKSIZE);

    pw_patch_new_node(ud.patch, &n[SINE_1]);
    node_sinosc(sp, n[SINE_1], block);

    pw_patch_new_node(ud.patch, &n[BISCALE]);
    node_scaler(sp, n[BISCALE], block);
    
    pw_patch_new_node(ud.patch, &n[SINE_2]);
    node_sinosc(sp, n[SINE_2], block);

    /* Set up connections */

    /* sinosc_1 -> scaler */
    connect(ud.patch, n[SINE_1], 2, n[BISCALE], 0);
    /* scaler -> sinosc_2 */
    connect(ud.patch, n[BISCALE], 3, n[SINE_2], 0);

    /* constants for sinosc_1 */
    constant(ud.patch, n[SINE_1], 0, 3);
    constant(ud.patch, n[SINE_1], 1, 1);

    /* constants for scaler */
    constant(ud.patch, n[BISCALE], 1, 100);
    constant(ud.patch, n[BISCALE], 2, 600);
   
    /* constants for sinosc_2 */
    constant(ud.patch, n[SINE_2], 1, 0.8);
    
    /* set output to be output of sinosc_2*/
    pw_node_get_cable(n[SINE_2], 2, &cable);
    pw_patch_set_out(ud.patch, cable);


    ud.out = pw_patch_get_out(ud.patch);
    sp_process(sp, &ud, process);

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