AIFF: Calculate the actual average bitrate even if a file is compressed.

Move property parsing code to Properties.
This commit is contained in:
Tsuda Kageyu 2015-05-25 10:29:14 +09:00
parent aede4ac851
commit 03fd0a3ead
5 changed files with 59 additions and 33 deletions

View 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);
}

View File

@ -132,6 +132,8 @@ namespace TagLib {
void read(bool readProperties, Properties::ReadStyle propertiesStyle);
friend class Properties;
class FilePrivate;
FilePrivate *d;
};

View 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) {

View 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;

View 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());