C Program reads in and spews out the attributes of a given .wav file.

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
.wav File
    Spews
      Attribute

        Learn More Multiple Choice Question :