~pbatch/patchwerk

patchwerk/examples/siren.c -rw-r--r-- 8.1 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Siren
 *
 * This code will generate a simple siren patch using
 * some of the pre-made patchwerk-wrapped soundpipe dsp
 * nodes found included in the patchwerk codebase.
 *
 * The patch is equivalent to the following runt code:
 *
 * == BEGIN RUNT CODE ==
 *
 * patchwerk nodes
 *
 * 0.1 1 sine 0 1 biscale bhold 0 cabset
 *
 * 0 cabget 100 600 scale 0.5 blsaw
 *
 * 0 cabget 100 2000 scale butlp
 *
 * bdup
 * bdup 0.97 10000 revsc bdrop 0.1 mul dcblock
 *
 * 0 cabget bunhold
 *
 * add
 *
 * "siren.wav" wavout bdrop
 *
 * sr 10 * _compute rep
 *
 * == END RUNT CODE ==
 *
 * It is advisable to understand how the runt code above
 * works before attempting to parse out the C code below.
 * That way, the runt code can be used as a sort of Rosetta
 * Stone. Comments in the C program will break up the
 * program by Runt statements. With any luck, a reader
 * should begin to understand connection between the
 * Patchwerk C library and Runt abstraction.
 *
 * Even with the DSP wrapper code, one can see from
 * this small program that the Patchwerk C API at this level
 * is quite repetive and redundant. In practice, it is best
 * to write abstractions on top of this and not mess with
 * these C operations directly. Otherwise, it is slow and
 * tedious work.
 *
 * When building up a patchwerk patch, the programmer must
 * be able to keep track of what is on the buffer stack at
 * all times. Missing a push or pop operation can cause the
 * entire patch to break. At the C level, these can be very
 * tedious to debug! It is highly recommended to express the
 * Patch using some sort of postfix notation like Sporth or
 * Runt. Presenting the patch in this way will naturally
 * align the stack operations.
 *
 */

#include <stdlib.h>
#include <soundpipe.h>
#include "patchwerk.h"

#include "../dsp/sine.h"
#include "../dsp/wavout.h"
#include "../dsp/biscale.h"
#include "../dsp/blsaw.h"
#include "../dsp/scale.h"
#include "../dsp/butlp.h"
#include "../dsp/revsc.h"
#include "../dsp/mul.h"
#include "../dsp/dcblock.h"
#include "../dsp/add.h"

#define NBUFS 8
#define STACKSIZE 10
#define SR 44100

static add_d * mk_add(pw_patch *patch,
                      sp_data *sp)
{
    pw_node *node;
    add_d *add;
    pw_patch_new_node(patch, &node);
    node_add(node, sp);
    pw_node_setup(node);
    add = pw_node_get_data(node);
    return add;
}

static dcblock_d * mk_dcblock(pw_patch *patch,
                              sp_data *sp)
{
    pw_node *node;
    dcblock_d *dcblock;
    pw_patch_new_node(patch, &node);
    node_dcblock(node, sp);
    pw_node_setup(node);
    dcblock = pw_node_get_data(node);
    return dcblock;
}

static mul_d * mk_mul(pw_patch *patch,
                      sp_data *sp)
{
    pw_node *node;
    mul_d *mul;
    pw_patch_new_node(patch, &node);
    node_mul(node, sp);
    pw_node_setup(node);
    mul = pw_node_get_data(node);
    return mul;
}

static revsc_d * mk_revsc(pw_patch *patch,
                          sp_data *sp)
{
    pw_node *node;
    revsc_d *revsc;
    pw_patch_new_node(patch, &node);
    node_revsc(node, sp);
    pw_node_setup(node);
    revsc = pw_node_get_data(node);
    return revsc;
}

static butlp_d * mk_butlp(pw_patch *patch,
                          sp_data *sp)
{
    pw_node *node;
    butlp_d *butlp;
    pw_patch_new_node(patch, &node);
    node_butlp(node, sp);
    pw_node_setup(node);
    butlp = pw_node_get_data(node);
    return butlp;
}

static blsaw_d * mk_blsaw(pw_patch *patch,
                          sp_data *sp)
{
    pw_node *node;
    blsaw_d *blsaw;
    pw_patch_new_node(patch, &node);
    node_blsaw(node, sp);
    pw_node_setup(node);
    blsaw = pw_node_get_data(node);
    return blsaw;
}

static scale_d * mk_scale(pw_patch *patch,
                          sp_data *sp)
{
    pw_node *node;
    scale_d *scale;
    pw_patch_new_node(patch, &node);
    node_scale(node, sp);
    pw_node_setup(node);
    scale = pw_node_get_data(node);
    return scale;
}

static biscale_d * mk_biscale(pw_patch *patch,
                              sp_data *sp)
{
    pw_node *node;
    biscale_d *biscale;
    pw_patch_new_node(patch, &node);
    node_biscale(node, sp);
    pw_node_setup(node);
    biscale = pw_node_get_data(node);
    return biscale;
}

static sine_d * mk_sine(pw_patch *patch,
                        sp_data *sp)
{
    pw_node *node;
    sine_d *sine;
    pw_patch_new_node(patch, &node);
    node_sine(node, sp);
    pw_node_setup(node);
    sine = pw_node_get_data(node);
    return sine;
}

static wavout_d * mk_wavout(pw_patch *patch,
                            sp_data *sp,
                            const char *filename)
{
    pw_node *node;
    wavout_d *wavout;

    pw_patch_new_node(patch, &node);
    node_wavout(sp, node, filename);
    pw_node_setup(node);
    wavout = pw_node_get_data(node);
    return wavout;
}

int main(int argc, char *argv[])
{
    sp_data *sp;
    pw_patch *patch;
    sine_d *sine;
    pw_stack *stack;
    wavout_d *wavout;
    unsigned int n;
    pw_buffer *buf;
    pw_cable *lfo;
    biscale_d *biscale;
    blsaw_d *blsaw;
    scale_d *scale;
    butlp_d *butlp;
    revsc_d *revsc;
    mul_d *mul;
    add_d *add;
    dcblock_d *dcblock;


    /* initialize + allocate */

    sp_create(&sp);

    patch = calloc(1, pw_patch_size());
    pw_patch_init(patch, BLKSIZE);
    pw_patch_alloc(patch, NBUFS, STACKSIZE);
    pw_patch_srate_set(patch, SR);
    sp->sr = pw_patch_srate_get(patch);
    pw_patch_data_set(patch, sp);
    stack = pw_patch_stack(patch);

    /* 0.1 1 sine */
    sine = mk_sine(patch, sp);
    pw_cable_set_value(sine->freq, 0.1);
    pw_cable_set_value(sine->amp, 1);

    /* 0 1 biscale */

    pw_stack_pop(stack, NULL);

    biscale = mk_biscale(patch, sp);

    pw_cable_connect(sine->out, biscale->in);
    pw_cable_set_value(biscale->min, 0);
    pw_cable_set_value(biscale->max, 1);

    /* bhold 0 cabset */

    pw_patch_bhold(patch, &buf);
    pw_stack_pop(stack, NULL);
    lfo = biscale->out;

    /* 0 cabget 100 600 scale */

    pw_stack_push_buffer(stack, buf);

    pw_stack_pop(stack, NULL);
    scale = mk_scale(patch, sp);
    pw_cable_connect(lfo, scale->in);
    pw_cable_set_value(scale->min, 100);
    pw_cable_set_value(scale->max, 600);

    /* 0.5 blsaw */

    pw_stack_pop(stack, NULL);

    blsaw = mk_blsaw(patch, sp);
    pw_cable_set_value(blsaw->amp, 0.5);
    pw_cable_connect(scale->out, blsaw->freq);

    /* 0 cabget 100 2000 scale butlp */

    pw_stack_push_buffer(stack, buf);
    pw_stack_pop(stack, NULL);

    scale = mk_scale(patch, sp);
    pw_cable_connect(lfo, scale->in);
    pw_cable_set_value(scale->min, 100);
    pw_cable_set_value(scale->max, 2000);


    /* butlp */

    pw_stack_pop(stack, NULL);
    pw_stack_pop(stack, NULL);
    butlp = mk_butlp(patch, sp);
    pw_cable_connect(scale->out, butlp->p_freq);
    pw_cable_connect(blsaw->out, butlp->in);

    /* bdup */

    pw_stack_dup(stack);

    /* bdup 0.97 10000 revsc */
    pw_stack_dup(stack);
    pw_stack_pop(stack, NULL);
    pw_stack_pop(stack, NULL);
    revsc = mk_revsc(patch, sp);
    pw_cable_connect(butlp->out, revsc->in[1]);
    pw_cable_connect(butlp->out, revsc->in[0]);
    pw_cable_set_constant(revsc->feedback, 0.97);
    pw_cable_set_constant(revsc->lpfreq, 10000);

    /* bdrop */

    pw_stack_pop(stack, NULL);

    /* 0.1 mul */

    pw_stack_pop(stack, NULL);
    mul = mk_mul(patch, sp);
    pw_cable_set_constant(mul->in1, 0.1);
    pw_cable_connect(revsc->out[0], mul->in2);

    /* dcblock */
    pw_stack_pop(stack, NULL);
    dcblock = mk_dcblock(patch, sp);
    pw_cable_connect(mul->out, dcblock->in);

    /* add */

    pw_stack_pop(stack, NULL);
    add = mk_add(patch, sp);
    pw_cable_connect(butlp->out, add->in1);
    pw_cable_connect(dcblock->out, add->in2);

    /* 0 cabget bunhold */

    pw_patch_bunhold(patch, buf);

    /* siren.wav wavout bdrop */

    pw_stack_pop(stack, NULL);
    wavout = mk_wavout(patch, sp, "siren.wav");
    pw_cable_connect(add->out, wavout->in);
    pw_stack_pop(stack, NULL);

    /* sr 10 * _compute rep */

    for (n = 0; n < 10 * SR; n++) {
        pw_patch_tick(patch);
    }

    /* cleanup */

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