title: Java spectrogram images: mp3 and microphone
body: <p>First of all i'm working on a little project to see the spectrum from some sounds.</p>
  <p>I got this working with a microphone:
  <a href="http://img25.imageshack.us/img25/4271/spectrumanalyzerfourier.png" rel="nofollow noreferrer">alt text http://img25.imageshack.us/img25/4271/spectrumanalyzerfourier.png</a></p>
  <p>The image above is just me talking and shouting through a microphone for a few seconds. This looks good to me.</p>
  <p>But when I try to read an MP3 file and make a spectogram image of it it looks a bit different. I tried the Aphex Twin - Windowlicker where you should normally see a face in the spectrogram image or at least some more darker colors. But it doesn't look so good:
  <a href="http://img10.imageshack.us/img10/3475/aphextwinhmm.png" rel="nofollow noreferrer">alt text http://img10.imageshack.us/img10/3475/aphextwinhmm.png</a></p>
  <p>Here is what I did with the microphone:</p>
  <pre><code>byte tempBuffer[] = new byte[10000];
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  counter = 20;
  
  // Microphone
  while (counter != 0) {
   int count = line.read(tempBuffer, 0, tempBuffer.length);
   if (count &gt; 0) {
    out.write(tempBuffer, 0, count);
   }
   counter--;
  }
  out.close();
  
  // FFT code below ...
  byte audio[] = out.toByteArray();
  // ...
  </code></pre>
  <p>And this is how I do it with the MP3:</p>
  <p><em>I used the same code to do the transformation and visualization only the audio capturing part is different (I only adjusted the hight in the drawing method to see if there is a difference but there wasn't one):</em></p>
  <pre><code>byte tempBuffer[] = new byte[10000];
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  FileInputStream input = null;
  
  File mp3 = new File(&quot;Aphex Twin - Widowlicker.mp3&quot;);
  input = new FileInputStream(mp3);
  int len;
  while((len = input.read(tempBuffer)) &gt; 0) {
   out.write(tempBuffer, 0, len);
  }
  
  out.close();
  input.close();
  
  // FFT code below ...
  byte audio[] = out.toByteArray();
  // ...
  </code></pre>
  <p>It would be nice if somebody could point me out what I am doing wrong with the MP3 file.</p>
  <p>These are my settings:</p>
  <ul>
  <li>Sample rate: 44100</li>
  <li>Bit per sample: 8</li>
  <li>Channels: 1 (mono)</li>
  <li>signed: true</li>
  <li>big endian: true <em>(i'm using AudioFormat in Java)</em></li>
  <li>tempBuffer to read audio: 10000 ( byte tempBuffer[] = new byte[10000]; )</li>
  <li>and for the FFT I split the audio in chuncks of 4096 (must be a power of 2)</li>
  </ul>
  <p>By the way: are these settings ok or should I use 16bps or stereo or is 10000 for the buffer too much or 4096 to small/big ?</p>
  <p>Thanks in advance</p>