Pages

Monday, June 20, 2011

C program to Reading and writing a wave file

The WAVE file format is a subset of Microsoft's RIFF specification for the storage of multimedia files. A RIFF file starts out with a file header followed by a sequence of data chunks. A WAVE file is often just a RIFF file with a single "WAVE" chunk which consists of two sub-chunks -- a "fmt " chunk specifying the data format and a "data" chunk containing the actual sample data. Call this form the "Canonical form". Who knows how it really all works.



Download source code
#include<stdio.h>

#include<stdlib.h>

struct file_header

{

unsigned char RIFF[4];

unsigned long int ChunkSize;

char WAVE[4];

char fmt[4];

unsigned long int Subchunk1Size;

unsigned short int AudioFormat;

unsigned short int NumChannels;

unsigned long int SampleRate;

unsigned long int ByteRate;

unsigned short int BlockAlign;

unsigned short int BitsPerSample;

char data[4];

unsigned long int Subchuk2Size;

};





struct HeaderType {

  long         RIFF;      //RIFF header

  char         NI1 [18];  //not important



  unsigned int Channels;  //channels 1 = mono; 2 = stereo

  long         Frequency; //sample frequency

  char         NI2 [6];   //not important

  char         BitRes;    //bit resolution 8/16 bit

  char         NI3 [12];  //not important

} Header;







struct sample

{

unsigned short int left_channel;

unsigned short int right_channel;



};



int main()

{

FILE *fp;



struct file_header source_header;

struct sample source_sample;



fp=fopen("hello.wav","rb");



fread(&source_header,sizeof(struct file_header),1,fp);



/*

printf("%d \n",sizeof(source_header.RIFF));

printf("%d \n",sizeof(source_header.ChunkSize));

printf("%d \n",sizeof(source_header.WAVE));

printf("%d \n",sizeof(source_header.fmt));

printf("%d \n",sizeof(source_header.Subchunk1Size));

printf("%d \n",sizeof(source_header.AudioFormat));

printf("%d \n",sizeof(source_header.NumChannels));

printf("%d \n",sizeof(source_header.SampleRate));

printf("%d \n",sizeof(source_header.ByteRate));

printf("%d \n",sizeof(source_header.ByteRate));

printf("%d \n",sizeof(source_header.BlockAlign));

printf("%d \n",sizeof(source_header.BitsPerSample));

printf("%d \n",sizeof(source_header.data));

printf("%d \n",sizeof(source_header.Subchuk2Size));

*/









printf("Audio format: %u \n",source_header.AudioFormat);

printf("Number fo channles: %u \n",source_header.NumChannels);

printf("Sample rate: %u \n",source_header.SampleRate);

printf("Byte rate: %u \n",source_header.ByteRate);

printf("Bits per simple: %u \n",source_header.BitsPerSample);









return 0;

}

No comments:

Post a Comment

Please Do not for get to comment