| @@ -0,0 +1,103 @@ |
| 1 | +#include <math.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdint.h> |
| 4 | +#include "patchwerk.h" |
| 5 | +#include "core.h" |
| 6 | +#define SK_MODALRES_PRIV |
| 7 | +#include "dsp/modalres.h" |
| 8 | + |
| 9 | +struct modalres_n { |
| 10 | + pw_cable *in; |
| 11 | + pw_cable *freq; |
| 12 | + pw_cable *q; |
| 13 | + pw_cable *out; |
| 14 | + sk_modalres modalres; |
| 15 | +}; |
| 16 | + |
| 17 | +static void compute(pw_node *node) |
| 18 | +{ |
| 19 | + int blksize; |
| 20 | + int n; |
| 21 | + struct modalres_n *modalres; |
| 22 | + |
| 23 | + blksize = pw_node_blksize(node); |
| 24 | + |
| 25 | + modalres = (struct modalres_n *)pw_node_get_data(node); |
| 26 | + |
| 27 | + for (n = 0; n < blksize; n++) { |
| 28 | + PWFLT in, freq, q, out; |
| 29 | + in = pw_cable_get(modalres->in, n); |
| 30 | + |
| 31 | + freq = pw_cable_get(modalres->freq, n); |
| 32 | + sk_modalres_freq(&modalres->modalres, freq); |
| 33 | + q = pw_cable_get(modalres->q, n); |
| 34 | + sk_modalres_q(&modalres->modalres, q); |
| 35 | + |
| 36 | + out = sk_modalres_tick(&modalres->modalres, in); |
| 37 | + pw_cable_set(modalres->out, n, out); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +static void destroy(pw_node *node) |
| 42 | +{ |
| 43 | + pw_patch *patch; |
| 44 | + int rc; |
| 45 | + void *ud; |
| 46 | + rc = pw_node_get_patch(node, &patch); |
| 47 | + if (rc != PW_OK) return; |
| 48 | + pw_node_cables_free(node); |
| 49 | + ud = pw_node_get_data(node); |
| 50 | + pw_memory_free(patch, &ud); |
| 51 | +} |
| 52 | + |
| 53 | +int sk_node_modalres(sk_core *core) |
| 54 | +{ |
| 55 | + pw_patch *patch; |
| 56 | + pw_node *node; |
| 57 | + int rc; |
| 58 | + sk_param in, freq, q; |
| 59 | + void *ud; |
| 60 | + struct modalres_n *modalres; |
| 61 | + int sr; |
| 62 | + |
| 63 | + rc = sk_param_get(core, &q); |
| 64 | + SK_ERROR_CHECK(rc); |
| 65 | + |
| 66 | + rc = sk_param_get(core, &freq); |
| 67 | + SK_ERROR_CHECK(rc); |
| 68 | + |
| 69 | + rc = sk_param_get(core, &in); |
| 70 | + SK_ERROR_CHECK(rc); |
| 71 | + |
| 72 | + patch = sk_core_patch(core); |
| 73 | + |
| 74 | + rc = pw_memory_alloc(patch, sizeof(struct modalres_n), &ud); |
| 75 | + SK_PW_ERROR_CHECK(rc); |
| 76 | + modalres = (struct modalres_n *)ud; |
| 77 | + |
| 78 | + sr = pw_patch_srate_get(patch); |
| 79 | + sk_modalres_init(&modalres->modalres, sr); |
| 80 | + |
| 81 | + rc = pw_patch_new_node(patch, &node); |
| 82 | + SK_PW_ERROR_CHECK(rc); |
| 83 | + |
| 84 | + rc = pw_node_cables_alloc(node, 4); |
| 85 | + SK_PW_ERROR_CHECK(rc); |
| 86 | + |
| 87 | + pw_node_set_block(node, 3); |
| 88 | + |
| 89 | + pw_node_get_cable(node, 0, &modalres->in); |
| 90 | + pw_node_get_cable(node, 1, &modalres->freq); |
| 91 | + pw_node_get_cable(node, 2, &modalres->q); |
| 92 | + pw_node_get_cable(node, 3, &modalres->out); |
| 93 | + |
| 94 | + pw_node_set_data(node, modalres); |
| 95 | + pw_node_set_compute(node, compute); |
| 96 | + pw_node_set_destroy(node, destroy); |
| 97 | + |
| 98 | + sk_param_set(core, node, &in, 0); |
| 99 | + sk_param_set(core, node, &freq, 1); |
| 100 | + sk_param_set(core, node, &q, 2); |
| 101 | + sk_param_out(core, node, 3); |
| 102 | + return 0; |
| 103 | +} |