firstHeader.sampleRate() > 0 &&
d->xingHeader->totalFrames() > 0)
{
- static const int blockSize[] = { 0, 384, 1152, 1152 };
-
- double timePerFrame = double(blockSize[firstHeader.layer()]) / firstHeader.sampleRate();
+ static const int blockSize[2][4] = {
+ // Version 1
+ { 0, 384, 1152, 1152 },
+ // Version 2 or 2.5
+ { 0, 384, 1152, 576 }
+ };
+
+ int versionIndex = firstHeader.version() == Header::Version1 ? 0 : 1;
+ double timePerFrame =
+ double(blockSize[versionIndex][firstHeader.layer()]) /
+ firstHeader.sampleRate();
d->length = int(timePerFrame * d->xingHeader->totalFrames());
d->bitrate = d->length > 0 ? d->xingHeader->totalSize() * 8 / d->length / 1000 : 0;
}
--- /dev/null
+#include <cppunit/extensions/HelperMacros.h>
+#include <string>
+#include <stdio.h>
+#include <mpegfile.h>
+
+using namespace std;
+using namespace TagLib;
+
+class TestMPEG : public CppUnit::TestFixture
+{
+ CPPUNIT_TEST_SUITE(TestMPEG);
+ CPPUNIT_TEST(testVersion2DurationWithXingHeader);
+ CPPUNIT_TEST_SUITE_END();
+
+public:
+
+ void testVersion2DurationWithXingHeader()
+ {
+ MPEG::File f("data/mpeg2.mp3");
+ CPPUNIT_ASSERT_EQUAL(5387, f.audioProperties()->length());
+ }
+
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(TestMPEG);