aboutsummaryrefslogtreecommitdiff
path: root/src/multiline~.c
blob: 70c62e9be57777e43304dcc1fcdc44c29deeb3ca (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/******************************************************
 *
 * zexy - implementation file
 *
 * copyleft (c) IOhannes m zmölnig
 *
 *   1999:forum::für::umläute:2004
 *
 *   institute of electronic music and acoustics (iem)
 *
 ******************************************************
 *
 * license: GNU General Public License v.2
 *
 ******************************************************/

/*
  a multiline that MULTIplicates MULTIple signals with "ramped floats" (--> like "line~")

  this is kind of multiplying some streams with the square diagonal matrix : diag*~
  for smooth-results we do this line~ thing

  1403:forum::für::umläute:2001
*/


/* i am not sure, whether there is a difference between loop-unrolling by hand or by compiler
 * i somehow have the feeling, that doing it by hand increases(!) performance for about 2%
 * anyhow, if you think that the compiler can do this job better, just disable the UNROLLED define below
 */
#define UNROLLED

#include "zexy.h"

/* --------------------------- multiline~ ---------------------------------- */

static t_class *mline_class;

typedef struct _mline {
  t_object x_obj;

  t_float time;

  int ticksleft;
  int retarget;

  t_float msec2tick;

  t_float *value;
  t_float *target;
  t_float *increment; /* single precision is really a bad */

  t_float **sigIN;
  t_float **sigOUT;
  t_float  *sigBUF;
  int       sigNUM;

} t_mline;

/* the message thing */

static void mline_list(t_mline *x, t_symbol *s, int argc, t_atom *argv)
{
  ZEXY_USEVAR(s);
  if (argc>x->sigNUM)x->time=atom_getfloat(argv+argc-1);

  if (x->time <= 0) {
    if (argc==1) {
      t_float f = atom_getfloat(argv);
      int i=x->sigNUM;
      while(i--)x->target[i]=x->value[i]=f;
    } else {
      int offset = (argc<x->sigNUM)?x->sigNUM-argc:0;
      int i=offset?argc:x->sigNUM;
      while(i--)x->target[i+offset]=x->value[i+offset]=atom_getfloat(argv++);
    }
    x->ticksleft=x->retarget=x->time=0;
  } else {
    if (argc==1) {
      int i = x->sigNUM;
      t_float f = atom_getfloat(argv);
      for(i=0;i<x->sigNUM;i++)x->target[i]=f;
    } else {
      int offset = (argc<x->sigNUM)?x->sigNUM-argc:0;
      int i=offset?argc:x->sigNUM;
      while(i--)x->target[i+offset]=atom_getfloat(argv++);
    }
    x->retarget = 1;
  }
}

static void mline_stop(t_mline *x)
{
  int i = x->sigNUM;
  while (i--) x->target[i] = x->value[i];
  x->ticksleft = x->retarget = 0;
}

/* the dsp thing */

static t_int *mline_perform(t_int *w)
{
  t_mline *x = (t_mline *)(w[1]);
  int n = (int)(w[2]);

  t_float **out = x->sigOUT;
  t_float **in  = x->sigIN;
  t_float  *buf = x->sigBUF, *sigBUF = buf;
  t_float  *inc = x->increment, *increment = inc;
  t_float  *val = x->value, *value = val;
  t_float  *tgt = x->target, *target = tgt;

  int sigNUM = x->sigNUM;

  if (x->retarget) {
    int nticks = x->time * x->msec2tick;

    if (!nticks) nticks = 1;
    x->ticksleft = nticks;

    x->retarget = 0;
  }

  if (x->ticksleft) {
    int N=n-1;
    t_float oneovernos = 1./(x->ticksleft*n);

    int i=sigNUM;
    while(i--)*inc++=(*tgt++-*val++)*oneovernos;
    
    n=-1;
    while (n++<N) {
      buf = sigBUF;
      val = value;
      inc = increment;

      i = sigNUM;
      while (i--)*buf++=in[i][n]*(*val++ += *inc++);
      i=sigNUM;
      buf=sigBUF;
      while (i--)out[i][n]=*buf++;
    }

    if (!--x->ticksleft) {
      val = value;
      tgt = target;
      i = sigNUM;
      while(i--)*val++=*tgt++;
    }
      
  } else { /* no ticks left */
    int i = sigNUM;
    while (n--) {
      i = sigNUM;
      val = value;
      buf = sigBUF;
      while (i--)*buf++=in[i][n]**val++;
      i = sigNUM;
      buf = sigBUF;
      while (i--)out[i][n]=*buf++;
    }
  }

  return (w+3);
}



static void mline_dsp(t_mline *x, t_signal **sp)
{
  int i = x->sigNUM, n = 0;
  t_float **dummy = x->sigIN;
  while(i--)*dummy++=sp[n++]->s_vec;

  i = x->sigNUM;
  dummy =x->sigOUT;
  while(i--)*dummy++=sp[n++]->s_vec;

  x->msec2tick = sp[0]->s_sr / (1000.f * sp[0]->s_n);
  dsp_add(mline_perform, 2, x, sp[0]->s_n);
}


/* setup/setdown things */

static void mline_free(t_mline *x)
{
  freebytes(x->value,     sizeof(x->value));
  freebytes(x->target,    sizeof(x->target));
  freebytes(x->increment, sizeof(x->increment));

  freebytes(x->sigIN,     sizeof(x->sigIN));
  freebytes(x->sigOUT,    sizeof(x->sigOUT));
  freebytes(x->sigBUF,    sizeof(x->sigBUF));
}


static void *mline_new(t_symbol *s, int argc, t_atom *argv)
{
    t_mline *x = (t_mline *)pd_new(mline_class);
    int i;
    ZEXY_USEVAR(s);

    if (!argc) {
      argc = 1;
      x->time = 0;
    } else {
      x->time = atom_getfloat(argv+argc-1);
      if (x->time < 0) x->time = 0;

      argc--;
      if (!argc) argc = 1;
    }

    x->sigNUM = argc;

    i = argc-1;

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

    while (i--) {
      inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
      outlet_new(&x->x_obj, &s_signal);
    }

    inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym(""));
    floatinlet_new(&x->x_obj, &x->time);
    
    x->sigIN  = (t_float **)getbytes(x->sigNUM * sizeof(t_float **));
    x->sigOUT = (t_float **)getbytes(x->sigNUM * sizeof(t_float **));
    x->sigBUF = (t_float  *)getbytes(x->sigNUM * sizeof(t_float  *));

    x->value     = (t_float *)getbytes(x->sigNUM * sizeof(t_float *));
    x->target    = (t_float *)getbytes(x->sigNUM * sizeof(t_float *));
    x->increment = (t_float *)getbytes(x->sigNUM * sizeof(t_float *));

    i = x->sigNUM;

    while (i--) {
      x->sigIN[i] = x->sigOUT[i] = 0;
      x->increment[i] = 0;
      x->value[x->sigNUM-i-1] = x->target[x->sigNUM-i-1] = atom_getfloat(argv+i);
    }

    x->msec2tick = x->ticksleft = x->retarget = 0;

    return (x);
}



void multiline_tilde_setup(void)
{
  mline_class = class_new(gensym("multiline~"), (t_newmethod)mline_new, (t_method)mline_free,
			  sizeof(t_mline), 0, A_GIMME, 0);

  class_addmethod(mline_class, (t_method)mline_dsp, gensym("dsp"), 0);
  class_addmethod(mline_class, nullfn, gensym("signal"), 0);

  class_addmethod(mline_class, (t_method)mline_list, gensym(""), A_GIMME, 0);
  class_addmethod(mline_class, (t_method)mline_stop, gensym("stop"), 0);

  class_sethelpsymbol(mline_class, gensym("zexy/multiline~"));
  zexy_register("multiline_tilde");
}