|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "wavedecoder_p.h" |
|
43 |
|
44 #include <QtCore/qtimer.h> |
|
45 #include <QtCore/qendian.h> |
|
46 |
|
47 QT_BEGIN_NAMESPACE |
|
48 |
|
49 WaveDecoder::WaveDecoder(QIODevice *s, QObject *parent): |
|
50 QIODevice(parent), |
|
51 haveFormat(false), |
|
52 dataSize(0), |
|
53 remaining(0), |
|
54 source(s) |
|
55 { |
|
56 open(QIODevice::ReadOnly | QIODevice::Unbuffered); |
|
57 |
|
58 if (source->bytesAvailable() >= qint64(sizeof(CombinedHeader) + sizeof(DATAHeader) + sizeof(quint16))) |
|
59 QTimer::singleShot(0, this, SLOT(handleData())); |
|
60 else |
|
61 connect(source, SIGNAL(readyRead()), SLOT(handleData())); |
|
62 } |
|
63 |
|
64 WaveDecoder::~WaveDecoder() |
|
65 { |
|
66 } |
|
67 |
|
68 QAudioFormat WaveDecoder::audioFormat() const |
|
69 { |
|
70 return format; |
|
71 } |
|
72 |
|
73 int WaveDecoder::duration() const |
|
74 { |
|
75 return size() * 1000 / (format.sampleSize() / 8) / format.channels() / format.frequency(); |
|
76 } |
|
77 |
|
78 qint64 WaveDecoder::size() const |
|
79 { |
|
80 return haveFormat ? dataSize : 0; |
|
81 } |
|
82 |
|
83 bool WaveDecoder::isSequential() const |
|
84 { |
|
85 return source->isSequential(); |
|
86 } |
|
87 |
|
88 qint64 WaveDecoder::bytesAvailable() const |
|
89 { |
|
90 return haveFormat ? source->bytesAvailable() : 0; |
|
91 } |
|
92 |
|
93 qint64 WaveDecoder::readData(char *data, qint64 maxlen) |
|
94 { |
|
95 return haveFormat ? source->read(data, maxlen) : 0; |
|
96 } |
|
97 |
|
98 qint64 WaveDecoder::writeData(const char *data, qint64 len) |
|
99 { |
|
100 Q_UNUSED(data); |
|
101 Q_UNUSED(len); |
|
102 |
|
103 return -1; |
|
104 } |
|
105 |
|
106 void WaveDecoder::handleData() |
|
107 { |
|
108 if (source->bytesAvailable() < qint64(sizeof(CombinedHeader) + sizeof(DATAHeader) + sizeof(quint16))) |
|
109 return; |
|
110 |
|
111 source->disconnect(SIGNAL(readyRead()), this, SLOT(handleData())); |
|
112 source->read((char*)&header, sizeof(CombinedHeader)); |
|
113 |
|
114 if (qstrncmp(header.riff.descriptor.id, "RIFF", 4) != 0 || |
|
115 qstrncmp(header.riff.type, "WAVE", 4) != 0 || |
|
116 qstrncmp(header.wave.descriptor.id, "fmt ", 4) != 0 || |
|
117 (header.wave.audioFormat != 0 && header.wave.audioFormat != 1)) { |
|
118 |
|
119 emit invalidFormat(); |
|
120 } |
|
121 else { |
|
122 DATAHeader dataHeader; |
|
123 |
|
124 if (qFromLittleEndian<quint32>(header.wave.descriptor.size) > sizeof(WAVEHeader)) { |
|
125 // Extended data available |
|
126 quint16 extraFormatBytes; |
|
127 source->peek((char*)&extraFormatBytes, sizeof(quint16)); |
|
128 extraFormatBytes = qFromLittleEndian<quint16>(extraFormatBytes); |
|
129 source->read(sizeof(quint16) + extraFormatBytes); // dump it all |
|
130 } |
|
131 |
|
132 source->read((char*)&dataHeader, sizeof(DATAHeader)); |
|
133 |
|
134 int bps = qFromLittleEndian<quint16>(header.wave.bitsPerSample); |
|
135 |
|
136 format.setCodec(QLatin1String("audio/pcm")); |
|
137 format.setSampleType(bps == 8 ? QAudioFormat::UnSignedInt : QAudioFormat::SignedInt); |
|
138 format.setByteOrder(QAudioFormat::LittleEndian); |
|
139 format.setFrequency(qFromLittleEndian<quint32>(header.wave.sampleRate)); |
|
140 format.setSampleSize(bps); |
|
141 format.setChannels(qFromLittleEndian<quint16>(header.wave.numChannels)); |
|
142 |
|
143 dataSize = qFromLittleEndian<quint32>(dataHeader.descriptor.size); |
|
144 |
|
145 haveFormat = true; |
|
146 connect(source, SIGNAL(readyRead()), SIGNAL(readyRead())); |
|
147 emit formatKnown(); |
|
148 } |
|
149 } |
|
150 |
|
151 QT_END_NAMESPACE |