1 /***************************************************************************
2 copyright : (C) 2013 by Stephen F. Booth
3 email : me@sbooth.org
4 ***************************************************************************/
5
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. *
10 * *
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. *
15 * *
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., 51 Franklin Street, Fifth Floor, Boston, MA *
19 * 02110-1301 USA *
20 * *
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 ***************************************************************************/
25
26 #include <tstring.h>
27 #include <tdebug.h>
28
29 #include "dsfproperties.h"
30
31 using namespace TagLib;
32
33 class DSF::Properties::PropertiesPrivate
34 {
35 public:
PropertiesPrivate()36 PropertiesPrivate() :
37 formatVersion(0),
38 formatID(0),
39 channelType(0),
40 channelNum(0),
41 samplingFrequency(0),
42 bitsPerSample(0),
43 sampleCount(0),
44 blockSizePerChannel(0),
45 bitrate(0),
46 length(0)
47 {
48 }
49
50 // Nomenclature is from DSF file format specification
51 unsigned int formatVersion;
52 unsigned int formatID;
53 unsigned int channelType;
54 unsigned int channelNum;
55 unsigned int samplingFrequency;
56 unsigned int bitsPerSample;
57 long long sampleCount;
58 unsigned int blockSizePerChannel;
59
60 // Computed
61 unsigned int bitrate;
62 unsigned int length;
63 };
64
65 ////////////////////////////////////////////////////////////////////////////////
66 // public members
67 ////////////////////////////////////////////////////////////////////////////////
68
Properties(const ByteVector & data,ReadStyle style)69 DSF::Properties::Properties(const ByteVector &data, ReadStyle style) : TagLib::AudioProperties(style)
70 {
71 d = new PropertiesPrivate;
72 read(data);
73 }
74
~Properties()75 DSF::Properties::~Properties()
76 {
77 delete d;
78 }
79
length() const80 int DSF::Properties::length() const
81 {
82 return lengthInSeconds();
83 }
84
lengthInSeconds() const85 int DSF::Properties::lengthInSeconds() const
86 {
87 return d->length / 1000;
88 }
89
lengthInMilliseconds() const90 int DSF::Properties::lengthInMilliseconds() const
91 {
92 return d->length;
93 }
94
bitrate() const95 int DSF::Properties::bitrate() const
96 {
97 return d->bitrate;
98 }
99
sampleRate() const100 int DSF::Properties::sampleRate() const
101 {
102 return d->samplingFrequency;
103 }
104
channels() const105 int DSF::Properties::channels() const
106 {
107 return d->channelNum;
108 }
109
110 // DSF specific
formatVersion() const111 int DSF::Properties::formatVersion() const
112 {
113 return d->formatVersion;
114 }
115
formatID() const116 int DSF::Properties::formatID() const
117 {
118 return d->formatID;
119 }
120
channelType() const121 int DSF::Properties::channelType() const
122 {
123 return d->channelType;
124 }
125
bitsPerSample() const126 int DSF::Properties::bitsPerSample() const
127 {
128 return d->bitsPerSample;
129 }
130
sampleCount() const131 long long DSF::Properties::sampleCount() const
132 {
133 return d->sampleCount;
134 }
135
blockSizePerChannel() const136 int DSF::Properties::blockSizePerChannel() const
137 {
138 return d->blockSizePerChannel;
139 }
140
141 ////////////////////////////////////////////////////////////////////////////////
142 // private members
143 ////////////////////////////////////////////////////////////////////////////////
144
read(const ByteVector & data)145 void DSF::Properties::read(const ByteVector &data)
146 {
147 d->formatVersion = data.toUInt(0U,false);
148 d->formatID = data.toUInt(4U,false);
149 d->channelType = data.toUInt(8U,false);
150 d->channelNum = data.toUInt(12U,false);
151 d->samplingFrequency = data.toUInt(16U,false);
152 d->bitsPerSample = data.toUInt(20U,false);
153 d->sampleCount = data.toLongLong(24U,false);
154 d->blockSizePerChannel = data.toUInt(32U,false);
155
156 d->bitrate
157 = static_cast<unsigned int>((d->samplingFrequency * d->bitsPerSample * d->channelNum) / 1000.0 + 0.5);
158 d->length
159 = d->samplingFrequency > 0 ? static_cast<unsigned int>(d->sampleCount * 1000.0 / d->samplingFrequency + 0.5) : 0;
160 }
161
162