~pbatch/patchwerk

patchwerk/examples/evn_sine.c -rw-r--r-- 3.6 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
#include <stdlib.h>
#include <soundpipe.h>
#include "patchwerk.h"
#include "sinosc.h"
#define BLKSIZE 64
#define N
typedef struct {
    int count;
    sp_metro *met;
    pw_patch *patch;
    pw_cable freq;
    pw_cable out;
    SPFLOAT ifreq;
    sp_data *sp;
    pw_evwalker walker;
    pw_evnode *node_A;
    pw_evnode *node_B;
} user_data;

static void compute(pw_node *node)
{
    int n;
    int blksize;
    user_data *ud;
    SPFLOAT met;

    blksize = pw_node_blksize(node);
    ud = pw_node_get_data(node);

    for(n = 0; n < blksize; n++) {
        sp_metro_compute(ud->sp, ud->met, NULL, &met);
        if(met != 0) {
            printf("tick...\n");
            pw_evwalker_walk(&ud->walker, n);
        }
        pw_cable_set(&ud->freq, n, ud->ifreq);
    }
}

static void destroy(pw_node *node)
{
    pw_node_cables_free(node);
}

static void node_eventer(pw_node *node, user_data *ud)
{
    pw_cable *cab;
    pw_node_cables_alloc(node, 1);
    pw_node_get_cable(node, 0, &cab);
    pw_cable_connect_nocheck(cab, &ud->freq);
    pw_node_set_compute(node, compute);
    pw_node_set_destroy(node, destroy);
    pw_node_set_data(node, ud);
}

static void process(sp_data *sp, void *ud)
{
    pw_patch *patch;
    user_data *data;

    data = ud;
    patch = data->patch;
   
    /* TODO: add clock for event graph */

    if(data->count == 0) {
       pw_patch_compute(patch); 
    }

    sp_out(sp, 0, pw_cable_get(&data->out, data->count));
    data->count = (data->count + 1) % BLKSIZE;
}

static pw_evnode * callback_A(pw_evnode *node, int pos)
{
    user_data *ud;
    ud = pw_evnode_data_get(node);
    printf("setting note to 72\n");
    ud->ifreq = sp_midi2cps(72);
    return pw_evnode_next(node);
}

static pw_evnode * callback_B(pw_evnode *node, int pos)
{
    user_data *ud;
    ud = pw_evnode_data_get(node);
    printf("setting note to 68\n");
    ud->ifreq = sp_midi2cps(68);
    return pw_evnode_next(node);
}

static void populate_events(user_data *ud)
{
    pw_evnode_new(ud->patch, &ud->node_A);
    pw_evnode_init(ud->node_A);
    pw_evnode_callback(ud->node_A, callback_A);
    pw_evnode_dur(ud->node_A, 4);
    pw_evnode_data_set(ud->node_A, ud);

    pw_evnode_new(ud->patch, &ud->node_B);
    pw_evnode_init(ud->node_B);
    pw_evnode_callback(ud->node_B, callback_B);
    pw_evnode_dur(ud->node_B, 2);
    pw_evnode_data_set(ud->node_B, ud);

    pw_evwalker_init(&ud->walker, ud->node_A, 0);

    pw_evnode_set_next(ud->node_A, ud->node_B);
    pw_evnode_set_next(ud->node_B, ud->node_A);
}

static void free_events(user_data *ud)
{
    pw_evnode_bye(ud->patch, &ud->node_A);
    pw_evnode_bye(ud->patch, &ud->node_B);
}

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

    patch = malloc(pw_patch_size());
    ud.patch = patch;
    ud.count = 0;
    ud.ifreq = 500;
    sp_create(&sp);
    ud.sp = sp;
    sp_metro_create(&ud.met);
    sp_metro_init(sp, ud.met);
    ud.met->freq = 4;

    pw_patch_init(patch, BLKSIZE);
    pw_cable_init(NULL, &ud.freq);
    pw_cable_init(NULL, &ud.out);
    pw_patch_alloc(patch, 4, 4);
    
    pw_patch_new_node(patch, &node);
    node_eventer(node, &ud);

    pw_patch_new_node(patch, &node);

    node_sinosc_v2(sp, node); 
    sinosc = pw_node_get_data(node);
    pw_cable_connect_nocheck(sinosc->out, &ud.out);
    pw_cable_connect_nocheck(&ud.freq, sinosc->freq);

    populate_events(&ud);

    sp_process(sp, &ud, process);

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