aboutsummaryrefslogtreecommitdiff
path: root/experimental/fofsynth~.c
blob: 5a9de5ba038f4204956c3809570f310d84e13852 (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
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
/* (C) Guenter Geiger <geiger@epy.co.at> */


#include <m_pd.h>
#include <math.h>
#ifdef _MSC_VER
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif

/* ------------------------ fofsynth~ ----------------------------- */

#ifndef NT
void garray_usedindsp(t_garray *x);
#endif

#define DEBUG(a,b) if (x->debug) post(a,b);

#define MAXGRAINS 1000
#define PD_PI 3.14159

static float* cos_table;
static float* halfcos_table;
static float* exp_table;

static void cos_maketable(void)
{
     int i;
     float *fp, phase, phsinc = (2. * PD_PI) / COSTABSIZE;
     
     if (cos_table) return;
     cos_table = (float *)getbytes(sizeof(float) * (COSTABSIZE+1));
     
     for (i = COSTABSIZE + 1, fp = cos_table, phase = 0; i--;
	  fp++, phase += phsinc) 
	  *fp = cos(phase);
     
}

static void halfcos_maketable(void)
{
     int i;
     float *fp, phase, phsinc = (PD_PI) / COSTABSIZE;
     
     if (halfcos_table) return;
     halfcos_table = (float *)getbytes(sizeof(float) * (COSTABSIZE+1));
     
     for (i = COSTABSIZE + 1, fp = halfcos_table, phase = PD_PI; i--;
	  fp++, phase += phsinc) 
	  *fp = 0.5*(cos(phase) + 1.0);
}


static void exp_maketable(void)
{
     int i;
     float *fp, phase, phsinc = (2 * PD_PI) / COSTABSIZE;
     
     if (exp_table) return;
     exp_table = (float *)getbytes(sizeof(float) * (COSTABSIZE+1));
     
     for (i = COSTABSIZE + 1, fp = exp_table, phase = 0; i--;
	  fp++, phase += phsinc) 
	  *fp = exp(-phase);
}


static t_class *fofsynth_class;

typedef struct _grain 
{
     struct _grain *next;
     t_float formph; /* 0 ... 1 */
     t_float formphinc; 
     t_float envph;
     int falling;
} t_grain;


typedef struct _fofsynth
{
     t_object x_obj;

     /* it is possible to use 2 array, prob change this one
	int the future */

     t_symbol* x_arrayname;

     /* template */

     int x_npoints;
     t_float *x_vec;
     
     /* fof */
     int debug;

     int maxgrains;
     int numgrains;

     float* x_envelope;

     /* the queue of grains */

     t_grain* grainbase;
     t_grain* grainstart;
     t_grain* grainend;
     

     t_float fundph; /* 0 to 1; if 1 -> add a new grain */

     t_float fundfreq; /* input parameter 1 */
     t_float formfreq; /* input paramter 2 */
     t_float risedur; /* input parameter 3 ( in % of total duration )*/
     t_float falldur; /* input parameter 5 ( in % of total duration */

     /* other */
     int neednewgrain;
} t_fofsynth;




/* build a cyclic list */
static t_grain* grain_makecyclic(t_grain* base,int num)
{
     t_grain* cur = base;
     while (--num) {
	  cur->next = cur+1;
	  cur++;
     }
     cur->next = base;
	 return base;
}





static t_int *fofsynth_perform(t_int *w)
{
     t_fofsynth* x = (t_fofsynth*) (w[1]);
     t_float *in = (t_float *)(w[2]);
     t_float *out = (t_float *)(w[3]);
     int n = (int)(w[4]);

     float totaldur = (x->risedur+ x->falldur)*0.01/ *in;

     float srate = 44100.0; /*((t_signal*)w[2])->s_sr;*/
     float israte = 1.0/srate;

     float fundphase = x->fundph;
     float numperiods = totaldur*x->formfreq;
     float formphinc = (x->formfreq/srate);

     float risinc;
     float fallinc;

     t_grain* cur;

     risinc = (x->risedur == 0) ? 1.0 : 1.0/ (srate*totaldur*0.01*x->risedur);
     fallinc = (x->falldur ==  0.0) ? 1.0 :1.0/ (srate*totaldur*0.01*x->falldur);
     
     DEBUG(" fundph %3.2f",x->fundph);
     DEBUG(" fundfreq %3.2f",x->fundfreq);
     DEBUG(" formfreq %3.2f",x->formfreq);
     DEBUG(" risedur %3.2f %",x->risedur);
     DEBUG(" falldur %3.2f %",x->falldur);
     DEBUG(" totaldur %3.2f s",totaldur);
     DEBUG(" risinc %0.6f",risinc);
     DEBUG(" fallinc %0.6f",fallinc);
     DEBUG(" formant increase %3.2f",formphinc);
     DEBUG(" numgrains %d",x->numgrains);
    
     while (n--)
     {
	  fundphase+=*++in*israte;
	  *out = 0.0;

	  if (x->neednewgrain) {  /* new grain, they are deleted separetely */
	       t_grain* newgrain = x->grainend;
/*	       DEBUG("new grain created",0); */
	       if (newgrain->next == x->grainstart) {
		    post("fof: grain overflow");		   
		    x->neednewgrain = 0;  
	       }
	       else {
		    x->numgrains++;
		    x->grainend = newgrain->next;
		    newgrain->formphinc = formphinc;
		    newgrain->falling = 0;
		    newgrain->formph = newgrain->envph = 0.0;
		    x->neednewgrain = 0;  
	       }
	  }

	  cur = x->grainstart;
	  while (cur != x->grainend) {
	       float formphase = cur->formph;
	       float envelope;

	       float tph = (formphase - (float)((int) formphase));
	       float val = *(x->x_vec + (int) (tph * x->x_npoints));

	       /* Apply the envelope */

	       if (!cur->falling && (cur->envph <= 1.0)) { 
		    envelope = *(halfcos_table + (int) (cur->envph * COSTABSIZE));
		    cur->envph+=risinc;
		    val *= envelope;
	       }
	       else if (!cur->falling)
	       {
		    cur->falling = 1;
		    cur->envph = 0;
	       }
	       

	       if (cur->falling) {
		    envelope = *(exp_table + (int) (cur->envph * COSTABSIZE));
		    cur->envph+=fallinc;
		    val *= envelope;
	       }		    

	       /* end of envelope code */


	       formphase+=cur->formphinc;
	       cur->formph = formphase;
	       
	       if (formphase >= numperiods) { /* dead */
		    DEBUG("grain died",0);
		    x->grainstart = cur->next;
		    x->numgrains--;/* not needed */
	       }
	       
	       cur = cur->next;
	       *out += val;
	  }
	  

	  if (fundphase > 1.0) {
	       fundphase -=  1.0;
	       x->neednewgrain=1;
	  }
	  out++;
     }

     x->fundph=fundphase;
     x->debug = 0;


     return (w+5);
}

void fofsynth_usearray(t_symbol* s,int* points,t_float** vec)
{
     t_garray *a;
     if (!(a = (t_garray *)pd_findbyclass(s, garray_class))) 
	  error("%s: no such array", s->s_name);
     else if (!garray_getfloatarray(a,points,vec))
	  error("%s: bad template for fof~", s->s_name);
     else
	  garray_usedindsp(a); 
}

static void fofsynth_dsp(t_fofsynth *x, t_signal **sp)
{
     
     if (x->x_arrayname)
	  fofsynth_usearray(x->x_arrayname,&x->x_npoints, &x->x_vec);	
     else {
	  x->x_npoints=COSTABSIZE;
	  x->x_vec = cos_table;
     }
               
     dsp_add(fofsynth_perform, 4, x,
	     sp[0]->s_vec,sp[1]->s_vec, sp[0]->s_n);
}


static void fofsynth_free(t_fofsynth *x)
{
     freebytes(x->grainbase,sizeof(t_grain)*x->maxgrains);
}


static void fofsynth_debug(t_fofsynth* x)
{
     x->debug = 1;
}


static void fofsynth_float(t_fofsynth* x,t_float f)
{
	x->fundfreq = f > 0.0 ? f : -f;
}


static void *fofsynth_new(t_symbol* s,t_float a,t_float b,t_float c,t_float d)
{
     int maxgrains = MAXGRAINS;
     t_fofsynth *x = (t_fofsynth *)pd_new(fofsynth_class);

     x->debug = 0;
     x->x_arrayname = s;

     if (s == &s_)
	  x->x_arrayname = NULL;

     /* setup the grain queue */
     
     x->grainbase = getbytes(sizeof(t_grain)*maxgrains);
     x->maxgrains = maxgrains;
     grain_makecyclic(x->grainbase,maxgrains);
     x->grainstart = x->grainbase;
     x->grainend = x->grainbase;    
     x->numgrains = 0;

     /* some of them could be signals too */

     floatinlet_new(&x->x_obj, &x->formfreq);
     floatinlet_new(&x->x_obj, &x->risedur);
     floatinlet_new(&x->x_obj, &x->falldur);

     x->fundph = 0.0; 
     x->fundfreq = 200.0;
     x->formfreq = 600.0; 
     x->risedur = 5.0; 
     x->falldur = 140.0; 

     if (a) x->fundfreq = a;
     if (b) x->formfreq = b; 
     if (c) x->risedur = c; 
     if (d) x->falldur = d; 

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

void fofsynth_tilde_setup(void)
{
     cos_table = NULL;
     halfcos_table = NULL;
     fofsynth_class = class_new(gensym("fof~"), (t_newmethod) fofsynth_new,(t_method) fofsynth_free,
				sizeof(t_fofsynth), 0,A_DEFSYM, A_DEFFLOAT,A_DEFFLOAT,A_DEFFLOAT,A_DEFFLOAT,0);
     class_addcreator((t_newmethod)fofsynth_new,gensym("fofsynth~"),A_DEFSYM, A_DEFFLOAT,A_DEFFLOAT,A_DEFFLOAT,A_DEFFLOAT,0);
     class_addmethod(fofsynth_class, nullfn, gensym("signal"), 0);
     class_addmethod(fofsynth_class, (t_method) fofsynth_dsp, gensym("dsp"), 0);
     class_addfloat(fofsynth_class, (t_method) fofsynth_float);
     class_addmethod(fofsynth_class,(t_method) fofsynth_debug, gensym("debug"),0);
     cos_maketable();
     halfcos_maketable();
     exp_maketable();
}