Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin...
[kugel-rb.git] / apps / plugins / pdbox / PDa / src / x_misc.c
blob394b294f621270be59a156c3602df5fb524e4dcd
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. */
5 /* misc. */
7 #include "m_pd.h"
8 #include "s_stuff.h"
9 #include <math.h>
10 #include <stdio.h>
11 #include <string.h>
12 #ifdef UNIX
13 #include <sys/types.h>
14 #include <sys/time.h>
15 #include <sys/times.h>
16 //#include <sys/param.h>
17 #endif
18 #ifdef MSW
19 #include <wtypes.h>
20 #include <time.h>
21 #endif
23 #if defined (MACOSX) || defined (__FreeBSD__)
24 #define HZ CLK_TCK
25 #endif
27 /* -------------------------- random ------------------------------ */
28 /* this is strictly homebrew and untested. */
30 static t_class *random_class;
32 typedef struct _random
34 t_object x_obj;
35 t_float x_f;
36 unsigned int x_state;
37 } t_random;
40 static int makeseed(void)
42 static unsigned int random_nextseed = 1489853723;
43 random_nextseed = random_nextseed * 435898247 + 938284287;
44 return (random_nextseed & 0x7fffffff);
47 static void *random_new(t_floatarg f)
49 t_random *x = (t_random *)pd_new(random_class);
50 x->x_f = f;
51 x->x_state = makeseed();
52 floatinlet_new(&x->x_obj, &x->x_f);
53 outlet_new(&x->x_obj, &s_float);
54 return (x);
57 static void random_bang(t_random *x)
59 int n = x->x_f, nval;
60 int range = (n < 1 ? 1 : n);
61 unsigned int randval = x->x_state;
62 x->x_state = randval = randval * 472940017 + 832416023;
63 nval = ((double)range) * ((double)randval)
64 * (1./4294967296.);
65 if (nval >= range) nval = range-1;
66 outlet_float(x->x_obj.ob_outlet, nval);
69 static void random_seed(t_random *x, float f, float glob)
71 x->x_state = f;
74 static void random_setup(void)
76 random_class = class_new(gensym("random"), (t_newmethod)random_new, 0,
77 sizeof(t_random), 0, A_DEFFLOAT, 0);
78 class_addbang(random_class, random_bang);
79 class_addmethod(random_class, (t_method)random_seed,
80 gensym("seed"), A_FLOAT, 0);
84 /* -------------------------- loadbang ------------------------------ */
85 static t_class *loadbang_class;
87 typedef struct _loadbang
89 t_object x_obj;
90 } t_loadbang;
92 static void *loadbang_new(void)
94 t_loadbang *x = (t_loadbang *)pd_new(loadbang_class);
95 outlet_new(&x->x_obj, &s_bang);
96 return (x);
99 static void loadbang_loadbang(t_loadbang *x)
101 if (!sys_noloadbang)
102 outlet_bang(x->x_obj.ob_outlet);
105 static void loadbang_setup(void)
107 loadbang_class = class_new(gensym("loadbang"), (t_newmethod)loadbang_new, 0,
108 sizeof(t_loadbang), 0, 0);
109 class_addmethod(loadbang_class, (t_method)loadbang_loadbang,
110 gensym("loadbang"), 0);
113 /* ------------- namecanvas (delete this later) --------------------- */
114 static t_class *namecanvas_class;
116 typedef struct _namecanvas
118 t_object x_obj;
119 t_symbol *x_sym;
120 t_pd *x_owner;
121 } t_namecanvas;
123 static void *namecanvas_new(t_symbol *s)
125 t_namecanvas *x = (t_namecanvas *)pd_new(namecanvas_class);
126 x->x_owner = (t_pd *)canvas_getcurrent();
127 x->x_sym = s;
128 if (*s->s_name) pd_bind(x->x_owner, s);
129 return (x);
132 static void namecanvas_free(t_namecanvas *x)
134 if (*x->x_sym->s_name) pd_unbind(x->x_owner, x->x_sym);
137 static void namecanvas_setup(void)
139 namecanvas_class = class_new(gensym("namecanvas"),
140 (t_newmethod)namecanvas_new, (t_method)namecanvas_free,
141 sizeof(t_namecanvas), CLASS_NOINLET, A_DEFSYM, 0);
144 /* ---------------serial ports (MSW only -- hack) ------------------------- */
145 #define MAXSERIAL 100
147 static t_class *serial_class;
149 typedef struct _serial
151 t_object x_obj;
152 int x_portno;
153 int x_open;
154 } t_serial;
156 static void serial_float(t_serial *x, t_float f)
158 int n = f;
159 char message[MAXSERIAL * 4 + 100];
160 if (!x->x_open)
162 sys_vgui("com%d_open\n", x->x_portno);
163 x->x_open = 1;
165 sprintf(message, "com%d_send \"\\%3.3o\"\n", x->x_portno, n);
166 sys_gui(message);
169 static void *serial_new(t_floatarg fportno)
171 int portno = fportno;
172 t_serial *x = (t_serial *)pd_new(serial_class);
173 if (!portno) portno = 1;
174 x->x_portno = portno;
175 x->x_open = 0;
176 return (x);
179 static void serial_setup(void)
181 serial_class = class_new(gensym("serial"), (t_newmethod)serial_new, 0,
182 sizeof(t_serial), 0, A_DEFFLOAT, 0);
183 class_addfloat(serial_class, serial_float);
186 /* -------------------------- cputime ------------------------------ */
188 static t_class *cputime_class;
190 typedef struct _cputime
192 t_object x_obj;
193 #ifdef UNIX
194 struct tms x_setcputime;
195 #endif
196 #ifdef MSW
197 LARGE_INTEGER x_kerneltime;
198 LARGE_INTEGER x_usertime;
199 int x_warned;
200 #endif
201 } t_cputime;
203 static void cputime_bang(t_cputime *x)
205 #ifdef UNIX
206 times(&x->x_setcputime);
207 #endif
208 #ifdef MSW
209 FILETIME ignorethis, ignorethat;
210 BOOL retval;
211 retval = GetProcessTimes(GetCurrentProcess(), &ignorethis, &ignorethat,
212 (FILETIME *)&x->x_kerneltime, (FILETIME *)&x->x_usertime);
213 if (!retval)
215 if (!x->x_warned)
216 post("cputime is apparently not supported on your platform");
217 x->x_warned = 1;
218 x->x_kerneltime.QuadPart = 0;
219 x->x_usertime.QuadPart = 0;
221 #endif
224 #define HZ 100
225 static void cputime_bang2(t_cputime *x)
227 #ifdef UNIX
228 float elapsedcpu;
229 struct tms newcputime;
230 times(&newcputime);
231 elapsedcpu = 1000 * (
232 newcputime.tms_utime + newcputime.tms_stime -
233 x->x_setcputime.tms_utime - x->x_setcputime.tms_stime) / HZ;
234 outlet_float(x->x_obj.ob_outlet, elapsedcpu);
235 #endif
236 #ifdef MSW
237 float elapsedcpu;
238 FILETIME ignorethis, ignorethat;
239 LARGE_INTEGER usertime, kerneltime;
240 BOOL retval;
242 retval = GetProcessTimes(GetCurrentProcess(), &ignorethis, &ignorethat,
243 (FILETIME *)&kerneltime, (FILETIME *)&usertime);
244 if (retval)
245 elapsedcpu = 0.0001 *
246 ((kerneltime.QuadPart - x->x_kerneltime.QuadPart) +
247 (usertime.QuadPart - x->x_usertime.QuadPart));
248 else elapsedcpu = 0;
249 outlet_float(x->x_obj.ob_outlet, elapsedcpu);
250 #endif
253 static void *cputime_new(void)
255 t_cputime *x = (t_cputime *)pd_new(cputime_class);
256 outlet_new(&x->x_obj, gensym("float"));
258 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("bang"), gensym("bang2"));
259 #ifdef MSW
260 x->x_warned = 0;
261 #endif
262 cputime_bang(x);
263 return (x);
266 static void cputime_setup(void)
268 cputime_class = class_new(gensym("cputime"), (t_newmethod)cputime_new, 0,
269 sizeof(t_cputime), 0, 0);
270 class_addbang(cputime_class, cputime_bang);
271 class_addmethod(cputime_class, (t_method)cputime_bang2, gensym("bang2"), 0);
274 /* -------------------------- realtime ------------------------------ */
276 static t_class *realtime_class;
278 typedef struct _realtime
280 t_object x_obj;
281 double x_setrealtime;
282 } t_realtime;
284 static void realtime_bang(t_realtime *x)
286 x->x_setrealtime = sys_getrealtime();
289 static void realtime_bang2(t_realtime *x)
291 outlet_float(x->x_obj.ob_outlet,
292 (sys_getrealtime() - x->x_setrealtime) * 1000.);
295 static void *realtime_new(void)
297 t_realtime *x = (t_realtime *)pd_new(realtime_class);
298 outlet_new(&x->x_obj, gensym("float"));
299 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("bang"), gensym("bang2"));
300 realtime_bang(x);
301 return (x);
304 static void realtime_setup(void)
306 realtime_class = class_new(gensym("realtime"), (t_newmethod)realtime_new, 0,
307 sizeof(t_realtime), 0, 0);
308 class_addbang(realtime_class, realtime_bang);
309 class_addmethod(realtime_class, (t_method)realtime_bang2, gensym("bang2"),
313 void x_misc_setup(void)
315 random_setup();
316 loadbang_setup();
317 namecanvas_setup();
318 serial_setup();
319 cputime_setup();
320 realtime_setup();
322 /* Copyright (c) 1997-1999 Miller Puckette.
323 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
324 * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
326 /* misc. */
328 #include "m_pd.h"
329 #include "s_stuff.h"
330 #include <math.h>
331 #include <stdio.h>
332 #include <string.h>
333 #ifdef UNIX
334 #include <sys/types.h>
335 #include <sys/time.h>
336 #include <sys/times.h>
337 //#include <sys/param.h>
338 #endif
339 #ifdef MSW
340 #include <wtypes.h>
341 #include <time.h>
342 #endif
344 #if defined (MACOSX) || defined (__FreeBSD__)
345 #define HZ CLK_TCK
346 #endif
348 /* -------------------------- random ------------------------------ */
349 /* this is strictly homebrew and untested. */
351 static t_class *random_class;
353 typedef struct _random
355 t_object x_obj;
356 t_float x_f;
357 unsigned int x_state;
358 } t_random;
361 static int makeseed(void)
363 static unsigned int random_nextseed = 1489853723;
364 random_nextseed = random_nextseed * 435898247 + 938284287;
365 return (random_nextseed & 0x7fffffff);
368 static void *random_new(t_floatarg f)
370 t_random *x = (t_random *)pd_new(random_class);
371 x->x_f = f;
372 x->x_state = makeseed();
373 floatinlet_new(&x->x_obj, &x->x_f);
374 outlet_new(&x->x_obj, &s_float);
375 return (x);
378 static void random_bang(t_random *x)
380 int n = x->x_f, nval;
381 int range = (n < 1 ? 1 : n);
382 unsigned int randval = x->x_state;
383 x->x_state = randval = randval * 472940017 + 832416023;
384 nval = ((double)range) * ((double)randval)
385 * (1./4294967296.);
386 if (nval >= range) nval = range-1;
387 outlet_float(x->x_obj.ob_outlet, nval);
390 static void random_seed(t_random *x, float f, float glob)
392 x->x_state = f;
395 static void random_setup(void)
397 random_class = class_new(gensym("random"), (t_newmethod)random_new, 0,
398 sizeof(t_random), 0, A_DEFFLOAT, 0);
399 class_addbang(random_class, random_bang);
400 class_addmethod(random_class, (t_method)random_seed,
401 gensym("seed"), A_FLOAT, 0);
405 /* -------------------------- loadbang ------------------------------ */
406 static t_class *loadbang_class;
408 typedef struct _loadbang
410 t_object x_obj;
411 } t_loadbang;
413 static void *loadbang_new(void)
415 t_loadbang *x = (t_loadbang *)pd_new(loadbang_class);
416 outlet_new(&x->x_obj, &s_bang);
417 return (x);
420 static void loadbang_loadbang(t_loadbang *x)
422 if (!sys_noloadbang)
423 outlet_bang(x->x_obj.ob_outlet);
426 static void loadbang_setup(void)
428 loadbang_class = class_new(gensym("loadbang"), (t_newmethod)loadbang_new, 0,
429 sizeof(t_loadbang), 0, 0);
430 class_addmethod(loadbang_class, (t_method)loadbang_loadbang,
431 gensym("loadbang"), 0);
434 /* ------------- namecanvas (delete this later) --------------------- */
435 static t_class *namecanvas_class;
437 typedef struct _namecanvas
439 t_object x_obj;
440 t_symbol *x_sym;
441 t_pd *x_owner;
442 } t_namecanvas;
444 static void *namecanvas_new(t_symbol *s)
446 t_namecanvas *x = (t_namecanvas *)pd_new(namecanvas_class);
447 x->x_owner = (t_pd *)canvas_getcurrent();
448 x->x_sym = s;
449 if (*s->s_name) pd_bind(x->x_owner, s);
450 return (x);
453 static void namecanvas_free(t_namecanvas *x)
455 if (*x->x_sym->s_name) pd_unbind(x->x_owner, x->x_sym);
458 static void namecanvas_setup(void)
460 namecanvas_class = class_new(gensym("namecanvas"),
461 (t_newmethod)namecanvas_new, (t_method)namecanvas_free,
462 sizeof(t_namecanvas), CLASS_NOINLET, A_DEFSYM, 0);
465 /* ---------------serial ports (MSW only -- hack) ------------------------- */
466 #define MAXSERIAL 100
468 static t_class *serial_class;
470 typedef struct _serial
472 t_object x_obj;
473 int x_portno;
474 int x_open;
475 } t_serial;
477 static void serial_float(t_serial *x, t_float f)
479 int n = f;
480 char message[MAXSERIAL * 4 + 100];
481 if (!x->x_open)
483 sys_vgui("com%d_open\n", x->x_portno);
484 x->x_open = 1;
486 sprintf(message, "com%d_send \"\\%3.3o\"\n", x->x_portno, n);
487 sys_gui(message);
490 static void *serial_new(t_floatarg fportno)
492 int portno = fportno;
493 t_serial *x = (t_serial *)pd_new(serial_class);
494 if (!portno) portno = 1;
495 x->x_portno = portno;
496 x->x_open = 0;
497 return (x);
500 static void serial_setup(void)
502 serial_class = class_new(gensym("serial"), (t_newmethod)serial_new, 0,
503 sizeof(t_serial), 0, A_DEFFLOAT, 0);
504 class_addfloat(serial_class, serial_float);
507 /* -------------------------- cputime ------------------------------ */
509 static t_class *cputime_class;
511 typedef struct _cputime
513 t_object x_obj;
514 #ifdef UNIX
515 struct tms x_setcputime;
516 #endif
517 #ifdef MSW
518 LARGE_INTEGER x_kerneltime;
519 LARGE_INTEGER x_usertime;
520 int x_warned;
521 #endif
522 } t_cputime;
524 static void cputime_bang(t_cputime *x)
526 #ifdef UNIX
527 times(&x->x_setcputime);
528 #endif
529 #ifdef MSW
530 FILETIME ignorethis, ignorethat;
531 BOOL retval;
532 retval = GetProcessTimes(GetCurrentProcess(), &ignorethis, &ignorethat,
533 (FILETIME *)&x->x_kerneltime, (FILETIME *)&x->x_usertime);
534 if (!retval)
536 if (!x->x_warned)
537 post("cputime is apparently not supported on your platform");
538 x->x_warned = 1;
539 x->x_kerneltime.QuadPart = 0;
540 x->x_usertime.QuadPart = 0;
542 #endif
545 #define HZ 100
546 static void cputime_bang2(t_cputime *x)
548 #ifdef UNIX
549 float elapsedcpu;
550 struct tms newcputime;
551 times(&newcputime);
552 elapsedcpu = 1000 * (
553 newcputime.tms_utime + newcputime.tms_stime -
554 x->x_setcputime.tms_utime - x->x_setcputime.tms_stime) / HZ;
555 outlet_float(x->x_obj.ob_outlet, elapsedcpu);
556 #endif
557 #ifdef MSW
558 float elapsedcpu;
559 FILETIME ignorethis, ignorethat;
560 LARGE_INTEGER usertime, kerneltime;
561 BOOL retval;
563 retval = GetProcessTimes(GetCurrentProcess(), &ignorethis, &ignorethat,
564 (FILETIME *)&kerneltime, (FILETIME *)&usertime);
565 if (retval)
566 elapsedcpu = 0.0001 *
567 ((kerneltime.QuadPart - x->x_kerneltime.QuadPart) +
568 (usertime.QuadPart - x->x_usertime.QuadPart));
569 else elapsedcpu = 0;
570 outlet_float(x->x_obj.ob_outlet, elapsedcpu);
571 #endif
574 static void *cputime_new(void)
576 t_cputime *x = (t_cputime *)pd_new(cputime_class);
577 outlet_new(&x->x_obj, gensym("float"));
579 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("bang"), gensym("bang2"));
580 #ifdef MSW
581 x->x_warned = 0;
582 #endif
583 cputime_bang(x);
584 return (x);
587 static void cputime_setup(void)
589 cputime_class = class_new(gensym("cputime"), (t_newmethod)cputime_new, 0,
590 sizeof(t_cputime), 0, 0);
591 class_addbang(cputime_class, cputime_bang);
592 class_addmethod(cputime_class, (t_method)cputime_bang2, gensym("bang2"), 0);
595 /* -------------------------- realtime ------------------------------ */
597 static t_class *realtime_class;
599 typedef struct _realtime
601 t_object x_obj;
602 double x_setrealtime;
603 } t_realtime;
605 static void realtime_bang(t_realtime *x)
607 x->x_setrealtime = sys_getrealtime();
610 static void realtime_bang2(t_realtime *x)
612 outlet_float(x->x_obj.ob_outlet,
613 (sys_getrealtime() - x->x_setrealtime) * 1000.);
616 static void *realtime_new(void)
618 t_realtime *x = (t_realtime *)pd_new(realtime_class);
619 outlet_new(&x->x_obj, gensym("float"));
620 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("bang"), gensym("bang2"));
621 realtime_bang(x);
622 return (x);
625 static void realtime_setup(void)
627 realtime_class = class_new(gensym("realtime"), (t_newmethod)realtime_new, 0,
628 sizeof(t_realtime), 0, 0);
629 class_addbang(realtime_class, realtime_bang);
630 class_addmethod(realtime_class, (t_method)realtime_bang2, gensym("bang2"),
634 void x_misc_setup(void)
636 random_setup();
637 loadbang_setup();
638 namecanvas_setup();
639 serial_setup();
640 cputime_setup();
641 realtime_setup();