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
|
#include "zexy.h"
#include <math.h>
#ifdef NT
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#define sqrtf sqrt
#endif
#ifdef MACOSX
#define sqrtf sqrt
#endif
/* mean :: the mean of a list of floats */
static t_class *mean_class;
typedef struct _mean
{
t_object x_obj;
} t_mean;
static void mean_list(t_mean *x, t_symbol *s, int argc, t_atom *argv)
{
t_float factor = 1./argc;
t_float sum = 0;
while(argc--)sum+=atom_getfloat(argv++);
outlet_float(x->x_obj.ob_outlet,sum*factor);
}
static void *mean_new(void)
{
t_mean *x = (t_mean *)pd_new(mean_class);
outlet_new(&x->x_obj, gensym("float"));
return (x);
}
static void mean_help(void)
{
post("mean\t:: calculate the mean of a list of floats");
}
static void mean_setup(void)
{
mean_class = class_new(gensym("mean"), (t_newmethod)mean_new, 0,
sizeof(t_mean), 0, A_DEFFLOAT, 0);
class_addlist(mean_class, (t_method)mean_list);
class_addmethod(mean_class, (t_method)mean_help, gensym("help"), 0);
class_sethelpsymbol(mean_class, gensym("zexy/mean"));
}
/* minmax :: get minimum and maximum of a list */
static t_class *minmax_class;
typedef struct _minmax
{
t_object x_obj;
t_float min;
t_float max;
t_outlet *mino, *maxo;
} t_minmax;
static void minmax_bang(t_minmax *x)
{
outlet_float(x->maxo,x->max);
outlet_float(x->mino,x->min);
}
static void minmax_list(t_minmax *x, t_symbol *s, int argc, t_atom *argv)
{
t_float min = atom_getfloat(argv++);
t_float max=min;
argc--;
while(argc--){
t_float f = atom_getfloat(argv++);
if (f<min)min=f;
else if (f>max)max=f;
}
x->min=min;
x->max=max;
minmax_bang(x);
}
static void *minmax_new(void)
{
t_minmax *x = (t_minmax *)pd_new(minmax_class);
x->mino=outlet_new(&x->x_obj, gensym("float"));
x->maxo=outlet_new(&x->x_obj, gensym("float"));
x->min = x->max = 0;
return (x);
}
static void minmax_help(void)
{
post("minmax\t:: get minimum and maximum of a list of floats");
}
static void minmax_setup(void)
{
minmax_class = class_new(gensym("minmax"), (t_newmethod)minmax_new, 0,
sizeof(t_minmax), 0, A_DEFFLOAT, 0);
class_addlist(minmax_class, (t_method)minmax_list);
class_addbang(minmax_class, (t_method)minmax_bang);
class_addmethod(minmax_class, (t_method)minmax_help, gensym("help"), 0);
class_sethelpsymbol(minmax_class, gensym("zexy/minmax"));
}
/* length :: get minimum and maximum of a list */
static t_class *length_class;
typedef struct _length
{
t_object x_obj;
} t_length;
static void length_list(t_length *x, t_symbol *s, int argc, t_atom *argv)
{
outlet_float(x->x_obj.ob_outlet, (t_float)argc);
}
static void length_any(t_length *x, t_symbol *s, int argc, t_atom *argv)
{
outlet_float(x->x_obj.ob_outlet, (t_float)argc+1);
}
static void *length_new(void)
{
t_length *x = (t_length *)pd_new(length_class);
outlet_new(&x->x_obj, gensym("float"));
return (x);
}
static void length_setup(void)
{
length_class = class_new(gensym("length"), (t_newmethod)length_new, 0,
sizeof(t_length), 0, A_DEFFLOAT, 0);
class_addlist(length_class, (t_method)length_list);
class_addanything(length_class, (t_method)length_any);
// class_addbang(length_class, (t_method)length_bang);
class_sethelpsymbol(length_class, gensym("zexy/length"));
}
/* global setup routine */
void z_stat_setup(void)
{
mean_setup();
minmax_setup();
length_setup();
}
|