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
|
/*
* repack : (re)pack floats/symbols/pointers to fixed-length packages
*
* (c) 1999-2011 IOhannes m zmölnig, forum::für::umläute, institute of electronic music and acoustics (iem)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "zexy.h"
#include <string.h>
/* -------------------- 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* UNUSED(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);
}
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);
zexy_register("repack");
}
|