2
votes

After searching on google for a long, I was unable to find some answers. So,I thought to put question on stackoverflow. I can't share code here due to company policies but I can share links from where I get knowledge. So, what I am trying to do from couple of days - Converting .pcm file to .wav file format. I am sharing some links about wav header and how I design the struct wav and then write that header in .wav file after this I want to append .pcm raw data after writing the wave header but What I am thinking problem is in appending the pcm data. click on the links which I followed for writing the code in c - link1 , wave-header info , link2 , link3 .

pre-requsite to convert pcm to wav. Take as a input

  • sample_rate = 44100
  • channels = 1
  • bits_per_sample (bit_depth or data_width) = 16

And I don't know it require endianness (byte_order). So, these things we require before. Okay, now tell me these things one by one.

  1. chunk_size
  2. no_of_samples
  3. fmt_length
  4. byte_rate
  5. block_align
  6. Subchunk2_size or data_size

Now, I'll share what I know. Please, correct me if I wrong.

  • chunk_size = 36 + data_size or subchuck2_size note: data_size i calculate through ftell(ptr) and ptr points to pcm file or another method is data_size = (channels * no_of_samples * bits_per_sample) / 8.
  • no_of_samples = (data_size * 8) / (channels * bits_per_sample);
  • fmt_length = 16
  • byte_rate = (sample_rate * bits_per_sample * channels) / 8
  • block_align = (channels * bits_per_sample) / 8
  • data_size = (channels * no_of_samples * bits_per_sample) / 8
  • float time = (float) file_size/ (float) ((sample_rate * channels * bits_per_sample) / 8);

confusion is in chunk_size and data_size or(subchunk2_size).

how I am writing pcm data after writing wav header into file

for(n = 0; n < chunk_size; n++)
{
    a = fgetc(ptr2);
    fwrite(&a,sizeof(char),1,ptr);
}

note: ptr2 is fopen return of pcm file.

So, What I think, i am appending pcm data into wav file in a wrong way.

please, tell me the solution. I am thanking you in advance. Guidance is fully appreciated.

looks like your ducks are in a row ... pull the trigger on writing it ... update with any specific issues ... yes getting endianness correct is essential ... its a rite of passage for everyone who gets into digital signal processing to write their own code to write and read wav files ... its a very self contained way to really understand PCM raw audio ... welcome to SOScott Stensland
problem is in chunk_size, data_size(subchunk2 size) and appending the raw pcm data in wav file. I made the wav header and I want to append pcm data. In wav header - about 3 fields I am not sure - chunk_size, data_size, no_of_samples. and next is to append pcm raw data behind wav header. Tell me this.shashank arora
@ScottStensland I solved the problem. The problem is in the way I was trying to write/append the pcm data in wav file and one more problem is in the header. You knw "Subchunk1ID" field name in wav header. it is of 4 bytes and the data we have to write in this field is 3 bytes -"fmt". so, 1 byte is missing. machine will not able to decode. data is not aligned properly. so write fmt in 4th field of wav like this - fwrite("fmt ", 4, sizeof(char), ptr); NOTE: space must be there. It will align the data. These thing will take care if I made structure of wav header (Structure padding).shashank arora
2nd problem is in writing the pcm data in wav file. while((n = fread(buffer, 1, sizeof(buffer), ptr2)) > 0) { if(n != fwrite(buffer, 1, n, ptr)) { perror("fwrite"); exit(1); } } Note: ptr2 points to .pcm file and ptr points to .wav file. This code is right and replaced with previous.shashank arora
@ScottStensland Can u help me in stereo channel. Above we discussed is working to mono (channel = 1). I want to write pcm for 2 channel (stereo).shashank arora