Added TagLib (with AUTORS and COPYING files)
[someplayer] / src / taglib / ogg / speex / speexproperties.cpp
1 /***************************************************************************
2     copyright            : (C) 2006 by Lukáš Lalinský
3     email                : lalinsky@gmail.com
4
5     copyright            : (C) 2002 - 2008 by Scott Wheeler
6     email                : wheeler@kde.org
7                            (original Vorbis implementation)
8  ***************************************************************************/
9
10 /***************************************************************************
11  *   This library is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU Lesser General Public License version   *
13  *   2.1 as published by the Free Software Foundation.                     *
14  *                                                                         *
15  *   This library is distributed in the hope that it will be useful, but   *
16  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
18  *   Lesser General Public License for more details.                       *
19  *                                                                         *
20  *   You should have received a copy of the GNU Lesser General Public      *
21  *   License along with this library; if not, write to the Free Software   *
22  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
23  *   USA                                                                   *
24  *                                                                         *
25  *   Alternatively, this file is available under the Mozilla Public        *
26  *   License Version 1.1.  You may obtain a copy of the License at         *
27  *   http://www.mozilla.org/MPL/                                           *
28  ***************************************************************************/
29
30 #include <tstring.h>
31 #include <tdebug.h>
32
33 #include <oggpageheader.h>
34
35 #include "speexproperties.h"
36 #include "speexfile.h"
37
38 using namespace TagLib;
39 using namespace TagLib::Ogg;
40
41 class Speex::Properties::PropertiesPrivate
42 {
43 public:
44   PropertiesPrivate(File *f, ReadStyle s) :
45     file(f),
46     style(s),
47     length(0),
48     bitrate(0),
49     sampleRate(0),
50     channels(0),
51     speexVersion(0),
52     vbr(false),
53     mode(0) {}
54
55   File *file;
56   ReadStyle style;
57   int length;
58   int bitrate;
59   int sampleRate;
60   int channels;
61   int speexVersion;
62   bool vbr;
63   int mode;
64 };
65
66 ////////////////////////////////////////////////////////////////////////////////
67 // public members
68 ////////////////////////////////////////////////////////////////////////////////
69
70 Speex::Properties::Properties(File *file, ReadStyle style) : AudioProperties(style)
71 {
72   d = new PropertiesPrivate(file, style);
73   read();
74 }
75
76 Speex::Properties::~Properties()
77 {
78   delete d;
79 }
80
81 int Speex::Properties::length() const
82 {
83   return d->length;
84 }
85
86 int Speex::Properties::bitrate() const
87 {
88   return int(float(d->bitrate) / float(1000) + 0.5);
89 }
90
91 int Speex::Properties::sampleRate() const
92 {
93   return d->sampleRate;
94 }
95
96 int Speex::Properties::channels() const
97 {
98   return d->channels;
99 }
100
101 int Speex::Properties::speexVersion() const
102 {
103   return d->speexVersion;
104 }
105
106 ////////////////////////////////////////////////////////////////////////////////
107 // private members
108 ////////////////////////////////////////////////////////////////////////////////
109
110 void Speex::Properties::read()
111 {
112   // Get the identification header from the Ogg implementation.
113
114   ByteVector data = d->file->packet(0);
115
116   int pos = 28;
117
118   // speex_version_id;       /**< Version for Speex (for checking compatibility) */
119   d->speexVersion = data.mid(pos, 4).toUInt(false);
120   pos += 4;
121
122   // header_size;            /**< Total size of the header ( sizeof(SpeexHeader) ) */
123   pos += 4;
124
125   // rate;                   /**< Sampling rate used */
126   d->sampleRate = data.mid(pos, 4).toUInt(false);
127   pos += 4;
128
129   // mode;                   /**< Mode used (0 for narrowband, 1 for wideband) */
130   d->mode = data.mid(pos, 4).toUInt(false);
131   pos += 4;
132
133   // mode_bitstream_version; /**< Version ID of the bit-stream */
134   pos += 4;
135
136   // nb_channels;            /**< Number of channels encoded */
137   d->channels = data.mid(pos, 4).toUInt(false);
138   pos += 4;
139
140   // bitrate;                /**< Bit-rate used */
141   d->bitrate = data.mid(pos, 4).toUInt(false);
142   pos += 4;
143
144   // frame_size;             /**< Size of frames */
145   // unsigned int frameSize = data.mid(pos, 4).toUInt(false);
146   pos += 4;
147
148   // vbr;                    /**< 1 for a VBR encoding, 0 otherwise */
149   d->vbr = data.mid(pos, 4).toUInt(false) == 1;
150   pos += 4;
151
152   // frames_per_packet;      /**< Number of frames stored per Ogg packet */
153   // unsigned int framesPerPacket = data.mid(pos, 4).toUInt(false);
154
155   const Ogg::PageHeader *first = d->file->firstPageHeader();
156   const Ogg::PageHeader *last = d->file->lastPageHeader();
157
158   if(first && last) {
159     long long start = first->absoluteGranularPosition();
160     long long end = last->absoluteGranularPosition();
161
162     if(start >= 0 && end >= 0 && d->sampleRate > 0)
163       d->length = (int) ((end - start) / (long long) d->sampleRate);
164     else
165       debug("Speex::Properties::read() -- Either the PCM values for the start or "
166             "end of this file was incorrect or the sample rate is zero.");
167   }
168   else
169     debug("Speex::Properties::read() -- Could not find valid first and last Ogg pages.");
170 }