Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

MP3 header reader

0.00/5 (No votes)
19 Apr 2004 1  
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