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
|
/* 3108:forum::für::umläute:2000 */
/* objects for manipulating packages*/
/*
repack : (re)pack floats/symbols/pointers to fixed-length packages
niagara : divides a package into 2 sub-packages
packel : get a specifique element of a package by its index
*/
#include "zexy.h"
#ifdef NT
#include <memory.h>
#endif
/* -------------------- repack ------------------------------ */
/*
(re)pack a sequence of (packages of) atoms into a package of given length
"bang" gives out the current package immediately
the second inlet lets you change the default package size
*/
static t_class *repack_class;
typedef struct _repack
{
t_object x_obj;
t_atom *buffer;
int bufsize;
int outputsize;
int current;
} t_repack;
static void repack_set(t_repack *x, t_float f)
{
/* set the new default size */
int n = f;
if (n > 0) {
/* flush all the newsized packages that are in the buffer */
t_atom *dumbuf = x->buffer;
int dumcur = x->current;
while (n <= dumcur) {
outlet_list(x->x_obj.ob_outlet, gensym("list"), n, dumbuf);
dumcur -= n;
dumbuf += n;
}
if (dumcur < 0) error("this should never happen :: dumcur = %d < 0", dumcur);
else {
memcpy(x->buffer, dumbuf, dumcur * sizeof(t_atom));
x->current = dumcur;
}
if (n > x->bufsize) {
dumbuf = (t_atom *)getbytes(n * sizeof(t_atom));
memcpy(dumbuf, x->buffer, x->current * sizeof(t_atom));
freebytes(x->buffer, x->bufsize * sizeof(t_atom));
x->buffer = dumbuf;
x->bufsize = n;
}
x->outputsize = n;
}
}
static void repack_bang(t_repack *x)
{
/* output the list as far as we are now */
outlet_list(x->x_obj.ob_outlet, gensym("list"), x->current, x->buffer);
x->current = 0;
}
static void repack_float(t_repack *x, t_float f)
{
/* add a float-atom to the list */
SETFLOAT(&x->buffer[x->current], f);
x->current++;
if (x->current >= x->outputsize) repack_bang(x);
}
static void repack_symbol(t_repack *x, t_symbol *s)
{
/* add a sym-atom to the list */
SETSYMBOL(&x->buffer[x->current], s);
x->current++;
if (x->current >= x->outputsize) repack_bang(x);
}
static void repack_pointer(t_repack *x, t_gpointer *p)
{
/* add a pointer-atom to the list */
SETPOINTER(&x->buffer[x->current], p);
x->current++;
if (x->current >= x->outputsize) repack_bang(x);
}
static void repack_list(t_repack *x, t_symbol *s, int argc, t_atom *argv)
{
int remain = x->outputsize - x->current;
t_atom *ap = argv;
if (argc >= remain) {
memcpy(x->buffer+x->current, ap, remain * sizeof(t_atom));
ap += remain;
argc -= remain;
outlet_list(x->x_obj.ob_outlet, gensym("list"), x->outputsize, x->buffer);
x->current = 0;
}
while (argc >= x->outputsize) {
outlet_list(x->x_obj.ob_outlet, gensym("list"), x->outputsize, ap);
ap += x->outputsize;
argc -= x->outputsize;
}
memcpy(x->buffer + x->current, ap, argc * sizeof(t_atom));
x->current += argc;
}
static void repack_anything(t_repack *x, t_symbol *s, int argc, t_atom *argv)
{
SETSYMBOL(&x->buffer[x->current], s);
x->current++;
if (x->current >= x->outputsize) {
repack_bang(x);
}
repack_list(x, gensym("list"), argc, argv);
}
static void *repack_new(t_floatarg f)
{
t_repack *x = (t_repack *)pd_new(repack_class);
x->outputsize = x->bufsize = (f>0.f)?f:2 ;
x->current = 0;
x->buffer = (t_atom *)getbytes(x->bufsize * sizeof(t_atom));
inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym(""));
outlet_new(&x->x_obj, 0);
return (x);
}
static void repack_setup(void)
{
repack_class = class_new(gensym("repack"), (t_newmethod)repack_new,
0, sizeof(t_repack), 0, A_DEFFLOAT, 0);
class_addbang (repack_class, repack_bang);
class_addfloat (repack_class, repack_float);
class_addsymbol (repack_class, repack_symbol);
class_addpointer (repack_class, repack_pointer);
class_addlist (repack_class, repack_list);
class_addanything(repack_class, repack_anything);
class_addmethod (repack_class, (t_method)repack_set, gensym(""), A_DEFFLOAT, 0);
class_sethelpsymbol(repack_class, gensym("zexy/repack"));
}
/* ------------------------- niagara ------------------------------- */
/*
divides a package into 2 sub-packages at a specified point
like the niagara-falls, some water goes down to the left side, the rest to the right side, devided by the rock
*/
static t_class *niagara_class;
typedef struct _niagara
{
t_object x_obj;
t_float rock;
t_outlet *left, *right;
} t_niagara;
static void niagara_list(t_niagara *x, t_symbol *s, int argc, t_atom *argv)
{
int n_l, n_r;
t_atom *ap_l, *ap_r;
int dumrock = x->rock;
int rock = ((dumrock < 0.f)?(argc+dumrock):dumrock);
n_l = (rock < argc)?rock:argc;
ap_l = argv;
n_r = argc - n_l;
ap_r = &argv[n_l];
if (n_r) outlet_list(x->right, s, n_r, ap_r);
if (n_l) outlet_list(x->left, s, n_l, ap_l);
}
static void niagara_any(t_niagara *x, t_symbol *s, int argc, t_atom *argv)
{
int n_l, n_r;
t_atom *ap_l, *ap_r;
t_symbol *s_r, *s_l;
int dumrock = x->rock;
int rock = ((dumrock < 0.f)?(argc+dumrock):dumrock-1);
n_l = (rock < argc)?rock:argc;
ap_l = argv;
s_l = s;
n_r = argc - n_l;
ap_r = &argv[n_l];
if (n_r) {
s_r = 0;
if (ap_r->a_type == A_FLOAT) s_r = gensym("list");
else {
s_r = atom_getsymbol(ap_r);
ap_r++;
n_r--;
}
outlet_anything(x->right, s_r, n_r, ap_r);
}
if (n_l+1 ) outlet_anything(x->left, s_l, n_l, ap_l);
}
static void *niagara_new(t_floatarg f)
{
t_niagara *x = (t_niagara *)pd_new(niagara_class);
x->rock = f;
x->left = outlet_new(&x->x_obj, &s_list);
x->right = outlet_new(&x->x_obj, &s_list);
floatinlet_new(&x->x_obj, &x->rock);
return (x);
}
static void niagara_setup(void)
{
niagara_class = class_new(gensym("niagara"), (t_newmethod)niagara_new,
0, sizeof(t_niagara), 0, A_DEFFLOAT, 0);
class_addlist (niagara_class, niagara_list);
class_addanything(niagara_class, niagara_any);
class_sethelpsymbol(niagara_class, gensym("zexy/niagara"));
}
/* ------------------------- packel ------------------------------- */
/*
get the nth element of a package
*/
static t_class *packel_class;
typedef struct _packel
{
t_object x_obj;
t_float position;
} t_packel;
static void packel_list(t_packel *x, t_symbol *s, int argc, t_atom *argv)
{
int mypos = x->position;
if (mypos) {
t_atom *current;
int pos = (mypos < 0)?(argc+mypos):(mypos-1);
if (pos < 0) pos = 0;
else if (pos >= argc) pos = argc - 1;
current = &(argv[pos]);
switch (current->a_type) {
case A_FLOAT:
outlet_float(x->x_obj.ob_outlet, atom_getfloat(current));
break;
case A_SYMBOL:
outlet_symbol(x->x_obj.ob_outlet, atom_getsymbol(current));
break;
case A_POINTER:
outlet_pointer(x->x_obj.ob_outlet, current->a_w.w_gpointer);
break;
case A_NULL:
outlet_bang(x->x_obj.ob_outlet);
default:
;
}
} else outlet_list(x->x_obj.ob_outlet, s, argc, argv);
}
static void packel_anything(t_packel *x, t_symbol *s, int argc, t_atom *argv)
{
t_atom *av2 = (t_atom *)getbytes((argc + 1) * sizeof(t_atom));
int i;
for (i = 0; i < argc; i++)
av2[i + 1] = argv[i];
SETSYMBOL(av2, s);
packel_list(x, gensym("list"), argc+1, av2);
freebytes(av2, (argc + 1) * sizeof(t_atom));
}
static void *packel_new(t_floatarg f)
{
t_packel *x = (t_packel *)pd_new(packel_class);
outlet_new(&x->x_obj, 0);
floatinlet_new(&x->x_obj, &x->position);
x->position = (int) f;
return (x);
}
static void packel_setup(void)
{
packel_class = class_new(gensym("packel"), (t_newmethod)packel_new,
0, sizeof(t_packel), 0, A_DEFFLOAT, 0);
class_addlist (packel_class, packel_list);
class_addanything(packel_class, packel_anything);
class_sethelpsymbol(packel_class, gensym("zexy/packel"));
}
/* -------------- overall setup routine for this file ----------------- */
void z_pack_setup(void)
{
repack_setup();
niagara_setup();
packel_setup();
/* I don't supply HELP - functionality, since this might harm overall-performance ere */
}
|