Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin...
[kugel-rb.git] / apps / plugins / pdbox / PDa / src / d_misc.c
blobf601d66d90af6a6fc205b3cd2f7e1aa121229033
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 /* miscellaneous: print~; more to come.
6 */
8 #include "m_pd.h"
9 #include <stdio.h>
10 #include <string.h>
12 /* ------------------------- print~ -------------------------- */
13 static t_class *print_class;
15 typedef struct _print
17 t_object x_obj;
18 float x_f;
19 t_symbol *x_sym;
20 int x_count;
21 } t_print;
23 static t_int *print_perform(t_int *w)
25 t_print *x = (t_print *)(w[1]);
26 t_float *in = (t_float *)(w[2]);
27 int n = (int)(w[3]);
28 if (x->x_count)
30 post("%s:", x->x_sym->s_name);
31 if (n == 1) post("%8g", in[0]);
32 else if (n == 2) post("%8g %8g", in[0], in[1]);
33 else if (n == 4) post("%8g %8g %8g %8g",
34 in[0], in[1], in[2], in[3]);
35 else while (n > 0)
37 post("%-8.5g %-8.5g %-8.5g %-8.5g %-8.5g %-8.5g %-8.5g %-8.5g",
38 in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]);
39 n -= 8;
40 in += 8;
42 x->x_count--;
44 return (w+4);
47 static void print_dsp(t_print *x, t_signal **sp)
49 dsp_add(print_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
52 static void print_float(t_print *x, t_float f)
54 if (f < 0) f = 0;
55 x->x_count = f;
58 static void print_bang(t_print *x)
60 x->x_count = 1;
63 static void *print_new(t_symbol *s)
65 t_print *x = (t_print *)pd_new(print_class);
66 x->x_sym = (s->s_name[0]? s : gensym("print~"));
67 x->x_count = 0;
68 x->x_f = 0;
69 return (x);
72 static void print_setup(void)
74 print_class = class_new(gensym("print~"), (t_newmethod)print_new, 0,
75 sizeof(t_print), 0, A_DEFSYM, 0);
76 CLASS_MAINSIGNALIN(print_class, t_print, x_f);
77 class_addmethod(print_class, (t_method)print_dsp, gensym("dsp"), 0);
78 class_addbang(print_class, print_bang);
79 class_addfloat(print_class, print_float);
82 /* ------------------------- scope~ -------------------------- */
83 /* this has been replaced by arrays; to be deleted later */
85 #include "g_canvas.h"
87 static t_class *scope_class;
89 #define SCOPESIZE 256
91 typedef struct _scope
93 t_object x_obj;
94 t_sample x_samps[SCOPESIZE];
95 int x_phase;
96 int x_drawn;
97 void *x_canvas;
98 } t_scope;
100 static t_int *scope_perform(t_int *w)
102 t_scope *x = (t_scope *)(w[1]);
103 t_float *in = (t_float *)(w[2]);
104 int n = (int)(w[3]), phase = x->x_phase;
105 while (n--)
107 x->x_samps[phase] = *in++;
108 phase = (phase + 1) & (SCOPESIZE-1);
110 x->x_phase = phase;
111 return (w+4);
114 static void scope_dsp(t_scope *x, t_signal **sp)
116 dsp_add(scope_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
119 static void scope_erase(t_scope *x)
121 if (x->x_drawn) sys_vgui(".x%x.c delete gumbo\n", x->x_canvas);
124 #define X1 10.
125 #define X2 20.
126 #define YC 5.
127 static void scope_bang(t_scope *x)
129 int n, phase;
130 char hugebuf[10000], *s = hugebuf;
131 scope_erase(x);
132 sys_vgui(".x%x.c create line 10c 5c 20c 5c -tags gumbo\n", x->x_canvas);
133 sprintf(s, ".x%x.c create line ", (t_int)x->x_canvas);
134 s += strlen(s);
135 for (n = 0, phase = x->x_phase;
136 n < SCOPESIZE; phase = ((phase+1) & (SCOPESIZE-1)), n++)
138 sprintf(s, "%fc %fc ", X1 + (X2 - X1) * (float)n * (1./SCOPESIZE),
139 YC - 5 * x->x_samps[phase]);
140 s += strlen(s);
141 /* post("phase %d", phase); */
143 sprintf(s, "-tags gumbo\n");
144 sys_gui(hugebuf);
145 x->x_drawn = 1;
148 static void scope_free(t_scope *x)
150 scope_erase(x);
153 static void *scope_new(t_symbol *s)
155 t_scope *x = (t_scope *)pd_new(scope_class);
156 error("scope: this is now obsolete; use arrays and tabwrite~ instead");
157 x->x_phase = 0;
158 x->x_drawn = 0;
159 x->x_canvas = canvas_getcurrent();
160 return (x);
163 static void scope_setup(void)
165 scope_class = class_new(gensym("scope~"), (t_newmethod)scope_new,
166 (t_method)scope_free, sizeof(t_scope), 0, A_DEFSYM, 0);
167 class_addmethod(scope_class, nullfn, gensym("signal"), 0);
168 class_addmethod(scope_class, (t_method)scope_dsp, gensym("dsp"), 0);
169 class_addbang(scope_class, scope_bang);
172 /* ------------------------ bang~ -------------------------- */
174 static t_class *bang_tilde_class;
176 typedef struct _bang
178 t_object x_obj;
179 t_clock *x_clock;
180 } t_bang;
182 static t_int *bang_tilde_perform(t_int *w)
184 t_bang *x = (t_bang *)(w[1]);
185 clock_delay(x->x_clock, 0);
186 return (w+2);
189 static void bang_tilde_dsp(t_bang *x, t_signal **sp)
191 dsp_add(bang_tilde_perform, 1, x);
194 static void bang_tilde_tick(t_bang *x)
196 outlet_bang(x->x_obj.ob_outlet);
199 static void bang_tilde_free(t_bang *x)
201 clock_free(x->x_clock);
204 static void *bang_tilde_new(t_symbol *s)
206 t_bang *x = (t_bang *)pd_new(bang_tilde_class);
207 x->x_clock = clock_new(x, (t_method)bang_tilde_tick);
208 outlet_new(&x->x_obj, &s_bang);
209 return (x);
212 static void bang_tilde_setup(void)
214 bang_tilde_class = class_new(gensym("bang~"), (t_newmethod)bang_tilde_new,
215 (t_method)bang_tilde_free, sizeof(t_bang), 0, 0);
216 class_addmethod(bang_tilde_class, (t_method)bang_tilde_dsp,
217 gensym("dsp"), 0);
220 /* ------------------------ samplerate~~ -------------------------- */
222 static t_class *samplerate_tilde_class;
224 typedef struct _samplerate
226 t_object x_obj;
227 } t_samplerate;
229 static void samplerate_tilde_bang(t_samplerate *x)
231 outlet_float(x->x_obj.ob_outlet, sys_getsr());
234 static void *samplerate_tilde_new(t_symbol *s)
236 t_samplerate *x = (t_samplerate *)pd_new(samplerate_tilde_class);
237 outlet_new(&x->x_obj, &s_float);
238 return (x);
241 static void samplerate_tilde_setup(void)
243 samplerate_tilde_class = class_new(gensym("samplerate~"),
244 (t_newmethod)samplerate_tilde_new, 0, sizeof(t_samplerate), 0, 0);
245 class_addbang(samplerate_tilde_class, samplerate_tilde_bang);
248 /* ------------------------ global setup routine ------------------------- */
250 void d_misc_setup(void)
252 #ifndef FIXEDPOINT
253 print_setup();
254 #endif
255 scope_setup();
256 bang_tilde_setup();
257 samplerate_tilde_setup();
263 /* Copyright (c) 1997-1999 Miller Puckette.
264 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
265 * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
267 /* miscellaneous: print~; more to come.
270 #include "m_pd.h"
271 #include <stdio.h>
272 #include <string.h>
274 /* ------------------------- print~ -------------------------- */
275 static t_class *print_class;
277 typedef struct _print
279 t_object x_obj;
280 float x_f;
281 t_symbol *x_sym;
282 int x_count;
283 } t_print;
285 static t_int *print_perform(t_int *w)
287 t_print *x = (t_print *)(w[1]);
288 t_float *in = (t_float *)(w[2]);
289 int n = (int)(w[3]);
290 if (x->x_count)
292 post("%s:", x->x_sym->s_name);
293 if (n == 1) post("%8g", in[0]);
294 else if (n == 2) post("%8g %8g", in[0], in[1]);
295 else if (n == 4) post("%8g %8g %8g %8g",
296 in[0], in[1], in[2], in[3]);
297 else while (n > 0)
299 post("%-8.5g %-8.5g %-8.5g %-8.5g %-8.5g %-8.5g %-8.5g %-8.5g",
300 in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]);
301 n -= 8;
302 in += 8;
304 x->x_count--;
306 return (w+4);
309 static void print_dsp(t_print *x, t_signal **sp)
311 dsp_add(print_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
314 static void print_float(t_print *x, t_float f)
316 if (f < 0) f = 0;
317 x->x_count = f;
320 static void print_bang(t_print *x)
322 x->x_count = 1;
325 static void *print_new(t_symbol *s)
327 t_print *x = (t_print *)pd_new(print_class);
328 x->x_sym = (s->s_name[0]? s : gensym("print~"));
329 x->x_count = 0;
330 x->x_f = 0;
331 return (x);
334 static void print_setup(void)
336 print_class = class_new(gensym("print~"), (t_newmethod)print_new, 0,
337 sizeof(t_print), 0, A_DEFSYM, 0);
338 CLASS_MAINSIGNALIN(print_class, t_print, x_f);
339 class_addmethod(print_class, (t_method)print_dsp, gensym("dsp"), 0);
340 class_addbang(print_class, print_bang);
341 class_addfloat(print_class, print_float);
344 /* ------------------------- scope~ -------------------------- */
345 /* this has been replaced by arrays; to be deleted later */
347 #include "g_canvas.h"
349 static t_class *scope_class;
351 #define SCOPESIZE 256
353 typedef struct _scope
355 t_object x_obj;
356 t_sample x_samps[SCOPESIZE];
357 int x_phase;
358 int x_drawn;
359 void *x_canvas;
360 } t_scope;
362 static t_int *scope_perform(t_int *w)
364 t_scope *x = (t_scope *)(w[1]);
365 t_float *in = (t_float *)(w[2]);
366 int n = (int)(w[3]), phase = x->x_phase;
367 while (n--)
369 x->x_samps[phase] = *in++;
370 phase = (phase + 1) & (SCOPESIZE-1);
372 x->x_phase = phase;
373 return (w+4);
376 static void scope_dsp(t_scope *x, t_signal **sp)
378 dsp_add(scope_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
381 static void scope_erase(t_scope *x)
383 if (x->x_drawn) sys_vgui(".x%x.c delete gumbo\n", x->x_canvas);
386 #define X1 10.
387 #define X2 20.
388 #define YC 5.
389 static void scope_bang(t_scope *x)
391 int n, phase;
392 char hugebuf[10000], *s = hugebuf;
393 scope_erase(x);
394 sys_vgui(".x%x.c create line 10c 5c 20c 5c -tags gumbo\n", x->x_canvas);
395 sprintf(s, ".x%x.c create line ", (t_int)x->x_canvas);
396 s += strlen(s);
397 for (n = 0, phase = x->x_phase;
398 n < SCOPESIZE; phase = ((phase+1) & (SCOPESIZE-1)), n++)
400 sprintf(s, "%fc %fc ", X1 + (X2 - X1) * (float)n * (1./SCOPESIZE),
401 YC - 5 * x->x_samps[phase]);
402 s += strlen(s);
403 /* post("phase %d", phase); */
405 sprintf(s, "-tags gumbo\n");
406 sys_gui(hugebuf);
407 x->x_drawn = 1;
410 static void scope_free(t_scope *x)
412 scope_erase(x);
415 static void *scope_new(t_symbol *s)
417 t_scope *x = (t_scope *)pd_new(scope_class);
418 error("scope: this is now obsolete; use arrays and tabwrite~ instead");
419 x->x_phase = 0;
420 x->x_drawn = 0;
421 x->x_canvas = canvas_getcurrent();
422 return (x);
425 static void scope_setup(void)
427 scope_class = class_new(gensym("scope~"), (t_newmethod)scope_new,
428 (t_method)scope_free, sizeof(t_scope), 0, A_DEFSYM, 0);
429 class_addmethod(scope_class, nullfn, gensym("signal"), 0);
430 class_addmethod(scope_class, (t_method)scope_dsp, gensym("dsp"), 0);
431 class_addbang(scope_class, scope_bang);
434 /* ------------------------ bang~ -------------------------- */
436 static t_class *bang_tilde_class;
438 typedef struct _bang
440 t_object x_obj;
441 t_clock *x_clock;
442 } t_bang;
444 static t_int *bang_tilde_perform(t_int *w)
446 t_bang *x = (t_bang *)(w[1]);
447 clock_delay(x->x_clock, 0);
448 return (w+2);
451 static void bang_tilde_dsp(t_bang *x, t_signal **sp)
453 dsp_add(bang_tilde_perform, 1, x);
456 static void bang_tilde_tick(t_bang *x)
458 outlet_bang(x->x_obj.ob_outlet);
461 static void bang_tilde_free(t_bang *x)
463 clock_free(x->x_clock);
466 static void *bang_tilde_new(t_symbol *s)
468 t_bang *x = (t_bang *)pd_new(bang_tilde_class);
469 x->x_clock = clock_new(x, (t_method)bang_tilde_tick);
470 outlet_new(&x->x_obj, &s_bang);
471 return (x);
474 static void bang_tilde_setup(void)
476 bang_tilde_class = class_new(gensym("bang~"), (t_newmethod)bang_tilde_new,
477 (t_method)bang_tilde_free, sizeof(t_bang), 0, 0);
478 class_addmethod(bang_tilde_class, (t_method)bang_tilde_dsp,
479 gensym("dsp"), 0);
482 /* ------------------------ samplerate~~ -------------------------- */
484 static t_class *samplerate_tilde_class;
486 typedef struct _samplerate
488 t_object x_obj;
489 } t_samplerate;
491 static void samplerate_tilde_bang(t_samplerate *x)
493 outlet_float(x->x_obj.ob_outlet, sys_getsr());
496 static void *samplerate_tilde_new(t_symbol *s)
498 t_samplerate *x = (t_samplerate *)pd_new(samplerate_tilde_class);
499 outlet_new(&x->x_obj, &s_float);
500 return (x);
503 static void samplerate_tilde_setup(void)
505 samplerate_tilde_class = class_new(gensym("samplerate~"),
506 (t_newmethod)samplerate_tilde_new, 0, sizeof(t_samplerate), 0, 0);
507 class_addbang(samplerate_tilde_class, samplerate_tilde_bang);
510 /* ------------------------ global setup routine ------------------------- */
512 void d_misc_setup(void)
514 #ifndef FIXEDPOINT
515 print_setup();
516 #endif
517 scope_setup();
518 bang_tilde_setup();
519 samplerate_tilde_setup();