diff options
author | Michael Goddard <michael.goddard@nokia.com> | 2012-02-02 15:28:21 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-02-06 23:18:56 +0100 |
commit | 3c4dcf00bb9922ed83f1defcd52092857da5103e (patch) | |
tree | 3d80472f02dace138a585f02cb7740e01b1cc3d6 /tests | |
parent | ee39683e273f705bfcffde8b6a9908230010843c (diff) |
Add a QAudioBuffer class.
Prereq for probing or decoding. The abstract API probably needs
to change.
Change-Id: Ie0bf796c1f581f34bbc0a8af2dffc387c513a330
Reviewed-by: Jonas Rabbe <jonas.rabbe@nokia.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/unit/multimedia.pro | 2 | ||||
-rw-r--r-- | tests/auto/unit/qaudiobuffer/qaudiobuffer.pro | 17 | ||||
-rw-r--r-- | tests/auto/unit/qaudiobuffer/tst_qaudiobuffer.cpp | 369 |
3 files changed, 388 insertions, 0 deletions
diff --git a/tests/auto/unit/multimedia.pro b/tests/auto/unit/multimedia.pro index 1fe7e7c55..a662d4413 100644 --- a/tests/auto/unit/multimedia.pro +++ b/tests/auto/unit/multimedia.pro @@ -29,6 +29,7 @@ SUBDIRS += \ qvideoframe \ qvideosurfaceformat \ qwavedecoder \ + qaudiobuffer # Tests depending on private interfaces should only be built if # these interfaces are exported. @@ -36,3 +37,4 @@ contains (QT_CONFIG, private_tests) { SUBDIRS += \ qdeclarativeaudio } + diff --git a/tests/auto/unit/qaudiobuffer/qaudiobuffer.pro b/tests/auto/unit/qaudiobuffer/qaudiobuffer.pro new file mode 100644 index 000000000..cbbaefe65 --- /dev/null +++ b/tests/auto/unit/qaudiobuffer/qaudiobuffer.pro @@ -0,0 +1,17 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2012-02-02T23:40:38 +# +#------------------------------------------------- + +QT += multimedia testlib +QT -= gui + +TARGET = tst_qaudiobuffer +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + +SOURCES += tst_qaudiobuffer.cpp +DEFINES += SRCDIR=\\\"$$PWD/\\\" diff --git a/tests/auto/unit/qaudiobuffer/tst_qaudiobuffer.cpp b/tests/auto/unit/qaudiobuffer/tst_qaudiobuffer.cpp new file mode 100644 index 000000000..7339cc772 --- /dev/null +++ b/tests/auto/unit/qaudiobuffer/tst_qaudiobuffer.cpp @@ -0,0 +1,369 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtCore/QString> +#include <QtTest/QtTest> + +#include <qaudiobuffer.h> + +class tst_QAudioBuffer : public QObject +{ + Q_OBJECT + +public: + tst_QAudioBuffer(); + ~tst_QAudioBuffer(); + +private Q_SLOTS: + void ctors(); + void assign(); + void constData() const; + void data_const() const; + void data(); + void durations(); + void durations_data(); + void stereoSample(); + +private: + QAudioFormat mFormat; + QAudioBuffer *mNull; + QAudioBuffer *mEmpty; + QAudioBuffer *mFromArray; +}; + +tst_QAudioBuffer::tst_QAudioBuffer() +{ + // Initialize some common buffers + mFormat.setChannelCount(2); + mFormat.setSampleSize(16); + mFormat.setSampleType(QAudioFormat::UnSignedInt); + mFormat.setSampleRate(10000); + mFormat.setCodec("audio/x-pcm"); // XXX this is not a good fit? + + QByteArray b(4000, 0x80); + mNull = new QAudioBuffer; + mEmpty = new QAudioBuffer(1000, mFormat); + mFromArray = new QAudioBuffer(b, mFormat); +} + + +tst_QAudioBuffer::~tst_QAudioBuffer() +{ + delete mNull; + delete mEmpty; + delete mFromArray; +} + +void tst_QAudioBuffer::ctors() +{ + // Null buffer + QVERIFY(!mNull->isValid()); + QVERIFY(mNull->constData() == 0); + QVERIFY(mNull->data() == 0); + QVERIFY(((const QAudioBuffer*)mNull)->data() == 0); + QCOMPARE(mNull->duration(), 0LL); + QCOMPARE(mNull->byteCount(), 0); + QCOMPARE(mNull->sampleCount(), 0); + QCOMPARE(mNull->startTime(), -1LL); + + // Empty buffer + QVERIFY(mEmpty->isValid()); + QVERIFY(mEmpty->constData() != 0); + QVERIFY(mEmpty->data() != 0); + QVERIFY(((const QAudioBuffer*)mEmpty)->data() != 0); + QCOMPARE(mEmpty->duration(), 50000LL); + QCOMPARE(mEmpty->byteCount(), 4000); + QCOMPARE(mEmpty->sampleCount(), 1000); + QCOMPARE(mEmpty->startTime(), -1LL); + + // bytearray buffer + QVERIFY(mFromArray->isValid()); + QVERIFY(mFromArray->constData() != 0); + QVERIFY(mFromArray->data() != 0); + QVERIFY(((const QAudioBuffer*)mFromArray)->data() != 0); + QCOMPARE(mFromArray->duration(), 50000LL); // 4000 bytes + QCOMPARE(mFromArray->byteCount(), 4000); + QCOMPARE(mFromArray->sampleCount(), 1000); + QCOMPARE(mFromArray->startTime(), -1LL); +} + +void tst_QAudioBuffer::assign() +{ + // Needs strong behaviour definition +} + +void tst_QAudioBuffer::constData() const +{ + const void *data = mEmpty->constData(); + QVERIFY(data != 0); + + const unsigned int *idata = reinterpret_cast<const unsigned int*>(data); + QCOMPARE(*idata, 0U); + + const QAudioBuffer::S8U *sdata = mEmpty->constData<QAudioBuffer::S8U>(); + QVERIFY(sdata); + QCOMPARE(sdata->left, (unsigned char)0); + QCOMPARE(sdata->right, (unsigned char)0); + + // The bytearray one should be 0x80 + data = mFromArray->constData(); + QVERIFY(data != 0); + + idata = reinterpret_cast<const unsigned int *>(data); + QEXPECT_FAIL("", "Unsigned 16bits are cleared to 0x8080 currently", Continue); + QCOMPARE(*idata, 0x80008000); + + sdata = mFromArray->constData<QAudioBuffer::S8U>(); + QCOMPARE(sdata->left, (unsigned char)0x80); + QCOMPARE(sdata->right, (unsigned char)0x80); +} + +void tst_QAudioBuffer::data_const() const +{ + const void *data = ((const QAudioBuffer*)mEmpty)->data(); + QVERIFY(data != 0); + + const unsigned int *idata = reinterpret_cast<const unsigned int*>(data); + QCOMPARE(*idata, 0U); + + const QAudioBuffer::S8U *sdata = ((const QAudioBuffer*)mEmpty)->constData<QAudioBuffer::S8U>(); + QVERIFY(sdata); + QCOMPARE(sdata->left, (unsigned char)0); + QCOMPARE(sdata->right, (unsigned char)0); + + // The bytearray one should be 0x80 + data = ((const QAudioBuffer*)mFromArray)->data(); + QVERIFY(data != 0); + + idata = reinterpret_cast<const unsigned int *>(data); + QEXPECT_FAIL("", "Unsigned 16bits are cleared to 0x8080 currently", Continue); + QCOMPARE(*idata, 0x80008000); + + sdata = ((const QAudioBuffer*)mFromArray)->constData<QAudioBuffer::S8U>(); + QCOMPARE(sdata->left, (unsigned char)0x80); + QCOMPARE(sdata->right, (unsigned char)0x80); +} + +void tst_QAudioBuffer::data() +{ + void *data = mEmpty->data(); + QVERIFY(data != 0); + + unsigned int *idata = reinterpret_cast<unsigned int*>(data); + QCOMPARE(*idata, 0U); + + QAudioBuffer::S8U *sdata = mEmpty->data<QAudioBuffer::S8U>(); + QVERIFY(sdata); + QCOMPARE(sdata->left, (unsigned char)0); + QCOMPARE(sdata->right, (unsigned char)0); + + // The bytearray one should be 0x80 + data = mFromArray->data(); + QVERIFY(data != 0); + + idata = reinterpret_cast<unsigned int *>(data); + QEXPECT_FAIL("", "Unsigned 16bits are cleared to 0x8080 currently", Continue); + QCOMPARE(*idata, 0x80008000); + + sdata = mFromArray->data<QAudioBuffer::S8U>(); + QCOMPARE(sdata->left, (unsigned char)0x80); + QCOMPARE(sdata->right, (unsigned char)0x80); +} + +void tst_QAudioBuffer::durations() +{ + QFETCH(int, channelCount); + QFETCH(int, sampleSize); + QFETCH(int, sampleCount); + QFETCH(QAudioFormat::SampleType, sampleType); + QFETCH(int, sampleRate); + QFETCH(qint64, duration); + QFETCH(int, byteCount); + + QAudioFormat f; + f.setChannelCount(channelCount); + f.setSampleType(sampleType); + f.setSampleSize(sampleSize); + f.setSampleRate(sampleRate); + f.setCodec("audio/x-pcm"); // XXX this is not a good fit? + + QAudioBuffer b(sampleCount, f); + + QCOMPARE(b.sampleCount(), sampleCount); + QCOMPARE(b.duration(), duration); + QCOMPARE(b.byteCount(), byteCount); +} + +void tst_QAudioBuffer::durations_data() +{ + QTest::addColumn<int>("channelCount"); + QTest::addColumn<int>("sampleSize"); + QTest::addColumn<int>("sampleCount"); + QTest::addColumn<QAudioFormat::SampleType>("sampleType"); + QTest::addColumn<int>("sampleRate"); + QTest::addColumn<qint64>("duration"); + QTest::addColumn<int>("byteCount"); + QTest::newRow("M8_1000_8K") << 1 << 8 << 1000 << QAudioFormat::UnSignedInt << 8000 << 125000LL << 1000; + QTest::newRow("M8_2000_8K") << 1 << 8 << 2000 << QAudioFormat::UnSignedInt << 8000 << 250000LL << 2000; + QTest::newRow("M8_1000_4K") << 1 << 8 << 1000 << QAudioFormat::UnSignedInt << 4000 << 250000LL << 1000; + + QTest::newRow("S8_1000_8K") << 2 << 8 << 1000 << QAudioFormat::UnSignedInt << 8000 << 62500LL << 2000; + + QTest::newRow("SF_1000_8K") << 2 << 32 << 1000 << QAudioFormat::Float << 8000 << 62500LL << 8000; + + QTest::newRow("4x128_1000_16K") << 4 << 128 << 1000 << QAudioFormat::SignedInt << 16000 << 15625LL << 64000; +} + +void tst_QAudioBuffer::stereoSample() +{ + // Uninitialized (should default to zero level for type) + QAudioBuffer::S8U s8u; + QAudioBuffer::S8S s8s; + QAudioBuffer::S16U s16u; + QAudioBuffer::S16S s16s; + QAudioBuffer::S32F s32f; + + QCOMPARE(s8u.left, (unsigned char) 0x80); + QCOMPARE(s8u.right, (unsigned char) 0x80); + QCOMPARE(s8u.average(), (unsigned char) 0x80); + + QCOMPARE(s8s.left, (signed char) 0x00); + QCOMPARE(s8s.right, (signed char) 0x00); + QCOMPARE(s8s.average(), (signed char) 0x0); + + QCOMPARE(s16u.left, (unsigned short) 0x8000); + QCOMPARE(s16u.right, (unsigned short) 0x8000); + QCOMPARE(s16u.average(), (unsigned short) 0x8000); + + QCOMPARE(s16s.left, (signed short) 0x0); + QCOMPARE(s16s.right, (signed short) 0x0); + QCOMPARE(s16s.average(), (signed short) 0x0); + + QCOMPARE(s32f.left, 0.0f); + QCOMPARE(s32f.right, 0.0f); + QCOMPARE(s32f.average(), 0.0f); + + // Initialized + QAudioBuffer::S8U s8u2(34, 145); + QAudioBuffer::S8S s8s2(23, -89); + QAudioBuffer::S16U s16u2(500, 45000); + QAudioBuffer::S16S s16s2(-10000, 346); + QAudioBuffer::S32F s32f2(500.7f, -123.1f); + + QCOMPARE(s8u2.left, (unsigned char) 34); + QCOMPARE(s8u2.right, (unsigned char) 145); + QCOMPARE(s8u2.average(), (unsigned char) 89); + + QCOMPARE(s8s2.left, (signed char) 23); + QCOMPARE(s8s2.right,(signed char) -89); + QCOMPARE(s8s2.average(), (signed char) -33); + + QCOMPARE(s16u2.left, (unsigned short) 500); + QCOMPARE(s16u2.right, (unsigned short) 45000); + QCOMPARE(s16u2.average(), (unsigned short) 22750); + + QCOMPARE(s16s2.left, (signed short) -10000); + QCOMPARE(s16s2.right, (signed short) 346); + QCOMPARE(s16s2.average(), (signed short) (-5000 + 173)); + + QCOMPARE(s32f2.left, 500.7f); + QCOMPARE(s32f2.right, -123.1f); + QCOMPARE(s32f2.average(), (500.7f - 123.1f)/2); + + // Assigned + s8u = s8u2; + s8s = s8s2; + s16u = s16u2; + s16s = s16s2; + s32f = s32f2; + + QCOMPARE(s8u.left, (unsigned char) 34); + QCOMPARE(s8u.right, (unsigned char) 145); + QCOMPARE(s8u.average(), (unsigned char) 89); + + QCOMPARE(s8s.left, (signed char) 23); + QCOMPARE(s8s.right, (signed char) -89); + QCOMPARE(s8s.average(), (signed char) -33); + + QCOMPARE(s16u.left, (unsigned short) 500); + QCOMPARE(s16u.right, (unsigned short) 45000); + QCOMPARE(s16u.average(), (unsigned short) 22750); + + QCOMPARE(s16s.left, (signed short) -10000); + QCOMPARE(s16s.right, (signed short) 346); + QCOMPARE(s16s.average(), (signed short) (-5000 + 173)); + + QCOMPARE(s32f.left, 500.7f); + QCOMPARE(s32f.right, -123.1f); + QCOMPARE(s32f.average(), (500.7f - 123.1f)/2); + + // Cleared + s8u.clear(); + s8s.clear(); + s16u.clear(); + s16s.clear(); + s32f.clear(); + + QCOMPARE(s8u.left, (unsigned char) 0x80); + QCOMPARE(s8u.right, (unsigned char) 0x80); + QCOMPARE(s8u.average(), (unsigned char) 0x80); + + QCOMPARE(s8s.left, (signed char) 0x00); + QCOMPARE(s8s.right, (signed char) 0x00); + QCOMPARE(s8s.average(), (signed char) 0x0); + + QCOMPARE(s16u.left, (unsigned short) 0x8000); + QCOMPARE(s16u.right, (unsigned short) 0x8000); + QCOMPARE(s16u.average(), (unsigned short) 0x8000); + + QCOMPARE(s16s.left, (signed short) 0x0); + QCOMPARE(s16s.right, (signed short) 0x0); + QCOMPARE(s16s.average(), (signed short) 0x0); + + QCOMPARE(s32f.left, 0.0f); + QCOMPARE(s32f.right, 0.0f); + QCOMPARE(s32f.average(), 0.0f); +} + + +QTEST_APPLESS_MAIN(tst_QAudioBuffer); + +#include "tst_qaudiobuffer.moc" |