This commit was manufactured by cvs2svn to accommodate

a server-side copy/move.


git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@288617 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
This commit is contained in:
Scott Wheeler
2004-02-17 02:11:05 +00:00
commit 7fe6647435
97 changed files with 17567 additions and 0 deletions

16
flac/Makefile.am Normal file
View File

@ -0,0 +1,16 @@
INCLUDES = \
-I$(top_srcdir)/taglib \
-I$(top_srcdir)/taglib/toolkit \
-I$(top_srcdir)/taglib/ogg \
-I$(top_srcdir)/taglib/mpeg/id3v2 \
-I$(top_srcdir)/taglib/mpeg/id3v1 \
$(all_includes)
noinst_LTLIBRARIES = libflac.la
libflac_la_SOURCES = flacfile.cpp flacproperties.cpp
taglib_include_HEADERS = flacfile.h flacproperties.h
taglib_includedir = $(includedir)/taglib
EXTRA_DIST = $(libflac_la_SOURCES) $(taglib_include_HEADERS)

380
flac/flacfile.cpp Normal file
View File

@ -0,0 +1,380 @@
/***************************************************************************
copyright : (C) 2003 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 <id3v2header.h>
#include "flacfile.h"
#include "flactag.h"
using namespace TagLib;
class FLAC::File::FilePrivate
{
public:
FilePrivate() :
ID3v2FrameFactory(ID3v2::FrameFactory::instance()),
ID3v2Tag(0),
ID3v2Location(-1),
ID3v2OriginalSize(0),
ID3v1Tag(0),
ID3v1Location(-1),
comment(0),
properties(0),
flacStart(0),
streamStart(0),
streamLength(0),
scanned(false),
hasXiphComment(false),
hasID3v2(false),
hasID3v1(false) {}
~FilePrivate()
{
delete ID3v2Tag;
delete ID3v1Tag;
delete comment;
delete properties;
}
const ID3v2::FrameFactory *ID3v2FrameFactory;
ID3v2::Tag *ID3v2Tag;
long ID3v2Location;
uint ID3v2OriginalSize;
ID3v1::Tag *ID3v1Tag;
long ID3v1Location;
Ogg::XiphComment *comment;
FLAC::Tag *tag;
Properties *properties;
ByteVector streamInfoData;
ByteVector xiphCommentData;
long flacStart;
long streamStart;
long streamLength;
bool scanned;
bool hasXiphComment;
bool hasID3v2;
bool hasID3v1;
};
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////
FLAC::File::File(const char *file, bool readProperties,
Properties::ReadStyle propertiesStyle) : TagLib::File(file)
{
d = new FilePrivate;
read(readProperties, propertiesStyle);
}
FLAC::File::~File()
{
delete d;
}
TagLib::Tag *FLAC::File::tag() const
{
return d->tag;
}
FLAC::Properties *FLAC::File::audioProperties() const
{
return d->properties;
}
void FLAC::File::save()
{
// Create new vorbis comments
if(!d->comment) {
d->comment = new Ogg::XiphComment;
if(d->tag)
Tag::duplicate(d->tag, d->comment, true);
}
d->xiphCommentData = d->comment->render();
ByteVector v = ByteVector::fromUInt(d->xiphCommentData.size());
// Set the type of the comment to be a Xiph / Vorbis comment
// (See scan() for comments on header-format)
v[0] = 4;
v.append(d->xiphCommentData);
// If file already have comment => find and update it
// if not => insert one
// TODO: Search for padding and use that
if(d->hasXiphComment) {
long nextPageOffset = d->flacStart;
seek(nextPageOffset);
ByteVector header = readBlock(4);
uint length = header.mid(1, 3).toUInt();
nextPageOffset += length + 4;
// Search through the remaining metadata
char blocktype = header[0] & 0x7f;
bool lastblock = header[0] & 0x80;
while(!lastblock) {
seek(nextPageOffset);
header = readBlock(4);
blocktype = header[0] & 0x7f;
lastblock = header[0] & 0x80;
length = header.mid(1, 3).toUInt();
// Type is vorbiscomment
if( blocktype == 4 ) {
v[0] = header[0];
insert(v, nextPageOffset, length + 4);
break;
}
nextPageOffset += length + 4;
}
}
else {
long nextPageOffset = d->flacStart;
seek(nextPageOffset);
ByteVector header = readBlock(4);
// char blockType = header[0] & 0x7f;
bool lastBlock = header[0] & 0x80;
uint length = header.mid(1, 3).toUInt();
// If last block was last, make this one last
if(lastBlock) {
// Copy the bottom seven bits into the new value
ByteVector h(static_cast<char>(header[0] & 0x7F));
insert(h, nextPageOffset, 1);
// Set the last bit
v[0] |= 0x80;
}
insert(v, nextPageOffset + length + 4, 0);
d->hasXiphComment = true;
}
// Update ID3 tag
if(d->hasID3v2 && d->ID3v2Tag)
insert(d->ID3v2Tag->render(), d->ID3v2Location, d->ID3v2OriginalSize);
if(d->hasID3v1 && d->ID3v1Tag) {
seek(-128, End);
writeBlock(d->ID3v1Tag->render());
}
}
void FLAC::File::setID3v2FrameFactory(const ID3v2::FrameFactory *factory)
{
d->ID3v2FrameFactory = factory;
}
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////
void FLAC::File::read(bool readProperties, Properties::ReadStyle propertiesStyle)
{
// Look for an ID3v2 tag
d->ID3v2Location = findID3v2();
if(d->ID3v2Location >= 0) {
d->ID3v2Tag = new ID3v2::Tag(this, d->ID3v2Location, d->ID3v2FrameFactory);
d->ID3v2OriginalSize = d->ID3v2Tag->header()->completeTagSize();
if(d->ID3v2Tag->header()->tagSize() <= 0) {
delete d->ID3v2Tag;
d->ID3v2Tag = 0;
}
else
d->hasID3v2 = true;
}
// 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 FLAC metadata, including vorbis comments
scan();
if(d->hasXiphComment)
d->comment = new Ogg::XiphComment(xiphCommentData());
if(d->hasXiphComment || d->hasID3v2 || d->hasID3v1)
d->tag = new FLAC::Tag(d->comment, d->ID3v2Tag, d->ID3v1Tag);
else
d->tag = new FLAC::Tag(new Ogg::XiphComment);
if(readProperties)
d->properties = new Properties(this, propertiesStyle);
}
ByteVector FLAC::File::streamInfoData()
{
scan();
return d->streamInfoData;
}
ByteVector FLAC::File::xiphCommentData()
{
scan();
return d->xiphCommentData;
}
long FLAC::File::streamLength()
{
return d->streamLength;
}
void FLAC::File::scan()
{
// Scan the metadata pages
if(d->scanned)
return;
if(!isValid())
return;
// Optimization: Use ID3v2 size and only skip that
long nextPageOffset = find("fLaC");
if(nextPageOffset < 0) {
debug("FLAC::File::scan() -- FLAC stream not found");
return;
}
nextPageOffset += 4;
d->flacStart = nextPageOffset;
seek(nextPageOffset);
ByteVector header = readBlock(4);
// Header format (from spec):
// <1> Last-metadata-block flag
// <7> BLOCK_TYPE
// 0 : STREAMINFO
// 1 : PADDING
// ..
// 4 : VORBIS_COMMENT
// ..
// <24> Length of metadata to follow
char blockType = header[0] & 0x7f;
bool lastBlock = header[0] & 0x80;
uint length = header.mid(1, 3).toUInt();
// First block should be the stream_info metadata
if(blockType != 0) {
debug("FLAC::File::scan() -- invalid FLAC stream");
return;
}
d->streamInfoData = readBlock(length);
nextPageOffset += length + 4;
// Search through the remaining metadata
while(!lastBlock) {
header = readBlock(4);
blockType = header[0] & 0x7f;
lastBlock = header[0] & 0x80;
length = header.mid(1, 3).toUInt();
// Found the vorbis-comment
if(blockType == 4) {
d->xiphCommentData = readBlock(length);
d->hasXiphComment = true;
}
nextPageOffset += length + 4;
seek(nextPageOffset);
}
// End of metadata, now comes the datastream
d->streamStart = nextPageOffset;
d->streamLength = File::length() - d->streamStart;
d->scanned = true;
}
long FLAC::File::findID3v1()
{
if(!isValid())
return -1;
seek(-128, End);
long p = tell();
if(readBlock(3) == ID3v1::Tag::fileIdentifier())
return p;
return -1;
}
long FLAC::File::findID3v2()
{
if(!isValid())
return -1;
seek(0);
if(readBlock(3) == ID3v2::Header::fileIdentifier())
return 0;
return -1;
}

128
flac/flacfile.h Normal file
View File

@ -0,0 +1,128 @@
/***************************************************************************
copyright : (C) 2003 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_FLACFILE_H
#define TAGLIB_FLACFILE_H
#include <tfile.h>
#include "flacproperties.h"
namespace TagLib {
class Tag;
namespace ID3v2 { class FrameFactory; }
//! An implementation of FLAC metadata
/*!
* This is implementation of FLAC metadata for non-Ogg FLAC files. At some
* point when Ogg / FLAC is more common there will be a similar implementation
* under the Ogg hiearchy.
*
* This supports ID3v1, ID3v2 and Xiph style comments as well as reading stream
* properties from the file.
*/
namespace FLAC {
//! An implementation of TagLib::File with FLAC specific methods
/*!
* This implements and provides an interface for FLAC 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 FLAC files.
*/
class File : public TagLib::File
{
public:
/*!
* Contructs an FLAC 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 a union of XiphComment
* with ID3v1 and ID3v2 tags.
*/
virtual TagLib::Tag *tag() const;
/*!
* Returns the FLAC::Properties for this file. If no audio properties
* were read then this will return a null pointer.
*/
virtual Properties *audioProperties() const;
/*!
* Save the file. This will primarily save the XiphComment, but
* will also keep any old ID3-tags up to date. If the file
* has no XiphComment, one will be constructed from the ID3-tags.
*/
virtual void save();
/*!
* Set the ID3v2::FrameFactory to something other than the default. This
* can be used to specify the way that ID3v2 frames will be interpreted
* when
*
* \see ID3v2FrameFactory
*/
void setID3v2FrameFactory(const ID3v2::FrameFactory *factory);
/*!
* Returns the block of data used by FLAC::Properties for parsing the
* stream properties.
*/
ByteVector streamInfoData();
/*!
* Returns the length of the audio-stream, used by FLAC::Properties for
* calculating the bitrate.
*/
long streamLength();
private:
File(const File &);
File &operator=(const File &);
void read(bool readProperties, Properties::ReadStyle propertiesStyle);
void scan();
long findID3v2();
long findID3v1();
ByteVector xiphCommentData();
class FilePrivate;
FilePrivate *d;
};
}
}
#endif

138
flac/flacproperties.cpp Normal file
View File

@ -0,0 +1,138 @@
/***************************************************************************
copyright : (C) 2003 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 "flacproperties.h"
#include "flacfile.h"
using namespace TagLib;
class FLAC::Properties::PropertiesPrivate
{
public:
PropertiesPrivate(File *f, ReadStyle s) :
file(f),
style(s),
length(0),
bitrate(0),
sampleRate(0),
sampleWidth(0),
channels(0) {}
File *file;
ReadStyle style;
int length;
int bitrate;
int sampleRate;
int sampleWidth;
int channels;
};
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////
FLAC::Properties::Properties(File *file, ReadStyle style) : AudioProperties(style)
{
d = new PropertiesPrivate(file, style);
read();
}
FLAC::Properties::~Properties()
{
delete d;
}
int FLAC::Properties::length() const
{
return d->length;
}
int FLAC::Properties::bitrate() const
{
return d->bitrate;
}
int FLAC::Properties::sampleRate() const
{
return d->sampleRate;
}
int FLAC::Properties::sampleWidth() const
{
return d->sampleWidth;
}
int FLAC::Properties::channels() const
{
return d->channels;
}
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////
void FLAC::Properties::read()
{
// Get the identification header.
ByteVector data = d->file->streamInfoData();
int pos = 0;
// Minimum block size (in samples)
pos += 2;
// Maximum block size (in samples)
pos += 2;
// Minimum frame size (in bytes)
pos += 3;
// Maximum frame size (in bytes)
pos += 3;
uint flags = data.mid(pos, 4).toUInt(true);
d->sampleRate = flags >> 12;
d->channels = ((flags >> 9) & 7) + 1;
d->sampleWidth = ((flags >> 4) & 31) + 1;
// The last 4 bit are the most significant 4 bits for the 36bit
// streamlength in samples. (Audio files measured in days)
uint highlength = (((flags & 0xf) << 28) / d->sampleRate) << 4;
pos += 4;
d->length = (data.mid(pos, 4).toUInt(true)) / d->sampleRate + highlength;
pos += 4;
// Uncompressed bitrate:
// d->bitrate = ((d->sampleRate * d->channels) / 1000) * d->sampleWidth;
// Real bitrate:
d->bitrate = ((d->file->streamLength()*8L) / d->length)/1000;
}

76
flac/flacproperties.h Normal file
View File

@ -0,0 +1,76 @@
/***************************************************************************
copyright : (C) 2003 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_FLACPROPERTIES_H
#define TAGLIB_FLACPROPERTIES_H
#include <audioproperties.h>
namespace TagLib {
namespace FLAC {
class File;
//! An implementation of audio property reading for FLAC
/*!
* This reads the data from an FLAC stream found in the AudioProperties
* API.
*/
class Properties : public AudioProperties
{
public:
/*!
* Create an instance of FLAC::Properties with the data read from the
* FLAC::File \a file.
*/
Properties(File *file, ReadStyle style = Average);
/*!
* Destroys this FLAC::Properties instance.
*/
virtual ~Properties();
// Reimplementations.
virtual int length() const;
virtual int bitrate() const;
virtual int sampleRate() const;
virtual int channels() const;
/*!
* Returns the sample width as read from the FLAC identification
* header.
*/
int sampleWidth() const;
private:
void read();
class PropertiesPrivate;
PropertiesPrivate *d;
};
}
}
#endif

212
flac/flactag.h Normal file
View File

@ -0,0 +1,212 @@
/***************************************************************************
copyright : (C) 2003 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_FLACTAG_H
#define TAGLIB_FLACTAG_H
////////////////////////////////////////////////////////////////////////////////
// Note that this header is not installed.
////////////////////////////////////////////////////////////////////////////////
#include <xiphcomment.h>
#include <id3v2tag.h>
#include <id3v1tag.h>
namespace TagLib {
namespace FLAC {
/*!
* A union of Xiph, ID3v2 and ID3v1 tags.
*/
class Tag : public TagLib::Tag
{
public:
Tag(Ogg::XiphComment *xiph, ID3v2::Tag *id3v2 = 0, ID3v1::Tag *id3v1 = 0) :
TagLib::Tag(),
xiph(xiph), id3v2(id3v2), id3v1(id3v1) {}
virtual String title() const {
if(xiph && !xiph->title().isEmpty())
return xiph->title();
if(id3v2 && !id3v2->title().isEmpty())
return id3v2->title();
if(id3v1)
return id3v1->title();
return String::null;
}
virtual String artist() const {
if(xiph && !xiph->artist().isEmpty())
return xiph->artist();
if(id3v2 && !id3v2->artist().isEmpty())
return id3v2->artist();
if(id3v1)
return id3v1->artist();
return String::null;
}
virtual String album() const {
if(xiph && !xiph->album().isEmpty())
return xiph->album();
if(id3v2 && !id3v2->album().isEmpty())
return id3v2->album();
if(id3v1)
return id3v1->album();
return String::null;
}
virtual String comment() const {
if(xiph && !xiph->comment().isEmpty())
return xiph->comment();
if(id3v2 && !id3v2->comment().isEmpty())
return id3v2->comment();
if(id3v1)
return id3v1->comment();
return String::null;
}
virtual String genre() const {
if(xiph && !xiph->genre().isEmpty())
return xiph->genre();
if(id3v2 && !id3v2->genre().isEmpty())
return id3v2->genre();
if(id3v1)
return id3v1->genre();
return String::null;
}
virtual uint year() const {
if(xiph && xiph->year() > 0)
return xiph->year();
if(id3v2 && id3v2->year() > 0)
return id3v2->year();
if(id3v1)
return id3v1->year();
return 0;
}
virtual uint track() const {
if(xiph && xiph->track() > 0)
return xiph->track();
if(id3v2 && id3v2->track() > 0)
return id3v2->track();
if(id3v1)
return id3v1->track();
return 0;
}
virtual void setTitle(const String &s) {
if(xiph)
xiph->setTitle(s);
if(id3v2)
id3v2->setTitle(s);
if(id3v1)
id3v1->setTitle(s);
}
virtual void setArtist(const String &s) {
if(xiph)
xiph->setArtist(s);
if(id3v2)
id3v2->setArtist(s);
if(id3v1)
id3v1->setArtist(s);
}
virtual void setAlbum(const String &s) {
if(xiph)
xiph->setAlbum(s);
if(id3v2)
id3v2->setAlbum(s);
if(id3v1)
id3v1->setAlbum(s);
}
virtual void setComment(const String &s) {
if(xiph)
xiph->setComment(s);
if(id3v2)
id3v2->setComment(s);
if(id3v1)
id3v1->setComment(s);
}
virtual void setGenre(const String &s) {
if(xiph)
xiph->setGenre(s);
if(id3v2)
id3v2->setGenre(s);
if(id3v1)
id3v1->setGenre(s);
}
virtual void setYear(uint i) {
if(xiph)
xiph->setYear(i);
if(id3v2)
id3v2->setYear(i);
if(id3v1)
id3v1->setYear(i);
}
virtual void setTrack(uint i) {
if(xiph)
xiph->setTrack(i);
if(id3v2)
id3v2->setTrack(i);
if(id3v1)
id3v1->setTrack(i);
}
private:
Ogg::XiphComment *xiph;
ID3v2::Tag *id3v2;
ID3v1::Tag *id3v1;
};
}
}
#endif
#endif