Click here to Skip to main content
15,914,452 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Beginner with VC executing command line Pin
palbano10-Jul-04 21:36
palbano10-Jul-04 21:36 
GeneralRe: Beginner with VC executing command line Pin
wb11-Jul-04 0:19
wb11-Jul-04 0:19 
GeneralRe: Beginner with VC executing command line Pin
pork chop11-Jul-04 3:35
pork chop11-Jul-04 3:35 
GeneralRe: Beginner with VC executing command line Pin
wb11-Jul-04 3:57
wb11-Jul-04 3:57 
GeneralRe: Beginner with VC executing command line Pin
pork chop12-Jul-04 3:41
pork chop12-Jul-04 3:41 
GeneralRe: Beginner with VC executing command line Pin
pork chop14-Jul-04 18:45
pork chop14-Jul-04 18:45 
GeneralCPropertySheet question Pin
hongheo7610-Jul-04 19:12
hongheo7610-Jul-04 19:12 
Generalproblem reading id3v2 tags Pin
user31610-Jul-04 16:39
user31610-Jul-04 16:39 
Hello everyone.
I'm trying to properly read an id3v2 tag.
I've been trying for days now to figure it out but I cant seem to get it right.

here is what i have so far:

<br />
#include <iostream><br />
#include <string><br />
#include <fstream><br />
<br />
int main(int argc, char * argv[])<br />
{<br />
  if (argc != 2) {<br />
    cout<<"usage: id3v2 filename.mp3\n";<br />
    exit(0);<br />
  }<br />
<br />
  ifstream in;<br />
  in.open(argv[1]);<br />
<br />
  //read in the header.<br />
  char header[10];<br />
  in.read(header,10);<br />
  header[10] = '\0';<br />
<br />
  //make sure the tag exists.<br />
  if(header[0] == 'I' && header[1] == 'D' && header[2] == '3'){<br />
      //get header size.<br />
      int hSize = ((header[3] & 127) << 21) |<br />
                   ((header[4] & 127) << 14) |<br />
                   ((header[5] & 127) <<  7) |<br />
                   (header[6] & 127);<br />
<br />
      //get the version of the tag.<br />
      int versionMajor, versionMinor;<br />
      versionMajor = header[3];<br />
      versionMinor = header[4];<br />
<br />
      //get the flags.<br />
      bool unsynchronisation,compression,experimental,extended,footer;<br />
      unsynchronisation = false;<br />
      compression       = false;<br />
      experimental      = false;<br />
      extended          = false;<br />
      footer            = false;<br />
<br />
      //not sure if this is correct.<br />
      //Dont really completely understand the 0x80,0x40,etc..<br />
      switch(versionMajor){<br />
          case 2:<br />
               // %ab000000 in v2.2<br />
               unsynchronisation = (bool)(header[5] & 0x80);  // a - Unsynchronisation<br />
               compression       = (bool)(header[5] & 0x40);  // b - Compression<br />
               break;<br />
<br />
          case 3:<br />
               //%abc00000 in v2.3<br />
               unsynchronisation = (bool)(header[5] & 0x80); // a - Unsynchronisation<br />
               extended          = (bool)(header[5] & 0x40); // b - Extended header<br />
               experimental      = (bool)(header[5] & 0x20); // c - Experimental indicator<br />
               break;<br />
<br />
          case 4:<br />
               // %abcd0000 in v2.4<br />
               unsynchronisation = (bool)(header[5] & 0x80); // a - Unsynchronisation<br />
               extended          = (bool)(header[5] & 0x40); // b - Extended header<br />
               experimental      = (bool)(header[5] & 0x20); // c - Experimental indicator<br />
               footer            = (bool)(header[5] & 0x10); // d - Footer present<br />
               break;<br />
      }<br />
<br />
      //size of all of the frames.<br />
      int sizeOfFrames;<br />
      sizeOfFrames = hSize - 10; //- 10 to disclude the actual header.<br />
      /*  not sure if this should apply.<br />
      if(extended){<br />
           cout<<"extended\n";<br />
           char extHeader[4];<br />
           in.read(extHeader, 4);<br />
           int extSize;<br />
           extSize =  ((header[0] & 127) << 21) |<br />
                   ((header[1] & 127) << 14) |<br />
                   ((header[2] & 127) <<  7) |<br />
                   (header[3] & 127);<br />
          in.seekg(extSize);<br />
          sizeOfFrames -= extSize;<br />
      }<br />
      if(footer)<br />
          sizeOfFrames -= 10; //disclude the footer.<br />
      */<br />
<br />
      cout<<"sizeOfFrames="<<sizeOfFrames<<endl;<br />
      cout<<"\n\n";<br />
<br />
          while(sizeOfFrames > 0){<br />
              char frameId[4];<br />
              char frameData[6];<br />
              int frameSize;<br />
<br />
              in.read(frameId,4); //read the frame id 'TIT2',etc..<br />
              in.read(frameData,6); //read the rest of the frame header.<br />
<br />
              frameId[4]   = '\0'; //add null terminator to end of string.<br />
              frameData[6] = '\0'; //add null terminator to end of string.<br />
<br />
              //get the size of the frame.<br />
              frameSize =  ((frameData[0] & 127) << 21) |<br />
                        ((frameData[1] & 127) << 14) |<br />
                        ((frameData[2] & 127) <<  7) |<br />
                        (frameData[3] & 127);<br />
<br />
              if((frameSize > 0) && (frameSize < sizeOfFrames)){<br />
<br />
                    char tagInfo[frameSize];<br />
                    in.read(tagInfo,frameSize);<br />
                    tagInfo[frameSize] = '\0'; //add null terminator to end of string.<br />
<br />
                    cout<<"frameId="<<frameId<<"\n";<br />
                    cout<<"frameSize="<<frameSize<<"\n";<br />
                    cout<<"tagInfo=";<br />
                    for(int x=0;x<sizeof(tagInfo);x++)<br />
                            cout<<tagInfo[x]; //for some reason i cant just cout<<tagInfo<<"\n\n";<br />
                    cout<<"\n\n";<br />
<br />
              }<br />
              sizeOfFrames -= frameSize;<br />
          }<br />
          if(versionMajor == 2){<br />
                  ; //to do?<br />
          }<br />
      }<br />
  system("pause");<br />
  return 0;<br />
}<br />


Sometimes it will read a bunch of garbage in from the file and print out
unexpected results, and other times it reads the id3 tag just fine.
for example: i used winamp to modify the id3v2 tag of one of my mp3 files adding the year. Then i ran this code again and it produced the unexpected results. A different mp3 file already had the year in its id3v2 tag, and when i ran this code, the program read the id3 tag just fine.

I'm almost sure that the frames have to be read differently depending on which version the tag is. I'm not sure if thats the problem or how to go about doing that.

I found someone else's code from another website that i have been trying to go off of to figure this out.

you can find it here:
http://tebl.homelinux.com/projects.php
just scroll down and click on "ID3V2"
and then click on "id3v2.c"
(sorry i cant give a direct link)

I compiled and ran the code from this website, and it reads the id3 tags for mp3 files just fine!!
So this just adds to my frustration.. I have someone else's code in front of me that works, and i still cant figure this out writing it on my own.

one thing though, i want to use fstream to do this because im most comfortable with it, and i dont want to use any structures or classes.
Just trying to write it flat and easy until i can understand everything.

Could anyone help me with the problems in my code?
I cant seem to figure out what im doing wrong.
Thanks for your time.
GeneralRe: problem reading id3v2 tags Pin
palbano10-Jul-04 17:09
palbano10-Jul-04 17:09 
GeneralRe: problem reading id3v2 tags Pin
user31611-Jul-04 18:34
user31611-Jul-04 18:34 
GeneralOpenGL &amp; GDI+ Pin
mostafa_pasha10-Jul-04 10:50
mostafa_pasha10-Jul-04 10:50 
GeneralOpenGL &amp; GDI+ Pin
mostafa_pasha10-Jul-04 10:48
mostafa_pasha10-Jul-04 10:48 
GeneralSerial &amp; Parrallel Communication! Pin
mostafa_pasha10-Jul-04 10:38
mostafa_pasha10-Jul-04 10:38 
GeneralRe: Serial &amp; Parrallel Communication! Pin
palbano10-Jul-04 17:00
palbano10-Jul-04 17:00 
GeneralDebug Vs. Release Pin
Neelesh K J Jain10-Jul-04 10:30
Neelesh K J Jain10-Jul-04 10:30 
GeneralRe: Debug Vs. Release Pin
palbano10-Jul-04 10:34
palbano10-Jul-04 10:34 
GeneralRe: Debug Vs. Release Pin
Gary R. Wheeler10-Jul-04 13:57
Gary R. Wheeler10-Jul-04 13:57 
Questionhow to get #322776(Dialog) class 's handle Pin
GflPower10-Jul-04 8:51
GflPower10-Jul-04 8:51 
AnswerRe: how to get #322776(Dialog) class 's handle Pin
GflPower10-Jul-04 8:52
GflPower10-Jul-04 8:52 
GeneralRe: how to get #322776(Dialog) class 's handle Pin
PJ Arends10-Jul-04 9:19
professionalPJ Arends10-Jul-04 9:19 
GeneralRe: how to get #322776(Dialog) class 's handle Pin
Johan Rosengren10-Jul-04 9:21
Johan Rosengren10-Jul-04 9:21 
Generalwindow without a caption bar Pin
rezbipul10-Jul-04 8:39
rezbipul10-Jul-04 8:39 
GeneralRe: window without a caption bar Pin
PJ Arends10-Jul-04 9:13
professionalPJ Arends10-Jul-04 9:13 
GeneralRe: window without a caption bar Pin
ThatsAlok10-Jul-04 20:01
ThatsAlok10-Jul-04 20:01 
GeneralTab control clips its children Pin
Jörgen Sigvardsson10-Jul-04 7:18
Jörgen Sigvardsson10-Jul-04 7:18 

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.