]> granicus.if.org Git - taglib/commitdiff
AIFF: Calculate the actual average bitrate even if a file is compressed.
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Mon, 25 May 2015 01:29:14 +0000 (10:29 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Thu, 18 Jun 2015 08:38:41 +0000 (17:38 +0900)
Move property parsing code to Properties.

taglib/riff/aiff/aifffile.cpp
taglib/riff/aiff/aifffile.h
taglib/riff/aiff/aiffproperties.cpp
taglib/riff/aiff/aiffproperties.h
tests/test_aiff.cpp

index 6c136cde8ab6ec04554fa8abd458172df1692790..6a6a84e84d6fc5c32f5e8af58bd7f3d4ee68b7d6 100644 (file)
@@ -137,42 +137,23 @@ bool RIFF::AIFF::File::hasID3v2Tag() const
 
 void RIFF::AIFF::File::read(bool readProperties, Properties::ReadStyle propertiesStyle)
 {
-  ByteVector formatData;
-  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) {
-        d->tagChunkID = name;
         d->tag = new ID3v2::Tag(this, chunkOffset(i));
+        d->tagChunkID = name;
         d->hasID3v2 = true;
       }
       else {
         debug("RIFF::AIFF::File::read() - Duplicate ID3v2 tag found.");
       }
     }
-    else if(readProperties) {
-      if(name == "COMM") {
-        if(formatData.isEmpty()) {
-          formatData = chunkData(i);
-        }
-        else {
-          debug("RIFF::AIFF::File::read() - Duplicate 'COMM' chunk found.");
-        }
-      }
-      else if(name == "SSND") {
-        if(streamLength == 0) {
-          streamLength = chunkDataSize(i) + chunkPadding(i);
-        }
-        else {
-          debug("RIFF::AIFF::File::read() - Duplicate 'SSND' chunk found.");
-        }
-      }
-    }
   }
 
   if(!d->tag)
-    d->tag = new ID3v2::Tag;
+    d->tag = new ID3v2::Tag();
 
-  if(!formatData.isEmpty())
-    d->properties = new Properties(formatData, propertiesStyle);
+  if(readProperties)
+    d->properties = new Properties(this, propertiesStyle);
 }
index a35b4cf3dec3e35c8ac5dbce1d5b1876b731c56e..8a5b45d352b2ffd46269ac01a791e8b57224d3eb 100644 (file)
@@ -132,6 +132,8 @@ namespace TagLib {
 
         void read(bool readProperties, Properties::ReadStyle propertiesStyle);
 
+        friend class Properties;
+
         class FilePrivate;
         FilePrivate *d;
       };
index 8cb5de62ccfea7c58f9b795cca2ae4d04bbeb8a1..6112e0d8fb89a3ee4cfdb2e70b37e0564026b4da 100644 (file)
@@ -25,6 +25,7 @@
 
 #include <tstring.h>
 #include <tdebug.h>
+#include "aifffile.h"
 #include "aiffproperties.h"
 
 using namespace TagLib;
@@ -56,11 +57,18 @@ public:
 // public members
 ////////////////////////////////////////////////////////////////////////////////
 
-RIFF::AIFF::Properties::Properties(const ByteVector &data, ReadStyle style) :
+RIFF::AIFF::Properties::Properties(const ByteVector & /*data*/, ReadStyle style) :
   AudioProperties(style),
   d(new PropertiesPrivate())
 {
-  read(data);
+  debug("RIFF::AIFF::Properties::Properties() - This constructor is no longer used.");
+}
+
+RIFF::AIFF::Properties::Properties(File *file, ReadStyle style) :
+  AudioProperties(style),
+  d(new PropertiesPrivate())
+{
+  read(file);
 }
 
 RIFF::AIFF::Properties::~Properties()
@@ -127,14 +135,38 @@ String RIFF::AIFF::Properties::compressionName() const
 {
   return d->compressionName;
 }
+
 ////////////////////////////////////////////////////////////////////////////////
 // private members
 ////////////////////////////////////////////////////////////////////////////////
 
-void RIFF::AIFF::Properties::read(const ByteVector &data)
-{
+void RIFF::AIFF::Properties::read(File *file)
+{
+  ByteVector data;
+  uint streamLength = 0;
+  for(uint i = 0; i < file->chunkCount(); i++) {
+    const ByteVector name = file->chunkName(i);
+    if(name == "COMM") {
+      if(data.isEmpty())
+        data = file->chunkData(i);
+      else
+        debug("RIFF::AIFF::Properties::read() - Duplicate 'COMM' chunk found.");
+    }
+    else if(name == "SSND") {
+      if(streamLength == 0)
+        streamLength = file->chunkDataSize(i) + file->chunkPadding(i);
+      else
+        debug("RIFF::AIFF::Properties::read() - Duplicate 'SSND' chunk found.");
+    }
+  }
+
   if(data.size() < 18) {
-    debug("RIFF::AIFF::Properties::read() - \"COMM\" chunk is too short for AIFF.");
+    debug("RIFF::AIFF::Properties::read() - 'COMM' chunk not found or too short.");
+    return;
+  }
+
+  if(streamLength == 0) {
+    debug("RIFF::AIFF::Properties::read() - 'SSND' chunk not found.");
     return;
   }
 
@@ -143,10 +175,13 @@ void RIFF::AIFF::Properties::read(const ByteVector &data)
   d->bitsPerSample = data.toShort(6U);
 
   const long double sampleRate = data.toFloat80BE(8);
-  if(sampleRate >= 1.0) {
+  if(sampleRate >= 1.0)
     d->sampleRate = static_cast<int>(sampleRate + 0.5);
-    d->bitrate    = static_cast<int>(sampleRate * d->bitsPerSample * d->channels / 1000.0 + 0.5);
-    d->length     = static_cast<int>(d->sampleFrames * 1000.0 / sampleRate + 0.5);
+
+  if(d->sampleFrames > 0 && d->sampleRate > 0) {
+    const double length = d->sampleFrames * 1000.0 / d->sampleRate;
+    d->length  = static_cast<int>(length + 0.5);
+    d->bitrate = static_cast<int>(streamLength * 8.0 / length + 0.5);
   }
 
   if(data.size() >= 23) {
index 651592a7349857b42ce781bcb0421d9b29d83b0b..1d92ac8744a6bea7fdcad98b47f07e5c408dd572 100644 (file)
@@ -49,9 +49,17 @@ namespace TagLib {
         /*!
          * Create an instance of AIFF::Properties with the data read from the
          * ByteVector \a data.
+         *
+         * \deprecated
          */
         Properties(const ByteVector &data, ReadStyle style);
 
+        /*!
+         * Create an instance of AIFF::Properties with the data read from the
+         * AIFF::File \a file.
+         */
+        Properties(File *file, ReadStyle style);
+
         /*!
          * Destroys this AIFF::Properties instance.
          */
@@ -146,7 +154,7 @@ namespace TagLib {
         Properties(const Properties &);
         Properties &operator=(const Properties &);
 
-        void read(const ByteVector &data);
+        void read(File *file);
 
         class PropertiesPrivate;
         PropertiesPrivate *d;
index 360b75493bc831e868247c9995196036adebc9e7..386c7686608b4834fbdc14c59ca9a319ef227803 100644 (file)
@@ -45,7 +45,7 @@ public:
     CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->length());
     CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->lengthInSeconds());
     CPPUNIT_ASSERT_EQUAL(37, f.audioProperties()->lengthInMilliseconds());
-    CPPUNIT_ASSERT_EQUAL(706, f.audioProperties()->bitrate());
+    CPPUNIT_ASSERT_EQUAL(355, f.audioProperties()->bitrate());
     CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
     CPPUNIT_ASSERT_EQUAL(1, f.audioProperties()->channels());
     CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->bitsPerSample());