Merge pull request #564 from TsudaKageyu/audioprop-aiff

(wishlist) AIFF: AudioProperties improvements
This commit is contained in:
Tsuda Kageyu
2015-08-01 01:29:46 +09:00
5 changed files with 181 additions and 58 deletions

View File

@ -40,10 +40,7 @@ public:
properties(0),
tag(0),
tagChunkID("ID3 "),
hasID3v2(false)
{
}
hasID3v2(false) {}
~FilePrivate()
{
@ -62,20 +59,20 @@ public:
// public members
////////////////////////////////////////////////////////////////////////////////
RIFF::AIFF::File::File(FileName file, bool readProperties,
Properties::ReadStyle propertiesStyle) : RIFF::File(file, BigEndian)
RIFF::AIFF::File::File(FileName file, bool readProperties, Properties::ReadStyle) :
RIFF::File(file, BigEndian),
d(new FilePrivate())
{
d = new FilePrivate;
if(isOpen())
read(readProperties, propertiesStyle);
read(readProperties);
}
RIFF::AIFF::File::File(IOStream *stream, bool readProperties,
Properties::ReadStyle propertiesStyle) : RIFF::File(stream, BigEndian)
RIFF::AIFF::File::File(IOStream *stream, bool readProperties, Properties::ReadStyle) :
RIFF::File(stream, BigEndian),
d(new FilePrivate())
{
d = new FilePrivate;
if(isOpen())
read(readProperties, propertiesStyle);
read(readProperties);
}
RIFF::AIFF::File::~File()
@ -135,34 +132,25 @@ bool RIFF::AIFF::File::hasID3v2Tag() const
// private members
////////////////////////////////////////////////////////////////////////////////
void RIFF::AIFF::File::read(bool readProperties, Properties::ReadStyle propertiesStyle)
void RIFF::AIFF::File::read(bool readProperties)
{
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(name == "COMM" && readProperties) {
if(formatData.isEmpty()) {
formatData = chunkData(i);
}
else {
debug("RIFF::AIFF::File::read() - Duplicate 'COMM' 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, Properties::Average);
}

View File

@ -130,7 +130,9 @@ namespace TagLib {
File(const File &);
File &operator=(const File &);
void read(bool readProperties, Properties::ReadStyle propertiesStyle);
void read(bool readProperties);
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;
@ -37,14 +38,14 @@ public:
bitrate(0),
sampleRate(0),
channels(0),
sampleWidth(0),
bitsPerSample(0),
sampleFrames(0) {}
int length;
int bitrate;
int sampleRate;
int channels;
int sampleWidth;
int bitsPerSample;
ByteVector compressionType;
String compressionName;
@ -56,10 +57,18 @@ public:
// public members
////////////////////////////////////////////////////////////////////////////////
RIFF::AIFF::Properties::Properties(const ByteVector &data, ReadStyle style) : AudioProperties(style)
RIFF::AIFF::Properties::Properties(const ByteVector &, ReadStyle style) :
AudioProperties(style),
d(new PropertiesPrivate())
{
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()
@ -68,6 +77,16 @@ RIFF::AIFF::Properties::~Properties()
}
int RIFF::AIFF::Properties::length() const
{
return lengthInSeconds();
}
int RIFF::AIFF::Properties::lengthInSeconds() const
{
return d->length / 1000;
}
int RIFF::AIFF::Properties::lengthInMilliseconds() const
{
return d->length;
}
@ -87,9 +106,14 @@ int RIFF::AIFF::Properties::channels() const
return d->channels;
}
int RIFF::AIFF::Properties::bitsPerSample() const
{
return d->bitsPerSample;
}
int RIFF::AIFF::Properties::sampleWidth() const
{
return d->sampleWidth;
return bitsPerSample();
}
TagLib::uint RIFF::AIFF::Properties::sampleFrames() const
@ -111,27 +135,57 @@ 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;
}
d->channels = data.toShort(0U);
d->sampleFrames = data.toUInt(2U);
d->sampleWidth = data.toShort(6U);
if(streamLength == 0) {
debug("RIFF::AIFF::Properties::read() - 'SSND' chunk not found.");
return;
}
d->channels = data.toShort(0U);
d->sampleFrames = data.toUInt(2U);
d->bitsPerSample = data.toShort(6U);
const long double sampleRate = data.toFloat80BE(8);
d->sampleRate = (int)sampleRate;
d->bitrate = (int)((sampleRate * d->sampleWidth * d->channels) / 1000.0);
d->length = d->sampleRate > 0 ? d->sampleFrames / d->sampleRate : 0;
if(sampleRate >= 1.0)
d->sampleRate = static_cast<int>(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) {
d->compressionType = data.mid(18, 4);
d->compressionName = String(data.mid(23, static_cast<uchar>(data[22])));
d->compressionName = String(data.mid(23, static_cast<uchar>(data[22])), String::Latin1);
}
}

View File

@ -49,22 +49,81 @@ 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.
*/
virtual ~Properties();
// Reimplementations.
/*!
* Returns the length of the file in seconds. The length is rounded down to
* the nearest whole second.
*
* \note This method is just an alias of lengthInSeconds().
*
* \deprecated
*/
virtual int length() const;
/*!
* Returns the length of the file in seconds. The length is rounded down to
* the nearest whole second.
*
* \see lengthInMilliseconds()
*/
// BIC: make virtual
int lengthInSeconds() const;
/*!
* Returns the length of the file in milliseconds.
*
* \see lengthInSeconds()
*/
// BIC: make virtual
int lengthInMilliseconds() const;
/*!
* Returns the average bit rate of the file in kb/s.
*/
virtual int bitrate() const;
/*!
* Returns the sample rate in Hz.
*/
virtual int sampleRate() const;
/*!
* Returns the number of audio channels.
*/
virtual int channels() const;
/*!
* Returns the number of bits per audio sample.
*/
int bitsPerSample() const;
/*!
* Returns the number of bits per audio sample.
*
* \note This method is just an alias of bitsPerSample().
*
* \deprecated
*/
int sampleWidth() const;
/*!
* Returns the number of sample frames
*/
uint sampleFrames() const;
/*!
@ -95,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

@ -12,9 +12,9 @@ using namespace TagLib;
class TestAIFF : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(TestAIFF);
CPPUNIT_TEST(testReading);
CPPUNIT_TEST(testSaveID3v2);
CPPUNIT_TEST(testAiffProperties);
CPPUNIT_TEST(testAiffCProperties);
CPPUNIT_TEST(testSaveID3v2);
CPPUNIT_TEST(testDuplicateID3v2);
CPPUNIT_TEST(testFuzzedFile1);
CPPUNIT_TEST(testFuzzedFile2);
@ -22,10 +22,38 @@ class TestAIFF : public CppUnit::TestFixture
public:
void testReading()
void testAiffProperties()
{
RIFF::AIFF::File f(TEST_FILE_PATH_C("empty.aiff"));
CPPUNIT_ASSERT_EQUAL(705, f.audioProperties()->bitrate());
CPPUNIT_ASSERT(f.audioProperties());
CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->length());
CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->lengthInSeconds());
CPPUNIT_ASSERT_EQUAL(67, f.audioProperties()->lengthInMilliseconds());
CPPUNIT_ASSERT_EQUAL(706, f.audioProperties()->bitrate());
CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
CPPUNIT_ASSERT_EQUAL(1, f.audioProperties()->channels());
CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->bitsPerSample());
CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->sampleWidth());
CPPUNIT_ASSERT_EQUAL(2941U, f.audioProperties()->sampleFrames());
CPPUNIT_ASSERT_EQUAL(false, f.audioProperties()->isAiffC());
}
void testAiffCProperties()
{
RIFF::AIFF::File f(TEST_FILE_PATH_C("alaw.aifc"));
CPPUNIT_ASSERT(f.audioProperties());
CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->length());
CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->lengthInSeconds());
CPPUNIT_ASSERT_EQUAL(37, f.audioProperties()->lengthInMilliseconds());
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());
CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->sampleWidth());
CPPUNIT_ASSERT_EQUAL(1622U, f.audioProperties()->sampleFrames());
CPPUNIT_ASSERT_EQUAL(true, f.audioProperties()->isAiffC());
CPPUNIT_ASSERT_EQUAL(ByteVector("ALAW"), f.audioProperties()->compressionType());
CPPUNIT_ASSERT_EQUAL(String("SGI CCITT G.711 A-law"), f.audioProperties()->compressionName());
}
void testSaveID3v2()
@ -47,14 +75,6 @@ public:
}
}
void testAiffCProperties()
{
RIFF::AIFF::File f(TEST_FILE_PATH_C("alaw.aifc"));
CPPUNIT_ASSERT(f.audioProperties()->isAiffC());
CPPUNIT_ASSERT_EQUAL(ByteVector("ALAW"), f.audioProperties()->compressionType());
CPPUNIT_ASSERT_EQUAL(String("SGI CCITT G.711 A-law"), f.audioProperties()->compressionName());
}
void testDuplicateID3v2()
{
RIFF::AIFF::File f(TEST_FILE_PATH_C("duplicate_id3v2.aiff"));