2
#include <stdio.h>
#include <string.h>

int main() {

    FILE *fin,*fout;
    char buffer[1028];


    int readcount=0;
    short NumChannels = 1;
    short BitsPerSample = 16;
    int SamplingRate =8000;
    short numOfSamples = 160;

    int ByteRate = NumChannels*BitsPerSample*SamplingRate/8;
    short BlockAlign = NumChannels*BitsPerSample/8;
    int DataSize = NumChannels*numOfSamples *  BitsPerSample/8;
    int chunkSize = 16;
    int totalSize = 36 + DataSize;
    short audioFormat = 1;

    if((fout = fopen("sample.wav", "w")) == NULL)
    {
        printf("Error opening out file ");
    }

    //totOutSample = 0;
    fwrite("RIFF", sizeof(char), 4,fout);
    fwrite(&totalSize, sizeof(int), 1, fout);
    fwrite("WAVE", sizeof(char), 4, fout);
    fwrite("fmt ", sizeof(char), 4, fout);
    fwrite(&chunkSize, sizeof(int),1,fout);
    fwrite(&audioFormat, sizeof(short), 1, fout);
    fwrite(&NumChannels, sizeof(short),1,fout);
    fwrite(&SamplingRate, sizeof(int), 1, fout);
    fwrite(&ByteRate, sizeof(int), 1, fout);
    fwrite(&BlockAlign, sizeof(short), 1, fout);
    fwrite(&BitsPerSample, sizeof(short), 1, fout);
    fwrite("data", sizeof(char), 4, fout);
    fwrite(&DataSize, sizeof(int), 1, fout);
    fclose(fout);
    fin=fopen("sample.raw","r");
    fout=fopen("sample.wav","a");

    while(!feof(fin))
    {
        fgets(buffer,sizeof(buffer),fin);
        fputs(buffer,fout);
    }

    fclose(fin);
    fclose(fout);
}

Can anyone help me to solve the problem in this code. Header is proper and its even properly written on file. But whenever opening in frhed the .wav file, its showing one extra field. And also file is not playing. If I try opening the file using media info its showing properties as expected except duration

Format : Wave File size
: 8.29 KiB Duration : 20ms Overall bit rate mode : Constant Overall bit rate
: 3 394 Kbps

Audio Format : PCM Format settings, Endianness : Little Format settings, Sign
: Signed Codec ID : 1 Duration
: 20ms Bit rate mode : Constant Bit rate
: 128 Kbps Channel(s) : 1 channel Sampling rate : 8 000 Hz Bit depth
: 16 bits Stream size : 320 Bytes (4%)

Evan Teran
  • 87,561
  • 32
  • 179
  • 238
Siri
  • 29
  • 1
  • 5
  • Can anyone help me out by explaining what input should I give for the numofSamples which is used to calculate Subchunk2Size (Subchunk2Size == NumSamples * NumChannels * BitsPerSample/8)? Here Subchunk2Size is DataSize. – Siri Dec 02 '13 at 03:03
  • Got it. Numofsamples is equivalent to your pcm file size as those are the sample values. So find the size of the pcm file and that's your NumSamples. I used the following code to find the size of pcm file fin=fopen("sample.raw","r"); if (stat ("sample.raw", & sb) != 0) { fprintf (stderr, "'stat' failed for '%s': %s.\n", "sample.raw", strerror (errno)); exit (EXIT_FAILURE); } numOfSamples=sb.st_size; – Siri Dec 02 '13 at 06:00

1 Answers1

1

I know nothing about the file formats, but I find it hard to believe they would have a binary header followed by ascii data. So the use of fgets and fputs I find questionable since fgets looks for a \n terminator. So you could be losing data if the buffer fills up before it happens to find a \n ( 0xa ) byte.

while(!feof(fin))
{
    fgets(buffer,sizeof(buffer),fin);
    fputs(buffer,fout);
}

Something like this would make more sense

size_t n;
while((n = fread(buffer, 1, sizeof(buffer), fin)) > 0) {
   if(n != fwrite(buffer, 1, n, fout)) {
      perror("fwrite");
      exit(1);
   }
}
if(n < 0) {
   perror("fread");
   exit(1);
}

Also, why do you close fout and then reopen it?

Charlie Burns
  • 6,994
  • 20
  • 29
  • Yea it worked... Thanks Mr. Charlie Burns... Some more modification in Header along with ur suggestion turned my favour... Also I removed reopening the file... :) THANK YOU SO MUCHHH.... :) – Siri Nov 13 '13 at 08:43
  • @Siri may you please link to your final solution. I also had a need for this please. – Noitidart Oct 15 '16 at 01:55