title: Quantization noise during writing wav file using C
body: <p>I'm just trying to write a library for read and write Wav files (just need it for audio processing), just as test, I read samples from a Wave File convert them to double (just standardize them to -1 ~ 1), and do nothing but transform them back to integer, according to the bit per sample (assume the Wav file have N bits per sample, I divided them through 2^(N-1)-1 and multiply with the same factor after to restore it)</p>
  
  <p>But the problem is, I get a wav file with background noise (id say it seems like quantisization noise) and I don't know why, can you help me find it out?</p>
  
  <p>the library is here: <a href="https://pastebin.com/mz5TWMPN" rel="nofollow noreferrer">https://pastebin.com/mz5TWMPN</a>
  the header file is: <a href="https://pastebin.com/Lr2tbmnv" rel="nofollow noreferrer">https://pastebin.com/Lr2tbmnv</a></p>
  
  <p>and a demo main function is like:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  #include "wavreader.h"
  
  #define FRAMESIZE 512
  
  int main()
  {
  
      FILE *fh;
      FILE *fhWrite;
      struct WavHeader * header;
      struct WavHeader * newHeader;
      double frame[FRAMESIZE];
      int iBytesWritten;
      int i;
      char test;
      fh = fopen("D:/ArbeitsOrdner/advanced_pacev/AudioSample/spfg.wav", "rb+");
      if (fh == NULL)
      {
          printf("Failed to open organ.wav\n");
          return 1;
      }
  
      fhWrite = fopen("D:/ArbeitsOrdner/MyC/test_organ.wav", "wb+");
      if (fhWrite == NULL)
      {
          printf("Failed to create test_organ.wav\n");
          return 1;
      }
  
      header = readWaveHeader(fh);
      printWaveHeader(header);
  
      newHeader = createWaveHeader(header-&gt;iChannels, header-&gt;iSampleRate, header-&gt;iBitsPerSample);
      WaveWriteHeader(fhWrite, newHeader);
  
      while (WaveReadFrame(fh, header, FRAMESIZE, frame) != -1)
      {
          iBytesWritten = WaveWriteFrame(fhWrite, newHeader, FRAMESIZE, frame);
          if (iBytesWritten &lt; 0)
          {
              printf("Error occured while writing to new file\n");
              return 1;
          }
      }    
  
      WaveWriteHeader(fhWrite, newHeader);
  
      fclose(fhWrite);
      fclose(fh);
  
      return 0;
  }
  </code></pre>
  
  <p></p>