Advertisement
nexfwall

[CopyPaste] MP3ID3.cpp

Jul 23rd, 2014
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. /*
  2. ** MP3ID3.cpp
  3. **
  4. ** Author: Yubo Dong (yubodong@gmail.com
  5. **
  6. ** Copyright (C) 2006 Yubo Dong
  7. **
  8. ** This is a wrapper class for libid3tag-0.15.1b.
  9. */
  10.  
  11. #include <io.h>
  12. #include <fcntl.h>
  13. #include <share.h>
  14. #include "MP3ID3.h"
  15.  
  16. CMP3ID3::CMP3ID3(void)
  17. {
  18.     m_pID3File =  NULL;
  19.     m_pID3Tag  =  NULL;
  20. }
  21.  
  22. CMP3ID3::CMP3ID3(string strFile, bool bReadOnly)
  23. {
  24.     m_pID3File =  NULL;
  25.     m_pID3Tag  =  NULL;
  26.     prepareMP3(strFile,bReadOnly);
  27. }
  28.  
  29. CMP3ID3::~CMP3ID3(void)
  30. {
  31.     if ( m_pID3File != NULL )
  32.         id3_file_close(m_pID3File);
  33. }
  34. /*
  35. ** bool prepareMP3(string strFile, bool bReadOnly)
  36. **
  37. ** Prepare a mp3 file to retreive it's tag information if it has
  38. **
  39. ** Parameter:
  40. ** strFile:     mp3 file with full pathname
  41. ** bReadOnly:   Indicate how you are going to open the file.
  42. */
  43. bool CMP3ID3::prepareMP3(string strFile, bool bReadOnly){
  44.     if ( m_pID3File != NULL )
  45.         id3_file_close(m_pID3File);
  46.  
  47.     int fileID = -1;
  48.     if ( bReadOnly )
  49.         fileID = sopen(strFile.c_str(),_O_RDONLY,_SH_DENYNO);
  50.     else
  51.         fileID = sopen(strFile.c_str(),_O_RDWR ,_SH_DENYWR);
  52.  
  53.     if ( fileID == -1 ) return false;
  54.  
  55.     if ( bReadOnly )
  56.         m_pID3File = id3_file_fdopen(fileID,ID3_FILE_MODE_READONLY);
  57.     else
  58.         m_pID3File = id3_file_fdopen(fileID,ID3_FILE_MODE_READWRITE);
  59.  
  60.     if ( m_pID3File == NULL ) return false;
  61.  
  62.     m_pID3Tag = id3_file_tag(m_pID3File);
  63.  
  64.     return true;
  65. }
  66. /*
  67. ** ustring getFrame(const char *frameID)
  68. **
  69. ** Return frame text by frameID. Frame ID is a four character string defined to uniquely
  70. ** identify a frame. Details see http://www.id3.org
  71. **
  72. */
  73. ustring CMP3ID3::getFrame(const char *frameID){
  74.     if ( m_pID3Tag == NULL || m_pID3File == NULL ) return ustring((unsigned char *)"");
  75.  
  76.     ustring str = ustring((unsigned char *)"");
  77.    
  78.     //Search for given frame by frame id
  79.     struct id3_frame *pFrame = id3_tag_findframe(m_pID3Tag,frameID,0);
  80.     if ( pFrame == NULL )  return ustring((unsigned char *)"");
  81.  
  82.     union id3_field field = pFrame->fields[1];
  83.     id3_ucs4_t const *pTemp = id3_field_getstrings(&field,0);
  84.     if ( !strcmp(frameID,"TCON") ){
  85.         //If the frameID is TCON, we then retreive genre name using id3_genre_name
  86.         id3_ucs4_t const *pGenre = id3_genre_name(pTemp);
  87.         pTemp = pGenre;
  88.     }
  89.  
  90.     id3_latin1_t *pStrLatinl;
  91.     if ( pTemp != NULL ){
  92.        pStrLatinl = id3_ucs4_latin1duplicate(pTemp);
  93.        str = pStrLatinl;
  94.        delete pStrLatinl;
  95.     }
  96.     return str;
  97. }
  98. ustring CMP3ID3::getTitle(){
  99.     return getFrame(ID3_FRAME_TITLE);
  100. }
  101. ustring CMP3ID3::getArtist(){
  102.     return getFrame(ID3_FRAME_ARTIST);
  103. }
  104. ustring CMP3ID3::getAlbum(){
  105.     return getFrame(ID3_FRAME_ALBUM);
  106. }
  107. ustring CMP3ID3::getYear(){
  108.     return getFrame(ID3_FRAME_YEAR);
  109. }
  110. ustring CMP3ID3::getTrack(){
  111.     return getFrame(ID3_FRAME_TRACK);
  112. }
  113. ustring CMP3ID3::getGenre(){
  114.     return getFrame(ID3_FRAME_GENRE);
  115. }
  116. ustring CMP3ID3::getComment(){
  117.     return getFrame(ID3_FRAME_COMMENT);
  118. }
  119. CMP3Info CMP3ID3::getMP3Info(){
  120.     CMP3Info mp3;
  121.     mp3.setTitle(getTitle());
  122.     mp3.setArtist(getArtist());
  123.     mp3.setAlbum(getAlbum());
  124.     mp3.setYear(getYear());
  125.     mp3.setTrack(getTrack());
  126.     mp3.setGenre(getGenre());
  127.     mp3.setComment(getComment());
  128.     return mp3;
  129. }
  130. CMP3Info CMP3ID3::getMP3Info(string strFile,bool bReadOnly){
  131.     prepareMP3(strFile,bReadOnly);
  132.     CMP3Info mp3 = getMP3Info();
  133.     mp3.setFile(ustring((unsigned char *)strFile.c_str()));
  134.     return mp3;
  135. }
  136.  
  137. /*
  138. ** long getTagLength(string strFile)
  139. **
  140. ** This is a static method so that you can easily get the length of id3 tag if there is
  141. ** an id3 tag residing in the beginning of the MP3 file. This is useful when you try
  142. ** to decode a mp3 file. You can simply skip the id3 tag to the real audio data. However
  143. ** some exception exists such as a mp3 file with XING header or Lame information
  144. */
  145. long CMP3ID3::getTagLength(string strFile){
  146.     int fileID = -1;
  147.     fileID = sopen(strFile.c_str(),_O_RDONLY,_SH_DENYNO);
  148.     if ( fileID != -1 ){
  149.         unsigned char buffer[ID3_TAG_QUERYSIZE];
  150.         read(fileID,(unsigned char *)buffer,ID3_TAG_QUERYSIZE);
  151.         long lRet = id3_tag_query(buffer, ID3_TAG_QUERYSIZE);
  152.         if ( lRet < 0 ) lRet = 0;
  153.         close(fileID);
  154.         return lRet;
  155.     }
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement