class FLAC::Properties::PropertiesPrivate
{
public:
- PropertiesPrivate(ByteVector d, long st, ReadStyle s) :
- data(d),
- streamLength(st),
- style(s),
+ PropertiesPrivate() :
length(0),
bitrate(0),
sampleRate(0),
- sampleWidth(0),
+ bitsPerSample(0),
channels(0),
sampleFrames(0) {}
- ByteVector data;
- long streamLength;
- ReadStyle style;
int length;
int bitrate;
int sampleRate;
- int sampleWidth;
+ int bitsPerSample;
int channels;
unsigned long long sampleFrames;
ByteVector signature;
// public members
////////////////////////////////////////////////////////////////////////////////
-FLAC::Properties::Properties(ByteVector data, long streamLength, ReadStyle style) : AudioProperties(style)
+FLAC::Properties::Properties(ByteVector data, long streamLength, ReadStyle style) :
+ AudioProperties(style),
+ d(new PropertiesPrivate())
{
- d = new PropertiesPrivate(data, streamLength, style);
- read();
+ read(data, streamLength);
}
-FLAC::Properties::Properties(File *file, ReadStyle style) : AudioProperties(style)
+FLAC::Properties::Properties(File *file, ReadStyle style) :
+ AudioProperties(style),
+ d(new PropertiesPrivate())
{
- d = new PropertiesPrivate(file->streamInfoData(), file->streamLength(), style);
- read();
+ read(file->streamInfoData(), file->streamLength());
}
FLAC::Properties::~Properties()
}
int FLAC::Properties::length() const
+{
+ return lengthInSeconds();
+}
+
+int FLAC::Properties::lengthInSeconds() const
+{
+ return d->length / 1000;
+}
+
+int FLAC::Properties::lengthInMilliseconds() const
{
return d->length;
}
return d->sampleRate;
}
+int FLAC::Properties::bitsPerSample() const
+{
+ return d->bitsPerSample;
+}
+
int FLAC::Properties::sampleWidth() const
{
- return d->sampleWidth;
+ return bitsPerSample();
}
int FLAC::Properties::channels() const
// private members
////////////////////////////////////////////////////////////////////////////////
-void FLAC::Properties::read()
+void FLAC::Properties::read(const ByteVector &data, long streamLength)
{
- if(d->data.size() < 18) {
+ if(data.size() < 18) {
debug("FLAC::Properties::read() - FLAC properties must contain at least 18 bytes.");
return;
}
// Maximum frame size (in bytes)
pos += 3;
- uint flags = d->data.toUInt(pos, true);
+ const uint flags = data.toUInt(pos, true);
pos += 4;
- d->sampleRate = flags >> 12;
- d->channels = ((flags >> 9) & 7) + 1;
- d->sampleWidth = ((flags >> 4) & 31) + 1;
+ d->sampleRate = flags >> 12;
+ d->channels = ((flags >> 9) & 7) + 1;
+ d->bitsPerSample = ((flags >> 4) & 31) + 1;
// The last 4 bits are the most significant 4 bits for the 36 bit
// stream length in samples. (Audio files measured in days)
- unsigned long long hi = flags & 0xf;
- unsigned long long lo = d->data.toUInt(pos, true);
+ const ulonglong hi = flags & 0xf;
+ const ulonglong lo = data.toUInt(pos, true);
pos += 4;
d->sampleFrames = (hi << 32) | lo;
- if(d->sampleRate > 0)
- d->length = int(d->sampleFrames / d->sampleRate);
-
- // Uncompressed bitrate:
-
- //d->bitrate = ((d->sampleRate * d->channels) / 1000) * d->sampleWidth;
-
- // Real bitrate:
-
- d->bitrate = d->length > 0 ? ((d->streamLength * 8UL) / d->length) / 1000 : 0;
+ 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);
+ }
- d->signature = d->data.mid(pos, 32);
+ if(data.size() >= pos + 16)
+ d->signature = data.mid(pos, 16);
}
*/
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 as read from the FLAC
+ * identification header.
+ */
+ int bitsPerSample() const;
+
/*!
* Returns the sample width as read from the FLAC identification
* header.
+ *
+ * \note This method is just an alias of bitsPerSample().
+ *
+ * \deprecated
*/
int sampleWidth() const;
Properties(const Properties &);
Properties &operator=(const Properties &);
- void read();
+ void read(const ByteVector &data, long streamLength);
class PropertiesPrivate;
PropertiesPrivate *d;
CPPUNIT_TEST(testSaveMultipleValues);
CPPUNIT_TEST(testDict);
CPPUNIT_TEST(testInvalid);
+ CPPUNIT_TEST(testAudioProperties);
CPPUNIT_TEST(testZeroSizedPadding);
CPPUNIT_TEST_SUITE_END();
CPPUNIT_ASSERT_EQUAL(TagLib::uint(0), f.properties().size());
}
+ void testAudioProperties()
+ {
+ FLAC::File f(TEST_FILE_PATH_C("sinewave.flac"));
+ CPPUNIT_ASSERT(f.audioProperties());
+ CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->length());
+ CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->lengthInSeconds());
+ CPPUNIT_ASSERT_EQUAL(3550, f.audioProperties()->lengthInMilliseconds());
+ CPPUNIT_ASSERT_EQUAL(145, f.audioProperties()->bitrate());
+ CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
+ CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->channels());
+ CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->bitsPerSample());
+ CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->sampleWidth());
+ CPPUNIT_ASSERT_EQUAL(156556ULL, f.audioProperties()->sampleFrames());
+ CPPUNIT_ASSERT_EQUAL(
+ ByteVector("\xcf\xe3\xd9\xda\xba\xde\xab\x2c\xbf\x2c\xa2\x35\x27\x4b\x7f\x76"),
+ f.audioProperties()->signature());
+ }
+
void testZeroSizedPadding()
{
ScopedFileCopy copy("zero-sized-padding", ".flac");