qtmobility/examples/audiodevices/audiodevices.cpp
changeset 14 6fbed849b4f4
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <qaudiodeviceinfo.h>
       
    44 
       
    45 #include "audiodevices.h"
       
    46 
       
    47 // Utility functions for converting QAudioFormat fields into text
       
    48 
       
    49 QString toString(QAudioFormat::SampleType sampleType)
       
    50 {
       
    51     QString result("Unknown");
       
    52     switch (sampleType) {
       
    53     case QAudioFormat::SignedInt:
       
    54         result = "SignedInt";
       
    55         break;
       
    56     case QAudioFormat::UnSignedInt:
       
    57         result = "UnSignedInt";
       
    58         break;
       
    59     case QAudioFormat::Float:
       
    60         result = "Float";
       
    61         break;
       
    62     }
       
    63     return result;
       
    64 }
       
    65 
       
    66 QString toString(QAudioFormat::Endian endian)
       
    67 {
       
    68     QString result("Unknown");
       
    69     switch (endian) {
       
    70     case QAudioFormat::LittleEndian:
       
    71         result = "LittleEndian";
       
    72         break;
       
    73     case QAudioFormat::BigEndian:
       
    74         result = "BigEndian";
       
    75         break;
       
    76     }
       
    77     return result;
       
    78 }
       
    79 
       
    80 
       
    81 AudioDevicesBase::AudioDevicesBase(QWidget *parent, Qt::WFlags f)
       
    82     : QMainWindow(parent, f)
       
    83 {
       
    84     setupUi(this);
       
    85 }
       
    86 
       
    87 AudioDevicesBase::~AudioDevicesBase() {}
       
    88 
       
    89 
       
    90 AudioTest::AudioTest(QWidget *parent, Qt::WFlags f)
       
    91     : AudioDevicesBase(parent, f)
       
    92 {
       
    93     mode = QAudio::AudioOutput;
       
    94 
       
    95     connect(testButton, SIGNAL(clicked()), SLOT(test()));
       
    96     connect(modeBox, SIGNAL(activated(int)), SLOT(modeChanged(int)));
       
    97     connect(deviceBox, SIGNAL(activated(int)), SLOT(deviceChanged(int)));
       
    98     connect(frequencyBox, SIGNAL(activated(int)), SLOT(freqChanged(int)));
       
    99     connect(channelsBox, SIGNAL(activated(int)), SLOT(channelChanged(int)));
       
   100     connect(codecsBox, SIGNAL(activated(int)), SLOT(codecChanged(int)));
       
   101     connect(sampleSizesBox, SIGNAL(activated(int)), SLOT(sampleSizeChanged(int)));
       
   102     connect(sampleTypesBox, SIGNAL(activated(int)), SLOT(sampleTypeChanged(int)));
       
   103     connect(endianBox, SIGNAL(activated(int)), SLOT(endianChanged(int)));
       
   104     connect(populateTableButton, SIGNAL(clicked()), SLOT(populateTable()));
       
   105 
       
   106     modeBox->setCurrentIndex(0);
       
   107     modeChanged(0);
       
   108     deviceBox->setCurrentIndex(0);
       
   109     deviceChanged(0);
       
   110 }
       
   111 
       
   112 AudioTest::~AudioTest()
       
   113 {
       
   114 }
       
   115 
       
   116 void AudioTest::test()
       
   117 {
       
   118     // tries to set all the settings picked.
       
   119     testResult->clear();
       
   120 
       
   121     if (!deviceInfo.isNull()) {
       
   122         if (deviceInfo.isFormatSupported(settings)) {
       
   123             testResult->setText(tr("Success"));
       
   124             nearestFreq->setText("");
       
   125             nearestChannel->setText("");
       
   126             nearestCodec->setText("");
       
   127             nearestSampleSize->setText("");
       
   128             nearestSampleType->setText("");
       
   129             nearestEndian->setText("");
       
   130         } else {
       
   131             QAudioFormat nearest = deviceInfo.nearestFormat(settings);
       
   132             testResult->setText(tr("Failed"));
       
   133             nearestFreq->setText(QString("%1").arg(nearest.frequency()));
       
   134             nearestChannel->setText(QString("%1").arg(nearest.channels()));
       
   135             nearestCodec->setText(nearest.codec());
       
   136             nearestSampleSize->setText(QString("%1").arg(nearest.sampleSize()));
       
   137             nearestSampleType->setText(toString(nearest.sampleType()));
       
   138             nearestEndian->setText(toString(nearest.byteOrder()));
       
   139         }
       
   140     }
       
   141     else
       
   142         testResult->setText(tr("No Device"));
       
   143 }
       
   144 
       
   145 void AudioTest::modeChanged(int idx)
       
   146 {
       
   147     testResult->clear();
       
   148 
       
   149     // mode has changed
       
   150     if (idx == 0)
       
   151         mode = QAudio::AudioInput;
       
   152     else
       
   153         mode = QAudio::AudioOutput;
       
   154 
       
   155     deviceBox->clear();
       
   156     foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(mode))
       
   157         deviceBox->addItem(deviceInfo.deviceName(), qVariantFromValue(deviceInfo));
       
   158 
       
   159     deviceBox->setCurrentIndex(0);
       
   160     deviceChanged(0);
       
   161 }
       
   162 
       
   163 void AudioTest::deviceChanged(int idx)
       
   164 {
       
   165     testResult->clear();
       
   166 
       
   167     if (deviceBox->count() == 0)
       
   168         return;
       
   169 
       
   170     // device has changed
       
   171     deviceInfo = deviceBox->itemData(idx).value<QAudioDeviceInfo>();
       
   172 
       
   173     frequencyBox->clear();
       
   174     QList<int> freqz = deviceInfo.supportedFrequencies();
       
   175     for(int i = 0; i < freqz.size(); ++i)
       
   176         frequencyBox->addItem(QString("%1").arg(freqz.at(i)));
       
   177     if(freqz.size())
       
   178         settings.setFrequency(freqz.at(0));
       
   179 
       
   180     channelsBox->clear();
       
   181     QList<int> chz = deviceInfo.supportedChannels();
       
   182     for(int i = 0; i < chz.size(); ++i)
       
   183         channelsBox->addItem(QString("%1").arg(chz.at(i)));
       
   184     if(chz.size())
       
   185         settings.setChannels(chz.at(0));
       
   186 
       
   187     codecsBox->clear();
       
   188     QStringList codecz = deviceInfo.supportedCodecs();
       
   189     for (int i = 0; i < codecz.size(); ++i)
       
   190         codecsBox->addItem(QString("%1").arg(codecz.at(i)));
       
   191     if (codecz.size())
       
   192         settings.setCodec(codecz.at(0));
       
   193     // Add false to create failed condition!
       
   194     codecsBox->addItem("audio/test");
       
   195 
       
   196     sampleSizesBox->clear();
       
   197     QList<int> sampleSizez = deviceInfo.supportedSampleSizes();
       
   198     for (int i = 0; i < sampleSizez.size(); ++i)
       
   199         sampleSizesBox->addItem(QString("%1").arg(sampleSizez.at(i)));
       
   200     if (sampleSizez.size())
       
   201         settings.setSampleSize(sampleSizez.at(0));
       
   202 
       
   203     sampleTypesBox->clear();
       
   204     QList<QAudioFormat::SampleType> sampleTypez = deviceInfo.supportedSampleTypes();
       
   205 
       
   206     for (int i = 0; i < sampleTypez.size(); ++i)
       
   207         sampleTypesBox->addItem(toString(sampleTypez.at(i)));
       
   208     if (sampleTypez.size())
       
   209         settings.setSampleType(sampleTypez.at(0));
       
   210 
       
   211     endianBox->clear();
       
   212     QList<QAudioFormat::Endian> endianz = deviceInfo.supportedByteOrders();
       
   213     for (int i = 0; i < endianz.size(); ++i)
       
   214         endianBox->addItem(toString(endianz.at(i)));
       
   215     if (endianz.size())
       
   216         settings.setByteOrder(endianz.at(0));
       
   217 
       
   218     allFormatsTable->clearContents();
       
   219 }
       
   220 
       
   221 void AudioTest::populateTable()
       
   222 {
       
   223     int row = 0;
       
   224 
       
   225     QAudioFormat format;
       
   226     foreach (QString codec, deviceInfo.supportedCodecs()) {
       
   227         format.setCodec(codec);
       
   228         foreach (int frequency, deviceInfo.supportedFrequencies()) {
       
   229             format.setFrequency(frequency);
       
   230             foreach (int channels, deviceInfo.supportedChannels()) {
       
   231                 format.setChannels(channels);
       
   232                 foreach (QAudioFormat::SampleType sampleType, deviceInfo.supportedSampleTypes()) {
       
   233                     format.setSampleType(sampleType);
       
   234                     foreach (int sampleSize, deviceInfo.supportedSampleSizes()) {
       
   235                         format.setSampleSize(sampleSize);
       
   236                         foreach (QAudioFormat::Endian endian, deviceInfo.supportedByteOrders()) {
       
   237                             format.setByteOrder(endian);
       
   238                             if (deviceInfo.isFormatSupported(format)) {
       
   239                                 allFormatsTable->setRowCount(row + 1);
       
   240 
       
   241                                 QTableWidgetItem *codecItem = new QTableWidgetItem(format.codec());
       
   242                                 allFormatsTable->setItem(row, 0, codecItem);
       
   243 
       
   244                                 QTableWidgetItem *frequencyItem = new QTableWidgetItem(QString("%1").arg(format.frequency()));
       
   245                                 allFormatsTable->setItem(row, 1, frequencyItem);
       
   246 
       
   247                                 QTableWidgetItem *channelsItem = new QTableWidgetItem(QString("%1").arg(format.channels()));
       
   248                                 allFormatsTable->setItem(row, 2, channelsItem);
       
   249 
       
   250                                 QTableWidgetItem *sampleTypeItem = new QTableWidgetItem(toString(format.sampleType()));
       
   251                                 allFormatsTable->setItem(row, 3, sampleTypeItem);
       
   252 
       
   253                                 QTableWidgetItem *sampleSizeItem = new QTableWidgetItem(QString("%1").arg(format.sampleSize()));
       
   254                                 allFormatsTable->setItem(row, 4, sampleSizeItem);
       
   255 
       
   256                                 QTableWidgetItem *byteOrderItem = new QTableWidgetItem(toString(format.byteOrder()));
       
   257                                 allFormatsTable->setItem(row, 5, byteOrderItem);
       
   258 
       
   259                                 ++row;
       
   260                             }
       
   261                         }
       
   262                     }
       
   263                 }
       
   264             }
       
   265         }
       
   266     }
       
   267 }
       
   268 
       
   269 void AudioTest::freqChanged(int idx)
       
   270 {
       
   271     // freq has changed
       
   272     settings.setFrequency(frequencyBox->itemText(idx).toInt());
       
   273 }
       
   274 
       
   275 void AudioTest::channelChanged(int idx)
       
   276 {
       
   277     settings.setChannels(channelsBox->itemText(idx).toInt());
       
   278 }
       
   279 
       
   280 void AudioTest::codecChanged(int idx)
       
   281 {
       
   282     settings.setCodec(codecsBox->itemText(idx));
       
   283 }
       
   284 
       
   285 void AudioTest::sampleSizeChanged(int idx)
       
   286 {
       
   287     settings.setSampleSize(sampleSizesBox->itemText(idx).toInt());
       
   288 }
       
   289 
       
   290 void AudioTest::sampleTypeChanged(int idx)
       
   291 {
       
   292     switch (sampleTypesBox->itemText(idx).toInt()) {
       
   293         case QAudioFormat::SignedInt:
       
   294             settings.setSampleType(QAudioFormat::SignedInt);
       
   295             break;
       
   296         case QAudioFormat::UnSignedInt:
       
   297             settings.setSampleType(QAudioFormat::UnSignedInt);
       
   298             break;
       
   299         case QAudioFormat::Float:
       
   300             settings.setSampleType(QAudioFormat::Float);
       
   301     }
       
   302 }
       
   303 
       
   304 void AudioTest::endianChanged(int idx)
       
   305 {
       
   306     switch (endianBox->itemText(idx).toInt()) {
       
   307         case QAudioFormat::LittleEndian:
       
   308             settings.setByteOrder(QAudioFormat::LittleEndian);
       
   309             break;
       
   310         case QAudioFormat::BigEndian:
       
   311             settings.setByteOrder(QAudioFormat::BigEndian);
       
   312     }
       
   313 }