HydrogenAudio

Lossy Audio Compression => MP3 => MP3 - Tech => Topic started by: alucard.ptit@gmail.com on 2021-04-06 02:48:13

Title: How to decode using lame (mp3->wav) in C
Post by: alucard.ptit@gmail.com on 2021-04-06 02:48:13
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
Title: Re: How to decode using lame (mp3->wav) in C
Post by: kode54 on 2021-04-06 03:49:42
It wants a single frame at a time, I assume.
Title: Re: How to decode using lame (mp3->wav) in C
Post by: alucard.ptit@gmail.com on 2021-04-06 04:15:24
I tried with 1 frame. it still returns 0.
Title: Re: How to decode using lame (mp3->wav) in C
Post by: kode54 on 2021-04-06 08:23:34
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.
Title: Re: How to decode using lame (mp3->wav) in C
Post by: alucard.ptit@gmail.com on 2021-05-11 04:04:20
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 ?
Title: Re: How to decode using lame (mp3->wav) in C
Post by: kode54 on 2021-05-11 04:27:20
There is no bit depth, it's floating point. Unclipped, too.
Title: Re: How to decode using lame (mp3->wav) in C
Post by: alucard.ptit@gmail.com on 2021-05-11 04:38:34
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
Title: Re: How to decode using lame (mp3->wav) in C
Post by: kode54 on 2021-05-11 07:59:22
You'll have to convert it to 16 bit then. MP3 has no bit depth.
Title: Re: How to decode using lame (mp3->wav) in C
Post by: alucard.ptit@gmail.com on 2021-05-11 08:23:49
Thank you very much.
So is there no way to calculate bit depth based on pcm data?
Title: Re: How to decode using lame (mp3->wav) in C
Post by: saratoga on 2021-05-11 16:47:55
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. 
Title: Re: How to decode using lame (mp3->wav) in C
Post by: kode54 on 2021-05-11 22:58:06
LAME outputs 32 bit floating point.
Title: Re: How to decode using lame (mp3->wav) in C
Post by: alucard.ptit@gmail.com on 2021-05-12 02:47:00
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)
Title: Re: How to decode using lame (mp3->wav) in C
Post by: kode54 on 2021-05-12 03:53:46
Using two different decoding functions, I guess.