/** Helper function to get a "good" string for a field, no matter what. */ String id3::v2::getStringAtIndex(const ID3_Frame* frame, ID3_FieldID fldName, size_t nIndex) { if (!frame) return ""; ID3_Field* fp = frame->GetField(fldName); if (!fp) return ""; return fp->GetText( nIndex, fp->GetEncoding() ); }
// Caller must delete [] dst after use. static void field_text(char** dst, ID3_Frame* frame) { iconv_t cd = iconv_open("UTF8","UTF16BE"); const int bufsize = 1000; char buf[bufsize]; if (NULL != frame) { ID3_Field* field = frame->GetField(ID3FN_TEXT); if (NULL != field) { switch(field->GetEncoding()){ case ID3TE_UTF16: case ID3TE_UTF16BE: // Convert to UTF-8 before returning the data to Go. if ( cd != (iconv_t) -1 ) { char* in = (char*) field->GetRawUnicodeText(); size_t insize = field->Size(); char* bufptr = buf; size_t bufbytes = bufsize; size_t rc = 0; // Initialize iconv state if( iconv(cd, NULL, NULL, &bufptr, &bufbytes) == (size_t) -1 ){ *dst = empty_string(); break; } if ( (rc = iconv(cd, &in, &insize, &bufptr, &bufbytes)) != (size_t) -1 ) { *bufptr = '\0'; *dst = new char[bufsize-bufbytes+1]; memcpy(*dst, buf, bufsize-bufbytes); (*dst)[bufsize-bufbytes] = '\0'; } else { *dst = empty_string(); } } else { *dst = empty_string(); } break; default: // Ascii *dst = new char[field->Size()+1]; (*dst)[0] = '\0'; size_t len = field->Get(*dst, field->Size()+1); (*dst)[field->Size()] = '\0'; } } } iconv_close(cd); }
char *ID3_GetString(const ID3_Frame *frame, ID3_FieldID fldName) { char *text = NULL; // if (NULL != frame) ID3_Field* fld; if (NULL != frame && NULL != (fld = frame->GetField(fldName))) { // ID3_Field* fld = frame->GetField(fldName); ID3_TextEnc enc = fld->GetEncoding(); fld->SetEncoding(ID3TE_ISO8859_1); size_t nText = fld->Size(); text = new char[nText + 1]; fld->Get(text, nText + 1); fld->SetEncoding(enc); } return text; }
String id3::v2::getString(const ID3_Frame* frame, ID3_FieldID fldName) { if (!frame) { return ""; } ID3_Field* fp = frame->GetField(fldName); if (!fp) { return ""; } ID3_TextEnc enc = fp->GetEncoding(); fp->SetEncoding(ID3TE_ASCII); String text(fp->GetRawText(), fp->Size()); fp->SetEncoding(enc); return text; }
String id3::v2::getStringAtIndex(const ID3_Frame* frame, ID3_FieldID fldName, size_t nIndex) { if (!frame) { return ""; } String text; ID3_Field* fp = frame->GetField(fldName); if (fp && fp->GetNumTextItems() < nIndex) { ID3_TextEnc enc = fp->GetEncoding(); fp->SetEncoding(ID3TE_ASCII); text = fp->GetRawTextItem(nIndex); fp->SetEncoding(enc); } return text; }
MP3Data * MP3DataGenerator::readMetadata( const char * p_filePath) { // Set up file connection. myID3_Tag = new ID3_Tag(); myID3_Tag->Link( p_filePath); // Storage with default initialization. MP3Data * myMP3Data = new MP3Data(); myMP3Data->setAll( "<undefinded>"); // Retrieve metadata. ID3_Tag::Iterator * tagIter = myID3_Tag->CreateIterator(); ID3_Frame * myFrame = NULL; while( NULL != ( myFrame = tagIter->GetNext())) { if( NULL != myFrame) { ID3_Field * myField = myFrame->GetField( ID3FN_TEXT); // Check if current frame is part of the set aka a field of interest. std::set<ID3_FrameID>::iterator current = interestingID3_FrameIDs->find( myFrame->GetID()); if ( NULL != myField && current != interestingID3_FrameIDs->end()) { ID3_FrameID_LUT * myLUT = new ID3_FrameID_LUT(); std::string frameRealname = std::string( myLUT->getRealname( myFrame->GetID())); // @TODO Show encoding in gui (optional) if( myField->GetEncoding() == ID3TE_ASCII || ID3TE_ISO8859_1) { encoding += frameRealname + ": ISO8859_1 , "; // Genre needs to be converted. if( myFrame->GetID() == ID3FID_CONTENTTYPE) // ID3_FrameID = 32 { const char * genre = myField->GetRawText(); // ID3V1 genre has is an int surrounded by round brackets; ID3V2 genre is text. unsigned int genreIntegerAlias = removeBrackets( genre); if( genreIntegerAlias < ID3_NR_OF_V1_GENRES && genre[0] == '(') myMP3Data->setGenre( ID3_v1_genre_description[ genreIntegerAlias]); else myMP3Data->setGenre( genre); } // All other tags. else if( myFrame->GetID() == ID3FID_TITLE) myMP3Data->setTitle( myField->GetRawText()); else if( myFrame->GetID() == ID3FID_ALBUM) myMP3Data->setAlbum( myField->GetRawText()); else if( myFrame->GetID() == ID3FID_YEAR) myMP3Data->setYear( myField->GetRawText()); else if( myFrame->GetID() == ID3FID_TRACKNUM) myMP3Data->setTracknumber( myField->GetRawText()); else if( myFrame->GetID() == ID3FID_LEADARTIST) myMP3Data->setArtist( myField->GetRawText()); } else if( myField->GetEncoding() == ID3TE_UTF8) { encoding += frameRealname + ": utf8 , "; myMP3Data->setAll( "<unicode utf8 encoding>"); } else if( myField->GetEncoding() == ID3TE_UNICODE || ID3TE_UTF16) { encoding += frameRealname + ": utf16 ,"; // @TODO Conversion to std::string ... myField->GetRawUnicodeText(); myMP3Data->setAll( "<unicode utf16 encoding>"); } else if( myField->GetEncoding() == ID3TE_UTF16BE) { encoding += frameRealname + ": utf16be , "; myMP3Data->setAll( "<unicode utf16be encoding>"); } else if( myField->GetEncoding() == ID3TE_NUMENCODINGS) { encoding += frameRealname + ": numencodings , "; myMP3Data->setAll( "<numencoding>"); } else { encoding += frameRealname + ": unknown , "; myMP3Data->setAll( "<<unknown encoding>"); } } } } // Retrieve file path and file name. myMP3Data->setFilepath( p_filePath); myMP3Data->setFilename( getFilename( p_filePath).c_str()); return myMP3Data; }
void play_debug_meta(char* filename){ ID3_Tag tag(filename); ID3_Tag::Iterator* iter = tag.CreateIterator(); ID3_Frame* frame = NULL; char buf[1000]; // Iconv conversion descriptor: UTF-16 -> UTF-8 iconv_t cd = iconv_open("UTF8","UTF16BE"); while (NULL != (frame = iter->GetNext())) { char* val = NULL; field_text(&val, frame); printf("Frame: type %d %s %s:\n", frame->GetID(), frame->GetTextID(), frame->GetDescription()); ID3_Frame::Iterator* fieldIter = frame->CreateIterator(); ID3_Field* field = NULL; while (NULL != (field = fieldIter->GetNext())) { printf(" Field: type "); ID3_FieldType type = field->GetType(); switch(type) { case ID3FTY_NONE: printf("none"); break; case ID3FTY_INTEGER: printf("int"); break; case ID3FTY_BINARY: printf("binary"); break; case ID3FTY_TEXTSTRING: field->Get(buf, 1000); printf("text with %d items, encoding ", field->GetNumTextItems()); switch(field->GetEncoding()){ case ID3TE_UTF16: printf("UTF-16"); if ( cd != (iconv_t) -1 ) { char* in = (char*) field->GetRawUnicodeText(); size_t insize = field->Size(); char* bufptr = buf; size_t bufsize = 1000; size_t rc = 0; // Initialize iconv state if( iconv(cd, NULL, NULL, &bufptr, &bufsize) == (size_t) -1 ){ printf("iconv init Failed\n"); } if ( (rc = iconv(cd, &in, &insize, &bufptr, &bufsize)) != (size_t) -1 ) { *bufptr = '\0'; printf(": '%s' (%d chars)\n", buf, rc); } else { printf("<conversion using iconv failed>"); perror("iconv"); } } break; case ID3TE_UTF16BE: printf("UTF-16BE"); printf(": '%s'", buf); break; case ID3TE_UTF8: printf("UTF-8"); printf(": '%s'", buf); break; case ID3TE_NONE: printf("none"); printf(": '%s'", buf); break; case ID3TE_ISO8859_1: printf("ISO-8859-1/ASCII"); printf(": '%s'", buf); break; } break; } printf("\n"); } delete fieldIter; delete [] val; } delete iter; iconv_close(cd); }