Audio file detection by content

This commit is contained in:
Jonas Kvinge
2018-05-10 15:29:28 +02:00
parent 7b2d1d95d3
commit 5392cc4109
232 changed files with 55355 additions and 133 deletions

View File

@@ -0,0 +1,310 @@
/***************************************************************************
copyright : (C) 2006 by Lukáš Lalinský
email : lalinsky@gmail.com
copyright : (C) 2004 by Allan Sandfeld Jensen
email : kde@carewolf.org
(original MPC implementation)
***************************************************************************/
/***************************************************************************
* 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., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#include <tbytevector.h>
#include <tstring.h>
#include <tdebug.h>
#include <tagunion.h>
#include <tstringlist.h>
#include <tpropertymap.h>
#include <tagutils.h>
#include "trueaudiofile.h"
#include "id3v1tag.h"
#include "id3v2tag.h"
#include "id3v2header.h"
using namespace TagLib;
namespace
{
enum { TrueAudioID3v2Index = 0, TrueAudioID3v1Index = 1 };
}
class TrueAudio::File::FilePrivate
{
public:
FilePrivate(const ID3v2::FrameFactory *frameFactory = ID3v2::FrameFactory::instance()) :
ID3v2FrameFactory(frameFactory),
ID3v2Location(-1),
ID3v2OriginalSize(0),
ID3v1Location(-1),
properties(0) {}
~FilePrivate()
{
delete properties;
}
const ID3v2::FrameFactory *ID3v2FrameFactory;
long ID3v2Location;
long ID3v2OriginalSize;
long ID3v1Location;
TagUnion tag;
Properties *properties;
};
////////////////////////////////////////////////////////////////////////////////
// static members
////////////////////////////////////////////////////////////////////////////////
bool TrueAudio::File::isSupported(IOStream *stream)
{
// A TrueAudio file has to start with "TTA". An ID3v2 tag may precede.
const ByteVector id = Utils::readHeader(stream, 3, true);
return (id == "TTA");
}
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////
TrueAudio::File::File(FileName file, bool readProperties, Properties::ReadStyle) :
TagLib::File(file),
d(new FilePrivate())
{
if(isOpen())
read(readProperties);
}
TrueAudio::File::File(FileName file, ID3v2::FrameFactory *frameFactory,
bool readProperties, Properties::ReadStyle) :
TagLib::File(file),
d(new FilePrivate(frameFactory))
{
if(isOpen())
read(readProperties);
}
TrueAudio::File::File(IOStream *stream, bool readProperties, Properties::ReadStyle) :
TagLib::File(stream),
d(new FilePrivate())
{
if(isOpen())
read(readProperties);
}
TrueAudio::File::File(IOStream *stream, ID3v2::FrameFactory *frameFactory,
bool readProperties, Properties::ReadStyle) :
TagLib::File(stream),
d(new FilePrivate(frameFactory))
{
if(isOpen())
read(readProperties);
}
TrueAudio::File::~File()
{
delete d;
}
TagLib::Tag *TrueAudio::File::tag() const
{
return &d->tag;
}
PropertyMap TrueAudio::File::properties() const
{
return d->tag.properties();
}
void TrueAudio::File::removeUnsupportedProperties(const StringList &unsupported)
{
d->tag.removeUnsupportedProperties(unsupported);
}
PropertyMap TrueAudio::File::setProperties(const PropertyMap &properties)
{
if(ID3v1Tag())
ID3v1Tag()->setProperties(properties);
return ID3v2Tag(true)->setProperties(properties);
}
TrueAudio::Properties *TrueAudio::File::audioProperties() const
{
return d->properties;
}
void TrueAudio::File::setID3v2FrameFactory(const ID3v2::FrameFactory *factory)
{
d->ID3v2FrameFactory = factory;
}
bool TrueAudio::File::save()
{
if(readOnly()) {
debug("TrueAudio::File::save() -- File is read only.");
return false;
}
// Update ID3v2 tag
if(ID3v2Tag() && !ID3v2Tag()->isEmpty()) {
// ID3v2 tag is not empty. Update the old one or create a new one.
if(d->ID3v2Location < 0)
d->ID3v2Location = 0;
const ByteVector data = ID3v2Tag()->render();
insert(data, d->ID3v2Location, d->ID3v2OriginalSize);
if(d->ID3v1Location >= 0)
d->ID3v1Location += (static_cast<long>(data.size()) - d->ID3v2OriginalSize);
d->ID3v2OriginalSize = data.size();
}
else {
// ID3v2 tag is empty. Remove the old one.
if(d->ID3v2Location >= 0) {
removeBlock(d->ID3v2Location, d->ID3v2OriginalSize);
if(d->ID3v1Location >= 0)
d->ID3v1Location -= d->ID3v2OriginalSize;
d->ID3v2Location = -1;
d->ID3v2OriginalSize = 0;
}
}
// Update ID3v1 tag
if(ID3v1Tag() && !ID3v1Tag()->isEmpty()) {
// ID3v1 tag is not empty. Update the old one or create a new one.
if(d->ID3v1Location >= 0) {
seek(d->ID3v1Location);
}
else {
seek(0, End);
d->ID3v1Location = tell();
}
writeBlock(ID3v1Tag()->render());
}
else {
// ID3v1 tag is empty. Remove the old one.
if(d->ID3v1Location >= 0) {
truncate(d->ID3v1Location);
d->ID3v1Location = -1;
}
}
return true;
}
ID3v1::Tag *TrueAudio::File::ID3v1Tag(bool create)
{
return d->tag.access<ID3v1::Tag>(TrueAudioID3v1Index, create);
}
ID3v2::Tag *TrueAudio::File::ID3v2Tag(bool create)
{
return d->tag.access<ID3v2::Tag>(TrueAudioID3v2Index, create);
}
void TrueAudio::File::strip(int tags)
{
if(tags & ID3v1)
d->tag.set(TrueAudioID3v1Index, 0);
if(tags & ID3v2)
d->tag.set(TrueAudioID3v2Index, 0);
if(!ID3v1Tag())
ID3v2Tag(true);
}
bool TrueAudio::File::hasID3v1Tag() const
{
return (d->ID3v1Location >= 0);
}
bool TrueAudio::File::hasID3v2Tag() const
{
return (d->ID3v2Location >= 0);
}
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////
void TrueAudio::File::read(bool readProperties)
{
// Look for an ID3v2 tag
d->ID3v2Location = Utils::findID3v2(this);
if(d->ID3v2Location >= 0) {
d->tag.set(TrueAudioID3v2Index, new ID3v2::Tag(this, d->ID3v2Location, d->ID3v2FrameFactory));
d->ID3v2OriginalSize = ID3v2Tag()->header()->completeTagSize();
}
// Look for an ID3v1 tag
d->ID3v1Location = Utils::findID3v1(this);
if(d->ID3v1Location >= 0)
d->tag.set(TrueAudioID3v1Index, new ID3v1::Tag(this, d->ID3v1Location));
if(d->ID3v1Location < 0)
ID3v2Tag(true);
// Look for TrueAudio metadata
if(readProperties) {
long streamLength;
if(d->ID3v1Location >= 0)
streamLength = d->ID3v1Location;
else
streamLength = length();
if(d->ID3v2Location >= 0) {
seek(d->ID3v2Location + d->ID3v2OriginalSize);
streamLength -= (d->ID3v2Location + d->ID3v2OriginalSize);
}
else {
seek(0);
}
d->properties = new Properties(readBlock(TrueAudio::HeaderSize), streamLength);
}
}

View File

@@ -0,0 +1,259 @@
/***************************************************************************
copyright : (C) 2006 by Lukáš Lalinský
email : lalinsky@gmail.com
copyright : (C) 2004 by Allan Sandfeld Jensen
email : kde@carewolf.org
(original MPC implementation)
***************************************************************************/
/***************************************************************************
* 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., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifndef TAGLIB_TRUEAUDIOFILE_H
#define TAGLIB_TRUEAUDIOFILE_H
#include "tfile.h"
#include "trueaudioproperties.h"
namespace TagLib {
class Tag;
namespace ID3v2 { class Tag; class FrameFactory; }
namespace ID3v1 { class Tag; }
//! An implementation of TrueAudio metadata
/*!
* This is implementation of TrueAudio metadata.
*
* This supports ID3v1 and ID3v2 tags as well as reading stream
* properties from the file.
*/
namespace TrueAudio {
//! An implementation of TagLib::File with TrueAudio specific methods
/*!
* This implements and provides an interface for TrueAudio 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 TrueAudio files.
*/
class TAGLIB_EXPORT 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 all tag types.
AllTags = 0xffff
};
/*!
* Constructs a TrueAudio file from \a file. If \a readProperties is true
* the file's audio properties will also be read.
*
* \note In the current implementation, \a propertiesStyle is ignored.
*/
File(FileName file, bool readProperties = true,
Properties::ReadStyle propertiesStyle = Properties::Average);
/*!
* Constructs a TrueAudio file from \a file. If \a readProperties is true
* the file's audio properties will also be read.
*
* If this file contains and ID3v2 tag the frames will be created using
* \a frameFactory.
*
* \note In the current implementation, \a propertiesStyle is ignored.
*/
File(FileName file, ID3v2::FrameFactory *frameFactory,
bool readProperties = true,
Properties::ReadStyle propertiesStyle = Properties::Average);
/*!
* Constructs a TrueAudio file from \a stream. If \a readProperties is true
* the file's audio properties will also be read.
*
* \note TagLib will *not* take ownership of the stream, the caller is
* responsible for deleting it after the File object.
*
* \note In the current implementation, \a propertiesStyle is ignored.
*/
File(IOStream *stream, bool readProperties = true,
Properties::ReadStyle propertiesStyle = Properties::Average);
/*!
* Constructs a TrueAudio file from \a stream. If \a readProperties is true
* the file's audio properties will also be read.
*
* \note TagLib will *not* take ownership of the stream, the caller is
* responsible for deleting it after the File object.
*
* If this file contains and ID3v2 tag the frames will be created using
* \a frameFactory.
*
* \note In the current implementation, \a propertiesStyle is ignored.
*/
File(IOStream *stream, ID3v2::FrameFactory *frameFactory,
bool readProperties = true,
Properties::ReadStyle propertiesStyle = Properties::Average);
/*!
* Destroys this instance of the File.
*/
virtual ~File();
/*!
* Returns the Tag for this file.
*/
virtual TagLib::Tag *tag() const;
/*!
* Implements the unified property interface -- export function.
* If the file contains both ID3v1 and v2 tags, only ID3v2 will be
* converted to the PropertyMap.
*/
PropertyMap properties() const;
/*!
* Implements the unified property interface -- import function.
* Creates in ID3v2 tag if necessary. If an ID3v1 tag exists, it will
* be updated as well, within the limitations of ID3v1.
*/
PropertyMap setProperties(const PropertyMap &);
void removeUnsupportedProperties(const StringList &properties);
/*!
* Returns the TrueAudio::Properties for this file. If no audio properties
* were read then this will return a null pointer.
*/
virtual Properties *audioProperties() const;
/*!
* Set the ID3v2::FrameFactory to something other than the default.
*
* \see ID3v2FrameFactory
* \deprecated This value should be passed in via the constructor
*/
void setID3v2FrameFactory(const ID3v2::FrameFactory *factory);
/*!
* Saves the file.
*/
virtual bool save();
/*!
* Returns a pointer to the ID3v1 tag of the file.
*
* If \a create is false (the default) this may 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 and returns a valid pointer.
*
* \note This may return a valid pointer regardless of whether or not the
* file on disk has an ID3v1 tag. Use hasID3v1Tag() to check if the file
* on disk actually has an ID3v1 tag.
*
* \note The Tag <b>is still</b> owned by the MPEG::File and should not be
* deleted by the user. It will be deleted when the file (object) is
* destroyed.
*
* \see hasID3v1Tag()
*/
ID3v1::Tag *ID3v1Tag(bool create = false);
/*!
* Returns a pointer to the ID3v2 tag of the file.
*
* If \a create is false (the default) this may return a null pointer
* if there is no valid ID3v2 tag. If \a create is true it will create
* an ID3v2 tag if one does not exist and returns a valid pointer.
*
* \note This may return a valid pointer regardless of whether or not the
* file on disk has an ID3v2 tag. Use hasID3v2Tag() to check if the file
* on disk actually has an ID3v2 tag.
*
* \note The Tag <b>is still</b> owned by the MPEG::File and should not be
* deleted by the user. It will be deleted when the file (object) is
* destroyed.
*
* \see hasID3v2Tag()
*/
ID3v2::Tag *ID3v2Tag(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 strip(int tags = AllTags);
/*!
* Returns whether or not the file on disk actually has an ID3v1 tag.
*
* \see ID3v1Tag()
*/
bool hasID3v1Tag() const;
/*!
* Returns whether or not the file on disk actually has an ID3v2 tag.
*
* \see ID3v2Tag()
*/
bool hasID3v2Tag() const;
/*!
* Returns whether or not the given \a stream can be opened as a TrueAudio
* file.
*
* \note This method is designed to do a quick check. The result may
* not necessarily be correct.
*/
static bool isSupported(IOStream *stream);
private:
File(const File &);
File &operator=(const File &);
void read(bool readProperties);
class FilePrivate;
FilePrivate *d;
};
}
}
#endif

View File

@@ -0,0 +1,171 @@
/***************************************************************************
copyright : (C) 2006 by Lukáš Lalinský
email : lalinsky@gmail.com
copyright : (C) 2004 by Allan Sandfeld Jensen
email : kde@carewolf.org
(original MPC implementation)
***************************************************************************/
/***************************************************************************
* 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., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#include <tstring.h>
#include <tdebug.h>
#include <bitset>
#include "trueaudioproperties.h"
#include "trueaudiofile.h"
using namespace TagLib;
class TrueAudio::Properties::PropertiesPrivate
{
public:
PropertiesPrivate() :
version(0),
length(0),
bitrate(0),
sampleRate(0),
channels(0),
bitsPerSample(0),
sampleFrames(0) {}
int version;
int length;
int bitrate;
int sampleRate;
int channels;
int bitsPerSample;
unsigned int sampleFrames;
};
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////
TrueAudio::Properties::Properties(const ByteVector &data, long streamLength, ReadStyle style) :
AudioProperties(style),
d(new PropertiesPrivate())
{
read(data, streamLength);
}
TrueAudio::Properties::~Properties()
{
delete d;
}
int TrueAudio::Properties::length() const
{
return lengthInSeconds();
}
int TrueAudio::Properties::lengthInSeconds() const
{
return d->length / 1000;
}
int TrueAudio::Properties::lengthInMilliseconds() const
{
return d->length;
}
int TrueAudio::Properties::bitrate() const
{
return d->bitrate;
}
int TrueAudio::Properties::sampleRate() const
{
return d->sampleRate;
}
int TrueAudio::Properties::bitsPerSample() const
{
return d->bitsPerSample;
}
int TrueAudio::Properties::channels() const
{
return d->channels;
}
unsigned int TrueAudio::Properties::sampleFrames() const
{
return d->sampleFrames;
}
int TrueAudio::Properties::ttaVersion() const
{
return d->version;
}
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////
void TrueAudio::Properties::read(const ByteVector &data, long streamLength)
{
if(data.size() < 4) {
debug("TrueAudio::Properties::read() -- data is too short.");
return;
}
if(!data.startsWith("TTA")) {
debug("TrueAudio::Properties::read() -- invalid header signature.");
return;
}
unsigned int pos = 3;
d->version = data[pos] - '0';
pos += 1;
// According to http://en.true-audio.com/TTA_Lossless_Audio_Codec_-_Format_Description
// TTA2 headers are in development, and have a different format
if(1 == d->version) {
if(data.size() < 18) {
debug("TrueAudio::Properties::read() -- data is too short.");
return;
}
// Skip the audio format
pos += 2;
d->channels = data.toShort(pos, false);
pos += 2;
d->bitsPerSample = data.toShort(pos, false);
pos += 2;
d->sampleRate = data.toUInt(pos, false);
pos += 4;
d->sampleFrames = data.toUInt(pos, false);
pos += 4;
if(d->sampleFrames > 0 && d->sampleRate > 0) {
const double length = d->sampleFrames * 1000.0 / d->sampleRate;
d->length = static_cast<int>(length + 0.5);
d->bitrate = static_cast<int>(streamLength * 8.0 / length + 0.5);
}
}
}

View File

@@ -0,0 +1,133 @@
/***************************************************************************
copyright : (C) 2006 by Lukáš Lalinský
email : lalinsky@gmail.com
copyright : (C) 2004 by Allan Sandfeld Jensen
email : kde@carewolf.org
(original MPC implementation)
***************************************************************************/
/***************************************************************************
* 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., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifndef TAGLIB_TRUEAUDIOPROPERTIES_H
#define TAGLIB_TRUEAUDIOPROPERTIES_H
#include "audioproperties.h"
namespace TagLib {
namespace TrueAudio {
class File;
static const unsigned int HeaderSize = 18;
//! An implementation of audio property reading for TrueAudio
/*!
* This reads the data from an TrueAudio stream found in the AudioProperties
* API.
*/
class TAGLIB_EXPORT Properties : public AudioProperties
{
public:
/*!
* Create an instance of TrueAudio::Properties with the data read from the
* ByteVector \a data.
*/
Properties(const ByteVector &data, long streamLength, ReadStyle style = Average);
/*!
* Destroys this TrueAudio::Properties instance.
*/
virtual ~Properties();
/*!
* Returns the length of the file in seconds. The length is rounded down to
* the nearest whole second.
*
* \note This method is just an alias of lengthInSeconds().
*
* \deprecated
*/
virtual int length() const;
/*!
* Returns the length of the file in seconds. The length is rounded down to
* the nearest whole second.
*
* \see lengthInMilliseconds()
*/
// BIC: make virtual
int lengthInSeconds() const;
/*!
* Returns the length of the file in milliseconds.
*
* \see lengthInSeconds()
*/
// BIC: make virtual
int lengthInMilliseconds() const;
/*!
* Returns the average bit rate of the file in kb/s.
*/
virtual int bitrate() const;
/*!
* Returns the sample rate in Hz.
*/
virtual int sampleRate() const;
/*!
* Returns the number of audio channels.
*/
virtual int channels() const;
/*!
* Returns the number of bits per audio sample.
*/
int bitsPerSample() const;
/*!
* Returns the total number of sample frames
*/
unsigned int sampleFrames() const;
/*!
* Returns the major version number.
*/
int ttaVersion() const;
private:
Properties(const Properties &);
Properties &operator=(const Properties &);
void read(const ByteVector &data, long streamLength);
class PropertiesPrivate;
PropertiesPrivate *d;
};
}
}
#endif