samm~.c 11.5 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 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
#include "MSPd.h"


static t_class *samm_class;

#define MAXBEATS (256)
#define OBJECT_NAME "samm~"
// #define DATE "prerelease"

typedef struct _samm
{
	t_object x_obj;
	float x_f;
	double tempo; /* current tempo */
	double onebeat_samps; /* number of samples for a single beat */
	double *beats; /* amount of beats for each active tempo outlet */
	double *metro_samps;/* number of samples to count down for each time interval */
	double *metro_beatdurs;/* number of beats for each metro time interval */
	double *metro;/* current countdown for each time interval */
	int metro_count; /* number of metronomes to keep track of */
	float sr; /* current sampling rate */
	short pause;
	short mute;
    // Pd only */
	float *trigger_vec;
	short vs;
} t_samm;

void *samm_new(t_symbol *msg, short argc, t_atom *argv);
void *samm_beatlist(t_samm *x, t_symbol *msg, short argc, t_atom *argv);
t_int *samm_perform(t_int *w);
void samm_dsp(t_samm *x, t_signal **sp);
void samm_tempo(t_samm *x, t_floatarg f);
void samm_divbeats(t_samm *x, t_symbol *msg, short argc, t_atom *argv);
void samm_msbeats(t_samm *x, t_symbol *msg, short argc, t_atom *argv);
void samm_sampbeats(t_samm *x, t_symbol *msg, short argc, t_atom *argv);
void samm_ratiobeats(t_samm *x, t_symbol *msg, short argc, t_atom *argv);
void samm_free(t_samm *x);
void samm_beatinfo(t_samm *x);
void samm_init(t_samm *x,short initialized);
void samm_mute(t_samm *x, t_floatarg f);
void samm_pause(t_samm *x);
void samm_arm(t_samm *x);
void samm_resume(t_samm *x);
void samm_beats(t_samm *x, t_symbol *msg, short argc, t_atom *argv);


void samm_beatinfo(t_samm *x)
{
	int i;
	post("tempo %.10f",x->tempo);
	post("samples in one beat: %.10f",x->onebeat_samps);
	for(i = 0; i < x->metro_count; i++){
		post("%d: relative duration %.10f, samples %.10f samples ratio to 1 beat: %.10f", i,
             x->metro_beatdurs[i], x->metro_samps[i], x->onebeat_samps / x->metro_samps[i]  );
	}
}

void samm_tilde_setup(void){
	samm_class = class_new(gensym("samm~"), (t_newmethod)samm_new,
						   (t_method)samm_free,sizeof(t_samm), 0,A_GIMME,0);
	CLASS_MAINSIGNALIN(samm_class, t_samm, x_f);
	class_addmethod(samm_class,(t_method)samm_dsp,gensym("dsp"),0);
	class_addmethod(samm_class,(t_method)samm_mute,gensym("mute"),A_FLOAT,0);
	class_addmethod(samm_class,(t_method)samm_tempo,gensym("tempo"),A_FLOAT,0);
	class_addmethod(samm_class,(t_method)samm_beatinfo,gensym("beatinfo"),0);
	class_addmethod(samm_class,(t_method)samm_beats,gensym("beats"),A_GIMME,0);
	class_addmethod(samm_class,(t_method)samm_divbeats,gensym("divbeats"),A_GIMME,0);
	class_addmethod(samm_class,(t_method)samm_msbeats,gensym("msbeats"),A_GIMME,0);
	class_addmethod(samm_class,(t_method)samm_sampbeats,gensym("sampbeats"),A_GIMME,0);
	class_addmethod(samm_class,(t_method)samm_ratiobeats,gensym("ratiobeats"),A_GIMME,0);
	class_addmethod(samm_class,(t_method)samm_pause,gensym("pause"),A_GIMME,0);
	class_addmethod(samm_class,(t_method)samm_arm,gensym("arm"),A_GIMME,0);
	class_addmethod(samm_class,(t_method)samm_resume,gensym("resume"),A_GIMME,0);
    potpourri_announce(OBJECT_NAME);
}



void samm_pause(t_samm *x)
{
	x->pause = 1;
}

void samm_mute(t_samm *x, t_floatarg f)
{
	x->mute = (short) f;
}

void samm_arm(t_samm *x)
{
	int i;
	
	x->pause = 1;
	for( i = 0; i < x->metro_count; i++){
		x->metro[i] = 1.0;
	}
	
}

void samm_resume(t_samm *x)
{
	x->pause = 0;
}

void samm_beats(t_samm *x, t_symbol *msg, short argc, t_atom *argv)
{
    int i;
    double beatdur;
    
	if(argc != x->metro_count){
		error("%s: arguments did not match metro count %d",OBJECT_NAME,x->metro_count);
		return;
	}
    
	for(i = 0; i < argc; i++){
		beatdur = (double)atom_getfloatarg(i,argc,argv);
    	if(!beatdur){
			error("%s: zero divisor given for beat stream %d",OBJECT_NAME,i+1);
			beatdur = 1.0;
		}
        
		x->metro_beatdurs[i] = beatdur;
		x->metro_samps[i] = x->metro_beatdurs[i] * x->onebeat_samps;
		x->metro[i] = 1.0; // initialize for instantaneous beat
	}
}

void samm_divbeats(t_samm *x, t_symbol *msg, short argc, t_atom *argv)
{
    int i;
    double divisor;
    
	if(argc != x->metro_count){
		error("%s: arguments did not match metro count %d",OBJECT_NAME,x->metro_count);
		return;
	}
    
	for(i = 0; i < argc; i++){
		divisor = (double)atom_getfloatarg(i,argc,argv);
    	if(!divisor){
			error("%s: zero divisor given for beat stream %d",OBJECT_NAME,i+1);
			divisor = 1.0;
		}
        
		x->metro_beatdurs[i] = 1.0 / divisor; // argument is now DIVISOR of beat
		x->metro_samps[i] = x->metro_beatdurs[i] * x->onebeat_samps;
		x->metro[i] = 1.0; // initialize for instantaneous beat
	}
}

void samm_msbeats(t_samm *x, t_symbol *msg, short argc, t_atom *argv)
{
    int i;
    double msecs;
	if(argc != x->metro_count){
		error("%s: arguments did not match metro count %d",OBJECT_NAME,x->metro_count);
		return;
	}
	for(i = 0; i < argc; i++){
		msecs = (double)atom_getfloatarg(i,argc,argv);
    	if(msecs <= 0){
			error("%s: illegal duration for beat stream %d",OBJECT_NAME,i+1);
			msecs = 1000.0;
		}
		x->metro_samps[i] = x->sr * .001 * msecs;
		x->metro_beatdurs[i] = x->metro_samps[i] / x->onebeat_samps; // just in case tempo changes
		x->metro[i] = 1.0; // initialize for instantaneous beat
	}
    
}

void samm_sampbeats(t_samm *x, t_symbol *msg, short argc, t_atom *argv)
{
    int i;
    double samples;
	if(argc != x->metro_count){
		error("%s: arguments did not match metro count %d",OBJECT_NAME,x->metro_count);
		return;
	}
	for(i = 0; i < argc; i++){
		samples = (double)atom_getfloatarg(i,argc,argv);
    	if(samples <= 0){
			error("%s: illegal duration for beat stream %d",OBJECT_NAME,i+1);
			samples = x->sr;
		}
		x->metro_samps[i] = samples;
		x->metro_beatdurs[i] = x->metro_samps[i] / x->onebeat_samps; // just in case tempo changes
		x->metro[i] = 1.0; // initialize for instantaneous beat
	}
}

void samm_ratiobeats(t_samm *x, t_symbol *msg, short argc, t_atom *argv)
{
    int i,j;
    double num,denom;
    
	if(argc != x->metro_count * 2){
		error("%s: arguments did not match metro count %d",OBJECT_NAME,x->metro_count);
		return;
	}
    
	for(i = 0, j= 0; i < argc; i += 2, j++){
		num = (double)atom_getfloatarg(i,argc,argv);
		denom = (double)atom_getfloatarg(i+1,argc,argv);
    	if(!denom){
			error("%s: zero divisor given for beat stream %d",OBJECT_NAME,(i/2)+1);
			denom = 1.0;
		}
        
		x->metro_beatdurs[j] = 4.0 * (num / denom);
        //		post("beat duration %f",4.0 * (num/denom));
		x->metro_samps[j] = x->metro_beatdurs[j] * x->onebeat_samps;
		x->metro[j] = 1.0; // initialize for instantaneous beat
	}
}

void samm_tempo(t_samm *x, t_floatarg f)
{
	int i;
	double last_tempo;
	double tempo_fac;
	
	if( f <= 0.0) {
		error("illegal tempo: %f", f);
		return;
	}
	last_tempo = x->tempo;
	x->tempo = f;
	tempo_fac = last_tempo / x->tempo; // shrink or stretch factor for beats
	x->onebeat_samps = (60.0/x->tempo) * x->sr;
	for(i = 0; i < x->metro_count; i++){
		x->metro_samps[i] = x->metro_beatdurs[i] * x->onebeat_samps;
		x->metro[i] *= tempo_fac;
	}
}


void *samm_new(t_symbol *msg, short argc, t_atom *argv)
{
241
	int i,j, default_tempo = 120;
Jonathan Wilkes's avatar
Jonathan Wilkes committed
242 243
	double divisor;
	t_samm *x;
244
    t_atom sane_defaults[2];
Jonathan Wilkes's avatar
Jonathan Wilkes committed
245
	
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    if(argc < 2){
        // If no args, set some sane defaults
        if (!argc){
            post("%s: warning: no tempo or beat streams provided: setting "
                 "tempo to 120 BPM and 1 stream to '1'");
            SETFLOAT(sane_defaults, default_tempo);
            SETFLOAT(sane_defaults+1, 1.);
            argc = 2;
            argv = sane_defaults;
        }
        else
        {
            error("%s: there must be at least 1 beat stream",OBJECT_NAME);
            return (void *)NULL;
        }
    }
    if(argc > MAXBEATS + 1)
Jonathan Wilkes's avatar
Jonathan Wilkes committed
263
    {
264 265
        error("%s: exceeded maximum of %d beat values",OBJECT_NAME, MAXBEATS);
        return (void *)NULL;
Jonathan Wilkes's avatar
Jonathan Wilkes committed
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
    }
    
    x = (t_samm *)pd_new(samm_class);
    x->metro_count = argc - 1;
    for(i=0;i< x->metro_count;i++)
        outlet_new(&x->x_obj, gensym("signal"));
	x->sr = sys_getsr();
	x->vs = sys_getblksize();
    
    x->pause = 0;
    x->mute = 0;
    
	x->beats = (double *) calloc(x->metro_count, sizeof(double));
	x->metro_samps = (double *) calloc(x->metro_count, sizeof(double));
	x->metro_beatdurs = (double *) calloc(x->metro_count, sizeof(double));
	x->metro = (double *) calloc(x->metro_count, sizeof(double));
    
	if(! x->sr ){
		x->sr = 44100;
		post("sr autoset to 44100");
	}
	
	if(argc > 0) {
		x->tempo = (double) atom_getfloatarg(0,argc,argv);
	}
    
	if( x->tempo <= 0.0 ){
293 294
		x->tempo = default_tempo;
		post("tempo autoset to %d BPM", default_tempo);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
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 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	}
	
	x->onebeat_samps = (60.0/x->tempo) * x->sr;
	
    
    
    
	for(i = 1,j = 0; i < argc; i++, j++){
        divisor = (double)atom_getfloatarg(i,argc,argv);
    	if(!divisor){
			error("%s: zero divisor given for beat stream %d",OBJECT_NAME,i);
			divisor = 1.0;
		}
        
		x->metro_beatdurs[j] = 1.0 / divisor; // argument is now DIVISOR of beat
		x->metro_samps[j] = x->metro_beatdurs[j] * x->onebeat_samps;
		x->metro[j] = 1.0; // initialize for instantaneous beat
		
	}
    //    post("there are %d beat streams",x->metro_count);
    samm_init(x,0);
    
    return (x);
}

void samm_free(t_samm *x)
{
    free(x->trigger_vec);
    free(x->beats);
    free(x->metro_samps);
    free(x->metro_beatdurs);
    free(x->metro);
    
}
void samm_init(t_samm *x,short initialized)
{
    if(!initialized){
        x->trigger_vec = (float*)calloc(x->vs, sizeof(float));
        
        
    } else {
        x->trigger_vec = (float*) realloc(x->trigger_vec,x->vs*sizeof(float));
    }
}


t_int *samm_perform(t_int *w)
{
	int i, j, k;
    //	float outval;
	t_samm *x = (t_samm *) (w[1]);
	t_float *inlet = (t_float *) (w[2]);
	t_float *beat_outlet;
	
	int n;
	
	int metro_count = x->metro_count;
	double *metro = x->metro;
	float *trigger_vec = x->trigger_vec;
	short pause = x->pause;
    
    n = (int) w[metro_count + 3];
    
    if(x->mute){
		for(i = 0, j=3; i < metro_count; i++, j++){
			beat_outlet = (t_float *) (w[j]);
			for(k=0; k < n; k++){
				beat_outlet[k] = 0.0; // use memset
			}
		}
		return (w + metro_count + 4);
    }
    /* main loop */
	for(i=0;i<n;i++)
		trigger_vec[i] = inlet[i];
    
	for(i = 0, j=3; i < metro_count; i++, j++){
		beat_outlet = (t_float *) (w[j]);
		for(k = 0; k < n; k++){
			if(trigger_vec[k]) {
				metro[i] = 1.0; // instant reset from an impulse
			}
			
			beat_outlet[k] = 0.0;
			if(! pause ){
				metro[i] -= 1.0;
				if( metro[i] <= 0){
					beat_outlet[k] = 1.0;
					metro[i] += x->metro_samps[i];
				}
			}
		}
	}
    
	return (w + metro_count + 4);
}


void samm_dsp(t_samm *x, t_signal **sp)
{
	long i;
    t_int **sigvec;
    int pointer_count;
    
    pointer_count = x->metro_count + 3; // all metros, plus 1 inlet, plus the object pointer, plus N
	
	if(x->vs != sp[0]->s_n){
		x->vs = sp[0]->s_n;
		samm_init(x,1);
	}
	if(x->sr != sp[0]->s_sr) {
		x->sr = sp[0]->s_sr;
		x->onebeat_samps = (60.0/x->tempo) * x->sr;
		for(i = 0; i < x->metro_count; i++){
			x->metro_samps[i] = x->metro_beatdurs[i] * x->onebeat_samps;
			x->metro[i] = 0;
		}
	}
	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;
	}
    //	post("attached %d pointers",pointer_count);

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