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