/* This program reads a wave file from the standard input and verifies that it has the correct format, which for us is monaural, 8 bits per sample, and 11025 samples/per second. If the file is incorrect, the program prints an error message and exits. If the file is correct, it prints the sampled data to standard output, using the following format: 0 0.834 1 0.876 . . . The bytes making up the data sample are translated from the range 0..255 to the range -1 to 1. Note an idiosyncrasy in the use of getchar. This function returns an int, so when we read a byte with it, we get a value between 0 and 255. However when we assign it to a char, and then back to an int, we get a value between -128 and 127, so we need to be careful! */ #include #include #include main() { char buf[16]; int intbuf[4],sample; int i,length; float sampleval; for(i=0;i<16;i++) buf[i]=getchar(); /* Check to see if this contain the correct header information for a wave file. */ if((buf[0]!='R')||(buf[1]!='I')||(buf[2]!='F')||(buf[3]!='F')|| (buf[8]!='W')||(buf[9]!='A')||(buf[10]!='V')||(buf[11]!='E')|| (buf[12]!='f')||(buf[13]!='m')||(buf[14]!='t')||(buf[15]!=' ')) { fprintf(stderr,"Bad header for WAV file.\n"); exit(0); } for(i=0;i<16;i++) buf[i]=getchar(); /* Check chunk length */ if(((int)buf[0]!=16)||((int)buf[1]!=0)||((int)buf[2]!=0)||((int)buf[3]!=0)) { fprintf(stderr,"Can't process file with this data format(1).\n"); exit(0); } /* Check for compression and single-channel */ if(((int)buf[4]!=1)||((int)buf[5]!=0)||((int)buf[6]!=1)||((int)buf[7]!=0)) { fprintf(stderr,"Can't process file with this data format.(2)\n"); exit(0); } /* Check sample rate */ if(((int)buf[8]!=17)||((int)buf[9]!=43)||((int)buf[10]!=0)||((int)buf[11]!=0)) { fprintf(stderr,"Can't process file with this sampling rate.\n"); exit(0); } /* Check number of bits per sample */ if(((int)buf[12]!=17)||((int)buf[13]!=43)||((int)buf[14]!=0)||((int)buf[15]!=0)) { fprintf(stderr,"Can't process file with this number of bits per sample.\n"); exit(0); } for(i=0;i<8;i++) buf[i]=getchar(); if((buf[4]!='d')||(buf[5]!='a')||(buf[6]!='t')||(buf[7]!='a')) { fprintf(stderr,"Can't process file with this data format(3).\n"); exit(0); } /* Now everything is all right. The next four bytes contain the number of samples. */ length=0; for(i=0;i<4;i++) { intbuf[i]=getchar(); } for(i=3;i>=0;i--) length=256*length+intbuf[i]; /*just to check printf("Number of samples: %d\n",length); */ for(i=0;i