From 69ac59f5f0b5a9cd546b1aea7b162df8289eb12f Mon Sep 17 00:00:00 2001 From: "Stephen F. Booth" Date: Fri, 6 Apr 2012 18:30:13 -0400 Subject: [PATCH] Added sampleFrames() to audio properties --- taglib/ape/apeproperties.cpp | 11 ++++++-- taglib/ape/apeproperties.h | 1 + taglib/mpc/mpcproperties.cpp | 39 +++++++++++++++++++++------- taglib/mpc/mpcproperties.h | 2 ++ taglib/wavpack/wavpackproperties.cpp | 8 ++++++ taglib/wavpack/wavpackproperties.h | 1 + 6 files changed, 51 insertions(+), 11 deletions(-) diff --git a/taglib/ape/apeproperties.cpp b/taglib/ape/apeproperties.cpp index 4f14d6c5..cb81e557 100644 --- a/taglib/ape/apeproperties.cpp +++ b/taglib/ape/apeproperties.cpp @@ -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; } diff --git a/taglib/ape/apeproperties.h b/taglib/ape/apeproperties.h index 8b543a57..f154ec34 100644 --- a/taglib/ape/apeproperties.h +++ b/taglib/ape/apeproperties.h @@ -71,6 +71,7 @@ namespace TagLib { * Returns number of bits per sample. */ int bitsPerSample() const; + uint sampleFrames() const; /*! * Returns APE version. diff --git a/taglib/mpc/mpcproperties.cpp b/taglib/mpc/mpcproperties.cpp index 9adc6924..3ce44cc2 100644 --- a/taglib/mpc/mpcproperties.cpp +++ b/taglib/mpc/mpcproperties.cpp @@ -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); + + d->sampleFrames = d->totalFrames * 1152 - 576; } - uint samples = frames * 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; diff --git a/taglib/mpc/mpcproperties.h b/taglib/mpc/mpcproperties.h index d1593458..8225306f 100644 --- a/taglib/mpc/mpcproperties.h +++ b/taglib/mpc/mpcproperties.h @@ -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 &); diff --git a/taglib/wavpack/wavpackproperties.cpp b/taglib/wavpack/wavpackproperties.cpp index 52552a0d..53c6d763 100644 --- a/taglib/wavpack/wavpackproperties.cpp +++ b/taglib/wavpack/wavpackproperties.cpp @@ -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; } diff --git a/taglib/wavpack/wavpackproperties.h b/taglib/wavpack/wavpackproperties.h index 74d18ea8..bd2209da 100644 --- a/taglib/wavpack/wavpackproperties.h +++ b/taglib/wavpack/wavpackproperties.h @@ -82,6 +82,7 @@ namespace TagLib { * Returns number of bits per sample. */ int bitsPerSample() const; + uint sampleFrames() const; /*! * Returns WavPack version.