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#include <QtCore/QString>
30#include <QtTest/QtTest>
31
32#include <qaudiobuffer.h>
33
34class tst_QAudioBuffer : public QObject
35{
36 Q_OBJECT
37
38public:
39 tst_QAudioBuffer();
40 ~tst_QAudioBuffer();
41
42private Q_SLOTS:
43 void ctors();
44 void assign();
45 void constData() const;
46 void data_const() const;
47 void data();
48 void durations();
49 void durations_data();
50 void stereoSample();
51
52private:
53 QAudioFormat mFormat;
54 QAudioBuffer *mNull;
55 QAudioBuffer *mEmpty;
56 QAudioBuffer *mFromArray;
57};
58
59tst_QAudioBuffer::tst_QAudioBuffer()
60{
61 // Initialize some common buffers
62 mFormat.setChannelCount(2);
63 mFormat.setSampleSize(16);
64 mFormat.setSampleType(QAudioFormat::UnSignedInt);
65 mFormat.setSampleRate(10000);
66 mFormat.setCodec("audio/pcm");
67
68 QByteArray b(4000, char(0x80));
69 mNull = new QAudioBuffer;
70 mEmpty = new QAudioBuffer(500, mFormat); // 500 stereo frames of 16 bits -> 2KB
71 mFromArray = new QAudioBuffer(b, mFormat);
72}
73
74
75tst_QAudioBuffer::~tst_QAudioBuffer()
76{
77 delete mNull;
78 delete mEmpty;
79 delete mFromArray;
80}
81
82void tst_QAudioBuffer::ctors()
83{
84 // Null buffer
85 QVERIFY(!mNull->isValid());
86 QVERIFY(mNull->constData() == 0);
87 QVERIFY(mNull->data() == 0);
88 QVERIFY(((const QAudioBuffer*)mNull)->data() == 0);
89 QCOMPARE(mNull->duration(), 0LL);
90 QCOMPARE(mNull->byteCount(), 0);
91 QCOMPARE(mNull->sampleCount(), 0);
92 QCOMPARE(mNull->frameCount(), 0);
93 QCOMPARE(mNull->startTime(), -1LL);
94
95 // Empty buffer
96 QVERIFY(mEmpty->isValid());
97 QVERIFY(mEmpty->constData() != 0);
98 QVERIFY(mEmpty->data() != 0);
99 QVERIFY(((const QAudioBuffer*)mEmpty)->data() != 0);
100 QCOMPARE(mEmpty->sampleCount(), 1000);
101 QCOMPARE(mEmpty->frameCount(), 500);
102 QCOMPARE(mEmpty->duration(), 50000LL);
103 QCOMPARE(mEmpty->byteCount(), 2000);
104 QCOMPARE(mEmpty->startTime(), -1LL);
105
106 // bytearray buffer
107 QVERIFY(mFromArray->isValid());
108 QVERIFY(mFromArray->constData() != 0);
109 QVERIFY(mFromArray->data() != 0);
110 QVERIFY(((const QAudioBuffer*)mFromArray)->data() != 0);
111 /// 4000 bytes at 10KHz, 2ch, 16bit = 40kBps -> 0.1s
112 QCOMPARE(mFromArray->duration(), 100000LL);
113 QCOMPARE(mFromArray->byteCount(), 4000);
114 QCOMPARE(mFromArray->sampleCount(), 2000);
115 QCOMPARE(mFromArray->frameCount(), 1000);
116 QCOMPARE(mFromArray->startTime(), -1LL);
117
118
119 // Now some invalid buffers
120 QAudioBuffer badFormat(1000, QAudioFormat());
121 QVERIFY(!badFormat.isValid());
122 QVERIFY(badFormat.constData() == 0);
123 QVERIFY(badFormat.data() == 0);
124 QVERIFY(((const QAudioBuffer*)&badFormat)->data() == 0);
125 QCOMPARE(badFormat.duration(), 0LL);
126 QCOMPARE(badFormat.byteCount(), 0);
127 QCOMPARE(badFormat.sampleCount(), 0);
128 QCOMPARE(badFormat.frameCount(), 0);
129 QCOMPARE(badFormat.startTime(), -1LL);
130
131 QAudioBuffer badArray(QByteArray(), mFormat);
132 QVERIFY(!badArray.isValid());
133 QVERIFY(badArray.constData() == 0);
134 QVERIFY(badArray.data() == 0);
135 QVERIFY(((const QAudioBuffer*)&badArray)->data() == 0);
136 QCOMPARE(badArray.duration(), 0LL);
137 QCOMPARE(badArray.byteCount(), 0);
138 QCOMPARE(badArray.sampleCount(), 0);
139 QCOMPARE(badArray.frameCount(), 0);
140 QCOMPARE(badArray.startTime(), -1LL);
141
142 QAudioBuffer badBoth = QAudioBuffer(QByteArray(), QAudioFormat());
143 QVERIFY(!badBoth.isValid());
144 QVERIFY(badBoth.constData() == 0);
145 QVERIFY(badBoth.data() == 0);
146 QVERIFY(((const QAudioBuffer*)&badBoth)->data() == 0);
147 QCOMPARE(badBoth.duration(), 0LL);
148 QCOMPARE(badBoth.byteCount(), 0);
149 QCOMPARE(badBoth.sampleCount(), 0);
150 QCOMPARE(badBoth.frameCount(), 0);
151 QCOMPARE(badBoth.startTime(), -1LL);
152}
153
154void tst_QAudioBuffer::assign()
155{
156 // TODO Needs strong behaviour definition
157}
158
159void tst_QAudioBuffer::constData() const
160{
161 const void *data = mEmpty->constData();
162 QVERIFY(data != 0);
163
164 const unsigned int *idata = reinterpret_cast<const unsigned int*>(data);
165 QCOMPARE(*idata, 0U);
166
167 const QAudioBuffer::S8U *sdata = mEmpty->constData<QAudioBuffer::S8U>();
168 QVERIFY(sdata);
169 QCOMPARE(sdata->left, (unsigned char)0);
170 QCOMPARE(sdata->right, (unsigned char)0);
171
172 // The bytearray one should be 0x80
173 data = mFromArray->constData();
174 QVERIFY(data != 0);
175
176 idata = reinterpret_cast<const unsigned int *>(data);
177 QEXPECT_FAIL("", "Unsigned 16bits are cleared to 0x8080 currently", Continue);
178 QCOMPARE(*idata, 0x80008000);
179
180 sdata = mFromArray->constData<QAudioBuffer::S8U>();
181 QCOMPARE(sdata->left, (unsigned char)0x80);
182 QCOMPARE(sdata->right, (unsigned char)0x80);
183}
184
185void tst_QAudioBuffer::data_const() const
186{
187 const void *data = ((const QAudioBuffer*)mEmpty)->data();
188 QVERIFY(data != 0);
189
190 const unsigned int *idata = reinterpret_cast<const unsigned int*>(data);
191 QCOMPARE(*idata, 0U);
192
193 const QAudioBuffer::S8U *sdata = ((const QAudioBuffer*)mEmpty)->constData<QAudioBuffer::S8U>();
194 QVERIFY(sdata);
195 QCOMPARE(sdata->left, (unsigned char)0);
196 QCOMPARE(sdata->right, (unsigned char)0);
197
198 // The bytearray one should be 0x80
199 data = ((const QAudioBuffer*)mFromArray)->data();
200 QVERIFY(data != 0);
201
202 idata = reinterpret_cast<const unsigned int *>(data);
203 QEXPECT_FAIL("", "Unsigned 16bits are cleared to 0x8080 currently", Continue);
204 QCOMPARE(*idata, 0x80008000);
205
206 sdata = ((const QAudioBuffer*)mFromArray)->constData<QAudioBuffer::S8U>();
207 QCOMPARE(sdata->left, (unsigned char)0x80);
208 QCOMPARE(sdata->right, (unsigned char)0x80);
209}
210
211void tst_QAudioBuffer::data()
212{
213 void *data = mEmpty->data();
214 QVERIFY(data != 0);
215
216 unsigned int *idata = reinterpret_cast<unsigned int*>(data);
217 QCOMPARE(*idata, 0U);
218
219 QAudioBuffer::S8U *sdata = mEmpty->data<QAudioBuffer::S8U>();
220 QVERIFY(sdata);
221 QCOMPARE(sdata->left, (unsigned char)0);
222 QCOMPARE(sdata->right, (unsigned char)0);
223
224 // The bytearray one should be 0x80
225 data = mFromArray->data();
226 QVERIFY(data != 0);
227
228 idata = reinterpret_cast<unsigned int *>(data);
229 QEXPECT_FAIL("", "Unsigned 16bits are cleared to 0x8080 currently", Continue);
230 QCOMPARE(*idata, 0x80008000);
231
232 sdata = mFromArray->data<QAudioBuffer::S8U>();
233 QCOMPARE(sdata->left, (unsigned char)0x80);
234 QCOMPARE(sdata->right, (unsigned char)0x80);
235}
236
237void tst_QAudioBuffer::durations()
238{
239 QFETCH(int, channelCount);
240 QFETCH(int, sampleSize);
241 QFETCH(int, frameCount);
242 int sampleCount = frameCount * channelCount;
243 QFETCH(QAudioFormat::SampleType, sampleType);
244 QFETCH(int, sampleRate);
245 QFETCH(qint64, duration);
246 QFETCH(int, byteCount);
247
248 QAudioFormat f;
249 f.setChannelCount(channelCount);
250 f.setSampleType(sampleType);
251 f.setSampleSize(sampleSize);
252 f.setSampleRate(sampleRate);
253 f.setCodec("audio/pcm");
254
255 QAudioBuffer b(frameCount, f);
256
257 QCOMPARE(b.frameCount(), frameCount);
258 QCOMPARE(b.sampleCount(), sampleCount);
259 QCOMPARE(b.duration(), duration);
260 QCOMPARE(b.byteCount(), byteCount);
261}
262
263void tst_QAudioBuffer::durations_data()
264{
265 QTest::addColumn<int>(name: "channelCount");
266 QTest::addColumn<int>(name: "sampleSize");
267 QTest::addColumn<int>(name: "frameCount");
268 QTest::addColumn<QAudioFormat::SampleType>(name: "sampleType");
269 QTest::addColumn<int>(name: "sampleRate");
270 QTest::addColumn<qint64>(name: "duration");
271 QTest::addColumn<int>(name: "byteCount");
272 QTest::newRow(dataTag: "M8_1000_8K") << 1 << 8 << 1000 << QAudioFormat::UnSignedInt << 8000 << 125000LL << 1000;
273 QTest::newRow(dataTag: "M8_2000_8K") << 1 << 8 << 2000 << QAudioFormat::UnSignedInt << 8000 << 250000LL << 2000;
274 QTest::newRow(dataTag: "M8_1000_4K") << 1 << 8 << 1000 << QAudioFormat::UnSignedInt << 4000 << 250000LL << 1000;
275
276 QTest::newRow(dataTag: "S8_1000_8K") << 2 << 8 << 500 << QAudioFormat::UnSignedInt << 8000 << 62500LL << 1000;
277
278 QTest::newRow(dataTag: "SF_1000_8K") << 2 << 32 << 500 << QAudioFormat::Float << 8000 << 62500LL << 4000;
279
280 QTest::newRow(dataTag: "4x128_1000_16K") << 4 << 128 << 250 << QAudioFormat::SignedInt << 16000 << 15625LL << 16000;
281}
282
283void tst_QAudioBuffer::stereoSample()
284{
285 // Uninitialized (should default to zero level for type)
286 QAudioBuffer::S8U s8u;
287 QAudioBuffer::S8S s8s;
288 QAudioBuffer::S16U s16u;
289 QAudioBuffer::S16S s16s;
290 QAudioBuffer::S32F s32f;
291
292 QCOMPARE(s8u.left, (unsigned char) 0x80);
293 QCOMPARE(s8u.right, (unsigned char) 0x80);
294 QCOMPARE(s8u.average(), (unsigned char) 0x80);
295
296 QCOMPARE(s8s.left, (signed char) 0x00);
297 QCOMPARE(s8s.right, (signed char) 0x00);
298 QCOMPARE(s8s.average(), (signed char) 0x0);
299
300 QCOMPARE(s16u.left, (unsigned short) 0x8000);
301 QCOMPARE(s16u.right, (unsigned short) 0x8000);
302 QCOMPARE(s16u.average(), (unsigned short) 0x8000);
303
304 QCOMPARE(s16s.left, (signed short) 0x0);
305 QCOMPARE(s16s.right, (signed short) 0x0);
306 QCOMPARE(s16s.average(), (signed short) 0x0);
307
308 QCOMPARE(s32f.left, 0.0f);
309 QCOMPARE(s32f.right, 0.0f);
310 QCOMPARE(s32f.average(), 0.0f);
311
312 // Initialized
313 QAudioBuffer::S8U s8u2(34, 145);
314 QAudioBuffer::S8S s8s2(23, -89);
315 QAudioBuffer::S16U s16u2(500, 45000);
316 QAudioBuffer::S16S s16s2(-10000, 346);
317 QAudioBuffer::S32F s32f2(500.7f, -123.1f);
318
319 QCOMPARE(s8u2.left, (unsigned char) 34);
320 QCOMPARE(s8u2.right, (unsigned char) 145);
321 QCOMPARE(s8u2.average(), (unsigned char) 89);
322
323 QCOMPARE(s8s2.left, (signed char) 23);
324 QCOMPARE(s8s2.right,(signed char) -89);
325 QCOMPARE(s8s2.average(), (signed char) -33);
326
327 QCOMPARE(s16u2.left, (unsigned short) 500);
328 QCOMPARE(s16u2.right, (unsigned short) 45000);
329 QCOMPARE(s16u2.average(), (unsigned short) 22750);
330
331 QCOMPARE(s16s2.left, (signed short) -10000);
332 QCOMPARE(s16s2.right, (signed short) 346);
333 QCOMPARE(s16s2.average(), (signed short) (-5000 + 173));
334
335 QCOMPARE(s32f2.left, 500.7f);
336 QCOMPARE(s32f2.right, -123.1f);
337 QCOMPARE(s32f2.average(), (500.7f - 123.1f)/2);
338
339 // Assigned
340 s8u = s8u2;
341 s8s = s8s2;
342 s16u = s16u2;
343 s16s = s16s2;
344 s32f = s32f2;
345
346 QCOMPARE(s8u.left, (unsigned char) 34);
347 QCOMPARE(s8u.right, (unsigned char) 145);
348 QCOMPARE(s8u.average(), (unsigned char) 89);
349
350 QCOMPARE(s8s.left, (signed char) 23);
351 QCOMPARE(s8s.right, (signed char) -89);
352 QCOMPARE(s8s.average(), (signed char) -33);
353
354 QCOMPARE(s16u.left, (unsigned short) 500);
355 QCOMPARE(s16u.right, (unsigned short) 45000);
356 QCOMPARE(s16u.average(), (unsigned short) 22750);
357
358 QCOMPARE(s16s.left, (signed short) -10000);
359 QCOMPARE(s16s.right, (signed short) 346);
360 QCOMPARE(s16s.average(), (signed short) (-5000 + 173));
361
362 QCOMPARE(s32f.left, 500.7f);
363 QCOMPARE(s32f.right, -123.1f);
364 QCOMPARE(s32f.average(), (500.7f - 123.1f)/2);
365
366 // Cleared
367 s8u.clear();
368 s8s.clear();
369 s16u.clear();
370 s16s.clear();
371 s32f.clear();
372
373 QCOMPARE(s8u.left, (unsigned char) 0x80);
374 QCOMPARE(s8u.right, (unsigned char) 0x80);
375 QCOMPARE(s8u.average(), (unsigned char) 0x80);
376
377 QCOMPARE(s8s.left, (signed char) 0x00);
378 QCOMPARE(s8s.right, (signed char) 0x00);
379 QCOMPARE(s8s.average(), (signed char) 0x0);
380
381 QCOMPARE(s16u.left, (unsigned short) 0x8000);
382 QCOMPARE(s16u.right, (unsigned short) 0x8000);
383 QCOMPARE(s16u.average(), (unsigned short) 0x8000);
384
385 QCOMPARE(s16s.left, (signed short) 0x0);
386 QCOMPARE(s16s.right, (signed short) 0x0);
387 QCOMPARE(s16s.average(), (signed short) 0x0);
388
389 QCOMPARE(s32f.left, 0.0f);
390 QCOMPARE(s32f.right, 0.0f);
391 QCOMPARE(s32f.average(), 0.0f);
392}
393
394
395QTEST_APPLESS_MAIN(tst_QAudioBuffer);
396
397#include "tst_qaudiobuffer.moc"
398

source code of qtmultimedia/tests/auto/unit/qaudiobuffer/tst_qaudiobuffer.cpp