File: clean_selector~.c

package info (click to toggle)
pd-lyonpotpourri 2.0%2Bgit20121009-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,176 kB
  • sloc: ansic: 18,330; makefile: 376
file content (320 lines) | stat: -rw-r--r-- 8,118 bytes parent folder | download | duplicates (3)
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
#include "MSPd.h"
#define MAX_CHANS (64)
#define CS_LINEAR (0)
#define CS_POWER (1)

#if __PD__
static t_class *clean_selector_class;
#endif

#if __MSP__
void *clean_selector_class;
#endif

typedef struct _clean_selector
{
#if __MSP__
	t_pxobject x_obj;
#endif
#if __PD__
	t_object x_obj;
	float x_f;
#endif
	// Variables Here
	short input_chans;
	short active_chan;
	short last_chan;
	int samps_to_fade;
	int fadesamps;
	float fadetime;
	float pi_over_two;
	short fadetype;
	short *connected_list;
	float **bulk ; // array to point to all input audio channels
	float sr;
	float vs;
	int inlet_count;
} t_clean_selector;

#define OBJECT_NAME "clean_selector~"

void *clean_selector_new(t_symbol *s, int argc, t_atom *argv);

t_int *clean_selector_perform(t_int *w);
void clean_selector_dsp(t_clean_selector *x, t_signal **sp, short *count);
void clean_selector_assist(t_clean_selector *x, void *b, long m, long a, char *s);
void clean_selector_float(t_clean_selector *x, t_float f);
void clean_selector_fadetime(t_clean_selector *x, t_floatarg f);
void clean_selector_int(t_clean_selector *x, t_int i);
void clean_selector_channel(t_clean_selector *x, t_floatarg i);
void clean_selector_dsp_free(t_clean_selector *x);

#if __MSP__
void main(void)
{
	setup((t_messlist **)&clean_selector_class, (method)clean_selector_new, (method)dsp_free, 
		  (short)sizeof(t_clean_selector), 0, A_GIMME, 0);
	addmess((method)clean_selector_dsp, "dsp", A_CANT, 0);
	addmess((method)clean_selector_assist,"assist",A_CANT,0);
	addmess((method)clean_selector_fadetime,"fadetime",A_FLOAT,0);
	addmess((method)clean_selector_channel,"channel",A_FLOAT,0);
	addfloat((method)clean_selector_float);
	addint((method)clean_selector_int);
	dsp_initclass();
	post("%s %s",OBJECT_NAME, LYONPOTPOURRI_MSG);
}
#endif

#if __PD__
void clean_selector_tilde_setup(void) {
	clean_selector_class = class_new(gensym("clean_selector~"), (t_newmethod)clean_selector_new, 
									 (t_method)clean_selector_dsp_free,sizeof(t_clean_selector), 0,A_GIMME,0);
	CLASS_MAINSIGNALIN(clean_selector_class, t_clean_selector, x_f);
	class_addmethod(clean_selector_class,(t_method)clean_selector_dsp,gensym("dsp"),0);
	class_addmethod(clean_selector_class,(t_method)clean_selector_fadetime,gensym("fadetime"),A_FLOAT,0);
	class_addmethod(clean_selector_class,(t_method)clean_selector_channel,gensym("channel"),A_FLOAT,0);
	post("%s %s",OBJECT_NAME, LYONPOTPOURRI_MSG);
}
#endif

void clean_selector_assist (t_clean_selector *x, void *b, long msg, long arg, char *dst)
{
	if (msg==1) {
		if(arg == 0){
			sprintf(dst,"(signal/int) Input 0, Channel Number");
		} else {
			sprintf(dst,"(signal) Input %ld",arg);
		}
		
	} else if (msg==2) {
		sprintf(dst,"(signal) Output");
	}
}

void *clean_selector_new(t_symbol *s, int argc, t_atom *argv)
{
	int i;
	t_clean_selector *x;
#if __MSP__
	x = (t_clean_selector *)newobject(clean_selector_class);
#endif
#if __PD__
	x = (t_clean_selector *)pd_new(clean_selector_class);
#endif
		x->fadetime = 0.05;
		x->inlet_count = 8;
		if(argc >= 1){
			x->inlet_count = (int)atom_getfloatarg(0,argc,argv);
			if(x->inlet_count < 2 || x->inlet_count > MAX_CHANS){
				error("%s: %d is illegal number of inlets",OBJECT_NAME,x->inlet_count);
				return (void *) NULL;
			}

		}  
		if(argc >= 2){
			x->fadetime = atom_getfloatarg(1,argc,argv) / 1000.0;
		}

//		post("argc %d inlet count %d fadetime %f",argc, x->inlet_count, x->fadetime);
#if __MSP__
	dsp_setup((t_pxobject *)x, x->inlet_count); 
	outlet_new((t_pxobject *)x, "signal");
	x->x_obj.z_misc |= Z_NO_INPLACE;
#endif
#if __PD__
	for(i=0; i< x->inlet_count - 1; i++){// create 16 inlets in total
		inlet_new(&x->x_obj, &x->x_obj.ob_pd,gensym("signal"), gensym("signal"));
	}
	outlet_new(&x->x_obj, gensym("signal"));
#endif
	
	
	x->sr = sys_getsr();
	if(!x->sr){
		x->sr = 44100.0;
		error("zero sampling rate - set to 44100");
	}
	x->fadetype = CS_POWER;
	x->pi_over_two = 1.57079632679;
	
	
    
    if(x->fadetime <= 0.0)
    	x->fadetime = .05;
    x->fadesamps = x->fadetime * x->sr;
    
    x->connected_list = (short *) t_getbytes(MAX_CHANS * sizeof(short));
    for(i=0;i<16;i++){
    	x->connected_list[i] = 0;
    }
    x->active_chan = x->last_chan = 0;
    x->bulk = (t_float **) t_getbytes(16 * sizeof(t_float *));
    x->samps_to_fade = 0;
	return (x);
}

void clean_selector_dsp_free(t_clean_selector *x)
{
#if __MSP__
	dsp_free((t_pxobject *)x);
#endif
	t_freebytes(x->bulk, 16 * sizeof(t_float *));
}


void clean_selector_fadetime(t_clean_selector *x, t_floatarg f)
{
	float fades = (float)f / 1000.0;
	
	if( fades < .0001 || fades > 1000.0 ){
		error("fade time is constrained to 0.1 - 1000000, but you wanted %f",f );
		return;
	}
	x->fadetime = fades;
	x->fadesamps = x->sr * x->fadetime;
	x->samps_to_fade = 0;
}

t_int *clean_selector_perform(t_int *w)
{
	
	t_clean_selector *x = (t_clean_selector *) (w[1]);
	int i;
	t_int n;
	t_float *out;
	
	int fadesamps = x->fadesamps;
	short active_chan = x->active_chan;
	short last_chan = x->last_chan;
	int samps_to_fade = x->samps_to_fade;
	float m1, m2;
	float **bulk = x->bulk;
	float pi_over_two = x->pi_over_two;
	short fadetype = x->fadetype;
	float phase;
	int inlet_count = x->inlet_count;
	
	for ( i = 0; i < inlet_count; i++ ) {
		bulk[i] = (t_float *)(w[2 + i]);
	}
	out = (t_float *)(w[inlet_count + 2]);
	n = w[inlet_count + 3]; 
	
	/********************************************/
	if ( active_chan >= 0 ) {
		while( n-- ) {
			if ( samps_to_fade >= 0 ) {
				if( fadetype == CS_POWER ){
					phase = pi_over_two * (1.0 - (samps_to_fade / (float) fadesamps)) ;
					m1 = sin( phase );
					m2 = cos( phase );
					--samps_to_fade;
					*out++ = (*(bulk[active_chan])++ * m1) + (*(bulk[last_chan])++ * m2);
				} 
			}
			else {
				*out++ =  *(bulk[active_chan])++;
			}
		}
	} 
  	else  {
  		while( n-- ) {
			*out++ = 0.0;
		}
  	}
    
	x->samps_to_fade = samps_to_fade;
	return (w + (inlet_count + 4));
}		

void clean_selector_dsp(t_clean_selector *x, t_signal **sp, short *count)
{
	long i;
	t_int **sigvec;
	int pointer_count;
	
	pointer_count = x->inlet_count + 3; // all inlets, 1 outlet, object pointer and vec-samps	
	sigvec  = (t_int **) calloc(pointer_count, sizeof(t_int *));	
	for(i = 0; i < pointer_count; i++){
		sigvec[i] = (t_int *) calloc(sizeof(t_int),1);
	}
	sigvec[0] = (t_int *)x; // first pointer is to the object
	
	sigvec[pointer_count - 1] = (t_int *)sp[0]->s_n; // last pointer is to vector size (N)

	for(i = 1; i < pointer_count - 1; i++){ // now attach the inlet and all outlets
		sigvec[i] = (t_int *)sp[i-1]->s_vec;
	}
		
	if(x->sr != sp[0]->s_sr){
		x->sr = sp[0]->s_sr;
		x->fadesamps = x->fadetime * x->sr;
		x->samps_to_fade = 0;
	}

#if __MSP__
	dsp_addv(clean_selector_perform, pointer_count, (void **) sigvec); 
#endif
#if __PD__
	dsp_addv(clean_selector_perform, pointer_count, (t_int *) sigvec); 
#endif
	free(sigvec);

#if __MSP__
	 for (i = 0; i < MAX_CHANS; i++) {
		 x->connected_list[i] = count[i];
	 }
#endif
#if __PD__
	 for (i = 0; i < MAX_CHANS; i++) {
		 x->connected_list[i] = 1;
	 }
#endif

			
}

#if __MSP__
void clean_selector_float(t_clean_selector *x, t_float f) // Look at floats at inlets
{
	clean_selector_int(x,(t_int)f);	
}

void clean_selector_int(t_clean_selector *x, t_int i) // Look at int at inlets
{
	int inlet = ((t_pxobject*)x)->z_in;
	
	if (inlet == 0)
	{		
			clean_selector_channel(x,(t_floatarg)i);
	}	

}
#endif

void clean_selector_channel(t_clean_selector *x, t_floatarg i) // Look at int at inlets
{
	int chan = i;
	if(chan < 0 || chan > x->inlet_count - 1){
		post("%s: channel %d out of range",OBJECT_NAME, chan);
		return;
	}	
	if(chan != x->active_chan) {
		
		x->last_chan = x->active_chan;
		x->active_chan = chan;
		x->samps_to_fade = x->fadesamps;
		if( x->active_chan < 0)
			x->active_chan = 0;
		if( x->active_chan > MAX_CHANS - 1) {
			x->active_chan = MAX_CHANS - 1;
		}
		if(! x->connected_list[chan]) {
		// do it anyway - it's user-stupidity
		/*
			post("warning: channel %d not connected",chan);
			x->active_chan = 1; */
		}
		// post("last: %d active %d", x->last_chan, x->active_chan);
	}	
}