aboutsummaryrefslogtreecommitdiff
path: root/iem_t3_lib/src/t3_line~.c
blob: 8d386f0f09b63f125ad4940188c213f1401e16a3 (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
266
267
268
269
270
271
272
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.

iem_t3_lib written by Gerhard Eckel, Thomas Musil, Copyright (c) IEM KUG Graz Austria 2000 - 2006 */


#include "m_pd.h"
#include "iemlib.h"

/* -------------------------- t3_line~ ------------------------------ */
static t_class *t3_line_tilde_class;

typedef struct _t3_line_tilde
{
  t_object x_obj;
  t_clock  *x_clock;
  t_float  *x_beg;
  double   x_cur_val;
  double   x_dst_val;
  double   x_inlet_val;
  double   x_inc64;
  double   x_inc;
  double   x_ms2samps;
  double   x_ticks2ms;
  double   x_inlet_time;
  double   x_dst_time;
  int      x_cur_samps;
  int      x_dur_samps;
  int      x_n;
  int      x_t3_bang_samps;
  int      x_transient;
} t_t3_line_tilde;

static void t3_line_tilde_nontransient(t_float *vec, t_t3_line_tilde *x, int n)
{
  int cur_samps = x->x_cur_samps, i;
  double inc = x->x_inc;
  double cur_val = x->x_cur_val;
  
  if(cur_samps)
  {
    if(cur_samps > n)
    {
      x->x_cur_samps -= n;
      while(n--)
      {
        cur_val += inc;
        *vec++ = (t_float)cur_val;
      }
      x->x_cur_val += x->x_inc64;
    }
    else if(cur_samps == n)
    {
      x->x_cur_samps = 0;
      while(n--)
      {
        cur_val += inc;
        *vec++ = (t_float)cur_val;
      }
      x->x_cur_val = x->x_dst_val;
    }
    else
    {
      for(i=0; i<cur_samps; i++)
      {
        cur_val += inc;
        *vec++ = (t_float)cur_val;
      }
      x->x_cur_val = cur_val = x->x_dst_val;
      for(i=cur_samps; i<n; i++)
        *vec++ = (t_float)cur_val;
      x->x_cur_samps = 0;
    }
  }
  else
  {
    while(n--)
      *vec++ = (t_float)cur_val;
  }
}

static t_int *t3_line_tilde_perform(t_int *w)
{
  t_float *out = (t_float *)(w[1]);
  t_t3_line_tilde *x = (t_t3_line_tilde *)(w[2]);
  int n = (int)(w[3]);
  
  if(x->x_transient)
  {
    t_float *trans = x->x_beg;
    
    while(n--)
      *out++ = *trans++;
    x->x_transient = 0;
  }
  else
    t3_line_tilde_nontransient(out, x, n);
  return(w+4);
}

static void t3_line_tilde_tick(t_t3_line_tilde *x)
{
  t_float *trans = x->x_beg;
  int n = x->x_n, t3_bang_samps, cur_samps, i;
  double inc, cur_val;
  
  if(!x->x_transient)
    t3_line_tilde_nontransient(trans, x, n);
  t3_bang_samps = x->x_t3_bang_samps;
  x->x_dst_val = x->x_inlet_val;
  if(x->x_inlet_time <= 0.0)
  {
    x->x_inlet_time = 0.0;
    x->x_dst_time = 0.0;
    x->x_dur_samps = 0;
    x->x_cur_samps = 0;
    cur_val = x->x_cur_val = x->x_dst_val;
    for(i=t3_bang_samps; i<n; i++)
      trans[i] = (t_float)cur_val;
  }
  else
  {
    int diff, end;
    
    x->x_dst_time = x->x_inlet_time;
    x->x_inlet_time = 0.0;
    cur_samps = (int)(x->x_dst_time * x->x_ms2samps);
    if(!cur_samps)
      cur_samps = 1;
    x->x_dur_samps = cur_samps;
    x->x_cur_samps = cur_samps;
    cur_val = x->x_cur_val = (double)trans[t3_bang_samps];
    inc = x->x_inc = (x->x_dst_val - cur_val)/(double)cur_samps;
    x->x_inc64 = (double)x->x_n * inc;
    diff = n - t3_bang_samps;
    if(cur_samps > diff)
    {
      for(i=t3_bang_samps; i<n; i++)
      {
        cur_val += inc;
        trans[i] = (t_float)cur_val;
      }
      x->x_cur_val += (double)diff * inc;
      x->x_cur_samps -= diff;
    }
    else if(cur_samps == diff)
    {
      for(i=t3_bang_samps; i<n; i++)
      {
        cur_val += inc;
        trans[i] = (t_float)cur_val;
      }
      x->x_cur_val = x->x_dst_val;
      x->x_cur_samps = 0;
    }
    else
    {
      end = t3_bang_samps + cur_samps;
      for(i=t3_bang_samps; i<end; i++)
      {
        cur_val += inc;
        trans[i] = (t_float)cur_val;
      }
      cur_val = x->x_cur_val = x->x_dst_val;
      x->x_cur_samps = 0;
      for(i=end; i<n; i++)
        trans[i] = (t_float)cur_val;
    }
  }
  x->x_transient = 1;
}

static void t3_line_tilde_list(t_t3_line_tilde *x, t_symbol *s, int ac, t_atom *av)
{
  if((ac >= 2)&&IS_A_FLOAT(av,0)&&IS_A_FLOAT(av,1))
  {
    int t3_bang_samps, ticks;
    double time;
    
    x->x_inlet_val = (double)atom_getfloatarg(1, ac, av);
    t3_bang_samps = (int)((t_float)atom_getfloatarg(0, ac, av)*x->x_ms2samps);
    if(t3_bang_samps < 0)
      t3_bang_samps = 0;
    ticks = t3_bang_samps / x->x_n;
    x->x_t3_bang_samps = t3_bang_samps - x->x_n * ticks;
    if((ac >= 3)&&IS_A_FLOAT(av,2))
    {
      time = (double)atom_getfloatarg(2, ac, av);
      if(time < 0.0)
        time = 0.0;
      x->x_inlet_time = time;
    }
    if(ticks < 1)
      t3_line_tilde_tick(x);
    else
      clock_delay(x->x_clock, (double)ticks * x->x_ticks2ms);
  }
}

static void t3_line_tilde_ft1(t_t3_line_tilde *x, t_float time)
{
  if(time < 0.0)
    time = 0.0;
  x->x_inlet_time = (double)time;
}

static void t3_line_tilde_stop(t_t3_line_tilde *x)
{
  clock_unset(x->x_clock);
  x->x_cur_samps = x->x_dur_samps = x->x_transient = 0;
  x->x_inc = x->x_inc64 = x->x_inlet_time = x->x_dst_time = 0.0;
}

static void t3_line_tilde_dsp(t_t3_line_tilde *x, t_signal **sp)
{
  int i;
  t_float val, *trans;
  
  if(sp[0]->s_n > x->x_n)
  {
    freebytes(x->x_beg, x->x_n*sizeof(t_float));
    x->x_n = (int)sp[0]->s_n;
    x->x_beg = (t_float *)getbytes(x->x_n*sizeof(t_float));
  }
  else
    x->x_n = (int)sp[0]->s_n;
  i = x->x_n;
  val = x->x_cur_val;
  trans = x->x_beg;
  while(i--)
    *trans++ = val;
  x->x_ms2samps = 0.001*(double)sp[0]->s_sr;
  x->x_ticks2ms = (double)x->x_n / x->x_ms2samps;
  dsp_add(t3_line_tilde_perform, 3, sp[0]->s_vec, x, sp[0]->s_n);
}

static void t3_line_tilde_free(t_t3_line_tilde *x)
{
  if(x->x_beg)
    freebytes(x->x_beg, x->x_n*sizeof(t_float));
  clock_free(x->x_clock);
}

static void *t3_line_tilde_new(t_floatarg init_val)
{
  t_t3_line_tilde *x = (t_t3_line_tilde *)pd_new(t3_line_tilde_class);
  int i;
  
  x->x_n = (int)sys_getblksize();
  x->x_beg = (t_float *)getbytes(x->x_n*sizeof(t_float));
  x->x_inlet_val = x->x_cur_val = x->x_dst_val = init_val;
  x->x_t3_bang_samps = x->x_cur_samps = x->x_dur_samps = x->x_transient = 0;
  x->x_inlet_time = x->x_dst_time = 0.0;
  x->x_inc64 = x->x_inc = 0.0;
  x->x_ms2samps = 0.001 * (double)sys_getsr();
  x->x_ticks2ms = (double)x->x_n / x->x_ms2samps;
  x->x_clock = clock_new(x, (t_method)t3_line_tilde_tick);
  outlet_new(&x->x_obj, &s_signal);
  inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft1"));
  return (x);
}

void t3_line_tilde_setup(void)
{
  t3_line_tilde_class = class_new(gensym("t3_line~"), (t_newmethod)t3_line_tilde_new,
    (t_method)t3_line_tilde_free, sizeof(t_t3_line_tilde), 0, A_DEFFLOAT, 0);
  class_addmethod(t3_line_tilde_class, (t_method)t3_line_tilde_dsp, gensym("dsp"), 0);
  class_addmethod(t3_line_tilde_class, (t_method)t3_line_tilde_stop, gensym("stop"), 0);
  class_addmethod(t3_line_tilde_class, (t_method)t3_line_tilde_ft1, gensym("ft1"), A_FLOAT, 0);
  class_addlist(t3_line_tilde_class, (t_method)t3_line_tilde_list);
//  class_sethelpsymbol(t3_line_tilde_class, gensym("iemhelp/help-t3_line~"));
}