epluribus~.c 3.76 KB
Newer Older
Jonathan Wilkes's avatar
Jonathan Wilkes committed
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
#include "MSPd.h"

static t_class *epluribus_class;

#define MAXBEATS (256)
#define OBJECT_NAME "epluribus~"
#define COMPILE_DATE "5.3.08"
#define OBJECT_VERSION "2.0"

typedef struct _epluribus
{
	t_object x_obj;
    float x_f;
	int incount; // how many inlets (must be at least 2)
	short inverse; // flag to look for minimum instead
} t_epluribus;

void *epluribus_new(t_symbol *msg, int argc, t_atom *argv);
t_int *epluribus_perform(t_int *w);
void epluribus_dsp(t_epluribus *x, t_signal **sp);
void epluribus_inverse(t_epluribus *x, t_floatarg tog);

void epluribus_tilde_setup(void)
{
    t_class *c;
	c = class_new(gensym("epluribus~"), (t_newmethod)epluribus_new,
                                0,sizeof(t_epluribus), 0,A_GIMME,0);
	CLASS_MAINSIGNALIN(c, t_epluribus, x_f);
	class_addmethod(c, (t_method)epluribus_dsp, gensym("dsp"), 0);
    class_addmethod(c, (t_method)epluribus_inverse, gensym("inverse"),A_FLOAT, 0);
    epluribus_class = c;
	potpourri_announce(OBJECT_NAME);
}

void epluribus_inverse(t_epluribus *x, t_floatarg tog)
{
	x->inverse = (short) tog;
}


void *epluribus_new(t_symbol *msg, int argc, t_atom *argv)
{
43
    t_epluribus *x;
Jonathan Wilkes's avatar
Jonathan Wilkes committed
44
    int i;
45 46 47 48 49 50 51 52 53 54
    x = (t_epluribus *)pd_new(epluribus_class);
    x->incount = (int) atom_getfloatarg(0,argc,argv);
    if(x->incount < 2 || x->incount > 256 ){
        int defcount = x->incount < 2 ? 2 : 256;
        post("%s: warning: there must be between 2 and 256 input vectors",
            OBJECT_NAME);
        post("defaulting to %d", defcount);
        x->incount = defcount;
    }

Jonathan Wilkes's avatar
Jonathan Wilkes committed
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
    for(i = 0; i < x->incount - 1; i++){
        inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("signal"),gensym("signal"));
    }
    outlet_new(&x->x_obj, gensym("signal"));
    outlet_new(&x->x_obj, gensym("signal"));
	x->inverse = 0; // by default don't do inverse behaviour
	return (x);
}


t_int *epluribus_perform(t_int *w)
{
	int i,j,k;
	t_epluribus *x = (t_epluribus *) (w[1]);
	t_float *inlet;
	t_float *outlet;
	t_float *selection;
	t_int n;
	t_float maxamp = 0.0;
	t_float maxout = 0.0;
	int maxloc;
	int incount = x->incount;
	int next_pointer = incount + 5;
	
	outlet = (t_float *) w[incount + 2];
	selection = (t_float *) w[incount + 3];
	n = (int) w[incount + 4];
	
	if( x->inverse ){
		for(k = 0; k < n; k ++ ){
			maxamp = 99999999.0;
			maxloc = 0;
			for(i = 0, j=2; i < incount ; i++, j++){
				inlet = (t_float *) (w[j]);
				if( maxamp > fabs( inlet[k] ) ){
					maxamp = fabs( inlet[k] ); 
					maxout = inlet[k]; // don't actually change signal
					maxloc = i + 1; // record location of max amp
				}
			}
			outlet[k] = maxout;
			selection[k] = maxloc;
		}
	} 
	else {
		for(k = 0; k < n; k ++ ){
			maxamp = 0.0;
			maxloc = 0;
			for(i = 0, j=2; i < incount ; i++, j++){
				inlet = (t_float *) (w[j]);
				if( maxamp < fabs( inlet[k] ) ){
					maxamp = fabs( inlet[k] );
					maxout = inlet[k]; // don't actually change signal
					maxloc = i + 1; // record location of max amp
				}
			}
			outlet[k] = maxout;
			selection[k] = maxloc;
		}
	}
		
	return w + next_pointer;
}


void epluribus_dsp(t_epluribus *x, t_signal **sp)
{
	long i;
	t_int **sigvec;
	int pointer_count;

	
	if( x->incount < 2 || x->incount > 256 ){
		post("bad vector count");
		return;
	}
	pointer_count = x->incount + 4; // all metros, plus 2 outlets, plus the object pointer, plus N

	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;
	}

	dsp_addv(epluribus_perform, pointer_count, (t_int *) sigvec); 
	free(sigvec);
	
}