1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | |
30 | #include <QtTest/QtTest> |
31 | #include <QtCore/qlocale.h> |
32 | #include <qaudioformat.h> |
33 | |
34 | #include <QStringList> |
35 | #include <QList> |
36 | |
37 | //TESTED_COMPONENT=src/multimedia |
38 | |
39 | class tst_QAudioFormat : public QObject |
40 | { |
41 | Q_OBJECT |
42 | |
43 | public: |
44 | tst_QAudioFormat(QObject* parent=0) : QObject(parent) {} |
45 | |
46 | private slots: |
47 | void checkNull(); |
48 | void checkSampleSize(); |
49 | void checkCodec(); |
50 | void checkByteOrder(); |
51 | void checkSampleType(); |
52 | void checkEquality(); |
53 | void checkAssignment(); |
54 | void checkSampleRate(); |
55 | void checkChannelCount(); |
56 | |
57 | void checkSizes(); |
58 | void checkSizes_data(); |
59 | }; |
60 | |
61 | void tst_QAudioFormat::checkNull() |
62 | { |
63 | // Default constructed QAudioFormat is invalid. |
64 | QAudioFormat audioFormat0; |
65 | QVERIFY(!audioFormat0.isValid()); |
66 | |
67 | // validity is transferred |
68 | QAudioFormat audioFormat1(audioFormat0); |
69 | QVERIFY(!audioFormat1.isValid()); |
70 | |
71 | audioFormat0.setSampleRate(44100); |
72 | audioFormat0.setChannelCount(2); |
73 | audioFormat0.setSampleSize(16); |
74 | audioFormat0.setCodec("audio/pcm" ); |
75 | audioFormat0.setSampleType(QAudioFormat::SignedInt); |
76 | QVERIFY(audioFormat0.isValid()); |
77 | } |
78 | |
79 | void tst_QAudioFormat::checkSampleSize() |
80 | { |
81 | QAudioFormat audioFormat; |
82 | audioFormat.setSampleSize(16); |
83 | QVERIFY(audioFormat.sampleSize() == 16); |
84 | } |
85 | |
86 | void tst_QAudioFormat::checkCodec() |
87 | { |
88 | QAudioFormat audioFormat; |
89 | audioFormat.setCodec(QString::fromLatin1(str: "audio/pcm" )); |
90 | QVERIFY(audioFormat.codec() == QString::fromLatin1("audio/pcm" )); |
91 | } |
92 | |
93 | void tst_QAudioFormat::checkByteOrder() |
94 | { |
95 | QAudioFormat audioFormat; |
96 | audioFormat.setByteOrder(QAudioFormat::LittleEndian); |
97 | QVERIFY(audioFormat.byteOrder() == QAudioFormat::LittleEndian); |
98 | |
99 | QTest::ignoreMessage(type: QtDebugMsg, message: "LittleEndian" ); |
100 | qDebug() << QAudioFormat::LittleEndian; |
101 | |
102 | audioFormat.setByteOrder(QAudioFormat::BigEndian); |
103 | QVERIFY(audioFormat.byteOrder() == QAudioFormat::BigEndian); |
104 | |
105 | QTest::ignoreMessage(type: QtDebugMsg, message: "BigEndian" ); |
106 | qDebug() << QAudioFormat::BigEndian; |
107 | } |
108 | |
109 | void tst_QAudioFormat::checkSampleType() |
110 | { |
111 | QAudioFormat audioFormat; |
112 | audioFormat.setSampleType(QAudioFormat::SignedInt); |
113 | QVERIFY(audioFormat.sampleType() == QAudioFormat::SignedInt); |
114 | QTest::ignoreMessage(type: QtDebugMsg, message: "SignedInt" ); |
115 | qDebug() << QAudioFormat::SignedInt; |
116 | |
117 | audioFormat.setSampleType(QAudioFormat::Unknown); |
118 | QVERIFY(audioFormat.sampleType() == QAudioFormat::Unknown); |
119 | QTest::ignoreMessage(type: QtDebugMsg, message: "Unknown" ); |
120 | qDebug() << QAudioFormat::Unknown; |
121 | |
122 | audioFormat.setSampleType(QAudioFormat::UnSignedInt); |
123 | QVERIFY(audioFormat.sampleType() == QAudioFormat::UnSignedInt); |
124 | QTest::ignoreMessage(type: QtDebugMsg, message: "UnSignedInt" ); |
125 | qDebug() << QAudioFormat::UnSignedInt; |
126 | |
127 | audioFormat.setSampleType(QAudioFormat::Float); |
128 | QVERIFY(audioFormat.sampleType() == QAudioFormat::Float); |
129 | QTest::ignoreMessage(type: QtDebugMsg, message: "Float" ); |
130 | qDebug() << QAudioFormat::Float; |
131 | } |
132 | |
133 | void tst_QAudioFormat::checkEquality() |
134 | { |
135 | QAudioFormat audioFormat0; |
136 | QAudioFormat audioFormat1; |
137 | |
138 | // Null formats are equivalent |
139 | QVERIFY(audioFormat0 == audioFormat1); |
140 | QVERIFY(!(audioFormat0 != audioFormat1)); |
141 | |
142 | // on filled formats |
143 | audioFormat0.setSampleRate(8000); |
144 | audioFormat0.setChannelCount(1); |
145 | audioFormat0.setSampleSize(8); |
146 | audioFormat0.setCodec("audio/pcm" ); |
147 | audioFormat0.setByteOrder(QAudioFormat::LittleEndian); |
148 | audioFormat0.setSampleType(QAudioFormat::UnSignedInt); |
149 | |
150 | audioFormat1.setSampleRate(8000); |
151 | audioFormat1.setChannelCount(1); |
152 | audioFormat1.setSampleSize(8); |
153 | audioFormat1.setCodec("audio/pcm" ); |
154 | audioFormat1.setByteOrder(QAudioFormat::LittleEndian); |
155 | audioFormat1.setSampleType(QAudioFormat::UnSignedInt); |
156 | |
157 | QVERIFY(audioFormat0 == audioFormat1); |
158 | QVERIFY(!(audioFormat0 != audioFormat1)); |
159 | |
160 | audioFormat0.setSampleRate(44100); |
161 | QVERIFY(audioFormat0 != audioFormat1); |
162 | QVERIFY(!(audioFormat0 == audioFormat1)); |
163 | } |
164 | |
165 | void tst_QAudioFormat::checkAssignment() |
166 | { |
167 | QAudioFormat audioFormat0; |
168 | QAudioFormat audioFormat1; |
169 | |
170 | audioFormat0.setSampleRate(8000); |
171 | audioFormat0.setChannelCount(1); |
172 | audioFormat0.setSampleSize(8); |
173 | audioFormat0.setCodec("audio/pcm" ); |
174 | audioFormat0.setByteOrder(QAudioFormat::LittleEndian); |
175 | audioFormat0.setSampleType(QAudioFormat::UnSignedInt); |
176 | |
177 | audioFormat1 = audioFormat0; |
178 | QVERIFY(audioFormat1 == audioFormat0); |
179 | |
180 | QAudioFormat audioFormat2(audioFormat0); |
181 | QVERIFY(audioFormat2 == audioFormat0); |
182 | } |
183 | |
184 | /* sampleRate() API property test. */ |
185 | void tst_QAudioFormat::checkSampleRate() |
186 | { |
187 | QAudioFormat audioFormat; |
188 | QVERIFY(audioFormat.sampleRate() == -1); |
189 | |
190 | audioFormat.setSampleRate(123); |
191 | QVERIFY(audioFormat.sampleRate() == 123); |
192 | } |
193 | |
194 | /* channelCount() API property test. */ |
195 | void tst_QAudioFormat::checkChannelCount() |
196 | { |
197 | // channels is the old name for channelCount, so |
198 | // they should always be equal |
199 | QAudioFormat audioFormat; |
200 | QVERIFY(audioFormat.channelCount() == -1); |
201 | QVERIFY(audioFormat.channelCount() == -1); |
202 | |
203 | audioFormat.setChannelCount(123); |
204 | QVERIFY(audioFormat.channelCount() == 123); |
205 | QVERIFY(audioFormat.channelCount() == 123); |
206 | |
207 | audioFormat.setChannelCount(5); |
208 | QVERIFY(audioFormat.channelCount() == 5); |
209 | QVERIFY(audioFormat.channelCount() == 5); |
210 | } |
211 | |
212 | void tst_QAudioFormat::checkSizes() |
213 | { |
214 | QFETCH(QAudioFormat, format); |
215 | QFETCH(int, frameSize); |
216 | QFETCH(int, byteCount); |
217 | QFETCH(int, frameCount); |
218 | QFETCH(qint64, durationForByte); |
219 | QFETCH(int, byteForFrame); |
220 | QFETCH(qint64, durationForByteForDuration); |
221 | QFETCH(int, byteForDuration); |
222 | QFETCH(int, framesForDuration); |
223 | |
224 | QCOMPARE(format.bytesPerFrame(), frameSize); |
225 | |
226 | // Byte input |
227 | QCOMPARE(format.framesForBytes(byteCount), frameCount); |
228 | QCOMPARE(format.durationForBytes(byteCount), durationForByte); |
229 | |
230 | // Framecount input |
231 | QCOMPARE(format.bytesForFrames(frameCount), byteForFrame); |
232 | QCOMPARE(format.durationForFrames(frameCount), durationForByte); |
233 | |
234 | // Duration input |
235 | QCOMPARE(format.bytesForDuration(durationForByteForDuration), byteForDuration); |
236 | QCOMPARE(format.framesForDuration(durationForByte), frameCount); |
237 | QCOMPARE(format.framesForDuration(durationForByteForDuration), framesForDuration); |
238 | } |
239 | |
240 | void tst_QAudioFormat::checkSizes_data() |
241 | { |
242 | QTest::addColumn<QAudioFormat>(name: "format" ); |
243 | QTest::addColumn<int>(name: "frameSize" ); |
244 | QTest::addColumn<int>(name: "byteCount" ); |
245 | QTest::addColumn<qint64>(name: "durationForByte" ); |
246 | QTest::addColumn<int>(name: "frameCount" ); // output of sampleCountforByte, input for byteForFrame |
247 | QTest::addColumn<int>(name: "byteForFrame" ); |
248 | QTest::addColumn<qint64>(name: "durationForByteForDuration" ); // input for byteForDuration |
249 | QTest::addColumn<int>(name: "byteForDuration" ); |
250 | QTest::addColumn<int>(name: "framesForDuration" ); |
251 | |
252 | QAudioFormat f; |
253 | QTest::newRow(dataTag: "invalid" ) << f << 0 << 0 << 0LL << 0 << 0 << 0LL << 0 << 0; |
254 | |
255 | f.setByteOrder(QAudioFormat::LittleEndian); |
256 | f.setChannelCount(1); |
257 | f.setSampleRate(8000); |
258 | f.setCodec("audio/pcm" ); |
259 | f.setSampleSize(8); |
260 | f.setSampleType(QAudioFormat::SignedInt); |
261 | |
262 | qint64 qrtr = 250000LL; |
263 | qint64 half = 500000LL; |
264 | qint64 one = 1000000LL; |
265 | qint64 two = 2000000LL; |
266 | |
267 | // No rounding errors with mono 8 bit |
268 | QTest::newRow(dataTag: "1ch_8b_8k_signed_4000" ) << f << 1 << 4000 << half << 4000 << 4000 << half << 4000 << 4000; |
269 | QTest::newRow(dataTag: "1ch_8b_8k_signed_8000" ) << f << 1 << 8000 << one << 8000 << 8000 << one << 8000 << 8000; |
270 | QTest::newRow(dataTag: "1ch_8b_8k_signed_16000" ) << f << 1 << 16000 << two << 16000 << 16000 << two << 16000 << 16000; |
271 | |
272 | // Mono 16bit |
273 | f.setSampleSize(16); |
274 | QTest::newRow(dataTag: "1ch_16b_8k_signed_8000" ) << f << 2 << 8000 << half << 4000 << 8000 << half << 8000 << 4000; |
275 | QTest::newRow(dataTag: "1ch_16b_8k_signed_16000" ) << f << 2 << 16000 << one << 8000 << 16000 << one << 16000 << 8000; |
276 | |
277 | // Rounding errors |
278 | QTest::newRow(dataTag: "1ch_16b_8k_signed_8001" ) << f << 2 << 8001 << half << 4000 << 8000 << half << 8000 << 4000; |
279 | QTest::newRow(dataTag: "1ch_16b_8k_signed_8000_duration1" ) << f << 2 << 8000 << half << 4000 << 8000 << half + 1 << 8000 << 4000; |
280 | QTest::newRow(dataTag: "1ch_16b_8k_signed_8000_duration2" ) << f << 2 << 8000 << half << 4000 << 8000 << half + 124 << 8000 << 4000; |
281 | QTest::newRow(dataTag: "1ch_16b_8k_signed_8000_duration3" ) << f << 2 << 8000 << half << 4000 << 8000 << half + 125 << 8002 << 4001; |
282 | QTest::newRow(dataTag: "1ch_16b_8k_signed_8000_duration4" ) << f << 2 << 8000 << half << 4000 << 8000 << half + 126 << 8002 << 4001; |
283 | |
284 | // Stereo 16 bit |
285 | f.setChannelCount(2); |
286 | QTest::newRow(dataTag: "2ch_16b_8k_signed_8000" ) << f << 4 << 8000 << qrtr << 2000 << 8000 << qrtr << 8000 << 2000; |
287 | QTest::newRow(dataTag: "2ch_16b_8k_signed_16000" ) << f << 4 << 16000 << half << 4000 << 16000 << half << 16000 << 4000; |
288 | |
289 | // More rounding errors |
290 | // First rounding bytes |
291 | QTest::newRow(dataTag: "2ch_16b_8k_signed_8001" ) << f << 4 << 8001 << qrtr << 2000 << 8000 << qrtr << 8000 << 2000; |
292 | QTest::newRow(dataTag: "2ch_16b_8k_signed_8002" ) << f << 4 << 8002 << qrtr << 2000 << 8000 << qrtr << 8000 << 2000; |
293 | QTest::newRow(dataTag: "2ch_16b_8k_signed_8003" ) << f << 4 << 8003 << qrtr << 2000 << 8000 << qrtr << 8000 << 2000; |
294 | |
295 | // Then rounding duration |
296 | // 8khz = 125us per frame |
297 | QTest::newRow(dataTag: "2ch_16b_8k_signed_8000_duration1" ) << f << 4 << 8000 << qrtr << 2000 << 8000 << qrtr + 1 << 8000 << 2000; |
298 | QTest::newRow(dataTag: "2ch_16b_8k_signed_8000_duration2" ) << f << 4 << 8000 << qrtr << 2000 << 8000 << qrtr + 124 << 8000 << 2000; |
299 | QTest::newRow(dataTag: "2ch_16b_8k_signed_8000_duration3" ) << f << 4 << 8000 << qrtr << 2000 << 8000 << qrtr + 125 << 8004 << 2001; |
300 | QTest::newRow(dataTag: "2ch_16b_8k_signed_8000_duration4" ) << f << 4 << 8000 << qrtr << 2000 << 8000 << qrtr + 126 << 8004 << 2001; |
301 | } |
302 | |
303 | QTEST_MAIN(tst_QAudioFormat) |
304 | |
305 | #include "tst_qaudioformat.moc" |
306 | |