diff options
45 files changed, 4052 insertions, 6 deletions
@@ -9,6 +9,8 @@ CMakeFiles/ *.sln *.suo *.user +.* +*~ /CMakeCache.txt /Doxyfile /config.h @@ -8,6 +8,8 @@ Allan Sandfeld Jensen <kde@carewolf.org> FLAC metadata implementation Teemu Tervo <teemu.tervo@gmx.net> Numerous bug reports and fixes +Mathias Panzenböck <grosser.meister.morti@gmx.net> + Mod, S3M, IT and XM metadata implementations Please send all patches and questions to taglib-devel@kde.org rather than to individual developers! diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e05bad9..611e9ec1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,3 +84,11 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.cmake ${CMAKE_CURRENT_BINARY file(COPY doc/taglib.png DESTINATION doc) add_custom_target(docs doxygen) +# uninstall target +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" + IMMEDIATE @ONLY) + +add_custom_target(uninstall + COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake") diff --git a/cmake_uninstall.cmake.in b/cmake_uninstall.cmake.in new file mode 100644 index 00000000..72e030fb --- /dev/null +++ b/cmake_uninstall.cmake.in @@ -0,0 +1,21 @@ +if (NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt") + message(FATAL_ERROR "Cannot find install manifest: \"@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt\"") +endif() + +file(READ "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt" files) +string(REGEX REPLACE "\n" ";" files "${files}") +foreach (file ${files}) + message(STATUS "Uninstalling \"$ENV{DESTDIR}${file}\"") + if (EXISTS "$ENV{DESTDIR}${file}") + execute_process( + COMMAND @CMAKE_COMMAND@ -E remove "$ENV{DESTDIR}${file}" + OUTPUT_VARIABLE rm_out + RESULT_VARIABLE rm_retval + ) + if(NOT ${rm_retval} EQUAL 0) + message(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"") + endif () + else () + message(STATUS "File \"$ENV{DESTDIR}${file}\" does not exist.") + endif () +endforeach() diff --git a/taglib/CMakeLists.txt b/taglib/CMakeLists.txt index e3ebb5ab..c41c1ea6 100644 --- a/taglib/CMakeLists.txt +++ b/taglib/CMakeLists.txt @@ -19,6 +19,10 @@ include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/riff ${CMAKE_CURRENT_SOURCE_DIR}/riff/aiff ${CMAKE_CURRENT_SOURCE_DIR}/riff/wav + ${CMAKE_CURRENT_SOURCE_DIR}/mod + ${CMAKE_CURRENT_SOURCE_DIR}/s3m + ${CMAKE_CURRENT_SOURCE_DIR}/it + ${CMAKE_CURRENT_SOURCE_DIR}/xm ) if(ZLIB_FOUND) @@ -108,6 +112,16 @@ set(tag_HDRS mp4/mp4item.h mp4/mp4properties.h mp4/mp4coverart.h + mod/modfilebase.h + mod/modfile.h + mod/modtag.h + mod/modproperties.h + it/itfile.h + it/itproperties.h + s3m/s3mfile.h + s3m/s3mproperties.h + xm/xmfile.h + xm/xmproperties.h ) set(mpeg_SRCS @@ -229,6 +243,28 @@ set(wav_SRCS riff/wav/wavproperties.cpp ) +set(mod_SRCS + mod/modfilebase.cpp + mod/modfile.cpp + mod/modtag.cpp + mod/modproperties.cpp +) + +set(s3m_SRCS + s3m/s3mfile.cpp + s3m/s3mproperties.cpp +) + +set(it_SRCS + it/itfile.cpp + it/itproperties.cpp +) + +set(xm_SRCS + xm/xmfile.cpp + xm/xmproperties.cpp +) + set(toolkit_SRCS toolkit/tstring.cpp toolkit/tstringlist.cpp @@ -246,7 +282,7 @@ set(tag_LIB_SRCS ${mpeg_SRCS} ${id3v1_SRCS} ${id3v2_SRCS} ${frames_SRCS} ${ogg_SRCS} ${vorbis_SRCS} ${oggflacs_SRCS} ${mpc_SRCS} ${ape_SRCS} ${toolkit_SRCS} ${flacs_SRCS} ${wavpack_SRCS} ${speex_SRCS} ${trueaudio_SRCS} ${riff_SRCS} ${aiff_SRCS} ${wav_SRCS} - ${asf_SRCS} ${mp4_SRCS} + ${asf_SRCS} ${mp4_SRCS} ${mod_SRCS} ${s3m_SRCS} ${it_SRCS} ${xm_SRCS} tag.cpp tagunion.cpp fileref.cpp diff --git a/taglib/fileref.cpp b/taglib/fileref.cpp index 4cbb6413..7058df42 100644 --- a/taglib/fileref.cpp +++ b/taglib/fileref.cpp @@ -49,6 +49,10 @@ #include "aifffile.h" #include "wavfile.h" #include "apefile.h" +#include "modfile.h" +#include "s3mfile.h" +#include "itfile.h" +#include "xmfile.h" using namespace TagLib; @@ -158,6 +162,13 @@ StringList FileRef::defaultFileExtensions() l.append("aiff"); l.append("wav"); l.append("ape"); + l.append("mod"); + l.append("module"); // alias for "mod" + l.append("nst"); // alias for "mod" + l.append("wow"); // alias for "mod" + l.append("s3m"); + l.append("it"); + l.append("xm"); return l; } @@ -252,6 +263,15 @@ File *FileRef::create(FileName fileName, bool readAudioProperties, return new RIFF::WAV::File(fileName, readAudioProperties, audioPropertiesStyle); if(ext == "APE") return new APE::File(fileName, readAudioProperties, audioPropertiesStyle); + // module, nst and wow are possible but uncommon extensions + if(ext == "MOD" || ext == "MODULE" || ext == "NST" || ext == "WOW") + return new Mod::File(fileName, readAudioProperties, audioPropertiesStyle); + if(ext == "S3M") + return new S3M::File(fileName, readAudioProperties, audioPropertiesStyle); + if(ext == "IT") + return new IT::File(fileName, readAudioProperties, audioPropertiesStyle); + if(ext == "XM") + return new XM::File(fileName, readAudioProperties, audioPropertiesStyle); } return 0; diff --git a/taglib/it/itfile.cpp b/taglib/it/itfile.cpp new file mode 100644 index 00000000..9dde7e6e --- /dev/null +++ b/taglib/it/itfile.cpp @@ -0,0 +1,316 @@ +/*************************************************************************** + 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 "tstringlist.h" +#include "itfile.h" +#include "tdebug.h" +#include "modfileprivate.h" + +using namespace TagLib; +using namespace IT; + +class IT::File::FilePrivate +{ +public: + FilePrivate(AudioProperties::ReadStyle propertiesStyle) + : tag(), properties(propertiesStyle) + { + } + + Mod::Tag tag; + IT::Properties properties; +}; + +IT::File::File(FileName file, bool readProperties, + AudioProperties::ReadStyle propertiesStyle) : + Mod::FileBase(file), + d(new FilePrivate(propertiesStyle)) +{ + read(readProperties); +} + +IT::File::File(IOStream *stream, bool readProperties, + AudioProperties::ReadStyle propertiesStyle) : + Mod::FileBase(stream), + d(new FilePrivate(propertiesStyle)) +{ + read(readProperties); +} + +IT::File::~File() +{ + delete d; +} + +Mod::Tag *IT::File::tag() const +{ + return &d->tag; +} + +IT::Properties *IT::File::audioProperties() const +{ + return &d->properties; +} + +bool IT::File::save() +{ + if(readOnly()) + { + debug("IT::File::save() - Cannot save to a read only file."); + return false; + } + seek(4); + writeString(d->tag.title(), 25); + writeByte(0); + + seek(2, Current); + + ushort length = 0; + ushort instrumentCount = 0; + ushort sampleCount = 0; + + if(!readU16L(length) || !readU16L(instrumentCount) || !readU16L(sampleCount)) + return false; + + seek(15, Current); + + // write comment as instrument and sample names: + StringList lines = d->tag.comment().split("\n"); + for(ushort i = 0; i < instrumentCount; ++ i) { + seek(192L + length + ((long)i << 2)); + ulong instrumentOffset = 0; + if(!readU32L(instrumentOffset)) + return false; + + seek(instrumentOffset + 32); + + if(i < lines.size()) + writeString(lines[i], 25); + else + writeString(String::null, 25); + writeByte(0); + } + + for(ushort i = 0; i < sampleCount; ++ i) { + seek(192L + length + ((long)instrumentCount << 2) + ((long)i << 2)); + ulong sampleOffset = 0; + if(!readU32L(sampleOffset)) + return false; + + seek(sampleOffset + 20); + + if((i + instrumentCount) < lines.size()) + writeString(lines[i + instrumentCount], 25); + else + writeString(String::null, 25); + writeByte(0); + } + + // write rest as message: + StringList messageLines; + for(uint i = instrumentCount + sampleCount; i < lines.size(); ++ i) + messageLines.append(lines[i]); + ByteVector message = messageLines.toString("\r").data(String::Latin1); + + // it's actually not really stated if the message needs a + // terminating NUL but it does not hurt to add one: + if(message.size() > 7999) + message.resize(7999); + message.append((char)0); + + ushort special = 0; + ushort messageLength = 0; + ulong messageOffset = 0; + + seek(46); + if(!readU16L(special)) + return false; + + long fileSize = this->length(); + if(special & Properties::MessageAttached) { + seek(54); + if(!readU16L(messageLength) || !readU32L(messageOffset)) + return false; + + if(messageLength == 0) + messageOffset = fileSize; + } + else + { + messageOffset = fileSize; + seek(46); + writeU16L(special | 0x1); + } + + if((messageOffset + messageLength) >= fileSize) { + // append new message + seek(54); + writeU16L(message.size()); + writeU32L(messageOffset); + seek(messageOffset); + writeBlock(message); + truncate(messageOffset + message.size()); + } + else { + // Only overwrite existing message. + // I'd need to parse (understand!) the whole file for more. + // Although I could just move the message to the end of file + // and let the existing one be, but that would waste space. + message.resize(messageLength, 0); + seek(messageOffset); + writeBlock(message); + } + return true; +} + +void IT::File::read(bool) +{ + if(!isOpen()) + return; + + seek(0); + READ_ASSERT(readBlock(4) == "IMPM"); + READ_STRING(d->tag.setTitle, 26); + + seek(2, Current); + + READ_U16L_AS(length); + READ_U16L_AS(instrumentCount); + READ_U16L_AS(sampleCount); + + d->properties.setInstrumentCount(instrumentCount); + d->properties.setSampleCount(sampleCount); + READ_U16L(d->properties.setPatternCount); + READ_U16L(d->properties.setVersion); + READ_U16L(d->properties.setCompatibleVersion); + READ_U16L(d->properties.setFlags); + READ_U16L_AS(special); + d->properties.setSpecial(special); + READ_BYTE(d->properties.setGlobalVolume); + READ_BYTE(d->properties.setMixVolume); + READ_BYTE(d->properties.setBpmSpeed); + READ_BYTE(d->properties.setTempo); + READ_BYTE(d->properties.setPanningSeparation); + READ_BYTE(d->properties.setPitchWheelDepth); + + // IT supports some kind of comment tag. Still, the + // sample/instrument names are abused as comments so + // I just add all together. + String message; + if(special & Properties::MessageAttached) { + READ_U16L_AS(messageLength); + READ_U32L_AS(messageOffset); + seek(messageOffset); + ByteVector messageBytes = readBlock(messageLength); + READ_ASSERT(messageBytes.size() == messageLength); + int index = messageBytes.find((char) 0); + if(index > -1) + messageBytes.resize(index, 0); + messageBytes.replace('\r', '\n'); + message = messageBytes; + } + + seek(64); + + ByteVector pannings = readBlock(64); + ByteVector volumes = readBlock(64); + READ_ASSERT(pannings.size() == 64 && volumes.size() == 64); + int channels = 0; + for(int i = 0; i < 64; ++ i) { + // Strictly speaking an IT file has always 64 channels, but + // I don't count disabled and muted channels. + // But this always gives 64 channels for all my files anyway. + // Strangely VLC does report other values. I wonder how VLC + // gets it's values. + if(pannings[i] < 128 && volumes[i] > 0) ++ channels; + } + d->properties.setChannels(channels); + + // real length might be shorter because of skips and terminator + ushort realLength = 0; + for(ushort i = 0; i < length; ++ i) { + READ_BYTE_AS(order); + if(order == 255) break; + if(order != 254) ++ realLength; + } + d->properties.setLengthInPatterns(realLength); + + StringList comment; + // Note: I found files that have nil characters somewhere + // in the instrument/sample names and more characters + // afterwards. The spec does not mention such a case. + // Currently I just discard anything after a nil, but + // e.g. VLC seems to interprete a nil as a space. I + // don't know what is the proper behaviour. + for(ushort i = 0; i < instrumentCount; ++ i) { + seek(192L + length + ((long)i << 2)); + READ_U32L_AS(instrumentOffset); + seek(instrumentOffset); + + ByteVector instrumentMagic = readBlock(4); + READ_ASSERT(instrumentMagic == "IMPI"); + + READ_STRING_AS(dosFileName, 13); + + seek(15, Current); + + READ_STRING_AS(instrumentName, 26); + comment.append(instrumentName); + } + + for(ushort i = 0; i < sampleCount; ++ i) { + seek(192L + length + ((long)instrumentCount << 2) + ((long)i << 2)); + READ_U32L_AS(sampleOffset); + + seek(sampleOffset); + + ByteVector sampleMagic = readBlock(4); + READ_ASSERT(sampleMagic == "IMPS"); + + READ_STRING_AS(dosFileName, 13); + READ_BYTE_AS(globalVolume); + READ_BYTE_AS(sampleFlags); + READ_BYTE_AS(sampleVolume); + READ_STRING_AS(sampleName, 26); + /* + READ_BYTE_AS(sampleCvt); + READ_BYTE_AS(samplePanning); + READ_U32L_AS(sampleLength); + READ_U32L_AS(loopStart); + READ_U32L_AS(loopStop); + READ_U32L_AS(c5speed); + READ_U32L_AS(sustainLoopStart); + READ_U32L_AS(sustainLoopEnd); + READ_U32L_AS(sampleDataOffset); + READ_BYTE_AS(vibratoSpeed); + READ_BYTE_AS(vibratoDepth); + READ_BYTE_AS(vibratoRate); + READ_BYTE_AS(vibratoType); + */ + + comment.append(sampleName); + } + + if(message.size() > 0) + comment.append(message); + d->tag.setComment(comment.toString("\n")); + d->tag.setTrackerName("Impulse Tracker"); +} diff --git a/taglib/it/itfile.h b/taglib/it/itfile.h new file mode 100644 index 00000000..73256d32 --- /dev/null +++ b/taglib/it/itfile.h @@ -0,0 +1,89 @@ +/*************************************************************************** + 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 * + ***************************************************************************/ + +#ifndef TAGLIB_ITFILE_H +#define TAGLIB_ITFILE_H + +#include "tfile.h" +#include "audioproperties.h" +#include "taglib_export.h" +#include "modfilebase.h" +#include "modtag.h" +#include "itproperties.h" + +namespace TagLib { + + namespace IT { + + class TAGLIB_EXPORT File : public Mod::FileBase { + public: + /*! + * Contructs a Impulse Tracker 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(FileName file, bool readProperties = true, + AudioProperties::ReadStyle propertiesStyle = + AudioProperties::Average); + + /*! + * Contructs a Impulse Tracker 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 *stram, bool readProperties = true, + AudioProperties::ReadStyle propertiesStyle = + AudioProperties::Average); + + /*! + * Destroys this instance of the File. + */ + virtual ~File(); + + Mod::Tag *tag() const; + + /*! + * Returns the IT::Properties for this file. If no audio properties + * were read then this will return a null pointer. + */ + IT::Properties *audioProperties() const; + + /*! + * Save the file. + * This is the same as calling save(AllTags); + * + * \note Saving Impulse Tracker tags is not supported. + */ + bool save(); + + private: + File(const File &); + File &operator=(const File &); + + void read(bool readProperties); + + class FilePrivate; + FilePrivate *d; + }; + } +} + +#endif diff --git a/taglib/it/itproperties.cpp b/taglib/it/itproperties.cpp new file mode 100644 index 00000000..392ec604 --- /dev/null +++ b/taglib/it/itproperties.cpp @@ -0,0 +1,245 @@ +/*************************************************************************** + 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 "itproperties.h" + +using namespace TagLib; +using namespace IT; + +class IT::Properties::PropertiesPrivate +{ +public: + PropertiesPrivate() : + channels(0), + lengthInPatterns(0), + instrumentCount(0), + sampleCount(0), + patternCount(0), + version(0), + compatibleVersion(0), + flags(0), + special(0), + globalVolume(0), + mixVolume(0), + tempo(0), + bpmSpeed(0), + panningSeparation(0), + pitchWheelDepth(0) + { + } + + int channels; + ushort lengthInPatterns; + ushort instrumentCount; + ushort sampleCount; + ushort patternCount; + ushort version; + ushort compatibleVersion; + ushort flags; + ushort special; + uchar globalVolume; + uchar mixVolume; + uchar tempo; + uchar bpmSpeed; + uchar panningSeparation; + uchar pitchWheelDepth; +}; + +IT::Properties::Properties(AudioProperties::ReadStyle propertiesStyle) : + AudioProperties(propertiesStyle), + d(new PropertiesPrivate) +{ +} + +IT::Properties::~Properties() +{ + delete d; +} + +int IT::Properties::length() const +{ + return 0; +} + +int IT::Properties::bitrate() const +{ + return 0; +} + +int IT::Properties::sampleRate() const +{ + return 0; +} + +int IT::Properties::channels() const +{ + return d->channels; +} + +ushort IT::Properties::lengthInPatterns() const +{ + return d->lengthInPatterns; +} + +bool IT::Properties::stereo() const +{ + return d->flags & Stereo; +} + +ushort IT::Properties::instrumentCount() const +{ + return d->instrumentCount; +} + +ushort IT::Properties::sampleCount() const +{ + return d->sampleCount; +} + +ushort IT::Properties::patternCount() const +{ + return d->patternCount; +} + +ushort IT::Properties::version() const +{ + return d->version; +} + +ushort IT::Properties::compatibleVersion() const +{ + return d->compatibleVersion; +} + +ushort IT::Properties::flags() const +{ + return d->flags; +} + +ushort IT::Properties::special() const +{ + return d->special; +} + +uchar IT::Properties::globalVolume() const +{ + return d->globalVolume; +} + +uchar IT::Properties::mixVolume() const +{ + return d->mixVolume; +} + +uchar IT::Properties::tempo() const +{ + return d->tempo; +} + +uchar IT::Properties::bpmSpeed() const +{ + return d->bpmSpeed; +} + +uchar IT::Properties::panningSeparation() const +{ + return d->panningSeparation; +} + +uchar IT::Properties::pitchWheelDepth() const +{ + return d->pitchWheelDepth; +} + +void IT::Properties::setChannels(int channels) +{ + d->channels = channels; +} + +void IT::Properties::setLengthInPatterns(ushort lengthInPatterns) +{ + d->lengthInPatterns = lengthInPatterns; +} + +void IT::Properties::setInstrumentCount(ushort instrumentCount) +{ + d->instrumentCount = instrumentCount; +} + +void IT::Properties::setSampleCount(ushort sampleCount) +{ + d->sampleCount = sampleCount; +} + +void IT::Properties::setPatternCount(ushort patternCount) +{ + d->patternCount = patternCount; +} + +void IT::Properties::setFlags(ushort flags) +{ + d->flags = flags; +} + +void IT::Properties::setSpecial(ushort special) +{ + d->special = special; +} + +void IT::Properties::setCompatibleVersion(ushort compatibleVersion) +{ + d->compatibleVersion = compatibleVersion; +} + +void IT::Properties::setVersion(ushort version) +{ + d->version = version; +} + +void IT::Properties::setGlobalVolume(uchar globalVolume) +{ + d->globalVolume = globalVolume; +} + +void IT::Properties::setMixVolume(uchar mixVolume) +{ + d->mixVolume = mixVolume; +} + +void IT::Properties::setTempo(uchar tempo) +{ + d->tempo = tempo; +} + +void IT::Properties::setBpmSpeed(uchar bpmSpeed) +{ + d->bpmSpeed = bpmSpeed; +} + +void IT::Properties::setPanningSeparation(uchar panningSeparation) +{ + d->panningSeparation = panningSeparation; +} + +void IT::Properties::setPitchWheelDepth(uchar pitchWheelDepth) +{ + d->pitchWheelDepth = pitchWheelDepth; +} diff --git a/taglib/it/itproperties.h b/taglib/it/itproperties.h new file mode 100644 index 00000000..79ebc383 --- /dev/null +++ b/taglib/it/itproperties.h @@ -0,0 +1,103 @@ +/*************************************************************************** + 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 * + ***************************************************************************/ + +#ifndef TAGLIB_ITPROPERTIES_H +#define TAGLIB_ITPROPERTIES_H + +#include "taglib.h" +#include "audioproperties.h" + +namespace TagLib { + namespace IT { + class TAGLIB_EXPORT Properties : public AudioProperties { + friend class File; + public: + /*! Flag bits. */ + enum { + Stereo = 1, + Vol0MixOptimizations = 2, + UseInstruments = 4, + LinearSlides = 8, + OldEffects = 16, + LinkEffects = 32, + UseMidiPitchController = 64, + RequestEmbeddedMidiConf = 128 + }; + + /*! Special bits. */ + enum { + MessageAttached = 1, + MidiConfEmbedded = 8 + }; + + Properties(AudioProperties::ReadStyle propertiesStyle); + virtual ~Properties(); + + int length() const; + int bitrate() const; + int sampleRate() const; + int channels() const; + + ushort lengthInPatterns() const; + bool stereo() const; + ushort instrumentCount() const; + ushort sampleCount() const; + ushort patternCount() const; + ushort version() const; + ushort compatibleVersion() const; + ushort flags() const; + ushort special() const; + uchar globalVolume() const; + uchar mixVolume() const; + uchar tempo() const; + uchar bpmSpeed() const; + uchar panningSeparation() const; + uchar pitchWheelDepth() const; + + protected: + void setChannels(int channels); + + void setLengthInPatterns(ushort lengthInPatterns); + void setInstrumentCount(ushort instrumentCount); + void setSampleCount (ushort sampleCount); + void setPatternCount(ushort patternCount); + void setVersion (ushort version); + void setCompatibleVersion(ushort compatibleVersion); + void setFlags (ushort flags); + void setSpecial (ushort special); + void setGlobalVolume(uchar globalVolume); + void setMixVolume (uchar mixVolume); + void setTempo (uchar tempo); + void setBpmSpeed (uchar bpmSpeed); + void setPanningSeparation(uchar panningSeparation); + void setPitchWheelDepth (uchar pitchWheelDepth); + + private: + Properties(const Properties&); + Properties &operator=(const Properties&); + + class PropertiesPrivate; + PropertiesPrivate *d; + }; + } +} + +#endif diff --git a/taglib/mod/modfile.cpp b/taglib/mod/modfile.cpp new file mode 100644 index 00000000..f242ea51 --- /dev/null +++ b/taglib/mod/modfile.cpp @@ -0,0 +1,174 @@ +/*************************************************************************** + 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 "modfile.h" +#include "tstringlist.h" +#include "tdebug.h" +#include "modfileprivate.h" + +using namespace TagLib; +using namespace Mod; + +class Mod::File::FilePrivate +{ +public: + FilePrivate(AudioProperties::ReadStyle propertiesStyle) + : properties(propertiesStyle) + { + } + + Mod::Tag tag; + Mod::Properties properties; +}; + +Mod::File::File(FileName file, bool readProperties, + AudioProperties::ReadStyle propertiesStyle) : + Mod::FileBase(file), + d(new FilePrivate(propertiesStyle)) +{ + read(readProperties); +} + +Mod::File::File(IOStream *stream, bool readProperties, + AudioProperties::ReadStyle propertiesStyle) : + Mod::FileBase(stream), + d(new FilePrivate(propertiesStyle)) +{ + read(readProperties); +} + +Mod::File::~File() +{ + delete d; +} + +Mod::Tag *Mod::File::tag() const +{ + return &d->tag; +} + +Mod::Properties *Mod::File::audioProperties() const +{ + return &d->properties; +} + +bool Mod::File::save() +{ + if(readOnly()) { + debug("Mod::File::save() - Cannot save to a read only file."); + return false; + } + seek(0); + writeString(d->tag.title(), 20); + StringList lines = d->tag.comment().split("\n"); + uint n = std::min(lines.size(), d->properties.instrumentCount()); + for(uint i = 0; i < n; ++ i) { + writeString(lines[i], 22); + seek(8, Current); + } + + for(uint i = n; i < d->properties.instrumentCount(); ++ i) { + writeString(String::null, 22); + seek(8, Current); + } + return true; +} + +void Mod::File::read(bool) +{ + if(!isOpen()) + return; + + seek(1080); + ByteVector modId = readBlock(4); + READ_ASSERT(modId.size() == 4); + + int channels = 4; + uint instruments = 31; + if(modId == "M.K." || modId == "M!K!" || modId == "M&K!" || modId == "N.T.") { + d->tag.setTrackerName("ProTracker"); + channels = 4; + } + else if(modId.startsWith("FLT") || modId.startsWith("TDZ")) { + d->tag.setTrackerName("StarTrekker"); + char digit = modId[3]; + READ_ASSERT(digit >= '0' && digit <= '9'); + channels = digit - '0'; + } + else if(modId.endsWith("CHN")) { + d->tag.setTrackerName("StarTrekker"); + char digit = modId[0]; + READ_ASSERT(digit >= '0' && digit <= '9'); + channels = digit - '0'; + } + else if(modId == "CD81" || modId == "OKTA") { + d->tag.setTrackerName("Atari Oktalyzer"); + channels = 8; + } + else if(modId.endsWith("CH") || modId.endsWith("CN")) { + d->tag.setTrackerName("TakeTracker"); + char digit = modId[0]; + READ_ASSERT(digit >= '0' && digit <= '9'); + channels = (digit - '0') * 10; + digit = modId[1]; + READ_ASSERT(digit >= '0' && digit <= '9'); + channels += digit - '0'; + } + else { + // Not sure if this is correct. I'd need a file + // created with NoiseTracker to check this. + d->tag.setTrackerName("NoiseTracker"); // probably + channels = 4; + instruments = 15; + } + d->properties.setChannels(channels); + d->properties.setInstrumentCount(instruments); + + seek(0); + READ_STRING(d->tag.setTitle, 20); + + StringList comment; + for(uint i = 0; i < instruments; ++ i) { + READ_STRING_AS(instrumentName, 22); + // value in words, * 2 (<< 1) for bytes: + READ_U16B_AS(sampleLength); + + READ_BYTE_AS(fineTuneByte); + int fineTune = fineTuneByte & 0xF; + // > 7 means negative value + if(fineTune > 7) fineTune -= 16; + + READ_BYTE_AS(volume); + if(volume > 64) volume = 64; + // volume in decibels: 20 * log10(volume / 64) + + // value in words, * 2 (<< 1) for bytes: + READ_U16B_AS(repeatStart); + // value in words, * 2 (<< 1) for bytes: + READ_U16B_AS(repatLength); + + comment.append(instrumentName); + } + + READ_BYTE(d->properties.setLengthInPatterns); + + d->tag.setComment(comment.toString("\n")); +} diff --git a/taglib/mod/modfile.h b/taglib/mod/modfile.h new file mode 100644 index 00000000..6fd41b00 --- /dev/null +++ b/taglib/mod/modfile.h @@ -0,0 +1,89 @@ +/*************************************************************************** + 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 * + ***************************************************************************/ + +#ifndef TAGLIB_MODFILE_H +#define TAGLIB_MODFILE_H + +#include "tfile.h" +#include "audioproperties.h" +#include "taglib_export.h" +#include "modfilebase.h" +#include "modtag.h" +#include "modproperties.h" + +namespace TagLib { + + namespace Mod { + + class TAGLIB_EXPORT File : public TagLib::Mod::FileBase { + public: + /*! + * Contructs a Protracker 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(FileName file, bool readProperties = true, + AudioProperties::ReadStyle propertiesStyle = + AudioProperties::Average); + + /*! + * Contructs a Protracker 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(); + + Mod::Tag *tag() const; + + /*! + * Returns the Mod::Properties for this file. If no audio properties + * were read then this will return a null pointer. + */ + Mod::Properties *audioProperties() const; + + /*! + * Save the file. + * This is the same as calling save(AllTags); + * + * \note Saving Protracker tags is not supported. + */ + bool save(); + + private: + File(const File &); + File &operator=(const File &); + + void read(bool readProperties); + + class FilePrivate; + FilePrivate *d; + }; + } +} + +#endif diff --git a/taglib/mod/modfilebase.cpp b/taglib/mod/modfilebase.cpp new file mode 100644 index 00000000..e074dac8 --- /dev/null +++ b/taglib/mod/modfilebase.cpp @@ -0,0 +1,120 @@ +/*************************************************************************** + 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 "tdebug.h" +#include "modfilebase.h" + +using namespace TagLib; +using namespace Mod; + +Mod::FileBase::FileBase(FileName file) : TagLib::File(file) +{ +} + +Mod::FileBase::FileBase(IOStream *stream) : TagLib::File(stream) +{ +} + +void Mod::FileBase::writeString(const String &s, ulong size, char padding) +{ + ByteVector data(s.data(String::Latin1)); + data.resize(size, padding); + writeBlock(data); +} + +bool Mod::FileBase::readString(String &s, ulong size) +{ + ByteVector data(readBlock(size)); + if(data.size() < size) return false; + int index = data.find((char) 0); + if(index > -1) + { + data.resize(index); + } + data.replace((char) 0xff, ' '); + + s = data; + return true; +} + +void Mod::FileBase::writeByte(uchar byte) +{ + ByteVector data(1, byte); + writeBlock(data); +} + +void Mod::FileBase::writeU16L(ushort number) +{ + writeBlock(ByteVector::fromShort(number, false)); +} + +void Mod::FileBase::writeU32L(ulong number) +{ + writeBlock(ByteVector::fromUInt(number, false)); +} + +void Mod::FileBase::writeU16B(ushort number) +{ + writeBlock(ByteVector::fromShort(number, true)); +} + +void Mod::FileBase::writeU32B(ulong number) +{ + writeBlock(ByteVector::fromUInt(number, true)); +} + +bool Mod::FileBase::readByte(uchar &byte) +{ + ByteVector data(readBlock(1)); + if(data.size() < 1) return false; + byte = data[0]; + return true; +} + +bool Mod::FileBase::readU16L(ushort &number) +{ + ByteVector data(readBlock(2)); + if(data.size() < 2) return false; + number = data.toUShort(false); + return true; +} + +bool Mod::FileBase::readU32L(ulong &number) { + ByteVector data(readBlock(4)); + if(data.size() < 4) return false; + number = data.toUInt(false); + return true; +} + +bool Mod::FileBase::readU16B(ushort &number) +{ + ByteVector data(readBlock(2)); + if(data.size() < 2) return false; + number = data.toUShort(true); + return true; +} + +bool Mod::FileBase::readU32B(ulong &number) { + ByteVector data(readBlock(4)); + if(data.size() < 4) return false; + number = data.toUInt(true); + return true; +} diff --git a/taglib/mod/modfilebase.h b/taglib/mod/modfilebase.h new file mode 100644 index 00000000..d03af493 --- /dev/null +++ b/taglib/mod/modfilebase.h @@ -0,0 +1,58 @@ +/*************************************************************************** + 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 * + ***************************************************************************/ + +#ifndef TAGLIB_MODFILEBASE_H +#define TAGLIB_MODFILEBASE_H + +#include "taglib.h" +#include "tfile.h" +#include "tstring.h" +#include "tlist.h" +#include "taglib_export.h" + +#include <algorithm> + +namespace TagLib { + namespace Mod { + class TAGLIB_EXPORT FileBase : public TagLib::File + { + protected: + FileBase(FileName file); + FileBase(IOStream *stream); + + void writeString(const String &s, ulong size, char padding = 0); + void writeByte(uchar byte); + void writeU16L(ushort number); + void writeU32L(ulong number); + void writeU16B(ushort number); + void writeU32B(ulong number); + + bool readString(String &s, ulong size); + bool readByte(uchar &byte); + bool readU16L(ushort &number); + bool readU32L(ulong &number); + bool readU16B(ushort &number); + bool readU32B(ulong &number); + }; + } +} + +#endif diff --git a/taglib/mod/modfileprivate.h b/taglib/mod/modfileprivate.h new file mode 100644 index 00000000..19a30019 --- /dev/null +++ b/taglib/mod/modfileprivate.h @@ -0,0 +1,67 @@ +/*************************************************************************** + 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 * + ***************************************************************************/ + +#ifndef TAGLIB_MODFILEPRIVATE_H +#define TAGLIB_MODFILEPRIVATE_H + +// some helper-macros only used internally by (s3m|it|xm)file.cpp +#define READ_ASSERT(cond) \ + if(!(cond)) \ + { \ + setValid(false); \ + return; \ + } + +#define READ(setter,type,read) \ + { \ + type number; \ + READ_ASSERT(read(number)); \ + setter(number); \ + } + +#define READ_BYTE(setter) READ(setter,uchar,readByte) +#define READ_U16L(setter) READ(setter,ushort,readU16L) +#define READ_U32L(setter) READ(setter,ulong,readU32L) +#define READ_U16B(setter) READ(setter,ushort,readU16B) +#define READ_U32B(setter) READ(setter,ulong,readU32B) + +#define READ_STRING(setter,size) \ + { \ + String s; \ + READ_ASSERT(readString(s, size)); \ + setter(s); \ + } + +#define READ_AS(type,name,read) \ + type name = 0; \ + READ_ASSERT(read(name)); + +#define READ_BYTE_AS(name) READ_AS(uchar,name,readByte) +#define READ_U16L_AS(name) READ_AS(ushort,name,readU16L) +#define READ_U32L_AS(name) READ_AS(ulong,name,readU32L) +#define READ_U16B_AS(name) READ_AS(ushort,name,readU16B) +#define READ_U32B_AS(name) READ_AS(ulong,name,readU32B) + +#define READ_STRING_AS(name,size) \ + String name; \ + READ_ASSERT(readString(name, size)); + +#endif diff --git a/taglib/mod/modproperties.cpp b/taglib/mod/modproperties.cpp new file mode 100644 index 00000000..47903454 --- /dev/null +++ b/taglib/mod/modproperties.cpp @@ -0,0 +1,96 @@ +/*************************************************************************** + 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 "modproperties.h" + +using namespace TagLib; +using namespace Mod; + +class Mod::Properties::PropertiesPrivate +{ +public: + PropertiesPrivate() : + channels(0), + instrumentCount(0), + lengthInPatterns(0) + { + } + + int channels; + uint instrumentCount; + uchar lengthInPatterns; +}; + +Mod::Properties::Properties(AudioProperties::ReadStyle propertiesStyle) : + AudioProperties(propertiesStyle), + d(new PropertiesPrivate) +{ +} + +Mod::Properties::~Properties() +{ + delete d; +} + +int Mod::Properties::length() const +{ + return 0; +} + +int Mod::Properties::bitrate() const +{ + return 0; +} + +int Mod::Properties::sampleRate() const +{ + return 0; +} + +int Mod::Properties::channels() const +{ + return d->channels; +} + +uint Mod::Properties::instrumentCount() const +{ + return d->instrumentCount; +} + +uchar Mod::Properties::lengthInPatterns() const +{ + return d->lengthInPatterns; +} + +void Mod::Properties::setChannels(int channels) +{ + d->channels = channels; +} + +void Mod::Properties::setInstrumentCount(uint instrumentCount) +{ + d->instrumentCount = instrumentCount; +} + +void Mod::Properties::setLengthInPatterns(uchar lengthInPatterns) +{ + d->lengthInPatterns = lengthInPatterns; +} diff --git a/taglib/mod/modproperties.h b/taglib/mod/modproperties.h new file mode 100644 index 00000000..8bf3ac5e --- /dev/null +++ b/taglib/mod/modproperties.h @@ -0,0 +1,60 @@ +/*************************************************************************** + 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 * + ***************************************************************************/ + +#ifndef TAGLIB_MODPROPERTIES_H +#define TAGLIB_MODPROPERTIES_H + +#include "taglib.h" +#include "audioproperties.h" + +namespace TagLib { + namespace Mod { + class TAGLIB_EXPORT Properties : public AudioProperties { + friend class File; + public: + Properties(AudioProperties::ReadStyle propertiesStyle); + virtual ~Properties(); + + int length() const; + int bitrate() const; + int sampleRate() const; + int channels() const; + + uint instrumentCount() const; + uchar lengthInPatterns() const; + + protected: + void setChannels(int channels); + + void setInstrumentCount(uint sampleCount); + void setLengthInPatterns(uchar lengthInPatterns); + + private: + Properties(const Properties&); + Properties &operator=(const Properties&); + + class PropertiesPrivate; + PropertiesPrivate *d; + }; + } +} + +#endif diff --git a/taglib/mod/modtag.cpp b/taglib/mod/modtag.cpp new file mode 100644 index 00000000..1dabe30e --- /dev/null +++ b/taglib/mod/modtag.cpp @@ -0,0 +1,122 @@ +/*************************************************************************** + 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 "modtag.h" + +using namespace TagLib; +using namespace Mod; + +class Mod::Tag::TagPrivate +{ +public: + TagPrivate() + { + } + + String title; + String comment; + String trackerName; +}; + +Mod::Tag::Tag() : TagLib::Tag() +{ + d = new TagPrivate; +} + +Mod::Tag::~Tag() +{ + delete d; +} + +String Mod::Tag::title() const +{ + return d->title; +} + +String Mod::Tag::artist() const +{ + return String::null; +} + +String Mod::Tag::album() const +{ + return String::null; +} + +String Mod::Tag::comment() const +{ + return d->comment; +} + +String Mod::Tag::genre() const +{ + return String::null; +} + +uint Mod::Tag::year() const +{ + return 0; +} + +uint Mod::Tag::track() const +{ + return 0; +} + +String Mod::Tag::trackerName() const +{ + return d->trackerName; +} + +void Mod::Tag::setTitle(const String &title) +{ + d->title = title; +} + +void Mod::Tag::setArtist(const String &) +{ +} + +void Mod::Tag::setAlbum(const String &) +{ +} + +void Mod::Tag::setComment(const String &comment) +{ + d->comment = comment; +} + +void Mod::Tag::setGenre(const String &) +{ +} + +void Mod::Tag::setYear(uint) +{ +} + +void Mod::Tag::setTrack(uint) +{ +} + +void Mod::Tag::setTrackerName(const String &trackerName) +{ + d->trackerName = trackerName; +} diff --git a/taglib/mod/modtag.h b/taglib/mod/modtag.h new file mode 100644 index 00000000..554ca326 --- /dev/null +++ b/taglib/mod/modtag.h @@ -0,0 +1,170 @@ +/*************************************************************************** + 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 * + ***************************************************************************/ + +#ifndef TAGLIB_MODTAG_H +#define TAGLIB_MODTAG_H + +#include "tag.h" + +namespace TagLib { + namespace Mod { + /*! + * Tags for module files (Mod, S3M, IT, XM). + * + * Note that only the \a title is supported as such by most + * module file formats. Except for XM files the \a trackerName + * is derived from the file format or the flavour of the file + * format. For XM files it is stored in the file. + * + * The \a comment tag is not strictly supported by module files, + * but it is common practice to abuse instrument/sample/pattern + * names as multiline comments. TagLib does so as well. + */ + class TAGLIB_EXPORT Tag : public TagLib::Tag + { + public: + Tag(); + virtual ~Tag(); + + /*! + * Returns the track name; if no track name is present in the tag + * String::null will be returned. + */ + String title() const; + + /*! + * Not supported by module files. Therefore always returns String::null. + */ + String artist() const; + + /*! + * Not supported by module files. Therefore always returns String::null. + */ + String album() const; + + /*! + * Returns the track comment derived from the instrument/sample/pattern + * names; if no comment is present in the tag String::null will be + * returned. + */ + String comment() const; + + /*! + * Not supported by module files. Therefore always returns String::null. + */ + String genre() const; + + /*! + * Not supported by module files. Therefore always returns 0. + */ + uint year() const; + + /*! + * Not supported by module files. Therefore always returns 0. + */ + uint track() const; + + /*! + * Returns the name of the tracker used to create/edit the module file. + * Only XM files store this tag to the file as such, for other formats + * (Mod, S3M, IT) this is derived from the file type or the flavour of + * the file type. Therefore only XM files might have an empty + * (String::null) tracker name. + */ + String trackerName() const; + + /*! + * Sets the title to \a title. If \a title is String::null then this + * value will be cleared. + * + * The length limits per file type are (1 characetr = 1 byte): + * Mod 20 characters, S3M 28 characters, IT 26 characters and XM 20 + * characters. + */ + void setTitle(const String &title); + + /*! + * Not supported by module files and therefore ignored. + */ + void setArtist(const String &artist); + + /*! + * Not supported by module files and therefore ignored. + */ + void setAlbum(const String &album); + + /*! + * Sets the comment to \a comment. If \a comment is String::null then + * this value will be cleared. + * + * Note that module file formats don't actually support a comment tag. + * Instead the names of instruments/patterns/samples are abused as + * a multiline comment. Because of this the number of lines in a + * module file is fixed to the number of instruments/patterns/samples. + * + * Also note that the instrument/pattern/sample name length is limited + * an thus the line length in comments are limited. Too big comments + * will be truncated. + * + * The line length limits per file type are (1 characetr = 1 byte): + * Mod 22 characters, S3M 28 characters, IT 26 characters and XM 22 + * characters. + */ + void setComment(const String &comment); + + /*! + * Not supported by module files and therefore ignored. + */ + void setGenre(const String &genre); + + /*! + * Not supported by module files and therefore ignored. + */ + void setYear(uint year); + + /*! + * Not supported by module files and therefore ignored. + */ + void setTrack(uint track); + + /*! + * Sets the tracker name to \a trackerName. If \a trackerName is + * String::null then this value will be cleared. + * + * Note that only XM files support this tag. Setting the + * tracker name for other module file formats will be ignored. + * + * The length of this tag is limited to 20 characters (1 character + * = 1 byte). + */ + void setTrackerName(const String &trackerName); + + private: + Tag(const Tag &); + Tag &operator=(const Tag &); + + class TagPrivate; + TagPrivate *d; + }; + } +} + +#endif diff --git a/taglib/s3m/s3mfile.cpp b/taglib/s3m/s3mfile.cpp new file mode 100644 index 00000000..0c8a712d --- /dev/null +++ b/taglib/s3m/s3mfile.cpp @@ -0,0 +1,230 @@ +/*************************************************************************** + 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 "s3mfile.h" +#include "tstringlist.h" +#include "tdebug.h" +#include "modfileprivate.h" + +#include <iostream> + +using namespace TagLib; +using namespace S3M; + +class S3M::File::FilePrivate +{ +public: + FilePrivate(AudioProperties::ReadStyle propertiesStyle) + : properties(propertiesStyle) + { + } + + Mod::Tag tag; + S3M::Properties properties; +}; + +S3M::File::File(FileName file, bool readProperties, + AudioProperties::ReadStyle propertiesStyle) : + Mod::FileBase(file), + d(new FilePrivate(propertiesStyle)) +{ + read(readProperties); +} + +S3M::File::File(IOStream *stream, bool readProperties, + AudioProperties::ReadStyle propertiesStyle) : + Mod::FileBase(stream), + d(new FilePrivate(propertiesStyle)) +{ + read(readProperties); +} + +S3M::File::~File() +{ + delete d; +} + +Mod::Tag *S3M::File::tag() const +{ + return &d->tag; +} + +S3M::Properties *S3M::File::audioProperties() const +{ + return &d->properties; +} + +bool S3M::File::save() +{ + if(readOnly()) { + debug("S3M::File::save() - Cannot save to a read only file."); + return false; + } + // note: if title starts with "Extended Module: " + // the file would look like an .xm file + seek(0); + writeString(d->tag.title(), 27); + // string terminating NUL is not optional: + writeByte(0); + + seek(32); + + ushort length = 0; + ushort sampleCount = 0; + + if(!readU16L(length) || !readU16L(sampleCount)) + return false; + + seek(28, Current); + + int channels = 0; + for(int i = 0; i < 32; ++ i) { + uchar setting = 0; + if(!readByte(setting)) + return false; + // or if(setting >= 128)? + // or channels = i + 1;? + // need a better spec! + if(setting != 0xff) ++ channels; + } + + seek(channels, Current); + + StringList lines = d->tag.comment().split("\n"); + // write comment as sample names: + for(ushort i = 0; i < sampleCount; ++ i) { + seek(96L + length + ((long)i << 1)); + + ushort instrumentOffset = 0; + if(!readU16L(instrumentOffset)) + return false; + seek(((long)instrumentOffset << 4) + 48); + + if(i < lines.size()) + writeString(lines[i], 27); + else + writeString(String::null, 27); + // string terminating NUL is not optional: + writeByte(0); + } + return true; +} + +void S3M::File::read(bool) +{ + if(!isOpen()) + return; + + READ_STRING(d->tag.setTitle, 28); + READ_BYTE_AS(mark); + READ_BYTE_AS(type); + + READ_ASSERT(mark == 0x1A && type == 0x10); + + seek(32); + + READ_U16L_AS(length); + READ_U16L_AS(sampleCount); + + d->properties.setSampleCount(sampleCount); + + READ_U16L(d->properties.setPatternCount); + READ_U16L(d->properties.setFlags); + READ_U16L(d->properties.setTrackerVersion); + READ_U16L(d->properties.setFileFormatVersion); + + READ_ASSERT(readBlock(4) == "SCRM"); + + READ_BYTE(d->properties.setGlobalVolume); + READ_BYTE(d->properties.setBpmSpeed); + READ_BYTE(d->properties.setTempo); + + READ_BYTE_AS(masterVolume); + d->properties.setMasterVolume(masterVolume & 0x7f); + d->properties.setStereo((masterVolume & 0x80) != 0); + + // I've seen players who call the next two bytes + // "ultra click" and "use panning values" (if == 0xFC). + // I don't see them in any spec, though. + // Hm, but there is "UltraClick-removal" and some other + // variables in ScreamTracker IIIs GUI. + + seek(12, Current); + + int channels = 0; + for(int i = 0; i < 32; ++ i) { + READ_BYTE_AS(setting); + // or if(setting >= 128)? + // or channels = i + 1;? + // need a better spec! + if(setting != 0xff) ++ channels; + } + d->properties.setChannels(channels); + + seek(96); + ushort realLength = 0; + for(ushort i = 0; i < length; ++ i) { + READ_BYTE_AS(order); + if(order == 255) break; + if(order != 254) ++ realLength; + } + d->properties.setLengthInPatterns(realLength); + + seek(channels, Current); + + // Note: The S3M spec mentions samples and instruments, but in + // the header there are only pointers to instruments. + // However, there I never found instruments (SCRI) but + // instead samples (SCRS). + StringList comment; + for(ushort i = 0; i < sampleCount; ++ i) { + seek(96L + length + ((long)i << 1)); + + READ_U16L_AS(sampleHeaderOffset); + seek((long)sampleHeaderOffset << 4); + + READ_BYTE_AS(sampleType); + READ_STRING_AS(dosFileName, 13); + READ_U16L_AS(sampleDataOffset); + READ_U32L_AS(sampleLength); + READ_U32L_AS(repeatStart); + READ_U32L_AS(repeatStop); + READ_BYTE_AS(sampleVolume); + + seek(1, Current); + + READ_BYTE_AS(packing); + READ_BYTE_AS(sampleFlags); + READ_U32L_AS(baseFrequency); + + seek(12, Current); + + READ_STRING_AS(sampleName, 28); + // The next 4 bytes should be "SCRS", but I've found + // files that are otherwise ok with 4 nils instead. + // READ_ASSERT(readBlock(4) == "SCRS"); + + comment.append(sampleName); + } + + d->tag.setComment(comment.toString("\n")); + d->tag.setTrackerName("ScreamTracker III"); +} diff --git a/taglib/s3m/s3mfile.h b/taglib/s3m/s3mfile.h new file mode 100644 index 00000000..6eb938a3 --- /dev/null +++ b/taglib/s3m/s3mfile.h @@ -0,0 +1,89 @@ +/*************************************************************************** + 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 * + ***************************************************************************/ + +#ifndef TAGLIB_S3MFILE_H +#define TAGLIB_S3MFILE_H + +#include "tfile.h" +#include "audioproperties.h" +#include "taglib_export.h" +#include "modfilebase.h" +#include "modtag.h" +#include "s3mproperties.h" + +namespace TagLib { + + namespace S3M { + + class TAGLIB_EXPORT File : public Mod::FileBase { + public: + /*! + * Contructs a ScreamTracker III 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(FileName file, bool readProperties = true, + AudioProperties::ReadStyle propertiesStyle = + AudioProperties::Average); + + /*! + * Contructs a ScreamTracker III 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(); + + Mod::Tag *tag() const; + + /*! + * Returns the S3M::Properties for this file. If no audio properties + * were read then this will return a null pointer. + */ + S3M::Properties *audioProperties() const; + + /*! + * Save the file. + * This is the same as calling save(AllTags); + * + * \note Saving ScreamTracker III tags is not supported. + */ + bool save(); + + private: + File(const File &); + File &operator=(const File &); + + void read(bool readProperties); + + class FilePrivate; + FilePrivate *d; + }; + } +} + +#endif diff --git a/taglib/s3m/s3mproperties.cpp b/taglib/s3m/s3mproperties.cpp new file mode 100644 index 00000000..8b50a60b --- /dev/null +++ b/taglib/s3m/s3mproperties.cpp @@ -0,0 +1,204 @@ +/*************************************************************************** + 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 "s3mproperties.h" + +using namespace TagLib; +using namespace S3M; + +class S3M::Properties::PropertiesPrivate +{ +public: + PropertiesPrivate() : + lengthInPatterns(0), + channels(0), + stereo(false), + sampleCount(0), + patternCount(0), + flags(0), + trackerVersion(0), + fileFormatVersion(0), + globalVolume(0), + masterVolume(0), + tempo(0), + bpmSpeed(0) + { + } + + ushort lengthInPatterns; + int channels; + bool stereo; + ushort sampleCount; + ushort patternCount; + ushort flags; + ushort trackerVersion; + ushort fileFormatVersion; + uchar globalVolume; + uchar masterVolume; + uchar tempo; + uchar bpmSpeed; +}; + +S3M::Properties::Properties(AudioProperties::ReadStyle propertiesStyle) : + AudioProperties(propertiesStyle), + d(new PropertiesPrivate) +{ +} + +S3M::Properties::~Properties() +{ + delete d; +} + +int S3M::Properties::length() const +{ + return 0; +} + +int S3M::Properties::bitrate() const +{ + return 0; +} + +int S3M::Properties::sampleRate() const +{ + return 0; +} + +int S3M::Properties::channels() const +{ + return d->channels; +} + +ushort S3M::Properties::lengthInPatterns() const +{ + return d->lengthInPatterns; +} + +bool S3M::Properties::stereo() const +{ + return d->stereo; +} + +ushort S3M::Properties::sampleCount() const +{ + return d->sampleCount; +} + +ushort S3M::Properties::patternCount() const +{ + return d->patternCount; +} + +ushort S3M::Properties::flags() const +{ + return d->flags; +} + +ushort S3M::Properties::trackerVersion() const +{ + return d->trackerVersion; +} + +ushort S3M::Properties::fileFormatVersion() const +{ + return d->fileFormatVersion; +} + +uchar S3M::Properties::globalVolume() const +{ + return d->globalVolume; +} + +uchar S3M::Properties::masterVolume() const +{ + return d->masterVolume; +} + +uchar S3M::Properties::tempo() const +{ + return d->tempo; +} + +uchar S3M::Properties::bpmSpeed() const +{ + return d->bpmSpeed; +} + +void S3M::Properties::setLengthInPatterns(ushort lengthInPatterns) +{ + d->lengthInPatterns = lengthInPatterns; +} + +void S3M::Properties::setChannels(int channels) +{ + d->channels = channels; +} + +void S3M::Properties::setStereo(bool stereo) +{ + d->stereo = stereo; +} + +void S3M::Properties::setSampleCount(ushort sampleCount) +{ + d->sampleCount = sampleCount; +} + +void S3M::Properties::setPatternCount(ushort patternCount) +{ + d->patternCount = patternCount; +} + +void S3M::Properties::setFlags(ushort flags) +{ + d->flags = flags; +} + +void S3M::Properties::setTrackerVersion(ushort trackerVersion) +{ + d->trackerVersion = trackerVersion; +} + +void S3M::Properties::setFileFormatVersion(ushort fileFormatVersion) +{ + d->fileFormatVersion = fileFormatVersion; +} + +void S3M::Properties::setGlobalVolume(uchar globalVolume) +{ + d->globalVolume = globalVolume; +} + +void S3M::Properties::setMasterVolume(uchar masterVolume) +{ + d->masterVolume = masterVolume; +} + +void S3M::Properties::setTempo(uchar tempo) +{ + d->tempo = tempo; +} + +void S3M::Properties::setBpmSpeed(uchar bpmSpeed) +{ + d->bpmSpeed = bpmSpeed; +} diff --git a/taglib/s3m/s3mproperties.h b/taglib/s3m/s3mproperties.h new file mode 100644 index 00000000..4be14508 --- /dev/null +++ b/taglib/s3m/s3mproperties.h @@ -0,0 +1,89 @@ +/*************************************************************************** + 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 * + ***************************************************************************/ + +#ifndef TAGLIB_S3MPROPERTIES_H +#define TAGLIB_S3MPROPERTIES_H + +#include "taglib.h" +#include "audioproperties.h" + +namespace TagLib { + namespace S3M { + class TAGLIB_EXPORT Properties : public AudioProperties { + friend class File; + public: + /*! Flag bits. */ + enum { + ST2Vibrato = 1, + ST2Tempo = 2, + AmigaSlides = 4, + Vol0MixOptimizations = 8, + AmigaLimits = 16, + EnableFilter = 32, + CustomData = 128 + }; + + Properties(AudioProperties::ReadStyle propertiesStyle); + virtual ~Properties(); + + int length() const; + int bitrate() const; + int sampleRate() const; + int channels() const; + + ushort lengthInPatterns() const; + bool stereo() const; + ushort sampleCount() const; + ushort patternCount() const; + ushort flags() const; + ushort trackerVersion() const; + ushort fileFormatVersion() const; + uchar globalVolume() const; + uchar masterVolume() const; + uchar tempo() const; + uchar bpmSpeed() const; + + protected: + void setChannels(int channels); + + void setLengthInPatterns (ushort lengthInPatterns); + void setStereo (bool stereo); + void setSampleCount (ushort sampleCount); + void setPatternCount (ushort patternCount); + void setFlags (ushort flags); + void setTrackerVersion (ushort trackerVersion); + void setFileFormatVersion(ushort fileFormatVersion); + void setGlobalVolume (uchar globalVolume); + void setMasterVolume (uchar masterVolume); + void setTempo (uchar tempo); + void setBpmSpeed (uchar bpmSpeed); + + private: + Properties(const Properties&); + Properties &operator=(const Properties&); + + class PropertiesPrivate; + PropertiesPrivate *d; + }; + } +} + +#endif diff --git a/taglib/toolkit/taglib.h b/taglib/toolkit/taglib.h index c42f1bde..eebafbc5 100644 --- a/taglib/toolkit/taglib.h +++ b/taglib/toolkit/taglib.h @@ -76,9 +76,10 @@ namespace TagLib { class String; typedef wchar_t wchar; - typedef unsigned char uchar; - typedef unsigned int uint; - typedef unsigned long ulong; + typedef unsigned char uchar; + typedef unsigned short ushort; + typedef unsigned int uint; + typedef unsigned long ulong; /*! * Unfortunately std::wstring isn't defined on some systems, (i.e. GCC < 3) diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index 99a10b10..a9a1ee43 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -26,6 +26,7 @@ #include "tstring.h" #include "unicode.h" #include "tdebug.h" +#include "tstringlist.h" #include <ostream> @@ -304,6 +305,26 @@ int String::rfind(const String &s, int offset) const return -1; } +StringList String::split(const String &separator) const +{ + StringList list; + for(int index = 0;;) + { + int sep = find(separator, index); + if(sep < 0) + { + list.append(substr(index, size() - index)); + break; + } + else + { + list.append(substr(index, sep - index)); + index = sep + separator.size(); + } + } + return list; +} + bool String::startsWith(const String &s) const { if(s.length() > length()) diff --git a/taglib/toolkit/tstring.h b/taglib/toolkit/tstring.h index 693d043f..10b9f66a 100644 --- a/taglib/toolkit/tstring.h +++ b/taglib/toolkit/tstring.h @@ -56,6 +56,8 @@ namespace TagLib { + class StringList; + //! A \e wide string class suitable for unicode. /*! @@ -240,6 +242,11 @@ namespace TagLib { int rfind(const String &s, int offset = -1) const; /*! + * Splits the string on each occurrence of \a separator. + */ + StringList split(const String &separator = " ") const; + + /*! * Returns true if the strings starts with the substring \a s. */ bool startsWith(const String &s) const; diff --git a/taglib/xm/xmfile.cpp b/taglib/xm/xmfile.cpp new file mode 100644 index 00000000..8f001e9e --- /dev/null +++ b/taglib/xm/xmfile.cpp @@ -0,0 +1,637 @@ +/*************************************************************************** + 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 "tstringlist.h" +#include "tdebug.h" +#include "xmfile.h" +#include "modfileprivate.h" + +#include <string.h> +#include <algorithm> + +using namespace TagLib; +using namespace XM; +using TagLib::uint; +using TagLib::ushort; +using TagLib::ulong; + +/*! + * The *Reader classes are helpers to make handling of the stripped XM + * format more easy. In the stripped XM format certain headersizes might + * be smaller than one would expect. The fields that are not included + * are then just some predefined valued (e.g. 0). + * + * Using these classes this code: + * + * if(headerSize >= 4) { + * if(!readU16L(value1)) ERROR(); + * if(headerSize >= 8) { + * if(!readU16L(value2)) ERROR(); + * if(headerSize >= 12) { + * if(!readString(value3, 22)) ERROR(); + * ... + * } + * } + * } + * + * Becomes: + * + * StructReader header; + * header.u16L(value1).u16L(value2).string(value3, 22). ...; + * if(header.read(*this, headerSize) < std::min(header.size(), headerSize)) + * ERROR(); + * + * Maybe if this is useful to other formats this cleasses can be moved to + * their onw public files. + */ +class Reader +{ +public: + virtual ~Reader() + { + } + + /*! + * Reads associated values from \a file, but never reads more + * then \a limit bytes. + */ + virtual uint read(TagLib::File &file, uint limit) = 0; + + /*! + * Returns the number of bytes this reader would like to read. + */ + virtual uint size() const = 0; +}; + +class SkipReader : public Reader +{ +public: + SkipReader(uint size) : m_size(size) + { + } + + uint read(TagLib::File &file, uint limit) + { + uint count = std::min(m_size, limit); + file.seek(count, TagLib::File::Current); + return count; + } + + uint size() const + { + return m_size; + } + +private: + uint m_size; +}; + +template<typename T> +class ValueReader : public Reader +{ +public: + ValueReader(T &value) : value(value) + { + } + +protected: + T &value; +}; + +class StringReader : public ValueReader<String> +{ +public: + StringReader(String &string, uint size) : + ValueReader<String>(string), m_size(size) + { + } + + uint read(TagLib::File &file, uint limit) + { + ByteVector data = file.readBlock(std::min(m_size,limit)); + uint count = data.size(); + int index = data.find((char) 0); + if(index > -1) { + data.resize(index); + } + data.replace((char) 0xff, ' '); + value = data; + return count; + } + + uint size() const + { + return m_size; + } + +private: + uint m_size; +}; + +class ByteReader : public ValueReader<uchar> +{ +public: + ByteReader(uchar &byte) : ValueReader<uchar>(byte) {} + + uint read(TagLib::File &file, uint limit) + { + ByteVector data = file.readBlock(std::min(1U,limit)); + if(data.size() > 0) { + value = data[0]; + } + return data.size(); + } + + uint size() const + { + return 1; + } +}; + +template<typename T> +class NumberReader : public ValueReader<T> +{ +public: + NumberReader(T &value, bool bigEndian) : + ValueReader<T>(value), bigEndian(bigEndian) + { + } + +protected: + bool bigEndian; +}; + +class U16Reader : public NumberReader<ushort> +{ +public: + U16Reader(ushort &value, bool bigEndian) + : NumberReader<ushort>(value, bigEndian) {} + + uint read(TagLib::File &file, uint limit) + { + ByteVector data = file.readBlock(std::min(2U,limit)); + value = data.toUShort(bigEndian); + return data.size(); + } + + uint size() const + { + return 2; + } +}; + +class U32Reader : public NumberReader<ulong> +{ +public: + U32Reader(ulong &value, bool bigEndian = true) : + NumberReader<ulong>(value, bigEndian) + { + } + + uint read(TagLib::File &file, uint limit) + { + ByteVector data = file.readBlock(std::min(4U,limit)); + value = data.toUInt(bigEndian); + return data.size(); + } + + uint size() const + { + return 4; + } +}; + +class StructReader : public Reader +{ +public: + StructReader() + { + m_readers.setAutoDelete(true); + } + + /*! + * Add a nested reader. This reader takes ownership. + */ + StructReader &reader(Reader *reader) + { + m_readers.append(reader); + return *this; + } + + /*! + * Don't read anything but skip \a size bytes. + */ + StructReader &skip(uint size) + { + m_readers.append(new SkipReader(size)); + return *this; + } + + /*! + * Read a string of \a size characters (bytes) into \a string. + */ + StructReader &string(String &string, uint size) + { + m_readers.append(new StringReader(string, size)); + return *this; + } + + /*! + * Read a byte into \a byte. + */ + StructReader &byte(uchar &byte) + { + m_readers.append(new ByteReader(byte)); + return *this; + } + + /*! + * Read a unsigned 16 Bit integer into \a number. The byte order + * is controlled by \a bigEndian. + */ + StructReader &u16(ushort &number, bool bigEndian) + { + m_readers.append(new U16Reader(number, bigEndian)); + return *this; + } + + /*! + * Read a unsigned 16 Bit little endian integer into \a number. + */ + StructReader &u16L(ushort &number) + { + return u16(number, false); + } + + /*! + * Read a unsigned 16 Bit big endian integer into \a number. + */ + StructReader &u16B(ushort &number) + { + return u16(number, true); + } + + /*! + * Read a unsigned 32 Bit integer into \a number. The byte order + * is controlled by \a bigEndian. + */ + StructReader &u32(ulong &number, bool bigEndian) + { + m_readers.append(new U32Reader(number, bigEndian)); + return *this; + } + + /*! + * Read a unsigned 32 Bit little endian integer into \a number. + */ + StructReader &u32L(ulong &number) + { + return u32(number, false); + } + + /*! + * Read a unsigned 32 Bit big endian integer into \a number. + */ + StructReader &u32B(ulong &number) + { + return u32(number, true); + } + + uint size() const + { + uint size = 0; + for(List<Reader*>::ConstIterator i = m_readers.begin(); + i != m_readers.end(); ++ i) { + size += (*i)->size(); + } + return size; + } + + uint read(TagLib::File &file, uint limit) + { + uint sumcount = 0; + for(List<Reader*>::Iterator i = m_readers.begin(); + limit > 0 && i != m_readers.end(); ++ i) { + uint count = (*i)->read(file, limit); + limit -= count; + sumcount += count; + } + return sumcount; + } + +private: + List<Reader*> m_readers; +}; + +class XM::File::FilePrivate +{ +public: + FilePrivate(AudioProperties::ReadStyle propertiesStyle) + : tag(), properties(propertiesStyle) + { + } + + Mod::Tag tag; + XM::Properties properties; +}; + +XM::File::File(FileName file, bool readProperties, + AudioProperties::ReadStyle propertiesStyle) : + Mod::FileBase(file), + d(new FilePrivate(propertiesStyle)) +{ + read(readProperties); +} + +XM::File::File(IOStream *stream, bool readProperties, + AudioProperties::ReadStyle propertiesStyle) : + Mod::FileBase(stream), + d(new FilePrivate(propertiesStyle)) +{ + read(readProperties); +} + +XM::File::~File() +{ + delete d; +} + +Mod::Tag *XM::File::tag() const +{ + return &d->tag; +} + +XM::Properties *XM::File::audioProperties() const +{ + return &d->properties; +} + +bool XM::File::save() +{ + if(readOnly()) { + debug("XM::File::save() - Cannot save to a read only file."); + return false; + } + seek(17); + writeString(d->tag.title(), 20); + seek(1, Current); + writeString(d->tag.trackerName(), 20); + seek(2, Current); + ulong headerSize = 0; + if(!readU32L(headerSize)) + return false; + seek(2+2+2, Current); + + ushort patternCount = 0; + ushort instrumentCount = 0; + if(!readU16L(patternCount) || !readU16L(instrumentCount)) + return false; + + seek(60 + headerSize); + + // need to read patterns again in order to seek to the instruments: + for(ushort i = 0; i < patternCount; ++ i) { + ulong patternHeaderLength = 0; + if(!readU32L(patternHeaderLength) || patternHeaderLength < 4) + return false; + + ushort dataSize = 0; + StructReader pattern; + pattern.skip(3).u16L(dataSize); + + uint count = pattern.read(*this, patternHeaderLength - 4U); + if(count != std::min(patternHeaderLength - 4U, (ulong)pattern.size())) + return false; + + seek(patternHeaderLength - (4 + count) + dataSize, Current); + } + + StringList lines = d->tag.comment().split("\n"); + uint sampleNameIndex = instrumentCount; + for(ushort i = 0; i < instrumentCount; ++ i) { + ulong instrumentHeaderSize = 0; + if(!readU32L(instrumentHeaderSize) || instrumentHeaderSize < 4) + return false; + + uint len = std::min(22UL, instrumentHeaderSize - 4U); + if(i > lines.size()) + writeString(String::null, len); + else + writeString(lines[i], len); + + long offset = 0; + if(instrumentHeaderSize >= 29U) { + ushort sampleCount = 0; + seek(1, Current); + if(!readU16L(sampleCount)) + return false; + + if(sampleCount > 0) { + ulong sampleHeaderSize = 0; + if(instrumentHeaderSize < 33U || !readU32L(sampleHeaderSize)) + return false; + // skip unhandeled header proportion: + seek(instrumentHeaderSize - 33, Current); + + for(ushort j = 0; j < sampleCount; ++ j) { + if(sampleHeaderSize > 4U) { + ulong sampleLength = 0; + if(!readU32L(sampleLength)) + return false; + offset += sampleLength; + + seek(std::min(sampleHeaderSize, 14UL), Current); + if(sampleHeaderSize > 18U) { + uint len = std::min(sampleHeaderSize - 18U, 22UL); + if(sampleNameIndex >= lines.size()) + writeString(String::null, len); + else + writeString(lines[sampleNameIndex ++], len); + seek(sampleHeaderSize - (18U + len), Current); + } + } + else { + seek(sampleHeaderSize, Current); + } + } + } + else { + offset = instrumentHeaderSize - 29; + } + } + else { + offset = instrumentHeaderSize - (4 + len); + } + seek(offset, Current); + } + + return true; +} + +void XM::File::read(bool) +{ + if(!isOpen()) + return; + + seek(0); + ByteVector magic = readBlock(17); + // it's all 0x00 for stripped XM files: + READ_ASSERT(magic == "Extended Module: " || magic == ByteVector(17, 0)); + + READ_STRING(d->tag.setTitle, 20); + READ_BYTE_AS(escape); + // in stripped XM files this is 0x00: + READ_ASSERT(escape == 0x1A || escape == 0x00); + + READ_STRING(d->tag.setTrackerName, 20); + READ_U16L(d->properties.setVersion); + + READ_U32L_AS(headerSize); + READ_ASSERT(headerSize >= 4); + + ushort length = 0; + ushort restartPosition = 0; + ushort channels = 0; + ushort patternCount = 0; + ushort instrumentCount = 0; + ushort flags = 0; + ushort tempo = 0; + ushort bpmSpeed = 0; + + StructReader header; + header.u16L(length) + .u16L(restartPosition) + .u16L(channels) + .u16L(patternCount) + .u16L(instrumentCount) + .u16L(flags) + .u16L(tempo) + .u16L(bpmSpeed); + + uint count = header.read(*this, headerSize - 4U); + uint size = std::min(headerSize - 4U, (ulong)header.size()); + + READ_ASSERT(count == size); + + d->properties.setLengthInPatterns(length); + d->properties.setRestartPosition(restartPosition); + d->properties.setChannels(channels); + d->properties.setPatternCount(patternCount); + d->properties.setInstrumentCount(instrumentCount); + d->properties.setFlags(flags); + d->properties.setTempo(tempo); + d->properties.setBpmSpeed(bpmSpeed); + + seek(60 + headerSize); + + // read patterns: + for(ushort i = 0; i < patternCount; ++ i) { + READ_U32L_AS(patternHeaderLength); + READ_ASSERT(patternHeaderLength >= 4); + + uchar packingType = 0; + ushort rowCount = 0; + ushort dataSize = 0; + StructReader pattern; + pattern.byte(packingType).u16L(rowCount).u16L(dataSize); + + uint count = pattern.read(*this, patternHeaderLength - 4U); + READ_ASSERT(count == std::min(patternHeaderLength - 4U, (ulong)pattern.size())); + + seek(patternHeaderLength - (4 + count) + dataSize, Current); + } + + StringList intrumentNames; + StringList sampleNames; + uint sumSampleCount = 0; + + // read instruments: + for(ushort i = 0; i < instrumentCount; ++ i) { + READ_U32L_AS(instrumentHeaderSize); + READ_ASSERT(instrumentHeaderSize >= 4); + + String instrumentName; + uchar instrumentType = 0; + ushort sampleCount = 0; + + StructReader instrument; + instrument.string(instrumentName, 22).byte(instrumentType).u16L(sampleCount); + + // 4 for instrumentHeaderSize + uint count = 4 + instrument.read(*this, instrumentHeaderSize - 4U); + READ_ASSERT(count == std::min(instrumentHeaderSize, (ulong)instrument.size() + 4)); + + ulong sampleHeaderSize = 0; + long offset = 0; + if(sampleCount > 0) { + sumSampleCount += sampleCount; + // wouldn't know which header size to assume otherwise: + READ_ASSERT(instrumentHeaderSize >= count + 4 && readU32L(sampleHeaderSize)); + // skip unhandeled header proportion: + seek(instrumentHeaderSize - count - 4, Current); + + for(ushort j = 0; j < sampleCount; ++ j) { + ulong sampleLength = 0; + ulong loopStart = 0; + ulong loopLength = 0; + uchar volume = 0; + uchar finetune = 0; + uchar sampleType = 0; + uchar panning = 0; + uchar noteNumber = 0; + uchar compression = 0; + String sampleName; + StructReader sample; + sample.u32L(sampleLength) + .u32L(loopStart) + .u32L(loopLength) + .byte(volume) + .byte(finetune) + .byte(sampleType) + .byte(panning) + .byte(noteNumber) + .byte(compression) + .string(sampleName, 22); + + uint count = sample.read(*this, sampleHeaderSize); + READ_ASSERT(count == std::min(sampleHeaderSize, (ulong)sample.size())); + // skip unhandeled header proportion: + seek(sampleHeaderSize - count, Current); + + offset += sampleLength; + sampleNames.append(sampleName); + } + } + else { + offset = instrumentHeaderSize - count; + } + intrumentNames.append(instrumentName); + seek(offset, Current); + } + + d->properties.setSampleCount(sumSampleCount); + String comment(intrumentNames.toString("\n")); + if(sampleNames.size() > 0) { + comment += "\n"; + comment += sampleNames.toString("\n"); + } + d->tag.setComment(comment); +} diff --git a/taglib/xm/xmfile.h b/taglib/xm/xmfile.h new file mode 100644 index 00000000..a4ae7244 --- /dev/null +++ b/taglib/xm/xmfile.h @@ -0,0 +1,89 @@ +/*************************************************************************** + 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 * + ***************************************************************************/ + +#ifndef TAGLIB_XMFILE_H +#define TAGLIB_XMFILE_H + +#include "tfile.h" +#include "audioproperties.h" +#include "taglib_export.h" +#include "modfilebase.h" +#include "modtag.h" +#include "xmproperties.h" + +namespace TagLib { + + namespace XM { + + class TAGLIB_EXPORT File : public Mod::FileBase { + public: + /*! + * Contructs a Extended Module 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(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(); + + Mod::Tag *tag() const; + + /*! + * Returns the XM::Properties for this file. If no audio properties + * were read then this will return a null pointer. + */ + XM::Properties *audioProperties() const; + + /*! + * Save the file. + * This is the same as calling save(AllTags); + * + * \note Saving Extended Module tags is not supported. + */ + bool save(); + + private: + File(const File &); + File &operator=(const File &); + + void read(bool readProperties); + + class FilePrivate; + FilePrivate *d; + }; + } +} + +#endif diff --git a/taglib/xm/xmproperties.cpp b/taglib/xm/xmproperties.cpp new file mode 100644 index 00000000..e0b489ac --- /dev/null +++ b/taglib/xm/xmproperties.cpp @@ -0,0 +1,180 @@ +/*************************************************************************** + 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() : + lengthInPatterns(0), + channels(0), + version(0), + restartPosition(0), + patternCount(0), + instrumentCount(0), + sampleCount(0), + flags(0), + tempo(0), + bpmSpeed(0) + { + } + + ushort lengthInPatterns; + int channels; + ushort version; + ushort restartPosition; + ushort patternCount; + ushort instrumentCount; + uint sampleCount; + 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::lengthInPatterns() const +{ + return d->lengthInPatterns; +} + +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; +} + +uint XM::Properties::sampleCount() const +{ + return d->sampleCount; +} + +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::setLengthInPatterns(ushort lengthInPatterns) +{ + d->lengthInPatterns = lengthInPatterns; +} + +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::setSampleCount(uint sampleCount) +{ + d->sampleCount = sampleCount; +} + +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; +} diff --git a/taglib/xm/xmproperties.h b/taglib/xm/xmproperties.h new file mode 100644 index 00000000..97f93111 --- /dev/null +++ b/taglib/xm/xmproperties.h @@ -0,0 +1,80 @@ +/*************************************************************************** + 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 * + ***************************************************************************/ + +#ifndef TAGLIB_XMPROPERTIES_H +#define TAGLIB_XMPROPERTIES_H + +#include "taglib.h" +#include "tstring.h" +#include "audioproperties.h" + +namespace TagLib { + namespace XM { + class Properties : public AudioProperties { + friend class File; + public: + /*! Flag bits. */ + enum { + LinearFreqTable = 1 // otherwise its the amiga freq. table + }; + + Properties(AudioProperties::ReadStyle propertiesStyle); + virtual ~Properties(); + + int length() const; + int bitrate() const; + int sampleRate() const; + int channels() const; + + ushort lengthInPatterns() const; + ushort version() const; + ushort restartPosition() const; + ushort patternCount() const; + ushort instrumentCount() const; + uint sampleCount() const; + ushort flags() const; + ushort tempo() const; + ushort bpmSpeed() const; + + protected: + void setChannels(int channels); + + void setLengthInPatterns(ushort lengthInPatterns); + void setVersion(ushort version); + void setRestartPosition(ushort restartPosition); + void setPatternCount(ushort patternCount); + void setInstrumentCount(ushort instrumentCount); + void setSampleCount(uint sampleCount); + void setFlags(ushort flags); + void setTempo(ushort tempo); + void setBpmSpeed(ushort bpmSpeed); + + private: + Properties(const Properties&); + Properties &operator=(const Properties&); + + class PropertiesPrivate; + PropertiesPrivate *d; + }; + } +} + +#endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 723803e9..67b29581 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -19,6 +19,10 @@ INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/../taglib/ogg/flac ${CMAKE_CURRENT_SOURCE_DIR}/../taglib/flac ${CMAKE_CURRENT_SOURCE_DIR}/../taglib/wavpack + ${CMAKE_CURRENT_SOURCE_DIR}/../taglib/mod + ${CMAKE_CURRENT_SOURCE_DIR}/../taglib/s3m + ${CMAKE_CURRENT_SOURCE_DIR}/../taglib/it + ${CMAKE_CURRENT_SOURCE_DIR}/../taglib/xm ) SET(test_runner_SRCS @@ -51,6 +55,10 @@ SET(test_runner_SRCS test_mp4item.cpp test_mp4coverart.cpp test_asf.cpp + test_mod.cpp + test_s3m.cpp + test_it.cpp + test_xm.cpp ) ADD_EXECUTABLE(test_runner ${test_runner_SRCS}) @@ -60,5 +68,4 @@ ADD_CUSTOM_TARGET(check ./test_runner DEPENDS test_runner ) - endif(BUILD_TESTS) diff --git a/tests/data/changed.mod b/tests/data/changed.mod Binary files differnew file mode 100644 index 00000000..13dcea8b --- /dev/null +++ b/tests/data/changed.mod diff --git a/tests/data/changed.s3m b/tests/data/changed.s3m Binary files differnew file mode 100644 index 00000000..37bd49cd --- /dev/null +++ b/tests/data/changed.s3m diff --git a/tests/data/changed.xm b/tests/data/changed.xm Binary files differnew file mode 100644 index 00000000..bb5db3dd --- /dev/null +++ b/tests/data/changed.xm diff --git a/tests/data/stripped.xm b/tests/data/stripped.xm Binary files differnew file mode 100644 index 00000000..57055f5f --- /dev/null +++ b/tests/data/stripped.xm diff --git a/tests/data/test.it b/tests/data/test.it Binary files differnew file mode 100644 index 00000000..379444b9 --- /dev/null +++ b/tests/data/test.it diff --git a/tests/data/test.mod b/tests/data/test.mod Binary files differnew file mode 100644 index 00000000..136b6119 --- /dev/null +++ b/tests/data/test.mod diff --git a/tests/data/test.s3m b/tests/data/test.s3m Binary files differnew file mode 100644 index 00000000..668250bb --- /dev/null +++ b/tests/data/test.s3m diff --git a/tests/data/test.xm b/tests/data/test.xm Binary files differnew file mode 100644 index 00000000..b09d9132 --- /dev/null +++ b/tests/data/test.xm diff --git a/tests/test_flac.cpp b/tests/test_flac.cpp index d93150a3..90baa844 100644 --- a/tests/test_flac.cpp +++ b/tests/test_flac.cpp @@ -190,7 +190,7 @@ public: void testSaveMultipleValues() { - ScopedFileCopy copy("silence-44-s", ".flac", false); + ScopedFileCopy copy("silence-44-s", ".flac"); string newname = copy.fileName(); FLAC::File *f = new FLAC::File(newname.c_str()); diff --git a/tests/test_it.cpp b/tests/test_it.cpp new file mode 100644 index 00000000..167aa6ef --- /dev/null +++ b/tests/test_it.cpp @@ -0,0 +1,136 @@ +/*************************************************************************** + 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 <cppunit/extensions/HelperMacros.h> +#include <itfile.h> +#include <tstringlist.h> +#include "utils.h" + +using namespace std; +using namespace TagLib; +using TagLib::uint; + +static const String titleBefore("test song name"); +static const String titleAfter("changed title"); + +static const String commentBefore( + "This is a sample name.\n" + "In module file formats\n" + "sample names are abused\n" + "as multiline comments.\n" + " "); + +static const String newComment( + "This is a sample name!\n" + "In module file formats\n" + "sample names are abused\n" + "as multiline comments.\n" + "-----------------------------------\n" + "The previous line is truncated but starting with this line\n" + "the comment is not limeted in the line length but to 8000\n" + "additional characters (bytes).\n" + "\n" + "This is because it is saved in the 'message' proportion of\n" + "IT files."); + +static const String commentAfter( + "This is a sample name!\n" + "In module file formats\n" + "sample names are abused\n" + "as multiline comments.\n" + "-------------------------\n" + "The previous line is truncated but starting with this line\n" + "the comment is not limeted in the line length but to 8000\n" + "additional characters (bytes).\n" + "\n" + "This is because it is saved in the 'message' proportion of\n" + "IT files."); + +class TestIT : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(TestIT); + CPPUNIT_TEST(testReadTags); + CPPUNIT_TEST(testWriteTags); + CPPUNIT_TEST_SUITE_END(); + +public: + void testReadTags() + { + testRead(TEST_FILE_PATH_C("test.it"), titleBefore, commentBefore); + } + + void testWriteTags() + { + ScopedFileCopy copy("test", ".it"); + { + IT::File file(copy.fileName().c_str()); + CPPUNIT_ASSERT(file.tag() != 0); + file.tag()->setTitle(titleAfter); + file.tag()->setComment(newComment); + file.tag()->setTrackerName("won't be saved"); + CPPUNIT_ASSERT(file.save()); + } + testRead(copy.fileName().c_str(), titleAfter, commentAfter); + } + +private: + void testRead(FileName fileName, const String &title, const String &comment) + { + IT::File file(fileName); + + CPPUNIT_ASSERT(file.isValid()); + + IT::Properties *p = file.audioProperties(); + Mod::Tag *t = file.tag(); + + CPPUNIT_ASSERT(0 != p); + CPPUNIT_ASSERT(0 != t); + + CPPUNIT_ASSERT_EQUAL( 0, p->length()); + CPPUNIT_ASSERT_EQUAL( 0, p->bitrate()); + CPPUNIT_ASSERT_EQUAL( 0, p->sampleRate()); + CPPUNIT_ASSERT_EQUAL(64, p->channels()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 0, p->lengthInPatterns()); + CPPUNIT_ASSERT_EQUAL(true, p->stereo()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 0, p->instrumentCount()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 5, p->sampleCount()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 1, p->patternCount()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort)535, p->version()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort)532, p->compatibleVersion()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 9, p->flags()); + CPPUNIT_ASSERT_EQUAL((TagLib::uchar)128, p->globalVolume()); + CPPUNIT_ASSERT_EQUAL((TagLib::uchar) 48, p->mixVolume()); + CPPUNIT_ASSERT_EQUAL((TagLib::uchar)125, p->tempo()); + CPPUNIT_ASSERT_EQUAL((TagLib::uchar) 6, p->bpmSpeed()); + CPPUNIT_ASSERT_EQUAL((TagLib::uchar)128, p->panningSeparation()); + CPPUNIT_ASSERT_EQUAL((TagLib::uchar) 0, p->pitchWheelDepth()); + CPPUNIT_ASSERT_EQUAL(title, t->title()); + CPPUNIT_ASSERT_EQUAL(String::null, t->artist()); + CPPUNIT_ASSERT_EQUAL(String::null, t->album()); + CPPUNIT_ASSERT_EQUAL(comment, t->comment()); + CPPUNIT_ASSERT_EQUAL(String::null, t->genre()); + CPPUNIT_ASSERT_EQUAL(0U, t->year()); + CPPUNIT_ASSERT_EQUAL(0U, t->track()); + CPPUNIT_ASSERT_EQUAL(String("Impulse Tracker"), t->trackerName()); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(TestIT); diff --git a/tests/test_mod.cpp b/tests/test_mod.cpp new file mode 100644 index 00000000..67c46f28 --- /dev/null +++ b/tests/test_mod.cpp @@ -0,0 +1,108 @@ +/*************************************************************************** + 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 <cppunit/extensions/HelperMacros.h> +#include <modfile.h> +#include "utils.h" + +using namespace std; +using namespace TagLib; + +static const String titleBefore("title of song"); +static const String titleAfter("changed title"); + +static const String commentBefore( + "Instrument names\n" + "are abused as\n" + "comments in\n" + "module file formats.\n" + "-+-+-+-+-+-+-+-+-+-+-+\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); + +static const String newComment( + "This line will be truncated because it is too long for a mod instrument name.\n" + "This line is ok."); + +static const String commentAfter( + "This line will be trun\n" + "This line is ok.\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); + +class TestMod : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(TestMod); + CPPUNIT_TEST(testReadTags); + CPPUNIT_TEST(testWriteTags); + CPPUNIT_TEST_SUITE_END(); + +public: + void testReadTags() + { + testRead(TEST_FILE_PATH_C("test.mod"), titleBefore, commentBefore); + } + + void testWriteTags() + { + ScopedFileCopy copy("test", ".mod"); + { + Mod::File file(copy.fileName().c_str()); + CPPUNIT_ASSERT(file.tag() != 0); + file.tag()->setTitle(titleAfter); + file.tag()->setComment(newComment); + CPPUNIT_ASSERT(file.save()); + } + testRead(copy.fileName().c_str(), titleAfter, commentAfter); + CPPUNIT_ASSERT(fileEqual( + copy.fileName(), + TEST_FILE_PATH_C("changed.mod"))); + } + +private: + void testRead(FileName fileName, const String &title, const String &comment) + { + Mod::File file(fileName); + + CPPUNIT_ASSERT(file.isValid()); + + Mod::Properties *p = file.audioProperties(); + Mod::Tag *t = file.tag(); + + CPPUNIT_ASSERT(0 != p); + CPPUNIT_ASSERT(0 != t); + + CPPUNIT_ASSERT_EQUAL(0, p->length()); + CPPUNIT_ASSERT_EQUAL(0, p->bitrate()); + CPPUNIT_ASSERT_EQUAL(0, p->sampleRate()); + CPPUNIT_ASSERT_EQUAL(8, p->channels()); + CPPUNIT_ASSERT_EQUAL(31U, p->instrumentCount()); + CPPUNIT_ASSERT_EQUAL((uchar)1, p->lengthInPatterns()); + CPPUNIT_ASSERT_EQUAL(title, t->title()); + CPPUNIT_ASSERT_EQUAL(String::null, t->artist()); + CPPUNIT_ASSERT_EQUAL(String::null, t->album()); + CPPUNIT_ASSERT_EQUAL(comment, t->comment()); + CPPUNIT_ASSERT_EQUAL(String::null, t->genre()); + CPPUNIT_ASSERT_EQUAL(0U, t->year()); + CPPUNIT_ASSERT_EQUAL(0U, t->track()); + CPPUNIT_ASSERT_EQUAL(String("StarTrekker"), t->trackerName()); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(TestMod); diff --git a/tests/test_s3m.cpp b/tests/test_s3m.cpp new file mode 100644 index 00000000..e2566626 --- /dev/null +++ b/tests/test_s3m.cpp @@ -0,0 +1,123 @@ +/*************************************************************************** + 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 <cppunit/extensions/HelperMacros.h> +#include <s3mfile.h> +#include "utils.h" + +using namespace std; +using namespace TagLib; + +static const String titleBefore("test song name"); +static const String titleAfter("changed title"); + +static const String commentBefore( + "This is an instrument name.\n" + "Module file formats\n" + "abuse instrument names\n" + "as multiline comments.\n" + " "); + +static const String newComment( + "This is an instrument name!\n" + "Module file formats\n" + "abuse instrument names\n" + "as multiline comments.\n" + "-----------------------------------\n" + "This line will be dropped and the previous is truncated."); + +static const String commentAfter( + "This is an instrument name!\n" + "Module file formats\n" + "abuse instrument names\n" + "as multiline comments.\n" + "---------------------------"); + +class TestS3M : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(TestS3M); + CPPUNIT_TEST(testReadTags); + CPPUNIT_TEST(testWriteTags); + CPPUNIT_TEST_SUITE_END(); + +public: + void testReadTags() + { + testRead(TEST_FILE_PATH_C("test.s3m"), titleBefore, commentBefore); + } + + void testWriteTags() + { + ScopedFileCopy copy("test", ".s3m"); + { + S3M::File file(copy.fileName().c_str()); + CPPUNIT_ASSERT(file.tag() != 0); + file.tag()->setTitle(titleAfter); + file.tag()->setComment(newComment); + file.tag()->setTrackerName("won't be saved"); + CPPUNIT_ASSERT(file.save()); + } + testRead(copy.fileName().c_str(), titleAfter, commentAfter); + CPPUNIT_ASSERT(fileEqual( + copy.fileName(), + TEST_FILE_PATH_C("changed.s3m"))); + } + +private: + void testRead(FileName fileName, const String &title, const String &comment) + { + S3M::File file(fileName); + + CPPUNIT_ASSERT(file.isValid()); + + S3M::Properties *p = file.audioProperties(); + Mod::Tag *t = file.tag(); + + CPPUNIT_ASSERT(0 != p); + CPPUNIT_ASSERT(0 != t); + + CPPUNIT_ASSERT_EQUAL( 0, p->length()); + CPPUNIT_ASSERT_EQUAL( 0, p->bitrate()); + CPPUNIT_ASSERT_EQUAL( 0, p->sampleRate()); + CPPUNIT_ASSERT_EQUAL(16, p->channels()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 0, p->lengthInPatterns()); + CPPUNIT_ASSERT_EQUAL(false, p->stereo()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 5, p->sampleCount()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 1, p->patternCount()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 0, p->flags()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort)4896, p->trackerVersion()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 2, p->fileFormatVersion()); + CPPUNIT_ASSERT_EQUAL((TagLib::uchar) 64, p->globalVolume()); + CPPUNIT_ASSERT_EQUAL((TagLib::uchar) 48, p->masterVolume()); + CPPUNIT_ASSERT_EQUAL((TagLib::uchar)125, p->tempo()); + CPPUNIT_ASSERT_EQUAL((TagLib::uchar) 6, p->bpmSpeed()); + CPPUNIT_ASSERT_EQUAL(title, t->title()); + CPPUNIT_ASSERT_EQUAL(String::null, t->artist()); + CPPUNIT_ASSERT_EQUAL(String::null, t->album()); + CPPUNIT_ASSERT_EQUAL(comment, t->comment()); + CPPUNIT_ASSERT_EQUAL(String::null, t->genre()); + CPPUNIT_ASSERT_EQUAL(0U, t->year()); + CPPUNIT_ASSERT_EQUAL(0U, t->track()); + CPPUNIT_ASSERT_EQUAL(String("ScreamTracker III"), t->trackerName()); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(TestS3M); diff --git a/tests/test_xm.cpp b/tests/test_xm.cpp new file mode 100644 index 00000000..c6af369e --- /dev/null +++ b/tests/test_xm.cpp @@ -0,0 +1,216 @@ +/*************************************************************************** + 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 <cppunit/extensions/HelperMacros.h> +#include <xmfile.h> +#include "utils.h" + +using namespace std; +using namespace TagLib; + +static const String titleBefore("title of song"); +static const String titleAfter("changed title"); + +static const String trackerNameBefore("MilkyTracker "); +static const String trackerNameAfter("TagLib"); + +static const String commentBefore( + "Instrument names\n" + "are abused as\n" + "comments in\n" + "module file formats.\n" + "-+-+-+-+-+-+-+-+-+-+-+\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "\n\n\n" + "Sample\n" + "names\n" + "are sometimes\n" + "also abused as\n" + "comments."); + +static const String newCommentShort( + "Instrument names\n" + "are abused as\n" + "comments in\n" + "module file formats.\n" + "======================\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "\n\n\n" + "Sample names\n" + "are sometimes\n" + "also abused as\n" + "comments."); + +static const String newCommentLong( + "Instrument names\n" + "are abused as\n" + "comments in\n" + "module file formats.\n" + "======================\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "\n\n\n" + "Sample names\n" + "are sometimes\n" + "also abused as\n" + "comments.\n" + "\n\n\n\n\n\n\n" + "TEST"); + +static const String commentAfter( + "Instrument names\n" + "are abused as\n" + "comments in\n" + "module file formats.\n" + "======================\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "\n\n\n" + "Sample names\n" + "are sometimes\n" + "also abused as\n" + "comments.\n"); + +class TestXM : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(TestXM); + CPPUNIT_TEST(testReadTags); + CPPUNIT_TEST(testReadStrippedTags); + CPPUNIT_TEST(testWriteTagsShort); + CPPUNIT_TEST(testWriteTagsLong); + CPPUNIT_TEST_SUITE_END(); + +public: + void testReadTags() + { + testRead(TEST_FILE_PATH_C("test.xm"), titleBefore, + commentBefore, trackerNameBefore); + } + + void testReadStrippedTags() + { + XM::File file(TEST_FILE_PATH_C("stripped.xm")); + CPPUNIT_ASSERT(file.isValid()); + + XM::Properties *p = file.audioProperties(); + Mod::Tag *t = file.tag(); + + CPPUNIT_ASSERT(0 != p); + CPPUNIT_ASSERT(0 != t); + + CPPUNIT_ASSERT_EQUAL(0, p->length()); + CPPUNIT_ASSERT_EQUAL(0, p->bitrate()); + CPPUNIT_ASSERT_EQUAL(0, p->sampleRate()); + CPPUNIT_ASSERT_EQUAL(8, p->channels()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 1, p->lengthInPatterns()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 0, p->version()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 0 , p->restartPosition()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 1, p->patternCount()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 0, p->instrumentCount()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 1, p->flags()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 6, p->tempo()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort)125, p->bpmSpeed()); + CPPUNIT_ASSERT_EQUAL(titleBefore, t->title()); + CPPUNIT_ASSERT_EQUAL(String::null, t->artist()); + CPPUNIT_ASSERT_EQUAL(String::null, t->album()); + CPPUNIT_ASSERT_EQUAL(String::null, t->comment()); + CPPUNIT_ASSERT_EQUAL(String::null, t->genre()); + CPPUNIT_ASSERT_EQUAL(0U, t->year()); + CPPUNIT_ASSERT_EQUAL(0U, t->track()); + CPPUNIT_ASSERT_EQUAL(String::null, t->trackerName()); + } + + void testWriteTagsShort() + { + testWriteTags(newCommentShort); + } + + void testWriteTagsLong() + { + testWriteTags(newCommentLong); + } + +private: + void testRead(FileName fileName, const String &title, + const String &comment, const String &trackerName) + { + XM::File file(fileName); + + CPPUNIT_ASSERT(file.isValid()); + + XM::Properties *p = file.audioProperties(); + Mod::Tag *t = file.tag(); + + CPPUNIT_ASSERT(0 != p); + CPPUNIT_ASSERT(0 != t); + + CPPUNIT_ASSERT_EQUAL(0, p->length()); + CPPUNIT_ASSERT_EQUAL(0, p->bitrate()); + CPPUNIT_ASSERT_EQUAL(0, p->sampleRate()); + CPPUNIT_ASSERT_EQUAL(8, p->channels()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 1, p->lengthInPatterns()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort)260, p->version()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 0, p->restartPosition()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 1, p->patternCount()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort)128, p->instrumentCount()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 1, p->flags()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort) 6, p->tempo()); + CPPUNIT_ASSERT_EQUAL((TagLib::ushort)125, p->bpmSpeed()); + CPPUNIT_ASSERT_EQUAL(title, t->title()); + CPPUNIT_ASSERT_EQUAL(String::null, t->artist()); + CPPUNIT_ASSERT_EQUAL(String::null, t->album()); + CPPUNIT_ASSERT_EQUAL(comment, t->comment()); + CPPUNIT_ASSERT_EQUAL(String::null, t->genre()); + CPPUNIT_ASSERT_EQUAL(0U, t->year()); + CPPUNIT_ASSERT_EQUAL(0U, t->track()); + CPPUNIT_ASSERT_EQUAL(trackerName, t->trackerName()); + } + + void testWriteTags(const String &comment) + { + ScopedFileCopy copy("test", ".xm"); + { + XM::File file(copy.fileName().c_str()); + CPPUNIT_ASSERT(file.tag() != 0); + file.tag()->setTitle(titleAfter); + file.tag()->setComment(comment); + file.tag()->setTrackerName(trackerNameAfter); + CPPUNIT_ASSERT(file.save()); + } + testRead(copy.fileName().c_str(), titleAfter, + commentAfter, trackerNameAfter); + CPPUNIT_ASSERT(fileEqual( + copy.fileName(), + TEST_FILE_PATH_C("changed.xm"))); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(TestXM); diff --git a/tests/utils.h b/tests/utils.h index 57226efc..39e15ce9 100644 --- a/tests/utils.h +++ b/tests/utils.h @@ -9,7 +9,9 @@ #include <sys/fcntl.h> #endif #include <stdio.h> +#include <string.h> #include <string> +#include <fstream> using namespace std; @@ -45,6 +47,35 @@ inline void deleteFile(const string &filename) remove(filename.c_str()); } +inline bool fileEqual(const string &filename1, const string &filename2) +{ + char buf1[BUFSIZ]; + char buf2[BUFSIZ]; + + ifstream stream1(filename1.c_str(), ios_base::in | ios_base::binary); + ifstream stream2(filename2.c_str(), ios_base::in | ios_base::binary); + + if(!stream1 && !stream2) return true; + if(!stream1 || !stream2) return false; + + for(;;) + { + stream1.read(buf1, BUFSIZ); + stream2.read(buf2, BUFSIZ); + + streamsize n1 = stream1.gcount(); + streamsize n2 = stream2.gcount(); + + if(n1 != n2) return false; + + if(n1 == 0) break; + + if(memcmp(buf1, buf2, n1) != 0) return false; + } + + return stream1.good() == stream2.good(); +} + class ScopedFileCopy { public: |