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.c642
1 files changed, 642 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/src/x_misc.c b/apps/plugins/pdbox/PDa/src/x_misc.c
new file mode 100644
index 0000000000..394b294f62
--- /dev/null
+++ b/apps/plugins/pdbox/PDa/src/x_misc.c
@@ -0,0 +1,642 @@
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. */
4
5/* misc. */
6
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
22
23#if defined (MACOSX) || defined (__FreeBSD__)
24#define HZ CLK_TCK
25#endif
26
27/* -------------------------- random ------------------------------ */
28/* this is strictly homebrew and untested. */
29
30static t_class *random_class;
31
32typedef struct _random
33{
34 t_object x_obj;
35 t_float x_f;
36 unsigned int x_state;
37} t_random;
38
39
40static int makeseed(void)
41{
42 static unsigned int random_nextseed = 1489853723;
43 random_nextseed = random_nextseed * 435898247 + 938284287;
44 return (random_nextseed & 0x7fffffff);
45}
46
47static void *random_new(t_floatarg f)
48{
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);
55}
56
57static void random_bang(t_random *x)
58{
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);
67}
68
69static void random_seed(t_random *x, float f, float glob)
70{
71 x->x_state = f;
72}
73
74static void random_setup(void)
75{
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);
81}
82
83
84/* -------------------------- loadbang ------------------------------ */
85static t_class *loadbang_class;
86
87typedef struct _loadbang
88{
89 t_object x_obj;
90} t_loadbang;
91
92static void *loadbang_new(void)
93{
94 t_loadbang *x = (t_loadbang *)pd_new(loadbang_class);
95 outlet_new(&x->x_obj, &s_bang);
96 return (x);
97}
98
99static void loadbang_loadbang(t_loadbang *x)
100{
101 if (!sys_noloadbang)
102 outlet_bang(x->x_obj.ob_outlet);
103}
104
105static void loadbang_setup(void)
106{
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);
111}
112
113/* ------------- namecanvas (delete this later) --------------------- */
114static t_class *namecanvas_class;
115
116typedef struct _namecanvas
117{
118 t_object x_obj;
119 t_symbol *x_sym;
120 t_pd *x_owner;
121} t_namecanvas;
122
123static void *namecanvas_new(t_symbol *s)
124{
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);
130}
131
132static void namecanvas_free(t_namecanvas *x)
133{
134 if (*x->x_sym->s_name) pd_unbind(x->x_owner, x->x_sym);
135}
136
137static void namecanvas_setup(void)
138{
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);
142}
143
144/* ---------------serial ports (MSW only -- hack) ------------------------- */
145#define MAXSERIAL 100
146
147static t_class *serial_class;
148
149typedef struct _serial
150{
151 t_object x_obj;
152 int x_portno;
153 int x_open;
154} t_serial;
155
156static void serial_float(t_serial *x, t_float f)
157{
158 int n = f;
159 char message[MAXSERIAL * 4 + 100];
160 if (!x->x_open)
161 {
162 sys_vgui("com%d_open\n", x->x_portno);
163 x->x_open = 1;
164 }
165 sprintf(message, "com%d_send \"\\%3.3o\"\n", x->x_portno, n);
166 sys_gui(message);
167}
168
169static void *serial_new(t_floatarg fportno)
170{
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);
177}
178
179static void serial_setup(void)
180{
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);
184}
185
186/* -------------------------- cputime ------------------------------ */
187
188static t_class *cputime_class;
189
190typedef struct _cputime
191{
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;
202
203static void cputime_bang(t_cputime *x)
204{
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)
214 {
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;
220 }
221#endif
222}
223
224#define HZ 100
225static void cputime_bang2(t_cputime *x)
226{
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;
241
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
251}
252
253static void *cputime_new(void)
254{
255 t_cputime *x = (t_cputime *)pd_new(cputime_class);
256 outlet_new(&x->x_obj, gensym("float"));
257
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);
264}
265
266static void cputime_setup(void)
267{
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);
272}
273
274/* -------------------------- realtime ------------------------------ */
275
276static t_class *realtime_class;
277
278typedef struct _realtime
279{
280 t_object x_obj;
281 double x_setrealtime;
282} t_realtime;
283
284static void realtime_bang(t_realtime *x)
285{
286 x->x_setrealtime = sys_getrealtime();
287}
288
289static void realtime_bang2(t_realtime *x)
290{
291 outlet_float(x->x_obj.ob_outlet,
292 (sys_getrealtime() - x->x_setrealtime) * 1000.);
293}
294
295static void *realtime_new(void)
296{
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);
302}
303
304static void realtime_setup(void)
305{
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"),
310 0);
311}
312
313void x_misc_setup(void)
314{
315 random_setup();
316 loadbang_setup();
317 namecanvas_setup();
318 serial_setup();
319 cputime_setup();
320 realtime_setup();
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
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}