From 3f9a617a72dc8167a762a1d75e75c98d27a7a214 Mon Sep 17 00:00:00 2001 From: Gareth Stockwell Date: Wed, 5 May 2010 10:43:20 +0100 Subject: Modified audiodevices example to list all supported formats Divided the UI into two tabs: - One which lists all formats supported by the device, in a table. This table is populated dynamically when the user presses a button, rather than at application startup - for some backends such as ALSA, isFormatSupported() is a time-consuming operation. - One which allows the user to specify a particular format, and then test whether it is supported. If the format is not supported, the nearest supported format is displayed. Changing mode causes test result to be cleared. Call showFullScreen() on small-screen devices. Reviewed-by: Kurt Korbatits --- examples/multimedia/audiodevices/audiodevices.cpp | 158 ++++-- examples/multimedia/audiodevices/audiodevices.h | 2 + .../multimedia/audiodevices/audiodevicesbase.ui | 565 ++++++++++++--------- examples/multimedia/audiodevices/main.cpp | 4 + 4 files changed, 439 insertions(+), 290 deletions(-) diff --git a/examples/multimedia/audiodevices/audiodevices.cpp b/examples/multimedia/audiodevices/audiodevices.cpp index 7d09c38..1a7a7ee 100644 --- a/examples/multimedia/audiodevices/audiodevices.cpp +++ b/examples/multimedia/audiodevices/audiodevices.cpp @@ -44,6 +44,40 @@ #include "audiodevices.h" +// Utility functions for converting QAudioFormat fields into text + +QString toString(QAudioFormat::SampleType sampleType) +{ + QString result("Unknown"); + switch (sampleType) { + case QAudioFormat::SignedInt: + result = "SignedInt"; + break; + case QAudioFormat::UnSignedInt: + result = "UnSignedInt"; + break; + case QAudioFormat::Float: + result = "Float"; + break; + } + return result; +} + +QString toString(QAudioFormat::Endian endian) +{ + QString result("Unknown"); + switch (endian) { + case QAudioFormat::LittleEndian: + result = "LittleEndian"; + break; + case QAudioFormat::BigEndian: + result = "BigEndian"; + break; + } + return result; +} + + AudioDevicesBase::AudioDevicesBase(QWidget *parent, Qt::WFlags f) : QMainWindow(parent, f) { @@ -67,6 +101,7 @@ AudioTest::AudioTest(QWidget *parent, Qt::WFlags f) connect(sampleSizesBox, SIGNAL(activated(int)), SLOT(sampleSizeChanged(int))); connect(sampleTypesBox, SIGNAL(activated(int)), SLOT(sampleTypeChanged(int))); connect(endianBox, SIGNAL(activated(int)), SLOT(endianChanged(int))); + connect(populateTableButton, SIGNAL(clicked()), SLOT(populateTable())); modeBox->setCurrentIndex(0); modeChanged(0); @@ -81,12 +116,11 @@ AudioTest::~AudioTest() void AudioTest::test() { // tries to set all the settings picked. - logOutput->clear(); - logOutput->append("NOTE: an invalid codec audio/test exists for testing, to get a fail condition."); + testResult->clear(); if (!deviceInfo.isNull()) { if (deviceInfo.isFormatSupported(settings)) { - logOutput->append(tr("Success")); + testResult->setText(tr("Success")); nearestFreq->setText(""); nearestChannel->setText(""); nearestCodec->setText(""); @@ -95,40 +129,23 @@ void AudioTest::test() nearestEndian->setText(""); } else { QAudioFormat nearest = deviceInfo.nearestFormat(settings); - logOutput->append(tr("Failed")); + testResult->setText(tr("Failed")); nearestFreq->setText(QString("%1").arg(nearest.frequency())); nearestChannel->setText(QString("%1").arg(nearest.channels())); nearestCodec->setText(nearest.codec()); nearestSampleSize->setText(QString("%1").arg(nearest.sampleSize())); - - switch(nearest.sampleType()) { - case QAudioFormat::SignedInt: - nearestSampleType->setText("SignedInt"); - break; - case QAudioFormat::UnSignedInt: - nearestSampleType->setText("UnSignedInt"); - break; - case QAudioFormat::Float: - nearestSampleType->setText("Float"); - break; - case QAudioFormat::Unknown: - nearestSampleType->setText("Unknown"); - } - switch(nearest.byteOrder()) { - case QAudioFormat::LittleEndian: - nearestEndian->setText("LittleEndian"); - break; - case QAudioFormat::BigEndian: - nearestEndian->setText("BigEndian"); - } + nearestSampleType->setText(toString(nearest.sampleType())); + nearestEndian->setText(toString(nearest.byteOrder())); } } else - logOutput->append(tr("No Device")); + testResult->setText(tr("No Device")); } void AudioTest::modeChanged(int idx) { + testResult->clear(); + // mode has changed if (idx == 0) mode = QAudio::AudioInput; @@ -138,10 +155,15 @@ void AudioTest::modeChanged(int idx) deviceBox->clear(); foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(mode)) deviceBox->addItem(deviceInfo.deviceName(), qVariantFromValue(deviceInfo)); + + deviceBox->setCurrentIndex(0); + deviceChanged(0); } void AudioTest::deviceChanged(int idx) { + testResult->clear(); + if (deviceBox->count() == 0) return; @@ -180,38 +202,68 @@ void AudioTest::deviceChanged(int idx) sampleTypesBox->clear(); QList sampleTypez = deviceInfo.supportedSampleTypes(); - for (int i = 0; i < sampleTypez.size(); ++i) { - switch(sampleTypez.at(i)) { - case QAudioFormat::SignedInt: - sampleTypesBox->addItem("SignedInt"); - break; - case QAudioFormat::UnSignedInt: - sampleTypesBox->addItem("UnSignedInt"); - break; - case QAudioFormat::Float: - sampleTypesBox->addItem("Float"); - break; - case QAudioFormat::Unknown: - sampleTypesBox->addItem("Unknown"); - } - if (sampleTypez.size()) - settings.setSampleType(sampleTypez.at(0)); - } + + for (int i = 0; i < sampleTypez.size(); ++i) + sampleTypesBox->addItem(toString(sampleTypez.at(i))); + if (sampleTypez.size()) + settings.setSampleType(sampleTypez.at(0)); endianBox->clear(); QList endianz = deviceInfo.supportedByteOrders(); - for (int i = 0; i < endianz.size(); ++i) { - switch (endianz.at(i)) { - case QAudioFormat::LittleEndian: - endianBox->addItem("Little Endian"); - break; - case QAudioFormat::BigEndian: - endianBox->addItem("Big Endian"); - break; - } - } + for (int i = 0; i < endianz.size(); ++i) + endianBox->addItem(toString(endianz.at(i))); if (endianz.size()) settings.setByteOrder(endianz.at(0)); + + allFormatsTable->clearContents(); +} + +void AudioTest::populateTable() +{ + int row = 0; + + QAudioFormat format; + foreach (QString codec, deviceInfo.supportedCodecs()) { + format.setCodec(codec); + foreach (int frequency, deviceInfo.supportedFrequencies()) { + format.setFrequency(frequency); + foreach (int channels, deviceInfo.supportedChannels()) { + format.setChannels(channels); + foreach (QAudioFormat::SampleType sampleType, deviceInfo.supportedSampleTypes()) { + format.setSampleType(sampleType); + foreach (int sampleSize, deviceInfo.supportedSampleSizes()) { + format.setSampleSize(sampleSize); + foreach (QAudioFormat::Endian endian, deviceInfo.supportedByteOrders()) { + format.setByteOrder(endian); + if (deviceInfo.isFormatSupported(format)) { + allFormatsTable->setRowCount(row + 1); + + QTableWidgetItem *codecItem = new QTableWidgetItem(format.codec()); + allFormatsTable->setItem(row, 0, codecItem); + + QTableWidgetItem *frequencyItem = new QTableWidgetItem(QString("%1").arg(format.frequency())); + allFormatsTable->setItem(row, 1, frequencyItem); + + QTableWidgetItem *channelsItem = new QTableWidgetItem(QString("%1").arg(format.channels())); + allFormatsTable->setItem(row, 2, channelsItem); + + QTableWidgetItem *sampleTypeItem = new QTableWidgetItem(toString(format.sampleType())); + allFormatsTable->setItem(row, 3, sampleTypeItem); + + QTableWidgetItem *sampleSizeItem = new QTableWidgetItem(QString("%1").arg(format.sampleSize())); + allFormatsTable->setItem(row, 4, sampleSizeItem); + + QTableWidgetItem *byteOrderItem = new QTableWidgetItem(toString(format.byteOrder())); + allFormatsTable->setItem(row, 5, byteOrderItem); + + ++row; + } + } + } + } + } + } + } } void AudioTest::freqChanged(int idx) diff --git a/examples/multimedia/audiodevices/audiodevices.h b/examples/multimedia/audiodevices/audiodevices.h index 15ace92..b5b5204 100644 --- a/examples/multimedia/audiodevices/audiodevices.h +++ b/examples/multimedia/audiodevices/audiodevices.h @@ -74,5 +74,7 @@ private slots: void sampleTypeChanged(int idx); void endianChanged(int idx); void test(); + void populateTable(); + }; diff --git a/examples/multimedia/audiodevices/audiodevicesbase.ui b/examples/multimedia/audiodevices/audiodevicesbase.ui index 667a6e5..23b45d7 100644 --- a/examples/multimedia/audiodevices/audiodevicesbase.ui +++ b/examples/multimedia/audiodevices/audiodevicesbase.ui @@ -6,8 +6,8 @@ 0 0 - 320 - 300 + 679 + 598 @@ -30,58 +30,30 @@ 0 - -192 - 282 - 471 + 0 + 659 + 558 - - - + + + - - - - 1 - 0 - - + - Device + Mode - + - Mode + Device - - - - 0 - 0 - - - - - 150 - 0 - - - - - - - - 0 - 0 - - Input @@ -94,203 +66,322 @@ - - - - QFrame::Panel - - - QFrame::Raised - - - Actual Settings - - - Qt::AlignCenter - - - - - - - QFrame::Panel - - - QFrame::Raised - - - Nearest Settings - - - Qt::AlignCenter - - - - - - - Frequency - - - - - - - Frequency - - - - - - - - - - false - - - - - - - Channels - - - - - - - Channel - - - - - - - - - - false - - - - - - - Codecs - - - - - - - Codec - - - - - - - - - - false - - - - - - - SampleSize - - - - - - - SampleSize - - - - - - - - - - false - - - - - - - SampleType - - - - - - - SampleType - - - - - - - - - - false - - - - - - - Endianess - - - - - - - Endianess - - - - - - - - - - false - - - - - - - false - - - - 0 - 0 - - - - Qt::ScrollBarAlwaysOff - - - - - - - Test - + + + + + + + 0 + + + + Test format + + + + + + + 0 + 0 + + + + QFrame::NoFrame + + + QFrame::Plain + + + <i>Actual Settings</i> + + + Qt::RichText + + + Qt::AlignCenter + + + + + + + + 0 + 0 + + + + QFrame::NoFrame + + + QFrame::Plain + + + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-style:italic;">Nearest Settings</span></p></body></html> + + + Qt::RichText + + + Qt::AlignCenter + + + + + + + + 0 + 0 + + + + + + + + false + + + + + + + + + + false + + + + + + + + + + false + + + + + + + + + + false + + + + + + + Test + + + + + + + + + + + + + + Frequency (Hz) + + + + + + + Channels + + + + + + + Sample size (bits) + + + + + + + Endianess + + + + + + + + 0 + 0 + + + + Note: an invalid codec 'audio/test' exists in order to allow an invalid format to be constructed, and therefore to trigger a 'nearest format' calculation. + + + true + + + + + + + Codec + + + + + + + false + + + + + + + + + + SampleType + + + + + + + + + + false + + + + + + + + All formats + + + + + + Populate table + + + + + + + QAbstractItemView::NoEditTriggers + + + false + + + QAbstractItemView::NoSelection + + + QAbstractItemView::SelectItems + + + Qt::ElideNone + + + false + + + false + + + false + + + false + + + false + + + false + + + false + + + false + + + false + + + + Codec + + + AlignHCenter|AlignVCenter|AlignCenter + + + + + Frequency (Hz) + + + AlignHCenter|AlignVCenter|AlignCenter + + + + + Channels + + + AlignHCenter|AlignVCenter|AlignCenter + + + + + Sample type + + + AlignHCenter|AlignVCenter|AlignCenter + + + + + Sample size (bits) + + + AlignHCenter|AlignVCenter|AlignCenter + + + + + Endianness + + + AlignHCenter|AlignVCenter|AlignCenter + + + + + + diff --git a/examples/multimedia/audiodevices/main.cpp b/examples/multimedia/audiodevices/main.cpp index 0ea18a1..8ca4932 100644 --- a/examples/multimedia/audiodevices/main.cpp +++ b/examples/multimedia/audiodevices/main.cpp @@ -49,7 +49,11 @@ int main(int argv, char **args) app.setApplicationName("Audio Device Test"); AudioTest audio; +#ifdef Q_OS_SYMBIAN + audio.showMaximized(); +#else audio.show(); +#endif return app.exec(); } -- cgit v0.12