Decoding MP3 files with JLayer
Asked Answered
C

1

3

I want to use JLayer to decode an MP3 file. I have searched and searched for documentation and examples on how exactly to do this, and have turned up nothing of use. Everything I find is embedded in other examples or references JavaSound, which is unacceptable in my case.

I feel like this is incredibly easy, but I can't figure out how to do it. I don't know what the parameters are for

Decoder decoder = new Decoder();
decoder.decodeFrame(Header header, Bitstream stream);

or how to obtain them.

tl;dr How do I decode an MP3 file with nothing but JLayer? No MP3 SPI, JavaSound, Tritonus--nothing.

Cholula answered 23/8, 2012 at 20:2 Comment(5)
You want to decode and do what ? store as Wave file ? play it using speakers ?Bogosian
@VitalyPolonetsky Store the samples in an array. I just want to be able to get all the samples out of an mp3 file.Cholula
So you want to store the samples for some future use (I suppose). But in what format you need the samples ?Bogosian
PCM. Jlayer has the decodeFrame method that returns a buffer of samples. I just want to know how to use that method to get a given amount of samples.Cholula
@Cholula did u have the code for converting .mp3 to pcm data using jLayer ?Mercuric
C
4

Figured it out myself.

Bitstream bitStream = new Bitstream(new FileInputStream("path/to/audio.mp3"));

while(condition){
    Decoder decoder = new Decoder();
    int[] samples = decoder.decodeFrame(bitStream.readFrame(), bitStream); //returns the next 2304 samples
    bitStream.closeFrame();

    //do whatever with your samples
}
Cholula answered 15/9, 2012 at 5:33 Comment(2)
Whatever you want. It controls how much of the mp3 is decoded. So if you wanted to decode the whole file, your condition would be whether there are more samples to decode or not.Cholula
@Albatross: this is really interesting. Do you think this could be used to stream mp3 over internet? And also how do you play those samples after your while ?Handicapped

© 2022 - 2024 — McMap. All rights reserved.