471,319 Members | 1,594 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

how to read data from a wave file

hello friends
can anybody tell me how to read data from a wave file..
i have writen code as ...
class file
FILE *fp;
char id[4];
char *sound_buffer; //four bytes to hold 'RIFF'
long int size; //32 bit value to hold file size
short int format_tag, channels, block_align, bits_per_sample; //our 16 values
long int format_length, sample_rate, avg_bytes_sec, data_size, i; //our 32 bit values
int a;

file(const char *fname,const char *mode)
fp = fopen(fname,mode);
if (fp==NULL)
printf("cant open");
fread(id, sizeof(id), 1, fp); //read in first four bytes
cout<<"\n"<<"id is "<<id;
//if (!strcmp(id, "RIFF"))
{ //we had 'RIFF' let's continue
fread(size, 4 , 1, fp); //read in 32bit size value
cout<<"\n size is "<< size;
fread(id, sizeof(id), 1, fp); //read in 4 byte string now
cout<<"\n wave id is "<<id;
if (!strcmp(id,"WAVE"))
{ //this is probably a wave file since it contained "WAVE"
fread(id, sizeof(id), 1, fp); //read in 4 bytes "fmt ";
cout<<"\n Format id is "<<id;
fread(&format_length, 4 ,1,fp);
fread(&format_tag, 2 , 1, fp); //check mmreg.h (i think?) for other
// possible format tags like ADPCM
fread(&channels, 2 ,1,fp); //1 mono, 2 stereo
fread(&sample_rate, 4 , 1, fp); //like 44100, 22050, etc...
fread(&avg_bytes_sec, 4 , 1, fp); //probably won't need this
fread(&block_align, 2 , 1, fp); //probably won't need this
fread(&bits_per_sample, 2 , 1, fp); //8 bit or 16 bit file?
fread(id, 4 , 1, fp); //read in 'data'
cout<<"\n id for data is "<<id;

upto here it is giving otput...
but after output it gives error as
loating point error:domain
abnormal program termination
null pointer assignment.

fread(data_size, 4 , 1, fp); //how many bytes of sound data we have
if((sound_buffer = (char *) malloc (sizeof(char) * data_size))==NULL); //set aside sound buffer space
cout<<"not enough memory";
fread(sound_buffer, sizeof(sound_buffer), data_size, fp); //read in our whole sound data chunk
printf(" \nError: RIFF file but not a wave file\n");
printf("Error: not a RIFF file\n");
void main()
char filename[50],ch;
printf("enter file name ");
file f(filename,"rb");

can anybody tell whats the problem?
Jun 10 '07 #1
0 1826

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by webstar | last post: by
reply views Thread by Ada | last post: by
2 posts views Thread by Sagaert Johan | last post: by
3 posts views Thread by VenuGopal | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.