473,382 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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
{
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
0 1981

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

Similar topics

1
by: webstar | last post by:
hi, could you please tell me how to play a small wave file if a button is pressed. or if there is another format that is easer then use that instead of wave. thanks matt
0
by: Ada | last post by:
i'm using C# and have been reading DX doc but cannot find the info i'm looking for. i want to load WAVE file and display the waveform. something similar to this: ...
2
by: Sagaert Johan | last post by:
Hi I can play a wave file through a p/invoke to winmm , but how can i play a wave file to a specific audio device if i have more then one audio device in my pc ? Johan
3
by: VenuGopal | last post by:
hi, i want to store the WAVE FILE CONTENTS in an XML. How do i do it? thanks Venugopal
1
by: Tito | last post by:
For an internet telephone application, I need to be able to read and write data to and from /dev/dsp simultaneously. I wrote some code and its not working. Anyone have any working code to do...
1
by: Hitchkas | last post by:
I want to access a wave file's sound data buffer, manipulate the sound and attach the processed buffer back into the wave file using C#. Are there any C# sample codes showing how this can be done?...
9
by: Morris Neuman | last post by:
Im working with VS 2005 and trying to use a Hyperlink field in a datagrid to play a wave file that is not located in the website folders but is in a plain folder on the same machine, windows 2003...
0
by: Karthik | last post by:
Hi, I want to record a sound wave from a mic and at the same time invert it and play the inverted wave.My code goes as follows, however nothing is written into the E:\inverted.wav file.Thanks in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.