1
/***************************************************************************
2
copyright : (C) 2002 - 2008 by Scott Wheeler
3
email : wheeler@kde.org
4
***************************************************************************/
6
/***************************************************************************
7
* This library is free software; you can redistribute it and/or modify *
8
* it under the terms of the GNU Lesser General Public License version *
9
* 2.1 as published by the Free Software Foundation. *
11
* This library is distributed in the hope that it will be useful, but *
12
* WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14
* Lesser General Public License for more details. *
16
* You should have received a copy of the GNU Lesser General Public *
17
* License along with this library; if not, write to the Free Software *
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
21
* Alternatively, this file is available under the Mozilla Public *
22
* License Version 1.1. You may obtain a copy of the License at *
23
* http://www.mozilla.org/MPL/ *
24
***************************************************************************/
29
#include <oggpageheader.h>
31
#include "vorbisproperties.h"
32
#include "vorbisfile.h"
34
using namespace TagLib;
36
class Vorbis::Properties::PropertiesPrivate
39
PropertiesPrivate(File *f, ReadStyle s) :
65
* Vorbis headers can be found with one type ID byte and the string "vorbis" in
66
* an Ogg stream. 0x01 indicates the setup header.
68
static const char vorbisSetupHeaderID[] = { 0x01, 'v', 'o', 'r', 'b', 'i', 's', 0 };
71
////////////////////////////////////////////////////////////////////////////////
73
////////////////////////////////////////////////////////////////////////////////
75
Vorbis::Properties::Properties(File *file, ReadStyle style) : AudioProperties(style)
77
d = new PropertiesPrivate(file, style);
81
Vorbis::Properties::~Properties()
86
int Vorbis::Properties::length() const
91
int Vorbis::Properties::bitrate() const
93
return int(float(d->bitrate) / float(1000) + 0.5);
96
int Vorbis::Properties::sampleRate() const
101
int Vorbis::Properties::channels() const
106
int Vorbis::Properties::vorbisVersion() const
108
return d->vorbisVersion;
111
int Vorbis::Properties::bitrateMaximum() const
113
return d->bitrateMaximum;
116
int Vorbis::Properties::bitrateNominal() const
118
return d->bitrateNominal;
121
int Vorbis::Properties::bitrateMinimum() const
123
return d->bitrateMinimum;
126
////////////////////////////////////////////////////////////////////////////////
128
////////////////////////////////////////////////////////////////////////////////
130
void Vorbis::Properties::read()
132
// Get the identification header from the Ogg implementation.
134
ByteVector data = d->file->packet(0);
138
if(data.mid(pos, 7) != vorbisSetupHeaderID) {
139
debug("Vorbis::Properties::read() -- invalid Vorbis identification header");
145
d->vorbisVersion = data.mid(pos, 4).toUInt(false);
148
d->channels = uchar(data[pos]);
151
d->sampleRate = data.mid(pos, 4).toUInt(false);
154
d->bitrateMaximum = data.mid(pos, 4).toUInt(false);
157
d->bitrateNominal = data.mid(pos, 4).toUInt(false);
160
d->bitrateMinimum = data.mid(pos, 4).toUInt(false);
162
// TODO: Later this should be only the "fast" mode.
163
d->bitrate = d->bitrateNominal;
165
// Find the length of the file. See http://wiki.xiph.org/VorbisStreamLength/
166
// for my notes on the topic.
168
const Ogg::PageHeader *first = d->file->firstPageHeader();
169
const Ogg::PageHeader *last = d->file->lastPageHeader();
172
long long start = first->absoluteGranularPosition();
173
long long end = last->absoluteGranularPosition();
175
if(start >= 0 && end >= 0 && d->sampleRate > 0)
176
d->length = (end - start) / (long long) d->sampleRate;
178
debug("Vorbis::Properties::read() -- Either the PCM values for the start or "
179
"end of this file was incorrect or the sample rate is zero.");
182
debug("Vorbis::Properties::read() -- Could not find valid first and last Ogg pages.");