~pbatch/patchwerk

patchwerk/node.w -rw-r--r-- 8.5 KiB
9c265356 — paul plan9 additions from Sigrid 3 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
@** Node.
\mkref{node} A node is the atomic element in the audio signal graph, capable
of reading and writing audio-rate signals.

@<Top@>+=
@<Node Top@>

@* Data. The C-Struct for the node is called |pw_node|. It is implemented to
be an opaque data structure.

@<Node Top@>+=
struct pw_node {
@<Node Data@>
};

@ It is assumed that the node is created using the |pw_patch| interface,
so the address of that top-level struct is saved.

@<Node Data@>+=
pw_patch *patch;

@ Each node is assigned a unique ID based on when they were created. nodes with
lower id numbers can connect to and control nodes with higher id numbers. This
design constraint greatly simplifies the creation of the graph and avoids
having to do back-tracing.
@<Node Data@>+=
int id;

@ The |setup| callback function is called to allocate and initialize any memory.
@<Node Data@>+=
pw_function setup;

@ The |destroy| function is called at the end of the program. It is meant
to free any memory allocated {\it setup}.
@<Node Data@>+=
pw_function destroy;

@ The |compute| callback function is called to compute a block of samples.
@<Node Data@>+=
pw_function compute;

@ DSP algorithms often will need some sort of internal memory. This memory
is stored in generic pointer called |ud|.
@<Node Data@>+=
void *ud;

@ A node can have an arbitrary number of connector cables for input/output.
@<Node Data@>+=
pw_cable *cables;
int ncables;

@ The block size is also stored. This is used to initialize audio-rate cables.
@<Node Data@>+=
int blksize;

@ A node can also have a type id specifier that can be optionally set by
a unit generator.
@<Node Data@>+=
int type;

@ In addition to a type id, a node can have a group id. This is useful when
wanting to distinguish between different sets of nodes.

@<Node Data@>+=
int group;

@ Nodes hold a pointer to the next sequential node so that they can be used
in a linked list.
@<Node Data@>+=
pw_node *next;

@* Functions.

@ The function |pw_node_size| returns the size of the opaque data struct
|pw_node|.
@<Header@>+=
size_t pw_node_size();

@ @<Node Top@>+=
size_t pw_node_size()
{
    return sizeof(pw_node);
}

@ The function |pw_node_init| zeros out an initializes the
|pw_node| structure.
By default, the compute + setup callbacks are set to be an
empty callback. The destroy callback by default will call
upon |pw_node_cables_free|, as this is nearly always needed
to be called here anyways.

@<Header@>+=
void pw_node_init(pw_node *node, int blksize);


@ @<Node Top@>+=
static void empty(pw_node *node) { }
static void free_cables(pw_node *node)
{
    pw_node_cables_free(node);
}
void pw_node_init(pw_node *node, int blksize)
{
    node->id = -1;
    node->setup = empty;
    node->compute = empty;
    node->destroy = free_cables;
    node->ncables = 0;
    node->blksize = blksize;
    node->type = -1;
    node->group = -1;
    node->next = NULL;
}

@ The function |pw_node_get_id| returns the node id.
@<Header@>+=
int pw_node_get_id(pw_node *node);

@ @<Node Top@>+=
int pw_node_get_id(pw_node *node)
{
    return node->id;
}

@ The function |pw_node_set_id| sets the node id.
@<Header@>+=
void pw_node_set_id(pw_node *node, int id);

@ @<Node Top@>+=
void pw_node_set_id(pw_node *node, int id)
{
    node->id = id;
}

@ The following functions set the callbacks for the setup, compute, and
destroy functions.

@<Header@>+=
void pw_node_set_setup(pw_node *node, pw_function fun);
void pw_node_set_compute(pw_node *node, pw_function fun);
void pw_node_set_destroy(pw_node *node, pw_function fun);


@ @<Node Top@>+=
void pw_node_set_setup(pw_node *node, pw_function fun)
{
    node->setup = fun;
}

void pw_node_set_compute(pw_node *node, pw_function fun)
{
    node->compute = fun;
}

void pw_node_set_destroy(pw_node *node, pw_function fun)
{
    node->destroy = fun;
}

@ The following functions are wrappers around the internal callback functions
for setup, compute, and destroy.

@<Header@>+=
void pw_node_setup(pw_node *node);
void pw_node_compute(pw_node *node);
void pw_node_destroy(pw_node *node);

@ @<Node Top@>+=
void pw_node_setup(pw_node *node)
{
    node->setup(node);
}

void pw_node_compute(pw_node *node)
{
    node->compute(node);
}

void pw_node_destroy(pw_node *node)
{
    node->destroy(node);
}

@ User data for a given node can be set and retrieved with the wrapper
functions |pw_node_set_data| and |pw_node_get_data|.

@<Header@>+=
void pw_node_set_data(pw_node *node, void *data);
void* pw_node_get_data(pw_node *node);

@ @<Node Top@>+=
void pw_node_set_data(pw_node *node, void *data)
{
    node->ud = data;
}

void* pw_node_get_data(pw_node *node)
{
    return node->ud;
}

@ Nodes con contain an arbitrary number of patch cables, which must
be preallocated with |pw_node_cables_alloc|
@<Header@>+=
int pw_node_cables_alloc(pw_node *node, int ncables);

@ @<Node Top@>+=
int pw_node_cables_alloc(pw_node *node, int ncables)
{
    int n;
    int rc;
    if(node->patch == NULL) return PW_NOT_OK;
    rc = pw_memory_alloc(node->patch,
        sizeof(pw_cable) * ncables,
        (void **)&node->cables);
    if(rc != PW_OK) return rc;
    node->ncables = ncables;
    for(n = 0; n < ncables; n++) {
        pw_cable_init(node, &node->cables[n]);
    }

    return PW_OK;
}

@ Nodes allocated with |pw_node_cables_alloc| must be freed with
|pw_node_cables_free|.
@<Header@>+=
int pw_node_cables_free(pw_node *node);

@ @<Node Top@>+=
int pw_node_cables_free(pw_node *node)
{
    int n;
    if(node->patch == NULL) return PW_NOT_OK;
    if(node->ncables > 0) {
        for(n = 0; n < node->ncables; n++) {
            pw_cable_free(&node->cables[n]);
        }
        return pw_memory_free(node->patch, (void **)&node->cables);
    }

    return PW_NOT_OK;
}

@ A |pw_cable| can be retrieved from the opaque |pw_node| struct using
|pw_node_get_cable|. On success, the function returns |PW_OK|,
and |PW_INVALID_CABLE| for failure.
@<Header@>+=
int pw_node_get_cable(pw_node *node, int id, pw_cable **cable);

@ @<Node Top@>+=
int pw_node_get_cable(pw_node *node, int id, pw_cable **cable)
{
    if(id >= node->ncables) {
        return PW_INVALID_CABLE; /* fail on out-of-range cable ID */
    }

    *cable = &node->cables[id];

    return PW_OK;
}

@ The function |pw_node_blksize| gets the block size of the cables.
@<Header@>+=
int pw_node_blksize(pw_node *node);

@ @<Node Top@>+=
int pw_node_blksize(pw_node *node)
{
    return node->blksize;
}

@ The function |pw_node_set_block| sets a cable internal to the node to be
an audio signal using the node's internal block size.
It returns |PW_OK| on sucess, and |PW_INVALID_CABLE| on failure.

@<Header@>+=
int pw_node_set_block(pw_node *node, int id);

@ This error code rarely gets checked in practice. Many have
considered this a bad idea. A global error flag is set
in place to attempt patchwerk from moving further down
the line.

@<Node Top@>+=
int pw_node_set_block(pw_node *node, int id)
{
    int rc;
    if(id > node->ncables) {
        return PW_INVALID_CABLE; /* fail on out-of-range cable ID */
    }

    rc = pw_cable_make_block(
        &node->cables[id],
        pw_patch_stack(node->patch),
        node->blksize);

    if (rc != PW_OK) {
        pw_patch_err(node->patch, PW_POOL_FULL);
    }

    return rc;
}

@ The function |pw_node_get_ncables| returns the number of cables in a node.
@<Header@>+=
int pw_node_get_ncables(pw_node *node);

@ @<Node Top@>+=
int pw_node_get_ncables(pw_node *node)
{
    return node->ncables;
}

@ The functions |pw_node_set_type| and |pw_node_get_type| set and get
the type ID of a particular node.
@<Header@>+=
int pw_node_get_type(pw_node *node);
void pw_node_set_type(pw_node *node, int type);

@ @<Node Top@>+=
int pw_node_get_type(pw_node *node)
{
    return node->type;
}

void pw_node_set_type(pw_node *node, int type)
{
    node->type = type;
}

@ A node is typically created using the |pw_patch| interface. This function
explicitely stores the pointer location of |pw_patch| inside of
a |pw_node|. When using the |pw_patch| abstraction, this function is called
automatically.
@<Header@>+=
void pw_node_set_patch(pw_node *node, pw_patch *patch);
int pw_node_get_patch(pw_node *node, pw_patch **patch);

@ @<Node Top@>+=
void pw_node_set_patch(pw_node *node, pw_patch *patch)
{
    node->patch = patch;
}

int pw_node_get_patch(pw_node *node, pw_patch **patch)
{
    *patch = node->patch;
    return PW_OK;
}

@ The function |pw_node_get_next| and |pw_node_set_next| both
get and set the next |pw_node| entry.
@<Header@>+=
pw_node * pw_node_get_next(pw_node *node);
void pw_node_set_next(pw_node *node, pw_node *next);

@ @<Node Top@>+=
pw_node * pw_node_get_next(pw_node *node)
{
    return node->next;
}

void pw_node_set_next(pw_node *node, pw_node *next)
{
    node->next = next;
}