1 /* Copyright (c) 1997-1999 Miller Puckette.
2 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
3 * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
12 extern uint64_t runningtime
;
20 #include <sys/types.h>
22 #include <sys/times.h>
23 //#include <sys/param.h>
31 #if defined (MACOSX) || defined (__FreeBSD__)
35 /* -------------------------- random ------------------------------ */
36 /* this is strictly homebrew and untested. */
38 static t_class
*random_class
;
40 typedef struct _random
48 static int makeseed(void)
50 static unsigned int random_nextseed
= 1489853723;
51 random_nextseed
= random_nextseed
* 435898247 + 938284287;
52 return (random_nextseed
& 0x7fffffff);
55 static void *random_new(t_floatarg f
)
57 t_random
*x
= (t_random
*)pd_new(random_class
);
59 x
->x_state
= makeseed();
60 floatinlet_new(&x
->x_obj
, &x
->x_f
);
61 outlet_new(&x
->x_obj
, &s_float
);
65 static void random_bang(t_random
*x
)
68 int range
= (n
< 1 ? 1 : n
);
69 unsigned int randval
= x
->x_state
;
70 x
->x_state
= randval
= randval
* 472940017 + 832416023;
71 nval
= ((double)range
) * ((double)randval
)
73 if (nval
>= range
) nval
= range
-1;
74 outlet_float(x
->x_obj
.ob_outlet
, nval
);
77 static void random_seed(t_random
*x
, float f
, float glob
)
85 static void random_setup(void)
87 random_class
= class_new(gensym("random"), (t_newmethod
)random_new
, 0,
88 sizeof(t_random
), 0, A_DEFFLOAT
, 0);
89 class_addbang(random_class
, random_bang
);
90 class_addmethod(random_class
, (t_method
)random_seed
,
91 gensym("seed"), A_FLOAT
, 0);
95 /* -------------------------- loadbang ------------------------------ */
96 static t_class
*loadbang_class
;
98 typedef struct _loadbang
103 static void *loadbang_new(void)
105 t_loadbang
*x
= (t_loadbang
*)pd_new(loadbang_class
);
106 outlet_new(&x
->x_obj
, &s_bang
);
110 static void loadbang_loadbang(t_loadbang
*x
)
113 outlet_bang(x
->x_obj
.ob_outlet
);
116 static void loadbang_setup(void)
118 loadbang_class
= class_new(gensym("loadbang"), (t_newmethod
)loadbang_new
, 0,
119 sizeof(t_loadbang
), 0, 0);
120 class_addmethod(loadbang_class
, (t_method
)loadbang_loadbang
,
121 gensym("loadbang"), 0);
124 /* ------------- namecanvas (delete this later) --------------------- */
125 static t_class
*namecanvas_class
;
127 typedef struct _namecanvas
134 static void *namecanvas_new(t_symbol
*s
)
136 t_namecanvas
*x
= (t_namecanvas
*)pd_new(namecanvas_class
);
137 x
->x_owner
= (t_pd
*)canvas_getcurrent();
139 if (*s
->s_name
) pd_bind(x
->x_owner
, s
);
143 static void namecanvas_free(t_namecanvas
*x
)
145 if (*x
->x_sym
->s_name
) pd_unbind(x
->x_owner
, x
->x_sym
);
148 static void namecanvas_setup(void)
150 namecanvas_class
= class_new(gensym("namecanvas"),
151 (t_newmethod
)namecanvas_new
, (t_method
)namecanvas_free
,
152 sizeof(t_namecanvas
), CLASS_NOINLET
, A_DEFSYM
, 0);
155 /* ---------------serial ports (MSW only -- hack) ------------------------- */
156 #define MAXSERIAL 100
158 static t_class
*serial_class
;
160 typedef struct _serial
167 static void serial_float(t_serial
*x
, t_float f
)
174 char message
[MAXSERIAL
* 4 + 100];
177 sys_vgui("com%d_open\n", x
->x_portno
);
180 sprintf(message
, "com%d_send \"\\%3.3o\"\n", x
->x_portno
, n
);
185 static void *serial_new(t_floatarg fportno
)
187 int portno
= fportno
;
188 t_serial
*x
= (t_serial
*)pd_new(serial_class
);
189 if (!portno
) portno
= 1;
190 x
->x_portno
= portno
;
195 static void serial_setup(void)
197 serial_class
= class_new(gensym("serial"), (t_newmethod
)serial_new
, 0,
198 sizeof(t_serial
), 0, A_DEFFLOAT
, 0);
199 class_addfloat(serial_class
, serial_float
);
202 /* -------------------------- cputime ------------------------------ */
204 static t_class
*cputime_class
;
206 typedef struct _cputime
210 uint64_t x_runningtime
;
213 struct tms x_setcputime
;
216 LARGE_INTEGER x_kerneltime
;
217 LARGE_INTEGER x_usertime
;
222 static void cputime_bang(t_cputime
*x
)
225 x
->x_runningtime
= runningtime
;
228 times(&x
->x_setcputime
);
231 FILETIME ignorethis
, ignorethat
;
233 retval
= GetProcessTimes(GetCurrentProcess(), &ignorethis
, &ignorethat
,
234 (FILETIME
*)&x
->x_kerneltime
, (FILETIME
*)&x
->x_usertime
);
238 post("cputime is apparently not supported on your platform");
240 x
->x_kerneltime
.QuadPart
= 0;
241 x
->x_usertime
.QuadPart
= 0;
249 static void cputime_bang2(t_cputime
*x
)
252 float elapsedcpu
= 1000 *
253 (runningtime
- x
->x_runningtime
) / HZ
;
254 outlet_float(x
->x_obj
.ob_outlet
, elapsedcpu
);
258 struct tms newcputime
;
260 elapsedcpu
= 1000 * (
261 newcputime
.tms_utime
+ newcputime
.tms_stime
-
262 x
->x_setcputime
.tms_utime
- x
->x_setcputime
.tms_stime
) / HZ
;
263 outlet_float(x
->x_obj
.ob_outlet
, elapsedcpu
);
267 FILETIME ignorethis
, ignorethat
;
268 LARGE_INTEGER usertime
, kerneltime
;
271 retval
= GetProcessTimes(GetCurrentProcess(), &ignorethis
, &ignorethat
,
272 (FILETIME
*)&kerneltime
, (FILETIME
*)&usertime
);
274 elapsedcpu
= 0.0001 *
275 ((kerneltime
.QuadPart
- x
->x_kerneltime
.QuadPart
) +
276 (usertime
.QuadPart
- x
->x_usertime
.QuadPart
));
278 outlet_float(x
->x_obj
.ob_outlet
, elapsedcpu
);
282 static void *cputime_new(void)
284 t_cputime
*x
= (t_cputime
*)pd_new(cputime_class
);
285 outlet_new(&x
->x_obj
, gensym("float"));
287 inlet_new(&x
->x_obj
, &x
->x_obj
.ob_pd
, gensym("bang"), gensym("bang2"));
295 static void cputime_setup(void)
297 cputime_class
= class_new(gensym("cputime"), (t_newmethod
)cputime_new
, 0,
298 sizeof(t_cputime
), 0, 0);
299 class_addbang(cputime_class
, cputime_bang
);
300 class_addmethod(cputime_class
, (t_method
)cputime_bang2
, gensym("bang2"), 0);
303 /* -------------------------- realtime ------------------------------ */
305 static t_class
*realtime_class
;
307 typedef struct _realtime
310 double x_setrealtime
;
313 static void realtime_bang(t_realtime
*x
)
315 x
->x_setrealtime
= sys_getrealtime();
318 static void realtime_bang2(t_realtime
*x
)
320 outlet_float(x
->x_obj
.ob_outlet
,
321 (sys_getrealtime() - x
->x_setrealtime
) * 1000.);
324 static void *realtime_new(void)
326 t_realtime
*x
= (t_realtime
*)pd_new(realtime_class
);
327 outlet_new(&x
->x_obj
, gensym("float"));
328 inlet_new(&x
->x_obj
, &x
->x_obj
.ob_pd
, gensym("bang"), gensym("bang2"));
333 static void realtime_setup(void)
335 realtime_class
= class_new(gensym("realtime"), (t_newmethod
)realtime_new
, 0,
336 sizeof(t_realtime
), 0, 0);
337 class_addbang(realtime_class
, realtime_bang
);
338 class_addmethod(realtime_class
, (t_method
)realtime_bang2
, gensym("bang2"),
342 void x_misc_setup(void)