QUOTE(freakngoat @ Mar 2 2004, 08:40 PM)
It doesn't seem the Lame tag always stores the bitrate (VBR), and it only stores the length of the music in bytes, not seconds. So how is this normally done?
If you have the bitrate and the bytesize, that's enough to get you the length in seconds, whether it's VBR or CBR. Let's take an example:
file A = 5480654 bytes, 192 kb/s CBR (winamp says 228 seconds)
192kbps = 192000 bits/sec = 24000 bytes/sec (1kbps = 1000 bps; 1 bit = 8 bytes)
5480654 / 24000 = 228.3605833 seconds
So as a formula:
seconds = bytes / (bitrate / 8)
Scanning the file frame-by-frame is of course the most accurate way and (if properly designed) can report the most accurate times & bitrates for corrupted files. You should scan for the first frame header, calculate the length of that frame in bytes, skip ahead that far to where the next frame header should be, check to see if a valid frame header is found there - if yes then continue, if no then go back to where you were and scan byte by byte until you find the next valid frame header (that is followed by a valid frame header to ensure you haven't found a false synch).
For any method you should probably have basic ID3v2 skipping code (it's very simple, read 10 bytes, calculate length of ID3v2, skip those bytes and then start scanning from there).
Xing and LAME both use the same VBR header structure (although LAME includes more information than the basic Xing specification, it's not relevant to the bitrate/playtime calculation). LAME also uses the identical structure with "Info" instead of "Xing" as the identifier - typically this is only for LAME CBR files with forced header, but it's legal to use "Info" or "Xing" for either VBR or CBR. PM me if you want to Xing SDK that outlines the header format. You can see an abbreviated description of it plus the full description of the LAME extension of it here:
http://gabriel.mp3-tech.org/mp3infotag.htmlThere's a couple (very) short VBRI-header sample files here:
http://www.getid3.org/samples/mp3/vbri/The "Xing"/"Info" string is located either 13, 21 or 36 bytes from the frame header, depending on MPEG version and number of channels:
MPEG-1 = 36 bytes (except mono = 21 bytes)
MPEG-2 = 21 bytes (except mono = 13 bytes)
MPEG-2.5 = 21 bytes (mono as well)
The "VBRI" string is always at 36 bytes. PM me if you want the VBRI SDK.