title: Downsampling a wav file using libsox
body: <p>I'm trying to convert a 16KHZ 16-bit Signed PCM encoded wave file to 8KHz 8-bit mu-encoded wav file using the Sox library C program (<a href="http://sox.sourceforge.net/" rel="nofollow noreferrer">http://sox.sourceforge.net/</a>). The conversion from PCM to mu is working fine. But when I apply the down-sampling effect the output file duration is just half of the i/p file(see below). I used the technique mentioned in the post <a href="https://stackoverflow.com/questions/16806592/how-to-change-the-samples-rates-when-do-the-format-conversion-by-sox-c-libraries/25061482#25061482">How to change the samples rates when do the format conversion by sox C libraries?</a> but it didn't help me.</p>
  
  <p>When I execute the below code I see a warning</p>
  
  <pre><code>wav: Premature EOF on .wav input file
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>Input File     : 'text2speech_0.wav'
  Channels       : 1
  Sample Rate    : 16000
  Precision      : 16-bit
  **Duration       : 00:00:06.24 = 99777 samples ~ 467.705 CDDA sectors**
  File Size      : 200k
  Bit Rate       : 256k
  Sample Encoding: 16-bit Signed Integer PCM
  
  
  Input File     : 'out_8k.wav'
  Channels       : 1
  Sample Rate    : 8000
  Precision      : 14-bit
  **Duration       : 00:00:03.12 = 24945 samples ~ 233.859 CDDA sectors**
  File Size      : 49.9k
  Bit Rate       : 128k
  Sample Encoding: 8bit u-law
  </code></pre>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>int main(int argc, char * argv[])
  {
      static sox_format_t * in, * out; /* input and output files */
      sox_effects_chain_t * chain;
      sox_effect_t * e;
      char * args[10];
      assert(argc == 3);
      assert(sox_init() == SOX_SUCCESS);
      assert(in = sox_open_read(argv[1], NULL, NULL, NULL));
  
      assert(out = sox_open_write(argv[2], &amp;in-&gt;signal, NULL, NULL, NULL, NULL));
  
      chain = sox_create_effects_chain(&amp;in-&gt;encoding, &amp;out-&gt;encoding);
  
      e = sox_create_effect(sox_find_effect("input"));
      args[0] = (char *)in, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
      assert(sox_add_effect(chain, e, &amp;in-&gt;signal, &amp;in-&gt;signal) == SOX_SUCCESS);
      free(e);
  
      out-&gt;signal.rate = 8000;
      in-&gt;signal.rate = 16000;
  
     if (in-&gt;signal.rate != out-&gt;signal.rate) {
          e = sox_create_effect(sox_find_effect("rate"));
          args[0] = "16000", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
          assert(sox_add_effect(chain, e, &amp;in-&gt;signal, &amp;out-&gt;signal) == SOX_SUCCESS);
          free(e);
      }
  
      if (in-&gt;signal.channels != out-&gt;signal.channels) {
          e = sox_create_effect(sox_find_effect("channels"));
          assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
          assert(sox_add_effect(chain, e, &amp;in-&gt;signal, &amp;out-&gt;signal) == SOX_SUCCESS);
          free(e);
      }
  
      e = sox_create_effect(sox_find_effect("output"));
      args[0] = (char *)out, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
      assert(sox_add_effect(chain, e, &amp;in-&gt;signal, &amp;out-&gt;signal) == SOX_SUCCESS);
      free(e);
  
      sox_flow_effects(chain, NULL, NULL);
  
      sox_delete_effects_chain(chain);
      sox_close(out);
      sox_close(in);
      sox_quit();
  
      return 0;
  }
  </code></pre>
  
  <p><strong>Compilation &amp; Execution:</strong></p>
  
  <pre><code>gcc -g -o example3 example3.c `pkg-config --cflags --libs sox`
  ./example3 text2speech_0.wav out_8k.wav
  </code></pre>