Click here to Skip to main content
Licence 
First Posted 19 Apr 2004
Views 83,850
Bookmarked 49 times

MP3 header reader

By | 19 Apr 2004 | Article
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

About the Author

shoonya

Web Developer

India India

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionNot reliable PinmemberMichael Chourdakis7:43 4 Oct '11  
Questionhow to do using C# Pinmembernapster919:22 22 Dec '10  
QuestionHow to find the ID3v2.3 tag size Pinmembersaranjeganinvent20:03 14 Mar '07  
AnswerRe: How to find the ID3v2.3 tag size PinmemberHamed_ji9:50 13 Apr '07  
GeneralFrame extraction PinmemberMissile_gandalf23:34 15 Nov '06  
GeneralRe: Frame extraction Pinmembershoonya16:21 16 Nov '06  
Questioncan u provide me some notes on MP3 header. PinmemberSandip Gaikwad19:44 18 Jul '06  
GeneralApplication crashes, gives exceptions PinmemberAshutosh Bhawasinka18:46 27 Jun '06  
GeneralRe: Application crashes, gives exceptions [modified] Pinmembershoonya1:50 28 Jun '06  
Generalfinding first frame Pinmembercxt11:27 14 May '04  
QuestionRead some field of header Pinmemberminh_atm16:49 4 Jan '07  
GeneralNo VBR PinmemberFabc4:12 4 May '04  
GeneralBit mask PinmemberAlexander M.3:58 20 Apr '04  
GeneralRe: Bit mask Pinmembershoonya8:03 20 Apr '04  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 20 Apr 2004
Article Copyright 2004 by shoonya
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid