Click here to Skip to main content
12,402,202 members (51,348 online)
Click here to Skip to main content
Add your own
alternative version

Stats

106.3K views
4.6K downloads
51 bookmarked
Posted

MP3 header reader

, 19 Apr 2004
Rate this:
Please Sign up or sign in to vote.
Program to read information in MP3 frame header.

Introduction

This program reads the first frame of an MP3 file and displays all the header information like, version, layer, bit rate, sampling frequency, etc.

Using the code

This happens to be the simplest thing on this planet! Check out the source code... you need to know about the MPEG header.

// finding first frame for MPEG 1 LAYER III
do
{
    curch=fgetc(fp);
    if(curch==255)
    {
        curch=fgetc(fp);
        if(tolower(curch/16)==15) flag1=1;
    }
}while(flag1==0);

// position of first frame header......
fpos_t filepos;
char firstframeposition[10];

fgetpos(fp,&filepos);
filepos-=2;

int firstheaderpos=filepos;
_ltoa(firstheaderpos,firstframeposition,10);
m_strinfo="First frame found at byte :";
m_strinfo+=firstframeposition;
m_ctlinfo.AddString(m_strinfo);


// version check MPEG 1/2...
int impegversion=tolower(((curch%16)/4)/2);
if(impegversion==1) m_strinfo="MPEG 1 ";
if(impegversion==0) m_strinfo="MPEG 2 ";


// layer check....
int layer=tolower((((curch%16)/4)%2)*2+(((curch%16)%4)/2));
CString mpeglayer[4]={"Reserved","Layer III","Layer II","Layer I"};
m_strinfo+=mpeglayer[layer];
m_ctlinfo.AddString(m_strinfo);

if(layer!=1) return 1;

// CRC check.......
int protectbit=tolower(((curch%16)%4)%2);
if(protectbit==1) m_strinfo="CRCs : No";
else m_strinfo="CRCs : Yes";
m_ctlinfo.AddString(m_strinfo);


//get next byte to read 3rd byte of header. 
curch=fgetc(fp);


// bitrate info
int bitratetable[2][16]={0,8,16,24,32,64,80,56,64,128,160,
  112,128,256,320,0,0,32,40,48,56,64,80,96,
  112,128,160,192,224,256,320,0};
int mp3bitrate=bitratetable[impegversion][tolower(curch/16)];
char mp3bps[2];
_itoa(mp3bitrate,mp3bps,10);
m_strinfo="Bitrate (kbps) : ";
m_strinfo+=mp3bps;
m_ctlinfo.AddString(m_strinfo);


// frequency info
long int frequencytable[2][4]={22050,24000,16000,0,44100,48000,32000,0};
int mp3frequency=frequencytable[impegversion][tolower((curch%16)/4)];
char mp3fs[10];
_itoa(mp3frequency,mp3fs,10);
m_strinfo="Frequency (Hz) : ";
m_strinfo+=mp3fs;
m_ctlinfo.AddString(m_strinfo);


// padding bit
int paddingbit=tolower(((curch%16)%4)/2);
if(paddingbit==0) m_strinfo="Padding : No";
else m_strinfo="Padding : Yes";
m_ctlinfo.AddString(m_strinfo);


// private bit
int privatebit=tolower(((curch%16)%4)%2);
if(privatebit==0) m_strinfo="Private : No";
else m_strinfo="Private : Yes";
m_ctlinfo.AddString(m_strinfo);


// get next char to read 4th byte of header.
curch=fgetc(fp);

//channel mode
CString channelmode[4]={"Stereo","Joint Stereo",
                         "Dual Channel","Single Channel"};
int ichannelmode=tolower((curch/16)/4);
m_ctlinfo.AddString(channelmode[ichannelmode]);


// mode extension ignored.

// copyright bit
int copyrightbit=tolower(((curch%16)/4)/2);
if(copyrightbit==1) m_strinfo="Copyrighted : Yes";
else m_strinfo="Copyrighted : No";
m_ctlinfo.AddString(m_strinfo);


//original bit
int originalbit=tolower(((curch%16)/4)%2);
if(originalbit==1) m_strinfo="Original : Yes";
else m_strinfo="Original : No";
m_ctlinfo.AddString(m_strinfo);


//emphasis
int emphasis=tolower((curch%16)%4);
if(emphasis==0) m_strinfo="Emphasis : None";
if(emphasis==1) m_strinfo="Emphasis : 50/15 ms";
if(emphasis==3) m_strinfo="Emphasis : CCITT J.17";
m_ctlinfo.AddString(m_strinfo);



// all header info over..... calculating frame size.
int iframesize=(144000*mp3bitrate/mp3frequency)+paddingbit;
char framesize[5];
_itoa(iframesize,framesize,10);
m_strinfo="Framesize : ";
m_strinfo+=framesize;
m_ctlinfo.AddString(m_strinfo);


//no. of frames...
int iframes=(filesize-firstheaderpos)/iframesize;
char frames[10];
_itoa(iframes,frames,10);
m_strinfo="Frames : ";
m_strinfo+=frames;
m_ctlinfo.AddString(m_strinfo);


//song length...
int iplaytime=toupper(iframes*26/1000);
char playtime[5];
_itoa(iplaytime,playtime,10);
m_strinfo="Length (in sec) : ";
m_strinfo+=playtime;
m_ctlinfo.AddString(m_strinfo);

Points of Interest

I learned about the MP3 file format... And this is my first project that I am uploading to the net.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

shoonya
Web Developer
India India
I was doing some VC++ coding for 2 years while at college. But once i got out of college in 2005, went to work for an IT company in the enterprise solutions space.

Since 2005 working on TIBCO technologies. Couple of times i started working on some C# projects but never went about completing them.

You may also be interested in...

Comments and Discussions

 
QuestionNice Little Project Pin
Member 1035599714-Jan-14 11:46
memberMember 1035599714-Jan-14 11:46 
QuestionNot reliable Pin
Michael Chourdakis4-Oct-11 7:43
memberMichael Chourdakis4-Oct-11 7:43 
Questionhow to do using C# Pin
napster922-Dec-10 19:22
membernapster922-Dec-10 19:22 
QuestionHow to find the ID3v2.3 tag size Pin
saranjeganinvent14-Mar-07 20:03
membersaranjeganinvent14-Mar-07 20:03 
AnswerRe: How to find the ID3v2.3 tag size Pin
Hamed_ji13-Apr-07 9:50
memberHamed_ji13-Apr-07 9:50 
GeneralFrame extraction Pin
Missile_gandalf15-Nov-06 23:34
memberMissile_gandalf15-Nov-06 23:34 
GeneralRe: Frame extraction Pin
shoonya16-Nov-06 16:21
membershoonya16-Nov-06 16:21 
Questioncan u provide me some notes on MP3 header. Pin
Sandip Gaikwad18-Jul-06 19:44
memberSandip Gaikwad18-Jul-06 19:44 
GeneralApplication crashes, gives exceptions Pin
Ashutosh Bhawasinka27-Jun-06 18:46
memberAshutosh Bhawasinka27-Jun-06 18:46 
GeneralRe: Application crashes, gives exceptions [modified] Pin
shoonya28-Jun-06 1:50
membershoonya28-Jun-06 1:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 20 Apr 2004
Article Copyright 2004 by shoonya
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid