|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This device is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This device contains pre-release code and may not be distributed. |
|
12 ** You may use this device 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 device 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 device LICENSE.LGPL included in the |
|
20 ** packaging of this device. 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 device LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this device, 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 <QtCore/qendian.h> |
|
43 #include "wavheader.h" |
|
44 |
|
45 |
|
46 struct chunk |
|
47 { |
|
48 char id[4]; |
|
49 quint32 size; |
|
50 }; |
|
51 |
|
52 struct RIFFHeader |
|
53 { |
|
54 chunk descriptor; // "RIFF" |
|
55 char type[4]; // "WAVE" |
|
56 }; |
|
57 |
|
58 struct WAVEHeader |
|
59 { |
|
60 chunk descriptor; |
|
61 quint16 audioFormat; |
|
62 quint16 numChannels; |
|
63 quint32 sampleRate; |
|
64 quint32 byteRate; |
|
65 quint16 blockAlign; |
|
66 quint16 bitsPerSample; |
|
67 }; |
|
68 |
|
69 struct DATAHeader |
|
70 { |
|
71 chunk descriptor; |
|
72 }; |
|
73 |
|
74 struct CombinedHeader |
|
75 { |
|
76 RIFFHeader riff; |
|
77 WAVEHeader wave; |
|
78 DATAHeader data; |
|
79 }; |
|
80 |
|
81 static const int HeaderLength = sizeof(CombinedHeader); |
|
82 |
|
83 |
|
84 WavHeader::WavHeader(const QAudioFormat &format, qint64 dataLength) |
|
85 : m_format(format) |
|
86 , m_dataLength(dataLength) |
|
87 { |
|
88 |
|
89 } |
|
90 |
|
91 bool WavHeader::read(QIODevice &device) |
|
92 { |
|
93 bool result = true; |
|
94 |
|
95 if (!device.isSequential()) |
|
96 result = device.seek(0); |
|
97 // else, assume that current position is the start of the header |
|
98 |
|
99 if (result) { |
|
100 CombinedHeader header; |
|
101 result = (device.read(reinterpret_cast<char *>(&header), HeaderLength) == HeaderLength); |
|
102 if (result) { |
|
103 if ((memcmp(&header.riff.descriptor.id, "RIFF", 4) == 0 |
|
104 || memcmp(&header.riff.descriptor.id, "RIFX", 4) == 0) |
|
105 && memcmp(&header.riff.type, "WAVE", 4) == 0 |
|
106 && memcmp(&header.wave.descriptor.id, "fmt ", 4) == 0 |
|
107 && header.wave.audioFormat == 1 // PCM |
|
108 ) { |
|
109 if (memcmp(&header.riff.descriptor.id, "RIFF", 4) == 0) |
|
110 m_format.setByteOrder(QAudioFormat::LittleEndian); |
|
111 else |
|
112 m_format.setByteOrder(QAudioFormat::BigEndian); |
|
113 |
|
114 m_format.setChannels(qFromLittleEndian<quint16>(header.wave.numChannels)); |
|
115 m_format.setCodec("audio/pcm"); |
|
116 m_format.setFrequency(qFromLittleEndian<quint32>(header.wave.sampleRate)); |
|
117 m_format.setSampleSize(qFromLittleEndian<quint16>(header.wave.bitsPerSample)); |
|
118 |
|
119 switch(header.wave.bitsPerSample) { |
|
120 case 8: |
|
121 m_format.setSampleType(QAudioFormat::UnSignedInt); |
|
122 break; |
|
123 case 16: |
|
124 m_format.setSampleType(QAudioFormat::SignedInt); |
|
125 break; |
|
126 default: |
|
127 result = false; |
|
128 } |
|
129 |
|
130 m_dataLength = device.size() - HeaderLength; |
|
131 } else { |
|
132 result = false; |
|
133 } |
|
134 } |
|
135 } |
|
136 |
|
137 return result; |
|
138 } |
|
139 |
|
140 bool WavHeader::write(QIODevice &device) |
|
141 { |
|
142 CombinedHeader header; |
|
143 |
|
144 memset(&header, 0, HeaderLength); |
|
145 |
|
146 // RIFF header |
|
147 if (m_format.byteOrder() == QAudioFormat::LittleEndian) |
|
148 memcpy(header.riff.descriptor.id,"RIFF",4); |
|
149 else |
|
150 memcpy(header.riff.descriptor.id,"RIFX",4); |
|
151 qToLittleEndian<quint32>(quint32(m_dataLength + HeaderLength - 8), |
|
152 reinterpret_cast<unsigned char*>(&header.riff.descriptor.size)); |
|
153 memcpy(header.riff.type, "WAVE",4); |
|
154 |
|
155 // WAVE header |
|
156 memcpy(header.wave.descriptor.id,"fmt ",4); |
|
157 qToLittleEndian<quint32>(quint32(16), |
|
158 reinterpret_cast<unsigned char*>(&header.wave.descriptor.size)); |
|
159 qToLittleEndian<quint16>(quint16(1), |
|
160 reinterpret_cast<unsigned char*>(&header.wave.audioFormat)); |
|
161 qToLittleEndian<quint16>(quint16(m_format.channels()), |
|
162 reinterpret_cast<unsigned char*>(&header.wave.numChannels)); |
|
163 qToLittleEndian<quint32>(quint32(m_format.frequency()), |
|
164 reinterpret_cast<unsigned char*>(&header.wave.sampleRate)); |
|
165 qToLittleEndian<quint32>(quint32(m_format.frequency() * m_format.channels() * m_format.sampleSize() / 8), |
|
166 reinterpret_cast<unsigned char*>(&header.wave.byteRate)); |
|
167 qToLittleEndian<quint16>(quint16(m_format.channels() * m_format.sampleSize() / 8), |
|
168 reinterpret_cast<unsigned char*>(&header.wave.blockAlign)); |
|
169 qToLittleEndian<quint16>(quint16(m_format.sampleSize()), |
|
170 reinterpret_cast<unsigned char*>(&header.wave.bitsPerSample)); |
|
171 |
|
172 // DATA header |
|
173 memcpy(header.data.descriptor.id,"data",4); |
|
174 qToLittleEndian<quint32>(quint32(m_dataLength), |
|
175 reinterpret_cast<unsigned char*>(&header.data.descriptor.size)); |
|
176 |
|
177 return (device.write(reinterpret_cast<const char *>(&header), HeaderLength) == HeaderLength); |
|
178 } |
|
179 |
|
180 const QAudioFormat& WavHeader::format() const |
|
181 { |
|
182 return m_format; |
|
183 } |
|
184 |
|
185 qint64 WavHeader::dataLength() const |
|
186 { |
|
187 return m_dataLength; |
|
188 } |
|
189 |
|
190 qint64 WavHeader::headerLength() |
|
191 { |
|
192 return HeaderLength; |
|
193 } |
|
194 |
|
195 bool WavHeader::writeDataLength(QIODevice &device, qint64 dataLength) |
|
196 { |
|
197 bool result = false; |
|
198 if (!device.isSequential()) { |
|
199 device.seek(40); |
|
200 unsigned char dataLengthLE[4]; |
|
201 qToLittleEndian<quint32>(quint32(dataLength), dataLengthLE); |
|
202 result = (device.write(reinterpret_cast<const char *>(dataLengthLE), 4) == 4); |
|
203 } |
|
204 return result; |
|
205 } |