title: Creating a .wav file from PCM data
body: <p>I am trying to read the data from SPH0645LM4H-B <a href="https://www.knowles.com/docs/default-source/model-downloads/sph0645lm4h-b-datasheet-rev-c.pdf?sfvrsn=4" rel="nofollow noreferrer">https://www.knowles.com/docs/default-source/model-downloads/sph0645lm4h-b-datasheet-rev-c.pdf?sfvrsn=4</a>
  and convert it to .wav format, so that I can verify whether my sensor is working correctly.
  So far, I have managed to interface the sensor and read data samples via i2s-dma transfer.</p>
  
  <p>Also, I found that <em>most mainstream computer soundfile formats consist of a header (containing the length, etc.) followed by 16-bit <strong>two's-complement PCM</em></strong> <a href="https://www.dsprelated.com/freebooks/mdft/Number_Systems_Digital_Audio.html" rel="nofollow noreferrer">https://www.dsprelated.com/freebooks/mdft/Number_Systems_Digital_Audio.html</a></p>
  
  <p>For converting the PCM data to wave format, I have referred <a href="https://stackoverflow.com/questions/19897879/converting-pcm-to-wav-file">converting PCM to wav file</a></p>
  
  <p>Now, the sensor  data format is <em>I2S, 24-bit, 2’s compliment, MSB first. The data precision is 18 bits; unused bits are</em> zeros.
  I have converted the sensor data to 32-bit (MSB is the sign bit)</p>
  
  <p>I have used the code from the following link (and the links mentioned in it) to create the .wav file:-
  <a href="https://stackoverflow.com/questions/48098254/append-pcm-audio-raw-data-into-wav-in-c?rq=1">Append .pcm audio raw data into wav (in C)</a></p>
  
  <p>//.wav file header data</p>
  
  <pre><code>struct wavfile
  {
  char            id[4];          // should always contain "RIFF"
  int             totallength;    // total file length minus 8
  char            wavefmt[8];     // should be "WAVEfmt "
  int             format;         // 16 for PCM format
  short           pcm;            // WAVE_FORMAT_IEEE_FLOAT (3).
  short           channels;       // channels
  int             frequency;      // sampling frequency, 16000 in this case
  int             bytes_per_second;
  short           bytes_by_capture;
  short           bits_per_sample;
  char            data[4];        // should always contain "data"
  int             bytes_in_data;
  };
  </code></pre>
  
  <p>//Writing the header to output .wav file</p>
  
  <pre><code>void write_wav_header(char* name, int samples, int channels)
  {
  struct wavfile filler;
  FILE *pFile;
  strcpy(filler.id, "RIFF");
  filler.totallength = (samples * channels) + sizeof(struct wavfile) - 8;
  strcpy(filler.wavefmt, "WAVEfmt ");
  filler.format = 16;
  filler.pcm = 3; // WAVE_FORMAT_IEEE_FLOAT (3).
  filler.channels = channels;
  filler.frequency = 32000;
  filler.bits_per_sample = 32;
  filler.bytes_per_second = filler.channels * filler.frequency * filler.bits_per_sample/8;
  filler.bytes_by_capture = filler.channels*filler.bits_per_sample/8;
  filler.bytes_in_data = samples * filler.channels * filler.bits_per_sample/8;
  strcpy(filler.data, "data");
  pFile = fopen(name, "wb");
  fwrite(&amp;filler, 1, sizeof(filler), pFile);
  fclose(pFile);
  }
  </code></pre>
  
  <p>//Appending the audio sensor data to this .wav file</p>
  
  <pre><code>void write_pcm_data_to_file(char* inFile, char* outFile)
  {
  char buffer[SAMPLE_SIZE];
  size_t n;
  FILE *fin,*fout;
  fin = fopen(inFile,"r");
  fout = fopen(outFile,"a");
  while((n = fread(buffer, 1, sizeof(buffer), fin)) &gt; 0)
  {
      if(n != fwrite(buffer, 1, n, fout))
      {
          perror("fwrite");
          exit(1);
      }
  }
  fclose(fin);
  fclose(fout);
  }
  </code></pre>
  
  <p>This is how the resulting .wav file looks in hex editor:-
  <a href="https://i.stack.imgur.com/G0s5N.jpg" rel="nofollow noreferrer">.wav file in hex editor</a>
  I can see that the wav header values are set correctly, followed by my PCM data from Audio Sensor.</p>
  
  <p>However, when I play the .wav file, I am not able to play the input audio. Instead, I hear a constant tone.
  I tried changing the input audio (played different music, songs), yet the resulting .wav file plays the same constant tone.</p>
  
  <p>In order to recreate the input music played to the microphone into a wav file, what modifications should I do?</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong><em>Edit:-</em></strong></p>
  
  <p>According to 42LeapsOfFaith 's answer, to convert my samples to hex, I used the following code:-</p>
  
  <pre><code>hexValue = strtoll(sample, NULL, 16);
  </code></pre>
  
  <p>I converted each value in the buffer, then wrote it into my .wav file. Now my .wav file looks like <a href="https://i.stack.imgur.com/3mBiQ.jpg" rel="nofollow noreferrer">modified .wav file in hex editor</a></p>
  
  <p>However, even this wav file does not play the audio.
  Any further suggestions to recreate the input music played to the microphone into a wav file?</p>
  
  <p><strong><em>Help is very much appreciated</em></strong></p>