~pbatch/patchwerk

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

static void compute(pw_node *node)
{
    int i;
    int blksize;
    SPFLOAT tmp;
    SPFLOAT in;
    scaler_d *scaler;

    scaler = pw_node_get_data(node);
    blksize = pw_node_blksize(node);
    
    for(i = 0; i < blksize; i++) {
        scaler->scaler->min = pw_cable_get(scaler->min, i);
        scaler->scaler->max = pw_cable_get(scaler->max, i);
        in = pw_cable_get(scaler->in, i);
        sp_biscale_compute(scaler->sp, scaler->scaler, &in, &tmp);
        pw_cable_set(scaler->out, i, tmp);
    }
}

static void destroy(pw_node *node)
{
    scaler_d *scaler;

    scaler = (scaler_d *) pw_node_get_data(node);

    sp_biscale_destroy(&scaler->scaler);

    pw_node_cables_free(node);
    free(scaler);
}

void node_scaler(sp_data *sp, pw_node *node, SPFLOAT *blk)
{
    scaler_d *scaler;

    sp = (sp_data *)pw_node_get_data(node);

    scaler = malloc(sizeof(scaler_d));
    scaler->sp = sp;

    sp_biscale_create(&scaler->scaler);
    sp_biscale_init(sp, scaler->scaler);

    pw_node_cables_alloc(node, 4);
    pw_node_get_cable(node, 0, &scaler->in);
    pw_node_get_cable(node, 1, &scaler->min);
    pw_node_get_cable(node, 2, &scaler->max);
    pw_node_get_cable(node, 3, &scaler->out);
    pw_cable_set_block(scaler->out, blk, pw_node_blksize(node));

    pw_cable_set_constant(scaler->min, 0);
    pw_cable_set_constant(scaler->max, 1);
    pw_cable_set_constant(scaler->in, 0);

    pw_node_set_type(node, TYPE_BISCALE);
    pw_node_set_data(node, scaler);

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

void node_scaler_v2(sp_data *sp, pw_node *node)
{
    scaler_d *scaler;

    sp = (sp_data *)pw_node_get_data(node);

    scaler = malloc(sizeof(scaler_d));
    scaler->sp = sp;

    sp_biscale_create(&scaler->scaler);
    sp_biscale_init(sp, scaler->scaler);

    pw_node_cables_alloc(node, 4);
    pw_node_get_cable(node, 0, &scaler->in);
    pw_node_get_cable(node, 1, &scaler->min);
    pw_node_get_cable(node, 2, &scaler->max);
    pw_node_get_cable(node, 3, &scaler->out);
    pw_node_set_block(node, 3);

    pw_cable_set_constant(scaler->min, 0);
    pw_cable_set_constant(scaler->max, 1);
    pw_cable_set_constant(scaler->in, 0);

    pw_node_set_type(node, TYPE_BISCALE);
    pw_node_set_data(node, scaler);

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