summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYoann Lopes <yoann.lopes@qt.io>2016-05-13 17:13:03 +0200
committerYoann Lopes <yoann.lopes@qt.io>2016-06-03 21:11:58 +0000
commitfe46759fc0a39418a3b5f32cf78a162c35a7aef7 (patch)
treeb043309dc6ee6a3de2b7477113125adda6ae1911
parent78a73643a31a761ada8cfafa80ddf2fb916c1d6a (diff)
Add new volume conversion API.
All Qt Multimedia volume APIs expect a linear volume factor. However, UI volume controls should typically use a logarithmic or cubic scale to match how the human ear perceives loudness. The new helper function (C++ and QML) provides a way to do the conversion from one scale to another. Change-Id: If7795426b728ab0e8fd6635988dbc9be795e3e5e Reviewed-by: Christian Stromme <christian.stromme@qt.io>
-rw-r--r--src/imports/multimedia/qdeclarativemultimediaglobal.cpp51
-rw-r--r--src/imports/multimedia/qdeclarativemultimediaglobal_p.h11
-rw-r--r--src/multimedia/audio/qaudio.cpp113
-rw-r--r--src/multimedia/audio/qaudio.h10
-rw-r--r--src/multimedia/doc/snippets/multimedia-snippets/audio.cpp16
-rw-r--r--tests/auto/unit/qaudionamespace/tst_qaudionamespace.cpp103
-rw-r--r--tests/auto/unit/qdeclarativemultimediaglobal/tst_qdeclarativemultimediaglobal.qml67
7 files changed, 370 insertions, 1 deletions
diff --git a/src/imports/multimedia/qdeclarativemultimediaglobal.cpp b/src/imports/multimedia/qdeclarativemultimediaglobal.cpp
index b2a1aed12..bb1b9c9d8 100644
--- a/src/imports/multimedia/qdeclarativemultimediaglobal.cpp
+++ b/src/imports/multimedia/qdeclarativemultimediaglobal.cpp
@@ -183,4 +183,55 @@ QJSValue QDeclarativeMultimediaGlobal::availableCameras() const
return availableCameras;
}
+/*!
+ \qmlmethod real QtMultimedia::QtMultimedia::convertVolume(real volume, VolumeScale from, VolumeScale to)
+
+ Converts an audio \a volume \a from a volume scale \a to another, and returns the result.
+
+ Depending on the context, different scales are used to represent audio volume. All Qt Multimedia
+ classes that have an audio volume use a linear scale, the reason is that the loudness of a
+ speaker is controlled by modulating its voltage on a linear scale. The human ear on the other
+ hand, perceives loudness in a logarithmic way. That is why the decibel scale, being a logarithmic
+ scale, is typically used to define sound levels. UI volume controls in professional audio
+ applications usually use a decibel scale. The cubic scale is a computationally cheap
+ approximation of a logarithmic scale, most applications should use a cubic scale for their UI
+ volume controls.
+
+ Valid values for \a from and \a to are:
+ \list
+ \li QtMultimedia.LinearVolumeScale - Linear scale. \c 0.0 (0%) is silence and \c 1.0 (100%) is
+ full volume. All Qt Multimedia types that have an audio volume use a linear scale.
+ \li QtMultimedia.CubicVolumeScale - Cubic scale. \c 0.0 (0%) is silence and \c 1.0 (100%) is full
+ volume. UI volume controls should usually use a cubic scale.
+ \li QtMultimedia.DecibelVolumeScale - Decibel (dB, amplitude) logarithmic scale. \c -200 is
+ silence and \c 0 is full volume.
+ \endlist
+
+ The following example shows how the volume value from a UI volume control can be converted so
+ that the perceived increase in loudness is the same when increasing the volume control from 0.2
+ to 0.3 as it is from 0.5 to 0.6:
+
+ \code
+ Slider {
+ id: volumeSlider
+
+ property real volume: QtMultimedia.convertVolume(volumeSlider.value,
+ QtMultimedia.CubicVolumeScale,
+ QtMultimedia.LinearVolumeScale)
+ }
+
+ MediaPlayer {
+ volume: volumeSlider.volume
+ }
+ \endcode
+
+ \since 5.8
+*/
+qreal QDeclarativeMultimediaGlobal::convertVolume(qreal volume,
+ QDeclarativeMultimediaGlobal::VolumeScale from,
+ QDeclarativeMultimediaGlobal::VolumeScale to) const
+{
+ return QAudio::convertVolume(volume, QAudio::VolumeScale(from), QAudio::VolumeScale(to));
+}
+
QT_END_NAMESPACE
diff --git a/src/imports/multimedia/qdeclarativemultimediaglobal_p.h b/src/imports/multimedia/qdeclarativemultimediaglobal_p.h
index 2374b560c..a8413cc09 100644
--- a/src/imports/multimedia/qdeclarativemultimediaglobal_p.h
+++ b/src/imports/multimedia/qdeclarativemultimediaglobal_p.h
@@ -53,6 +53,7 @@
#include <QtQml/qqml.h>
#include <QtQml/qjsvalue.h>
+#include <QtMultimedia/qaudio.h>
QT_BEGIN_NAMESPACE
@@ -63,12 +64,22 @@ class QDeclarativeMultimediaGlobal : public QObject
Q_PROPERTY(QJSValue defaultCamera READ defaultCamera NOTIFY defaultCameraChanged)
Q_PROPERTY(QJSValue availableCameras READ availableCameras NOTIFY availableCamerasChanged)
+ Q_ENUMS(VolumeScale)
+
public:
+ enum VolumeScale {
+ LinearVolumeScale = QAudio::LinearVolumeScale,
+ CubicVolumeScale = QAudio::CubicVolumeScale,
+ DecibelVolumeScale = QAudio::DecibelVolumeScale
+ };
+
explicit QDeclarativeMultimediaGlobal(QJSEngine *engine, QObject *parent = 0);
QJSValue defaultCamera() const;
QJSValue availableCameras() const;
+ Q_INVOKABLE qreal convertVolume(qreal volume, VolumeScale from, VolumeScale to) const;
+
Q_SIGNALS:
// Unused at the moment. QCameraInfo doesn't notify when cameras are added or removed,
// but it might change in the future.
diff --git a/src/multimedia/audio/qaudio.cpp b/src/multimedia/audio/qaudio.cpp
index c708fa4f8..426c984df 100644
--- a/src/multimedia/audio/qaudio.cpp
+++ b/src/multimedia/audio/qaudio.cpp
@@ -39,6 +39,7 @@
#include <qaudio.h>
+#include <qmath.h>
#include <QDebug>
QT_BEGIN_NAMESPACE
@@ -49,6 +50,7 @@ static void qRegisterAudioMetaTypes()
qRegisterMetaType<QAudio::State>();
qRegisterMetaType<QAudio::Mode>();
qRegisterMetaType<QAudio::Role>();
+ qRegisterMetaType<QAudio::VolumeScale>();
}
Q_CONSTRUCTOR_FUNCTION(qRegisterAudioMetaTypes)
@@ -111,6 +113,98 @@ Q_CONSTRUCTOR_FUNCTION(qRegisterAudioMetaTypes)
\sa QMediaPlayer::setAudioRole()
*/
+/*!
+ \enum QAudio::VolumeScale
+
+ This enum defines the different audio volume scales.
+
+ \value LinearVolumeScale Linear scale. \c 0.0 (0%) is silence and \c 1.0 (100%) is full
+ volume. All Qt Multimedia classes that have an audio volume use a
+ linear scale.
+ \value CubicVolumeScale Cubic scale. \c 0.0 (0%) is silence and \c 1.0 (100%) is full
+ volume. UI volume controls should usually use a cubic scale.
+ \value DecibelVolumeScale Decibel (dB, amplitude) logarithmic scale. \c -200 is silence and
+ \c 0 is full volume.
+
+ \since 5.8
+ \sa QAudio::convertVolume()
+*/
+
+namespace QAudio
+{
+
+/*!
+ \fn qreal QAudio::convertVolume(qreal volume, VolumeScale from, VolumeScale to)
+
+ Converts an audio \a volume \a from a volume scale \a to another, and returns the result.
+
+ Depending on the context, different scales are used to represent audio volume. All Qt Multimedia
+ classes that have an audio volume use a linear scale, the reason is that the loudness of a
+ speaker is controlled by modulating its voltage on a linear scale. The human ear on the other
+ hand, perceives loudness in a logarithmic way. That is why the decibel scale, being a logarithmic
+ scale, is typically used to define sound levels. UI volume controls in professional audio
+ applications usually use a decibel scale. The cubic scale is a computationally cheap
+ approximation of a logarithmic scale, most applications should use a cubic scale for their UI
+ volume controls.
+
+ The following example shows how to convert the volume value from a slider control before passing
+ it to a QMediaPlayer. As a result, the perceived increase in volume is the same when increasing
+ the volume slider from 20 to 30 as it is from 50 to 60:
+
+ \snippet multimedia-snippets/audio.cpp Volume conversion
+
+ \since 5.8
+ \sa VolumeScale, QMediaPlayer::setVolume(), QAudioOutput::setVolume(),
+ QAudioInput::setVolume(), QSoundEffect::setVolume(), QMediaRecorder::setVolume()
+*/
+qreal convertVolume(qreal volume, VolumeScale from, VolumeScale to)
+{
+ switch (from) {
+ case LinearVolumeScale:
+ volume = qMax(qreal(0), volume);
+ switch (to) {
+ case LinearVolumeScale:
+ return volume;
+ case CubicVolumeScale:
+ return qPow(volume, qreal(1 / 3.0));
+ case DecibelVolumeScale:
+ if (volume < 0.001)
+ return qreal(-200);
+ else
+ return qreal(20.0) * std::log10(volume);
+ }
+ break;
+ case CubicVolumeScale:
+ volume = qMax(qreal(0), volume);
+ switch (to) {
+ case LinearVolumeScale:
+ return volume * volume * volume;
+ case CubicVolumeScale:
+ return volume;
+ case DecibelVolumeScale:
+ if (volume < 0.001)
+ return qreal(-200);
+ else
+ return qreal(3.0 * 20.0) * std::log10(volume);
+ }
+ break;
+ case DecibelVolumeScale:
+ switch (to) {
+ case LinearVolumeScale:
+ return qPow(qreal(10.0), volume / qreal(20.0));
+ case CubicVolumeScale:
+ return qPow(qreal(10.0), volume / qreal(3.0 * 20.0));
+ case DecibelVolumeScale:
+ return volume;
+ }
+ break;
+ }
+
+ return volume;
+}
+
+}
+
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, QAudio::Error error)
{
@@ -210,6 +304,25 @@ QDebug operator<<(QDebug dbg, QAudio::Role role)
}
return dbg;
}
+
+QDebug operator<<(QDebug dbg, QAudio::VolumeScale scale)
+{
+ QDebugStateSaver saver(dbg);
+ dbg.nospace();
+ switch (scale) {
+ case QAudio::LinearVolumeScale:
+ dbg << "LinearVolumeScale";
+ break;
+ case QAudio::CubicVolumeScale:
+ dbg << "CubicVolumeScale";
+ break;
+ case QAudio::DecibelVolumeScale:
+ dbg << "DecibelVolumeScale";
+ break;
+ }
+ return dbg;
+}
+
#endif
diff --git a/src/multimedia/audio/qaudio.h b/src/multimedia/audio/qaudio.h
index 449ddefae..9bbc6cf22 100644
--- a/src/multimedia/audio/qaudio.h
+++ b/src/multimedia/audio/qaudio.h
@@ -70,6 +70,14 @@ namespace QAudio
SonificationRole,
GameRole
};
+
+ enum VolumeScale {
+ LinearVolumeScale,
+ CubicVolumeScale,
+ DecibelVolumeScale
+ };
+
+ Q_MULTIMEDIA_EXPORT qreal convertVolume(qreal volume, VolumeScale from, VolumeScale to);
}
#ifndef QT_NO_DEBUG_STREAM
@@ -77,6 +85,7 @@ Q_MULTIMEDIA_EXPORT QDebug operator<<(QDebug dbg, QAudio::Error error);
Q_MULTIMEDIA_EXPORT QDebug operator<<(QDebug dbg, QAudio::State state);
Q_MULTIMEDIA_EXPORT QDebug operator<<(QDebug dbg, QAudio::Mode mode);
Q_MULTIMEDIA_EXPORT QDebug operator<<(QDebug dbg, QAudio::Role role);
+Q_MULTIMEDIA_EXPORT QDebug operator<<(QDebug dbg, QAudio::VolumeScale role);
#endif
QT_END_NAMESPACE
@@ -85,5 +94,6 @@ Q_DECLARE_METATYPE(QAudio::Error)
Q_DECLARE_METATYPE(QAudio::State)
Q_DECLARE_METATYPE(QAudio::Mode)
Q_DECLARE_METATYPE(QAudio::Role)
+Q_DECLARE_METATYPE(QAudio::VolumeScale)
#endif // QAUDIO_H
diff --git a/src/multimedia/doc/snippets/multimedia-snippets/audio.cpp b/src/multimedia/doc/snippets/multimedia-snippets/audio.cpp
index 310206405..736d1b306 100644
--- a/src/multimedia/doc/snippets/multimedia-snippets/audio.cpp
+++ b/src/multimedia/doc/snippets/multimedia-snippets/audio.cpp
@@ -47,6 +47,7 @@
#include "qaudiooutput.h"
#include "qaudioprobe.h"
#include "qaudiodecoder.h"
+#include "qmediaplayer.h"
class AudioInputExample : public QObject {
Q_OBJECT
@@ -247,3 +248,18 @@ void AudioDecodingExample::decode()
// Now wait for bufferReady() signal and call decoder->read()
//! [Local audio decoding]
}
+
+QMediaPlayer player;
+
+//! [Volume conversion]
+void applyVolume(int volumeSliderValue)
+{
+ // volumeSliderValue is in the range [0..100]
+
+ qreal linearVolume = QAudio::convertVolume(volumeSliderValue / qreal(100.0),
+ QAudio::CubicVolumeScale,
+ QAudio::LinearVolumeScale);
+
+ player.setVolume(qRound(linearVolume * 100));
+}
+//! [Volume conversion]
diff --git a/tests/auto/unit/qaudionamespace/tst_qaudionamespace.cpp b/tests/auto/unit/qaudionamespace/tst_qaudionamespace.cpp
index ebc58b66b..7907cd290 100644
--- a/tests/auto/unit/qaudionamespace/tst_qaudionamespace.cpp
+++ b/tests/auto/unit/qaudionamespace/tst_qaudionamespace.cpp
@@ -30,7 +30,7 @@
#include <QtTest/QtTest>
-#include "qaudio.h"
+#include <QtMultimedia/qaudio.h>
// Adds an enum, and the stringized version
#define ADD_ENUM_TEST(x) \
@@ -49,6 +49,10 @@ private slots:
void debugState_data();
void debugMode();
void debugMode_data();
+ void debugVolumeScale();
+ void debugVolumeScale_data();
+ void convertVolume();
+ void convertVolume_data();
};
void tst_QAudioNamespace::debugError_data()
@@ -109,6 +113,103 @@ void tst_QAudioNamespace::debugMode()
QTest::ignoreMessage(QtDebugMsg, stringized.toLatin1().constData());
qDebug() << mode;
}
+
+void tst_QAudioNamespace::debugVolumeScale_data()
+{
+ QTest::addColumn<QAudio::VolumeScale>("volumeScale");
+ QTest::addColumn<QString>("stringized");
+
+ ADD_ENUM_TEST(LinearVolumeScale);
+ ADD_ENUM_TEST(CubicVolumeScale);
+ ADD_ENUM_TEST(DecibelVolumeScale);
+}
+
+void tst_QAudioNamespace::debugVolumeScale()
+{
+ QFETCH(QAudio::VolumeScale, volumeScale);
+ QFETCH(QString, stringized);
+
+ QTest::ignoreMessage(QtDebugMsg, stringized.toLatin1().constData());
+ qDebug() << volumeScale;
+}
+
+void tst_QAudioNamespace::convertVolume_data()
+{
+ QTest::addColumn<qreal>("input");
+ QTest::addColumn<QAudio::VolumeScale>("from");
+ QTest::addColumn<QAudio::VolumeScale>("to");
+ QTest::addColumn<qreal>("expectedOutput");
+
+ QTest::newRow("-1.0 from linear to linear") << qreal(-1.0) << QAudio::LinearVolumeScale << QAudio::LinearVolumeScale << qreal(0.0);
+ QTest::newRow("0.0 from linear to linear") << qreal(0.0) << QAudio::LinearVolumeScale << QAudio::LinearVolumeScale << qreal(0.0);
+ QTest::newRow("0.5 from linear to linear") << qreal(0.5) << QAudio::LinearVolumeScale << QAudio::LinearVolumeScale << qreal(0.5);
+ QTest::newRow("1.0 from linear to linear") << qreal(1.0) << QAudio::LinearVolumeScale << QAudio::LinearVolumeScale << qreal(1.0);
+
+ QTest::newRow("-1.0 from linear to cubic") << qreal(-1.0) << QAudio::LinearVolumeScale << QAudio::CubicVolumeScale << qreal(0.0);
+ QTest::newRow("0.0 from linear to cubic") << qreal(0.0) << QAudio::LinearVolumeScale << QAudio::CubicVolumeScale << qreal(0.0);
+ QTest::newRow("0.33 from linear to cubic") << qreal(0.33) << QAudio::LinearVolumeScale << QAudio::CubicVolumeScale << qreal(0.69);
+ QTest::newRow("0.5 from linear to cubic") << qreal(0.5) << QAudio::LinearVolumeScale << QAudio::CubicVolumeScale << qreal(0.79);
+ QTest::newRow("0.72 from linear to cubic") << qreal(0.72) << QAudio::LinearVolumeScale << QAudio::CubicVolumeScale << qreal(0.89);
+ QTest::newRow("1.0 from linear to cubic") << qreal(1.0) << QAudio::LinearVolumeScale << QAudio::CubicVolumeScale << qreal(1.0);
+
+ QTest::newRow("-1.0 from linear to decibel") << qreal(-1.0) << QAudio::LinearVolumeScale << QAudio::DecibelVolumeScale << qreal(-200);
+ QTest::newRow("0.0 from linear to decibel") << qreal(0.0) << QAudio::LinearVolumeScale << QAudio::DecibelVolumeScale << qreal(-200);
+ QTest::newRow("0.33 from linear to decibel") << qreal(0.33) << QAudio::LinearVolumeScale << QAudio::DecibelVolumeScale << qreal(-9.63);
+ QTest::newRow("0.5 from linear to decibel") << qreal(0.5) << QAudio::LinearVolumeScale << QAudio::DecibelVolumeScale << qreal(-6.02);
+ QTest::newRow("0.72 from linear to decibel") << qreal(0.72) << QAudio::LinearVolumeScale << QAudio::DecibelVolumeScale << qreal(-2.85);
+ QTest::newRow("1.0 from linear to decibel") << qreal(1.0) << QAudio::LinearVolumeScale << QAudio::DecibelVolumeScale << qreal(0);
+
+ QTest::newRow("-1.0 from cubic to linear") << qreal(-1.0) << QAudio::CubicVolumeScale << QAudio::LinearVolumeScale << qreal(0.0);
+ QTest::newRow("0.0 from cubic to linear") << qreal(0.0) << QAudio::CubicVolumeScale << QAudio::LinearVolumeScale << qreal(0.0);
+ QTest::newRow("0.33 from cubic to linear") << qreal(0.33) << QAudio::CubicVolumeScale << QAudio::LinearVolumeScale << qreal(0.04);
+ QTest::newRow("0.5 from cubic to linear") << qreal(0.5) << QAudio::CubicVolumeScale << QAudio::LinearVolumeScale << qreal(0.13);
+ QTest::newRow("0.72 from cubic to linear") << qreal(0.72) << QAudio::CubicVolumeScale << QAudio::LinearVolumeScale << qreal(0.37);
+ QTest::newRow("1.0 from cubic to linear") << qreal(1.0) << QAudio::CubicVolumeScale << QAudio::LinearVolumeScale << qreal(1.0);
+
+ QTest::newRow("-1.0 from cubic to cubic") << qreal(-1.0) << QAudio::CubicVolumeScale << QAudio::CubicVolumeScale << qreal(0.0);
+ QTest::newRow("0.0 from cubic to cubic") << qreal(0.0) << QAudio::CubicVolumeScale << QAudio::CubicVolumeScale << qreal(0.0);
+ QTest::newRow("0.5 from cubic to cubic") << qreal(0.5) << QAudio::CubicVolumeScale << QAudio::CubicVolumeScale << qreal(0.5);
+ QTest::newRow("1.0 from cubic to cubic") << qreal(1.0) << QAudio::CubicVolumeScale << QAudio::CubicVolumeScale << qreal(1.0);
+
+ QTest::newRow("-1.0 from cubic to decibel") << qreal(-1.0) << QAudio::CubicVolumeScale << QAudio::DecibelVolumeScale << qreal(-200);
+ QTest::newRow("0.0 from cubic to decibel") << qreal(0.0) << QAudio::CubicVolumeScale << QAudio::DecibelVolumeScale << qreal(-200);
+ QTest::newRow("0.33 from cubic to decibel") << qreal(0.33) << QAudio::CubicVolumeScale << QAudio::DecibelVolumeScale << qreal(-28.89);
+ QTest::newRow("0.5 from cubic to decibel") << qreal(0.5) << QAudio::CubicVolumeScale << QAudio::DecibelVolumeScale << qreal(-18.06);
+ QTest::newRow("0.72 from cubic to decibel") << qreal(0.72) << QAudio::CubicVolumeScale << QAudio::DecibelVolumeScale << qreal(-8.56);
+ QTest::newRow("1.0 from cubic to decibel") << qreal(1.0) << QAudio::CubicVolumeScale << QAudio::DecibelVolumeScale << qreal(0);
+
+ QTest::newRow("-1000 from decibel to linear") << qreal(-1000.0) << QAudio::DecibelVolumeScale << QAudio::LinearVolumeScale << qreal(0.0);
+ QTest::newRow("-200 from decibel to linear") << qreal(-200.0) << QAudio::DecibelVolumeScale << QAudio::LinearVolumeScale << qreal(0.0);
+ QTest::newRow("-40 from decibel to linear") << qreal(-40.0) << QAudio::DecibelVolumeScale << QAudio::LinearVolumeScale << qreal(0.01);
+ QTest::newRow("-10 from decibel to linear") << qreal(-10.0) << QAudio::DecibelVolumeScale << QAudio::LinearVolumeScale << qreal(0.32);
+ QTest::newRow("-5 from decibel to linear") << qreal(-5.0) << QAudio::DecibelVolumeScale << QAudio::LinearVolumeScale << qreal(0.56);
+ QTest::newRow("0 from decibel to linear") << qreal(0.0) << QAudio::DecibelVolumeScale << QAudio::LinearVolumeScale << qreal(1.0);
+
+ QTest::newRow("-1000 from decibel to cubic") << qreal(-1000.0) << QAudio::DecibelVolumeScale << QAudio::CubicVolumeScale << qreal(0.0);
+ QTest::newRow("-200 from decibel to cubic") << qreal(-200.0) << QAudio::DecibelVolumeScale << QAudio::CubicVolumeScale << qreal(0.0);
+ QTest::newRow("-40 from decibel to cubic") << qreal(-40.0) << QAudio::DecibelVolumeScale << QAudio::CubicVolumeScale << qreal(0.22);
+ QTest::newRow("-10 from decibel to cubic") << qreal(-10.0) << QAudio::DecibelVolumeScale << QAudio::CubicVolumeScale << qreal(0.68);
+ QTest::newRow("-5 from decibel to cubic") << qreal(-5.0) << QAudio::DecibelVolumeScale << QAudio::CubicVolumeScale << qreal(0.83);
+ QTest::newRow("0 from decibel to cubic") << qreal(0.0) << QAudio::DecibelVolumeScale << QAudio::CubicVolumeScale << qreal(1.0);
+
+ QTest::newRow("-1000 from decibel to decibel") << qreal(-1000.0) << QAudio::DecibelVolumeScale << QAudio::DecibelVolumeScale << qreal(-1000.0);
+ QTest::newRow("-200 from decibel to decibel") << qreal(-200.0) << QAudio::DecibelVolumeScale << QAudio::DecibelVolumeScale << qreal(-200.0);
+ QTest::newRow("-30 from decibel to decibel") << qreal(-30.0) << QAudio::DecibelVolumeScale << QAudio::DecibelVolumeScale << qreal(-30.0);
+ QTest::newRow("0 from decibel to decibel") << qreal(0.0) << QAudio::DecibelVolumeScale << QAudio::DecibelVolumeScale << qreal(0.0);
+}
+
+void tst_QAudioNamespace::convertVolume()
+{
+ QFETCH(qreal, input);
+ QFETCH(QAudio::VolumeScale, from);
+ QFETCH(QAudio::VolumeScale, to);
+ QFETCH(qreal, expectedOutput);
+
+ qreal actualOutput = QAudio::convertVolume(input, from, to);
+ QVERIFY2(qAbs(actualOutput - expectedOutput) < 0.01,
+ QString("actual: %1, expected: %2").arg(actualOutput).arg(expectedOutput).toLocal8Bit().constData());
+}
+
QTEST_MAIN(tst_QAudioNamespace)
#include "tst_qaudionamespace.moc"
diff --git a/tests/auto/unit/qdeclarativemultimediaglobal/tst_qdeclarativemultimediaglobal.qml b/tests/auto/unit/qdeclarativemultimediaglobal/tst_qdeclarativemultimediaglobal.qml
index bce0ca791..22f4bf604 100644
--- a/tests/auto/unit/qdeclarativemultimediaglobal/tst_qdeclarativemultimediaglobal.qml
+++ b/tests/auto/unit/qdeclarativemultimediaglobal/tst_qdeclarativemultimediaglobal.qml
@@ -63,4 +63,71 @@ TestCase {
compare(camera.orientation, 0, "orientation");
}
+ function test_convertVolume_data() {
+ return [
+ { tag: "-1.0 from linear to linear", input: -1, from: QtMultimedia.LinearVolumeScale, to: QtMultimedia.LinearVolumeScale, expectedOutput: 0 },
+ { tag: "0.0 from linear to linear", input: 0, from: QtMultimedia.LinearVolumeScale, to: QtMultimedia.LinearVolumeScale, expectedOutput: 0 },
+ { tag: "0.5 from linear to linear", input: 0.5, from: QtMultimedia.LinearVolumeScale, to: QtMultimedia.LinearVolumeScale, expectedOutput: 0.5 },
+ { tag: "1.0 from linear to linear", input: 1.0, from: QtMultimedia.LinearVolumeScale, to: QtMultimedia.LinearVolumeScale, expectedOutput: 1.0 },
+
+ { tag: "-1.0 from linear to cubic", input: -1, from: QtMultimedia.LinearVolumeScale, to: QtMultimedia.CubicVolumeScale, expectedOutput: 0 },
+ { tag: "0.0 from linear to cubic", input: 0, from: QtMultimedia.LinearVolumeScale, to: QtMultimedia.CubicVolumeScale, expectedOutput: 0 },
+ { tag: "0.33 from linear to cubic", input: 0.33, from: QtMultimedia.LinearVolumeScale, to: QtMultimedia.CubicVolumeScale, expectedOutput: 0.69 },
+ { tag: "0.5 from linear to cubic", input: 0.5, from: QtMultimedia.LinearVolumeScale, to: QtMultimedia.CubicVolumeScale, expectedOutput: 0.79 },
+ { tag: "0.72 from linear to cubic", input: 0.72, from: QtMultimedia.LinearVolumeScale, to: QtMultimedia.CubicVolumeScale, expectedOutput: 0.89 },
+ { tag: "1.0 from linear to cubic", input: 1.0, from: QtMultimedia.LinearVolumeScale, to: QtMultimedia.CubicVolumeScale, expectedOutput: 1.0 },
+
+ { tag: "-1.0 from linear to decibel", input: -1, from: QtMultimedia.LinearVolumeScale, to: QtMultimedia.DecibelVolumeScale, expectedOutput: -200 },
+ { tag: "0.0 from linear to decibel", input: 0, from: QtMultimedia.LinearVolumeScale, to: QtMultimedia.DecibelVolumeScale, expectedOutput: -200 },
+ { tag: "0.33 from linear to decibel", input: 0.33, from: QtMultimedia.LinearVolumeScale, to: QtMultimedia.DecibelVolumeScale, expectedOutput: -9.63 },
+ { tag: "0.5 from linear to decibel", input: 0.5, from: QtMultimedia.LinearVolumeScale, to: QtMultimedia.DecibelVolumeScale, expectedOutput: -6.02 },
+ { tag: "0.72 from linear to decibel", input: 0.72, from: QtMultimedia.LinearVolumeScale, to: QtMultimedia.DecibelVolumeScale, expectedOutput: -2.85 },
+ { tag: "1.0 from linear to decibel", input: 1.0, from: QtMultimedia.LinearVolumeScale, to: QtMultimedia.DecibelVolumeScale, expectedOutput: 0 },
+
+ { tag: "-1.0 from cubic to linear", input: -1, from: QtMultimedia.CubicVolumeScale, to: QtMultimedia.LinearVolumeScale, expectedOutput: 0 },
+ { tag: "0.0 from cubic to linear", input: 0, from: QtMultimedia.CubicVolumeScale, to: QtMultimedia.LinearVolumeScale, expectedOutput: 0 },
+ { tag: "0.33 from cubic to linear", input: 0.33, from: QtMultimedia.CubicVolumeScale, to: QtMultimedia.LinearVolumeScale, expectedOutput: 0.04 },
+ { tag: "0.5 from cubic to linear", input: 0.5, from: QtMultimedia.CubicVolumeScale, to: QtMultimedia.LinearVolumeScale, expectedOutput: 0.13 },
+ { tag: "0.72 from cubic to linear", input: 0.72, from: QtMultimedia.CubicVolumeScale, to: QtMultimedia.LinearVolumeScale, expectedOutput: 0.37 },
+ { tag: "1.0 from cubic to linear", input: 1.0, from: QtMultimedia.CubicVolumeScale, to: QtMultimedia.LinearVolumeScale, expectedOutput: 1 },
+
+ { tag: "-1.0 from cubic to cubic", input: -1, from: QtMultimedia.CubicVolumeScale, to: QtMultimedia.CubicVolumeScale, expectedOutput: 0 },
+ { tag: "0.0 from cubic to cubic", input: 0, from: QtMultimedia.CubicVolumeScale, to: QtMultimedia.CubicVolumeScale, expectedOutput: 0 },
+ { tag: "0.5 from cubic to cubic", input: 0.5, from: QtMultimedia.CubicVolumeScale, to: QtMultimedia.CubicVolumeScale, expectedOutput: 0.5 },
+ { tag: "1.0 from cubic to cubic", input: 1.0, from: QtMultimedia.CubicVolumeScale, to: QtMultimedia.CubicVolumeScale, expectedOutput: 1.0 },
+
+ { tag: "-1.0 from cubic to decibel", input: -1, from: QtMultimedia.CubicVolumeScale, to: QtMultimedia.DecibelVolumeScale, expectedOutput: -200 },
+ { tag: "0.0 from cubic to decibel", input: 0, from: QtMultimedia.CubicVolumeScale, to: QtMultimedia.DecibelVolumeScale, expectedOutput: -200 },
+ { tag: "0.33 from cubic to decibel", input: 0.33, from: QtMultimedia.CubicVolumeScale, to: QtMultimedia.DecibelVolumeScale, expectedOutput: -28.89 },
+ { tag: "0.5 from cubic to decibel", input: 0.5, from: QtMultimedia.CubicVolumeScale, to: QtMultimedia.DecibelVolumeScale, expectedOutput: -18.06 },
+ { tag: "0.72 from cubic to decibel", input: 0.72, from: QtMultimedia.CubicVolumeScale, to: QtMultimedia.DecibelVolumeScale, expectedOutput: -8.56 },
+ { tag: "1.0 from cubic to decibel", input: 1.0, from: QtMultimedia.CubicVolumeScale, to: QtMultimedia.DecibelVolumeScale, expectedOutput: 0 },
+
+ { tag: "-1000 from decibel to linear", input: -1000, from: QtMultimedia.DecibelVolumeScale, to: QtMultimedia.LinearVolumeScale, expectedOutput: 0 },
+ { tag: "-200 from decibel to linear", input: -200, from: QtMultimedia.DecibelVolumeScale, to: QtMultimedia.LinearVolumeScale, expectedOutput: 0 },
+ { tag: "-40 from decibel to linear", input: -40, from: QtMultimedia.DecibelVolumeScale, to: QtMultimedia.LinearVolumeScale, expectedOutput: 0.01 },
+ { tag: "-10 from decibel to linear", input: -10, from: QtMultimedia.DecibelVolumeScale, to: QtMultimedia.LinearVolumeScale, expectedOutput: 0.32 },
+ { tag: "-5 from decibel to linear", input: -5, from: QtMultimedia.DecibelVolumeScale, to: QtMultimedia.LinearVolumeScale, expectedOutput: 0.56 },
+ { tag: "0 from decibel to linear", input: 0, from: QtMultimedia.DecibelVolumeScale, to: QtMultimedia.LinearVolumeScale, expectedOutput: 1 },
+
+ { tag: "-1000 from decibel to cubic", input: -1000, from: QtMultimedia.DecibelVolumeScale, to: QtMultimedia.CubicVolumeScale, expectedOutput: 0 },
+ { tag: "-200 from decibel to cubic", input: -200, from: QtMultimedia.DecibelVolumeScale, to: QtMultimedia.CubicVolumeScale, expectedOutput: 0 },
+ { tag: "-40 from decibel to cubic", input: -40, from: QtMultimedia.DecibelVolumeScale, to: QtMultimedia.CubicVolumeScale, expectedOutput: 0.22 },
+ { tag: "-10 from decibel to cubic", input: -10, from: QtMultimedia.DecibelVolumeScale, to: QtMultimedia.CubicVolumeScale, expectedOutput: 0.68 },
+ { tag: "-5 from decibel to cubic", input: -5, from: QtMultimedia.DecibelVolumeScale, to: QtMultimedia.CubicVolumeScale, expectedOutput: 0.83 },
+ { tag: "0 from decibel to cubic", input: 0, from: QtMultimedia.DecibelVolumeScale, to: QtMultimedia.CubicVolumeScale, expectedOutput: 1 },
+
+ { tag: "-1000 from decibel to decibel", input: -1000, from: QtMultimedia.DecibelVolumeScale, to: QtMultimedia.DecibelVolumeScale, expectedOutput: -1000 },
+ { tag: "-200 from decibel to decibel", input: -200, from: QtMultimedia.DecibelVolumeScale, to: QtMultimedia.DecibelVolumeScale, expectedOutput: -200 },
+ { tag: "-30 from decibel to decibel", input: -30, from: QtMultimedia.DecibelVolumeScale, to: QtMultimedia.DecibelVolumeScale, expectedOutput: -30 },
+ { tag: "0 from decibel to decibel", input: 0, from: QtMultimedia.DecibelVolumeScale, to: QtMultimedia.DecibelVolumeScale, expectedOutput: 0 },
+ ]
+ }
+
+ function test_convertVolume(data) {
+ fuzzyCompare(QtMultimedia.convertVolume(data.input, data.from, data.to),
+ data.expectedOutput,
+ 0.01);
+ }
+
}