2018-05-10 15:29:28 +02:00
|
|
|
/***************************************************************************
|
|
|
|
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 <tpropertymap.h>
|
|
|
|
#include <tagutils.h>
|
|
|
|
|
|
|
|
#include "wavpackfile.h"
|
|
|
|
#include "id3v1tag.h"
|
|
|
|
#include "id3v2header.h"
|
|
|
|
#include "apetag.h"
|
|
|
|
#include "apefooter.h"
|
|
|
|
|
2019-04-25 23:00:25 +02:00
|
|
|
using namespace Strawberry_TagLib::TagLib;
|
2018-05-10 15:29:28 +02:00
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
namespace {
|
2020-06-14 17:01:05 +02:00
|
|
|
enum {
|
|
|
|
WavAPEIndex,
|
|
|
|
WavID3v1Index
|
|
|
|
};
|
2018-05-10 15:29:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
class WavPack::File::FilePrivate {
|
|
|
|
public:
|
|
|
|
FilePrivate() : APELocation(-1),
|
|
|
|
APESize(0),
|
|
|
|
ID3v1Location(-1),
|
2020-06-14 17:01:05 +02:00
|
|
|
properties(nullptr) {}
|
2020-06-13 19:02:42 +02:00
|
|
|
|
|
|
|
~FilePrivate() {
|
2018-05-10 15:29:28 +02:00
|
|
|
delete properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
long APELocation;
|
|
|
|
long APESize;
|
|
|
|
|
|
|
|
long ID3v1Location;
|
|
|
|
|
|
|
|
TagUnion tag;
|
|
|
|
|
|
|
|
Properties *properties;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// static members
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
bool WavPack::File::isSupported(IOStream *stream) {
|
2020-06-14 17:01:05 +02:00
|
|
|
|
2018-05-10 15:29:28 +02:00
|
|
|
// A WavPack file has to start with "wvpk".
|
|
|
|
|
|
|
|
const ByteVector id = Utils::readHeader(stream, 4, false);
|
|
|
|
return (id == "wvpk");
|
2020-06-14 17:01:05 +02:00
|
|
|
|
2018-05-10 15:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// public members
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-06-14 17:01:05 +02:00
|
|
|
WavPack::File::File(FileName file, bool readProperties, Properties::ReadStyle) : Strawberry_TagLib::TagLib::File(file), d(new FilePrivate()) {
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (isOpen())
|
2018-05-10 15:29:28 +02:00
|
|
|
read(readProperties);
|
2020-06-14 17:01:05 +02:00
|
|
|
|
2018-05-10 15:29:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-14 17:01:05 +02:00
|
|
|
WavPack::File::File(IOStream *stream, bool readProperties, Properties::ReadStyle) : Strawberry_TagLib::TagLib::File(stream), d(new FilePrivate()) {
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (isOpen())
|
2018-05-10 15:29:28 +02:00
|
|
|
read(readProperties);
|
2020-06-14 17:01:05 +02:00
|
|
|
|
2018-05-10 15:29:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
WavPack::File::~File() {
|
2018-05-10 15:29:28 +02:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
Strawberry_TagLib::TagLib::Tag *WavPack::File::tag() const {
|
2018-05-10 15:29:28 +02:00
|
|
|
return &d->tag;
|
|
|
|
}
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
PropertyMap WavPack::File::properties() const {
|
2018-05-10 15:29:28 +02:00
|
|
|
return d->tag.properties();
|
|
|
|
}
|
|
|
|
|
2020-06-14 18:58:24 +02:00
|
|
|
void WavPack::File::removeUnsupportedProperties(const StringList &properties) {
|
|
|
|
d->tag.removeUnsupportedProperties(properties);
|
2018-05-10 15:29:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
PropertyMap WavPack::File::setProperties(const PropertyMap &properties) {
|
2020-06-14 17:01:05 +02:00
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (ID3v1Tag())
|
2018-05-10 15:29:28 +02:00
|
|
|
ID3v1Tag()->setProperties(properties);
|
|
|
|
|
|
|
|
return APETag(true)->setProperties(properties);
|
2020-06-14 17:01:05 +02:00
|
|
|
|
2018-05-10 15:29:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
WavPack::Properties *WavPack::File::audioProperties() const {
|
2018-05-10 15:29:28 +02:00
|
|
|
return d->properties;
|
|
|
|
}
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
bool WavPack::File::save() {
|
2020-06-14 17:01:05 +02:00
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (readOnly()) {
|
2018-05-10 15:29:28 +02:00
|
|
|
debug("WavPack::File::save() -- File is read only.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update ID3v1 tag
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (ID3v1Tag() && !ID3v1Tag()->isEmpty()) {
|
2018-05-10 15:29:28 +02:00
|
|
|
|
|
|
|
// ID3v1 tag is not empty. Update the old one or create a new one.
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (d->ID3v1Location >= 0) {
|
2018-05-10 15:29:28 +02:00
|
|
|
seek(d->ID3v1Location);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
seek(0, End);
|
|
|
|
d->ID3v1Location = tell();
|
|
|
|
}
|
|
|
|
|
|
|
|
writeBlock(ID3v1Tag()->render());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
// ID3v1 tag is empty. Remove the old one.
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (d->ID3v1Location >= 0) {
|
2018-05-10 15:29:28 +02:00
|
|
|
truncate(d->ID3v1Location);
|
|
|
|
d->ID3v1Location = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update APE tag
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (APETag() && !APETag()->isEmpty()) {
|
2018-05-10 15:29:28 +02:00
|
|
|
|
|
|
|
// APE tag is not empty. Update the old one or create a new one.
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (d->APELocation < 0) {
|
|
|
|
if (d->ID3v1Location >= 0)
|
2018-05-10 15:29:28 +02:00
|
|
|
d->APELocation = d->ID3v1Location;
|
|
|
|
else
|
|
|
|
d->APELocation = length();
|
|
|
|
}
|
|
|
|
|
|
|
|
const ByteVector data = APETag()->render();
|
|
|
|
insert(data, d->APELocation, d->APESize);
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (d->ID3v1Location >= 0)
|
2018-05-10 15:29:28 +02:00
|
|
|
d->ID3v1Location += (static_cast<long>(data.size()) - d->APESize);
|
|
|
|
|
|
|
|
d->APESize = data.size();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
// APE tag is empty. Remove the old one.
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (d->APELocation >= 0) {
|
2018-05-10 15:29:28 +02:00
|
|
|
removeBlock(d->APELocation, d->APESize);
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (d->ID3v1Location >= 0)
|
2018-05-10 15:29:28 +02:00
|
|
|
d->ID3v1Location -= d->APESize;
|
|
|
|
|
|
|
|
d->APELocation = -1;
|
|
|
|
d->APESize = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-06-14 17:01:05 +02:00
|
|
|
|
2018-05-10 15:29:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
ID3v1::Tag *WavPack::File::ID3v1Tag(bool create) {
|
2018-05-10 15:29:28 +02:00
|
|
|
return d->tag.access<ID3v1::Tag>(WavID3v1Index, create);
|
|
|
|
}
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
APE::Tag *WavPack::File::APETag(bool create) {
|
2018-05-10 15:29:28 +02:00
|
|
|
return d->tag.access<APE::Tag>(WavAPEIndex, create);
|
|
|
|
}
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
void WavPack::File::strip(int tags) {
|
2020-06-14 17:01:05 +02:00
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (tags & ID3v1)
|
2020-06-14 17:01:05 +02:00
|
|
|
d->tag.set(WavID3v1Index, nullptr);
|
2018-05-10 15:29:28 +02:00
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (tags & APE)
|
2020-06-14 17:01:05 +02:00
|
|
|
d->tag.set(WavAPEIndex, nullptr);
|
2018-05-10 15:29:28 +02:00
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (!ID3v1Tag())
|
2018-05-10 15:29:28 +02:00
|
|
|
APETag(true);
|
2020-06-14 17:01:05 +02:00
|
|
|
|
2018-05-10 15:29:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
bool WavPack::File::hasID3v1Tag() const {
|
2018-05-10 15:29:28 +02:00
|
|
|
return (d->ID3v1Location >= 0);
|
|
|
|
}
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
bool WavPack::File::hasAPETag() const {
|
2018-05-10 15:29:28 +02:00
|
|
|
return (d->APELocation >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// private members
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
void WavPack::File::read(bool readProperties) {
|
2020-06-14 17:01:05 +02:00
|
|
|
|
2018-05-10 15:29:28 +02:00
|
|
|
// Look for an ID3v1 tag
|
|
|
|
|
|
|
|
d->ID3v1Location = Utils::findID3v1(this);
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (d->ID3v1Location >= 0)
|
2018-05-10 15:29:28 +02:00
|
|
|
d->tag.set(WavID3v1Index, new ID3v1::Tag(this, d->ID3v1Location));
|
|
|
|
|
|
|
|
// Look for an APE tag
|
|
|
|
|
|
|
|
d->APELocation = Utils::findAPE(this, d->ID3v1Location);
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (d->APELocation >= 0) {
|
2018-05-10 15:29:28 +02:00
|
|
|
d->tag.set(WavAPEIndex, new APE::Tag(this, d->APELocation));
|
|
|
|
d->APESize = APETag()->footer()->completeTagSize();
|
|
|
|
d->APELocation = d->APELocation + APE::Footer::size() - d->APESize;
|
|
|
|
}
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (d->ID3v1Location >= 0)
|
2018-05-10 15:29:28 +02:00
|
|
|
APETag(true);
|
|
|
|
|
|
|
|
// Look for WavPack audio properties
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (readProperties) {
|
2018-05-10 15:29:28 +02:00
|
|
|
|
|
|
|
long streamLength;
|
|
|
|
|
2020-06-13 19:02:42 +02:00
|
|
|
if (d->APELocation >= 0)
|
2018-05-10 15:29:28 +02:00
|
|
|
streamLength = d->APELocation;
|
2020-06-13 19:02:42 +02:00
|
|
|
else if (d->ID3v1Location >= 0)
|
2018-05-10 15:29:28 +02:00
|
|
|
streamLength = d->ID3v1Location;
|
|
|
|
else
|
|
|
|
streamLength = length();
|
|
|
|
|
|
|
|
d->properties = new Properties(this, streamLength);
|
|
|
|
}
|
2020-06-14 17:01:05 +02:00
|
|
|
|
2018-05-10 15:29:28 +02:00
|
|
|
}
|