title: Downsample PCM audio 44100 to 8000
body: <p>I've taken over maintaining an android application that records an audio source using AudioRecord in 44100, 16-bit mono.  I need to downsample it to 8000.  The current app has a downsample algorithm in it that I'm not sure is working correctly.  When another app (which is a black box) receives it, the audio plays but with a loud buzzing sound in the background and slower than expected audio.  Other audio in the proper format piped into the receiving app is received fine with no issues.  </p>
  
  <p>The algorithm that I have is listed here (I suspect it was originally decompiled code from somewhere based on the variable names).  </p>
  
  <pre><code>private void downSample() {
      int v5 = 0;
      int v4 = 0;
      int v2;
      for (v2 = 0; v2 &lt; 0xA0; ++v2) {
          v5 += 0xD755;
          int v6 = v5 / 0x2710;
          v5 -= v6 * 0x2710;
          double v0 = 0;
          int v3 = v4;
          v4 += v6;
          while (v3 &lt; v4) {
              v0 += ((double) ((((float) this.readBuffer[v3])) * this.volume));
              ++v3;
          }
  
          v0 /= ((double) v6);
          if (32767 &lt; v0) {
              v0 = 32767;
          }
          else if (-32768 &gt; v0) {
              v0 = -32768;
          }
  
          this.downSampledBuffer[v2] = ((short) (((int) v0)));
      }
  }
  </code></pre>
  
  <p>readBuffer is a short[] that is populated by the recording source.  downSampledBuffer is also a short[]. Any thoughts on what is going wrong?   </p>