title: Adjusting audio volume in real time from byte[]?
body: <p>I am trying to write a simple application which plays sound and can alter the volume of that sound at any time during playing. I am doing this by converting each pair of bytes in the byte array of the sound into an int, then multiplying that int by increase or decrease in volume and then writing them back as two bytes (i.e. 1 sample). However, this results in extreme distortion in the sound. Is it possible that I have got the bit shifting wrong? My sound format is:</p>
  
  <pre><code>.wav 44100.0hz, 16bit, little-endian
  </code></pre>
  
  <p>At the moment the byte array that I pass the adjustVolume method represents a 10th of a second of audio data. i.e. sampleRate/10</p>
  
  <p><strong>Is there something I am missing here that is causing it to distort and not scale volume properly? Have I got the writing of bytes back and fort wrong?</strong></p>
  
  <pre><code>private byte[] adjustVolume(byte[] audioSamples, double volume) {
          byte[] array = new byte[audioSamples.length];
          for (int i = 0; i &lt; array.length; i += 2) {
              // convert byte pair to int
              int audioSample = (int) (((audioSamples[i + 1] &amp; 0xff) &lt;&lt; 8) | (audioSamples[i] &amp; 0xff));
  
  
              audioSample = (int) (audioSample * volume);
  
  
              // convert back
              array[i] = (byte) audioSample;
              array[i + 1] = (byte) (audioSample &gt;&gt; 16);
  
          }
          return array;
      }
  </code></pre>
  
  <p>This code is based off of: <a href="https://stackoverflow.com/questions/14485873/audio-change-volume-of-samples-in-byte-array#comment20185595_14485873">Audio: Change Volume of samples in byte array</a> in which the asker is trying to do the same thing. However, having used the code from his question (which I think was not updated after he got his answer) I can't get it to work and I am not exactly sure what it is doing.</p>