~pbatch/patchwerk

patchwerk/examples/sinosc.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
#include <stdlib.h>
#include <soundpipe.h>
#include "patchwerk.h"
#include "sinosc.h"

static void compute(pw_node *node)
{
    int i;
    int blksize;
    SPFLOAT tmp;
    sinosc_d *sinosc;

    sinosc = pw_node_get_data(node);
    blksize = pw_node_blksize(node); 
    for(i = 0; i < blksize; i++) {
        sinosc->osc->freq = pw_cable_get(sinosc->freq, i);
        sinosc->osc->amp = pw_cable_get(sinosc->amp, i);
        sp_osc_compute(sinosc->sp, sinosc->osc, NULL, &tmp);
        pw_cable_set(sinosc->out, i, tmp);
    }
}

static void destroy(pw_node *node)
{
    sinosc_d *sinosc;

    sinosc = (sinosc_d *) pw_node_get_data(node);

    sp_osc_destroy(&sinosc->osc);
    sp_ftbl_destroy(&sinosc->ft);

    pw_node_cables_free(node);

    free(sinosc);
}


void node_sinosc(sp_data *sp, pw_node *node, SPFLOAT *blk)
{
    sinosc_d *sinosc;

    sinosc = malloc(sizeof(sinosc_d));
    sinosc->sp = sp;

    sp_ftbl_create(sp, &sinosc->ft, 8192);
    sp_gen_sine(sp, sinosc->ft);
    sp_osc_create(&sinosc->osc);
    sp_osc_init(sp, sinosc->osc, sinosc->ft, 0.f);

    pw_node_cables_alloc(node, 3);
    pw_node_get_cable(node, 0, &sinosc->freq);
    pw_node_get_cable(node, 1, &sinosc->amp);
    pw_node_get_cable(node, 2, &sinosc->out);
    pw_cable_set_block(sinosc->out, blk, pw_node_blksize(node));
    
    pw_cable_set_constant(sinosc->freq, 480);
    pw_cable_set_constant(sinosc->amp, 0.3);

    pw_node_set_type(node, 0);
    pw_node_set_data(node, sinosc);

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

void node_sinosc_v2(sp_data *sp, pw_node *node)
{
    sinosc_d *sinosc;

    sinosc = malloc(sizeof(sinosc_d));
    sinosc->sp = sp;

    sp_ftbl_create(sp, &sinosc->ft, 8192);
    sp_gen_sine(sp, sinosc->ft);
    sp_osc_create(&sinosc->osc);
    sp_osc_init(sp, sinosc->osc, sinosc->ft, 0.f);

    pw_node_cables_alloc(node, 3);
    pw_node_get_cable(node, 0, &sinosc->freq);
    pw_node_get_cable(node, 1, &sinosc->amp);
    pw_node_get_cable(node, 2, &sinosc->out);

    /* use internal buffer stack here */
    pw_node_set_block(node, 2);
    
    pw_cable_set_constant(sinosc->freq, 480);
    pw_cable_set_constant(sinosc->amp, 0.3);

    pw_node_set_type(node, 0);
    pw_node_set_data(node, sinosc);

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