This is starting to look more sane.

git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@588022 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
This commit is contained in:
Scott Wheeler
2006-09-24 17:12:40 +00:00
parent f9dbcabc8c
commit 60a3f993b5
121 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1 @@
INSTALL( FILES mpcfile.h mpcproperties.h DESTINATION ${INCLUDE_INSTALL_DIR}/taglib)

16
taglib/mpc/Makefile.am Normal file
View File

@ -0,0 +1,16 @@
INCLUDES = \
-I$(top_srcdir)/taglib \
-I$(top_srcdir)/taglib/toolkit \
-I$(top_srcdir)/taglib/ape \
-I$(top_srcdir)/taglib/mpeg/id3v1 \
-I$(top_srcdir)/taglib/mpeg/id3v2 \
$(all_includes)
noinst_LTLIBRARIES = libmpc.la
libmpc_la_SOURCES = mpcfile.cpp mpcproperties.cpp
taglib_include_HEADERS = mpcfile.h mpcproperties.h
taglib_includedir = $(includedir)/taglib
EXTRA_DIST = $(libmpc_la_SOURCES) $(taglib_include_HEADERS)

171
taglib/mpc/combinedtag.h Normal file
View File

@ -0,0 +1,171 @@
/***************************************************************************
copyright : (C) 2004 by Allan Sandfeld Jensen
email : kde@carewolf.org
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
***************************************************************************/
#ifndef DO_NOT_DOCUMENT // Tell Doxygen not to document this header
#ifndef TAGLIB_COMBINEDTAG_H
#define TAGLIB_COMBINEDTAG_H
////////////////////////////////////////////////////////////////////////////////
// Note that this header is not installed.
////////////////////////////////////////////////////////////////////////////////
#include <tag.h>
namespace TagLib {
/*!
* A union of two TagLib::Tags.
*/
class CombinedTag : public TagLib::Tag
{
public:
CombinedTag(Tag *tag1 = 0, Tag *tag2 = 0)
: TagLib::Tag(),
tag1(tag1), tag2(tag2) {}
virtual String title() const {
if(tag1 && !tag1->title().isEmpty())
return tag1->title();
if(tag2)
return tag2->title();
return String::null;
}
virtual String artist() const {
if(tag1 && !tag1->artist().isEmpty())
return tag1->artist();
if(tag2)
return tag2->artist();
return String::null;
}
virtual String album() const {
if(tag1 && !tag1->album().isEmpty())
return tag1->album();
if(tag2)
return tag2->album();
return String::null;
}
virtual String comment() const {
if(tag1 && !tag1->comment().isEmpty())
return tag1->comment();
if(tag2)
return tag2->comment();
return String::null;
}
virtual String genre() const {
if(tag1 && !tag1->genre().isEmpty())
return tag1->genre();
if(tag2)
return tag2->genre();
return String::null;
}
virtual uint year() const {
if(tag1 && tag1->year() > 0)
return tag1->year();
if(tag2)
return tag2->year();
return 0;
}
virtual uint track() const {
if(tag1 && tag1->track() > 0)
return tag1->track();
if(tag2)
return tag2->track();
return 0;
}
virtual void setTitle(const String &s) {
if(tag1)
tag1->setTitle(s);
if(tag2)
tag2->setTitle(s);
}
virtual void setArtist(const String &s) {
if(tag1)
tag1->setArtist(s);
if(tag2)
tag2->setArtist(s);
}
virtual void setAlbum(const String &s) {
if(tag1)
tag1->setAlbum(s);
if(tag2)
tag2->setAlbum(s);
}
virtual void setComment(const String &s) {
if(tag1)
tag1->setComment(s);
if(tag2)
tag2->setComment(s);
}
virtual void setGenre(const String &s) {
if(tag1)
tag1->setGenre(s);
if(tag2)
tag2->setGenre(s);
}
virtual void setYear(uint i) {
if(tag1)
tag1->setYear(i);
if(tag2)
tag2->setYear(i);
}
virtual void setTrack(uint i) {
if(tag1)
tag1->setTrack(i);
if(tag2)
tag2->setTrack(i);
}
private:
Tag *tag1;
Tag *tag2;
};
}
#endif
#endif

361
taglib/mpc/mpcfile.cpp Normal file
View File

@ -0,0 +1,361 @@
/***************************************************************************
copyright : (C) 2004 by Allan Sandfeld Jensen
email : kde@carewolf.org
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
***************************************************************************/
#include <tbytevector.h>
#include <tstring.h>
#include <tdebug.h>
#include "mpcfile.h"
#include "id3v1tag.h"
#include "id3v2header.h"
#include "apetag.h"
#include "apefooter.h"
#include "combinedtag.h"
using namespace TagLib;
class MPC::File::FilePrivate
{
public:
FilePrivate() :
APETag(0),
APELocation(-1),
APESize(0),
ID3v1Tag(0),
ID3v1Location(-1),
ID3v2Header(0),
ID3v2Location(-1),
ID3v2Size(0),
tag(0),
properties(0),
scanned(false),
hasAPE(false),
hasID3v1(false),
hasID3v2(false) {}
~FilePrivate()
{
if (tag != ID3v1Tag && tag != APETag) delete tag;
delete ID3v1Tag;
delete APETag;
delete ID3v2Header;
delete properties;
}
APE::Tag *APETag;
// long APEFooter;
long APELocation;
uint APESize;
ID3v1::Tag *ID3v1Tag;
long ID3v1Location;
ID3v2::Header *ID3v2Header;
long ID3v2Location;
uint ID3v2Size;
Tag *tag;
Properties *properties;
bool scanned;
// These indicate whether the file *on disk* has these tags, not if
// this data structure does. This is used in computing offsets.
bool hasAPE;
bool hasID3v1;
bool hasID3v2;
};
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////
MPC::File::File(const char *file, bool readProperties,
Properties::ReadStyle propertiesStyle) : TagLib::File(file)
{
d = new FilePrivate;
read(readProperties, propertiesStyle);
}
MPC::File::~File()
{
delete d;
}
TagLib::Tag *MPC::File::tag() const
{
return d->tag;
}
MPC::Properties *MPC::File::audioProperties() const
{
return d->properties;
}
bool MPC::File::save()
{
if(readOnly()) {
debug("MPC::File::save() -- File is read only.");
return false;
}
// Possibly strip ID3v2 tag
if(d->hasID3v2 && !d->ID3v2Header) {
removeBlock(d->ID3v2Location, d->ID3v2Size);
d->hasID3v2 = false;
if(d->hasID3v1)
d->ID3v1Location -= d->ID3v2Size;
if(d->hasAPE)
d->APELocation -= d->ID3v2Size;
}
// Update ID3v1 tag
if(d->ID3v1Tag) {
if(d->hasID3v1) {
seek(d->ID3v1Location);
writeBlock(d->ID3v1Tag->render());
}
else {
seek(0, End);
d->ID3v1Location = tell();
writeBlock(d->ID3v1Tag->render());
d->hasID3v1 = true;
}
} else
if(d->hasID3v1) {
removeBlock(d->ID3v1Location, 128);
d->hasID3v1 = false;
if(d->hasAPE) {
if(d->APELocation > d->ID3v1Location)
d->APELocation -= 128;
}
}
// Update APE tag
if(d->APETag) {
if(d->hasAPE)
insert(d->APETag->render(), d->APELocation, d->APESize);
else {
if(d->hasID3v1) {
insert(d->APETag->render(), d->ID3v1Location, 0);
d->APESize = d->APETag->footer()->completeTagSize();
d->hasAPE = true;
d->APELocation = d->ID3v1Location;
d->ID3v1Location += d->APESize;
}
else {
seek(0, End);
d->APELocation = tell();
writeBlock(d->APETag->render());
d->APESize = d->APETag->footer()->completeTagSize();
d->hasAPE = true;
}
}
}
else
if(d->hasAPE) {
removeBlock(d->APELocation, d->APESize);
d->hasAPE = false;
if(d->hasID3v1) {
if (d->ID3v1Location > d->APELocation)
d->ID3v1Location -= d->APESize;
}
}
return true;
}
ID3v1::Tag *MPC::File::ID3v1Tag(bool create)
{
if(!create || d->ID3v1Tag)
return d->ID3v1Tag;
// no ID3v1 tag exists and we've been asked to create one
d->ID3v1Tag = new ID3v1::Tag;
if(d->APETag)
d->tag = new CombinedTag(d->APETag, d->ID3v1Tag);
else
d->tag = d->ID3v1Tag;
return d->ID3v1Tag;
}
APE::Tag *MPC::File::APETag(bool create)
{
if(!create || d->APETag)
return d->APETag;
// no APE tag exists and we've been asked to create one
d->APETag = new APE::Tag;
if(d->ID3v1Tag)
d->tag = new CombinedTag(d->APETag, d->ID3v1Tag);
else
d->tag = d->APETag;
return d->APETag;
}
void MPC::File::remove(int tags)
{
if(tags & ID3v1) {
delete d->ID3v1Tag;
d->ID3v1Tag = 0;
if(d->APETag)
d->tag = d->APETag;
else
d->tag = d->APETag = new APE::Tag;
}
if(tags & ID3v2) {
delete d->ID3v2Header;
d->ID3v2Header = 0;
}
if(tags & APE) {
delete d->APETag;
d->APETag = 0;
if(d->ID3v1Tag)
d->tag = d->ID3v1Tag;
else
d->tag = d->APETag = new APE::Tag;
}
}
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////
void MPC::File::read(bool readProperties, Properties::ReadStyle /* propertiesStyle */)
{
// Look for an ID3v1 tag
d->ID3v1Location = findID3v1();
if(d->ID3v1Location >= 0) {
d->ID3v1Tag = new ID3v1::Tag(this, d->ID3v1Location);
d->hasID3v1 = true;
}
// Look for an APE tag
findAPE();
d->APELocation = findAPE();
if(d->APELocation >= 0) {
d->APETag = new APE::Tag(this, d->APELocation);
d->APESize = d->APETag->footer()->completeTagSize();
d->APELocation = d->APELocation + d->APETag->footer()->size() - d->APESize;
d->hasAPE = true;
}
if(d->hasID3v1 && d->hasAPE)
d->tag = new CombinedTag(d->APETag, d->ID3v1Tag);
else {
if(d->hasID3v1)
d->tag = d->ID3v1Tag;
else {
if(d->hasAPE)
d->tag = d->APETag;
else
d->tag = d->APETag = new APE::Tag;
}
}
// Look for and skip an ID3v2 tag
d->ID3v2Location = findID3v2();
if(d->ID3v2Location >= 0) {
seek(d->ID3v2Location);
d->ID3v2Header = new ID3v2::Header(readBlock(ID3v2::Header::size()));
d->ID3v2Size = d->ID3v2Header->completeTagSize();
d->hasID3v2 = true;
}
if(d->hasID3v2)
seek(d->ID3v2Location + d->ID3v2Size);
else
seek(0);
// Look for MPC metadata
if(readProperties) {
d->properties = new Properties(readBlock(MPC::HeaderSize),
length() - d->ID3v2Size - d->APESize);
}
}
long MPC::File::findAPE()
{
if(!isValid())
return -1;
if(d->hasID3v1)
seek(-160, End);
else
seek(-32, End);
long p = tell();
if(readBlock(8) == APE::Tag::fileIdentifier())
return p;
return -1;
}
long MPC::File::findID3v1()
{
if(!isValid())
return -1;
seek(-128, End);
long p = tell();
if(readBlock(3) == ID3v1::Tag::fileIdentifier())
return p;
return -1;
}
long MPC::File::findID3v2()
{
if(!isValid())
return -1;
seek(0);
if(readBlock(3) == ID3v2::Header::fileIdentifier())
return 0;
return -1;
}

162
taglib/mpc/mpcfile.h Normal file
View File

@ -0,0 +1,162 @@
/***************************************************************************
copyright : (C) 2004 by Allan Sandfeld Jensen
email : kde@carewolf.org
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
***************************************************************************/
#ifndef TAGLIB_MPCFILE_H
#define TAGLIB_MPCFILE_H
#include "tfile.h"
#include "mpcproperties.h"
namespace TagLib {
class Tag;
namespace ID3v1 { class Tag; }
namespace APE { class Tag; }
//! An implementation of MPC metadata
/*!
* This is implementation of MPC metadata.
*
* This supports ID3v1 and APE (v1 and v2) style comments as well as reading stream
* properties from the file. ID3v2 tags are invalid in MPC-files, but will be skipped
* and ignored.
*/
namespace MPC {
//! An implementation of TagLib::File with MPC specific methods
/*!
* This implements and provides an interface for MPC files to the
* TagLib::Tag and TagLib::AudioProperties interfaces by way of implementing
* the abstract TagLib::File API as well as providing some additional
* information specific to MPC files.
* The only invalid tag combination supported is an ID3v1 tag after an APE tag.
*/
class File : public TagLib::File
{
public:
/*!
* This set of flags is used for various operations and is suitable for
* being OR-ed together.
*/
enum TagTypes {
//! Empty set. Matches no tag types.
NoTags = 0x0000,
//! Matches ID3v1 tags.
ID3v1 = 0x0001,
//! Matches ID3v2 tags.
ID3v2 = 0x0002,
//! Matches APE tags.
APE = 0x0004,
//! Matches all tag types.
AllTags = 0xffff
};
/*!
* Contructs an MPC file from \a file. If \a readProperties is true the
* file's audio properties will also be read using \a propertiesStyle. If
* false, \a propertiesStyle is ignored.
*/
File(const char *file, bool readProperties = true,
Properties::ReadStyle propertiesStyle = Properties::Average);
/*!
* Destroys this instance of the File.
*/
virtual ~File();
/*!
* Returns the Tag for this file. This will be an APE tag, an ID3v1 tag
* or a combination of the two.
*/
virtual TagLib::Tag *tag() const;
/*!
* Returns the MPC::Properties for this file. If no audio properties
* were read then this will return a null pointer.
*/
virtual Properties *audioProperties() const;
/*!
* Saves the file.
*/
virtual bool save();
/*!
* Returns a pointer to the ID3v1 tag of the file.
*
* If \a create is false (the default) this will return a null pointer
* if there is no valid ID3v1 tag. If \a create is true it will create
* an ID3v1 tag if one does not exist. If there is already an APE tag, the
* new ID3v1 tag will be placed after it.
*
* \note The Tag <b>is still</b> owned by the APE::File and should not be
* deleted by the user. It will be deleted when the file (object) is
* destroyed.
*/
ID3v1::Tag *ID3v1Tag(bool create = false);
/*!
* Returns a pointer to the APE tag of the file.
*
* If \a create is false (the default) this will return a null pointer
* if there is no valid APE tag. If \a create is true it will create
* a APE tag if one does not exist. If there is already an ID3v1 tag, thes
* new APE tag will be placed before it.
*
* \note The Tag <b>is still</b> owned by the APE::File and should not be
* deleted by the user. It will be deleted when the file (object) is
* destroyed.
*/
APE::Tag *APETag(bool create = false);
/*!
* This will remove the tags that match the OR-ed together TagTypes from the
* file. By default it removes all tags.
*
* \note This will also invalidate pointers to the tags
* as their memory will be freed.
* \note In order to make the removal permanent save() still needs to be called
*/
void remove(int tags = AllTags);
private:
File(const File &);
File &operator=(const File &);
void read(bool readProperties, Properties::ReadStyle propertiesStyle);
void scan();
long findAPE();
long findID3v1();
long findID3v2();
class FilePrivate;
FilePrivate *d;
};
}
}
#endif

View File

@ -0,0 +1,140 @@
/***************************************************************************
copyright : (C) 2004 by Allan Sandfeld Jensen
email : kde@carewolf.org
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
***************************************************************************/
#include <tstring.h>
#include <tdebug.h>
#include <bitset>
#include "mpcproperties.h"
#include "mpcfile.h"
using namespace TagLib;
class MPC::Properties::PropertiesPrivate
{
public:
PropertiesPrivate(const ByteVector &d, long length, ReadStyle s) :
data(d),
streamLength(length),
style(s),
version(0),
length(0),
bitrate(0),
sampleRate(0),
channels(0) {}
ByteVector data;
long streamLength;
ReadStyle style;
int version;
int length;
int bitrate;
int sampleRate;
int channels;
};
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////
MPC::Properties::Properties(const ByteVector &data, long streamLength, ReadStyle style) : AudioProperties(style)
{
d = new PropertiesPrivate(data, streamLength, style);
read();
}
MPC::Properties::~Properties()
{
delete d;
}
int MPC::Properties::length() const
{
return d->length;
}
int MPC::Properties::bitrate() const
{
return d->bitrate;
}
int MPC::Properties::sampleRate() const
{
return d->sampleRate;
}
/*
int MPC::Properties::sampleWidth() const
{
return d->sampleWidth;
}
*/
int MPC::Properties::channels() const
{
return d->channels;
}
int MPC::Properties::mpcVersion() const
{
return d->version;
}
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////
static const unsigned short sftable [4] = { 44100, 48000, 37800, 32000 };
void MPC::Properties::read()
{
if(!d->data.startsWith("MP+"))
return;
d->version = d->data[3] & 15;
unsigned int frames;
if(d->version >= 7) {
frames = d->data.mid(4, 4).toUInt(false);
std::bitset<32> flags = d->data.mid(8, 4).toUInt(false);
d->sampleRate = sftable[flags[17] * 2 + flags[16]];
d->channels = 2;
}
else {
unsigned int headerData = d->data.mid(0, 4).toUInt(false);
d->bitrate = (headerData >> 23) & 0x01ff;
d->version = (headerData >> 11) & 0x03ff;
d->sampleRate = 44100;
d->channels = 2;
if(d->version >= 5)
frames = d->data.mid(4, 4).toUInt(false);
else
frames = d->data.mid(4, 2).toUInt(false);
}
unsigned int samples = frames * 1152 - 576;
d->length = d->sampleRate > 0 ? (samples + (d->sampleRate / 2)) / d->sampleRate : 0;
if(!d->bitrate)
d->bitrate = d->length > 0 ? ((d->streamLength * 8L) / d->length) / 1000 : 0;
}

View File

@ -0,0 +1,77 @@
/***************************************************************************
copyright : (C) 2004 by Allan Sandfeld Jensen
email : kde@carewolf.org
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
***************************************************************************/
#ifndef TAGLIB_MPCPROPERTIES_H
#define TAGLIB_MPCPROPERTIES_H
#include "audioproperties.h"
namespace TagLib {
namespace MPC {
class File;
static const uint HeaderSize = 8*7;
//! An implementation of audio property reading for MPC
/*!
* This reads the data from an MPC stream found in the AudioProperties
* API.
*/
class Properties : public AudioProperties
{
public:
/*!
* Create an instance of MPC::Properties with the data read from the
* ByteVector \a data.
*/
Properties(const ByteVector &data, long streamLength, ReadStyle style = Average);
/*!
* Destroys this MPC::Properties instance.
*/
virtual ~Properties();
// Reimplementations.
virtual int length() const;
virtual int bitrate() const;
virtual int sampleRate() const;
virtual int channels() const;
/*!
* Returns the version of the bitstream (SV4-SV7)
*/
int mpcVersion() const;
private:
void read();
class PropertiesPrivate;
PropertiesPrivate *d;
};
}
}
#endif