strawberry-audio-player-win.../3rdparty/taglib/wavpack/wavpackfile.cpp

270 lines
6.9 KiB
C++
Raw Normal View History

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/ *
***************************************************************************/
2020-06-26 23:30:30 +02:00
#include "tbytevector.h"
#include "tstring.h"
#include "tdebug.h"
#include "tagunion.h"
#include "tpropertymap.h"
#include "tagutils.h"
2018-05-10 15:29:28 +02:00
#include "wavpackfile.h"
#include "id3v1tag.h"
#include "id3v2header.h"
#include "apetag.h"
#include "apefooter.h"
using namespace Strawberry_TagLib::TagLib;
2018-05-10 15:29:28 +02:00
2020-06-13 19:02:42 +02:00
namespace {
enum {
WavAPEIndex,
WavID3v1Index
};
2018-05-10 15:29:28 +02:00
}
2020-06-13 19:02:42 +02:00
class WavPack::File::FilePrivate {
public:
explicit FilePrivate() : APELocation(-1),
APESize(0),
ID3v1Location(-1),
properties(nullptr) {}
2020-06-13 19:02:42 +02:00
~FilePrivate() {
2018-05-10 15:29:28 +02:00
delete properties;
}
2020-06-26 23:30:30 +02:00
long long APELocation;
long long APESize;
2018-05-10 15:29:28 +02:00
2020-06-26 23:30:30 +02:00
long long ID3v1Location;
2018-05-10 15:29:28 +02:00
2020-06-26 23:30:30 +02:00
DoubleTagUnion tag;
2018-05-10 15:29:28 +02:00
AudioProperties *properties;
2018-05-10 15:29:28 +02:00
};
////////////////////////////////////////////////////////////////////////////////
// static members
////////////////////////////////////////////////////////////////////////////////
2020-06-13 19:02:42 +02:00
bool WavPack::File::isSupported(IOStream *stream) {
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");
2018-05-10 15:29:28 +02:00
}
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////
WavPack::File::File(FileName file, bool readProperties, AudioProperties::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);
2018-05-10 15:29:28 +02:00
}
WavPack::File::File(IOStream *stream, bool readProperties, AudioProperties::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);
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::setProperties(const PropertyMap &properties) {
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);
2018-05-10 15:29:28 +02:00
}
WavPack::AudioProperties *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-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;
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-13 19:02:42 +02:00
if (tags & ID3v1)
d->tag.set(WavID3v1Index, nullptr);
2018-05-10 15:29:28 +02:00
2020-06-13 19:02:42 +02:00
if (tags & APE)
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);
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) {
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
2020-06-26 23:30:30 +02:00
long long streamLength;
2018-05-10 15:29:28 +02:00
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 AudioProperties(this, streamLength);
2018-05-10 15:29:28 +02:00
}
2018-05-10 15:29:28 +02:00
}