Saturday, December 5, 2009

WAVE RECORDER

Simple wave program in C language.It is used to provide the process of the wave sound.


void main(void)
{
int gd=DETECT,gm;
struct format
{
long chunk_size;
short format_tag;
unsigned short channels;
unsigned long sample_persec;
unsigned long bytes_persec;
unsigned short block_align;
unsigned short bits_per_samp;
};
FILE*fp1;
char c,test[4];
char fname[12];
long int data,lend;
struct format fmt;
unsigned char ch_data;
char c_data,sample[4000];
float peak,acr[400];
int i,k,f,t,d,v;
float scale;
int pitch;
char rising;
clrscr();
printf("enter the wave file name...\n");
scanf("%s",fname);
fp1=fopen(fname,"r");
if(fp1==NULL)
{
printf("errorin opening wave file\n");
exit(0);
}
read: fgets(test,5,fp1);
if(strncmp("fmt",test,4)!=0)
goto read;
if(strncmp("fmt",test,4)!=0)
printf("\n chunk id=%s",test);
fread(&fmt,sizeof(fmt),1,fp1);
printf("wave file details....\n");
printf("***********************\n");
printf("chunk size :%d",fmt.chunk_size);
printf("\n format tag :%d",fmt.format_tag);
printf("\n numer of channels =%d",fmt.channels);
printf("\n samples per second=%ld",fmt.sample_persec);
printf("\n bytes per second :%ld",fmt.bytes_persec);
printf("\n block align :%d",fmt.block_align);
printf("\n bits per sample :%d",fmt.bits_per_samp);
printf("\n");
if(fmt.chunk_size!=16)
fread(&d,2,1,fp1);
printf("************************\n");
readdat:
fread(test,4,1,fp1);
fread(&lend,4,1,fp1);
if(strncmp(test,"data",4)!=0)
{
if(lend>0)
while(lend!=0)
{
fread(&c,1,1,fp1);
lend--;
}
goto readdat;
}
if((fmt.bits_per_samp==8)&&(fmt.channels==1))
{
v=0;
f=0;
while(1)
{
f++;
c_data=fgetc(fp1);
ch_data=c_data;
if((ch_data<122)||(ch_data>128))
v++;
else
v=0;
if(v==10)
{
fseek(fp1,-10,SEEK_CUR);
break;
}
}
t=0;
while(1)
{
c_data=fgetc(fp1);
ch_data=c_data;
if(t<4000)
sample[t]=ch_data-127;
else
break;
if(c_data==EOF)
break;
t=t+1;
}
}
for(k=0;k<400;k++)
{
acr[k]=0;
for(i=0;i<400;i++)
acr[k]=acr[k]+sample[i]*sample[i+k];
}
i=1;
k=1;
peak=acr[0]*0.95;
rising=0;
while(i<400)
{
if(acr[i]>acr[i-1])
rising=1;
else
rising=0;
if((acr[i]>=peak)&&(rising==1))
{
peak=acr[i];
k=i;
break;
}
i=i+1;
}
if(k==1)
{
i=1;
peak=0.7*acr[0];
rising=0;
while(i<400)
{
if(acr[i]>acr[i-1])
rising=1;
else
rising=0;
if((acr[i]>=peak)&&(rising==1))
{
peak=acr[i];
k=i;
break;
}
i=i+1;
}
}
if(k<10)
printf("pitch doubtful....\n");
pitch=8000/k;
printf("\npitch frequency=%d hertz\n",pitch);
printf("\npress any key..\n");
getch();
initgraph(&gd,&gm,"C:TURBOC2\EGAVGABGT");
clearviewport();
setcolor(YELLOW);
outtextxy(250,30,"signal");
line(100,150,500,150);
t=0;
moveto(100,150);
while(t<400)
{
lineto(100+t,(150-sample[t]));
t=t+1;
delay(2000);
}
outtextxy(230,250,"autocorrelation");
line(100,350,500,350);
moveto(100,350);
i=0;
scale=64/acr[0];
while(i<400)
{
lineto(100+i,(350-(int)(acr[i]*scale)));
i=i+1;
delay(2000);
}
setcolor(WHITE);
getch();
closegraph();
fclose(fp1);
}

NOTE:

  • Wave program using header files are math.h,stdlib.h,graphics.h.
  • File concept is available in this program.

0 comments:

Post a Comment

  © Blogger template Coozie by Ourblogtemplates.com 2008

Back to TOP