Click here to Skip to main content
15,885,309 members
Articles / Desktop Programming / MFC
Article

MP3 header reader

Rate me:
Please Sign up or sign in to vote.
4.17/5 (8 votes)
19 Apr 2004 132.5K   5.3K   51   15
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


Written By
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.

Comments and Discussions

 
QuestionNice Little Project Pin
Member 1035599714-Jan-14 11:46
Member 1035599714-Jan-14 11:46 
QuestionNot reliable Pin
Michael Chourdakis4-Oct-11 7:43
mvaMichael Chourdakis4-Oct-11 7:43 
Questionhow to do using C# Pin
napster922-Dec-10 19:22
napster922-Dec-10 19:22 
QuestionHow to find the ID3v2.3 tag size Pin
saranjeganinvent14-Mar-07 20:03
saranjeganinvent14-Mar-07 20:03 
AnswerRe: How to find the ID3v2.3 tag size Pin
Hamed J.I13-Apr-07 9:50
Hamed J.I13-Apr-07 9:50 
GeneralFrame extraction Pin
Missile_gandalf15-Nov-06 23:34
Missile_gandalf15-Nov-06 23:34 
GeneralRe: Frame extraction Pin
shoonya16-Nov-06 16:21
shoonya16-Nov-06 16:21 
Questioncan u provide me some notes on MP3 header. Pin
Sandip Gaikwad18-Jul-06 19:44
Sandip Gaikwad18-Jul-06 19:44 
Hi, i am just looking at some of the audio formats. Can u please provide me with the header format that is present in MP3 files. Which I can visually identify it.
Without running any C-code.
GeneralApplication crashes, gives exceptions Pin
Ashutosh Bhawasinka27-Jun-06 18:46
Ashutosh Bhawasinka27-Jun-06 18:46 
GeneralRe: Application crashes, gives exceptions [modified] Pin
shoonya28-Jun-06 1:50
shoonya28-Jun-06 1:50 
Generalfinding first frame Pin
cxt14-May-04 11:27
cxt14-May-04 11:27 
QuestionRead some field of header Pin
minh_atm4-Jan-07 16:49
minh_atm4-Jan-07 16:49 
GeneralNo VBR Pin
Fabc4-May-04 4:12
Fabc4-May-04 4:12 
GeneralBit mask Pin
Alexander M.,20-Apr-04 3:58
Alexander M.,20-Apr-04 3:58 
GeneralRe: Bit mask Pin
shoonya20-Apr-04 8:03
shoonya20-Apr-04 8:03 

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.