Ejemplo n.º 1
0
void CALLBACK timer_display(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
	static float   wbuffer[1024];
	static float   ibuffer[1024];
	static float   rbuffer[1024];
	int            i, j, k, bar_width;
	static int     v[1024];
	static int     fc[1024]; /* falling coefficient */
	static int     fp[1024];
	static int     fpfc[1024];
	int            id, tv, nid;
	unsigned long  freq, mb;
	static int     firsttime = 1;
	double         tav;
	int            rbars_count;
	COLORREF       colorvalue;

	if(sfennec.audio.output.getfloatbuffer(wbuffer, 1024, 1) != 1) return;

	col_bar_line = color_adjust(visdata.getdata(get_color, color_dark, 0, 0), 1.3);
	col_bar_fill = (COLORREF)visdata.getdata(get_color, color_dark, 0, 0);

	sfennec.audio.output.getdata(audio_v_data_frequency, &freq);
	
	fft_float(1024, 0, wbuffer, 0, ibuffer, rbuffer);

	vwx = visdata.getdata(get_visual_x, 0, 0, 0);
	vwy = visdata.getdata(get_visual_y, 0, 0, 0);
	vww = visdata.getdata(get_visual_w, 0, 0, 0);
	vwh = visdata.getdata(get_visual_h, 0, 0, 0);

	if(bars_count <= 0)
		bars_count   = 8;

	bar_width    = bars_count;
	rbars_count  = vww / bar_width;

	if(firsttime)
	{
		memset(v, 0, sizeof(v));
		memset(fp, 0, sizeof(fp));
		firsttime = 0;
	}

	if(freq > (20000 * 2))
		mb = (512 * (20000 * 2)) / freq;
	else
		mb = 512;
	
	for(i=0; i<rbars_count; i++)
	{
		id  = (mb * i) / rbars_count;
		nid = (mb * (i + 1)) / rbars_count;

		k = (nid - id);
		
		for(j=0, tav=0.0f; j<=1/*k*/; j++)
		{
			nid = id + j;
			tav += fabs( sqrt(ibuffer[nid] * ibuffer[nid] + rbuffer[nid] * rbuffer[nid]) ) * ((pow(1.007, nid) - 0.4) / 80.0 * (double)vwh);
		}

		/*tav /= k;*/

		tv = (int)tav;

		
		if(tv > vwh)tv = vwh;


		if(tv > v[id])
		{
			v[id]    = tv;
			fc[id]   = 2;
		}else{
			v[id]  -= fc[id];
			fc[id] += fallco * (double)vwh / 100.0;
		}

		if(tv > fp[id])
		{
			fpfc[id] = -(tv - fp[id]) / 5;
			fp[id]   = tv;
		}else{
			if(fp[id] > 0)
			{
				fpfc[id] += 2;
				if(!peak_mode_hold || fpfc[id] > 0)
					fp[id]   -= fpfc[id];
			}
		}

		fp[id] = max(fp[id], v[id]);
		fp[id] = max(fp[id], 1);
		v[id]  = max(v[id], 0);
		
		if(fp[id] > vwh)fp[id] = vwh;

		colorvalue = RGB(min(((fp[id] *  255) / vwh) + 20, 255), 192, 125);
		
		draw_rect((i * bar_width) + vwx, vwy, max(bar_width, 1), max(vwh - v[id], 0), RGB(0, 0, 0));
		
		if(showbars)
			draw_rect((i * bar_width) + vwx, vwy + vwh, max(bar_width, 1), -min(v[id], vwh), colorvalue);
		else
			draw_rect((i * bar_width) + vwx, vwy + vwh, max(bar_width, 1), -min(v[id], vwh), 0);
		
		//

		if(showfall)
			draw_rect((i * bar_width) + vwx, vwy + max(vwh - fp[id], 0), max(bar_width , 1), 1, colorvalue);


		SendMessage(window_vis, WM_USER /* overlay */, 0, (LPARAM)&dc);

	}
}
Ejemplo n.º 2
0
int main_fft(int procNumber) {

	unsigned i,j;
/*	float *RealIn;
	float *ImagIn;
	float *RealOut;
	float *ImagOut;
	float *coeff;
	float *amp;
*/

float RealIn[MAXSIZE];
float ImagIn[MAXSIZE];
float RealOut[MAXSIZE];
float ImagOut[MAXSIZE];
float coeff[MAXSIZE];
float amp[MAXSIZE];

int invfft=0;
 

 /*RealIn=(float*)malloc(sizeof(float)*MAXSIZE);
 ImagIn=(float*)malloc(sizeof(float)*MAXSIZE);
 RealOut=(float*)malloc(sizeof(float)*MAXSIZE);
 ImagOut=(float*)malloc(sizeof(float)*MAXSIZE);
 coeff=(float*)malloc(sizeof(float)*MAXWAVES);
 amp=(float*)malloc(sizeof(float)*MAXWAVES);*/

 /* Makes MAXWAVES waves of random amplitude and period */

	srand(1);
	for(i=0;i<MAXWAVES;i++) 
	{
		coeff[i] = rand()%1000;
		amp[i] = rand()%1000;
	}
 

for(i=0;i<MAXSIZE;i++) 
 {
   /*   RealIn[i]=rand();*/
	 RealIn[i]=0;
	 for(j=0;j<MAXWAVES;j++) 
	 {
		 /* randomly select sin or cos */
		 if (rand()%2)
		 {
		 		RealIn[i]+=coeff[j]*cos(amp[j]*i);
			}
		 else
		 {
		 	RealIn[i]+=coeff[j]*sin(amp[j]*i);
		 }
  	 ImagIn[i]=0;
	 }
 }

 /* regular*/
 fft_float (MAXSIZE,invfft,RealIn,ImagIn,RealOut,ImagOut);
 
 pthread_mutex_lock(&mutex_print);
 fprintf(fileout_fft,"RealOut:\n");
 //printf("RealOut:\n");
 for (i=0;i<MAXSIZE;i++)
 {
   fprintf(fileout_fft,"%f ", RealOut[i]);
   //printf("%f ", RealOut[i]);
 }
 fprintf(fileout_fft,"\n");
 //printf("\n");

 fprintf(fileout_fft,"ImagOut:\n");
 //printf("ImagOut:\n");
 for (i=0;i<MAXSIZE;i++)
 {
      fprintf(fileout_fft,"%f ", ImagOut[i]);
      //printf("%f ", ImagOut[i]);
 }
 fprintf(fileout_fft,"\n");
 //printf("\n");

 pthread_mutex_unlock(&mutex_print);

 /*free(RealIn);
 free(ImagIn);
 free(RealOut);
 free(ImagOut);
 free(coeff);
 free(amp);*/

 return 0;

}
Ejemplo n.º 3
0
int main(int argc, char *argv[]) {
	unsigned MAXSIZE;
	unsigned MAXWAVES;
	unsigned i,j;
	float *RealIn;
	float *ImagIn;
	float *RealOut;
	float *ImagOut;
	float *coeff;
	float *amp;
	int invfft=0;

	if (argc<3)
	{
		printf("Usage: fft <waves> <length> -i\n");
		printf("-i performs an inverse fft\n");
		printf("make <waves> random sinusoids");
		printf("<length> is the number of samples\n");
		exit(-1);
	}
	else if (argc==4)
		invfft = !strncmp(argv[3],"-i",2);
	MAXSIZE=atoi(argv[2]);
	MAXWAVES=atoi(argv[1]);
		
 srand(1);

 RealIn=(float*)malloc(sizeof(float)*MAXSIZE);
 ImagIn=(float*)malloc(sizeof(float)*MAXSIZE);
 RealOut=(float*)malloc(sizeof(float)*MAXSIZE);
 ImagOut=(float*)malloc(sizeof(float)*MAXSIZE);
 coeff=(float*)malloc(sizeof(float)*MAXWAVES);
 amp=(float*)malloc(sizeof(float)*MAXWAVES);

 /* Makes MAXWAVES waves of random amplitude and period */
	for(i=0;i<MAXWAVES;i++) 
	{
		coeff[i] = rand()%1000;
		amp[i] = rand()%1000;
	}
 for(i=0;i<MAXSIZE;i++) 
 {
   /*   RealIn[i]=rand();*/
	 RealIn[i]=0;
	 for(j=0;j<MAXWAVES;j++) 
	 {
		 /* randomly select sin or cos */
		 if (rand()%2)
		 {
		 		RealIn[i]+=coeff[j]*cos(amp[j]*i);
			}
		 else
		 {
		 	RealIn[i]+=coeff[j]*sin(amp[j]*i);
		 }
  	 ImagIn[i]=0;
	 }
 }

 /* regular*/
 fft_float (MAXSIZE,invfft,RealIn,ImagIn,RealOut,ImagOut);
 
 printf("RealOut:\n");
 for (i=0;i<MAXSIZE;i++)
   printf("%f \t", RealOut[i]);
 printf("\n");

printf("ImagOut:\n");
 for (i=0;i<MAXSIZE;i++)
   printf("%f \t", ImagOut[i]);
   printf("\n");

 free(RealIn);
 free(ImagIn);
 free(RealOut);
 free(ImagOut);
 free(coeff);
 free(amp);
 exit(0);


}
Ejemplo n.º 4
0
//recognize the fundamental frequency and amplitude of the voice and transfer them to midi note and velocity
//return false if the voice is recognized as silence, return true otherwise
int voice2midi(int sampleRate,int numSamples, float *data, float *freq, int *velocity)
{
	
	if(!isPowerOfTwo(numSamples))numSamples=reduceToPowerOfTwo(numSamples);

	move2zero(numSamples,data);

	float aa=absoluteAverage(numSamples,data);
	
	if(aa<SOUND_THRESHOLD)
	{
		*velocity = 0;
		return false;
	}
	
	float *realOut=new float[numSamples];
	float *imagOut=new float[numSamples];

	float *realOut2=new float[numSamples];
	float *imagOut2=new float[numSamples];


	for(int i=0;i<numSamples;i++)
		data[i] *= (1 + cos(2*PI*i/numSamples - PI)/2);

	fft_float(numSamples,false,data,0,realOut,imagOut);
	for(int i=0;i<numSamples;i++)
		realOut[i]=realOut[i]*realOut[i]+imagOut[i]*imagOut[i];
	fft_float(numSamples,true,realOut,0,realOut2,imagOut2);

	float maxValue=-999999;
	int maxPosition=-1;
	for(int i=1;i<numSamples/2;i++)
	{
		if(isLocalMax(numSamples,realOut2,i))
			if(maxValue<realOut2[i])
			{
				maxValue=realOut2[i];
				maxPosition=i;
				//break;
			}			

	}

	float timePerFrame=(float)numSamples/sampleRate;
	float T=timePerFrame*maxPosition/numSamples;
	float f=1/T;

	//printf("%d,%f,%f\n",maxPosition,T,f);
	//cout<<maxPosition<<endl;

	*freq=f;
	*velocity=amplitude2midi(aa);

	delete []realOut;
	delete []imagOut;
	delete []realOut2;
	delete []imagOut2;

	return true;

}