0

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?

mr.M
  • 851
  • 6
  • 23
  • 41
  • Are you checking many frames or just the first one? A frame represents a small fraction of one second so its possible you're checking a segment where the actual sound hasn't even begun to play... Out of 200 frames check the 199th frame's pcmOUT, does that have non-zero data? – VC.One May 10 '16 at 11:31

1 Answers1

2

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
  }
gpasch
  • 2,672
  • 3
  • 10
  • 12