summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/src/x_misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/x_misc.c')
-rw-r--r--apps/plugins/pdbox/PDa/src/x_misc.c320
1 files changed, 0 insertions, 320 deletions
diff --git a/apps/plugins/pdbox/PDa/src/x_misc.c b/apps/plugins/pdbox/PDa/src/x_misc.c
index 394b294f62..e7d0005421 100644
--- a/apps/plugins/pdbox/PDa/src/x_misc.c
+++ b/apps/plugins/pdbox/PDa/src/x_misc.c
@@ -319,324 +319,4 @@ void x_misc_setup(void)
319 cputime_setup(); 319 cputime_setup();
320 realtime_setup(); 320 realtime_setup();
321} 321}
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. */
325
326/* misc. */
327
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
343
344#if defined (MACOSX) || defined (__FreeBSD__)
345#define HZ CLK_TCK
346#endif
347
348/* -------------------------- random ------------------------------ */
349/* this is strictly homebrew and untested. */
350
351static t_class *random_class;
352
353typedef struct _random
354{
355 t_object x_obj;
356 t_float x_f;
357 unsigned int x_state;
358} t_random;
359
360
361static int makeseed(void)
362{
363 static unsigned int random_nextseed = 1489853723;
364 random_nextseed = random_nextseed * 435898247 + 938284287;
365 return (random_nextseed & 0x7fffffff);
366}
367
368static void *random_new(t_floatarg f)
369{
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);
376}
377
378static void random_bang(t_random *x)
379{
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);
388}
389
390static void random_seed(t_random *x, float f, float glob)
391{
392 x->x_state = f;
393}
394
395static void random_setup(void)
396{
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);
402}
403
404
405/* -------------------------- loadbang ------------------------------ */
406static t_class *loadbang_class;
407
408typedef struct _loadbang
409{
410 t_object x_obj;
411} t_loadbang;
412
413static void *loadbang_new(void)
414{
415 t_loadbang *x = (t_loadbang *)pd_new(loadbang_class);
416 outlet_new(&x->x_obj, &s_bang);
417 return (x);
418}
419
420static void loadbang_loadbang(t_loadbang *x)
421{
422 if (!sys_noloadbang)
423 outlet_bang(x->x_obj.ob_outlet);
424}
425
426static void loadbang_setup(void)
427{
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);
432}
433
434/* ------------- namecanvas (delete this later) --------------------- */
435static t_class *namecanvas_class;
436
437typedef struct _namecanvas
438{
439 t_object x_obj;
440 t_symbol *x_sym;
441 t_pd *x_owner;
442} t_namecanvas;
443
444static void *namecanvas_new(t_symbol *s)
445{
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);
451}
452
453static void namecanvas_free(t_namecanvas *x)
454{
455 if (*x->x_sym->s_name) pd_unbind(x->x_owner, x->x_sym);
456}
457
458static void namecanvas_setup(void)
459{
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);
463}
464
465/* ---------------serial ports (MSW only -- hack) ------------------------- */
466#define MAXSERIAL 100
467
468static t_class *serial_class;
469
470typedef struct _serial
471{
472 t_object x_obj;
473 int x_portno;
474 int x_open;
475} t_serial;
476
477static void serial_float(t_serial *x, t_float f)
478{
479 int n = f;
480 char message[MAXSERIAL * 4 + 100];
481 if (!x->x_open)
482 {
483 sys_vgui("com%d_open\n", x->x_portno);
484 x->x_open = 1;
485 }
486 sprintf(message, "com%d_send \"\\%3.3o\"\n", x->x_portno, n);
487 sys_gui(message);
488}
489
490static void *serial_new(t_floatarg fportno)
491{
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);
498}
499
500static void serial_setup(void)
501{
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);
505}
506
507/* -------------------------- cputime ------------------------------ */
508
509static t_class *cputime_class;
510
511typedef struct _cputime
512{
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;
523
524static void cputime_bang(t_cputime *x)
525{
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)
535 {
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;
541 }
542#endif
543}
544
545#define HZ 100
546static void cputime_bang2(t_cputime *x)
547{
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;
562
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
572}
573
574static void *cputime_new(void)
575{
576 t_cputime *x = (t_cputime *)pd_new(cputime_class);
577 outlet_new(&x->x_obj, gensym("float"));
578
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);
585}
586
587static void cputime_setup(void)
588{
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);
593}
594
595/* -------------------------- realtime ------------------------------ */
596
597static t_class *realtime_class;
598
599typedef struct _realtime
600{
601 t_object x_obj;
602 double x_setrealtime;
603} t_realtime;
604
605static void realtime_bang(t_realtime *x)
606{
607 x->x_setrealtime = sys_getrealtime();
608}
609
610static void realtime_bang2(t_realtime *x)
611{
612 outlet_float(x->x_obj.ob_outlet,
613 (sys_getrealtime() - x->x_setrealtime) * 1000.);
614}
615
616static void *realtime_new(void)
617{
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);
623}
624
625static void realtime_setup(void)
626{
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"),
631 0);
632}
633 322
634void x_misc_setup(void)
635{
636 random_setup();
637 loadbang_setup();
638 namecanvas_setup();
639 serial_setup();
640 cputime_setup();
641 realtime_setup();
642}