]> granicus.if.org Git - taglib/commitdiff
Added sampleFrames() to audio properties
authorStephen F. Booth <me@sbooth.org>
Fri, 6 Apr 2012 22:30:13 +0000 (18:30 -0400)
committerStephen F. Booth <me@sbooth.org>
Fri, 6 Apr 2012 22:30:13 +0000 (18:30 -0400)
taglib/ape/apeproperties.cpp
taglib/ape/apeproperties.h
taglib/mpc/mpcproperties.cpp
taglib/mpc/mpcproperties.h
taglib/wavpack/wavpackproperties.cpp
taglib/wavpack/wavpackproperties.h

index 4f14d6c5eca4ee38afa146fa897e8af19e48485a..cb81e557c0b383ceae5ef1e5f10cc3036ca5ffb2 100644 (file)
@@ -46,6 +46,7 @@ public:
     channels(0),
     version(0),
     bitsPerSample(0),
+    sampleFrames(0),
     file(file),
     streamLength(streamLength) {}
 
@@ -55,6 +56,7 @@ public:
   int channels;
   int version;
   int bitsPerSample;
+  uint sampleFrames;
   File *file;
   long streamLength;
 };
@@ -104,6 +106,11 @@ int APE::Properties::bitsPerSample() const
   return d->bitsPerSample;
 }
 
+uint APE::Properties::sampleFrames() const
+{
+  return d->sampleFrames;
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // private members
 ////////////////////////////////////////////////////////////////////////////////
@@ -192,8 +199,8 @@ void APE::Properties::analyzeCurrent()
   uint totalFrames = header.mid(12, 4).toUInt(false);
   uint blocksPerFrame = header.mid(4, 4).toUInt(false);
   uint finalFrameBlocks = header.mid(8, 4).toUInt(false);
-  uint totalBlocks = totalFrames > 0 ? (totalFrames -  1) * blocksPerFrame + finalFrameBlocks : 0;
-  d->length = d->sampleRate > 0 ? totalBlocks / d->sampleRate : 0;
+  d->sampleFrames = totalFrames > 0 ? (totalFrames -  1) * blocksPerFrame + finalFrameBlocks : 0;
+  d->length = d->sampleRate > 0 ? d->sampleFrames / d->sampleRate : 0;
   d->bitrate = d->length > 0 ? ((d->streamLength * 8L) / d->length) / 1000 : 0;
 }
 
index 8b543a57268b715deb67ade292b053b4eb68fe7c..f154ec34249b9b4c93c9385efbcd778e8ee33af8 100644 (file)
@@ -71,6 +71,7 @@ namespace TagLib {
        * Returns number of bits per sample.
        */
       int bitsPerSample() const;
+      uint sampleFrames() const;
 
       /*!
        * Returns APE version.
index 9adc692465be512c931a8d38eff4ce51c468ecc2..3ce44cc2bb0b8dbea01b7483952e8154c2382884 100644 (file)
@@ -43,7 +43,9 @@ public:
     length(0),
     bitrate(0),
     sampleRate(0),
-    channels(0) {}
+    channels(0),
+    totalFrames(0),
+    sampleFrames(0) {}
 
   ByteVector data;
   long streamLength;
@@ -53,6 +55,8 @@ public:
   int bitrate;
   int sampleRate;
   int channels;
+  uint totalFrames;
+  uint sampleFrames;
 };
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -95,6 +99,16 @@ int MPC::Properties::mpcVersion() const
   return d->version;
 }
 
+uint MPC::Properties::totalFrames() const
+{
+  return d->totalFrames;
+}
+
+uint MPC::Properties::sampleFrames() const
+{
+  return d->sampleFrames;
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // private members
 ////////////////////////////////////////////////////////////////////////////////
@@ -108,14 +122,21 @@ void MPC::Properties::read()
 
   d->version = d->data[3] & 15;
 
-  unsigned int frames;
-
   if(d->version >= 7) {
-    frames = d->data.mid(4, 4).toUInt(false);
+    d->totalFrames = d->data.mid(4, 4).toUInt(false);
 
     std::bitset<32> flags(TAGLIB_CONSTRUCT_BITSET(d->data.mid(8, 4).toUInt(false)));
     d->sampleRate = sftable[flags[17] * 2 + flags[16]];
     d->channels = 2;
+
+    uint gapless = d->data.mid(5, 4).toUInt(false);
+    bool trueGapless = (gapless >> 31) & 0x0001;
+    if(trueGapless) {
+      uint lastFrameSamples = (gapless >> 20) & 0x07FF;
+      d->sampleFrames = d->totalFrames * 1152 - lastFrameSamples;
+    }
+    else
+      d->sampleFrames = d->totalFrames * 1152 - 576;
   }
   else {
     uint headerData = d->data.mid(0, 4).toUInt(false);
@@ -126,14 +147,14 @@ void MPC::Properties::read()
     d->channels = 2;
 
     if(d->version >= 5)
-      frames = d->data.mid(4, 4).toUInt(false);
+      d->totalFrames = d->data.mid(4, 4).toUInt(false);
     else
-      frames = d->data.mid(6, 2).toUInt(false);
-  }
+      d->totalFrames = d->data.mid(6, 2).toUInt(false);
 
-  uint samples = frames * 1152 - 576;
+    d->sampleFrames = d->totalFrames * 1152 - 576;
+  }
 
-  d->length = d->sampleRate > 0 ? (samples + (d->sampleRate / 2)) / d->sampleRate : 0;
+  d->length = d->sampleRate > 0 ? (d->sampleFrames + (d->sampleRate / 2)) / d->sampleRate : 0;
 
   if(!d->bitrate)
     d->bitrate = d->length > 0 ? ((d->streamLength * 8L) / d->length) / 1000 : 0;
index d1593458f754723e7ef520f1ac961b46409c917f..8225306f89c8231bdfcc66e12ab1c5f0186a9c04 100644 (file)
@@ -69,6 +69,8 @@ namespace TagLib {
        * Returns the version of the bitstream (SV4-SV7)
        */
       int mpcVersion() const;
+      uint totalFrames() const;
+      uint sampleFrames() const;
 
     private:
       Properties(const Properties &);
index 52552a0d064337a256538e7640a8afa620dfa671..53c6d7635fe92fa5c8f433e154ec1261823b9db7 100644 (file)
@@ -48,6 +48,7 @@ public:
     channels(0),
     version(0),
     bitsPerSample(0),
+    sampleFrames(0),
     file(0) {}
 
   ByteVector data;
@@ -59,6 +60,7 @@ public:
   int channels;
   int version;
   int bitsPerSample;
+  uint sampleFrames;
   File *file;
 };
 
@@ -115,6 +117,11 @@ int WavPack::Properties::bitsPerSample() const
   return d->bitsPerSample;
 }
 
+uint WavPack::Properties::sampleFrames() const
+{
+  return d->sampleFrames;
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // private members
 ////////////////////////////////////////////////////////////////////////////////
@@ -161,6 +168,7 @@ void WavPack::Properties::read()
     }
   }
   d->length = d->sampleRate > 0 ? (samples + (d->sampleRate / 2)) / d->sampleRate : 0;
+  d->sampleFrames = samples;
 
   d->bitrate = d->length > 0 ? ((d->streamLength * 8L) / d->length) / 1000 : 0;
 }
index 74d18ea876531e021aca6af701316cc8cf9e1342..bd2209da2099f241ff123c6335dfd90af9ed9011 100644 (file)
@@ -82,6 +82,7 @@ namespace TagLib {
        * Returns number of bits per sample.
        */
       int bitsPerSample() const;
+      uint sampleFrames() const;
 
       /*!
        * Returns WavPack version.