1 /***************************************************************************
2 copyright : (C) 2008 by Scott Wheeler
3 email : wheeler@kde.org
4 ***************************************************************************/
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. *
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. *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
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 ***************************************************************************/
29 // ldexp is a c99 function, which might not be defined in <cmath>
30 // so we pull in math.h too and hope it does the right (wrong) thing
31 // wrt. c99 functions in C++
34 #include "aiffproperties.h"
36 ////////////////////////////////////////////////////////////////////////////////
37 // nasty 80-bit float helpers
38 ////////////////////////////////////////////////////////////////////////////////
40 #define UnsignedToFloat(u) (((double)((long)(u - 2147483647L - 1))) + 2147483648.0)
42 static double ConvertFromIeeeExtended(unsigned char *bytes)
46 unsigned long hiMant, loMant;
48 expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
50 hiMant = ((unsigned long)(bytes[2] & 0xFF) << 24) |
51 ((unsigned long)(bytes[3] & 0xFF) << 16) |
52 ((unsigned long)(bytes[4] & 0xFF) << 8) |
53 ((unsigned long)(bytes[5] & 0xFF));
55 loMant = ((unsigned long)(bytes[6] & 0xFF) << 24) |
56 ((unsigned long)(bytes[7] & 0xFF) << 16) |
57 ((unsigned long)(bytes[8] & 0xFF) << 8) |
58 ((unsigned long)(bytes[9] & 0xFF));
60 if (expon == 0 && hiMant == 0 && loMant == 0)
63 if(expon == 0x7FFF) /* Infinity or NaN */
67 f = ldexp(UnsignedToFloat(hiMant), expon -= 31);
68 f += ldexp(UnsignedToFloat(loMant), expon -= 32);
78 using namespace TagLib;
80 class RIFF::AIFF::Properties::PropertiesPrivate
98 ////////////////////////////////////////////////////////////////////////////////
100 ////////////////////////////////////////////////////////////////////////////////
102 RIFF::AIFF::Properties::Properties(const ByteVector &data, ReadStyle style) : AudioProperties(style)
104 d = new PropertiesPrivate;
108 RIFF::AIFF::Properties::~Properties()
113 int RIFF::AIFF::Properties::length() const
118 int RIFF::AIFF::Properties::bitrate() const
123 int RIFF::AIFF::Properties::sampleRate() const
125 return d->sampleRate;
128 int RIFF::AIFF::Properties::channels() const
133 ////////////////////////////////////////////////////////////////////////////////
135 ////////////////////////////////////////////////////////////////////////////////
137 void RIFF::AIFF::Properties::read(const ByteVector &data)
139 d->channels = data.mid(0, 2).toShort();
140 uint sampleFrames = data.mid(2, 4).toUInt();
141 short sampleSize = data.mid(6, 2).toShort();
142 double sampleRate = ConvertFromIeeeExtended(reinterpret_cast<unsigned char *>(data.mid(8, 10).data()));
143 d->sampleRate = sampleRate;
144 d->bitrate = (sampleRate * sampleSize * d->channels) / 1024.0;
145 d->length = sampleFrames / d->sampleRate;