~pbatch/patchwerk

patchwerk/janet/main.c -rw-r--r-- 5.3 KiB
9c265356 — paul plan9 additions from Sigrid 4 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
#include <janet.h>
#include "patchwerk.h"
#include "soundpipe.h"

void pw_janet_nodes(JanetTable *env);

static Janet mkpatch(int32_t argc, Janet *argv)
{
    int nbufs;
    int stksize;
    int blksize;
    int sr;
    pw_patch *patch;

    janet_fixarity(argc, 4);

    nbufs = janet_unwrap_integer(argv[0]);
    stksize = janet_unwrap_integer(argv[1]);
    blksize = janet_unwrap_integer(argv[2]);
    sr = janet_unwrap_integer(argv[3]);

    patch = calloc(1, pw_patch_size());
    pw_patch_init(patch, blksize);
    pw_patch_alloc(patch, nbufs, stksize);
    pw_patch_srate_set(patch, sr);

    return janet_wrap_abstract(patch);
}

static Janet delpatch(int32_t argc, Janet *argv)
{
    pw_patch *patch;

    janet_fixarity(argc, 1);

    patch = janet_unwrap_abstract(argv[0]);
    pw_patch_destroy(patch);
    pw_patch_free_nodes(patch);
    free(patch);
    return janet_wrap_nil();
}

static Janet j_sp_create(int32_t argc, Janet *argv)
{
    sp_data *sp;
    int sr;

    janet_fixarity(argc, 1);

    sr = janet_unwrap_integer(argv[0]);
    sp_create(&sp);
    sp->sr = sr;
    return janet_wrap_abstract(sp);
}

static Janet j_sp_destroy(int32_t argc, Janet *argv)
{
    sp_data *sp;

    janet_fixarity(argc, 1);

    sp = janet_unwrap_abstract(argv[0]);
    sp_destroy(&sp);
    return janet_wrap_nil();
}

static Janet cable_set_constant(int32_t argc, Janet *argv)
{
    pw_cable *c;
    PWFLT val;

    janet_fixarity(argc, 2);

    c = janet_unwrap_abstract(argv[0]);
    val = janet_unwrap_number(argv[1]);

    pw_cable_set_constant(c, val);
    return janet_wrap_nil();
}

static Janet node_get_cable(int32_t argc, Janet *argv)
{
    pw_node *node;
    pw_cable *c;
    int pos;
    int rc;

    janet_fixarity(argc, 2);

    node = janet_unwrap_abstract(argv[0]);
    pos = janet_unwrap_integer(argv[1]);

    rc = pw_node_get_cable(node, pos, &c);

    if (rc != PW_OK) {
        return janet_wrap_nil();
    }

    return janet_wrap_abstract(c);
}

static Janet node_get_data(int32_t argc, Janet *argv)
{
    pw_node *node;
    pw_cable *c;
    int rc;
    void *ud;

    janet_fixarity(argc, 1);

    node = janet_unwrap_abstract(argv[0]);

    ud = pw_node_get_data(node);

    return janet_wrap_abstract(ud);
}

static Janet patch_stack(int32_t argc, Janet *argv)
{
    pw_patch *patch;
    pw_stack *stack;

    janet_fixarity(argc, 1);

    patch = janet_unwrap_abstract(argv[0]);
    stack = pw_patch_stack(patch);

    return janet_wrap_abstract(stack);
}

static Janet pop_stack(int32_t argc, Janet *argv)
{
    pw_stack *stack;
    pw_buffer *buf;
    int rc;

    janet_fixarity(argc, 1);

    stack = janet_unwrap_abstract(argv[0]);

    buf = NULL;
    rc = pw_stack_pop(stack, &buf);

    if (rc != PW_OK) return janet_wrap_nil();

    return janet_wrap_abstract(buf);
}

static Janet dup_stack(int32_t argc, Janet *argv)
{
    pw_stack *stack;

    janet_fixarity(argc, 1);

    stack = janet_unwrap_abstract(argv[0]);

    pw_stack_dup(stack);

    return janet_wrap_nil();
}

static Janet cable_connect(int32_t argc, Janet *argv)
{
    pw_cable *c1;
    pw_cable *c2;

    janet_fixarity(argc, 2);

    c1 = janet_unwrap_abstract(argv[0]);
    c2 = janet_unwrap_abstract(argv[1]);

    pw_cable_connect(c1, c2);

    return janet_wrap_nil();
}

static Janet patch_tick(int32_t argc, Janet *argv)
{
    pw_patch *patch;

    janet_fixarity(argc, 1);

    patch = janet_unwrap_abstract(argv[0]);

    pw_patch_tick(patch);

    return janet_wrap_nil();
}

static Janet patch_bhold(int32_t argc, Janet *argv)
{
    pw_patch *patch;
    pw_buffer *buf;

    janet_fixarity(argc, 1);

    patch = janet_unwrap_abstract(argv[0]);

    pw_patch_bhold(patch, &buf);

    return janet_wrap_abstract(buf);
}

static Janet patch_bunhold(int32_t argc, Janet *argv)
{
    pw_patch *patch;
    pw_buffer *buf;

    janet_fixarity(argc, 2);

    patch = janet_unwrap_abstract(argv[0]);
    buf = janet_unwrap_abstract(argv[1]);

    pw_patch_bhold(patch, &buf);

    return janet_wrap_nil();
}

static Janet stack_push_buffer(int32_t argc, Janet *argv)
{
    pw_stack *stack;
    pw_buffer *buf;

    janet_fixarity(argc, 2);

    stack = janet_unwrap_abstract(argv[0]);
    buf = janet_unwrap_abstract(argv[1]);

    pw_stack_push_buffer(stack, buf);

    return janet_wrap_nil();
}

static const JanetReg cfuns[] =
{
    {"mkpatch", mkpatch, "Sets up patchwerk."},
    {"delpatch", delpatch, "cleans up patchwerk."},
    {"sp-create", j_sp_create, "Creates soundpipe struct."},
    {"sp-destroy", j_sp_destroy, "Destroys soundpipe struct."},
    {"cable-set-constant", cable_set_constant, "sets constant."},
    {"node-get-cable", node_get_cable, "gets cable."},
    {"node-get-data", node_get_data, "Gets node data."},
    {"patch-stack", patch_stack, "Patch Stack."},
    {"stack-pop", pop_stack, "Pop from buffer stack."},
    {"stack-dup", dup_stack, "Duplicate item on stack."},
    {"cable-connect", cable_connect, "Connect."},
    {"patch-tick", patch_tick, "computes a single sample"},
    {"patch-bhold", patch_bhold, "holds last buffer on stack"},
    {"patch-bunhold", patch_bunhold, "unholds buffer"},
    {"stack-push-buffer", stack_push_buffer, "Pushes buffer onto stack."},
    {NULL, NULL, NULL}
};

void pw_janet_loader(JanetTable *env)
{
    janet_cfuns(env, "patchwerk", cfuns);
}

#ifdef PW_JANET_PLUGIN
JANET_MODULE_ENTRY(JanetTable *env)
{
    pw_janet_loader(env);
    pw_janet_nodes(env);
}
#endif