| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:BSD$ | 
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows: | 
|---|
| 11 | ** | 
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without | 
|---|
| 13 | ** modification, are permitted provided that the following conditions are | 
|---|
| 14 | ** met: | 
|---|
| 15 | **   * Redistributions of source code must retain the above copyright | 
|---|
| 16 | **     notice, this list of conditions and the following disclaimer. | 
|---|
| 17 | **   * Redistributions in binary form must reproduce the above copyright | 
|---|
| 18 | **     notice, this list of conditions and the following disclaimer in | 
|---|
| 19 | **     the documentation and/or other materials provided with the | 
|---|
| 20 | **     distribution. | 
|---|
| 21 | **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor | 
|---|
| 22 | **     the names of its contributors may be used to endorse or promote | 
|---|
| 23 | **     products derived from this software without specific prior written | 
|---|
| 24 | **     permission. | 
|---|
| 25 | ** | 
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | 
|---|
| 37 | ** $QT_END_LICENSE$ | 
|---|
| 38 | ** | 
|---|
| 39 | ****************************************************************************/ | 
|---|
| 40 |  | 
|---|
| 41 |  | 
|---|
| 42 | #include <QAudioDeviceInfo> | 
|---|
| 43 |  | 
|---|
| 44 | #include "audiodevices.h" | 
|---|
| 45 |  | 
|---|
| 46 | // Utility functions for converting QAudioFormat fields into text | 
|---|
| 47 |  | 
|---|
| 48 | QString toString(QAudioFormat::SampleType sampleType) | 
|---|
| 49 | { | 
|---|
| 50 | QString result("Unknown"); | 
|---|
| 51 | switch (sampleType) { | 
|---|
| 52 | case QAudioFormat::SignedInt: | 
|---|
| 53 | result = "SignedInt"; | 
|---|
| 54 | break; | 
|---|
| 55 | case QAudioFormat::UnSignedInt: | 
|---|
| 56 | result = "UnSignedInt"; | 
|---|
| 57 | break; | 
|---|
| 58 | case QAudioFormat::Float: | 
|---|
| 59 | result = "Float"; | 
|---|
| 60 | break; | 
|---|
| 61 | case QAudioFormat::Unknown: | 
|---|
| 62 | break; | 
|---|
| 63 | } | 
|---|
| 64 | return result; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | QString toString(QAudioFormat::Endian endian) | 
|---|
| 68 | { | 
|---|
| 69 | QString result("Unknown"); | 
|---|
| 70 | switch (endian) { | 
|---|
| 71 | case QAudioFormat::LittleEndian: | 
|---|
| 72 | result = "LittleEndian"; | 
|---|
| 73 | break; | 
|---|
| 74 | case QAudioFormat::BigEndian: | 
|---|
| 75 | result = "BigEndian"; | 
|---|
| 76 | break; | 
|---|
| 77 | } | 
|---|
| 78 | return result; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 | AudioDevicesBase::AudioDevicesBase(QWidget *parent, Qt::WFlags f) | 
|---|
| 83 | : QMainWindow(parent, f) | 
|---|
| 84 | { | 
|---|
| 85 | setupUi(this); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | AudioDevicesBase::~AudioDevicesBase() {} | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 | AudioTest::AudioTest(QWidget *parent, Qt::WFlags f) | 
|---|
| 92 | : AudioDevicesBase(parent, f) | 
|---|
| 93 | { | 
|---|
| 94 | mode = QAudio::AudioOutput; | 
|---|
| 95 |  | 
|---|
| 96 | connect(testButton, SIGNAL(clicked()), SLOT(test())); | 
|---|
| 97 | connect(modeBox, SIGNAL(activated(int)), SLOT(modeChanged(int))); | 
|---|
| 98 | connect(deviceBox, SIGNAL(activated(int)), SLOT(deviceChanged(int))); | 
|---|
| 99 | connect(frequencyBox, SIGNAL(activated(int)), SLOT(freqChanged(int))); | 
|---|
| 100 | connect(channelsBox, SIGNAL(activated(int)), SLOT(channelChanged(int))); | 
|---|
| 101 | connect(codecsBox, SIGNAL(activated(int)), SLOT(codecChanged(int))); | 
|---|
| 102 | connect(sampleSizesBox, SIGNAL(activated(int)), SLOT(sampleSizeChanged(int))); | 
|---|
| 103 | connect(sampleTypesBox, SIGNAL(activated(int)), SLOT(sampleTypeChanged(int))); | 
|---|
| 104 | connect(endianBox, SIGNAL(activated(int)), SLOT(endianChanged(int))); | 
|---|
| 105 | connect(populateTableButton, SIGNAL(clicked()), SLOT(populateTable())); | 
|---|
| 106 |  | 
|---|
| 107 | modeBox->setCurrentIndex(0); | 
|---|
| 108 | modeChanged(0); | 
|---|
| 109 | deviceBox->setCurrentIndex(0); | 
|---|
| 110 | deviceChanged(0); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | AudioTest::~AudioTest() | 
|---|
| 114 | { | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | void AudioTest::test() | 
|---|
| 118 | { | 
|---|
| 119 | // tries to set all the settings picked. | 
|---|
| 120 | testResult->clear(); | 
|---|
| 121 |  | 
|---|
| 122 | if (!deviceInfo.isNull()) { | 
|---|
| 123 | if (deviceInfo.isFormatSupported(settings)) { | 
|---|
| 124 | testResult->setText(tr("Success")); | 
|---|
| 125 | nearestFreq->setText(""); | 
|---|
| 126 | nearestChannel->setText(""); | 
|---|
| 127 | nearestCodec->setText(""); | 
|---|
| 128 | nearestSampleSize->setText(""); | 
|---|
| 129 | nearestSampleType->setText(""); | 
|---|
| 130 | nearestEndian->setText(""); | 
|---|
| 131 | } else { | 
|---|
| 132 | QAudioFormat nearest = deviceInfo.nearestFormat(settings); | 
|---|
| 133 | testResult->setText(tr("Failed")); | 
|---|
| 134 | nearestFreq->setText(QString("%1").arg(nearest.frequency())); | 
|---|
| 135 | nearestChannel->setText(QString("%1").arg(nearest.channels())); | 
|---|
| 136 | nearestCodec->setText(nearest.codec()); | 
|---|
| 137 | nearestSampleSize->setText(QString("%1").arg(nearest.sampleSize())); | 
|---|
| 138 | nearestSampleType->setText(toString(nearest.sampleType())); | 
|---|
| 139 | nearestEndian->setText(toString(nearest.byteOrder())); | 
|---|
| 140 | } | 
|---|
| 141 | } | 
|---|
| 142 | else | 
|---|
| 143 | testResult->setText(tr("No Device")); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | void AudioTest::modeChanged(int idx) | 
|---|
| 147 | { | 
|---|
| 148 | testResult->clear(); | 
|---|
| 149 |  | 
|---|
| 150 | // mode has changed | 
|---|
| 151 | if (idx == 0) | 
|---|
| 152 | mode = QAudio::AudioInput; | 
|---|
| 153 | else | 
|---|
| 154 | mode = QAudio::AudioOutput; | 
|---|
| 155 |  | 
|---|
| 156 | deviceBox->clear(); | 
|---|
| 157 | foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(mode)) | 
|---|
| 158 | deviceBox->addItem(deviceInfo.deviceName(), qVariantFromValue(deviceInfo)); | 
|---|
| 159 |  | 
|---|
| 160 | deviceBox->setCurrentIndex(0); | 
|---|
| 161 | deviceChanged(0); | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | void AudioTest::deviceChanged(int idx) | 
|---|
| 165 | { | 
|---|
| 166 | testResult->clear(); | 
|---|
| 167 |  | 
|---|
| 168 | if (deviceBox->count() == 0) | 
|---|
| 169 | return; | 
|---|
| 170 |  | 
|---|
| 171 | // device has changed | 
|---|
| 172 | deviceInfo = deviceBox->itemData(idx).value<QAudioDeviceInfo>(); | 
|---|
| 173 |  | 
|---|
| 174 | frequencyBox->clear(); | 
|---|
| 175 | QList<int> freqz = deviceInfo.supportedFrequencies(); | 
|---|
| 176 | for(int i = 0; i < freqz.size(); ++i) | 
|---|
| 177 | frequencyBox->addItem(QString("%1").arg(freqz.at(i))); | 
|---|
| 178 | if(freqz.size()) | 
|---|
| 179 | settings.setFrequency(freqz.at(0)); | 
|---|
| 180 |  | 
|---|
| 181 | channelsBox->clear(); | 
|---|
| 182 | QList<int> chz = deviceInfo.supportedChannels(); | 
|---|
| 183 | for(int i = 0; i < chz.size(); ++i) | 
|---|
| 184 | channelsBox->addItem(QString("%1").arg(chz.at(i))); | 
|---|
| 185 | if(chz.size()) | 
|---|
| 186 | settings.setChannels(chz.at(0)); | 
|---|
| 187 |  | 
|---|
| 188 | codecsBox->clear(); | 
|---|
| 189 | QStringList codecz = deviceInfo.supportedCodecs(); | 
|---|
| 190 | for (int i = 0; i < codecz.size(); ++i) | 
|---|
| 191 | codecsBox->addItem(QString("%1").arg(codecz.at(i))); | 
|---|
| 192 | if (codecz.size()) | 
|---|
| 193 | settings.setCodec(codecz.at(0)); | 
|---|
| 194 | // Add false to create failed condition! | 
|---|
| 195 | codecsBox->addItem("audio/test"); | 
|---|
| 196 |  | 
|---|
| 197 | sampleSizesBox->clear(); | 
|---|
| 198 | QList<int> sampleSizez = deviceInfo.supportedSampleSizes(); | 
|---|
| 199 | for (int i = 0; i < sampleSizez.size(); ++i) | 
|---|
| 200 | sampleSizesBox->addItem(QString("%1").arg(sampleSizez.at(i))); | 
|---|
| 201 | if (sampleSizez.size()) | 
|---|
| 202 | settings.setSampleSize(sampleSizez.at(0)); | 
|---|
| 203 |  | 
|---|
| 204 | sampleTypesBox->clear(); | 
|---|
| 205 | QList<QAudioFormat::SampleType> sampleTypez = deviceInfo.supportedSampleTypes(); | 
|---|
| 206 |  | 
|---|
| 207 | for (int i = 0; i < sampleTypez.size(); ++i) | 
|---|
| 208 | sampleTypesBox->addItem(toString(sampleTypez.at(i))); | 
|---|
| 209 | if (sampleTypez.size()) | 
|---|
| 210 | settings.setSampleType(sampleTypez.at(0)); | 
|---|
| 211 |  | 
|---|
| 212 | endianBox->clear(); | 
|---|
| 213 | QList<QAudioFormat::Endian> endianz = deviceInfo.supportedByteOrders(); | 
|---|
| 214 | for (int i = 0; i < endianz.size(); ++i) | 
|---|
| 215 | endianBox->addItem(toString(endianz.at(i))); | 
|---|
| 216 | if (endianz.size()) | 
|---|
| 217 | settings.setByteOrder(endianz.at(0)); | 
|---|
| 218 |  | 
|---|
| 219 | allFormatsTable->clearContents(); | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | void AudioTest::populateTable() | 
|---|
| 223 | { | 
|---|
| 224 | int row = 0; | 
|---|
| 225 |  | 
|---|
| 226 | QAudioFormat format; | 
|---|
| 227 | foreach (QString codec, deviceInfo.supportedCodecs()) { | 
|---|
| 228 | format.setCodec(codec); | 
|---|
| 229 | foreach (int frequency, deviceInfo.supportedFrequencies()) { | 
|---|
| 230 | format.setFrequency(frequency); | 
|---|
| 231 | foreach (int channels, deviceInfo.supportedChannels()) { | 
|---|
| 232 | format.setChannels(channels); | 
|---|
| 233 | foreach (QAudioFormat::SampleType sampleType, deviceInfo.supportedSampleTypes()) { | 
|---|
| 234 | format.setSampleType(sampleType); | 
|---|
| 235 | foreach (int sampleSize, deviceInfo.supportedSampleSizes()) { | 
|---|
| 236 | format.setSampleSize(sampleSize); | 
|---|
| 237 | foreach (QAudioFormat::Endian endian, deviceInfo.supportedByteOrders()) { | 
|---|
| 238 | format.setByteOrder(endian); | 
|---|
| 239 | if (deviceInfo.isFormatSupported(format)) { | 
|---|
| 240 | allFormatsTable->setRowCount(row + 1); | 
|---|
| 241 |  | 
|---|
| 242 | QTableWidgetItem *codecItem = new QTableWidgetItem(format.codec()); | 
|---|
| 243 | allFormatsTable->setItem(row, 0, codecItem); | 
|---|
| 244 |  | 
|---|
| 245 | QTableWidgetItem *frequencyItem = new QTableWidgetItem(QString("%1").arg(format.frequency())); | 
|---|
| 246 | allFormatsTable->setItem(row, 1, frequencyItem); | 
|---|
| 247 |  | 
|---|
| 248 | QTableWidgetItem *channelsItem = new QTableWidgetItem(QString("%1").arg(format.channels())); | 
|---|
| 249 | allFormatsTable->setItem(row, 2, channelsItem); | 
|---|
| 250 |  | 
|---|
| 251 | QTableWidgetItem *sampleTypeItem = new QTableWidgetItem(toString(format.sampleType())); | 
|---|
| 252 | allFormatsTable->setItem(row, 3, sampleTypeItem); | 
|---|
| 253 |  | 
|---|
| 254 | QTableWidgetItem *sampleSizeItem = new QTableWidgetItem(QString("%1").arg(format.sampleSize())); | 
|---|
| 255 | allFormatsTable->setItem(row, 4, sampleSizeItem); | 
|---|
| 256 |  | 
|---|
| 257 | QTableWidgetItem *byteOrderItem = new QTableWidgetItem(toString(format.byteOrder())); | 
|---|
| 258 | allFormatsTable->setItem(row, 5, byteOrderItem); | 
|---|
| 259 |  | 
|---|
| 260 | ++row; | 
|---|
| 261 | } | 
|---|
| 262 | } | 
|---|
| 263 | } | 
|---|
| 264 | } | 
|---|
| 265 | } | 
|---|
| 266 | } | 
|---|
| 267 | } | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | void AudioTest::freqChanged(int idx) | 
|---|
| 271 | { | 
|---|
| 272 | // freq has changed | 
|---|
| 273 | settings.setFrequency(frequencyBox->itemText(idx).toInt()); | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 | void AudioTest::channelChanged(int idx) | 
|---|
| 277 | { | 
|---|
| 278 | settings.setChannels(channelsBox->itemText(idx).toInt()); | 
|---|
| 279 | } | 
|---|
| 280 |  | 
|---|
| 281 | void AudioTest::codecChanged(int idx) | 
|---|
| 282 | { | 
|---|
| 283 | settings.setCodec(codecsBox->itemText(idx)); | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | void AudioTest::sampleSizeChanged(int idx) | 
|---|
| 287 | { | 
|---|
| 288 | settings.setSampleSize(sampleSizesBox->itemText(idx).toInt()); | 
|---|
| 289 | } | 
|---|
| 290 |  | 
|---|
| 291 | void AudioTest::sampleTypeChanged(int idx) | 
|---|
| 292 | { | 
|---|
| 293 | switch (sampleTypesBox->itemText(idx).toInt()) { | 
|---|
| 294 | case QAudioFormat::SignedInt: | 
|---|
| 295 | settings.setSampleType(QAudioFormat::SignedInt); | 
|---|
| 296 | break; | 
|---|
| 297 | case QAudioFormat::UnSignedInt: | 
|---|
| 298 | settings.setSampleType(QAudioFormat::UnSignedInt); | 
|---|
| 299 | break; | 
|---|
| 300 | case QAudioFormat::Float: | 
|---|
| 301 | settings.setSampleType(QAudioFormat::Float); | 
|---|
| 302 | } | 
|---|
| 303 | } | 
|---|
| 304 |  | 
|---|
| 305 | void AudioTest::endianChanged(int idx) | 
|---|
| 306 | { | 
|---|
| 307 | switch (endianBox->itemText(idx).toInt()) { | 
|---|
| 308 | case QAudioFormat::LittleEndian: | 
|---|
| 309 | settings.setByteOrder(QAudioFormat::LittleEndian); | 
|---|
| 310 | break; | 
|---|
| 311 | case QAudioFormat::BigEndian: | 
|---|
| 312 | settings.setByteOrder(QAudioFormat::BigEndian); | 
|---|
| 313 | } | 
|---|
| 314 | } | 
|---|