Thread View: borland.public.cppbuilder.multimedia
3 messages
3 total messages
Started by Adam Casperson
Sun, 09 Dec 2001 09:45
mpeg audio headers
Author: Adam Casperson
Date: Sun, 09 Dec 2001 09:45
Date: Sun, 09 Dec 2001 09:45
96 lines
3606 bytes
3606 bytes
Hi all
Im having a lot of trouble trying to read mp3 file headers (its driving me
nuts actually). I only need to read the version, layer, and bitrate so that
I can then calculate the play time of the song. Below is the code I'm using
to do this
int __fastcall TForm1::GetMPEGFileLength(String fileName)
{
DWORD buffer;
int fileSize, temp;
FILE *openFile;
if((openFile = fopen(fileName.c_str(), "rb")) != NULL) {
fileSize = fseek(openFile, 0, 2);
fseek(openFile, 0, 0);
fread(&buffer, 1, 4, openFile);
temp = ExtractBinary(buffer, 19, 20);
PlayList->Items->Add("Ver bin " + String(temp));
switch (temp)
{
case 1:
mpegVer = 0;
break;
case 2:
mpegVer = 2;
break;
case 3:
mpegVer = 1;
break;
default:
mpegVer = -1;
}
temp = ExtractBinary(buffer, 17, 18);
PlayList->Items->Add("Layer bin " + String(temp));
switch (temp)
{
case 1:
mpegLayer = 3;
break;
case 2:
mpegLayer = 2;
break;
case 3:
mpegLayer = 1;
break;
default:
mpegLayer = -1;
}
temp = ExtractBinary(buffer, 12, 15);
PlayList->Items->Add("Rate bin " + String(temp));
int x = -1;
if (mpegVer == 1)
{
if (mpegLayer == 1) x = 0;
else if (mpegLayer == 2) x = 1;
else if (mpegLayer == 3) x = 2;
}
else if (mpegVer == 2)
{
if (mpegLayer == 1) x = 3;
else if ((mpegLayer == 2) || (mpegLayer == 3)) x = 4;
}
mpegBitRate = bitrates[x][temp];
fclose(openFile);
int seconds = (fileSize * 8) / (mpegBitRate * 1000);
return seconds;
} else {
return 0;
}
}
//--------------------------------------------------------------------------
-
int __fastcall TForm1::ExtractBinary(DWORD binary, int leastSig, int
mostSig)
{
DWORD temp = binary;
for(int x = 0; x < (32 - (mostSig + 1)); x++) {
temp <<= 1;
}
for(int x2 = 0; x2 < (32 - (mostSig + 1)); x2++) {
temp >>= 1;
}
for(int y = 0; y < leastSig; y++) {
temp >>= 1;
}
return (int)temp;
}
Using this code, for some files I get the correct version and layer but
incorrect bitrate, or I get the wrong version and layer and the bitrate is
then blown out to some stupid number like -480000. Does anyone know what I'm
doing wrong? Thanks
Re: mpeg audio headers
Author: Dave Richards
Date: Tue, 11 Dec 2001 17:57
Date: Tue, 11 Dec 2001 17:57
27 lines
1981 bytes
1981 bytes
"Adam Casperson" <casper2700@yahoo.com.au> wrote in message
news:3c13328e_1@dnews...
> Im having a lot of trouble trying to read mp3 file headers (its driving me
> nuts actually).
I know the feeling ... and sadly I don't think I have the answer to your
problem. The only comments I'd make are that it's not necessarily safe to
assume that the start of an MP3 file is aligned with a block and possibly
you could improve reliability by looking for the sync pulse in the stream
(although I'm not entirely sure).
I have a slight suspicion that the ExtractBinary routine doesn't do exactly
what you want (like it is leaving one more MSB on than you want (?) ). You
could do something altogether more elegant along the lines of:
// leastSig, mostSig are integers from 0 to 31 indicating the bit position
DWORD __fastcall TForm1::ExtractBinary(DWORD binary, int leastSig, int
mostSig)
{
unsigned long msb_mask = (1<<mostSig)-1;
unsigned long lsb_mask = (1<<leastSig)-1;
unsigned long data_mask = msb_mask & ~lsb_mask;
return (binary & data_mask) >> leastSig;
}
Dave.
Re: mpeg audio headers
Author: Dave Richards
Date: Tue, 11 Dec 2001 17:58
Date: Tue, 11 Dec 2001 17:58
4 lines
1088 bytes
1088 bytes
As a slight postscript, you could just bolt on an open source MPEG decoder like MPG123. Overkill, I know, but saves immense hassle. Dave.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads