I want to do the FFT on a MP3 file. For this, I would like to extract the decoded byte stream from this file format (I believe it is called the raw PCM data).For this task I am using the jLayer 1.0.1 library. Here the code that should extract the PCM data for each frame:
short[] pcmOut = {};
Bitstream bitStream = new Bitstream(new FileInputStream(path_to_mp3_file));
boolean done = false;
while (!done) {
Header frameHeader = bitStream.readFrame();
if (frameHeader == null) {
done = true;
} else {
Decoder decoder = new Decoder();
SampleBuffer output = (SampleBuffer) decoder.decodeFrame(bitStream.readFrame(), bitStream); //returns the next 2304 samples
short[] next = output.getBuffer();
pcmOut = concatArrays(pcmOut, next);
//do whatever with your samples
}
bitStream.closeFrame();
}
for (int i = 0; i < pcmOut.length; i++) {
if (pcmOut[i] != 0) {
System.out.println(pcmOut[i]);
}
}
The problem is that the variable short[] pcmOut
is filled with the zeros only for a valid MP3 file. What is the root cause of such problem?
Checking this it seems to be related to the Decoder; apart from that it is a waste to create a new decoder each time it seems it is a fundamental problem in this implementation as it seems to return to the first frame while a constant decoder seems to keep track:
Decoder decoder = new Decoder();
while (!done) {
Header frameHeader = bitStream.readFrame();
if (frameHeader == null) {
done = true;
}
else {
SampleBuffer output = (SampleBuffer) decoder.decodeFrame(frameHeader, bitStream);
short[] next = output.getBuffer();
for(int i=0; i<next.length; i++) System.out.print(" "+next[i]);
pcmOut = concatArrays(pcmOut, next);
//do whatever with your samples
}