Append .pcm audio raw data into wav (in C)

2019-07-31 08:52发布

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.

标签: c audio wav pcm
0条回答
登录 后发表回答