~pbatch/patchwerk

patchwerk/examples/noisy.c -rw-r--r-- 1015 bytes
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
#include <stdlib.h>
#include "patchwerk.h"
#include "noisy.h"

static void destroy(pw_node *node)
{
    noisy_d *noisy;
    noisy = (noisy_d *) pw_node_get_data(node);
    pw_node_cables_free(node);
    free(noisy);
}

static void compute(pw_node *node)
{
    int i;
    int blksize;
    PWFLT amp;
    PWFLT samp;
    noisy_d *noisy;

    noisy = pw_node_get_data(node);
    blksize = pw_node_blksize(node);
    
    for(i = 0; i < blksize; i++) {
        amp = pw_cable_get(noisy->amp, i);
        samp = ((2*((PWFLT)rand()/RAND_MAX))-1) * amp;
        pw_cable_set(noisy->out, i, samp);
    }
}

void node_noisy(pw_node *node)
{
    noisy_d *noise;
    
    noise = malloc(sizeof(noisy_d));

    pw_node_cables_alloc(node, 2);
    pw_node_get_cable(node, 0, &noise->amp);
    pw_node_get_cable(node, 1, &noise->out);
    pw_node_set_block(node, 1);

    pw_cable_set_constant(noise->amp, 0.3);

    pw_node_set_compute(node, compute);
    pw_node_set_destroy(node, destroy);
    pw_node_set_data(node, noise);
}