Pages

Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

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;

}

Wednesday, June 15, 2011

C program to Read and write a bmp (bitmap) file.

Last weak, I experimented with reading that bmp (bitmap) file. the bmp file format is most popular and have simplest structure than other compressed file formats like jpg or gif etc. so any purpose to modify or image processing applications that require to perform the file read or write operation. I simply start with bmp file to learn how to read edit and write the image files.



Download source code hear