diff --git a/taglib/mpeg/mpegproperties.cpp b/taglib/mpeg/mpegproperties.cpp index ed1af124..76286b93 100644 --- a/taglib/mpeg/mpegproperties.cpp +++ b/taglib/mpeg/mpegproperties.cpp @@ -215,9 +215,17 @@ void MPEG::Properties::read() firstHeader.sampleRate() > 0 && d->xingHeader->totalFrames() > 0) { - static const int blockSize[] = { 0, 384, 1152, 1152 }; + static const int blockSize[2][4] = { + // Version 1 + { 0, 384, 1152, 1152 }, + // Version 2 or 2.5 + { 0, 384, 1152, 576 } + }; - double timePerFrame = double(blockSize[firstHeader.layer()]) / firstHeader.sampleRate(); + 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; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 34aa7fab..72040d0f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -13,6 +13,7 @@ SET(test_runner_SRCS main.cpp test_list.cpp test_map.cpp + test_mpeg.cpp test_synchdata.cpp test_bytevector.cpp test_string.cpp diff --git a/tests/Makefile.am b/tests/Makefile.am index 47a6b3f0..1c24d94f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -10,6 +10,7 @@ test_runner_SOURCES = \ main.cpp \ test_list.cpp \ test_map.cpp \ + test_mpeg.cpp \ test_synchdata.cpp \ test_bytevector.cpp \ test_string.cpp \ diff --git a/tests/data/mpeg2.mp3 b/tests/data/mpeg2.mp3 new file mode 100644 index 00000000..13e8d53d Binary files /dev/null and b/tests/data/mpeg2.mp3 differ diff --git a/tests/test_mpeg.cpp b/tests/test_mpeg.cpp new file mode 100644 index 00000000..6278ff55 --- /dev/null +++ b/tests/test_mpeg.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +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);