File: function~.c

package info (click to toggle)
pd-lyonpotpourri 2.0%2Bgit20121009-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,180 kB
  • sloc: ansic: 18,330; makefile: 376
file content (394 lines) | stat: -rw-r--r-- 9,633 bytes parent folder | download | duplicates (2)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "MSPd.h"

/* comment */

#if __PD__
static t_class *function_class;
#endif

#if __MSP__
void *function_class;
#endif

typedef struct _function
{
#if __MSP__
  t_pxobject x_obj;
  t_buffer *wavebuf; // holds waveform samples
#endif
#if __PD__
  t_object x_obj;
  float x_f;
#endif
  t_symbol *wavename; // name of waveform buffer

  /* to make array mimic buffer~ */
  long b_frames;
  long b_nchans;
  float *b_samples;
  short normalize;
} t_function;

void function_setbuf(t_function *x, t_symbol *wavename);
void *function_new(t_symbol *msg, short argc, t_atom *argv);
void function_dsp(t_function *x, t_signal **sp, short *count);
void function_redraw(t_function *x);
void function_clear(t_function *x);
void function_addsyn(t_function *x, t_symbol *msg, short argc, t_atom *argv);
void function_aenv(t_function *x, t_symbol *msg, short argc, t_atom *argv);
void function_adenv(t_function *x, t_symbol *msg, short argc, t_atom *argv);
void function_normalize(t_function *x, t_floatarg f);
void function_adrenv(t_function *x, t_symbol *msg, short argc, t_atom *argv);
void function_rcos(t_function *x);
void function_gaussian(t_function *x);

#if __MSP__
void main(void)
{
  setup((t_messlist **)&function_class, (method)function_new, (method)function_dsp_free, 
    	(short)sizeof(t_function), 0, A_GIMME, 0);
  addmess((method)function_dsp,"dsp", A_CANT, 0);
  addmess((method)function_assist,"assist", A_CANT, 0);
  addmess((method)function_dblclick,"dblclick", A_CANT, 0);
  addmess((method)function_setbuf,"setbuf", A_SYM, A_SYM, 0);
  addmess((method)function_info,"info", 0); 
  dsp_initclass();
}
#endif

#if __PD__
#define NO_FREE_FUNCTION 0
void function_tilde_setup(void)
{
  function_class = class_new(gensym("function~"), (t_newmethod)function_new, 
			     NO_FREE_FUNCTION,sizeof(t_function), 0,A_GIMME,0);
  CLASS_MAINSIGNALIN(function_class, t_function, x_f);
  class_addmethod(function_class,(t_method)function_dsp,gensym("dsp"),0);
  class_addmethod(function_class,(t_method)function_addsyn,gensym("addsyn"),A_GIMME,0);
  class_addmethod(function_class,(t_method)function_aenv,gensym("aenv"),A_GIMME,0);
  class_addmethod(function_class,(t_method)function_adenv,gensym("adenv"),A_GIMME,0);
  class_addmethod(function_class,(t_method)function_adrenv,gensym("adrenv"),A_GIMME,0);
  class_addmethod(function_class,(t_method)function_clear,gensym("clear"),0);
  class_addmethod(function_class,(t_method)function_normalize,gensym("normalize"),A_FLOAT,0);
  class_addmethod(function_class,(t_method)function_rcos,gensym("rcos"),0);
  class_addmethod(function_class,(t_method)function_gaussian,gensym("gaussian"),0);
  post("function~ by Eric Lyon");
}
#endif

void function_rcos(t_function *x)
{
  int i;
  long b_frames = x->b_frames;
  float *b_samples = x->b_samples;
  function_setbuf(x, x->wavename);
    for(i=0;i<b_frames;i++){
        b_samples[i] = 0.5 - 0.5 * cos(TWOPI * (float)i/(float)b_frames);
    }
  function_redraw(x);
}

void function_gaussian(t_function *x)
{
  int i;
  long b_frames = x->b_frames;
  float *b_samples = x->b_samples;
  float arg, xarg,in;
  
  if(!b_frames){
    post("* zero length function!");
    return;
  }
  function_setbuf(x, x->wavename);
  arg = 12.0 / (float)b_frames;
  xarg = 1.0;
  in = -6.0;

  for(i=0;i<b_frames;i++){
    b_samples[i] = xarg * pow(2.71828, -(in*in)/2.0);
    in += arg;  
  }
    function_redraw(x);
}

void function_normalize(t_function *x, t_floatarg f)
{
  x->normalize = (short)f;
}

void function_clear(t_function *x)
{
  int i;
  long b_frames = x->b_frames;
  float *b_samples = x->b_samples;
 
  function_setbuf(x, x->wavename);
  b_frames = x->b_frames;
  b_samples = x->b_samples;
  for(i=0;i<b_frames;i++)
    b_samples[i] = 0.0;
  function_redraw(x);
}

void function_adrenv(t_function *x, t_symbol *msg, short argc, t_atom *argv)
{
  int i,j;
  int al, dl, sl, rl;
  long b_frames = x->b_frames;
  float *b_samples = x->b_samples;
  float frac;
  float downgain = 0.33;
 
    function_setbuf(x, x->wavename); 
  al = (float) b_frames * atom_getfloatarg(0,argc,argv);
  dl = (float) b_frames * atom_getfloatarg(1,argc,argv);
  rl = (float) b_frames * atom_getfloatarg(2,argc,argv);
  downgain = atom_getfloatarg(3,argc,argv);
  if(downgain <= 0)
    downgain = 0.333;
  if(al+dl+rl >= b_frames){
      post("atk and dk and release are too long");
      return;
  }
  sl = b_frames - (al+dl+rl);
  
  for(i=0;i<al;i++){
    b_samples[i] = (float)i/(float)al;
  }
  for(i=al, j=dl;i<al+dl;i++,j--){
    b_samples[i] = downgain + (1.-downgain)*(float)j/(float)dl;
  }
  for(i=al+dl;i<al+dl+sl;i++){
    b_samples[i] = downgain;
  }
  for(i=al+dl+sl,j=rl;i<b_frames;i++,j--){
    b_samples[i] = downgain * (float)j/(float)rl;
  }
  function_redraw(x);  
}

void function_adenv(t_function *x, t_symbol *msg, short argc, t_atom *argv)
{
  int i,j;
  int al, dl, rl;
  long b_frames = x->b_frames;
  float *b_samples = x->b_samples;
  float frac;
  float downgain = 0.33;
 
    function_setbuf(x, x->wavename); 
  al = (float) b_frames * atom_getfloatarg(0,argc,argv);
  dl = (float) b_frames * atom_getfloatarg(1,argc,argv);
  downgain = atom_getfloatarg(2,argc,argv);
  if(downgain <= 0)
    downgain = 0.333;
  if(al+dl >= b_frames){
      post("atk and dk are too long");
      return;
  }
  rl = b_frames - (al+dl);
  
  for(i=0;i<al;i++){
    b_samples[i] = (float)i/(float)al;
  }
  for(i=al, j=dl;i<al+dl;i++,j--){
    b_samples[i] = downgain + (1.-downgain)*(float)j/(float)dl;
  }
  for(i=al+dl,j=rl;i<b_frames;i++,j--){
    b_samples[i] = downgain * (float)j/(float)rl;
  }
  function_redraw(x);  
}

void function_aenv(t_function *x, t_symbol *msg, short argc, t_atom *argv)
{
  int i,j;
  int al, dl;
  long b_frames = x->b_frames;
  float *b_samples = x->b_samples;
  float frac;
  frac = atom_getfloatarg(0,argc,argv);

  function_setbuf(x, x->wavename);
  if(frac <= 0 || frac >= 1){
    post("* attack time must range from 0.0 - 1.0, rather than %f",frac);
  }
  
  al = b_frames * frac;

  dl = b_frames - al;
  for(i=0;i<al;i++){
    b_samples[i] = (float)i/(float)al;
  }
  for(i=al, j=dl;i<b_frames;i++,j--){
    b_samples[i] = (float)j/(float)dl;
  }
  function_redraw(x);  
}

void function_addsyn(t_function *x, t_symbol *msg, short argc, t_atom *argv)
{
  int i,j;
  long b_frames = x->b_frames;
  float *b_samples = x->b_samples;
  float amp;
  float maxamp, rescale;

  function_setbuf(x, x->wavename);
  amp = atom_getfloatarg(0,argc,argv);
  for(i=0;i<b_frames;i++){
    b_samples[i] = amp;
  }
  for(j=1;j<argc;j++){
    amp = atom_getfloatarg(j,argc,argv);
    if(amp){
      for(i=0;i<b_frames;i++){
	b_samples[i] += amp * sin(TWOPI * (float) j * (float)i/(float)b_frames);
      }
    }
  }
  if(x->normalize){
    maxamp = 0;
    for(i=0;i<b_frames;i++){
      if(maxamp < fabs(b_samples[i]))
	maxamp = fabs(b_samples[i]);
    }
    if(!maxamp){
      post("* zero maxamp!");
      return;
    }
    rescale = 1.0 / maxamp;
    for(i=0;i<b_frames;i++){
      b_samples[i] *= rescale;
    }
  }
  function_redraw(x);
}


void *function_new(t_symbol *msg, short argc, t_atom *argv)
{
#if __MSP__
  t_function *x = (t_function *)newobject(function_class);
  dsp_setup((t_pxobject *)x,1);	
  outlet_new((t_pxobject *)x, "signal");
  x->maxchans = 2;
#endif

#if __PD__
  t_function *x = (t_function *)pd_new(function_class);
  outlet_new(&x->x_obj, gensym("signal"));
#endif

  x->wavename = atom_getsymbolarg(0,argc,argv);
  x->normalize = 1;
  
  return (x);
}


#if __MSP__
void function_dblclick(t_function *x)
{
  t_buffer *b;
  t_symbol *wavename = x->wavename;
	
  if ((b = (t_buffer *)(wavename->s_thing)) && ob_sym(b) == gensym("buffer~"))
    mess0((t_object *)b,gensym("dblclick"));

}

void function_setbuf(t_function *x, t_symbol *wavename)
{
  t_buffer *b;

  x->hosed = 0;
		
  if ((b = (t_buffer *)(wavename->s_thing)) && ob_sym(b) == gensym("buffer~")) {
    if( b->b_nchans > 2 ){
      error("wavetable must be a mono or stereo buffer");
      x->hosed = 1;
    } else {
      x->wavebuf = b;
    }		
  } else {
    error("function~: no such buffer~ %s", wavename->s_name);
    x->hosed = 1;
  }

  x->wavename = wavename;
}
#endif

#if __PD__
void function_setbuf(t_function *x, t_symbol *wavename)
{
  int frames;
  t_garray *a;

  x->b_frames = 0;
  x->b_nchans = 1;
  if (!(a = (t_garray *)pd_findbyclass(wavename, garray_class))) {
    if (*wavename->s_name) pd_error(x, "function~: %s: no such array", wavename->s_name);
  }
  else if (!garray_getfloatarray(a, &frames, &x->b_samples)) {
    pd_error(x, "%s: bad template for function~", wavename->s_name);
  }
  else  {
    x->b_frames = frames;
    garray_usedindsp(a);
  }
}

void function_redraw(t_function *x)
{
  t_garray *a;
  if (!(a = (t_garray *)pd_findbyclass(x->wavename, garray_class))) {
    if (*x->wavename->s_name) pd_error(x, "function~: %s: no such array", x->wavename->s_name);
  }
  else  {
    garray_redraw(a);
  }
}

#endif


// may not even need this much work
/*
t_int *function_perform(t_int *w)
{
}

*/

/*
void function_seed(t_function *x, t_floatarg seed)
{
  srandom((long)seed);
}

#define LONG_RAND_MAX 2147483648.0
float function_boundrand(float min, float max)
{
  return min + (max-min) * ((float) random()/LONG_RAND_MAX);
}
*/

void function_dsp(t_function *x, t_signal **sp, short *count)
{
}

void function_assist (t_function *x, void *b, long msg, long arg, char *dst)
{
  if (msg==1) {
    switch (arg) {
    case 0: sprintf(dst,"(messages) Groove Sync Signal"); break;
    }
  } else if (msg==2) {
    switch (arg){
    case 0: sprintf(dst,"(signal) No Output"); break;
    }  
  }
}