title: Why does buffer size affect audio data?
body: <p>I am using <code>javax.sound.sampled</code> and <code>JLayer</code> to play a <code>MP3</code> file. I am trying to analyze the audio input stream to determine when the song starts and when it ends (based on the audio levels in the beginning and end of the MP3). A 4 minute song may only have 3 minutes and 55 seconds of actual music while the rest is silence, which is why I am determining this.</p>
  
  <p>I thought I could determine this information by finding the first and last non-zero bytes in the stream.</p>
  
  <p><strong>Problem:</strong> The issue is that when I adjust the buffer size, the position of the first non-zero byte changes. Why is this, and shouldn't it remain constant no matter the buffer size?</p>
  
  <p>E.g. At a buffer size of 16, the startFrame correlates to the 17th byte. With a buffer size of 64, the startFrame correlates to the 65th byte.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>        byte[] buffer;
          int pos = 0;
          short silenceThreshold = 1;
  
          startFrame = 0;
          endFrame = -1;
  
          boolean startFrameSet = false;
  
          buffer = new byte[16];
          byte prevVal = 0;
          for (int n = 0; n != -1; n = audioInputStream.read(buffer, 0,
                  buffer.length)) {
  
              for (int i = 0; i &lt; buffer.length; i++) {
                  if (buffer[i] &gt;= silenceThreshold || buffer[i] &lt;= -silenceThreshold) {
                      // Is not silent
                      if (!startFrameSet) {
                          startFrame = (pos * buffer.length) + i;
                          startFrameSet = true;
                      }
                  } else {
                      // Silence
                      // If the previous value is &gt; 0 or &lt; 0, set endFrame
                      if (prevVal &gt;= silenceThreshold || prevVal &lt;= silenceThreshold) {
                          endFrame = (pos * buffer.length) + i;
                      }
                  }
                  prevVal = buffer[i];
              }
  
              pos++;
          }
  
          //If last byte is not within silence threshold (song doesn't end in silence).
          if (prevVal &gt;= silenceThreshold || prevVal &lt;= silenceThreshold) {
              // last frame is not silent
              endFrame = -1;
          }
  </code></pre>
  
  <p>I figure I misunderstood how the audio input stream and audio in general works.</p>