From 9c8e36d3bedc7152783a43cb10b1ea73edd40fcb Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Wed, 27 May 2015 18:25:32 +0900 Subject: [PATCH] WAV: Move property parsing code to Properties. Make use of 'fact' chunk to get the number of total samples. --- taglib/riff/wav/wavfile.cpp | 29 ++-------------- taglib/riff/wav/wavfile.h | 2 ++ taglib/riff/wav/wavproperties.cpp | 55 ++++++++++++++++++++++++------- taglib/riff/wav/wavproperties.h | 11 +++---- 4 files changed, 53 insertions(+), 44 deletions(-) diff --git a/taglib/riff/wav/wavfile.cpp b/taglib/riff/wav/wavfile.cpp index 4d2b4196..c1c2decb 100644 --- a/taglib/riff/wav/wavfile.cpp +++ b/taglib/riff/wav/wavfile.cpp @@ -191,10 +191,7 @@ bool RIFF::WAV::File::hasInfoTag() const void RIFF::WAV::File::read(bool readProperties, Properties::ReadStyle propertiesStyle) { - ByteVector formatData; - uint streamLength = 0; - uint totalSamples = 0; - for(uint i = 0; i < chunkCount(); i++) { + for(uint i = 0; i < chunkCount(); ++i) { const ByteVector name = chunkName(i); if(name == "ID3 " || name == "id3 ") { if(!d->tag[ID3v2Index]) { @@ -218,26 +215,6 @@ void RIFF::WAV::File::read(bool readProperties, Properties::ReadStyle properties } } } - else if(readProperties) { - if(name == "fmt ") { - if(formatData.isEmpty()) - formatData = chunkData(i); - else - debug("RIFF::WAV::File::read() - Duplicate 'fmt ' chunk found."); - } - else if(name == "data") { - if(streamLength == 0) - streamLength = chunkDataSize(i) + chunkPadding(i); - else - debug("RIFF::WAV::File::read() - Duplicate 'data' chunk found."); - } - else if(name == "fact") { - if(totalSamples == 0) - totalSamples = chunkData(i).toUInt(0, false); - else - debug("RIFF::WAV::File::read() - Duplicate 'fact' chunk found."); - } - } } if(!d->tag[ID3v2Index]) @@ -246,8 +223,8 @@ void RIFF::WAV::File::read(bool readProperties, Properties::ReadStyle properties if(!d->tag[InfoIndex]) d->tag.set(InfoIndex, new RIFF::Info::Tag); - if(!formatData.isEmpty()) - d->properties = new Properties(formatData, streamLength, totalSamples, propertiesStyle); + if(readProperties) + d->properties = new Properties(this, propertiesStyle); } void RIFF::WAV::File::strip(TagTypes tags) diff --git a/taglib/riff/wav/wavfile.h b/taglib/riff/wav/wavfile.h index d8186a69..5cafe76f 100644 --- a/taglib/riff/wav/wavfile.h +++ b/taglib/riff/wav/wavfile.h @@ -179,6 +179,8 @@ namespace TagLib { */ uint findInfoTagChunk(); + friend class Properties; + class FilePrivate; FilePrivate *d; }; diff --git a/taglib/riff/wav/wavproperties.cpp b/taglib/riff/wav/wavproperties.cpp index e61973ab..48f068c7 100644 --- a/taglib/riff/wav/wavproperties.cpp +++ b/taglib/riff/wav/wavproperties.cpp @@ -23,12 +23,9 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#include "wavproperties.h" - -#include #include -#include -#include +#include "wavfile.h" +#include "wavproperties.h" using namespace TagLib; @@ -64,18 +61,18 @@ RIFF::WAV::Properties::Properties(const ByteVector & /*data*/, ReadStyle style) debug("RIFF::WAV::Properties::Properties() -- This constructor is no longer used."); } -RIFF::WAV::Properties::Properties(const ByteVector &data, uint streamLength, ReadStyle style) : +RIFF::WAV::Properties::Properties(const ByteVector & /*data*/, uint /*streamLength*/, ReadStyle style) : AudioProperties(style), d(new PropertiesPrivate()) { debug("RIFF::WAV::Properties::Properties() -- This constructor is no longer used."); } -TagLib::RIFF::WAV::Properties::Properties(const ByteVector &data, uint streamLength, uint totalSamples, ReadStyle style) : +TagLib::RIFF::WAV::Properties::Properties(File *file, ReadStyle style) : AudioProperties(style), d(new PropertiesPrivate()) { - read(data, streamLength, totalSamples); + read(file); } RIFF::WAV::Properties::~Properties() @@ -137,14 +134,50 @@ int RIFF::WAV::Properties::format() const // private members //////////////////////////////////////////////////////////////////////////////// -void RIFF::WAV::Properties::read(const ByteVector &data, uint streamLength, uint totalSamples) +void RIFF::WAV::Properties::read(File *file) { + ByteVector data; + uint streamLength = 0; + uint totalSamples = 0; + + for(uint i = 0; i < file->chunkCount(); ++i) { + const ByteVector name = file->chunkName(i); + if(name == "fmt ") { + if(data.isEmpty()) + data = file->chunkData(i); + else + debug("RIFF::WAV::Properties::read() - Duplicate 'fmt ' chunk found."); + } + else if(name == "data") { + if(streamLength == 0) + streamLength = file->chunkDataSize(i) + file->chunkPadding(i); + else + debug("RIFF::WAV::Properties::read() - Duplicate 'data' chunk found."); + } + else if(name == "fact") { + if(totalSamples == 0) + totalSamples = file->chunkData(i).toUInt(0, false); + else + debug("RIFF::WAV::Properties::read() - Duplicate 'fact' chunk found."); + } + } + if(data.size() < 16) { - debug("RIFF::WAV::Properties::read() - \"fmt \" chunk is too short for WAV."); + debug("RIFF::WAV::Properties::read() - 'fmt ' chunk not found or too short."); + return; + } + + if(streamLength == 0) { + debug("RIFF::WAV::Properties::read() - 'data' chunk not found."); + return; + } + + d->format = data.toShort(0, false); + if(d->format != 1 && totalSamples == 0) { + debug("RIFF::WAV::Properties::read() - Non-PCM format, but 'fact' chunk not found."); return; } - d->format = data.toShort(0, false); d->channels = data.toShort(2, false); d->sampleRate = data.toUInt(4, false); d->bitsPerSample = data.toShort(14, false); diff --git a/taglib/riff/wav/wavproperties.h b/taglib/riff/wav/wavproperties.h index 12367eb2..ab1b9e70 100644 --- a/taglib/riff/wav/wavproperties.h +++ b/taglib/riff/wav/wavproperties.h @@ -67,12 +67,9 @@ namespace TagLib { /*! * Create an instance of WAV::Properties with the data read from the - * ByteVector \a data and the length calculated using \a streamLength - * and \a totalSamples. - * - * \note totalSamples can be zero if the file doesn't have a 'fact' chunk. + * WAV::File \a file. */ - Properties(const ByteVector &data, uint streamLength, uint totalSamples, ReadStyle style); + Properties(File *file, ReadStyle style); /*! * Destroys this WAV::Properties instance. @@ -144,7 +141,7 @@ namespace TagLib { * Returns the format ID of the file. * 0 for unknown, 1 for PCM, 2 for ADPCM, 3 for 32/64-bit IEEE754, and so forth. * - * \note For further information, refer to \a mmreg.h in Windows Media SDK. + * \note For further information, refer to RFC 2361. */ int format() const; @@ -152,7 +149,7 @@ namespace TagLib { Properties(const Properties &); Properties &operator=(const Properties &); - void read(const ByteVector &data, uint streamLength, uint totalSamples); + void read(File *file); class PropertiesPrivate; PropertiesPrivate *d;