Beispiel #1
0
static void *specCentroid_tilde_new(t_symbol *s, int argc, t_atom *argv)
{
    t_specCentroid_tilde *x = (t_specCentroid_tilde *)pd_new(specCentroid_tilde_class);
	int i, isPow2;
	s=s;
	
	x->x_centroid = outlet_new(&x->x_obj, &s_float);

	x->signal_R = (t_sample *)getbytes(0);
	x->hann = (float *)getbytes(0);
	
	if(argc > 0)
	{
		x->window = atom_getfloat(argv);
		isPow2 = (int)x->window && !( ((int)x->window-1) & (int)x->window );
		
		if(!isPow2)
		{
			error("requested window size is not a power of 2. default value of 1024 used instead");
			x->window = 1024;
		};
	}
	else
		x->window = 1024;	
	
	
	x->sr = 44100.0;
	x->n = 64.0;
	x->overlap = 1;
	x->normalize = 1;
	x->last_dsp_time = clock_getlogicaltime();

	x->signal_R = (t_sample *)t_resizebytes(x->signal_R, 0, (x->window+x->n) * sizeof(t_sample));
	x->hann = (float *)t_resizebytes(x->hann, 0, x->window * sizeof(float));
	
	// initialize hann window
 	for(i=0; i<x->window; i++)
 		x->hann[i] = 0.5 * (1 - cos(2*M_PI*i/x->window));

 	for(i=0; i<(x->window+x->n); i++)
		x->signal_R[i] = 0.0;
		
    post("specCentroid~: window size: %i", (int)x->window);
    
    return (x);
}
Beispiel #2
0
static void ceil_list(t_ceil *x, t_symbol *s, int argc, t_atom *argv)
{
  int old_bytes = x->bytes, i = 0;
  x->bytes = argc*sizeof(t_atom);
  x->output_list = (t_atom *)t_resizebytes(x->output_list,old_bytes,x->bytes);
  for(i=0;i<argc;i++)
    SETFLOAT(x->output_list+i,convert(atom_getfloatarg(i,argc,argv)));
  outlet_list(x->float_outlet,0,argc,x->output_list);
}
Beispiel #3
0
static void nearestPoint_clear(t_nearestPoint *x)
{
	int i;
	
	for(i=0; i<x->numPoints; i++)
		t_freebytes(x->x_coordinates[i].coordinate, x->dimensions*sizeof(t_float));
		
	x->x_coordinates = (t_coordinate *)t_resizebytes(x->x_coordinates, x->numPoints*sizeof(t_coordinate), 0);

	x->numPoints = 0;
}
Beispiel #4
0
static void bark_samplerate(t_bark *x, t_floatarg sr)
{
    int i, oldNumFilters;

    oldNumFilters = x->numFilters;

	if(sr<=0)
	{
		error("samplerate must be > 0. default value of 44100 used instead.");
		x->sr = 44100;
	}
	else
		x->sr = sr;

	x->debounceSamp = x->debounceTime*0.001*x->sr;

    x->numFilters = tIDLib_getBarkFilterSpacing(&x->x_filterFreqs, x->sizeFilterFreqs, x->barkSpacing, x->sr);

    x->sizeFilterFreqs = x->numFilters+2;

    tIDLib_createFilterbank(x->x_filterFreqs, &x->x_filterbank, oldNumFilters, x->numFilters, x->window, x->sr);

    x->loBin = 0;
    x->hiBin = x->numFilters-1;

    x->mask = (t_float *)t_resizebytes(x->mask, oldNumFilters*sizeof(t_float), x->numFilters*sizeof(t_float));
    x->growth = (t_float *)t_resizebytes(x->growth, oldNumFilters*sizeof(t_float), x->numFilters*sizeof(t_float));
    x->numPeriods = (int *)t_resizebytes(x->numPeriods, oldNumFilters*sizeof(int), x->numFilters*sizeof(int));
    x->growthList = (t_atom *)t_resizebytes(x->growthList, oldNumFilters*sizeof(t_atom), x->numFilters*sizeof(t_atom));
    x->loudWeights = (t_float *)t_resizebytes(x->loudWeights, oldNumFilters*sizeof(t_float), x->numFilters*sizeof(t_float));

    for(i=0; i<x->numFilters; i++)
    {
	    x->mask[i] = 0.0;
	    x->growth[i] = 0.0;
	    x->numPeriods[i] = 0.0;
	    SETFLOAT(x->growthList+i, 0.0);
	    x->loudWeights[i] = 0.0;
    }
}
Beispiel #5
0
static void barkSpec_tilde_window(t_barkSpec_tilde *x, t_floatarg ws)
{
	int i, isPow2, window, oldNumFilters;

	window = ws;

	isPow2 = window && !( (window-1) & window );

	if( !isPow2 )
		error("requested window size is not a power of 2.");
	else
	{
		x->signal_R = (t_sample *)t_resizebytes(x->signal_R, (x->window+x->n)*sizeof(t_sample), (window+x->n)*sizeof(t_sample));
		x->blackman = (t_float *)t_resizebytes(x->blackman, x->window*sizeof(t_float), window*sizeof(t_float));
		x->cosine = (t_float *)t_resizebytes(x->cosine, x->window*sizeof(t_float), window*sizeof(t_float));
		x->hamming = (t_float *)t_resizebytes(x->hamming, x->window*sizeof(t_float), window*sizeof(t_float));
		x->hann = (t_float *)t_resizebytes(x->hann, x->window*sizeof(t_float), window*sizeof(t_float));

		x->window = (t_float)window;

		// re-init window functions
		tIDLib_blackmanWindow(x->blackman, x->window);
		tIDLib_cosineWindow(x->cosine, x->window);
		tIDLib_hammingWindow(x->hamming, x->window);
		tIDLib_hannWindow(x->hann, x->window);

		// init signal buffer
		for(i=0; i<x->window+x->n; i++)
			x->signal_R[i] = 0.0;

		oldNumFilters = x->numFilters;
		x->numFilters = tIDLib_getBarkFilterSpacing(&x->x_filterFreqs, x->sizeFilterFreqs, x->barkSpacing, x->sr);

		x->sizeFilterFreqs = x->numFilters+2;

		tIDLib_createFilterbank(x->x_filterFreqs, &x->x_filterbank, oldNumFilters, x->numFilters, x->window, x->sr);

		post("window size: %i", (int)x->window);
	}
}
Beispiel #6
0
static void bfcc_tilde_window(t_bfcc_tilde *x, t_floatarg f)
{
	int i, isPow2;
	
	isPow2 = (int)f && !( ((int)f-1) & (int)f );
	
	if( !isPow2 )
		error("requested window size is not a power of 2.");
	else
	{
		x->signal_R = (t_sample *)t_resizebytes(x->signal_R, (x->window+x->n) * sizeof(t_sample), (f+x->n) * sizeof(t_sample));
		x->blackman = (float *)t_resizebytes(x->blackman, x->window * sizeof(float), f * sizeof(float));
		x->cosine = (float *)t_resizebytes(x->cosine, x->window * sizeof(float), f * sizeof(float));
		x->hamming = (float *)t_resizebytes(x->hamming, x->window * sizeof(float), f * sizeof(float));
		x->hann = (float *)t_resizebytes(x->hann, x->window * sizeof(float), f * sizeof(float));
	
		x->window = (int)f;

		// re-init window functions
		for(i=0; i<x->window; i++)
		{
			x->blackman[i] = 0.42 - (0.5 * cos(2*M_PI*i/x->window)) + (0.08 * cos(4*M_PI*i/x->window));
			x->cosine[i] = sin(M_PI*i/x->window);
			x->hamming[i] = 0.5 - (0.46 * cos(2*M_PI*i/x->window));
			x->hann[i] = 0.5 * (1 - cos(2*M_PI*i/x->window));
		};	

 		
		// init signal buffer
		for(i=0; i<(x->window+x->n); i++)
			x->signal_R[i] = 0.0;
			
		bfcc_tilde_create_filterbank(x, x->bark_spacing);
		
		post("window size: %i. overlap: %i. sampling rate: %i", (int)x->window, x->overlap, (int)(x->sr/x->overlap));
	}
}
Beispiel #7
0
static void graph_ylabel(t_glist *x, t_symbol *s, int argc, t_atom *argv)
{
    int i;
    if (argc < 1) error("graph_ylabel: no x value given");
    else
    {
        x->gl_ylabelx = atom_getfloat(argv);
        argv++; argc--;
        x->gl_ylabel = (t_symbol **)t_resizebytes(x->gl_ylabel,
            x->gl_nylabels * sizeof (t_symbol *), argc * sizeof (t_symbol *));
        x->gl_nylabels = argc;
        for (i = 0; i < argc; i++) x->gl_ylabel[i] = atom_gensym(&argv[i]);
    }
    glist_redraw(x);
}
Beispiel #8
0
static void specCentroid_tilde_window(t_specCentroid_tilde *x, t_floatarg f)
{
	int i, isPow2;
	
	isPow2 = (int)f && !( ((int)f-1) & (int)f );
	
	if( !isPow2 )
		error("requested window size is not a power of 2");
	else
	{
		x->signal_R = (t_sample *)t_resizebytes(x->signal_R, (x->window+x->n) * sizeof(t_sample), (f+x->n) * sizeof(t_sample));
		x->hann = (float *)t_resizebytes(x->hann, x->window * sizeof(float), f * sizeof(float));
		x->window = (int)f;
		
		for(i=0; i<x->window; i++)
			x->hann[i] = 0.5 * (1 - cos(2*M_PI*i/x->window));

		// init signal buffer
		for(i=0; i<(x->window+x->n); i++)
			x->signal_R[i] = 0.0;
					
		post("window size: %i. overlap: %i. sampling rate: %i", (int)x->window, x->overlap, (int)(x->sr/x->overlap));
	}
}
Beispiel #9
0
static void *specCentroid_new(t_symbol *s)
{
    t_specCentroid *x = (t_specCentroid *)pd_new(specCentroid_class);
	int i;
	t_garray *a;

	x->x_centroid = outlet_new(&x->x_obj, &s_float);

	if(s)
	{
		x->x_arrayname = s;

	    if(!(a = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class)))
	        ;
	    else if(!garray_getfloatwords(a, &x->x_array_points, &x->x_vec))
	    	pd_error(x, "%s: bad template for specCentroid", x->x_arrayname->s_name);
	}
	else
		error("specCentroid: no array specified.");

	x->sr = 44100.0;
	x->window = 1; // should be a bogus size initially to force the proper resizes when a real _analyze request comes through
	x->window_func_size = 1;
	x->window_function = 3;
	x->power_spectrum = 0;

	x->max_window_size = 131072; // this seems to be the maximum size allowable by mayer_realfft();
	x->powers_of_two = (int *)getbytes(sizeof(int));

	x->powers_of_two[0] = 64; // must have at least this large of a window

	i=1;
	while(x->powers_of_two[i-1] < x->max_window_size)
	{
		x->powers_of_two = (int *)t_resizebytes(x->powers_of_two, i*sizeof(int), (i+1)*sizeof(int));
		x->powers_of_two[i] = pow(2, i+6); // +6 because we're starting at 2**6
		i++;
	}

	x->pow_two_arr_size = i;

	x->signal_R = (t_sample *)getbytes(x->window*sizeof(t_sample));

	// initialize signal_R
	memset(x->signal_R, 0, x->window*sizeof(t_sample));
	
    return (x);
}
Beispiel #10
0
void binbuf_restore(t_binbuf *x, int argc, t_atom *argv)
{
    int newsize = x->b_n + argc, i;
    t_atom *ap;
    if((ap = t_resizebytes(x->b_vec, x->b_n * sizeof(*x->b_vec),
	newsize * sizeof(*x->b_vec))))
	    x->b_vec = ap;
    else
    {
    	error("binbuf_addmessage: out of space");
    	return;
    }

    for (ap = x->b_vec + x->b_n, i = argc; i--; ap++)
    {
    	if (argv->a_type == A_SYMBOL)
    	{
	    char *str = argv->a_w.w_symbol->s_name;
    	    if (!strcmp(str, ";")) SETSEMI(ap);
    	    else if (!strcmp(str, ",")) SETCOMMA(ap);
    	    else if (str[0] == '$' && str[1] >= '0' && str[1] <= '9')
    	    {
	    	int dollsym = 0;
	    	char *str2;
		for (str2 = str + 2; *str2; str2++)
		    if (*str2 < '0' || *str2 > '9')
		    	dollsym = 1;
    	    	if (dollsym)
		    SETDOLLSYM(ap, gensym(str + 1));
		else
		{
		    int dollar = 0;
#ifdef ROCKBOX
                    dollar = atoi(argv->a_w.w_symbol->s_name + 1);
#else
    	    	    sscanf(argv->a_w.w_symbol->s_name + 1, "%d", &dollar);
#endif
    	    	    SETDOLLAR(ap, dollar);
    	    	}
	    }
    	    else *ap = *argv;
    	    argv++;
    	}
    	else *ap = *(argv++);
    }
    x->b_n = newsize;
}
Beispiel #11
0
/* ----------------------------- netreceive ------------------------- */
static void netreceive_notify(t_netreceive *x, int fd)
{
    int i;
    for (i = 0; i < x->x_nconnections; i++)
    {
        if (x->x_connections[i] == fd)
        {
            memmove(x->x_connections+i, x->x_connections+(i+1),
                sizeof(int) * (x->x_nconnections - (i+1)));
            x->x_connections = (int *)t_resizebytes(x->x_connections,
                x->x_nconnections * sizeof(int), 
                    (x->x_nconnections-1) * sizeof(int));
            x->x_nconnections--;
        }
    }
    outlet_float(x->x_ns.x_connectout, x->x_nconnections);
}
Beispiel #12
0
static void netreceive_closeall(t_netreceive *x)
{
    int i;
    for (i = 0; i < x->x_nconnections; i++)
    {
        sys_rmpollfn(x->x_connections[i]);
        sys_closesocket(x->x_connections[i]);
    }
    x->x_connections = (int *)t_resizebytes(x->x_connections, 
        x->x_nconnections * sizeof(int), 0);
    x->x_nconnections = 0;
    if (x->x_ns.x_sockfd >= 0)
    {
        sys_rmpollfn(x->x_ns.x_sockfd);
        sys_closesocket(x->x_ns.x_sockfd);
    }
    x->x_ns.x_sockfd = -1;
}
Beispiel #13
0
static void specCentroid_window(t_specCentroid *x, t_floatarg w)
{
	int isPow2;
	
	isPow2 = (int)w && !( ((int)w-1) & (int)w );
	
	if( !isPow2 )
		error("requested window size is not a power of 2.");
	else
	{
		x->signal_R = (t_sample *)t_resizebytes(x->signal_R, x->window * sizeof(t_sample), w * sizeof(t_sample));
	
		x->window = (int)w;
 		
		// init signal buffer
		memset(x->signal_R, 0, x->window*sizeof(t_sample));
					
		post("window size: %i. sampling rate: %i", (int)x->window, (int)x->sr);
	}
}
Beispiel #14
0
static void zeroCrossing_tilde_window(t_zeroCrossing_tilde *x, t_floatarg w)
{
	int i;

	if(w < 64)
	{
		error("minimum window size is 64 samples.");
		w = 64;
	};
		

	x->signal_R = (t_sample *)t_resizebytes(x->signal_R, (x->window+x->n) * sizeof(t_sample), (w+x->n) * sizeof(t_sample));
	x->window = (int)w;

	// init signal buffer
	for(i=0; i<(x->window+x->n); i++)
		x->signal_R[i] = 0.0;
				
	post("window size: %i. overlap: %i. sampling rate: %i", (int)x->window, x->overlap, (int)(x->sr/x->overlap));
}
Beispiel #15
0
static void bfcc_tilde_filterbank_multiply(t_sample *in, int normalize, t_filter *x_filterbank, t_indices *x_indices, int num_filters)
{
	int i, j, k;
	float sum, sumsum, *filter_power;

	// create local memory
	filter_power = (float *)getbytes(0);
	filter_power = (float *)t_resizebytes(filter_power, 0, num_filters * sizeof(float));

 	sumsum = 0;
	
	for(i=0; i<num_filters; i++)
    {
	   	sum = 0;
 
		for(j=x_indices[i].index[0], k=0; j< (int)(x_indices[i].index[1] + 1); j++, k++)	
	    	sum += in[j] * x_filterbank[i].filter[k];
		
		sum /= k;
		filter_power[i] = sum;  // get the total power.  another weighting might be better.

 		sumsum += sum;  // normalize so power in all bands sums to 1
	};
	
	if(normalize)
	{
		// prevent divide by 0
		if(sumsum==0)
			sumsum=1;
		else
			sumsum = 1/sumsum; // take the reciprocal here to save a divide below
	}
	else
		sumsum=1;
		
	for(i=0; i<num_filters; i++)
		in[i] = filter_power[i]*sumsum;

	// free local memory
	t_freebytes(filter_power, num_filters * sizeof(float));
}
Beispiel #16
0
/***********************buffsize**********************/
void phasevoc_tilde_buffsize (t_phasevoc_tilde *x, t_floatarg g)
{
  if (g<=0)
    {
      post("buffsize must be greater than 0: buffsize = 5");
      x->circBuffSeconds = 5;
    }
  else
    {
      x->circBuffSeconds = g;
      // memset(x->circBuff, 0, sizeof(float) * x->circBuffLength);
      x->circBuffLength = x->circBuffSeconds * x->sampleRate;
      
      x->circBuff = (t_float *)t_getbytes(0);
      x->circBuff = (t_float *)t_resizebytes(x->circBuff, 0, sizeof(float) * x->circBuffLength);
      
      phasevoc_tilde_reinit(x);
      
      post("input buffer is %f seconds", g);
    }
}
Beispiel #17
0
void binbuf_add(t_binbuf *x, int argc, t_atom *argv)
{
    int newsize = x->b_n + argc, i;
    t_atom *ap;
    if((ap = t_resizebytes(x->b_vec, x->b_n * sizeof(*x->b_vec),
	newsize * sizeof(*x->b_vec))))
	    x->b_vec = ap;
    else
    {
    	error("binbuf_addmessage: out of space");
    	return;
    }
#if 0
    startpost("binbuf_add: ");
    postatom(argc, argv);
    endpost();
#endif
    for (ap = x->b_vec + x->b_n, i = argc; i--; ap++)
    	*ap = *(argv++);
    x->b_n = newsize;
}
Beispiel #18
0
static void nearestPoint_add(t_nearestPoint *x, t_symbol *s, int argc, t_atom *argv)
{
	int i, pointIdx, dimensions;

	pointIdx = x->numPoints;
	dimensions = argc;
	s=s; // to get rid of 'unused variable' warning

	if(dimensions == x->dimensions)
	{
		x->x_coordinates = (t_coordinate *)t_resizebytes(x->x_coordinates, x->numPoints * sizeof(t_coordinate), (x->numPoints+1) * sizeof(t_coordinate));
		
		x->x_coordinates[pointIdx].coordinate = (t_float *)t_getbytes(dimensions*sizeof(t_float));
		
		x->numPoints++;
				
		for(i=0; i<dimensions; i++)
			x->x_coordinates[pointIdx].coordinate[i] = atom_getfloat(argv+i);
	}
	else
		error("nearestPoint: dimensionality mismatch.");
}
Beispiel #19
0
static void *zeroCrossing_tilde_new(t_symbol *s, int argc, t_atom *argv)
{
    t_zeroCrossing_tilde *x = (t_zeroCrossing_tilde *)pd_new(zeroCrossing_tilde_class);
	int i;
	
	x->x_crossings = outlet_new(&x->x_obj, &s_float);
	s=s;
	
	x->signal_R = (t_sample *)getbytes(0);

	if(argc > 0)
	{
		x->window = atom_getfloat(argv);
		
		if(x->window < 64)
		{
			error("minimum window size is 64 samples.");
			x->window = 64;
		};
	}
	else
		x->window = 1024;	
	
	
	x->sr = 44100.0;
	x->n = 64.0;
	x->overlap = 1;
	x->last_dsp_time = clock_getlogicaltime();

	x->signal_R = (t_sample *)t_resizebytes(x->signal_R, 0, (x->window+x->n) * sizeof(t_sample));

 	for(i=0; i<(x->window+x->n); i++)
		x->signal_R[i] = 0.0;
		
    post("zeroCrossing~: window size: %i", (int)x->window);
    
    return (x);
}
Beispiel #20
0
static void netreceive_connectpoll(t_netreceive *x)
{
    int fd = accept(x->x_ns.x_sockfd, 0, 0);
    if (fd < 0) post("netreceive: accept failed");
    else
    {
        int nconnections = x->x_nconnections+1;
        
        x->x_connections = (int *)t_resizebytes(x->x_connections,
            x->x_nconnections * sizeof(int), nconnections * sizeof(int));
        x->x_connections[x->x_nconnections] = fd;
        if (x->x_ns.x_bin)
            sys_addpollfn(fd, (t_fdpollfn)netsend_readbin, x);
        else
        {
            t_socketreceiver *y = socketreceiver_new((void *)x, 
            (t_socketnotifier)netreceive_notify,
                (x->x_ns.x_msgout ? netsend_doit : 0), 0);
            sys_addpollfn(fd, (t_fdpollfn)socketreceiver_read, y);
        }
        outlet_float(x->x_ns.x_connectout, (x->x_nconnections = nconnections));
    }
}
Beispiel #21
0
static void specRolloff_tilde_window(t_specRolloff_tilde *x, t_floatarg w)
{
	int i, window, isPow2;

	window = w;

	isPow2 = window && !( (window-1) & window );

	if( !isPow2 )
		error("requested window size is not a power of 2");
	else
	{
		x->signal_R = (t_sample *)t_resizebytes(x->signal_R, (x->window+x->n) * sizeof(t_sample), (window+x->n) * sizeof(t_sample));
		x->blackman = (t_float *)t_resizebytes(x->blackman, x->window*sizeof(t_float), window*sizeof(t_float));
		x->cosine = (t_float *)t_resizebytes(x->cosine, x->window*sizeof(t_float), window*sizeof(t_float));
		x->hamming = (t_float *)t_resizebytes(x->hamming, x->window*sizeof(t_float), window*sizeof(t_float));
		x->hann = (t_float *)t_resizebytes(x->hann, x->window*sizeof(t_float), window*sizeof(t_float));
		x->binFreqs = (t_float *)t_resizebytes(x->binFreqs, x->window*sizeof(t_float), window*sizeof(t_float));

		x->window = (t_float)window;

		// re-init window functions
		tIDLib_blackmanWindow(x->blackman, x->window);
		tIDLib_cosineWindow(x->cosine, x->window);
		tIDLib_hammingWindow(x->hamming, x->window);
		tIDLib_hannWindow(x->hann, x->window);

		// init signal buffer
		for(i=0; i<(x->window+x->n); i++)
			x->signal_R[i] = 0.0;

		// freqs for each bin based on current window size and sample rate
		for(i=0; i<x->window; i++)
			x->binFreqs[i] = (x->sr/x->window)*i;

		post("window size: %i", (int)x->window);
	}
}
Beispiel #22
0
static void specCentroid_analyze(t_specCentroid *x, t_floatarg start, t_floatarg n)
{
	int i, j, old_window, window, window_half, start_samp, end_samp, length_samp;
	float *window_func_ptr;
    float dividend, divisor, centroid;
	t_sample *signal_I;
	t_garray *a;

	if(!(a = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class)))
        pd_error(x, "%s: no such array", x->x_arrayname->s_name);
    else if(!garray_getfloatwords(a, &x->x_array_points, &x->x_vec))
    	pd_error(x, "%s: bad template for specCentroid", x->x_arrayname->s_name);
	else
	{

	start_samp = start;
	
	if(start_samp < 0)
		start_samp = 0;

	if(n)
		end_samp = start_samp + n-1;
	else
		end_samp = start_samp + x->window-1;
		
	if(end_samp > x->x_array_points)
		end_samp = x->x_array_points-1;

	length_samp = end_samp - start_samp + 1;

	if(end_samp <= start_samp)
	{
		error("bad range of samples.");
		return;
	}
		
	if(length_samp > x->powers_of_two[x->pow_two_arr_size-1])
	{
		post("WARNING: specCentroid: window truncated because requested size is larger than the current max_window setting. Use the max_window method to allow larger windows. Sizes of more than 131072 may produce unreliable results.");
		length_samp = x->powers_of_two[x->pow_two_arr_size-1];
		window = length_samp;
		end_samp = start_samp + window - 1;
	}
	else
	{
		i=0;
		while(length_samp > x->powers_of_two[i])
			i++;

		window = x->powers_of_two[i];
	}

	window_half = window * 0.5;

	if(x->window != window)
	{
		old_window = x->window;
		x->window = window;
		x->signal_R = (t_sample *)t_resizebytes(x->signal_R, old_window*sizeof(t_sample), window*sizeof(t_sample));
	}
	
	if(x->window_func_size != length_samp)
	{
		x->blackman = (float *)t_resizebytes(x->blackman, x->window_func_size*sizeof(float), length_samp*sizeof(float));
		x->cosine = (float *)t_resizebytes(x->cosine, x->window_func_size*sizeof(float), length_samp*sizeof(float));
		x->hamming = (float *)t_resizebytes(x->hamming, x->window_func_size*sizeof(float), length_samp*sizeof(float));
		x->hann = (float *)t_resizebytes(x->hann, x->window_func_size*sizeof(float), length_samp*sizeof(float));

		x->window_func_size = length_samp;
		
		specCentroid_blackman_window(x->blackman, x->window_func_size);
		specCentroid_cosine_window(x->cosine, x->window_func_size);
		specCentroid_hamming_window(x->hamming, x->window_func_size);
		specCentroid_hann_window(x->hann, x->window_func_size);
	}

	// create local memory
	signal_I = (t_sample *)getbytes(window_half*sizeof(t_sample));

	// construct analysis window
	for(i=0, j=start_samp; j<=end_samp; i++, j++)
		x->signal_R[i] = x->x_vec[j].w_float;

	// set window
	window_func_ptr = x->hann; //default case to get rid of compile warning
	switch(x->window_function)
	{
		case 0:
			window_func_ptr = x->blackman;
			break;
		case 1:
			window_func_ptr = x->cosine;
			break;
		case 2:
			window_func_ptr = x->hamming;
			break;
		case 3:
			window_func_ptr = x->hann;
			break;
		default:
			break;
	};
	
	// then multiply against the chosen window
	for(i=0; i<length_samp; i++, window_func_ptr++)
		x->signal_R[i] *= *window_func_ptr;

	// then zero pad the end
	for(; i<window; i++)
		x->signal_R[i] = 0.0;

	mayer_realfft(window, x->signal_R);
	specCentroid_realfft_unpack(window, window_half, x->signal_R, signal_I);
	specCentroid_power(window_half, x->signal_R, signal_I);

	if(!x->power_spectrum)
		specCentroid_mag(window_half, x->signal_R);

	dividend=0;
	divisor=0;
	centroid=0;
	
	for(i=0; i<window_half; i++)
		dividend += x->signal_R[i]*(i*(x->sr/window));  // weight by bin freq

	for(i=0; i<window_half; i++)
		divisor += x->signal_R[i];
		
	if(divisor==0) divisor = 1; // don't divide by zero
	
	centroid = dividend/divisor;
		
	outlet_float(x->x_centroid, centroid);

	// free local memory
	t_freebytes(signal_I, window_half*sizeof(t_sample));

	}
}
Beispiel #23
0
static void *specBrightness_tilde_new(t_symbol *s, int argc, t_atom *argv)
{
    t_specBrightness_tilde *x = (t_specBrightness_tilde *)pd_new(specBrightness_tilde_class);
	int i, isPow2;
	s=s;
	
	x->x_brightness = outlet_new(&x->x_obj, &s_float);
	
	x->signal_R = (t_sample *)getbytes(0);
	x->hann = (float *)getbytes(0);
	x->bin_freqs = (float *)getbytes(0);
	
	x->sr = 44100.0;
	x->n = 64.0;

	if(argc > 1)
	{
		x->window = atom_getfloat(argv);
		isPow2 = (int)x->window && !( ((int)x->window-1) & (int)x->window );
		
		if(!isPow2)
		{
			error("requested window size is not a power of 2. default value of 1024 used instead");
			x->window = 1024;
		};

		x->freq_boundary = atom_getfloat(argv+1);
		if(x->freq_boundary<0)
		{
			error("boundary frequency must be a positive real number and less than Nyquist.");
			x->freq_boundary = 1200;
		}
		else if(x->freq_boundary>(x->sr/2))
		{
			error("boundary frequency must be a positive real number and less than Nyquist.");
			x->freq_boundary = 1200;
		};
		
	}
	else if(argc > 0)
	{
		x->window = atom_getfloat(argv);
		isPow2 = (int)x->window && !( ((int)x->window-1) & (int)x->window );
		
		if(!isPow2)
		{
			error("requested window size is not a power of 2. default value of 1024 used instead");
			x->window = 1024;
		};
		
		x->freq_boundary = 1200;

	}
	else
	{
		x->window = 1024;
		x->freq_boundary = 1200;
	};

	x->signal_R = (t_sample *)t_resizebytes(x->signal_R, 0, (x->window+x->n) * sizeof(t_sample));
	x->hann = (float *)t_resizebytes(x->hann, 0, x->window * sizeof(float));
	x->bin_freqs = (float *)t_resizebytes(x->bin_freqs, 0, x->window * sizeof(float));
	
	// x->sr declared above before creation argument parsing	
	x->overlap = 1;
	x->normalize = 1;
	x->last_dsp_time = clock_getlogicaltime();

	// freqs for each bin based on current window size
	for(i=0; i<x->window; i++)
		x->bin_freqs[i] = ((x->sr/x->overlap)/x->window) * i;
		
	// initialize hann window
 	for(i=0; i<x->window; i++)
 		x->hann[i] = 0.5 * (1 - cos(2*M_PI*i/x->window));

 	for(i=0; i<(x->window+x->n); i++)
		x->signal_R[i] = 0.0;
		
    post("specBrightness~: window size: %i. boundary freq: %.2f", (int)x->window, x->freq_boundary);
    
    return (x);
}
Beispiel #24
0
static void specSpread_tilde_bang(t_specSpread_tilde *x)
{
    int i, j, window, window_half, bang_sample;
    float dividend, divisor, centroid, spread;
    t_sample *signal_R, *signal_I;
	double current_time;

    window = x->window;
    window_half = window*0.5;

	// create local memory
	signal_R = (t_sample *)getbytes(0);
	signal_I = (t_sample *)getbytes(0);
	signal_R = (t_sample *)t_resizebytes(signal_R, 0, window*sizeof(t_sample));
	signal_I = (t_sample *)t_resizebytes(signal_I, 0, window_half*sizeof(t_sample));
    
	current_time = clock_gettimesince(x->last_dsp_time);
	bang_sample = (int)(((current_time/1000.0)*x->sr)+0.5); // round

	if (bang_sample < 0)
        bang_sample = 0;
    else if ( bang_sample >= x->n )
        bang_sample = x->n - 1;
            
	// construct analysis window using bang_sample as the end of the window
	for(i=0, j=bang_sample; i<window; i++, j++)
		signal_R[i] = x->signal_R[j];
	
	specSpread_tilde_hann(window, signal_R, x->hann);
	mayer_realfft(window, signal_R);
	specSpread_tilde_realfft_unpack(window, window_half, signal_R, signal_I);
	specSpread_tilde_abs(window_half, signal_R, signal_I);
	
// 	if(x->normalize)
// 		specSpread_tilde_normal(window_half, signal_R);

	dividend=0;
	divisor=0;
	centroid=0;
	
	for(i=0; i<window_half; i++)
	{
		dividend += signal_R[i] * x->bin_freqs[i];  // weight by bin freq
		divisor += signal_R[i];
	}
	
	if(divisor==0) divisor = 1; // don't divide by zero
	
	centroid = dividend/divisor;

	dividend=0;
	spread=0;

	for(i=0; i<window_half; i++)
		dividend += ( (x->bin_freqs[i] - centroid) * (x->bin_freqs[i] - centroid) ) * signal_R[i];

	if(divisor==0) divisor = 1; // don't divide by zero

	spread = sqrt(dividend/divisor);

	outlet_float(x->x_spread, spread);

	// free local memory
	t_freebytes(signal_R, window * sizeof(t_sample));
	t_freebytes(signal_I, window_half * sizeof(t_sample));
}
Beispiel #25
0
void class_addmethod(t_class *c, t_method fn, t_symbol *sel,
    t_atomtype arg1, ...)
{
    va_list ap;
    t_methodentry *m;
    t_atomtype argtype = arg1;
    int nargs;
    
    va_start(ap, arg1);
        /* "signal" method specifies that we take audio signals but
        that we don't want automatic float to signal conversion.  This
        is obsolete; you should now use the CLASS_MAINSIGNALIN macro. */
    if (sel == &s_signal)
    {
        if (c->c_floatsignalin)
            post("warning: signal method overrides class_mainsignalin");
        c->c_floatsignalin = -1;
    }
        /* check for special cases.  "Pointer" is missing here so that
        pd_objectmaker's pointer method can be typechecked differently.  */
    if (sel == &s_bang)
    {
        if (argtype) goto phooey;
        class_addbang(c, fn);
    }
    else if (sel == &s_float)
    {
        if (argtype != A_FLOAT || va_arg(ap, t_atomtype)) goto phooey;
        class_doaddfloat(c, fn);
    }
    else if (sel == &s_symbol)
    {
        if (argtype != A_SYMBOL || va_arg(ap, t_atomtype)) goto phooey;
        class_addsymbol(c, fn);
    }
    else if (sel == &s_blob) /* MP 20070106 blob type */
    {
        post("class_addmethod: %p", fn);
        if (argtype != A_BLOB || va_arg(ap, t_atomtype)) goto phooey;
        class_addblob(c, fn);
    }
    else if (sel == &s_list)
    {
        if (argtype != A_GIMME) goto phooey;
        class_addlist(c, fn);
    }
    else if (sel == &s_anything)
    {
        if (argtype != A_GIMME) goto phooey;
        class_addanything(c, fn);
    }
    else
    {
        /* Pd-extended doesn't use the aliasing automagic
        int i;
        for (i = 0; i < c->c_nmethod; i++)
            if (c->c_methods[i].me_name == sel)
        {
            char nbuf[80];
            snprintf(nbuf, 80, "%s_aliased", sel->s_name);
            c->c_methods[i].me_name = gensym(nbuf);
            if (c == pd_objectmaker)
                post("warning: class '%s' overwritten; old one renamed '%s'",
                    sel->s_name, nbuf);
            else post("warning: old method '%s' for class '%s' renamed '%s'",
                sel->s_name, c->c_name->s_name, nbuf);
        }
        */
        c->c_methods = t_resizebytes(c->c_methods,
            c->c_nmethod * sizeof(*c->c_methods),
            (c->c_nmethod + 1) * sizeof(*c->c_methods));
        m = c->c_methods +  c->c_nmethod;
        c->c_nmethod++;
        m->me_name = sel;
        m->me_fun = (t_gotfn)fn;
        nargs = 0;
        while (argtype != A_NULL && nargs < MAXPDARG)
        {
            m->me_arg[nargs++] = argtype;
            argtype = va_arg(ap, t_atomtype);
        }
        if (argtype != A_NULL)
            error("%s_%s: only 5 arguments are typecheckable; use A_GIMME",
                c->c_name->s_name, sel->s_name);
        va_end(ap);
        m->me_arg[nargs] = A_NULL;
    }
    return;
phooey:
    bug("class_addmethod: %s_%s: bad argument types\n",
        c->c_name->s_name, sel->s_name);
}
Beispiel #26
0
static void *specBrightness_new(t_symbol *s, t_floatarg boundary)
{
    t_specBrightness *x = (t_specBrightness *)pd_new(specBrightness_class);
	int i;
	t_garray *a;

	x->x_brightness = outlet_new(&x->x_obj, &s_float);

	x->sr = 44100.0;
	
	if(boundary)
	{
		x->x_arrayname = s;

	    if(!(a = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class)))
	        ;
	    else if(!garray_getfloatwords(a, &x->x_arrayPoints, &x->x_vec))
	    	pd_error(x, "%s: bad template for specBrightness", x->x_arrayname->s_name);
	    	
		x->freqBoundary = boundary;

		if(x->freqBoundary<0)
		{
			error("boundary frequency must be a positive real number and less than Nyquist.");
			x->freqBoundary = 1200;
		}
		else if(x->freqBoundary>(x->sr*0.5))
		{
			error("boundary frequency must be a positive real number and less than Nyquist.");
			x->freqBoundary = 1200;
		};
	}
	else if(s)
	{
		x->x_arrayname = s;

	    if(!(a = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class)))
	        ;
	    else if(!garray_getfloatwords(a, &x->x_arrayPoints, &x->x_vec))
	    	pd_error(x, "%s: bad template for specBrightness", x->x_arrayname->s_name);
	    	
	    x->freqBoundary = 1200;
	}
	else
		error("specBrightness: no array specified.");

	x->window = 1; // should be a bogus size initially to force the proper resizes when a real _analyze request comes through
	x->windowFuncSize = 1;
	x->windowFunction = 4; // 4 is hann window
	x->powerSpectrum = 0; // choose mag (0) or power (1) spec in the specBrightness computation
	x->binBoundary = 0;

	x->maxWindowSize = MAXWINDOWSIZE; // this seems to be the maximum size allowable by mayer_realfft();
	x->powersOfTwo = (int *)t_getbytes(sizeof(int));

	x->powersOfTwo[0] = 64; // must have at least this large of a window

	i=1;
	while(x->powersOfTwo[i-1] < x->maxWindowSize)
	{
		x->powersOfTwo = (int *)t_resizebytes(x->powersOfTwo, i*sizeof(int), (i+1)*sizeof(int));
		x->powersOfTwo[i] = pow(2, i+6); // +6 because we're starting at 2**6
		i++;
	}

	x->powTwoArrSize = i;

	x->signal_R = (t_sample *)t_getbytes(x->window*sizeof(t_sample));

	for(i=0; i<x->window; i++)
		x->signal_R[i] = 0.0;

  	x->blackman = (t_float *)t_getbytes(x->windowFuncSize*sizeof(t_float));
  	x->cosine = (t_float *)t_getbytes(x->windowFuncSize*sizeof(t_float));
  	x->hamming = (t_float *)t_getbytes(x->windowFuncSize*sizeof(t_float));
  	x->hann = (t_float *)t_getbytes(x->windowFuncSize*sizeof(t_float));

 	// initialize signal windowing functions
	tIDLib_blackmanWindow(x->blackman, x->windowFuncSize);
	tIDLib_cosineWindow(x->cosine, x->windowFuncSize);
	tIDLib_hammingWindow(x->hamming, x->windowFuncSize);
	tIDLib_hannWindow(x->hann, x->windowFuncSize);

	x->binFreqs = (t_float *)t_getbytes(x->window*sizeof(t_float));

    return (x);
}
Beispiel #27
0
/************new***********/
void *phasevoc_tilde_new(t_symbol *s, int argc, t_atom *argv)
{
 

  t_phasevoc_tilde *x = (t_phasevoc_tilde *)pd_new(phasevoc_tilde_class);
 
 

  inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("timestretch"));
  inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("pitchshift"));
  outlet_new(&x->x_obj, &s_signal);
  x->buffPos = outlet_new(&x->x_obj, &s_float);

  x->sampleRate = sys_getsr();
  x->circBuffSeconds = 5;
  x->circBuffLength = x->sampleRate * x->circBuffSeconds;
  x->circBuffIndex = 0;
  x->circBuffWindowBackIndex = x->circBuffWindowStart = 0;
  x->sampHoldBuffIndex = x->sampHoldBuffWindowIndex = x->sampHoldBuffWindowStart = x->circBuffToSampHoldCopyIndex = 0;
  x->sampHoldBuffLength = 0;
  x->timestretch = 1.0;
  x->pitchshift = 0.0;
  x->fftSize = 1024;
  x->fftHalfSize = x->fftSize >> 1;
  x->hopSize = 64;
  x->overlapChunk = x->fftSize>>2;
  x->overlap = x->fftSize/x->hopSize;
  x->twoPi = 8.0f * atanf(1.0f);
  x->bufferPosition = 0;
  x->n = 64;
  x->inputTime = x->outputTime = x->dsp_tick = 0;
  x->buffer_limit = x->fftSize/x->n/x->overlap;
  x->fft_tick = 0;
  x->outputTime = 0;
  x->phaselock = 0;
  x->pause = 0;
  x->waszeroflag = 0;



  while (argc > 0)
    {
      t_symbol *firstarg = atom_getsymbolarg(0, argc, argv);
      if (!strcmp(firstarg->s_name, "-buffsize"))
        {
	  phasevoc_tilde_buffsize(x, atom_getfloatarg(1, argc, argv));
	  argc-=2, argv+=2;
        }
      else if (!strcmp(firstarg->s_name, "-windowsize"))
        {
	  phasevoc_tilde_windowsize(x, atom_getfloatarg(1, argc, argv));
	  argc-=2, argv+=2;
        }
      else if (!strcmp(firstarg->s_name, "-hopsize"))
        {
	  phasevoc_tilde_hopsize(x, atom_getfloatarg(1, argc, argv));
	  argc-=2, argv+=2;
        }
      else
	{
	  pd_error(x, "phasevoc: %s: unknown flag or argument missing",
		   firstarg->s_name);
	  argc--, argv++;
	}

    }


  
  //
  x->circBuff = (t_float *)t_getbytes(0);
  x->circBuff = (t_float *)t_resizebytes(x->circBuff, 0, sizeof(float) * x->circBuffLength);
  //
  x->sampHoldBuff = (t_float *)t_getbytes(0);
  x->sampHoldBuff = (t_float *)t_resizebytes(x->sampHoldBuff, 0, sizeof(float) * x->circBuffLength);
  //
  x->inFrontBuff = (t_float *)t_getbytes(0);
  x->inFrontBuff = (t_float *)t_resizebytes(x->inFrontBuff, 0, sizeof(float) * x->fftSize);
  //
  x->inBackBuff = (t_float *)t_getbytes(0);
  x->inBackBuff = (t_float *)t_resizebytes(x->inBackBuff, 0, sizeof(float) * x->fftSize);
  //
  x->outBuff = (t_float *)t_getbytes(0);
  x->outBuff = (t_float *)t_resizebytes(x->outBuff, 0, sizeof(float) * (x->hopSize + x->n));
  //
  x->inFrontShift = (t_float *)t_getbytes(0);
  x->inFrontShift = (t_float *)t_resizebytes(x->inFrontShift, 0, sizeof(float) * x->fftSize);
  //
  x->inBackShift = (t_float *)t_getbytes(0);
  x->inBackShift = (t_float *)t_resizebytes(x->inBackShift, 0, sizeof(float) * x->fftSize);
  //
  x->frontReal = (t_float *)t_getbytes(0);
  x->frontReal = (t_float *)t_resizebytes(x->frontReal, 0, sizeof(float) * (x->fftHalfSize+1));
  //
  x->frontImag = (t_float *)t_getbytes(0);
  x->frontImag = (t_float *)t_resizebytes(x->frontImag, 0, sizeof(float) * (x->fftHalfSize+1));
  //
  x->backReal = (t_float *)t_getbytes(0);
  x->backReal = (t_float *)t_resizebytes(x->backReal, 0, sizeof(float) * (x->fftHalfSize+1));
  //
  x->backImag = (t_float *)t_getbytes(0);
  x->backImag = (t_float *)t_resizebytes(x->backImag, 0, sizeof(float) * (x->fftHalfSize+1));
  //
  x->prevReal = (t_float *)t_getbytes(0);
  x->prevReal = (t_float *)t_resizebytes(x->prevReal, 0, sizeof(float) * (x->fftHalfSize+1));
  //
  x->prevImag = (t_float *)t_getbytes(0);
  x->prevImag = (t_float *)t_resizebytes(x->prevImag, 0, sizeof(float) * (x->fftHalfSize+1));
  //
  x->conjReal = (t_float *)t_getbytes(0);
  x->conjReal = (t_float *)t_resizebytes(x->conjReal, 0, sizeof(float) * (x->fftHalfSize+1));
  //
  x->conjImag = (t_float *)t_getbytes(0);
  x->conjImag = (t_float *)t_resizebytes(x->conjImag, 0, sizeof(float) * (x->fftHalfSize+1));
  //
  x->outReal = (t_float *)t_getbytes(0);
  x->outReal = (t_float *)t_resizebytes(x->outReal, 0, sizeof(float) * (x->fftHalfSize+1));
  //
  x->outImag = (t_float *)t_getbytes(0);
  x->outImag = (t_float *)t_resizebytes(x->outImag, 0, sizeof(float) * (x->fftHalfSize+1));
  //
  x->rsqrt = (t_float *)t_getbytes(0);
  x->rsqrt = (t_float *)t_resizebytes(x->rsqrt, 0, sizeof(float) * (x->fftHalfSize+1));
  //
  x->outSpectra = (t_float *)t_getbytes(0);
  x->outSpectra = (t_float *)t_resizebytes(x->outSpectra, 0, sizeof(float) * x->fftSize);
  //
  x->analWindow = (t_float *)t_getbytes(0);
  x->analWindow = (t_float *)t_resizebytes(x->analWindow, 0, sizeof(float) * x->fftSize);
  //
  x->synthWindow = (t_float *)t_getbytes(0);
  x->synthWindow = (t_float *)t_resizebytes(x->synthWindow, 0, sizeof(float) * x->fftSize);
 //
  x->nonoverlappedOutBuff = (t_float *)t_getbytes(0);
  x->nonoverlappedOutBuff = (t_float *)t_resizebytes(x->nonoverlappedOutBuff, 0, sizeof(float) * x->fftSize * x->overlap);
 

  init_window(x);



 
 

return (void *)x;
}
Beispiel #28
0
    /* convert text to a binbuf */
void binbuf_text(t_binbuf *x, char *text, size_t size)
{
    char buf[MAXPDSTRING+1], *bufp, *ebuf = buf+MAXPDSTRING;
    const char *textp = text, *etext = text+size;
    t_atom *ap;
    int nalloc = 16, natom = 0;
    t_freebytes(x->b_vec, x->b_n * sizeof(*x->b_vec));
    x->b_vec = t_getbytes(nalloc * sizeof(*x->b_vec));
    ap = x->b_vec;
    x->b_n = 0;
    while (1)
    {
#ifndef ROCKBOX
	int type;
#endif
	    /* skip leading space */
	while ((textp != etext) && (*textp == ' ' || *textp == '\n'
	    || *textp == '\r' || *textp == '\t')) textp++;
	if (textp == etext) break;
	if (*textp == ';') SETSEMI(ap), textp++;
	else if (*textp == ',') SETCOMMA(ap), textp++;
	else
	{
	    	/* it's an atom other than a comma or semi */
	    char c;
	    int floatstate = 0, slash = 0, /* lastslash = 0, */
	    	firstslash = (*textp == '\\');
	    bufp = buf;
	    do
	    {
		c = *bufp = *textp++;
		/* lastslash = slash; */
		slash = (c == '\\');

		if (floatstate >= 0)
		{
		    int digit = (c >= '0' && c <= '9'),
			dot = (c == '.'), minus = (c == '-'),
			plusminus = (minus || (c == '+')),
			expon = (c == 'e' || c == 'E');
		    if (floatstate == 0)    /* beginning */
		    {
			if (minus) floatstate = 1;
			else if (digit) floatstate = 2;
			else if (dot) floatstate = 3;
			else floatstate = -1;
		    }
		    else if (floatstate == 1)	/* got minus */
		    {
			if (digit) floatstate = 2;
			else if (dot) floatstate = 3;
			else floatstate = -1;
		    }
		    else if (floatstate == 2)	/* got digits */
		    {
			if (dot) floatstate = 4;
			else if (expon) floatstate = 6;
			else if (!digit) floatstate = -1;
		    }
		    else if (floatstate == 3)	/* got '.' without digits */
		    {
			if (digit) floatstate = 5;
			else floatstate = -1;
		    }
		    else if (floatstate == 4)	/* got '.' after digits */
		    {
			if (digit) floatstate = 5;
			else if (expon) floatstate = 6;
			else floatstate = -1;
		    }
		    else if (floatstate == 5)	/* got digits after . */
		    {
			if (expon) floatstate = 6;
			else if (!digit) floatstate = -1;
		    }
		    else if (floatstate == 6)	/* got 'e' */
		    {
			if (plusminus) floatstate = 7;
			else if (digit) floatstate = 8;
			else floatstate = -1;
		    }
		    else if (floatstate == 7)	/* got plus or minus */
		    {
			if (digit) floatstate = 8;
			else floatstate = -1;
		    }
		    else if (floatstate == 8)	/* got digits */
		    {
			if (!digit) floatstate = -1;
		    }
		}
		if (!slash) bufp++;
	    }
	    while (textp != etext && bufp != ebuf && 
		(slash || (*textp != ' ' && *textp != '\n' && *textp != '\r'
		    && *textp != '\t' &&*textp != ',' && *textp != ';')));
	    *bufp = 0;
#if 0
	    post("buf %s", buf);
#endif

	    if (*buf == '$' && buf[1] >= '0' && buf[1] <= '9' && !firstslash)
	    {
		for (bufp = buf+2; *bufp; bufp++)
		    if (*bufp < '0' || *bufp > '9')
		{
		    SETDOLLSYM(ap, gensym(buf+1));
		    goto didit;
		}
		SETDOLLAR(ap, atoi(buf+1));
	    didit: ;
	    }
	    else
	    {
		if (floatstate == 2 || floatstate == 4 || floatstate == 5 ||
		    floatstate == 8)
		    	SETFLOAT(ap, atof(buf));
		else SETSYMBOL(ap, gensym(buf));
	    }
	}
	ap++;
	natom++;
	if (natom == nalloc)
	{
	    x->b_vec = t_resizebytes(x->b_vec, nalloc * sizeof(*x->b_vec),
		nalloc * (2*sizeof(*x->b_vec)));
	    nalloc = nalloc * 2;
	    ap = x->b_vec + natom;
	}
	if (textp == etext) break;
    }
    /* reallocate the vector to exactly the right size */
    x->b_vec = t_resizebytes(x->b_vec, nalloc * sizeof(*x->b_vec),
	natom * sizeof(*x->b_vec));
    x->b_n = natom;
}
Beispiel #29
0
static void specCentroid_analyze(t_specCentroid *x, t_floatarg start, t_floatarg n)
{
	int i, j, oldWindow, window, windowHalf, startSamp, endSamp, lengthSamp;
    t_float dividend, divisor, centroid, *windowFuncPtr;
	t_sample *signal_I;
	t_garray *a;

	if(!(a = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class)))
        pd_error(x, "%s: no such array", x->x_arrayname->s_name);
    else if(!garray_getfloatwords(a, &x->x_arrayPoints, &x->x_vec))
    	pd_error(x, "%s: bad template for specCentroid", x->x_arrayname->s_name);
	else
	{

	startSamp = start;
	startSamp = (startSamp<0)?0:startSamp;

	if(n)
		endSamp = startSamp + n-1;
	else
		endSamp = startSamp + x->window-1;

	if(endSamp > x->x_arrayPoints)
		endSamp = x->x_arrayPoints-1;

	lengthSamp = endSamp-startSamp+1;

	if(endSamp <= startSamp)
	{
		error("bad range of samples.");
		return;
	}

	if(lengthSamp > x->powersOfTwo[x->powTwoArrSize-1])
	{
		post("WARNING: specCentroid: window truncated because requested size is larger than the current max_window setting. Use the max_window method to allow larger windows.");
		lengthSamp = x->powersOfTwo[x->powTwoArrSize-1];
		window = lengthSamp;
		endSamp = startSamp + window - 1;
	}
	else
	{
		i=0;
		while(lengthSamp > x->powersOfTwo[i])
			i++;

		window = x->powersOfTwo[i];
	}

	windowHalf = window * 0.5;

	if(x->window != window)
	{
		oldWindow = x->window;
		x->window = window;

		x->signal_R = (t_sample *)t_resizebytes(x->signal_R, oldWindow*sizeof(t_sample), x->window*sizeof(t_sample));
	}

	if(x->windowFuncSize != lengthSamp)
	{
		x->blackman = (t_float *)t_resizebytes(x->blackman, x->windowFuncSize*sizeof(t_float), lengthSamp*sizeof(t_float));
		x->cosine = (t_float *)t_resizebytes(x->cosine, x->windowFuncSize*sizeof(t_float), lengthSamp*sizeof(t_float));
		x->hamming = (t_float *)t_resizebytes(x->hamming, x->windowFuncSize*sizeof(t_float), lengthSamp*sizeof(t_float));
		x->hann = (t_float *)t_resizebytes(x->hann, x->windowFuncSize*sizeof(t_float), lengthSamp*sizeof(t_float));

		x->windowFuncSize = lengthSamp;

		tIDLib_blackmanWindow(x->blackman, x->windowFuncSize);
		tIDLib_cosineWindow(x->cosine, x->windowFuncSize);
		tIDLib_hammingWindow(x->hamming, x->windowFuncSize);
		tIDLib_hannWindow(x->hann, x->windowFuncSize);
	}

	// create local memory
	signal_I = (t_sample *)t_getbytes((windowHalf+1)*sizeof(t_sample));

	// construct analysis window
	for(i=0, j=startSamp; j<=endSamp; i++, j++)
		x->signal_R[i] = x->x_vec[j].w_float;

	// set window function
	windowFuncPtr = x->hann; //default case to get rid of compile warning

	switch(x->windowFunction)
	{
		case 0:
			break;
		case 1:
			windowFuncPtr = x->blackman;
			break;
		case 2:
			windowFuncPtr = x->cosine;
			break;
		case 3:
			windowFuncPtr = x->hamming;
			break;
		case 4:
			windowFuncPtr = x->hann;
			break;
		default:
			break;
	};

	// if windowFunction == 0, skip the windowing (rectangular)
	if(x->windowFunction>0)
		for(i=0; i<lengthSamp; i++, windowFuncPtr++)
			x->signal_R[i] *= *windowFuncPtr;

	// then zero pad the end
	for(; i<window; i++)
		x->signal_R[i] = 0.0;

	mayer_realfft(window, x->signal_R);
	tIDLib_realfftUnpack(window, windowHalf, x->signal_R, signal_I);
	tIDLib_power(windowHalf+1, x->signal_R, signal_I);

	// power spectrum sometimes generates lower scores than magnitude. make it optional.
	if(!x->powerSpectrum)
		tIDLib_mag(windowHalf+1, x->signal_R);

	dividend=0;
	divisor=0;
	centroid=0;
	
	for(i=0; i<=windowHalf; i++)
		dividend += x->signal_R[i]*(i*(x->sr/window));  // weight by bin freq

	for(i=0; i<=windowHalf; i++)
		divisor += x->signal_R[i];
		
	if(divisor==0) divisor = 1; // don't divide by zero
	
	centroid = dividend/divisor;
		
	outlet_float(x->x_centroid, centroid);

	// free local memory
	t_freebytes(signal_I, (windowHalf+1)*sizeof(t_sample));
	}
}
Beispiel #30
0
void binbuf_clear(t_binbuf *x)
{
    x->b_vec = t_resizebytes(x->b_vec, x->b_n * sizeof(*x->b_vec), 0);
    x->b_n = 0;
}