aboutsummaryrefslogtreecommitdiff
path: root/signal/mixer~.c
blob: 5d90285306270f34d5e03b0c96b4dd193bc3dfa1 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* (C) Oswald Berthold  <opt@web.fm> */


#include "math.h"
#include <m_pd.h>

/* ----------------------------- mixer ----------------------------- */
static t_class *mixer_class;



typedef struct _mixer
{
  t_object x_obj;
  t_int x_n;
  t_float* x_m;
} t_mixer;

static void *mixer_new(t_symbol *s, t_floatarg num)
{
  int i;
  t_mixer *x = (t_mixer *)pd_new(mixer_class);
  if (num < 1)  x->x_n = 1;
  else x->x_n = (int) num;
	
  x->x_m = getbytes(sizeof(t_float)*x->x_n);

  for (i=0;i<x->x_n /* - 1 */ ;i++) {
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
    x->x_m[i] = 1.;
  }	

  

  outlet_new(&x->x_obj, &s_signal);
  return (x);
}

void mixer_list(t_mixer* x,t_symbol* s,t_int argc, t_atom* argv) 
{
  int chan;
  t_float val;

  if (argc != 2) {
    post("unknown");
    return;
  }

  chan = (int) atom_getfloat(argv++);
  val =   atom_getfloat(argv++);


  x->x_m[chan] = val;

}


t_int *mixer_perform(t_int *w)
{
  t_mixer* x = (t_mixer*) (w[1]);
  int n = (int)(w[2]);
  t_float **in = getbytes(sizeof(float)*x->x_n);
  t_float *out;
  int i,j;
  int offset = 3;

  for (i=0;i < x->x_n;i++) {
    in[i] = (t_float *)(w[offset+i]);
  }

  out = (t_float *)(w[offset+i]);
  
  while (n--) {
    *out = 0.;
    for (j=0;j<x->x_n;j++) {
      *out += *(in[j]++) * x->x_m[j];
    }
    out++;
  }
  return (w+offset+1+i);
}

static void mixer_dsp(t_mixer *x, t_signal **sp)
{
  int i;
  t_int** myvec = getbytes(sizeof(t_int)*(x->x_n + 3));
  
  myvec[0] = (t_int*)x;
  myvec[1] = (t_int*)sp[0]->s_n;

  for (i=0;i < x->x_n+1;i++)
    myvec[2 + i] = (t_int*)sp[i]->s_vec;

  dsp_addv(mixer_perform, x->x_n + 3, (t_int*)myvec);
  freebytes(myvec,sizeof(t_int)*(x->x_n + 3));
}

void mixer_tilde_setup(void)
{
  mixer_class = class_new(gensym("mixer~"), (t_newmethod)mixer_new, 0,
			  sizeof(t_mixer), 0, A_DEFFLOAT, A_DEFSYM,A_NULL);
  class_addmethod(mixer_class, nullfn, gensym("signal"), 0);
  class_addmethod(mixer_class, (t_method)mixer_dsp, gensym("dsp"), 0);
  
  class_addlist(mixer_class,mixer_list);
}