~pbatch/patchwerk

ref: 55a5d76dd62d57f855d26ef30deff11a5ee31fa1 patchwerk/runt/runt_patchwerk.c -rw-r--r-- 12.4 KiB
55a5d76d — paul re-enabled pdhalf 1 year, 1 month 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#include <stdio.h>
#include <runt.h>
#include <soundpipe.h>
#include <string.h>
#include "patchwerk.h"
#include "runt_patchwerk.h"
#include "ugen_enums.h"
#include "runt_ftbl.h"

#define BLKSIZE 64

static pw_cable *ocable;
static pw_subpatch subpatch;
static int rpw_is_loaded;

#ifndef NO_LIBSNDFILE
static int pos = 0;
static void process(sp_data *sp, void *ptr)
{
    SPFLOAT out;

    if(pos == 0) {
        pw_subpatch_compute(&subpatch);
    }

    out = pw_cable_get(ocable, pos);
    pos = (pos + 1) % BLKSIZE;
    sp_out(sp, 0, out);
}
#endif

static void patch_cleanup(pw_patch *patch)
{
    pw_patch_destroy(patch);
    pw_patch_free_nodes(patch);
}

static runt_int rproc_patch_cleanup(runt_vm *vm, runt_ptr p)
{
    pw_patch *patch;
    sp_data *sp;

    patch = rpw_get_patch(p);

    sp = pw_patch_data_get(patch);

    patch_cleanup(patch);

    sp_destroy(&sp);

    return RUNT_OK;
}

static runt_int rproc_compute(runt_vm *vm, runt_ptr p)
{
    pw_patch *patch;

    patch = rpw_get_patch(p);
    pw_patch_tick(patch);
    return RUNT_OK;
}

static runt_int rproc_write_wav(runt_vm *vm, runt_ptr p)
{
    pw_patch *patch;
#ifndef NO_LIBSNDFILE
    sp_data *sp;
#endif

    patch = rpw_get_patch(p);
    pw_subpatch_save(patch, &subpatch);
    ocable = pw_subpatch_out(&subpatch);
#ifndef NO_LIBSNDFILE
    sp = pw_patch_data_get(patch);
    sp_process(sp, patch, process);
#endif
    return RUNT_OK;
}

static runt_int rproc_set_dur(runt_vm *vm, runt_ptr p)
{
    pw_patch *patch;
    sp_data *sp;
    runt_float dur;
    runt_int rc;
    runt_stacklet *s;

    rc = runt_ppop(vm, &s);
    RUNT_ERROR_CHECK(rc);
    dur = s->f;

    patch = rpw_get_patch(p);
    sp = pw_patch_data_get(patch);
    sp->len = dur * sp->sr;
    return RUNT_OK;
}

static runt_int rproc_filename(runt_vm *vm, runt_ptr p)
{
    pw_patch *patch;
    sp_data *sp;
    const char *str;
    runt_int rc;
    runt_stacklet *s;

    rc = runt_ppop(vm, &s);
    RUNT_ERROR_CHECK(rc);
    str = runt_to_string(s->p);

    patch = rpw_get_patch(p);
    sp = pw_patch_data_get(patch);
    strncpy(sp->filename, str, 60);
    return RUNT_OK;
}

static runt_int rproc_new_node(runt_vm *vm, runt_ptr p)
{
    runt_int rc;
    runt_stacklet *s;
    pw_node *node;
    pw_patch *patch;

    patch = rpw_get_patch(p);
    rc = pw_patch_new_node(patch, &node);
    PW_RUNT_ERROR_CHECK(rc);

    rc = runt_ppush(vm, &s);
    RUNT_ERROR_CHECK(rc);
    s->p = runt_mk_cptr(vm, node);
    s->t = RUNT_END + TYPE_NODE;

    return RUNT_OK;
}

void patchwerk_word_define(runt_vm *vm, runt_ptr p,
    const char *str,
    runt_uint size,
    runt_proc proc)
{
    runt_cell *cell;
    runt_keyword_define(vm, str, size, proc, &cell);
    cell->p = p;
}

void patchwerk_word_define_with_copy(runt_vm *vm, runt_ptr p,
    const char *str,
    runt_uint size,
    runt_proc proc,
    runt_copy_proc copy)
{
    runt_cell *cell;
    runt_keyword_define_with_copy(vm, str, size, proc, copy, &cell);
    cell->p = p;
}

runt_int rpw_get_param(runt_vm *vm, rpw_param *par)
{
    runt_stacklet *s;
    runt_int rc;

    rc = runt_ppop(vm, &s);
    RUNT_ERROR_CHECK(rc);
    if(s->t == RUNT_FLOAT) {
        par->type = 0;
        par->data.f = s->f;
    } else if(s->t == RUNT_END + TYPE_CABLE) {
        par->type = 1;
        par->data.c = runt_to_cptr(s->p);
        pw_cable_pop(par->data.c);
    } else {
        runt_print(vm, "This doesn't seem like a valid cable.\n");
        return RUNT_NOT_OK;
    }

    return RUNT_OK;
}

runt_int rpw_peak_param(runt_vm *vm, rpw_param *par)
{
    runt_stacklet *s;
    runt_int rc;

    rc = runt_ppop(vm, &s);
    RUNT_ERROR_CHECK(rc);
    if(s->t == RUNT_FLOAT) {
        par->type = 0;
        par->data.f = s->f;
    } else if(s->t == RUNT_END + TYPE_CABLE) {
        par->type = 1;
        par->data.c = runt_to_cptr(s->p);
    } else {
        runt_print(vm, "This doesn't seem like a valid cable.\n");
        return RUNT_NOT_OK;
    }

    return RUNT_OK;
}

runt_int rpw_set_param(
    runt_vm *vm,
    pw_node *node,
    rpw_param *par,
    runt_int cable_id
)
{
    pw_cable *cable;
    runt_int rc;
    pw_node_get_cable(node, cable_id, &cable);
    if(par->type == 0) {
        pw_cable_set_value(cable, par->data.f);
    } else {
        rc = pw_cable_connect(par->data.c, cable);
        PW_RUNT_ERROR_CHECK(rc);
    }
    return RUNT_OK;
}

runt_int rpw_override_param(
    runt_vm *vm,
    pw_node *node,
    rpw_param *par,
    runt_int cable_id
)
{
    pw_cable *cable;
    pw_node_get_cable(node, cable_id, &cable);
    if(par->type == 0) {
        pw_cable_set_value(cable, par->data.f);
    } else {
        pw_cable_connect_nocheck(cable, par->data.c);
    }
    return RUNT_OK;
}


runt_int rpw_push_output (
    runt_vm *vm,
    pw_node *node,
    runt_stacklet *s,
    runt_int id
)
{
    pw_cable *cable;
    pw_node_get_cable(node, id, &cable);
    rpw_stacklet_set_cable(vm, s, cable);
    return RUNT_OK;
}

void rpw_stacklet_set_cable(runt_vm *vm,
                            runt_stacklet *s,
                            pw_cable *c)
{
    s->p = runt_mk_cptr(vm, c);
    s->t = TYPE_CABLE + RUNT_END;
}

static runt_int rproc_reset(runt_vm *vm, runt_ptr p)
{
    pw_patch *patch;

    patch = rpw_get_patch(p);
    pw_patch_destroy(patch);
    pw_patch_free_nodes(patch);
    return RUNT_OK;
}

static runt_int rproc_status(runt_vm *vm, runt_ptr p)
{
    pw_stack *stack;
    pw_patch *patch;
    pw_bufferpool *pool;

    patch = rpw_get_patch(p);
    stack = pw_patch_stack(patch);
    pool = pw_patch_pool(patch);
    pw_dump_bufferpool(runt_filehandle_get(vm), pool, 1);
    pw_dump_stack(runt_filehandle_get(vm), stack, 1);
    return RUNT_OK;
}

static runt_int rproc_sr(runt_vm *vm, runt_ptr p)
{
    pw_patch *patch;
    runt_int rc;
    runt_stacklet *s;

    patch = rpw_get_patch(p);

    rc = runt_ppush(vm, &s);
    RUNT_ERROR_CHECK(rc);

    s->f = pw_patch_srate_get(patch);

    return RUNT_OK;
}

static runt_int rproc_set_sr(runt_vm *vm, runt_ptr p)
{
    pw_patch *patch;
    runt_int rc;
    runt_stacklet *s;

    patch = rpw_get_patch(p);

    rc = runt_ppop(vm, &s);
    RUNT_ERROR_CHECK(rc);

    pw_patch_srate_set(patch, s->f);

    return RUNT_OK;
}

static int counter = 0;

static runt_int rproc_cabstep(runt_vm *vm, runt_ptr p)
{
    pw_patch *patch;
    pw_cable *cab;
    int blksize;
    runt_int rc;
    runt_stacklet *s;
    runt_stacklet *out;

    rc = runt_ppop(vm, &s);
    RUNT_ERROR_CHECK(rc);

    cab = runt_to_cptr(s->p);
    patch = rpw_get_patch(p);
    blksize = pw_patch_blksize(patch);

    rc = runt_ppush(vm, &out);

    if(counter == 0) {
        pw_patch_compute(patch);
    }

    out->f = pw_cable_get(cab, counter);
    counter = (counter + 1) % blksize;

    return RUNT_OK;
}

static runt_int rproc_cabdump(runt_vm *vm, runt_ptr p)
{
    rpw_param c;
    runt_int rc;

    rc = rpw_peak_param(vm, &c);
    RUNT_ERROR_CHECK(rc);

    if(!rpw_param_is_cable(&c)) {
        runt_print(vm, "Parameter is not a cable!\n");
        return RUNT_NOT_OK;
    }

    pw_dump_cable(runt_filehandle_get(vm), rpw_param_get_cable(&c));

    return RUNT_OK;
}

static void loader(runt_vm *vm, runt_ptr p)
{
    patchwerk_word_define(vm, p, "set_dur", 7, rproc_set_dur);
    patchwerk_word_define(vm, p, "filename", 8, rproc_filename);
    patchwerk_word_define(vm, p, "write_wav", 9, rproc_write_wav);
    patchwerk_word_define(vm, p, "compute", 7, rproc_compute);
    patchwerk_word_define(vm, p, "new_node", 8, rproc_new_node);
    patchwerk_word_define(vm, p, "nodes", 5, runt_load_nodes);
    patchwerk_word_define(vm, p, "reset", 5, rproc_reset);
    patchwerk_word_define(vm, p, "status", 6, rproc_status);
    patchwerk_word_define(vm, p, "sr", 2, rproc_sr);
    patchwerk_word_define(vm, p, "cabstep", 7, rproc_cabstep);
    patchwerk_word_define(vm, p, "cabdump", 7, rproc_cabdump);
}

static runt_int rproc_patchwerk(runt_vm *vm, runt_ptr p)
{
    pw_patch *patch;
    sp_data *sp;

    if(rpw_is_loaded) {
        runt_print(vm, "Patchwerk is already loaded!\n");
        return RUNT_NOT_OK;
    }

    rpw_is_loaded = 1;
    patch = rpw_get_patch(p);
    pw_patch_alloc(patch, 8, 10);
    sp_create(&sp);
    sp->sr = pw_patch_srate_get(patch);
    pw_patch_data_set(patch, sp);
    loader(vm, p);
    runt_add_destructor(vm, rproc_patch_cleanup, p);
    runt_mark_set(vm);
    return runt_is_alive(vm);
}

static runt_int rproc_patchwerk_noalloc(runt_vm *vm, runt_ptr p)
{
    if(rpw_is_loaded) {
        runt_print(vm, "Patchwerk is already loaded!\n");
        return RUNT_NOT_OK;
    }

    rpw_is_loaded = 1;
    loader(vm, p);
    runt_mark_set(vm);
    return runt_is_alive(vm);
}

runt_ptr runt_load_patchwerk(runt_vm *vm)
{
    runt_ptr p;
    pw_patch *patch;

    rpw_is_loaded = 0;
    runt_malloc(vm, pw_patch_size(), (void **)&patch);
    pw_patch_init(patch, BLKSIZE);
    p = runt_mk_cptr(vm, patch);
    runt_data(vm, "__pw", 4, p);
    patchwerk_word_define(vm, p, "patchwerk", 9, rproc_patchwerk);
    patchwerk_word_define(vm, p, "set_sr", 6, rproc_set_sr);
    return p;
}

runt_ptr runt_load_patchwerk_withdata(runt_vm *vm, pw_patch *patch)
{
    runt_ptr p;
    rpw_is_loaded = 0;
    p = runt_mk_cptr(vm, patch);
    runt_data(vm, "__pw", 4, p);
    patchwerk_word_define(vm, p, "patchwerk", 9, rproc_patchwerk_noalloc);
    return p;
}

runt_int rpw_get_end()
{
    return TYPE_END;
}

runt_int rpw_stack_is_cable(runt_stacklet *s)
{
    return s->t == (RUNT_END + TYPE_CABLE);
}

runt_int rpw_plugin_data(runt_vm *vm, runt_ptr *p)
{
    runt_int rc;
    rc = runt_data_search(vm, "__pw", p);
    if(rc != RUNT_OK) {
        runt_print(vm, "Could not find patchwerk library.\n");
        return RUNT_NOT_OK;
    }
    return RUNT_OK;
}

pw_patch * rpw_get_patch(runt_ptr p)
{
    return (pw_patch *)runt_to_cptr(p);
}

runt_int rpw_param_is_constant(rpw_param *par)
{
    return par->type == 0;
}

runt_int rpw_param_is_cable(rpw_param *par)
{
    return par->type == 1;
}

pw_cable * rpw_param_get_cable(rpw_param *par)
{
    return par->data.c;
}

PWFLT rpw_param_get_constant(rpw_param *par)
{
    return par->data.f;
}

runt_int rpw_ftbl_get(runt_vm *vm, const char *name, runt_ptr *p)
{
    runt_int rc;
    runt_uint size;
    runt_entry *entry;
    runt_cell *c;

    size = strlen(name);

    rc = runt_word_search(vm, name, size, &entry);
    RUNT_ERROR_CHECK(rc);
    rc = runt_cell_pool_get_cell(vm, entry->cell->id + 1, &c);
    RUNT_ERROR_CHECK(rc);
    /* TODO: add type checking */
    *p = c->p;
    return RUNT_OK;
}

runt_int rpw_get_ftable(runt_vm *vm, sp_ftbl **ft)
{
    runt_int rc;
    runt_stacklet *s;

    rc = runt_ppop(vm, &s);
    RUNT_ERROR_CHECK(rc);
    if(s->t != RUNT_END + TYPE_FTBL) {
        runt_print(vm, "Parameter does not seem to be an ftable\n");
        *ft = NULL;
        return RUNT_NOT_OK;
    }
    *ft = runt_to_cptr(s->p);
    return RUNT_OK;
}

static void free_ftable(pw_pointer *p)
{
    sp_ftbl *ft;

    ft = pw_pointer_data(p);
    sp_ftbl_destroy(&ft);
}

void rpw_append_ftable(pw_patch *patch, sp_ftbl *ft)
{
    pw_patch_append_userdata(patch, free_ftable, ft);
}

void rpw_stacklet_ftable(runt_vm *vm, runt_stacklet *s, sp_ftbl *ft)
{
    s->p = runt_mk_cptr(vm, ft);
    s->t = RUNT_END + TYPE_FTBL;
}

static void var_compute(pw_node *node)
{
    int blksize;
    PWFLT *val;
    pw_cable *out;
    int n;
    PWFLT x;

    val = pw_node_get_data(node);
    pw_node_get_cable(node, 0, &out);
    blksize = pw_node_blksize(node);

    x = *val;
    for(n = 0; n < blksize; n++) {
        pw_cable_set(out, n, x);
    }
}

static void var_destroy(pw_node *node)
{
    pw_node_cables_free(node);
}

static void node_var(pw_node *node, PWFLT *var)
{
    pw_node_cables_alloc(node, 1);
    pw_node_set_block(node, 0);
    pw_node_set_compute(node, var_compute);
    pw_node_set_destroy(node, var_destroy);
    pw_node_set_data(node, var);
}

static runt_int rproc_var(runt_vm *vm, runt_ptr p)
{
    runt_int rc;
    pw_node *node;
    pw_patch *patch;
    runt_ptr pw;
    PWFLT *var;
    runt_stacklet *out;

    rc = rpw_plugin_data(vm, &pw);
    RUNT_ERROR_CHECK(rc);

    patch = rpw_get_patch(pw);
    var = runt_to_cptr(p);

    rc = runt_ppush(vm, &out);
    RUNT_ERROR_CHECK(rc);

    rc = pw_patch_new_node(patch, &node);
    PW_RUNT_ERROR_CHECK(rc);

    node_var(node, var);

    rpw_push_output(vm, node, out, 0);

    return RUNT_OK;
}

void rpw_node_var(runt_vm *vm,
                  const char *name,
                  PWFLT *val)
{
    runt_ptr p;
    p = runt_mk_cptr(vm, val);
    patchwerk_word_define(vm, p,
                          name, strlen(name),
                          rproc_var);
}