From 75579dc0ddd90868d752a530b11e338da7675d65 Mon Sep 17 00:00:00 2001 From: Scott Wheeler Date: Thu, 19 Jun 2008 11:15:39 +0000 Subject: [PATCH] Provisional .wav support. Tag writing will probably be disabled by default... git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@822160 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- taglib/CMakeLists.txt | 1 + taglib/Makefile.am | 1 + taglib/fileref.cpp | 4 ++ taglib/riff/CMakeLists.txt | 1 + taglib/riff/Makefile.am | 4 +- taglib/riff/wav/CMakeLists.txt | 1 + taglib/riff/wav/Makefile.am | 13 ++++ taglib/riff/wav/wavfile.cpp | 108 ++++++++++++++++++++++++++++++ taglib/riff/wav/wavfile.h | 102 ++++++++++++++++++++++++++++ taglib/riff/wav/wavproperties.cpp | 104 ++++++++++++++++++++++++++++ taglib/riff/wav/wavproperties.h | 82 +++++++++++++++++++++++ 11 files changed, 419 insertions(+), 2 deletions(-) create mode 100644 taglib/riff/wav/CMakeLists.txt create mode 100644 taglib/riff/wav/Makefile.am create mode 100644 taglib/riff/wav/wavfile.cpp create mode 100644 taglib/riff/wav/wavfile.h create mode 100644 taglib/riff/wav/wavproperties.cpp create mode 100644 taglib/riff/wav/wavproperties.h diff --git a/taglib/CMakeLists.txt b/taglib/CMakeLists.txt index e7cb7452..98f11bfd 100644 --- a/taglib/CMakeLists.txt +++ b/taglib/CMakeLists.txt @@ -16,6 +16,7 @@ INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/trueaudio ${CMAKE_CURRENT_SOURCE_DIR}/riff ${CMAKE_CURRENT_SOURCE_DIR}/riff/aiff + ${CMAKE_CURRENT_SOURCE_DIR}/riff/wav ${CMAKE_CURRENT_BINARY_DIR}/taglib ${CMAKE_CURRENT_BINARY_DIR}/.. ) diff --git a/taglib/Makefile.am b/taglib/Makefile.am index 5a997a80..a75a5495 100644 --- a/taglib/Makefile.am +++ b/taglib/Makefile.am @@ -14,6 +14,7 @@ INCLUDES = \ -I$(top_srcdir)/taglib/trueaudio \ -I$(top_srcdir)/taglib/riff \ -I$(top_srcdir)/taglib/riff/aiff \ + -I$(top_srcdir)/taglib/riff/wav \ -I$(top_srcdir)/taglib/mpeg/id3v2 \ $(all_includes) diff --git a/taglib/fileref.cpp b/taglib/fileref.cpp index 0fc977a3..ce6f649b 100644 --- a/taglib/fileref.cpp +++ b/taglib/fileref.cpp @@ -36,6 +36,7 @@ #include "speexfile.h" #include "trueaudiofile.h" #include "aifffile.h" +#include "wavfile.h" using namespace TagLib; @@ -124,6 +125,7 @@ StringList FileRef::defaultFileExtensions() l.append("tta"); l.append("aif"); l.append("aiff"); + l.append("wav"); return l; } @@ -202,6 +204,8 @@ File *FileRef::create(FileName fileName, bool readAudioProperties, return new TrueAudio::File(fileName, readAudioProperties, audioPropertiesStyle); if(s.substr(s.size() - 4, 4).upper() == ".AIF") return new RIFF::AIFF::File(fileName, readAudioProperties, audioPropertiesStyle); + if(s.substr(s.size() - 4, 4).upper() == ".WAV") + return new RIFF::WAV::File(fileName, readAudioProperties, audioPropertiesStyle); } if(s.size() > 5) { if(s.substr(s.size() - 5, 5).upper() == ".AIFF") diff --git a/taglib/riff/CMakeLists.txt b/taglib/riff/CMakeLists.txt index 56c35d96..f691ee41 100644 --- a/taglib/riff/CMakeLists.txt +++ b/taglib/riff/CMakeLists.txt @@ -1,3 +1,4 @@ ADD_SUBDIRECTORY( aiff ) +ADD_SUBDIRECTORY( wav ) INSTALL( FILES rifffile.h DESTINATION ${INCLUDE_INSTALL_DIR}/taglib) diff --git a/taglib/riff/Makefile.am b/taglib/riff/Makefile.am index f7bbacb9..5ef736b4 100644 --- a/taglib/riff/Makefile.am +++ b/taglib/riff/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS = aiff +SUBDIRS = aiff wav INCLUDES = \ -I$(top_srcdir)/taglib \ @@ -12,4 +12,4 @@ libriff_la_SOURCES = rifffile.cpp taglib_include_HEADERS = rifffile.h taglib_includedir = $(includedir)/taglib -libriff_la_LIBADD = ./aiff/libaiff.la +libriff_la_LIBADD = ./aiff/libaiff.la ./wav/libwav.la diff --git a/taglib/riff/wav/CMakeLists.txt b/taglib/riff/wav/CMakeLists.txt new file mode 100644 index 00000000..c011579c --- /dev/null +++ b/taglib/riff/wav/CMakeLists.txt @@ -0,0 +1 @@ +INSTALL( FILES wavfile.h wavproperties.h DESTINATION ${INCLUDE_INSTALL_DIR}/taglib) diff --git a/taglib/riff/wav/Makefile.am b/taglib/riff/wav/Makefile.am new file mode 100644 index 00000000..767c29f4 --- /dev/null +++ b/taglib/riff/wav/Makefile.am @@ -0,0 +1,13 @@ +INCLUDES = \ + -I$(top_srcdir)/taglib \ + -I$(top_srcdir)/taglib/toolkit \ + -I$(top_srcdir)/taglib/riff \ + -I$(top_srcdir)/taglib/mpeg/id3v2 \ + $(all_includes) + +noinst_LTLIBRARIES = libwav.la + +libwav_la_SOURCES = wavfile.cpp wavproperties.cpp + +taglib_include_HEADERS = wavfile.h wavproperties.h +taglib_includedir = $(includedir)/taglib diff --git a/taglib/riff/wav/wavfile.cpp b/taglib/riff/wav/wavfile.cpp new file mode 100644 index 00000000..b2123ad5 --- /dev/null +++ b/taglib/riff/wav/wavfile.cpp @@ -0,0 +1,108 @@ +/*************************************************************************** + copyright : (C) 2008 by Scott Wheeler + email : wheeler@kde.org + ***************************************************************************/ + +/*************************************************************************** + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * + * USA * + * * + * Alternatively, this file is available under the Mozilla Public * + * License Version 1.1. You may obtain a copy of the License at * + * http://www.mozilla.org/MPL/ * + ***************************************************************************/ + +#include +#include +#include + +#include "wavfile.h" + +using namespace TagLib; + +class RIFF::WAV::File::FilePrivate +{ +public: + FilePrivate() : + properties(0), + tag(0) + { + + } + + ~FilePrivate() + { + delete properties; + delete tag; + } + + Properties *properties; + ID3v2::Tag *tag; +}; + +//////////////////////////////////////////////////////////////////////////////// +// public members +//////////////////////////////////////////////////////////////////////////////// + +RIFF::WAV::File::File(FileName file, bool readProperties, + Properties::ReadStyle propertiesStyle) : RIFF::File(file, LittleEndian) +{ + d = new FilePrivate; + if(isOpen()) + read(readProperties, propertiesStyle); +} + +RIFF::WAV::File::~File() +{ + delete d; +} + +ID3v2::Tag *RIFF::WAV::File::tag() const +{ + return d->tag; +} + +RIFF::WAV::Properties *RIFF::WAV::File::audioProperties() const +{ + return d->properties; +} + +bool RIFF::WAV::File::save() +{ + if(readOnly()) { + debug("RIFF::WAV::File::save() -- File is read only."); + return false; + } + + setChunkData("ID3 ", d->tag->render()); + + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +// private members +//////////////////////////////////////////////////////////////////////////////// + +void RIFF::WAV::File::read(bool readProperties, Properties::ReadStyle propertiesStyle) +{ + for(uint i = 0; i < chunkCount(); i++) { + if(chunkName(i) == "ID3 ") + d->tag = new ID3v2::Tag(this, chunkOffset(i)); + else if(chunkName(i) == "fmt " && readProperties) + d->properties = new Properties(chunkData(i), propertiesStyle); + } + + if(!d->tag) + d->tag = new ID3v2::Tag; +} diff --git a/taglib/riff/wav/wavfile.h b/taglib/riff/wav/wavfile.h new file mode 100644 index 00000000..6c36a3c2 --- /dev/null +++ b/taglib/riff/wav/wavfile.h @@ -0,0 +1,102 @@ +/*************************************************************************** + copyright : (C) 2008 by Scott Wheeler + email : wheeler@kde.org +***************************************************************************/ + +/*************************************************************************** + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * + * USA * + * * + * Alternatively, this file is available under the Mozilla Public * + * License Version 1.1. You may obtain a copy of the License at * + * http://www.mozilla.org/MPL/ * + ***************************************************************************/ + +#ifndef TAGLIB_WAVFILE_H +#define TAGLIB_WAVFILE_H + +#include "rifffile.h" +#include "id3v2tag.h" +#include "wavproperties.h" + +namespace TagLib { + + namespace RIFF { + + //! An implementation of WAV metadata + + /*! + * This is implementation of WAV metadata. + * + * This supports an ID3v2 tag as well as reading stream from the ID3 RIFF + * chunk as well as properties from the file. + */ + + namespace WAV { + + //! An implementation of TagLib::File with WAV specific methods + + /*! + * This implements and provides an interface for WAV files to the + * TagLib::Tag and TagLib::AudioProperties interfaces by way of implementing + * the abstract TagLib::File API as well as providing some additional + * information specific to WAV files. + */ + + class TAGLIB_EXPORT File : public TagLib::RIFF::File + { + public: + /*! + * Contructs an WAV 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, + Properties::ReadStyle propertiesStyle = Properties::Average); + + /*! + * Destroys this instance of the File. + */ + virtual ~File(); + + /*! + * Returns the Tag for this file. + */ + virtual ID3v2::Tag *tag() const; + + /*! + * Returns the WAV::Properties for this file. If no audio properties + * were read then this will return a null pointer. + */ + virtual Properties *audioProperties() const; + + /*! + * Saves the file. + */ + virtual bool save(); + + private: + File(const File &); + File &operator=(const File &); + + void read(bool readProperties, Properties::ReadStyle propertiesStyle); + + class FilePrivate; + FilePrivate *d; + }; + } + } +} + +#endif diff --git a/taglib/riff/wav/wavproperties.cpp b/taglib/riff/wav/wavproperties.cpp new file mode 100644 index 00000000..18920dd2 --- /dev/null +++ b/taglib/riff/wav/wavproperties.cpp @@ -0,0 +1,104 @@ +/*************************************************************************** + copyright : (C) 2008 by Scott Wheeler + email : wheeler@kde.org + ***************************************************************************/ + +/*************************************************************************** + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * + * USA * + * * + * Alternatively, this file is available under the Mozilla Public * + * License Version 1.1. You may obtain a copy of the License at * + * http://www.mozilla.org/MPL/ * + ***************************************************************************/ + +#include "wavproperties.h" + +#include +#include +#include +#include + +using namespace TagLib; + +class RIFF::WAV::Properties::PropertiesPrivate +{ +public: + PropertiesPrivate() : + format(0), + length(0), + bitrate(0), + sampleRate(0), + channels(0) + { + + } + + short format; + int length; + int bitrate; + int sampleRate; + int channels; +}; + +//////////////////////////////////////////////////////////////////////////////// +// public members +//////////////////////////////////////////////////////////////////////////////// + +RIFF::WAV::Properties::Properties(const ByteVector &data, ReadStyle style) : AudioProperties(style) +{ + d = new PropertiesPrivate; + read(data); +} + +RIFF::WAV::Properties::~Properties() +{ + delete d; +} + +int RIFF::WAV::Properties::length() const +{ + return d->length; +} + +int RIFF::WAV::Properties::bitrate() const +{ + return d->bitrate; +} + +int RIFF::WAV::Properties::sampleRate() const +{ + return d->sampleRate; +} + +int RIFF::WAV::Properties::channels() const +{ + return d->channels; +} + +//////////////////////////////////////////////////////////////////////////////// +// private members +//////////////////////////////////////////////////////////////////////////////// + +void RIFF::WAV::Properties::read(const ByteVector &data) +{ + d->format = data.mid(0, 2).toShort(false); + d->channels = data.mid(2, 2).toShort(false); + d->sampleRate = data.mid(4, 4).toUInt(false); + d->bitrate = data.mid(8, 4).toUInt(false) * 8 / 1024; + + // short bitsPerSample = data.mid(10, 2).toShort(); + // d->bitrate = (sampleRate * sampleSize * d->channels) / 1024.0; + // d->length = sampleFrames / d->sampleRate; +} diff --git a/taglib/riff/wav/wavproperties.h b/taglib/riff/wav/wavproperties.h new file mode 100644 index 00000000..531f877f --- /dev/null +++ b/taglib/riff/wav/wavproperties.h @@ -0,0 +1,82 @@ +/*************************************************************************** + copyright : (C) 2008 by Scott Wheeler + email : wheeler@kde.org +***************************************************************************/ + +/*************************************************************************** + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * + * USA * + * * + * Alternatively, this file is available under the Mozilla Public * + * License Version 1.1. You may obtain a copy of the License at * + * http://www.mozilla.org/MPL/ * + ***************************************************************************/ + +#ifndef TAGLIB_WAVPROPERTIES_H +#define TAGLIB_WAVPROPERTIES_H + +#include "audioproperties.h" + +namespace TagLib { + + class ByteVector; + + namespace RIFF { + + namespace WAV { + + class File; + + //! An implementation of audio property reading for WAV + + /*! + * This reads the data from an WAV stream found in the AudioProperties + * API. + */ + + class TAGLIB_EXPORT Properties : public AudioProperties + { + public: + /*! + * Create an instance of WAV::Properties with the data read from the + * ByteVector \a data. + */ + Properties(const ByteVector &data, ReadStyle style); + + /*! + * Destroys this WAV::Properties instance. + */ + virtual ~Properties(); + + // Reimplementations. + + virtual int length() const; + virtual int bitrate() const; + virtual int sampleRate() const; + virtual int channels() const; + + private: + Properties(const Properties &); + Properties &operator=(const Properties &); + + void read(const ByteVector &data); + + class PropertiesPrivate; + PropertiesPrivate *d; + }; + } + } +} + +#endif