/examples/multimedia/audiodevices/audiodevices.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 316 lines · 225 code · 46 blank · 45 comment · 24 complexity · 846fd8841823ff76981f75d5daad99ea MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 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. #include <QAudioDeviceInfo>
  41. #include "audiodevices.h"
  42. // Utility functions for converting QAudioFormat fields into text
  43. QString toString(QAudioFormat::SampleType sampleType)
  44. {
  45. QString result;
  46. switch (sampleType) {
  47. case QAudioFormat::SignedInt:
  48. result = "SignedInt";
  49. break;
  50. case QAudioFormat::UnSignedInt:
  51. result = "UnSignedInt";
  52. break;
  53. case QAudioFormat::Float:
  54. result = "Float";
  55. break;
  56. default:
  57. case QAudioFormat::Unknown:
  58. result = "Unknown";
  59. break;
  60. }
  61. return result;
  62. }
  63. QString toString(QAudioFormat::Endian endian)
  64. {
  65. QString result("Unknown");
  66. switch (endian) {
  67. case QAudioFormat::LittleEndian:
  68. result = "LittleEndian";
  69. break;
  70. case QAudioFormat::BigEndian:
  71. result = "BigEndian";
  72. break;
  73. }
  74. return result;
  75. }
  76. AudioDevicesBase::AudioDevicesBase(QWidget *parent, Qt::WFlags f)
  77. : QMainWindow(parent, f)
  78. {
  79. setupUi(this);
  80. }
  81. AudioDevicesBase::~AudioDevicesBase() {}
  82. AudioTest::AudioTest(QWidget *parent, Qt::WFlags f)
  83. : AudioDevicesBase(parent, f)
  84. {
  85. mode = QAudio::AudioOutput;
  86. connect(testButton, SIGNAL(clicked()), SLOT(test()));
  87. connect(modeBox, SIGNAL(activated(int)), SLOT(modeChanged(int)));
  88. connect(deviceBox, SIGNAL(activated(int)), SLOT(deviceChanged(int)));
  89. connect(frequencyBox, SIGNAL(activated(int)), SLOT(freqChanged(int)));
  90. connect(channelsBox, SIGNAL(activated(int)), SLOT(channelChanged(int)));
  91. connect(codecsBox, SIGNAL(activated(int)), SLOT(codecChanged(int)));
  92. connect(sampleSizesBox, SIGNAL(activated(int)), SLOT(sampleSizeChanged(int)));
  93. connect(sampleTypesBox, SIGNAL(activated(int)), SLOT(sampleTypeChanged(int)));
  94. connect(endianBox, SIGNAL(activated(int)), SLOT(endianChanged(int)));
  95. connect(populateTableButton, SIGNAL(clicked()), SLOT(populateTable()));
  96. modeBox->setCurrentIndex(0);
  97. modeChanged(0);
  98. deviceBox->setCurrentIndex(0);
  99. deviceChanged(0);
  100. }
  101. AudioTest::~AudioTest()
  102. {
  103. }
  104. void AudioTest::test()
  105. {
  106. // tries to set all the settings picked.
  107. testResult->clear();
  108. if (!deviceInfo.isNull()) {
  109. if (deviceInfo.isFormatSupported(settings)) {
  110. testResult->setText(tr("Success"));
  111. nearestFreq->setText("");
  112. nearestChannel->setText("");
  113. nearestCodec->setText("");
  114. nearestSampleSize->setText("");
  115. nearestSampleType->setText("");
  116. nearestEndian->setText("");
  117. } else {
  118. QAudioFormat nearest = deviceInfo.nearestFormat(settings);
  119. testResult->setText(tr("Failed"));
  120. nearestFreq->setText(QString("%1").arg(nearest.frequency()));
  121. nearestChannel->setText(QString("%1").arg(nearest.channels()));
  122. nearestCodec->setText(nearest.codec());
  123. nearestSampleSize->setText(QString("%1").arg(nearest.sampleSize()));
  124. nearestSampleType->setText(toString(nearest.sampleType()));
  125. nearestEndian->setText(toString(nearest.byteOrder()));
  126. }
  127. }
  128. else
  129. testResult->setText(tr("No Device"));
  130. }
  131. void AudioTest::modeChanged(int idx)
  132. {
  133. testResult->clear();
  134. // mode has changed
  135. if (idx == 0)
  136. mode = QAudio::AudioInput;
  137. else
  138. mode = QAudio::AudioOutput;
  139. deviceBox->clear();
  140. foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(mode))
  141. deviceBox->addItem(deviceInfo.deviceName(), QVariant::fromValue(deviceInfo));
  142. deviceBox->setCurrentIndex(0);
  143. deviceChanged(0);
  144. }
  145. void AudioTest::deviceChanged(int idx)
  146. {
  147. testResult->clear();
  148. if (deviceBox->count() == 0)
  149. return;
  150. // device has changed
  151. deviceInfo = deviceBox->itemData(idx).value<QAudioDeviceInfo>();
  152. frequencyBox->clear();
  153. QList<int> freqz = deviceInfo.supportedFrequencies();
  154. for(int i = 0; i < freqz.size(); ++i)
  155. frequencyBox->addItem(QString("%1").arg(freqz.at(i)));
  156. if(freqz.size())
  157. settings.setFrequency(freqz.at(0));
  158. channelsBox->clear();
  159. QList<int> chz = deviceInfo.supportedChannels();
  160. for(int i = 0; i < chz.size(); ++i)
  161. channelsBox->addItem(QString("%1").arg(chz.at(i)));
  162. if(chz.size())
  163. settings.setChannels(chz.at(0));
  164. codecsBox->clear();
  165. QStringList codecz = deviceInfo.supportedCodecs();
  166. for (int i = 0; i < codecz.size(); ++i)
  167. codecsBox->addItem(QString("%1").arg(codecz.at(i)));
  168. if (codecz.size())
  169. settings.setCodec(codecz.at(0));
  170. // Add false to create failed condition!
  171. codecsBox->addItem("audio/test");
  172. sampleSizesBox->clear();
  173. QList<int> sampleSizez = deviceInfo.supportedSampleSizes();
  174. for (int i = 0; i < sampleSizez.size(); ++i)
  175. sampleSizesBox->addItem(QString("%1").arg(sampleSizez.at(i)));
  176. if (sampleSizez.size())
  177. settings.setSampleSize(sampleSizez.at(0));
  178. sampleTypesBox->clear();
  179. QList<QAudioFormat::SampleType> sampleTypez = deviceInfo.supportedSampleTypes();
  180. for (int i = 0; i < sampleTypez.size(); ++i)
  181. sampleTypesBox->addItem(toString(sampleTypez.at(i)));
  182. if (sampleTypez.size())
  183. settings.setSampleType(sampleTypez.at(0));
  184. endianBox->clear();
  185. QList<QAudioFormat::Endian> endianz = deviceInfo.supportedByteOrders();
  186. for (int i = 0; i < endianz.size(); ++i)
  187. endianBox->addItem(toString(endianz.at(i)));
  188. if (endianz.size())
  189. settings.setByteOrder(endianz.at(0));
  190. allFormatsTable->clearContents();
  191. }
  192. void AudioTest::populateTable()
  193. {
  194. int row = 0;
  195. QAudioFormat format;
  196. foreach (QString codec, deviceInfo.supportedCodecs()) {
  197. format.setCodec(codec);
  198. foreach (int frequency, deviceInfo.supportedFrequencies()) {
  199. format.setFrequency(frequency);
  200. foreach (int channels, deviceInfo.supportedChannels()) {
  201. format.setChannels(channels);
  202. foreach (QAudioFormat::SampleType sampleType, deviceInfo.supportedSampleTypes()) {
  203. format.setSampleType(sampleType);
  204. foreach (int sampleSize, deviceInfo.supportedSampleSizes()) {
  205. format.setSampleSize(sampleSize);
  206. foreach (QAudioFormat::Endian endian, deviceInfo.supportedByteOrders()) {
  207. format.setByteOrder(endian);
  208. if (deviceInfo.isFormatSupported(format)) {
  209. allFormatsTable->setRowCount(row + 1);
  210. QTableWidgetItem *codecItem = new QTableWidgetItem(format.codec());
  211. allFormatsTable->setItem(row, 0, codecItem);
  212. QTableWidgetItem *frequencyItem = new QTableWidgetItem(QString("%1").arg(format.frequency()));
  213. allFormatsTable->setItem(row, 1, frequencyItem);
  214. QTableWidgetItem *channelsItem = new QTableWidgetItem(QString("%1").arg(format.channels()));
  215. allFormatsTable->setItem(row, 2, channelsItem);
  216. QTableWidgetItem *sampleTypeItem = new QTableWidgetItem(toString(format.sampleType()));
  217. allFormatsTable->setItem(row, 3, sampleTypeItem);
  218. QTableWidgetItem *sampleSizeItem = new QTableWidgetItem(QString("%1").arg(format.sampleSize()));
  219. allFormatsTable->setItem(row, 4, sampleSizeItem);
  220. QTableWidgetItem *byteOrderItem = new QTableWidgetItem(toString(format.byteOrder()));
  221. allFormatsTable->setItem(row, 5, byteOrderItem);
  222. ++row;
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }
  230. }
  231. void AudioTest::freqChanged(int idx)
  232. {
  233. // freq has changed
  234. settings.setFrequency(frequencyBox->itemText(idx).toInt());
  235. }
  236. void AudioTest::channelChanged(int idx)
  237. {
  238. settings.setChannels(channelsBox->itemText(idx).toInt());
  239. }
  240. void AudioTest::codecChanged(int idx)
  241. {
  242. settings.setCodec(codecsBox->itemText(idx));
  243. }
  244. void AudioTest::sampleSizeChanged(int idx)
  245. {
  246. settings.setSampleSize(sampleSizesBox->itemText(idx).toInt());
  247. }
  248. void AudioTest::sampleTypeChanged(int idx)
  249. {
  250. switch (sampleTypesBox->itemText(idx).toInt()) {
  251. case QAudioFormat::SignedInt:
  252. settings.setSampleType(QAudioFormat::SignedInt);
  253. break;
  254. case QAudioFormat::UnSignedInt:
  255. settings.setSampleType(QAudioFormat::UnSignedInt);
  256. break;
  257. case QAudioFormat::Float:
  258. settings.setSampleType(QAudioFormat::Float);
  259. }
  260. }
  261. void AudioTest::endianChanged(int idx)
  262. {
  263. switch (endianBox->itemText(idx).toInt()) {
  264. case QAudioFormat::LittleEndian:
  265. settings.setByteOrder(QAudioFormat::LittleEndian);
  266. break;
  267. case QAudioFormat::BigEndian:
  268. settings.setByteOrder(QAudioFormat::BigEndian);
  269. }
  270. }