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
|
#include "m_pd.h"
#include <string.h>
#define IS_A_SYMBOL(atom,index) ((atom+index)->a_type == A_SYMBOL)
#define IS_A_FLOAT(atom,index) ((atom+index)->a_type == A_FLOAT)
/* Mode 0: unfold all the list till its end or when banged.
* Mode 1: unfold each element of the list at each bang.
* Mode 0 is by default. Mode 1 is activated when list_unfold is
* created with "wait" or "manually" as only argument.
*/
static t_class *list_unfold_class;
typedef struct _list_unfold {
t_object x_obj;
t_int iterating;
t_outlet* outlet1;
t_outlet* outlet2;
t_int mode;
int memSize;
int ac;
t_atom* av;
} t_list_unfold;
void list_unfold_bang(t_list_unfold *x)
{
//x->i_count = x->i_down;
if ( x->mode == 0) {
x->iterating = 0;
} else {
int i;
if ( x->iterating < x->ac ) {
i = x->iterating;
x->iterating++;
outlet_float(x->outlet2,i);
if ( IS_A_FLOAT(x->av,i) ) {
outlet_float(x->outlet1,atom_getfloat(x->av + i));
} else {
outlet_symbol(x->outlet1,atom_getsymbol(x->av + i));
}
}
}
}
void list_unfold_anything(t_list_unfold *x, t_symbol* s, int ac, t_atom* av)
{
if ( x->mode == 0) {
// Output all
int i =0;
int offset =0;
x->iterating = 1;
if ( s != &s_list && s != &s_float && s != &s_symbol ) {
outlet_float(x->outlet2,0);
outlet_symbol(x->outlet1,s);
offset=1;
}
for ( ; i < ac && x->iterating; i++ ) {
outlet_float(x->outlet2,i+offset);
if ( IS_A_FLOAT(av,0) ) {
outlet_float(x->outlet1,atom_getfloat(av));
} else {
outlet_symbol(x->outlet1,atom_getsymbol(av));
}
av++;
}
} else {
x->iterating = 0;
// Copy and wait for bangs to output
int do_selector = ( s != &s_list && s != &s_float && s != &s_symbol );
x->ac = ac + do_selector; //One more for the selector
// Resize memory if required and add 3 atoms just in case
if(x->ac > x->memSize) {
x->av = resizebytes(x->av, x->memSize * sizeof(*(x->av)),
(3 + x->ac) * sizeof(*(x->av)));
x->memSize = 3 + x->ac;
}
t_atom* dst = x->av;
// Copy selector
if ( do_selector ) {
SETSYMBOL(dst, s);
dst++;
}
// Copy atoms
while(ac--) *dst++ = *av++;
}
}
static void list_unfold_free(t_list_unfold *x)
{
freebytes(x->av, x->memSize * sizeof(*(x->av)));
}
void *list_unfold_new(t_symbol *s, int argc, t_atom *argv)
{
t_list_unfold *x = (t_list_unfold *)pd_new(list_unfold_class);
x->iterating = 0;
x->mode = 0;
if (argc && IS_A_SYMBOL(argv,0) ) {
t_symbol* type = atom_getsymbol(argv);
if (strcmp(type->s_name,"wait")==0 || strcmp(type->s_name,"manually")==0) {
x->mode = 1;
}
}
// Initialize memory
x->memSize = 10;
x->ac = 0;
x->av = getbytes(x->memSize * sizeof(*(x->av)));
x->outlet1 = outlet_new(&x->x_obj, &s_list);
x->outlet2 = outlet_new(&x->x_obj, &s_float);
return (void *)x;
}
void list_unfold_setup(void) {
list_unfold_class = class_new(gensym("list_unfold"),
(t_newmethod)list_unfold_new,
(t_method)list_unfold_free, sizeof(t_list_unfold),
CLASS_DEFAULT,
A_GIMME, 0);
class_addbang (list_unfold_class, list_unfold_bang);
class_addanything (list_unfold_class, list_unfold_anything);
//class_addlist (list_unfold_class, list_unfold_list);
}
|