Java equivelant to AS3's sound.extract()?

I’m trying to port some Actionscript code to Java, and I’m looking for a way to get an array of frequencies in a format where it’s 1 float per sample per channel and there’s 2 channels from an mp3 file.

Anyone have an example available on how to accomplish this?

Do you really mean frequencies or just an array of samples?
Guessing you mean samples, check the documentation of JLayer (an mp3 decoder library) at http://www.javazoom.net/javalayer/documents.html
I never used JLayer myself, but I’m sure you can figure out how to get the decoded audio data from your mp3 using JLayer using the docs.

hrmm I’ll look into it, but right now I’ve got this working:


import java.io.*;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;


public class JMFTest {

	public static void main(String[] args) throws Exception {
       FileInputStream in = new FileInputStream("test.mp3");
       AudioInputStream source = AudioSystem.getAudioInputStream(new BufferedInputStream(in, 1024));
       AudioInputStream pcm = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, source);
       File newFile = new File("test.wav");
       AudioSystem.write(pcm, AudioFileFormat.Type.WAVE, newFile);
       FileInputStream fis = new FileInputStream(newFile);
       ByteArrayOutputStream boas = new ByteArrayOutputStream(new Long(newFile.length()).intValue());
       byte[] buf = new byte[8 * 1024];
       for(int len=0; (len = fis.read(buf)) != -1;)
       {
    	   boas.write(buf,0,len);
       }
       byte[] ba = boas.toByteArray();
       for(int i = 44; i < ba.length; i+=2)
       {
    	   float value = ((ba[i] & 0xFF) | (ba[i + 1] << 8))/ 32768.0F;
    	   value /= 2.0;
    	   ba[i + 1] = (byte)((int)(value * 32768) >> 8);
    	   ba[i] = (byte)((int)(value * 32768) & 0xFF);
       }
       System.out.println(ba.length - 44);
       File newerFile = new File("test1.wav");
       FileOutputStream fos = new FileOutputStream(newerFile);
       fos.write(ba);
    }
}

right now it halves the volume of the sound file, but only seems to work in the format I want if the mp3 used is in the same format, so I’m looking for a simpler and more consistent way to do this, I’ll look into JLayer, and see if I can find what I’m looking for.

(Fast) fourier transform will give you the spectrum. There’s plenty of Java FFT implementations around.