Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: How to decode using lame (mp3->wav) in C (Read 4167 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

How to decode using lame (mp3->wav) in C

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

 

Re: How to decode using lame (mp3->wav) in C

Reply #1
It wants a single frame at a time, I assume.

Re: How to decode using lame (mp3->wav) in C

Reply #2
I tried with 1 frame. it still returns 0.

Re: How to decode using lame (mp3->wav) in C

Reply #3
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.

Re: How to decode using lame (mp3->wav) in C

Reply #4
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 ?

Re: How to decode using lame (mp3->wav) in C

Reply #5
There is no bit depth, it's floating point. Unclipped, too.

Re: How to decode using lame (mp3->wav) in C

Reply #6
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

Re: How to decode using lame (mp3->wav) in C

Reply #7
You'll have to convert it to 16 bit then. MP3 has no bit depth.

Re: How to decode using lame (mp3->wav) in C

Reply #8
Thank you very much.
So is there no way to calculate bit depth based on pcm data?

Re: How to decode using lame (mp3->wav) in C

Reply #9
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. 

Re: How to decode using lame (mp3->wav) in C

Reply #10
LAME outputs 32 bit floating point.

Re: How to decode using lame (mp3->wav) in C

Reply #11
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)

Re: How to decode using lame (mp3->wav) in C

Reply #12
Using two different decoding functions, I guess.