📄 id3v2.cpp
字号:
/*____________________________________________________________________________
FreeAmp - The Free MP3 Player
Portions Copyright (C) 1999 EMusic.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id: id3v2.cpp,v 1.32 2001/01/16 21:01:46 robert Exp $
____________________________________________________________________________*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#ifdef WIN32
#include <winsock.h>
#else
#include <sys/types.h>
#include <netinet/in.h>
#endif
#ifdef __QNX__
#include <strings.h>
#endif
#include "id3config.h"
#include "errors.h"
#include "utility.h"
#include "debug.h"
#include "id3.h"
#include "id3v2.h"
const int iDataFieldLen = 1024;
const int genreOther = 12;
const int genreMax = 126;
const char *genreList[] =
{
"Blues",
"Classic Rock",
"Country",
"Dance",
"Disco",
"Funk",
"Grunge",
"Hip-Hop",
"Jazz",
"Metal",
"New Age",
"Oldies",
"Other",
"Pop",
"R&B",
"Rap",
"Reggae",
"Rock",
"Techno",
"Industrial",
"Alternative",
"Ska",
"Death Metal",
"Pranks",
"Soundtrack",
"Euro-Techno",
"Ambient",
"Trip-Hop",
"Vocal",
"Jazz+Funk",
"Fusion",
"Trance",
"Classical",
"Instrumental",
"Acid",
"House",
"Game",
"Sound Clip",
"Gospel",
"Noise",
"AlternRock",
"Bass",
"Soul",
"Punk",
"Space",
"Meditative",
"Instrumental Pop",
"Instrumental Rock",
"Ethnic",
"Gothic",
"Darkwave",
"Techno-Industrial",
"Electronic",
"Pop-Folk",
"Eurodance",
"Dream",
"Southern Rock",
"Comedy",
"Cult",
"Gangsta",
"Top 40",
"Christian Rap",
"Pop/Funk",
"Jungle",
"Native American",
"Cabaret",
"New Wave",
"Psychadelic",
"Rave",
"Showtunes",
"Trailer",
"Lo-Fi",
"Tribal",
"Acid Punk",
"Acid Jazz",
"Polka",
"Retro",
"Musical",
"Rock & Roll",
"Hard Rock",
"Folk",
"Folk-Rock",
"National Folk",
"Swing",
"Fast Fusion",
"Bebob",
"Latin",
"Revival",
"Celtic",
"Bluegrass",
"Avantgarde",
"Gothic Rock",
"Progressive Rock",
"Psychedelic Rock",
"Symphonic Rock",
"Slow Rock",
"Big Band",
"Chorus",
"Easy Listening",
"Acoustic",
"Humour",
"Speech",
"Chanson",
"Opera",
"Chamber Music",
"Sonata",
"Symphony",
"Booty Bass",
"Primus",
"Porn Groove",
"Satire",
"Slow Jam",
"Club",
"Tango",
"Samba",
"Folklore",
"Ballad",
"Power Ballad",
"Rhythmic Soul",
"Freestyle",
"Duet",
"Punk Rock",
"Drum Solo",
"Acapella",
"Euro-House",
"Dance Hall",
"\0"
};
#ifndef COMPILING_SIGAPP
extern "C"
{
MetaDataFormat *Initialize(FAContext* context)
{
return new ID3v2(context);
}
}
#endif
ID3v2::ID3v2(FAContext* context):MetaDataFormat(context)
{
}
ID3v2::~ID3v2()
{
}
#ifdef HAVE_ID3V2
bool ID3v2::ReadMetaData(const char* url, MetaData* metadata)
{
ID3Tag *pTag;
ID3Frame *pFrame;
char *pData;
char *ptr;
ID3Field *pField;
assert(url);
assert(metadata);
ptr = strrchr(url, '.');
if (ptr == NULL)
return false;
if (strcasecmp(ptr, ".mp3"))
return false;
char path[_MAX_PATH];
uint32 length = sizeof(path);
URLToFilePath(url, path, &length);
pTag = ID3Tag_New();
#ifdef WIN32
int ret = ID3Tag_Link(pTag, path);
if (ret <= 0)
{
ID3Tag_Delete(pTag);
return false;
}
#else
ID3Tag_Link(pTag, path);
if (!ID3Tag_HasTagType(pTag, ID3TT_ID3V1) &&
!ID3Tag_HasTagType(pTag, ID3TT_ID3V2) &&
!ID3Tag_HasTagType(pTag, ID3TT_MUSICMATCH))
{
ID3Tag_Delete(pTag);
return false;
}
#endif
pData = new char[iDataFieldLen + 1];
pFrame = ID3Tag_FindFrameWithID(pTag, ID3FID_TITLE);
if (pFrame)
{
pData[0] = 0;
pField = ID3Frame_GetField(pFrame, ID3FN_TEXT);
ID3Field_GetASCII(pField, pData, iDataFieldLen);
if (strlen(pData) > 0)
metadata->SetTitle(pData);
}
pFrame = ID3Tag_FindFrameWithID(pTag, ID3FID_ALBUM);
if (pFrame)
{
pData[0] = 0;
pField = ID3Frame_GetField(pFrame, ID3FN_TEXT);
ID3Field_GetASCII(pField, pData, iDataFieldLen);
if (strlen(pData) > 0)
metadata->SetAlbum(pData);
}
// Pull artist out of LEADARTIST if it exists
pFrame = ID3Tag_FindFrameWithID(pTag, ID3FID_LEADARTIST);
if (pFrame)
{
pData[0] = 0;
pField = ID3Frame_GetField(pFrame, ID3FN_TEXT);
ID3Field_GetASCII(pField, pData, iDataFieldLen);
if (strlen(pData) > 0)
metadata->SetArtist(pData);
}
else // No?, Ok, try to pull artist out of BAND if it exists
pFrame = ID3Tag_FindFrameWithID(pTag, ID3FID_BAND);
if (pFrame)
{
pData[0] = 0;
pField = ID3Frame_GetField(pFrame, ID3FN_TEXT);
ID3Field_GetASCII(pField, pData, iDataFieldLen);
if (strlen(pData) > 0)
metadata->SetArtist(pData);
}
pFrame = ID3Tag_FindFrameWithID(pTag, ID3FID_COMMENT);
if (pFrame)
{
pData[0] = 0;
pField = ID3Frame_GetField(pFrame, ID3FN_TEXT);
ID3Field_GetASCII(pField, pData, iDataFieldLen);
if (strlen(pData) > 0)
metadata->SetComment(pData);
}
pFrame = ID3Tag_FindFrameWithID(pTag, ID3FID_SONGLEN);
if (pFrame)
{
pData[0] = 0;
pField = ID3Frame_GetField(pFrame, ID3FN_TEXT);
ID3Field_GetASCII(pField, pData, iDataFieldLen);
if (strlen(pData) > 0)
metadata->SetTime(atoi(pData) / 1000);
}
pFrame = ID3Tag_FindFrameWithID(pTag, ID3FID_YEAR);
if (pFrame)
{
pData[0] = 0;
pField = ID3Frame_GetField(pFrame, ID3FN_TEXT);
ID3Field_GetASCII(pField, pData, iDataFieldLen);
if (strlen(pData) > 0)
metadata->SetYear(atoi(pData));
}
pFrame = ID3Tag_FindFrameWithID(pTag, ID3FID_SIZE);
if (pFrame)
{
pData[0] = 0;
pField = ID3Frame_GetField(pFrame, ID3FN_TEXT);
ID3Field_GetASCII(pField, pData, iDataFieldLen);
if (strlen(pData) > 0)
metadata->SetSize(atoi(pData));
}
pFrame = ID3Tag_FindFrameWithID(pTag, ID3FID_TRACKNUM);
if (pFrame)
{
pData[0] = 0;
pField = ID3Frame_GetField(pFrame, ID3FN_TEXT);
ID3Field_GetASCII(pField, pData, iDataFieldLen);
if (strlen(pData) > 0)
metadata->SetTrack(atoi(pData));
}
pFrame = ID3Tag_FindFrameWithID(pTag, ID3FID_TRACKNUM);
if (pFrame)
{
pData[0] = 0;
pField = ID3Frame_GetField(pFrame, ID3FN_TEXT);
ID3Field_GetASCII(pField, pData, iDataFieldLen);
if (strlen(pData) > 0)
metadata->SetTrack(atoi(pData));
}
pFrame = ID3Tag_FindFrameWithID(pTag, ID3FID_CONTENTTYPE);
if (pFrame)
{
char genre[255];
int genreId, ret;
pData[0] = 0;
pField = ID3Frame_GetField(pFrame, ID3FN_TEXT);
ID3Field_GetASCII(pField, pData, iDataFieldLen);
if (strlen(pData) > 0)
{
genre[0] = 0;
ret = sscanf(pData, "(%d)%[^\n]", &genreId, genre);
if (ret > 0)
{
if (ret == 2)
metadata->SetGenre(genre);
else
if (genreId != genreOther)
{
LookupGenre(genreId, genre);
metadata->SetGenre(genre);
}
}
else
{
metadata->SetGenre(pData);
}
}
}
delete pData;
ID3Tag_Delete(pTag);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -