How to write a C Program reads in and spews out the attributes of a given .wav file in C Programming Language ?
Solution:
/* C program reads in and spews out the attributes of a given .wav file. */ #include <stdio.h> #include <string.h> unsigned int get4BytesLSB(FILE *file); unsigned short get2BytesLSB(FILE *file); void read4Chars(FILE *file, char yourString[5]); int main(int argc, const char * argv[]) { char fileName[100]; char* extension; unsigned int chunkSize=0, subChunk1Size = 0, sampleRate = 0, byteRate = 0, blockAlign = 0, subChunk2Size = 0; unsigned short audioFormat = 0, numChannels = 0, bitsPerSample = 0; char chunkID[4+1], format[4+1], subChunk1ID[4+1], subChunk2ID[4+1]; FILE *wavFile; while(1)//incase they enter invalid file name { printf("enter a .wav filepath\n"); scanf("%s",fileName); extension = strstr(fileName,"."); if (extension)//incase its null if (!strcasecmp(extension,".wav")) break; printf("that isnt a .wav file.\n"); } wavFile = fopen(fileName,"rb"); //now read in all of the data read4Chars(wavFile, chunkID); chunkSize = get4BytesLSB(wavFile); read4Chars(wavFile, format); read4Chars(wavFile, subChunk1ID); subChunk1Size = get4BytesLSB(wavFile); audioFormat = get2BytesLSB(wavFile); numChannels = get2BytesLSB(wavFile); sampleRate = get4BytesLSB(wavFile); byteRate = get4BytesLSB(wavFile); blockAlign = get2BytesLSB(wavFile); bitsPerSample = get2BytesLSB(wavFile); read4Chars(wavFile, subChunk2ID); subChunk2Size = get4BytesLSB(wavFile); //now print out all of the data printf("----------------------\n"); printf("chunkID = %s\n",chunkID); printf("chunkSize = %u\n",chunkSize); printf("format = %s\n", format); printf("subChunk1ID = %s\n", subChunk1ID); printf("audioFormat = %hu\n",audioFormat); printf("numChannels = %hu\n",numChannels); printf("sampleRate = %u\n",sampleRate); printf("byteRate = %u\n",byteRate); printf("blockAlign = %u\n",blockAlign); printf("bitsPerSample = %hu\n",bitsPerSample); printf("subChunk2ID = %s\n",subChunk2ID); printf("subChunk2Size = %u\n",subChunk2Size); printf("----------------------\n"); } //reads 4 chars into a 4-length string, big endian void read4Chars(FILE *file, char yourString[5]) { yourString[0] = fgetc(file); yourString[1] = fgetc(file); yourString[2] = fgetc(file); yourString[3] = fgetc(file); yourString[4] = '\0'; } //reads 4 bytes into a uint, little endian unsigned int get4BytesLSB(FILE *file) { return fgetc(file)*1 + fgetc(file)*256 + fgetc(file)*256*256 + fgetc(file)*256*256*256; } //reads 2 bytes into a ushort, little endian unsigned short get2BytesLSB(FILE *file) { return fgetc(file)*1 + fgetc(file)*256; }
Learn More :
Read
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C or C++ program to read marks of 4 subjects and find how many students are pass and fail with division
- C Program readers/writers
- C Program to Demonstrates use of reader/writer lock
- C Program to Write/Read Dynamic Data Example
- C Function to read instructions from the file and obey them
- A small I/O bound program to copy N bytes from an input file to an output file.
- Input reads in the array positions and range for random generation.
- C Program to Read and Write FIFO
- pthreads Readers/Writers Lock C Program
- C Program to opens and reads dictionary file specified in spellrc
- C Program Number of Judge and Score From Each Judge
- Read 10 real numbers using a vector and show the least of them C Program
- C Program To Read A Parenthesised Infix Expression From The User And Check Whether It Is Well Parenthesised Or Not
- C Program To Read The Adjecancy Matrix of Directed Graph And Convert It Into Adjecancy List
- C Program Read a char & print next char ( file to file )
- Menu Driven Program to Read Two Integers Find Sum, Difference and Product C Program
.wav File
Spews
Attribute
given
- C Program To Find Week Day Of A Given Date
- Give me an integer and I will sum it with the previous natural numbers
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- C Program to Check Given String is Palindrome or Not
- C Program That in a Given Natural Number x Insert Figure c at Position p
- Return the number of tokens given a command
- C Program Recursive function that searches for a given file in a given folder
- C Program to accept m*n matrix from user and display the elements of given matrix using function
- C program to display the transpose of given 3 X 3 matrix
- C Program to convert given decimal number into binary number