diff options
author | Cyrille Henry <nusmuk@users.sourceforge.net> | 2015-09-16 16:59:34 +0000 |
---|---|---|
committer | Cyrille Henry <nusmuk@users.sourceforge.net> | 2015-09-16 16:59:34 +0000 |
commit | 85b2d2247f811c66cb4b2c4bd5f7f331ebdbe743 (patch) | |
tree | 91929a03ee9884973b09bb98b5559aa546e35a36 /nusmuk-utils/tab_derivate.c | |
parent | 297a520ab0d1f7084d0ac059e8576c631cd88720 (diff) |
new tab manipulation object : integration and derivative
svn path=/trunk/externals/nusmuk/; revision=17566
Diffstat (limited to 'nusmuk-utils/tab_derivate.c')
-rw-r--r-- | nusmuk-utils/tab_derivate.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/nusmuk-utils/tab_derivate.c b/nusmuk-utils/tab_derivate.c new file mode 100644 index 0000000..f035b99 --- /dev/null +++ b/nusmuk-utils/tab_derivate.c @@ -0,0 +1,84 @@ +#include "m_pd.h" + +#define MIN(a,b) (((a)<(b))?(a):(b)) + +static t_class *tab_derivate_class; + +typedef struct _tab_derivate { + t_object x_obj; + t_symbol *x_arrayname_src1; + t_symbol *x_arrayname_dst; + t_outlet *b_out; +} t_tab_derivate; + +void *tab_derivate_new(t_symbol *s_src1, t_symbol *s_dst) { + t_tab_derivate *x = (t_tab_derivate *)pd_new(tab_derivate_class); + + x->x_arrayname_src1 = s_src1; + x->x_arrayname_dst = s_dst; + + x->b_out = outlet_new(&x->x_obj, &s_bang); + + return (void *)x; +} + +void tab_derivate_compute(t_tab_derivate *x, t_float nb) { + t_garray *a_src1, *a_dst; + int npoints_src1, npoints_dst; + t_word *vec_src1, *vec_dst; + t_float tmp2, tmp1; + + int nb_derivate, i; + + if (!(a_src1 = (t_garray *)pd_findbyclass(x->x_arrayname_src1, garray_class))) + pd_error(x, "%s: no such array", x->x_arrayname_src1->s_name); + else if (!garray_getfloatwords(a_src1, &npoints_src1, &vec_src1)) + pd_error(x, "%s: bad template for tab_derivate", x->x_arrayname_src1->s_name); + else if (!(a_dst = (t_garray *)pd_findbyclass(x->x_arrayname_dst, garray_class))) + pd_error(x, "%s: no such array", x->x_arrayname_dst->s_name); + else if (!garray_getfloatwords(a_dst, &npoints_dst, &vec_dst)) + pd_error(x, "%s: bad template for tab_derivate", x->x_arrayname_dst->s_name); + else + { + nb_derivate = MIN(npoints_src1, npoints_dst); + // post("nb_derivate = %d",nb_derivate); + + if (nb > 0) + nb_derivate = MIN(nb_derivate, nb); + + tmp1 = 0; + for (i=0; i<nb_derivate;i++) { + tmp2 = vec_src1[i].w_float; + vec_dst[i].w_float = tmp2 - tmp1; + tmp1 = tmp2; + } + garray_redraw(a_dst); + } + outlet_bang(x->b_out); +} + +void tab_derivate_bang(t_tab_derivate *x) { + tab_derivate_compute(x, -1) ; +} + +void tab_derivate_float(t_tab_derivate *x, t_float nb) { + tab_derivate_compute(x, nb) ; +} + +void tab_derivate_src1(t_tab_derivate *x, t_symbol *s_src) { + x->x_arrayname_src1 = s_src; +} + +void tab_derivate_dst(t_tab_derivate *x, t_symbol *s_dst) { + x->x_arrayname_dst = s_dst; +} + +void tab_derivate_setup(void) { + tab_derivate_class = class_new(gensym("tab_derivate"), (t_newmethod)tab_derivate_new, + 0, sizeof(t_tab_derivate), CLASS_DEFAULT, A_DEFSYM, A_DEFSYM, A_DEFSYM, 0); + class_addbang(tab_derivate_class, tab_derivate_bang); + class_addfloat(tab_derivate_class, tab_derivate_float); + class_addmethod(tab_derivate_class, (t_method)tab_derivate_src1, gensym("src1"), A_DEFSYM, 0); + class_addmethod(tab_derivate_class, (t_method)tab_derivate_dst, gensym("dst"), A_DEFSYM, 0); +} + |