delta~.c 1.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include "m_pd.h"
#include <math.h>
#ifdef _MSC_VER
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif

/* ------------------------ delta~ ----------------------------- */

/* tilde object to take difference value. */

static t_class *delta_class;

typedef struct _delta
{
  t_object x_obj;
  t_sample x_last;
} t_delta;

static t_int *delta_perform(t_int *w)
{
  t_delta *x  = (t_delta *)(w[1]);
  t_float *in = (t_float *)(w[2]);
  t_float *out = (t_float *)(w[3]);
  int n = (int)(w[4]);
  while (n--)
    {
      float f = *(in++);
      // *out++ = (f > 0 ? f : -f);	
      *out++ = (f > x->x_last ? fabs(f - x->x_last) : -fabs(f - x->x_last));
      x->x_last = f;
    }
  return (w+5);
}

static void delta_dsp(t_delta *x, t_signal **sp)
{
  dsp_add(delta_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
}

static void *delta_new(void)
{
  t_delta *x = (t_delta *)pd_new(delta_class);
  x->x_last = 0.;
  outlet_new(&x->x_obj, gensym("signal"));
  return (x);
}

void delta_tilde_setup(void)
{
  delta_class = class_new(gensym("delta~"), (t_newmethod)delta_new, 0,
			  sizeof(t_delta), 0, A_DEFFLOAT, 0);
  class_addmethod(delta_class, nullfn, gensym("signal"), 0);
  class_addmethod(delta_class, (t_method)delta_dsp, gensym("dsp"), 0);
}