Added TagLib (with AUTORS and COPYING files)
[someplayer] / src / taglib / wavpack / wavpackproperties.cpp
1 /***************************************************************************
2     copyright            : (C) 2006 by Lukáš Lalinský
3     email                : lalinsky@gmail.com
4
5     copyright            : (C) 2004 by Allan Sandfeld Jensen
6     email                : kde@carewolf.org
7                            (original MPC 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 #include <bitset>
33
34 #include "wavpackproperties.h"
35 #include "wavpackfile.h"
36
37 using namespace TagLib;
38
39 class WavPack::Properties::PropertiesPrivate
40 {
41 public:
42   PropertiesPrivate(const ByteVector &d, long length, ReadStyle s) :
43     data(d),
44     streamLength(length),
45     style(s),
46     length(0),
47     bitrate(0),
48     sampleRate(0),
49     channels(0),
50     version(0),
51     bitsPerSample(0) {}
52
53   ByteVector data;
54   long streamLength;
55   ReadStyle style;
56   int length;
57   int bitrate;
58   int sampleRate;
59   int channels;
60   int version;
61   int bitsPerSample;
62 };
63
64 ////////////////////////////////////////////////////////////////////////////////
65 // public members
66 ////////////////////////////////////////////////////////////////////////////////
67
68 WavPack::Properties::Properties(const ByteVector &data, long streamLength, ReadStyle style) : AudioProperties(style)
69 {
70   d = new PropertiesPrivate(data, streamLength, style);
71   read();
72 }
73
74 WavPack::Properties::~Properties()
75 {
76   delete d;
77 }
78
79 int WavPack::Properties::length() const
80 {
81   return d->length;
82 }
83
84 int WavPack::Properties::bitrate() const
85 {
86   return d->bitrate;
87 }
88
89 int WavPack::Properties::sampleRate() const
90 {
91   return d->sampleRate;
92 }
93
94 int WavPack::Properties::channels() const
95 {
96   return d->channels;
97 }
98
99 int WavPack::Properties::version() const
100 {
101   return d->version;
102 }
103
104 int WavPack::Properties::bitsPerSample() const
105 {
106   return d->bitsPerSample;
107 }
108
109 ////////////////////////////////////////////////////////////////////////////////
110 // private members
111 ////////////////////////////////////////////////////////////////////////////////
112
113 static const unsigned int sample_rates[] = { 6000, 8000, 9600, 11025, 12000,
114     16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000, 192000 };
115
116 #define BYTES_STORED    3
117 #define MONO_FLAG       4
118
119 #define SHIFT_LSB       13
120 #define SHIFT_MASK      (0x1fL << SHIFT_LSB)
121
122 #define SRATE_LSB       23
123 #define SRATE_MASK      (0xfL << SRATE_LSB)
124
125 void WavPack::Properties::read()
126 {
127   if(!d->data.startsWith("wvpk"))
128     return;
129
130   d->version = d->data.mid(8, 2).toShort(false);
131
132   unsigned int flags = d->data.mid(24, 4).toUInt(false);
133   d->bitsPerSample = ((flags & BYTES_STORED) + 1) * 8 -
134     ((flags & SHIFT_MASK) >> SHIFT_LSB);
135   d->sampleRate = sample_rates[(flags & SRATE_MASK) >> SRATE_LSB];
136   d->channels = (flags & MONO_FLAG) ? 1 : 2;
137
138   unsigned int samples = d->data.mid(12, 4).toUInt(false);
139   d->length = d->sampleRate > 0 ? (samples + (d->sampleRate / 2)) / d->sampleRate : 0;
140
141   d->bitrate = d->length > 0 ? ((d->streamLength * 8L) / d->length) / 1000 : 0;
142 }
143