Fixed bug with no_repeate mode, improved prev button behaviour
[someplayer] / src / taglib / trueaudio / trueaudioproperties.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 "trueaudioproperties.h"
35 #include "trueaudiofile.h"
36
37 using namespace TagLib;
38
39 class TrueAudio::Properties::PropertiesPrivate
40 {
41 public:
42   PropertiesPrivate(const ByteVector &d, long length, ReadStyle s) :
43     data(d),
44     streamLength(length),
45     style(s),
46     version(0),
47     length(0),
48     bitrate(0),
49     sampleRate(0),
50     channels(0),
51     bitsPerSample(0) {}
52
53   ByteVector data;
54   long streamLength;
55   ReadStyle style;
56   int version;
57   int length;
58   int bitrate;
59   int sampleRate;
60   int channels;
61   int bitsPerSample;
62 };
63
64 ////////////////////////////////////////////////////////////////////////////////
65 // public members
66 ////////////////////////////////////////////////////////////////////////////////
67
68 TrueAudio::Properties::Properties(const ByteVector &data, long streamLength, ReadStyle style) : AudioProperties(style)
69 {
70   d = new PropertiesPrivate(data, streamLength, style);
71   read();
72 }
73
74 TrueAudio::Properties::~Properties()
75 {
76   delete d;
77 }
78
79 int TrueAudio::Properties::length() const
80 {
81   return d->length;
82 }
83
84 int TrueAudio::Properties::bitrate() const
85 {
86   return d->bitrate;
87 }
88
89 int TrueAudio::Properties::sampleRate() const
90 {
91   return d->sampleRate;
92 }
93
94 int TrueAudio::Properties::bitsPerSample() const
95 {
96   return d->bitsPerSample;
97 }
98
99 int TrueAudio::Properties::channels() const
100 {
101   return d->channels;
102 }
103
104 int TrueAudio::Properties::ttaVersion() const
105 {
106   return d->version;
107 }
108
109 ////////////////////////////////////////////////////////////////////////////////
110 // private members
111 ////////////////////////////////////////////////////////////////////////////////
112
113 void TrueAudio::Properties::read()
114 {
115   if(!d->data.startsWith("TTA"))
116     return;
117
118   int pos = 3;
119
120   d->version = d->data[pos] - '0';
121   pos += 1 + 2;
122
123   d->channels = d->data.mid(pos, 2).toShort(false);
124   pos += 2;
125
126   d->bitsPerSample = d->data.mid(pos, 2).toShort(false);
127   pos += 2;
128
129   d->sampleRate = d->data.mid(pos, 4).toUInt(false);
130   pos += 4;
131
132   unsigned long samples = d->data.mid(pos, 4).toUInt(false);
133   d->length = samples / d->sampleRate;
134
135   d->bitrate = d->length > 0 ? ((d->streamLength * 8L) / d->length) / 1000 : 0;
136 }