/* 
 * Mix the signals on the first two inlets where the third inlet
 * (or argument) provides the attenuation factor of the second signal.
 * y := a*x_1 + (1-a)*x_2
 * 
 * 2006-03-27	Thomas Strathmann <thomas@pdp7.org>
 */
#include "m_pd.h"

static t_class *mix_tilde_class;

typedef struct _mix_tilde {
  t_object  x_obj;
  t_sample f_mix;
  t_sample f;
} t_mix_tilde;


t_int *mix_tilde_perform(t_int *w) {
  t_mix_tilde *x = (t_mix_tilde *)(w[1]);
  t_sample  *in1 =    (t_sample *)(w[2]);
  t_sample  *in2 =    (t_sample *)(w[3]);
  t_sample  *out =    (t_sample *)(w[4]);
  int          n =           (int)(w[5]);

  while (n--) {
    float x1 = *(in1++);
    float x2 = *(in2++);
    *(out++) = x->f_mix*x1 + (1.0 - x->f_mix)*x2;
  }

  return (w+6);
}

void mix_tilde_dsp(t_mix_tilde *x, t_signal **sp) {
  dsp_add(mix_tilde_perform, 5, x,
          sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);
}

void *mix_tilde_new(t_floatarg f) {
  t_mix_tilde *x = (t_mix_tilde *)pd_new(mix_tilde_class);
  x->f_mix = f;
  inlet_new (&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
  floatinlet_new (&x->x_obj, &x->f_mix);
  outlet_new(&x->x_obj, &s_signal);
  return (void *)x;
}

void mix_tilde_setup(void) {
  mix_tilde_class = class_new(gensym("mix~"),
        (t_newmethod)mix_tilde_new,
        0, sizeof(t_mix_tilde),
        CLASS_DEFAULT, 
        A_DEFFLOAT, 0);

  class_addmethod(mix_tilde_class,
        (t_method)mix_tilde_dsp, gensym("dsp"), 0);
  CLASS_MAINSIGNALIN(mix_tilde_class, t_mix_tilde, f);
}
