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
|
static t_class *pak_class;
static t_class *pak_inlet_class;
typedef struct _pak
{
t_object x_obj;
t_int x_n; // number of args
t_atom *x_vec; // input values
t_int x_nptr; // number of pointers
t_gpointer *x_gpointer; // the pointers
t_atom *x_outvec; // space for output values
int count;
struct _pak_inlet **proxy;
t_inlet **in;
} t_pak;
typedef struct _pak_inlet
{
t_pd p_pd;
t_pack *master;
int id;
t_symbol* type;
} t_pak_inlet;
static void *pak_new(t_symbol *s, int argc, t_atom *argv)
{
t_pak *x = (t_pak *)pd_new(pak_class);
t_atom defarg[2], *vap, *vec, *vp;
t_gpointer *gp;
int nptr = 0;
int i;
if (!argc)
{
argv = defarg;
argc = 2;
SETFLOAT(&defarg[0], 0);
SETFLOAT(&defarg[1], 0);
}
x->x_n = argc;
vec = x->x_vec = (t_atom *)getbytes(argc * sizeof(*x->x_vec));
x->x_outvec = (t_atom *)getbytes(argc * sizeof(*x->x_outvec));
for (i = argc, ap = argv; i--; ap++)
if (ap->a_type == A_SYMBOL && *ap->a_w.w_symbol->s_name == 'p')
nptr++;
gp = x->x_gpointer = (t_gpointer *)t_getbytes(nptr * sizeof (*gp));
x->x_nptr = nptr;
// Create inlet proxys
x->count = argc - 1;
if (x->count < 0) x->count = 0;
x->in = (t_inlet **)getbytes(x->count * sizeof(t_inlet *));
x->proxy = (t_pak_inlet**)getbytes(x->count * sizeof(t_pak_inlet*));
//
/*
for (n = 0; n < x->count; n++) {
x->proxy[n]=(t_pak_inlet*)pd_new(pak_inlet_class);
x->proxy[n]->master = x;
x->proxy[n]->id=n;
x->in[n] = inlet_new ((t_object*)x, (t_pd*)x->proxy[n], 0,0);
}
*/
int n;
for (i = 0, vp = x->x_vec, ap = argv; i < argc; i++, ap++, vp++)
{
n = i-1;
if (ap->a_type == A_FLOAT)
{
*vp = *ap;
if (i) {
//floatinlet_new(&x->x_obj, &vp->a_w.w_float);
x->proxy[n]=(t_pak_inlet*)pd_new(pak_inlet_class);
x->proxy[n]->master = x;
x->proxy[n]->id=n;
x->proxy[n]->typr = &s_float;
x->in[n] = inlet_new ((t_object*)x, (t_pd*)x->proxy[n], 0,0);
}
}
else if (ap->a_type == A_SYMBOL)
{
char c = *ap->a_w.w_symbol->s_name;
if (c == 's')
{
SETSYMBOL(vp, &s_symbol);
if (i) symbolinlet_new(&x->x_obj, &vp->a_w.w_symbol);
}
else if (c == 'p')
{
vp->a_type = A_POINTER;
vp->a_w.w_gpointer = gp;
gpointer_init(gp);
if (i) pointerinlet_new(&x->x_obj, gp);
gp++;
}
else
{
if (c != 'f') pd_error(x, "pak: %s: bad type",
ap->a_w.w_symbol->s_name);
SETFLOAT(vp, 0);
if (i) floatinlet_new(&x->x_obj, &vp->a_w.w_float);
}
}
}
outlet_new(&x->x_obj, &s_list);
return (x);
}
static void pak_bang(t_pak *x)
{
int i, reentered = 0, size = x->x_n * sizeof (t_atom);
t_gpointer *gp;
t_atom *outvec;
for (i = x->x_nptr, gp = x->x_gpointer; i--; gp++)
if (!gpointer_check(gp, 1))
{
pd_error(x, "pak: stale pointer");
return;
}
// reentrancy protection. The first time through use the pre-allocated
// x_outvec; if we're reentered we have to allocate new memory.
if (!x->x_outvec)
{
// LATER figure out how to deal with reentrancy and pointers...
if (x->x_nptr)
post("pak_bang: warning: reentry with pointers unprotected");
outvec = t_getbytes(size);
reentered = 1;
}
else
{
outvec = x->x_outvec;
x->x_outvec = 0;
}
memcpy(outvec, x->x_vec, size);
outlet_list(x->x_obj.ob_outlet, &s_list, x->x_n, outvec);
if (reentered)
t_freebytes(outvec, size);
else x->x_outvec = outvec;
}
static void pak_pointer(t_pak *x, t_gpointer *gp)
{
if (x->x_vec->a_type == A_POINTER)
{
gpointer_unset(x->x_gpointer);
*x->x_gpointer = *gp;
if (gp->gp_stub) gp->gp_stub->gs_refcount++;
pak_bang(x);
}
else pd_error(x, "pak_pointer: wrong type");
}
static void pak_float(t_pak *x, t_float f)
{
if (x->x_vec->a_type == A_FLOAT)
{
x->x_vec->a_w.w_float = f;
pak_bang(x);
}
else pd_error(x, "pak_float: wrong type");
}
static void pak_symbol(t_pak *x, t_symbol *s)
{
if (x->x_vec->a_type == A_SYMBOL)
{
x->x_vec->a_w.w_symbol = s;
pak_bang(x);
}
else pd_error(x, "pak_symbol: wrong type");
}
static void pak_list(t_pak *x, t_symbol *s, int ac, t_atom *av)
{
obj_list(&x->x_obj, 0, ac, av);
}
static void pak_anything(t_pak *x, t_symbol *s, int ac, t_atom *av)
{
t_atom *av2 = (t_atom *)getbytes((ac + 1) * sizeof(t_atom));
int i;
for (i = 0; i < ac; i++)
av2[i + 1] = av[i];
SETSYMBOL(av2, s);
obj_list(&x->x_obj, 0, ac+1, av2);
freebytes(av2, (ac + 1) * sizeof(t_atom));
}
static void pak_inlet(t_pak *y, t_symbol *s, int argc, t_atom *argv)
{
t_mux*x=y->p_master;
if(y->id==x->i_selected)
outlet_anything(x->x_obj.ob_outlet, s, argc, argv);
}
static void pak_free(t_pak *x)
{
t_gpointer *gp;
int i;
for (gp = x->x_gpointer, i = x->x_nptr; i--; gp++)
gpointer_unset(gp);
freebytes(x->x_vec, x->x_n * sizeof(*x->x_vec));
freebytes(x->x_outvec, x->x_n * sizeof(*x->x_outvec));
freebytes(x->x_gpointer, x->x_nptr * sizeof(*x->x_gpointer));
// Free proxys
const int count = x->count;
if(x->in && x->proxy){
int n=0;
for(n=0; n<count; n++){
if(x->in[n]){
inlet_free(x->in[n]);
}
x->in[n]=0;
if(x->proxy[n]){
t_pak_inlet *y=x->proxy[n];
y->master=0;
y->id=0;
pd_free(&y->p_pd);
}
x->proxy[n]=0;
}
freebytes(x->in, x->count * sizeof(t_inlet *));
freebytes(x->proxy, x->count * sizeof(t_pak_inlet*));
}
}
static void pak_setup(void)
{
pak_class = class_new(gensym("pak"), (t_newmethod)pak_new,
(t_method)pak_free, sizeof(t_pak), 0, A_GIMME, 0);
class_addbang(pak_class, pak_bang);
class_addpointer(pak_class, pak_pointer);
class_addfloat(pak_class, pak_float);
class_addsymbol(pak_class, pak_symbol);
class_addlist(pak_class, pak_list);
class_addanything(pak_class, pak_anything);
// Setup proxies
pak_inlet_class = class_new(0, 0, 0, sizeof(t_pak_inlet),CLASS_PD | CLASS_NOINLET, 0);
class_addanything(pak_inlet_class, pak_inlet);
}
//////////////// MULTIPLEX //////////////
/*
static t_class *mux_class;
static t_class *muxproxy_class;
typedef struct _mux
{
t_object x_obj;
struct _muxproxy **x_proxy;
int i_count;
int i_selected;
t_inlet **in;
} t_mux;
typedef struct _muxproxy
{
t_pd p_pd;
t_mux *p_master;
int id;
} t_muxproxy;
static void mux_select(t_mux *x, t_float f)
{
x->i_selected=f;
}
static void mux_anything(t_muxproxy *y, t_symbol *s, int argc, t_atom *argv)
{
t_mux*x=y->p_master;
if(y->id==x->i_selected)
outlet_anything(x->x_obj.ob_outlet, s, argc, argv);
}
static void *mux_new(t_symbol *s, int argc, t_atom *argv)
{
int n = (argc < 2)?2:argc;
t_mux *x = (t_mux *)pd_new(mux_class);
x->i_selected=0;
x->i_count = n;
x->in = (t_inlet **)getbytes(x->i_count * sizeof(t_inlet *));
x->x_proxy = (t_muxproxy**)getbytes(x->i_count * sizeof(t_muxproxy*));
for (n = 0; n<x->i_count; n++) {
x->x_proxy[n]=(t_muxproxy*)pd_new(muxproxy_class);
x->x_proxy[n]->p_master = x;
x->x_proxy[n]->id=n;
x->in[n] = inlet_new ((t_object*)x, (t_pd*)x->x_proxy[n], 0,0);
}
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym(""));
outlet_new(&x->x_obj, 0);
return (x);
}
static void mux_free(t_mux*x){
const int count = x->i_count;
if(x->in && x->x_proxy){
int n=0;
for(n=0; n<count; n++){
if(x->in[n]){
inlet_free(x->in[n]);
}
x->in[n]=0;
if(x->x_proxy[n]){
t_muxproxy *y=x->x_proxy[n];
y->p_master=0;
y->id=0;
pd_free(&y->p_pd);
}
x->x_proxy[n]=0;
}
freebytes(x->in, x->i_count * sizeof(t_inlet *));
freebytes(x->x_proxy, x->i_count * sizeof(t_muxproxy*));
}
// pd_free(&y->p_pd);
}
void multiplex_setup(void)
{
mux_class = class_new(gensym("multiplex"), (t_newmethod)mux_new,
(t_method)mux_free, sizeof(t_mux), CLASS_NOINLET, A_GIMME, 0);
class_addcreator((t_newmethod)mux_new, gensym("mux"), A_GIMME, 0);
class_addmethod (mux_class, (t_method)mux_select, gensym(""), A_DEFFLOAT, 0);
muxproxy_class = class_new(0, 0, 0,
sizeof(t_muxproxy),
CLASS_PD | CLASS_NOINLET, 0);
class_addanything(muxproxy_class, mux_anything);
zexy_register("multiplex");
}
void mux_setup(void)
{
multiplex_setup();
}
*/
|