I want to decode mp3 to pcm using lame on android.
I have found the article below.
https://www.jianshu.com/p/c9feaec59bab
But the problem I have is
samples = hip_decode1_headers(hip, mp3_buffer, mp3_len, pcm_l, pcm_r, &mp3data);
always returns 0 when mp3_buffer I removed the ID3 tag and it contains about 8 mp3 frames
It wants a single frame at a time, I assume.
I tried with 1 frame. it still returns 0.
Maybe this will be helpful?
https://github.com/VFR-maniac/lame/blob/master/frontend/get_audio.c#L2051
Apparently, when decoding the first frame of the file, which may be a LAME/Xing header, you should use hip_decode1_headersB(), which also accepts parameters for the encoder delay and padding fields to be returned to. You should be starting your stream and decoding at least until it reports that the header has been parsed.
Thank you brother, I fixed it.
By the way I have 1 more question.
When I decode from mp3 -> pcm. So how do calculate the bit depth of the data pcm ?
There is no bit depth, it's floating point. Unclipped, too.
because i want to play pcm with audiotrack.
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
SAMPLERATE,
AudioFormat.CHANNEL_OUT_STEREO,
AudioFormat.ENCODING_PCM_16BIT,
minBufferSize,
AudioTrack.MODE_STREAM);
I'm hard-coded as ENCODING_PCM_16BIT to init audiotrack.
That is not true. So I want to know the exact bitrate of the pcm data
You'll have to convert it to 16 bit then. MP3 has no bit depth.
Thank you very much.
So is there no way to calculate bit depth based on pcm data?
Thank you very much.
So is there no way to calculate bit depth based on pcm data?
If you know that samples are integer and how many samples there are you can divide the total size by the number of samples.
LAME outputs 32 bit floating point.
Hello kode54. You are a great admin.
I realized that: the duration of mp3 and pcm after decoding will be equal => I have a formula for calculating the bit depth
bit depth = pcm size / (samepleRate * duration * digital channel).
Since I decode each frame of the mp3 file I can calculate its duration and know the output pcm size.
The problem is when I decode the first few frames (4 frames) then
The bit depth is different from the following frame.
For example, for the first 4 frames pcm, the size I get is:
pcm size: short 18432 = 36864 bytes
samepleRate = 44100
duration = 0.026122 * 4 = 0.14488
channel = 2.
According to the above formula, bit depth = 4 => ENCODING_PCM_FLOAT
For the rest of the frames, use the next 3 frames after the first 4 frames.
pcm size = short 6912 = 13824 bytes
samepleRate = 44100
duration = 0.078366
channel = 2
=> bit depth = 2 => ENCODING_PCM_16BIT
The problem I want to ask is: Why did the first frames (4 frames) produce larger pcm size than the rest.
And is my bit depth formula correct for the first frames?
(my explanation and english is not good. If there is something not understood please ask me again. Thanks)
Using two different decoding functions, I guess.