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_global.c
blob2b129ac5f4219f5978470ae17b7057cafce57fe3
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 /* send~, receive~, throw~, catch~ */
7 #include "m_pd.h"
8 #include <string.h>
10 #define DEFSENDVS 64 /* LATER get send to get this from canvas */
12 /* ----------------------------- send~ ----------------------------- */
13 static t_class *sigsend_class;
15 typedef struct _sigsend
17 t_object x_obj;
18 t_symbol *x_sym;
19 int x_n;
20 t_sample *x_vec;
21 float x_f;
22 } t_sigsend;
24 static void *sigsend_new(t_symbol *s)
26 t_sigsend *x = (t_sigsend *)pd_new(sigsend_class);
27 pd_bind(&x->x_obj.ob_pd, s);
28 x->x_sym = s;
29 x->x_n = DEFSENDVS;
30 x->x_vec = (t_sample *)getbytes(DEFSENDVS * sizeof(t_sample));
31 memset((char *)(x->x_vec), 0, DEFSENDVS * sizeof(t_sample));
32 x->x_f = 0;
33 return (x);
36 static t_int *sigsend_perform(t_int *w)
38 t_sample *in = (t_sample *)(w[1]);
39 t_sample *out = (t_sample *)(w[2]);
40 int n = (int)(w[3]);
41 while (n--)
43 *out = (PD_BIGORSMALL(*in) ? 0 : *in);
44 out++;
45 in++;
47 return (w+4);
50 static void sigsend_dsp(t_sigsend *x, t_signal **sp)
52 if (x->x_n == sp[0]->s_n)
53 dsp_add(sigsend_perform, 3, sp[0]->s_vec, x->x_vec, sp[0]->s_n);
54 else error("sigsend %s: unexpected vector size", x->x_sym->s_name);
57 static void sigsend_free(t_sigsend *x)
59 pd_unbind(&x->x_obj.ob_pd, x->x_sym);
60 freebytes(x->x_vec, x->x_n * sizeof(float));
63 static void sigsend_setup(void)
65 sigsend_class = class_new(gensym("send~"), (t_newmethod)sigsend_new,
66 (t_method)sigsend_free, sizeof(t_sigsend), 0, A_DEFSYM, 0);
67 class_addcreator((t_newmethod)sigsend_new, gensym("s~"), A_DEFSYM, 0);
68 CLASS_MAINSIGNALIN(sigsend_class, t_sigsend, x_f);
69 class_addmethod(sigsend_class, (t_method)sigsend_dsp, gensym("dsp"), 0);
72 /* ----------------------------- receive~ ----------------------------- */
73 static t_class *sigreceive_class;
75 typedef struct _sigreceive
77 t_object x_obj;
78 t_symbol *x_sym;
79 t_sample *x_wherefrom;
80 int x_n;
81 } t_sigreceive;
83 static void *sigreceive_new(t_symbol *s)
85 t_sigreceive *x = (t_sigreceive *)pd_new(sigreceive_class);
86 x->x_n = DEFSENDVS; /* LATER find our vector size correctly */
87 x->x_sym = s;
88 x->x_wherefrom = 0;
89 outlet_new(&x->x_obj, &s_signal);
90 return (x);
93 static t_int *sigreceive_perform(t_int *w)
95 t_sigreceive *x = (t_sigreceive *)(w[1]);
96 t_sample *out = (t_sample *)(w[2]);
97 int n = (int)(w[3]);
98 t_sample *in = x->x_wherefrom;
99 if (in)
101 while (n--)
102 *out++ = *in++;
104 else
106 while (n--)
107 *out++ = 0;
109 return (w+4);
112 static void sigreceive_set(t_sigreceive *x, t_symbol *s)
114 t_sigsend *sender = (t_sigsend *)pd_findbyclass((x->x_sym = s),
115 sigsend_class);
116 if (sender)
118 if (sender->x_n == x->x_n)
119 x->x_wherefrom = sender->x_vec;
120 else
122 pd_error(x, "receive~ %s: vector size mismatch", x->x_sym->s_name);
123 x->x_wherefrom = 0;
126 else
128 pd_error(x, "receive~ %s: no matching send", x->x_sym->s_name);
129 x->x_wherefrom = 0;
133 static void sigreceive_dsp(t_sigreceive *x, t_signal **sp)
135 if (sp[0]->s_n != x->x_n)
137 pd_error(x, "receive~ %s: vector size mismatch", x->x_sym->s_name);
139 else
141 sigreceive_set(x, x->x_sym);
142 dsp_add(sigreceive_perform, 3,
143 x, sp[0]->s_vec, sp[0]->s_n);
147 static void sigreceive_setup(void)
149 sigreceive_class = class_new(gensym("receive~"),
150 (t_newmethod)sigreceive_new, 0,
151 sizeof(t_sigreceive), 0, A_DEFSYM, 0);
152 class_addcreator((t_newmethod)sigreceive_new, gensym("r~"), A_DEFSYM, 0);
153 class_addmethod(sigreceive_class, (t_method)sigreceive_set, gensym("set"),
154 A_SYMBOL, 0);
155 class_addmethod(sigreceive_class, (t_method)sigreceive_dsp, gensym("dsp"),
157 class_sethelpsymbol(sigreceive_class, gensym("send~"));
160 /* ----------------------------- catch~ ----------------------------- */
161 static t_class *sigcatch_class;
163 typedef struct _sigcatch
165 t_object x_obj;
166 t_symbol *x_sym;
167 int x_n;
168 t_sample *x_vec;
169 } t_sigcatch;
171 static void *sigcatch_new(t_symbol *s)
173 t_sigcatch *x = (t_sigcatch *)pd_new(sigcatch_class);
174 pd_bind(&x->x_obj.ob_pd, s);
175 x->x_sym = s;
176 x->x_n = DEFSENDVS;
177 x->x_vec = (t_sample *)getbytes(DEFSENDVS * sizeof(t_sample));
178 memset((char *)(x->x_vec), 0, DEFSENDVS * sizeof(t_sample));
179 outlet_new(&x->x_obj, &s_signal);
180 return (x);
183 static t_int *sigcatch_perform(t_int *w)
185 t_sample *in = (t_sample *)(w[1]);
186 t_sample *out = (t_sample *)(w[2]);
187 int n = (int)(w[3]);
188 while (n--) *out++ = *in, *in++ = 0;
189 return (w+4);
192 static void sigcatch_dsp(t_sigcatch *x, t_signal **sp)
194 if (x->x_n == sp[0]->s_n)
195 dsp_add(sigcatch_perform, 3, x->x_vec, sp[0]->s_vec, sp[0]->s_n);
196 else error("sigcatch %s: unexpected vector size", x->x_sym->s_name);
199 static void sigcatch_free(t_sigcatch *x)
201 pd_unbind(&x->x_obj.ob_pd, x->x_sym);
202 freebytes(x->x_vec, x->x_n * sizeof(float));
205 static void sigcatch_setup(void)
207 sigcatch_class = class_new(gensym("catch~"), (t_newmethod)sigcatch_new,
208 (t_method)sigcatch_free, sizeof(t_sigcatch), CLASS_NOINLET, A_DEFSYM, 0);
209 class_addmethod(sigcatch_class, (t_method)sigcatch_dsp, gensym("dsp"), 0);
210 class_sethelpsymbol(sigcatch_class, gensym("throw~"));
213 /* ----------------------------- throw~ ----------------------------- */
214 static t_class *sigthrow_class;
216 typedef struct _sigthrow
218 t_object x_obj;
219 t_symbol *x_sym;
220 t_sample *x_whereto;
221 int x_n;
222 t_float x_f;
223 } t_sigthrow;
225 static void *sigthrow_new(t_symbol *s)
227 t_sigthrow *x = (t_sigthrow *)pd_new(sigthrow_class);
228 x->x_sym = s;
229 x->x_whereto = 0;
230 x->x_n = DEFSENDVS;
231 x->x_f = 0;
232 return (x);
235 static t_int *sigthrow_perform(t_int *w)
237 t_sigthrow *x = (t_sigthrow *)(w[1]);
238 t_sample *in = (t_sample *)(w[2]);
239 int n = (int)(w[3]);
240 t_sample *out = x->x_whereto;
241 if (out)
243 while (n--)
245 *out += (PD_BIGORSMALL(*in) ? 0 : *in);
246 out++;
247 in++;
250 return (w+4);
253 static void sigthrow_set(t_sigthrow *x, t_symbol *s)
255 t_sigcatch *catcher = (t_sigcatch *)pd_findbyclass((x->x_sym = s),
256 sigcatch_class);
257 if (catcher)
259 if (catcher->x_n == x->x_n)
260 x->x_whereto = catcher->x_vec;
261 else
263 pd_error(x, "throw~ %s: vector size mismatch", x->x_sym->s_name);
264 x->x_whereto = 0;
267 else
269 pd_error(x, "throw~ %s: no matching catch", x->x_sym->s_name);
270 x->x_whereto = 0;
274 static void sigthrow_dsp(t_sigthrow *x, t_signal **sp)
276 if (sp[0]->s_n != x->x_n)
278 pd_error(x, "throw~ %s: vector size mismatch", x->x_sym->s_name);
280 else
282 sigthrow_set(x, x->x_sym);
283 dsp_add(sigthrow_perform, 3,
284 x, sp[0]->s_vec, sp[0]->s_n);
288 static void sigthrow_setup(void)
290 sigthrow_class = class_new(gensym("throw~"), (t_newmethod)sigthrow_new, 0,
291 sizeof(t_sigthrow), 0, A_DEFSYM, 0);
292 class_addcreator((t_newmethod)sigthrow_new, gensym("r~"), A_DEFSYM, 0);
293 class_addmethod(sigthrow_class, (t_method)sigthrow_set, gensym("set"),
294 A_SYMBOL, 0);
295 CLASS_MAINSIGNALIN(sigthrow_class, t_sigthrow, x_f);
296 class_addmethod(sigthrow_class, (t_method)sigthrow_dsp, gensym("dsp"), 0);
299 /* ----------------------- global setup routine ---------------- */
301 void d_global_setup(void)
303 sigsend_setup();
304 sigreceive_setup();
305 sigcatch_setup();
306 sigthrow_setup();
309 /* Copyright (c) 1997-1999 Miller Puckette.
310 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
311 * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
313 /* send~, receive~, throw~, catch~ */
315 #include "m_pd.h"
316 #include <string.h>
318 #define DEFSENDVS 64 /* LATER get send to get this from canvas */
320 /* ----------------------------- send~ ----------------------------- */
321 static t_class *sigsend_class;
323 typedef struct _sigsend
325 t_object x_obj;
326 t_symbol *x_sym;
327 int x_n;
328 t_sample *x_vec;
329 float x_f;
330 } t_sigsend;
332 static void *sigsend_new(t_symbol *s)
334 t_sigsend *x = (t_sigsend *)pd_new(sigsend_class);
335 pd_bind(&x->x_obj.ob_pd, s);
336 x->x_sym = s;
337 x->x_n = DEFSENDVS;
338 x->x_vec = (t_sample *)getbytes(DEFSENDVS * sizeof(t_sample));
339 memset((char *)(x->x_vec), 0, DEFSENDVS * sizeof(t_sample));
340 x->x_f = 0;
341 return (x);
344 static t_int *sigsend_perform(t_int *w)
346 t_sample *in = (t_sample *)(w[1]);
347 t_sample *out = (t_sample *)(w[2]);
348 int n = (int)(w[3]);
349 while (n--)
351 *out = (PD_BIGORSMALL(*in) ? 0 : *in);
352 out++;
353 in++;
355 return (w+4);
358 static void sigsend_dsp(t_sigsend *x, t_signal **sp)
360 if (x->x_n == sp[0]->s_n)
361 dsp_add(sigsend_perform, 3, sp[0]->s_vec, x->x_vec, sp[0]->s_n);
362 else error("sigsend %s: unexpected vector size", x->x_sym->s_name);
365 static void sigsend_free(t_sigsend *x)
367 pd_unbind(&x->x_obj.ob_pd, x->x_sym);
368 freebytes(x->x_vec, x->x_n * sizeof(float));
371 static void sigsend_setup(void)
373 sigsend_class = class_new(gensym("send~"), (t_newmethod)sigsend_new,
374 (t_method)sigsend_free, sizeof(t_sigsend), 0, A_DEFSYM, 0);
375 class_addcreator((t_newmethod)sigsend_new, gensym("s~"), A_DEFSYM, 0);
376 CLASS_MAINSIGNALIN(sigsend_class, t_sigsend, x_f);
377 class_addmethod(sigsend_class, (t_method)sigsend_dsp, gensym("dsp"), 0);
380 /* ----------------------------- receive~ ----------------------------- */
381 static t_class *sigreceive_class;
383 typedef struct _sigreceive
385 t_object x_obj;
386 t_symbol *x_sym;
387 t_sample *x_wherefrom;
388 int x_n;
389 } t_sigreceive;
391 static void *sigreceive_new(t_symbol *s)
393 t_sigreceive *x = (t_sigreceive *)pd_new(sigreceive_class);
394 x->x_n = DEFSENDVS; /* LATER find our vector size correctly */
395 x->x_sym = s;
396 x->x_wherefrom = 0;
397 outlet_new(&x->x_obj, &s_signal);
398 return (x);
401 static t_int *sigreceive_perform(t_int *w)
403 t_sigreceive *x = (t_sigreceive *)(w[1]);
404 t_sample *out = (t_sample *)(w[2]);
405 int n = (int)(w[3]);
406 t_sample *in = x->x_wherefrom;
407 if (in)
409 while (n--)
410 *out++ = *in++;
412 else
414 while (n--)
415 *out++ = 0;
417 return (w+4);
420 static void sigreceive_set(t_sigreceive *x, t_symbol *s)
422 t_sigsend *sender = (t_sigsend *)pd_findbyclass((x->x_sym = s),
423 sigsend_class);
424 if (sender)
426 if (sender->x_n == x->x_n)
427 x->x_wherefrom = sender->x_vec;
428 else
430 pd_error(x, "receive~ %s: vector size mismatch", x->x_sym->s_name);
431 x->x_wherefrom = 0;
434 else
436 pd_error(x, "receive~ %s: no matching send", x->x_sym->s_name);
437 x->x_wherefrom = 0;
441 static void sigreceive_dsp(t_sigreceive *x, t_signal **sp)
443 if (sp[0]->s_n != x->x_n)
445 pd_error(x, "receive~ %s: vector size mismatch", x->x_sym->s_name);
447 else
449 sigreceive_set(x, x->x_sym);
450 dsp_add(sigreceive_perform, 3,
451 x, sp[0]->s_vec, sp[0]->s_n);
455 static void sigreceive_setup(void)
457 sigreceive_class = class_new(gensym("receive~"),
458 (t_newmethod)sigreceive_new, 0,
459 sizeof(t_sigreceive), 0, A_DEFSYM, 0);
460 class_addcreator((t_newmethod)sigreceive_new, gensym("r~"), A_DEFSYM, 0);
461 class_addmethod(sigreceive_class, (t_method)sigreceive_set, gensym("set"),
462 A_SYMBOL, 0);
463 class_addmethod(sigreceive_class, (t_method)sigreceive_dsp, gensym("dsp"),
465 class_sethelpsymbol(sigreceive_class, gensym("send~"));
468 /* ----------------------------- catch~ ----------------------------- */
469 static t_class *sigcatch_class;
471 typedef struct _sigcatch
473 t_object x_obj;
474 t_symbol *x_sym;
475 int x_n;
476 t_sample *x_vec;
477 } t_sigcatch;
479 static void *sigcatch_new(t_symbol *s)
481 t_sigcatch *x = (t_sigcatch *)pd_new(sigcatch_class);
482 pd_bind(&x->x_obj.ob_pd, s);
483 x->x_sym = s;
484 x->x_n = DEFSENDVS;
485 x->x_vec = (t_sample *)getbytes(DEFSENDVS * sizeof(t_sample));
486 memset((char *)(x->x_vec), 0, DEFSENDVS * sizeof(t_sample));
487 outlet_new(&x->x_obj, &s_signal);
488 return (x);
491 static t_int *sigcatch_perform(t_int *w)
493 t_sample *in = (t_sample *)(w[1]);
494 t_sample *out = (t_sample *)(w[2]);
495 int n = (int)(w[3]);
496 while (n--) *out++ = *in, *in++ = 0;
497 return (w+4);
500 static void sigcatch_dsp(t_sigcatch *x, t_signal **sp)
502 if (x->x_n == sp[0]->s_n)
503 dsp_add(sigcatch_perform, 3, x->x_vec, sp[0]->s_vec, sp[0]->s_n);
504 else error("sigcatch %s: unexpected vector size", x->x_sym->s_name);
507 static void sigcatch_free(t_sigcatch *x)
509 pd_unbind(&x->x_obj.ob_pd, x->x_sym);
510 freebytes(x->x_vec, x->x_n * sizeof(float));
513 static void sigcatch_setup(void)
515 sigcatch_class = class_new(gensym("catch~"), (t_newmethod)sigcatch_new,
516 (t_method)sigcatch_free, sizeof(t_sigcatch), CLASS_NOINLET, A_DEFSYM, 0);
517 class_addmethod(sigcatch_class, (t_method)sigcatch_dsp, gensym("dsp"), 0);
518 class_sethelpsymbol(sigcatch_class, gensym("throw~"));
521 /* ----------------------------- throw~ ----------------------------- */
522 static t_class *sigthrow_class;
524 typedef struct _sigthrow
526 t_object x_obj;
527 t_symbol *x_sym;
528 t_sample *x_whereto;
529 int x_n;
530 t_float x_f;
531 } t_sigthrow;
533 static void *sigthrow_new(t_symbol *s)
535 t_sigthrow *x = (t_sigthrow *)pd_new(sigthrow_class);
536 x->x_sym = s;
537 x->x_whereto = 0;
538 x->x_n = DEFSENDVS;
539 x->x_f = 0;
540 return (x);
543 static t_int *sigthrow_perform(t_int *w)
545 t_sigthrow *x = (t_sigthrow *)(w[1]);
546 t_sample *in = (t_sample *)(w[2]);
547 int n = (int)(w[3]);
548 t_sample *out = x->x_whereto;
549 if (out)
551 while (n--)
553 *out += (PD_BIGORSMALL(*in) ? 0 : *in);
554 out++;
555 in++;
558 return (w+4);
561 static void sigthrow_set(t_sigthrow *x, t_symbol *s)
563 t_sigcatch *catcher = (t_sigcatch *)pd_findbyclass((x->x_sym = s),
564 sigcatch_class);
565 if (catcher)
567 if (catcher->x_n == x->x_n)
568 x->x_whereto = catcher->x_vec;
569 else
571 pd_error(x, "throw~ %s: vector size mismatch", x->x_sym->s_name);
572 x->x_whereto = 0;
575 else
577 pd_error(x, "throw~ %s: no matching catch", x->x_sym->s_name);
578 x->x_whereto = 0;
582 static void sigthrow_dsp(t_sigthrow *x, t_signal **sp)
584 if (sp[0]->s_n != x->x_n)
586 pd_error(x, "throw~ %s: vector size mismatch", x->x_sym->s_name);
588 else
590 sigthrow_set(x, x->x_sym);
591 dsp_add(sigthrow_perform, 3,
592 x, sp[0]->s_vec, sp[0]->s_n);
596 static void sigthrow_setup(void)
598 sigthrow_class = class_new(gensym("throw~"), (t_newmethod)sigthrow_new, 0,
599 sizeof(t_sigthrow), 0, A_DEFSYM, 0);
600 class_addcreator((t_newmethod)sigthrow_new, gensym("r~"), A_DEFSYM, 0);
601 class_addmethod(sigthrow_class, (t_method)sigthrow_set, gensym("set"),
602 A_SYMBOL, 0);
603 CLASS_MAINSIGNALIN(sigthrow_class, t_sigthrow, x_f);
604 class_addmethod(sigthrow_class, (t_method)sigthrow_dsp, gensym("dsp"), 0);
607 /* ----------------------- global setup routine ---------------- */
609 void d_global_setup(void)
611 sigsend_setup();
612 sigreceive_setup();
613 sigcatch_setup();
614 sigthrow_setup();