2012-10-28 02:12:18 +02:00
|
|
|
/***************************************************************************
|
|
|
|
copyright : (C) 2008 by Scott Wheeler
|
|
|
|
email : wheeler@kde.org
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* This library is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License version *
|
|
|
|
* 2.1 as published by the Free Software Foundation. *
|
|
|
|
* *
|
|
|
|
* This library is distributed in the hope that it will be useful, but *
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
|
|
|
|
* Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public *
|
|
|
|
* License along with this library; if not, write to the Free Software *
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
|
|
|
|
* 02110-1301 USA *
|
|
|
|
* *
|
|
|
|
* Alternatively, this file is available under the Mozilla Public *
|
|
|
|
* License Version 1.1. You may obtain a copy of the License at *
|
|
|
|
* http://www.mozilla.org/MPL/ *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include <tdebug.h>
|
2015-11-24 19:36:24 +01:00
|
|
|
#include "wavfile.h"
|
|
|
|
#include "wavproperties.h"
|
2012-10-28 02:12:18 +02:00
|
|
|
|
|
|
|
using namespace TagLib;
|
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// Quoted from RFC 2361.
|
|
|
|
enum WaveFormat
|
|
|
|
{
|
|
|
|
FORMAT_UNKNOWN = 0x0000,
|
|
|
|
FORMAT_PCM = 0x0001
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-10-28 02:12:18 +02:00
|
|
|
class RIFF::WAV::Properties::PropertiesPrivate
|
|
|
|
{
|
|
|
|
public:
|
2015-11-24 19:36:24 +01:00
|
|
|
PropertiesPrivate() :
|
2012-10-28 02:12:18 +02:00
|
|
|
format(0),
|
|
|
|
length(0),
|
|
|
|
bitrate(0),
|
|
|
|
sampleRate(0),
|
|
|
|
channels(0),
|
2015-11-24 19:36:24 +01:00
|
|
|
bitsPerSample(0),
|
|
|
|
sampleFrames(0) {}
|
2012-10-28 02:12:18 +02:00
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
int format;
|
2012-10-28 02:12:18 +02:00
|
|
|
int length;
|
|
|
|
int bitrate;
|
|
|
|
int sampleRate;
|
|
|
|
int channels;
|
2015-11-24 19:36:24 +01:00
|
|
|
int bitsPerSample;
|
2012-10-28 02:12:18 +02:00
|
|
|
uint sampleFrames;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// public members
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
RIFF::WAV::Properties::Properties(const ByteVector & /*data*/, ReadStyle style) :
|
|
|
|
AudioProperties(style),
|
|
|
|
d(new PropertiesPrivate())
|
2012-10-28 02:12:18 +02:00
|
|
|
{
|
2015-11-24 19:36:24 +01:00
|
|
|
debug("RIFF::WAV::Properties::Properties() -- This constructor is no longer used.");
|
2012-10-28 02:12:18 +02:00
|
|
|
}
|
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
RIFF::WAV::Properties::Properties(const ByteVector & /*data*/, uint /*streamLength*/, ReadStyle style) :
|
|
|
|
AudioProperties(style),
|
|
|
|
d(new PropertiesPrivate())
|
2012-10-28 02:12:18 +02:00
|
|
|
{
|
2015-11-24 19:36:24 +01:00
|
|
|
debug("RIFF::WAV::Properties::Properties() -- This constructor is no longer used.");
|
|
|
|
}
|
|
|
|
|
|
|
|
TagLib::RIFF::WAV::Properties::Properties(File *file, ReadStyle style) :
|
|
|
|
AudioProperties(style),
|
|
|
|
d(new PropertiesPrivate())
|
|
|
|
{
|
|
|
|
read(file);
|
2012-10-28 02:12:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RIFF::WAV::Properties::~Properties()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RIFF::WAV::Properties::length() const
|
2015-11-24 19:36:24 +01:00
|
|
|
{
|
|
|
|
return lengthInSeconds();
|
|
|
|
}
|
|
|
|
|
|
|
|
int RIFF::WAV::Properties::lengthInSeconds() const
|
|
|
|
{
|
|
|
|
return d->length / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RIFF::WAV::Properties::lengthInMilliseconds() const
|
2012-10-28 02:12:18 +02:00
|
|
|
{
|
|
|
|
return d->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RIFF::WAV::Properties::bitrate() const
|
|
|
|
{
|
|
|
|
return d->bitrate;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RIFF::WAV::Properties::sampleRate() const
|
|
|
|
{
|
|
|
|
return d->sampleRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RIFF::WAV::Properties::channels() const
|
|
|
|
{
|
|
|
|
return d->channels;
|
|
|
|
}
|
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
int RIFF::WAV::Properties::bitsPerSample() const
|
|
|
|
{
|
|
|
|
return d->bitsPerSample;
|
|
|
|
}
|
|
|
|
|
2012-10-28 02:12:18 +02:00
|
|
|
int RIFF::WAV::Properties::sampleWidth() const
|
|
|
|
{
|
2015-11-24 19:36:24 +01:00
|
|
|
return bitsPerSample();
|
2012-10-28 02:12:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TagLib::uint RIFF::WAV::Properties::sampleFrames() const
|
|
|
|
{
|
|
|
|
return d->sampleFrames;
|
|
|
|
}
|
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
int RIFF::WAV::Properties::format() const
|
|
|
|
{
|
|
|
|
return d->format;
|
|
|
|
}
|
|
|
|
|
2012-10-28 02:12:18 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// private members
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
void RIFF::WAV::Properties::read(File *file)
|
2012-10-28 02:12:18 +02:00
|
|
|
{
|
2015-11-24 19:36:24 +01:00
|
|
|
ByteVector data;
|
|
|
|
uint streamLength = 0;
|
|
|
|
uint totalSamples = 0;
|
|
|
|
|
|
|
|
for(uint i = 0; i < file->chunkCount(); ++i) {
|
|
|
|
const ByteVector name = file->chunkName(i);
|
|
|
|
if(name == "fmt ") {
|
|
|
|
if(data.isEmpty())
|
|
|
|
data = file->chunkData(i);
|
|
|
|
else
|
|
|
|
debug("RIFF::WAV::Properties::read() - Duplicate 'fmt ' chunk found.");
|
|
|
|
}
|
|
|
|
else if(name == "data") {
|
|
|
|
if(streamLength == 0)
|
|
|
|
streamLength = file->chunkDataSize(i) + file->chunkPadding(i);
|
|
|
|
else
|
|
|
|
debug("RIFF::WAV::Properties::read() - Duplicate 'data' chunk found.");
|
|
|
|
}
|
|
|
|
else if(name == "fact") {
|
|
|
|
if(totalSamples == 0)
|
|
|
|
totalSamples = file->chunkData(i).toUInt(0, false);
|
|
|
|
else
|
|
|
|
debug("RIFF::WAV::Properties::read() - Duplicate 'fact' chunk found.");
|
|
|
|
}
|
|
|
|
}
|
2012-10-28 02:12:18 +02:00
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
if(data.size() < 16) {
|
|
|
|
debug("RIFF::WAV::Properties::read() - 'fmt ' chunk not found or too short.");
|
|
|
|
return;
|
|
|
|
}
|
2012-10-28 02:12:18 +02:00
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
if(streamLength == 0) {
|
|
|
|
debug("RIFF::WAV::Properties::read() - 'data' chunk not found.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->format = data.toShort(0, false);
|
|
|
|
if(d->format != FORMAT_PCM && totalSamples == 0) {
|
|
|
|
debug("RIFF::WAV::Properties::read() - Non-PCM format, but 'fact' chunk not found.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->channels = data.toShort(2, false);
|
|
|
|
d->sampleRate = data.toUInt(4, false);
|
|
|
|
d->bitsPerSample = data.toShort(14, false);
|
|
|
|
|
|
|
|
if(totalSamples > 0)
|
|
|
|
d->sampleFrames = totalSamples;
|
|
|
|
else if(d->channels > 0 && d->bitsPerSample > 0)
|
|
|
|
d->sampleFrames = streamLength / (d->channels * ((d->bitsPerSample + 7) / 8));
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const uint byteRate = data.toUInt(8, false);
|
|
|
|
if(byteRate > 0) {
|
|
|
|
d->length = static_cast<int>(streamLength * 1000.0 / byteRate + 0.5);
|
|
|
|
d->bitrate = static_cast<int>(byteRate * 8.0 / 1000.0 + 0.5);
|
|
|
|
}
|
|
|
|
}
|
2012-10-28 02:12:18 +02:00
|
|
|
}
|