refactored for ABI compat and write support of some tags of s3m/it/xm files

tags that can be written:
 * s3m: title
 * it: title
 * xm: title, trackerName
This commit is contained in:
Mathias Panzenböck
2011-06-14 00:46:23 +02:00
parent 5332fb5cf8
commit f6dbd32ed3
25 changed files with 1457 additions and 734 deletions

View File

@ -21,138 +21,175 @@
#include "tstringlist.h"
#include "xmfile.h"
#include "modfileprivate.h"
#include <algorithm>
namespace TagLib {
using namespace TagLib;
using namespace XM;
namespace XM {
class XM::File::FilePrivate
{
public:
FilePrivate(AudioProperties::ReadStyle propertiesStyle)
: tag(), properties(propertiesStyle)
{
}
File::File(FileName file, bool readProperties,
AudioProperties::ReadStyle propertiesStyle) :
Mod::File(file), m_tag(0), m_properties(0) {
read(readProperties, propertiesStyle);
XM::Tag tag;
XM::Properties properties;
};
XM::File::File(FileName file, bool readProperties,
AudioProperties::ReadStyle propertiesStyle) :
Mod::File(file),
d(new FilePrivate(propertiesStyle))
{
read(readProperties);
}
File::~File() {
delete m_tag;
delete m_properties;
XM::File::File(IOStream *stream, bool readProperties,
AudioProperties::ReadStyle propertiesStyle) :
Mod::File(stream),
d(new FilePrivate(propertiesStyle))
{
read(readProperties);
}
XM::Tag *File::tag() const {
return m_tag;
XM::File::~File()
{
delete d;
}
XM::Properties *File::audioProperties() const {
return m_properties;
XM::Tag *XM::File::tag() const
{
return &d->tag;
}
bool File::save() {
return false;
XM::Properties *XM::File::audioProperties() const
{
return &d->properties;
}
void File::read(bool, AudioProperties::ReadStyle propertiesStyle) {
delete m_tag;
delete m_properties;
bool XM::File::save()
{
seek(17);
writeString(d->tag.title(), 20);
seek(1, Current);
writeString(d->tag.trackerName(), 20);
// TODO: write comment as instrument and sample names
return true;
}
m_tag = new XM::Tag();
m_properties = new XM::Properties(propertiesStyle);
if (!isOpen())
void XM::File::read(bool)
{
if(!isOpen())
return;
try {
if (readBytes(17) != "Extended Module: ") {
throw Mod::ReadError();
}
READ_ASSERT(readBlock(17) == "Extended Module: ");
m_tag->setTitle(readString(20));
READ_STRING(d->tag.setTitle, 20);
READ_BYTE_AS(mark);
READ_ASSERT(mark == 0x1A);
READ_STRING(d->tag.setTrackerName, 20);
READ_U16L(d->properties.setVersion);
READ_U32L_AS(headerSize);
READ_U16L(d->properties.setSampleLength);
READ_U16L(d->properties.setRestartPosition);
READ_U16L(d->properties.setChannels);
READ_U16L_AS(patternCount);
d->properties.setPatternCount(patternCount);
READ_U16L_AS(instrumentCount);
d->properties.setInstrumentCount(instrumentCount);
READ_U16L(d->properties.setFlags);
READ_U16L(d->properties.setTempo);
READ_U16L(d->properties.setBpmSpeed);
if (readByte() != 0x1A) {
throw Mod::ReadError();
}
m_tag->setTrackerName(readString(20));
m_properties->setVersion(readU16L());
uint32_t headerSize = readU32L();
m_properties->setSampleLength(readU16L());
m_properties->setRestartPosition(readU16L());
m_properties->setChannels(readU16L());
uint32_t patternCount = readU16L();
m_properties->setPatternCount(patternCount);
uint32_t instrumentCount = readU16L();
m_properties->setInstrumentCount(instrumentCount);
m_properties->setFlags(readU16L());
m_properties->setTempo(readU16L());
m_properties->setBpmSpeed(readU16L());
seek(60 + headerSize);
seek(60 + headerSize);
for (uint16_t i = 0; i < patternCount; ++ i) {
uint32_t patternHeaderLength = readU32L();
uint8_t patternType = readByte();
uint16_t rowCount = readU16L();
uint16_t patternDataSize = readU16L();
for(ushort i = 0; i < patternCount; ++ i)
{
READ_U32L_AS(patternHeaderLength);
READ_BYTE_AS(patternType);
READ_U16L_AS(rowCount);
READ_U16L_AS(patternDataSize);
seek(patternHeaderLength - (4+1+2+2) + patternDataSize, Current);
}
seek(patternHeaderLength - (4+1+2+2) + patternDataSize, Current);
}
StringList intrumentNames;
StringList sampleNames;
for (uint16_t i = 0; i < instrumentCount; ++ i) {
long pos = tell();
uint32_t instrumentSize = readU32L();
StringList intrumentNames;
StringList sampleNames;
for(ushort i = 0; i < instrumentCount; ++ i)
{
long pos = tell();
READ_U32L_AS(instrumentSize);
String instrumentName;
uint8_t instrumentType = 0;
uint16_t sampleCount = 0;
String instrumentName;
uchar instrumentType = 0;
ushort sampleCount = 0;
if (instrumentSize > 4) {
instrumentName = readString(std::min((uint32_t)22,instrumentSize-4));
if(instrumentSize > 4)
{
if(!readString(instrumentName, std::min(22UL, instrumentSize-4)))
{
setValid(false);
return;
}
if (instrumentSize >= (4+22+1)) {
instrumentType = readByte();
if(instrumentSize >= (4+22+1))
{
if(!readByte(instrumentType))
{
setValid(false);
return;
}
if (instrumentSize >= (4+22+1+2)) {
sampleCount = readU16L();
if (instrumentSize >= (4+22+1+2))
{
if(!readU16L(sampleCount))
{
setValid(false);
return;
}
}
}
uint32_t sampleHeaderSize = 0;
uint32_t sumSampleLength = 0;
if (sampleCount > 0) {
sampleHeaderSize = readU32L();
seek(pos + instrumentSize);
long sampleheaderPos = tell();
for (uint16_t j = 0; j < sampleCount; ++ j) {
seek(sampleheaderPos + sampleHeaderSize * j);
uint32_t length = readU32L();
uint32_t loopStart = readU32L();
uint32_t loopLength = readU32L();
uint8_t volume = readByte();
uint8_t finetune = readByte();
uint8_t sampleType = readByte();
uint8_t panning = readByte();
uint8_t noteNumber = readByte();
uint8_t compression = readByte();
String sampleName = readString(22);
sumSampleLength += length;
sampleNames.append(sampleName);
}
}
intrumentNames.append(instrumentName);
seek(pos + instrumentSize + sampleHeaderSize * sampleCount + sumSampleLength);
}
m_tag->setComment(intrumentNames.toString("\n") + "\n" + sampleNames.toString("\n"));
}
catch (const Mod::ReadError&) {
setValid(false);
}
}
ulong sumSampleLength = 0;
ulong sampleHeaderSize = 0;
if (sampleCount > 0)
{
if(!readU32L(sampleHeaderSize))
{
setValid(false);
return;
}
seek(pos + instrumentSize);
long sampleheaderPos = tell();
for (ushort j = 0; j < sampleCount; ++ j)
{
seek(sampleheaderPos + sampleHeaderSize * j);
READ_U32L_AS(length);
READ_U32L_AS(loopStart);
READ_U32L_AS(loopLength);
READ_BYTE_AS(volume);
READ_BYTE_AS(finetune);
READ_BYTE_AS(sampleType);
READ_BYTE_AS(panning);
READ_BYTE_AS(noteNumber);
READ_BYTE_AS(compression);
READ_STRING_AS(sampleName, 22);
sumSampleLength += length;
sampleNames.append(sampleName);
}
}
intrumentNames.append(instrumentName);
seek(pos + instrumentSize + sampleHeaderSize * sampleCount + sumSampleLength);
}
d->tag.setComment(intrumentNames.toString("\n") + "\n" + sampleNames.toString("\n"));
}

View File

@ -42,22 +42,31 @@ namespace TagLib {
* is true the file's audio properties will also be read using
* \a propertiesStyle. If false, \a propertiesStyle is ignored.
*/
explicit File(FileName file, bool readProperties = true,
AudioProperties::ReadStyle propertiesStyle =
AudioProperties::Average);
File(FileName file, bool readProperties = true,
AudioProperties::ReadStyle propertiesStyle =
AudioProperties::Average);
/*!
* Contructs a Extended Module file from \a stream. If \a readProperties
* is true the file's audio properties will also be read using
* \a propertiesStyle. If false, \a propertiesStyle is ignored.
*/
File(IOStream *stream, bool readProperties = true,
AudioProperties::ReadStyle propertiesStyle =
AudioProperties::Average);
/*!
* Destroys this instance of the File.
*/
virtual ~File();
virtual XM::Tag *tag() const;
XM::Tag *tag() const;
/*!
* Returns the XM::Properties for this file. If no audio properties
* were read then this will return a null pointer.
*/
virtual XM::Properties *audioProperties() const;
XM::Properties *audioProperties() const;
/*!
* Save the file.
@ -65,16 +74,16 @@ namespace TagLib {
*
* \note Saving Extended Module tags is not supported.
*/
virtual bool save();
void read(bool readProperties, AudioProperties::ReadStyle propertiesStyle);
bool save();
private:
File(const File &);
File &operator=(const File &);
XM::Tag *m_tag;
XM::Properties *m_properties;
void read(bool readProperties);
class FilePrivate;
FilePrivate *d;
};
}
}

View File

@ -1,37 +0,0 @@
/***************************************************************************
copyright : (C) 2011 by Mathias Panzenböck
email : grosser.meister.morti@gmx.net
***************************************************************************/
/***************************************************************************
* 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 *
***************************************************************************/
#include "xmfiletyperesolver.h"
#include "xmfile.h"
TagLib::File *XMFileTypeResolver::createFile(
TagLib::FileName fileName,
bool readProperties,
TagLib::AudioProperties::ReadStyle propertiesStyle) const {
TagLib::XM::File *f = new TagLib::XM::File(fileName, readProperties, propertiesStyle);
if (f->isValid()) {
return f;
}
else {
delete f;
return 0;
}
}

168
taglib/xm/xmproperties.cpp Normal file
View File

@ -0,0 +1,168 @@
/***************************************************************************
copyright :(C) 2011 by Mathias Panzenböck
email : grosser.meister.morti@gmx.net
***************************************************************************/
/***************************************************************************
* 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 *
***************************************************************************/
#include "xmproperties.h"
using namespace TagLib;
using namespace XM;
class XM::Properties::PropertiesPrivate
{
public:
PropertiesPrivate() :
sampleLength(0),
channels(0),
version(0),
restartPosition(0),
patternCount(0),
instrumentCount(0),
flags(0),
tempo(0),
bpmSpeed(0)
{
}
ushort sampleLength;
int channels;
ushort version;
ushort restartPosition;
ushort patternCount;
ushort instrumentCount;
ushort flags;
ushort tempo;
ushort bpmSpeed;
};
XM::Properties::Properties(AudioProperties::ReadStyle propertiesStyle) :
AudioProperties(propertiesStyle),
d(new PropertiesPrivate)
{
}
XM::Properties::~Properties()
{
delete d;
}
int XM::Properties::length() const
{
return 0;
}
int XM::Properties::bitrate() const
{
return 0;
}
int XM::Properties::sampleRate() const
{
return 0;
}
int XM::Properties::channels() const
{
return d->channels;
}
ushort XM::Properties::sampleLength() const
{
return d->sampleLength;
}
ushort XM::Properties::version() const
{
return d->version;
}
ushort XM::Properties::restartPosition() const
{
return d->restartPosition;
}
ushort XM::Properties::patternCount() const
{
return d->patternCount;
}
ushort XM::Properties::instrumentCount() const
{
return d->instrumentCount;
}
ushort XM::Properties::flags() const
{
return d->flags;
}
ushort XM::Properties::tempo() const
{
return d->tempo;
}
ushort XM::Properties::bpmSpeed() const
{
return d->bpmSpeed;
}
void XM::Properties::setSampleLength(int sampleLength)
{
d->sampleLength = sampleLength;
}
void XM::Properties::setChannels(int channels)
{
d->channels = channels;
}
void XM::Properties::setVersion(ushort version)
{
d->version = version;
}
void XM::Properties::setRestartPosition(ushort restartPosition)
{
d->restartPosition = restartPosition;
}
void XM::Properties::setPatternCount(ushort patternCount)
{
d->patternCount = patternCount;
}
void XM::Properties::setInstrumentCount(ushort instrumentCount)
{
d->instrumentCount = instrumentCount;
}
void XM::Properties::setFlags(ushort flags)
{
d->flags = flags;
}
void XM::Properties::setTempo(ushort tempo)
{
d->tempo = tempo;
}
void XM::Properties::setBpmSpeed(ushort bpmSpeed)
{
d->bpmSpeed = bpmSpeed;
}

View File

@ -22,8 +22,7 @@
#ifndef TAGLIB_XMPROPERTIES_H
#define TAGLIB_XMPROPERTIES_H
#include <stdint.h>
#include "taglib.h"
#include "tstring.h"
#include "audioproperties.h"
@ -32,58 +31,41 @@ namespace TagLib {
class Properties : public AudioProperties {
friend class File;
public:
Properties(AudioProperties::ReadStyle propertiesStyle) :
AudioProperties(propertiesStyle),
m_sampleLength(0),
m_channels(0),
m_version(0),
m_restartPosition(0),
m_patternCount(0),
m_instrumentCount(0),
m_flags(0),
m_tempo(0),
m_bpmSpeed(0) {}
Properties(AudioProperties::ReadStyle propertiesStyle);
virtual ~Properties();
int length() const { return 0; }
int bitrate() const { return 0; }
int sampleRate() const { return 0; }
int channels() const { return m_channels; }
int length() const;
int bitrate() const;
int sampleRate() const;
int channels() const;
uint16_t sampleLength() const { return m_sampleLength; }
uint16_t version() const { return m_version; }
uint16_t restartPosition() const { return m_restartPosition; }
uint16_t patternCount() const { return m_patternCount; }
uint16_t instrumentCount() const { return m_instrumentCount; }
uint16_t flags() const { return m_flags; }
uint16_t tempo() const { return m_tempo; }
uint16_t bpmSpeed() const { return m_bpmSpeed; }
ushort sampleLength() const;
ushort version() const;
ushort restartPosition() const;
ushort patternCount() const;
ushort instrumentCount() const;
ushort flags() const;
ushort tempo() const;
ushort bpmSpeed() const;
protected:
void setSampleLength(int sampleLength) { m_sampleLength = sampleLength; }
void setChannels(int channels) { m_channels = channels; }
void setSampleLength(int sampleLength);
void setChannels(int channels);
void setVersion(uint16_t version) { m_version = version; }
void setRestartPosition(uint16_t restartPosition) {
m_restartPosition = restartPosition;
}
void setPatternCount(uint16_t patternCount) { m_patternCount = patternCount; }
void setInstrumentCount(uint16_t instrumentCount) {
m_instrumentCount = instrumentCount;
}
void setFlags (uint16_t flags) { m_flags = flags; }
void setTempo (uint16_t tempo) { m_tempo = tempo; }
void setBpmSpeed(uint16_t bpmSpeed) { m_bpmSpeed = bpmSpeed; }
void setVersion(ushort version);
void setRestartPosition(ushort restartPosition);
void setPatternCount(ushort patternCount);
void setInstrumentCount(ushort instrumentCount);
void setFlags(ushort flags);
void setTempo(ushort tempo);
void setBpmSpeed(ushort bpmSpeed);
private:
uint16_t m_sampleLength;
int m_channels;
uint16_t m_version;
uint16_t m_restartPosition;
uint16_t m_patternCount;
uint16_t m_instrumentCount;
uint16_t m_flags;
uint16_t m_tempo;
uint16_t m_bpmSpeed;
Properties(const Properties&);
Properties &operator=(const Properties&);
class PropertiesPrivate;
PropertiesPrivate *d;
};
}
}

View File

@ -1,6 +1,6 @@
/***************************************************************************
copyright : (C) 2011 by Mathias Panzenböck
email : grosser.meister.morti@gmx.net
copyright : (C) 2011 by Mathias Panzenböck
email : grosser.meister.morti@gmx.net
***************************************************************************/
/***************************************************************************
@ -19,17 +19,102 @@
* MA 02110-1301 USA *
***************************************************************************/
#ifndef TAGLIB_XMFILETYPERESOLVER_H
#define TAGLIB_XMFILETYPERESOLVER_H
#include "xmtag.h"
#include "fileref.h"
#include "taglib_export.h"
using namespace TagLib;
using namespace XM;
class TAGLIB_EXPORT XMFileTypeResolver : public TagLib::FileRef::FileTypeResolver {
TagLib::File *createFile(TagLib::FileName fileName,
bool readAudioProperties,
TagLib::AudioProperties::ReadStyle audioPropertiesStyle) const;
~XMFileTypeResolver() {}
class XM::Tag::TagPrivate
{
public:
TagPrivate() {}
String title;
String comment;
String trackerName;
};
#endif
XM::Tag::Tag() : TagLib::Tag()
{
d = new TagPrivate;
}
XM::Tag::~Tag()
{
delete d;
}
String XM::Tag::title() const
{
return d->title;
}
String XM::Tag::artist() const
{
return String::null;
}
String XM::Tag::album() const
{
return String::null;
}
String XM::Tag::comment() const
{
return d->comment;
}
String XM::Tag::genre() const
{
return String::null;
}
uint XM::Tag::year() const
{
return 0;
}
uint XM::Tag::track() const
{
return 0;
}
String XM::Tag::trackerName() const
{
return d->trackerName;
}
void XM::Tag::setTitle(const String &title)
{
d->title = title;
}
void XM::Tag::setArtist(const String &)
{
}
void XM::Tag::setAlbum(const String &)
{
}
void XM::Tag::setComment(const String &comment)
{
d->comment = comment;
}
void XM::Tag::setGenre(const String &)
{
}
void XM::Tag::setYear(uint)
{
}
void XM::Tag::setTrack(uint)
{
}
void XM::Tag::setTrackerName(const String &trackerName)
{
d->trackerName = trackerName;
}

View File

@ -26,16 +26,35 @@
namespace TagLib {
namespace XM {
class Tag : public Mod::Tag {
class Tag : public TagLib::Tag {
public:
String trackerName() const { return m_trackerName; }
Tag();
virtual ~Tag();
void setTrackerName(const String &trackerName) {
m_trackerName = trackerName;
}
String title() const;
String artist() const;
String album() const;
String comment() const;
String genre() const;
uint year() const;
uint track() const;
String trackerName() const;
void setTitle (const String &title);
void setArtist (const String &artist);
void setAlbum (const String &album);
void setComment(const String &comment);
void setGenre (const String &genre);
void setYear (uint year);
void setTrack(uint track);
void setTrackerName(const String &trackerName);
private:
String m_trackerName;
Tag(const Tag &);
Tag &operator=(const Tag &);
class TagPrivate;
TagPrivate *d;
};
}
}