]> granicus.if.org Git - taglib/commitdiff
WAV: Move property parsing code to Properties.
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Wed, 27 May 2015 09:25:32 +0000 (18:25 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Thu, 18 Jun 2015 08:47:39 +0000 (17:47 +0900)
Make use of 'fact' chunk to get the number of total samples.

taglib/riff/wav/wavfile.cpp
taglib/riff/wav/wavfile.h
taglib/riff/wav/wavproperties.cpp
taglib/riff/wav/wavproperties.h

index 4d2b41967282cce1f38483a410cc5123a81c095b..c1c2decbb94779b92841b9054c8c21ffabf5e655 100644 (file)
@@ -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)
index d8186a6988e92700cc009e7fc968a226de4d31db..5cafe76f6de4a4f3961bdad46d3a1a4883e47936 100644 (file)
@@ -179,6 +179,8 @@ namespace TagLib {
          */
         uint findInfoTagChunk();
 
+        friend class Properties;
+
         class FilePrivate;
         FilePrivate *d;
       };
index e61973ab1bb801d5e5fd8d6eee33695f55740fe1..48f068c757560e03c273f31311e73ef5d3e5bee4 100644 (file)
  *   http://www.mozilla.org/MPL/                                           *
  ***************************************************************************/
 
-#include "wavproperties.h"
-
-#include <tstring.h>
 #include <tdebug.h>
-#include <cmath>
-#include <math.h>
+#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);
index 12367eb280650886c54b78a11c573cd449440779..ab1b9e70100b9db46da1ae1a44afb610ac4e7062 100644 (file)
@@ -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;