BASS & Calculating Frame Size

Started by Sebastian_Mares,

Sebastian_Mares

Hello,

I was wondering how BASS calculates the frame size of a MP3 file.
I have got a file with this specification:

MPEG 2.5 Layer III
128 kbp/s (CBR)
8000 Hz, Mono
4:27

When using the following formula...

Frame Size = (144 * (BitRate * 1000) / Sampling Rate) + Padding
Frames = Audio Size / Frame Size
Duration = Frames / 27.8

...the duration is 1:12 (1/4 of the real length).

BASS calculates the length correctly. How does it do that?

PS: The 27.8 are based on the sampling rate. See Azoth's code for more information: http://www.dynamic-technologies.net/MPEGHeaderDemo.zip

Sebastian_Mares

#1
The real number of frames is 3973 and my program returns 1986. Therefore, something is wrong with

Frame Size = (144 * (BitRate * 1000) / Sampling Rate) + Padding



PS: The code works good for a 44100 Hz file using stereo mode.

Sebastian_Mares

#2
I have just tested the code on a stereo MP3 file using 48000 Hz.
The frame number is correct, but the duration is returned as 6:08.

Ian @ un4seen

You need to check the "version" bit... if that's 0 (ie. not set), then halve the frame size.

Sebastian_Mares

OK, I just did and it still doesn't work. It doesn't return 1:12 anymore instead of 4:47, it returns 2:23. When the sampling rate is 48 KHz, it returns 6:08.

BTW, the problem here actually doesn't have anything to do with BASS. I was just wondering how comes that BASS calculates the length, frame size... correctly and my program (which doesn't uses BASS) not.

Sebastian_Mares

One more thing... The frame size IS correct on on the 48 KHz file, only the duration not, while on the 8 KHz one, the frame size is only one half of the actual size (1986 instead of 3973).

Ian @ un4seen

QuoteDuration = Frames / 27.8
That calculation needs changing...

   Duration = Frames * 1152 / Sampling Rate

Again, if the "version" bit is not set, halve it.

Sebastian_Mares

Cool, thanks for the tip! It works now!

I will fix some other bugs found in the code and change some routines with ones I have found in a Delphi code.
Apropos Delphi, do you know what "Div" means? Does it have anything to do with division?

Sebastian_Mares

#8
QuoteApropos Delphi, do you know what "Div" means? Does it have anything to do with division?
OK, I found out that Div divides two numbers and discards the decimals (17 / 2 = 8.5 and 17 Div 2 = 8).

I will use Fix(17 / 2) in VB

Sebastian_Mares

OK, two more things:

1. I have to halve the frame size if the MPEG version is 2.5 and 2.0.
2. The formula you gave me applies only for layer II and III. For layer I replace the 1152 with 384.