~pbatch/patchwerk

patchwerk/cable.w -rw-r--r-- 11.6 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
@** Cable.
\mkref{cable}
The cable is the primary means of sharing signals between modules.
The cabling system takes advantage of C pointers to create implicit
connections, which is very computationally efficient.

@<Top@>+=
@<Cable Enums@>@/
@<Cable Top@>

@* Data. The c-structure for the cable is contained in a typedef called
|pw_cable|.

@<Cable Data@>=
struct pw_cable {
@<Variables in Cable Data@>@/
};

@ The |pw_cable| forward declartion is defined in the header file
|@(patchwerk.h@>|.

@<Type Declarations@>+=
typedef struct pw_cable pw_cable;

@ A |pw_cable| is assumed to be created using a node. A copy of this node
is stored inside of the data structure.
@<Variables in Cable Data@>+=
pw_node *node;

@ The output of a cable is stored in the pointer |val|. By default, it points
to the internal variable |ival|. This is a constant value. This pointer can
be reset to point to another memory address.
@<Variables in Cable Data@>+=
PWFLT *val;
PWFLT ival;

@ It is a common paradigm in audio signal process to write DSP routines that
process blocks rather than single samples. Every cable has an internal block
of memory that must explicitely be allocated, with the size of the block stored
as an integer. This block size should be a system-wide size set by the
|pw_patch| struct. Any non-zero block size will be an indicator that memory
for the block has been allocated, and must therefore be freed.

@<Variables in Cable Data@>+=
PWFLT *blk;
int blksize;

@ The internal type of a cable keeps track of if a memory pointer is
overridden or not. It is set to be one of the macros defined in
|@<Cable Enums@>|.
@<Variables in Cable Data@>+=
unsigned char type;

@ The following enum contains the various states a cable can be in.
|CABLE_IVAL| is the default value, meaning the pointer is set to be
the internal constant value. |CABLE_BLOCK| indicates that the cable
is set to point to the internally allocated audio block. |CABLE_OVERRIDE|
indicates that the cable has been overridden by another block. The assumption
is that the other block is set to be a |CABLE_BLOCK|.

@<Cable Enums@>=
enum {
CABLE_IVAL,
CABLE_BLOCK
};

@ A |pw_cable| pointer is saved internally. By default, this is set to itself.
Whenever a cable connection happens, the connecting cable gets stored here.
This can be used for back tracing the graph.
@<Variables in Cable Data@>+=
pw_cable *pcable;

@ Patchwerk uses an internal buffer interface known as a |pw_buffer| to
store audio-rate blocks. This buffer is a low-level component to
patchwerk's memory-efficient interfaces for handling audio buffers.

More information can be found at |@<A Single Buffer@>| and
|@<The Buffer Pool@>|.

@<Variables in Cable Data@>+=
pw_buffer *buf;

@* Functions.

@ The function |pw_cable_init| initializes and zeros out variables. No
memory allocation is done here. If a cable is not using a node, the |node|
parameter can be set to be {\tt NULL}.
@<Header@>+=
void pw_cable_init(pw_node *node, pw_cable *cable);

@ @<Cable Top@>+=
void pw_cable_init(pw_node *node, pw_cable *cable)
{
    cable->ival = 0;
    cable->blksize = 0;
    cable->blk = NULL;
    cable->type = CABLE_IVAL;
    cable->val = &cable->ival;
    cable->node = node;
    cable->pcable = cable;
    cable->buf = NULL;
}

@ The function |pw_cable_free| frees any allocated memory blocks. If
the block size is nonzero, it assumes that memory has been previously
allocated and frees it.

@<Header@>+=
void pw_cable_free(pw_cable *cable);

@ @<Cable Top@>+=
void pw_cable_free(pw_cable *cable)
{
    if(cable->blksize != 0) {
        /* free(cable->blk); */
    }
}

@ The function |pw_cable_set_block| sets an internal block of memory
for block-based processing. The block must be allocated beforehand.
@<Cable Top@>+=
void pw_cable_set_block(pw_cable *cable, PWFLT *blk, int blksize)
{
    cable->blk = blk;
    cable->blksize = blksize;
    cable->val = cable->blk;
    cable->type = CABLE_BLOCK;
}

@ @<Header@>+=
void pw_cable_set_block(pw_cable *cable, PWFLT *blk, int blksize);

@ The function |pw_cable_set_constant| sets the internal constant value.
@<Header@>+=
void pw_cable_set_constant(pw_cable *cable, PWFLT val);

@ @<Cable Top@>+=
void pw_cable_set_constant(pw_cable *cable, PWFLT val)
{
    cable->val = &cable->ival;
    cable->ival = val;
    cable->type = CABLE_IVAL;
}

@ The function |pw_cable_set_value| sets the value of the cable to a
variable. Unlike |pw_cable_set_constant|, this function does not override
the pointer value. If a cable value has been overridden with another
pointer, it will set that value.

@<Header@>+=
void pw_cable_set_value(pw_cable *c, PWFLT val);

@ @<Cable Top@>+=
void pw_cable_set_value(pw_cable *c, PWFLT val)
{
    *c->val = val;
}

@ The function |pw_cable_get| retrieves a value from a cable at a given
index position |pos|. If the cable type is a constant value |CABLE_IVAL|,
the index position is discarded.

@<Header@>+=
PWFLT pw_cable_get(pw_cable *cable, int pos);

@ @<Cable Top@>+=
PWFLT pw_cable_get(pw_cable *cable, int pos)
{
    if(cable->type == CABLE_IVAL) {
        return *cable->val;
    } else {
        return cable->val[pos];
    }
}

@ @<Header@>+=
void pw_cable_set(pw_cable *cable, int pos, PWFLT val);

@ @<Cable Top@>+=
void pw_cable_set(pw_cable *cable, int pos, PWFLT val)
{
    if(cable->type == CABLE_IVAL) {
        *cable->val = val;
    } else {
        cable->val[pos] = val;
    }
}


@ The function |pw_cable_connect| connects the output of cable |c1| to
cable |c2|. In order for a cable to connect to another cable, the node
id must be less. If it isn't, it returns the error code
|PW_CONNECTION_MISMATCH|

The pointer value and type entries of |c2| are set to be whatever the |c1|
values are.

@<Header@>+=
int pw_cable_connect(pw_cable *c1, pw_cable *c2);

@ @<Cable Top@>+=
int pw_cable_connect(pw_cable *c1, pw_cable *c2)
{
    int id1, id2;

    id1 = pw_node_get_id(c1->node);
    id2 = pw_node_get_id(c2->node);

    if(id1 > id2) return PW_CONNECTION_MISMATCH;

    pw_cable_connect_nocheck(c1, c2);

    return PW_OK;
}

@ The function |pw_cable_connect_nocheck| connects the output of cable |c1|
to the |c2|. This cable does not check for cable ID numbers, so it is useful
for floating cables not connected to a node.

@<Header@>+=
void pw_cable_connect_nocheck(pw_cable *c1, pw_cable *c2);


@ @<Cable Top@>+=
void pw_cable_connect_nocheck(pw_cable *c1, pw_cable *c2)
{
    c2->type = c1->type;
    pw_cable_override(c1, c2);
}

@ The function |pw_cable_pop| indirectly pops a buffer from the buffer
stack. This function was written specifically to work with the
Runt interface. It assumed that buffer being popped is the buffer
belonging to this specific cable, but some error checking is done.
If the cable is not a block cable, the
function will return |PW_NOT_OK|. Otherwise, it will return |PW_OK|.

@<Cable Top@>+=

int pw_cable_pop(pw_cable *cab)
{
    pw_stack *stack;
    pw_node *node;
    pw_buffer *tmp;

    if(!pw_cable_is_block(cab)) return PW_NOT_OK;

    node = cab->node;
    stack = pw_patch_stack(node->patch);
    pw_stack_pop(stack, &tmp);

    return PW_OK;
}

@ @<Header@> += int pw_cable_pop(pw_cable *cab);

@
@<Cable Top@>+=

void pw_cable_push(pw_cable *cab)
{
    pw_stack *stack;
    pw_node *node;
    pw_buffer *tmp;
    tmp = NULL;

    node = cab->node;
    stack = pw_patch_stack(node->patch);
    pw_stack_push(stack, &tmp);
}

@ @<Header@> += void pw_cable_push(pw_cable *cab);

@ The function |pw_cable_is_block| checks if the cable type is a block not.
This a direct boolean comparison, so the function will return 1 on true, and
0 on false.

@<Cable Top@>+=
int pw_cable_is_block(pw_cable *cab)
{
    return cab->type == CABLE_BLOCK;
}

@ @<Header@>+=
int pw_cable_is_block(pw_cable *cab);

@ The function |pw_cable_is_constant| checks if the cable type is a constant.
This a direct boolean comparison, so the function will return 1 on true, and
0 on false.
@<Cable Top@>+=
int pw_cable_is_constant(pw_cable *cab)
{
    return cab->type == CABLE_IVAL;
}

@ @<Header@>+=
int pw_cable_is_constant(pw_cable *cab);

@ The function |pw_cable_get_buffer| and |pw_cable_set_buffer| get and
set the buffer value inside of a cable.

{\bf Note:} This function does not do type checking and assumes the cable
is block. If the cable is not a block, this buffer value may return null.

@<Cable Top@>+=
pw_buffer * pw_cable_get_buffer(pw_cable *cab)
{
    return cab->buf;
}

void pw_cable_set_buffer(pw_cable *cab, pw_buffer *buf)
{
    cab->buf = buf;
}

@ @<Header@>+=
pw_buffer * pw_cable_get_buffer(pw_cable *cab);
void pw_cable_set_buffer(pw_cable *cab, pw_buffer *buf);

@ The function |pw_cable_make_block| will set up a block cable using a
buffer stack. Block size must also be given here. This code has been
refactored from |pw_node_set_block| in order to make it easier to build
standalone cables for things like sends and throws. Block size will have to
be specified here. This can be retrieved with |pw_node_blksize| or
|pw_patch_blksize|.

On success, |pw_cable_make_block| will return |PW_OK|. Otherwise, it will
return |PW_NOT_OK|.

% TODO: add more specific error handling

@<Cable Top@>+=
int pw_cable_make_block(pw_cable *cable, pw_stack *stack, int blksize)
{
    pw_buffer *buf;
    PWFLT *blk;

    buf = NULL;

    if(pw_stack_push(stack, &buf) != PW_OK) {
        return PW_NOT_OK;
    }

    blk = pw_buffer_data(buf);
    pw_cable_set_block(cable, blk, blksize);
    pw_cable_set_buffer(cable, buf);

    return PW_OK;
}

@ @<Header@>+=
int pw_cable_make_block(pw_cable *cable, pw_stack *stack, int blksize);

@ The function |pw_cable_clear| zeros out a block cable. If the cable is
not a block cable, it will return |PW_NOT_OK|.

@<Cable Top@>+=
int pw_cable_clear(pw_cable *cab)
{
    int i;
    if(!pw_cable_is_block(cab)) return PW_NOT_OK;

    for(i = 0; i < cab->blksize; i++) {
        cab->val[i] = 0;
    }

    return PW_OK;
}

@ @<Header@>+=
int pw_cable_clear(pw_cable *cab);

@ The function |pw_cable_mix| will mix the signal contained in the
block cable |in| to the block cable |sum| by some scaling amount |mix|.

@<Cable Top@>+=
int pw_cable_mix(pw_cable *in, pw_cable *sum, PWFLT mix)
{
    int i;
    if(!pw_cable_is_block(in) || !pw_cable_is_block(sum)) return PW_NOT_OK;

    for(i = 0; i < sum->blksize; i++) {
        sum->val[i] += in->val[i]*mix;
    }

    return PW_OK;
}

@ @<Header@>+=
int pw_cable_mix(pw_cable *in, pw_cable *sum, PWFLT mix);

@ The function |pw_cable_blksize| returns the block size of the cable.
@<Cable Top@>+=

int pw_cable_blksize(pw_cable *cable)
{
    return cable->blksize;
}

@ @<Header@>+=
int pw_cable_blksize(pw_cable *cable);

@ The function |pw_cable_override| overrides the value pointer of cable |c2|
to be the value of cable |c1|, without setting the type flag. This
can be thought of as a more low-level component of |pw_cable_connect_nocheck|.

@ @<Header@>+=
void pw_cable_override(pw_cable *c1, pw_cable *c2);

@ Note below that block size value is copied alongside the other values. This
is a delibrate choice. Dummy cables are cables initialized without knowing any
global state information, and meant to be overriden by other cables.
By default, cables are set to a block size of 0. Having the block size set to
an incorrect value make |pw_cable_clear| work incorrectly, if at all.

@<Cable Top@>+=
void pw_cable_override(pw_cable *c1, pw_cable *c2)
{
    c2->val = c1->val;
    c2->pcable = c1;
    c2->blksize = c1->blksize;
}

@ The function |pw_cable_copy| copies the contents of one
cable (c1) to another cable (c2).

@ @<Header@>+=
void pw_cable_copy(pw_cable *c1, pw_cable *c2);

@ @<Cable Top@>+=
void pw_cable_copy(pw_cable *c1, pw_cable *c2)
{
    int blksize;
    int n;
    PWFLT tmp;

    blksize = c1->blksize;

    for(n = 0; n < blksize; n++) {
        tmp = pw_cable_get(c1, n);
        pw_cable_set(c2, n, tmp);
    }
}