~njh-aelius/maxosx/musicbrainz-tags

« back to all changes in this revision

Viewing changes to Frameworks/taglib/taglib/taglib/speex/speexproperties.cpp

  • Committer: stephen_booth
  • Date: 2008-04-30 01:48:01 UTC
  • Revision ID: svn-v4:6b6cea13-1402-0410-9567-a7afb52bf336:trunk:1371
Fixing the taglib source tree

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
40
 
class Speex::Properties::PropertiesPrivate
41
 
{
42
 
public:
43
 
  PropertiesPrivate(File *f, ReadStyle s) :
44
 
    file(f),
45
 
    style(s),
46
 
    length(0),
47
 
    bitrate(0),
48
 
    sampleRate(0),
49
 
    channels(0),
50
 
    speexVersion(0),
51
 
    vbr(false),
52
 
    mode(0) {}
53
 
 
54
 
  File *file;
55
 
  ReadStyle style;
56
 
  int length;
57
 
  int bitrate;
58
 
  int sampleRate;
59
 
  int channels;
60
 
  int speexVersion;
61
 
  bool vbr;
62
 
  int mode;
63
 
};
64
 
 
65
 
////////////////////////////////////////////////////////////////////////////////
66
 
// public members
67
 
////////////////////////////////////////////////////////////////////////////////
68
 
 
69
 
Speex::Properties::Properties(File *file, ReadStyle style) : AudioProperties(style)
70
 
{
71
 
  d = new PropertiesPrivate(file, style);
72
 
  read();
73
 
}
74
 
 
75
 
Speex::Properties::~Properties()
76
 
{
77
 
  delete d;
78
 
}
79
 
 
80
 
int Speex::Properties::length() const
81
 
{
82
 
  return d->length;
83
 
}
84
 
 
85
 
int Speex::Properties::bitrate() const
86
 
{
87
 
  return int(float(d->bitrate) / float(1000) + 0.5);
88
 
}
89
 
 
90
 
int Speex::Properties::sampleRate() const
91
 
{
92
 
  return d->sampleRate;
93
 
}
94
 
 
95
 
int Speex::Properties::channels() const
96
 
{
97
 
  return d->channels;
98
 
}
99
 
 
100
 
int Speex::Properties::speexVersion() const
101
 
{
102
 
  return d->speexVersion;
103
 
}
104
 
 
105
 
////////////////////////////////////////////////////////////////////////////////
106
 
// private members
107
 
////////////////////////////////////////////////////////////////////////////////
108
 
 
109
 
void Speex::Properties::read()
110
 
{
111
 
  // Get the identification header from the Ogg implementation.
112
 
 
113
 
  ByteVector data = d->file->packet(0);
114
 
 
115
 
  int pos = 28;
116
 
 
117
 
  // speex_version_id;       /**< Version for Speex (for checking compatibility) */
118
 
  d->speexVersion = data.mid(pos, 4).toUInt(false);
119
 
  pos += 4;
120
 
 
121
 
  // header_size;            /**< Total size of the header ( sizeof(SpeexHeader) ) */
122
 
  pos += 4;
123
 
 
124
 
  // rate;                   /**< Sampling rate used */
125
 
  d->sampleRate = data.mid(pos, 4).toUInt(false);
126
 
  pos += 4;
127
 
 
128
 
  // mode;                   /**< Mode used (0 for narrowband, 1 for wideband) */
129
 
  d->mode = data.mid(pos, 4).toUInt(false);
130
 
  pos += 4;
131
 
 
132
 
  // mode_bitstream_version; /**< Version ID of the bit-stream */
133
 
  pos += 4;
134
 
 
135
 
  // nb_channels;            /**< Number of channels encoded */
136
 
  d->channels = data.mid(pos, 4).toUInt(false);
137
 
  pos += 4;
138
 
 
139
 
  // bitrate;                /**< Bit-rate used */
140
 
  d->bitrate = data.mid(pos, 4).toUInt(false);
141
 
  pos += 4;
142
 
 
143
 
  // frame_size;             /**< Size of frames */
144
 
  // unsigned int frameSize = data.mid(pos, 4).toUInt(false);
145
 
  pos += 4;
146
 
 
147
 
  // vbr;                    /**< 1 for a VBR encoding, 0 otherwise */
148
 
  d->vbr = data.mid(pos, 4).toUInt(false) == 1;
149
 
  pos += 4;
150
 
 
151
 
  // frames_per_packet;      /**< Number of frames stored per Ogg packet */
152
 
  // unsigned int framesPerPacket = data.mid(pos, 4).toUInt(false);
153
 
 
154
 
  const Ogg::PageHeader *first = d->file->firstPageHeader();
155
 
  const Ogg::PageHeader *last = d->file->lastPageHeader();
156
 
 
157
 
  if(first && last) {
158
 
    long long start = first->absoluteGranularPosition();
159
 
    long long end = last->absoluteGranularPosition();
160
 
 
161
 
    if(start >= 0 && end >= 0 && d->sampleRate > 0)
162
 
      d->length = (int) ((end - start) / (long long) d->sampleRate);
163
 
    else
164
 
      debug("Speex::Properties::read() -- Either the PCM values for the start or "
165
 
            "end of this file was incorrect or the sample rate is zero.");
166
 
  }
167
 
  else
168
 
    debug("Speex::Properties::read() -- Could not find valid first and last Ogg pages.");
169
 
}