By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

how to read data from a wave file

P: 1
hello friends
can anybody tell me how to read data from a wave file..
i have writen code as ...
class file
{
private:
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;

public:
file(const char *fname,const char *mode)
{
fp = fopen(fname,mode);
if (fp==NULL)
printf("cant open");
else
{
fread(id, sizeof(id), 1, fp); //read in first four bytes
cout<<"\n"<<"id is "<<id;
a=strcmp(id,"RIFF");
//if (!strcmp(id, "RIFF"))
if(a==0)
{ //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
for(i=0;i<=data_size;i++)
cout<<sound_buffer[i];
}
else
printf(" \nError: RIFF file but not a wave file\n");
}
else
printf("Error: not a RIFF file\n");
}
fclose(fp);
}
};
void main()
{
char filename[50],ch;
clrscr();
printf("enter file name ");
scanf("%s",filename);
file f(filename,"rb");
getch();
}

can anybody tell whats the problem?
Jun 10 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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