|
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 Qt Mobility Components. |
|
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 #include "qaudiodevicefactory_p.h" |
|
43 #include "qaudiosystem.h" |
|
44 #include "qaudiodeviceinfo.h" |
|
45 |
|
46 #include <QtCore/qmap.h> |
|
47 |
|
48 QT_BEGIN_NAMESPACE |
|
49 |
|
50 class QAudioDeviceInfoPrivate : public QSharedData |
|
51 { |
|
52 public: |
|
53 QAudioDeviceInfoPrivate():info(0) {} |
|
54 QAudioDeviceInfoPrivate(const QString &r, const QByteArray &h, QAudio::Mode m): |
|
55 realm(r), handle(h), mode(m) |
|
56 { |
|
57 info = QAudioDeviceFactory::audioDeviceInfo(realm, handle, mode); |
|
58 } |
|
59 |
|
60 QAudioDeviceInfoPrivate(const QAudioDeviceInfoPrivate &other): |
|
61 QSharedData(other), |
|
62 realm(other.realm), handle(other.handle), mode(other.mode) |
|
63 { |
|
64 info = QAudioDeviceFactory::audioDeviceInfo(realm, handle, mode); |
|
65 } |
|
66 |
|
67 QAudioDeviceInfoPrivate& operator=(const QAudioDeviceInfoPrivate &other) |
|
68 { |
|
69 delete info; |
|
70 |
|
71 realm = other.realm; |
|
72 handle = other.handle; |
|
73 mode = other.mode; |
|
74 info = QAudioDeviceFactory::audioDeviceInfo(realm, handle, mode); |
|
75 return *this; |
|
76 } |
|
77 |
|
78 ~QAudioDeviceInfoPrivate() |
|
79 { |
|
80 delete info; |
|
81 } |
|
82 |
|
83 QString realm; |
|
84 QByteArray handle; |
|
85 QAudio::Mode mode; |
|
86 QAbstractAudioDeviceInfo* info; |
|
87 }; |
|
88 |
|
89 |
|
90 /*! |
|
91 \class QAudioDeviceInfo |
|
92 \brief The QAudioDeviceInfo class provides an interface to query audio devices and their functionality. |
|
93 \inmodule QtMultimediaKit |
|
94 \ingroup multimedia |
|
95 |
|
96 \since 4.6 |
|
97 |
|
98 QAudioDeviceInfo lets you query for audio devices--such as sound |
|
99 cards and USB headsets--that are currently available on the system. |
|
100 The audio devices available are dependent on the platform or audio plugins installed. |
|
101 |
|
102 You can also query each device for the formats it supports. A |
|
103 format in this context is a set consisting of a specific byte |
|
104 order, channel, codec, frequency, sample rate, and sample type. A |
|
105 format is represented by the QAudioFormat class. |
|
106 |
|
107 The values supported by the the device for each of these |
|
108 parameters can be fetched with |
|
109 supportedByteOrders(), supportedChannelCounts(), supportedCodecs(), |
|
110 supportedSampleRates(), supportedSampleSizes(), and |
|
111 supportedSampleTypes(). The combinations supported are dependent on the platform, |
|
112 audio plugins installed and the audio device capabilities. If you need a specific format, you can check if |
|
113 the device supports it with isFormatSupported(), or fetch a |
|
114 supported format that is as close as possible to the format with |
|
115 nearestFormat(). For instance: |
|
116 |
|
117 \code |
|
118 QAudioFormat format; |
|
119 format.setFrequency(44100); |
|
120 \endcode |
|
121 \dots 8 |
|
122 \code |
|
123 format.setSampleType(QAudioFormat::SignedInt); |
|
124 |
|
125 QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice()); |
|
126 |
|
127 if (!info.isFormatSupported(format)) |
|
128 format = info.nearestFormat(format); |
|
129 \endcode |
|
130 |
|
131 A QAudioDeviceInfo is used by Qt to construct |
|
132 classes that communicate with the device--such as |
|
133 QAudioInput, and QAudioOutput. The static |
|
134 functions defaultInputDevice(), defaultOutputDevice(), and |
|
135 availableDevices() let you get a list of all available |
|
136 devices. Devices are fetch according to the value of mode |
|
137 this is specified by the QAudio::Mode enum. |
|
138 The QAudioDeviceInfo returned are only valid for the QAudio::Mode. |
|
139 |
|
140 For instance: |
|
141 |
|
142 \code |
|
143 foreach(const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput)) |
|
144 qDebug() << "Device name: " << deviceInfo.deviceName(); |
|
145 \endcode |
|
146 |
|
147 In this code sample, we loop through all devices that are able to output |
|
148 sound, i.e., play an audio stream in a supported format. For each device we |
|
149 find, we simply print the deviceName(). |
|
150 |
|
151 \sa QAudioOutput, QAudioInput |
|
152 */ |
|
153 |
|
154 /*! |
|
155 Constructs an empty QAudioDeviceInfo object. |
|
156 */ |
|
157 |
|
158 QAudioDeviceInfo::QAudioDeviceInfo(): |
|
159 d(new QAudioDeviceInfoPrivate) |
|
160 { |
|
161 } |
|
162 |
|
163 /*! |
|
164 Constructs a copy of \a other. |
|
165 */ |
|
166 |
|
167 QAudioDeviceInfo::QAudioDeviceInfo(const QAudioDeviceInfo& other): |
|
168 d(other.d) |
|
169 { |
|
170 } |
|
171 |
|
172 /*! |
|
173 Destroy this audio device info. |
|
174 */ |
|
175 |
|
176 QAudioDeviceInfo::~QAudioDeviceInfo() |
|
177 { |
|
178 } |
|
179 |
|
180 /*! |
|
181 Sets the QAudioDeviceInfo object to be equal to \a other. |
|
182 */ |
|
183 |
|
184 QAudioDeviceInfo& QAudioDeviceInfo::operator=(const QAudioDeviceInfo &other) |
|
185 { |
|
186 d = other.d; |
|
187 return *this; |
|
188 } |
|
189 |
|
190 /*! |
|
191 Returns whether this QAudioDeviceInfo object holds a device definition. |
|
192 */ |
|
193 |
|
194 bool QAudioDeviceInfo::isNull() const |
|
195 { |
|
196 return d->info == 0; |
|
197 } |
|
198 |
|
199 /*! |
|
200 Returns human readable name of audio device. |
|
201 |
|
202 Device names vary depending on platform/audio plugin being used. |
|
203 |
|
204 They are a unique string identifiers for the audio device. |
|
205 |
|
206 eg. default, Intel, U0x46d0x9a4 |
|
207 */ |
|
208 |
|
209 QString QAudioDeviceInfo::deviceName() const |
|
210 { |
|
211 return isNull() ? QString() : d->info->deviceName(); |
|
212 } |
|
213 |
|
214 /*! |
|
215 Returns true if \a settings are supported by the audio device of this QAudioDeviceInfo. |
|
216 */ |
|
217 |
|
218 bool QAudioDeviceInfo::isFormatSupported(const QAudioFormat &settings) const |
|
219 { |
|
220 return isNull() ? false : d->info->isFormatSupported(settings); |
|
221 } |
|
222 |
|
223 /*! |
|
224 Returns QAudioFormat of default settings. |
|
225 |
|
226 These settings are provided by the platform/audio plugin being used. |
|
227 |
|
228 They also are dependent on the QAudio::Mode being used. |
|
229 |
|
230 A typical audio system would provide something like: |
|
231 \list |
|
232 \o Input settings: 8000Hz mono 8 bit. |
|
233 \o Output settings: 44100Hz stereo 16 bit little endian. |
|
234 \endlist |
|
235 */ |
|
236 |
|
237 QAudioFormat QAudioDeviceInfo::preferredFormat() const |
|
238 { |
|
239 return isNull() ? QAudioFormat() : d->info->preferredFormat(); |
|
240 } |
|
241 |
|
242 /*! |
|
243 Returns closest QAudioFormat to \a settings that system audio supports. |
|
244 |
|
245 These settings are provided by the platform/audio plugin being used. |
|
246 |
|
247 They also are dependent on the QAudio::Mode being used. |
|
248 */ |
|
249 |
|
250 QAudioFormat QAudioDeviceInfo::nearestFormat(const QAudioFormat &settings) const |
|
251 { |
|
252 if (isFormatSupported(settings)) |
|
253 return settings; |
|
254 |
|
255 QAudioFormat nearest = settings; |
|
256 |
|
257 QList<QString> testCodecs = supportedCodecs(); |
|
258 QList<int> testChannels = supportedChannels(); |
|
259 QList<QAudioFormat::Endian> testByteOrders = supportedByteOrders(); |
|
260 QList<QAudioFormat::SampleType> testSampleTypes; |
|
261 QList<QAudioFormat::SampleType> sampleTypesAvailable = supportedSampleTypes(); |
|
262 QMap<int,int> testFrequencies; |
|
263 QList<int> frequenciesAvailable = supportedFrequencies(); |
|
264 QMap<int,int> testSampleSizes; |
|
265 QList<int> sampleSizesAvailable = supportedSampleSizes(); |
|
266 |
|
267 // Get sorted lists for checking |
|
268 if (testCodecs.contains(settings.codec())) { |
|
269 testCodecs.removeAll(settings.codec()); |
|
270 testCodecs.insert(0, settings.codec()); |
|
271 } |
|
272 testChannels.removeAll(settings.channels()); |
|
273 testChannels.insert(0, settings.channels()); |
|
274 testByteOrders.removeAll(settings.byteOrder()); |
|
275 testByteOrders.insert(0, settings.byteOrder()); |
|
276 |
|
277 if (sampleTypesAvailable.contains(settings.sampleType())) |
|
278 testSampleTypes.append(settings.sampleType()); |
|
279 if (sampleTypesAvailable.contains(QAudioFormat::SignedInt)) |
|
280 testSampleTypes.append(QAudioFormat::SignedInt); |
|
281 if (sampleTypesAvailable.contains(QAudioFormat::UnSignedInt)) |
|
282 testSampleTypes.append(QAudioFormat::UnSignedInt); |
|
283 if (sampleTypesAvailable.contains(QAudioFormat::Float)) |
|
284 testSampleTypes.append(QAudioFormat::Float); |
|
285 |
|
286 if (sampleSizesAvailable.contains(settings.sampleSize())) |
|
287 testSampleSizes.insert(0,settings.sampleSize()); |
|
288 sampleSizesAvailable.removeAll(settings.sampleSize()); |
|
289 foreach (int size, sampleSizesAvailable) { |
|
290 int larger = (size > settings.sampleSize()) ? size : settings.sampleSize(); |
|
291 int smaller = (size > settings.sampleSize()) ? settings.sampleSize() : size; |
|
292 bool isMultiple = ( 0 == (larger % smaller)); |
|
293 int diff = larger - smaller; |
|
294 testSampleSizes.insert((isMultiple ? diff : diff+100000), size); |
|
295 } |
|
296 if (frequenciesAvailable.contains(settings.frequency())) |
|
297 testFrequencies.insert(0,settings.frequency()); |
|
298 frequenciesAvailable.removeAll(settings.frequency()); |
|
299 foreach (int frequency, frequenciesAvailable) { |
|
300 int larger = (frequency > settings.frequency()) ? frequency : settings.frequency(); |
|
301 int smaller = (frequency > settings.frequency()) ? settings.frequency() : frequency; |
|
302 bool isMultiple = ( 0 == (larger % smaller)); |
|
303 int diff = larger - smaller; |
|
304 testFrequencies.insert((isMultiple ? diff : diff+100000), frequency); |
|
305 } |
|
306 |
|
307 // Try to find nearest |
|
308 foreach (QString codec, testCodecs) { |
|
309 nearest.setCodec(codec); |
|
310 foreach (QAudioFormat::Endian order, testByteOrders) { |
|
311 nearest.setByteOrder(order); |
|
312 foreach (QAudioFormat::SampleType sample, testSampleTypes) { |
|
313 nearest.setSampleType(sample); |
|
314 QMapIterator<int, int> sz(testSampleSizes); |
|
315 while (sz.hasNext()) { |
|
316 sz.next(); |
|
317 nearest.setSampleSize(sz.value()); |
|
318 foreach (int channel, testChannels) { |
|
319 nearest.setChannels(channel); |
|
320 QMapIterator<int, int> i(testFrequencies); |
|
321 while (i.hasNext()) { |
|
322 i.next(); |
|
323 nearest.setFrequency(i.value()); |
|
324 if (isFormatSupported(nearest)) |
|
325 return nearest; |
|
326 } |
|
327 } |
|
328 } |
|
329 } |
|
330 } |
|
331 } |
|
332 //Fallback |
|
333 return preferredFormat(); |
|
334 } |
|
335 |
|
336 /*! |
|
337 Returns a list of supported codecs. |
|
338 |
|
339 All platform and plugin implementations should provide support for: |
|
340 |
|
341 "audio/pcm" - Linear PCM |
|
342 |
|
343 For writing plugins to support additional codecs refer to: |
|
344 |
|
345 http://www.iana.org/assignments/media-types/audio/ |
|
346 */ |
|
347 |
|
348 QStringList QAudioDeviceInfo::supportedCodecs() const |
|
349 { |
|
350 return isNull() ? QStringList() : d->info->supportedCodecs(); |
|
351 } |
|
352 |
|
353 /*! |
|
354 Returns a list of supported sample rates. |
|
355 |
|
356 \since 4.7 |
|
357 */ |
|
358 |
|
359 QList<int> QAudioDeviceInfo::supportedSampleRates() const |
|
360 { |
|
361 return supportedFrequencies(); |
|
362 } |
|
363 |
|
364 /*! |
|
365 \obsolete |
|
366 |
|
367 Use supportedSampleRates() instead. |
|
368 */ |
|
369 |
|
370 QList<int> QAudioDeviceInfo::supportedFrequencies() const |
|
371 { |
|
372 return isNull() ? QList<int>() : d->info->supportedSampleRates(); |
|
373 } |
|
374 |
|
375 /*! |
|
376 Returns a list of supported channel counts. |
|
377 |
|
378 \since 4.7 |
|
379 */ |
|
380 |
|
381 QList<int> QAudioDeviceInfo::supportedChannelCounts() const |
|
382 { |
|
383 return supportedChannels(); |
|
384 } |
|
385 |
|
386 /*! |
|
387 \obsolete |
|
388 |
|
389 Use supportedChannelCount() instead. |
|
390 */ |
|
391 |
|
392 QList<int> QAudioDeviceInfo::supportedChannels() const |
|
393 { |
|
394 return isNull() ? QList<int>() : d->info->supportedChannelCounts(); |
|
395 } |
|
396 |
|
397 /*! |
|
398 Returns a list of supported sample sizes. |
|
399 */ |
|
400 |
|
401 QList<int> QAudioDeviceInfo::supportedSampleSizes() const |
|
402 { |
|
403 return isNull() ? QList<int>() : d->info->supportedSampleSizes(); |
|
404 } |
|
405 |
|
406 /*! |
|
407 Returns a list of supported byte orders. |
|
408 */ |
|
409 |
|
410 QList<QAudioFormat::Endian> QAudioDeviceInfo::supportedByteOrders() const |
|
411 { |
|
412 return isNull() ? QList<QAudioFormat::Endian>() : d->info->supportedByteOrders(); |
|
413 } |
|
414 |
|
415 /*! |
|
416 Returns a list of supported sample types. |
|
417 */ |
|
418 |
|
419 QList<QAudioFormat::SampleType> QAudioDeviceInfo::supportedSampleTypes() const |
|
420 { |
|
421 return isNull() ? QList<QAudioFormat::SampleType>() : d->info->supportedSampleTypes(); |
|
422 } |
|
423 |
|
424 /*! |
|
425 Returns the name of the default input audio device. |
|
426 All platform and audio plugin implementations provide a default audio device to use. |
|
427 */ |
|
428 |
|
429 QAudioDeviceInfo QAudioDeviceInfo::defaultInputDevice() |
|
430 { |
|
431 return QAudioDeviceFactory::defaultInputDevice(); |
|
432 } |
|
433 |
|
434 /*! |
|
435 Returns the name of the default output audio device. |
|
436 All platform and audio plugin implementations provide a default audio device to use. |
|
437 */ |
|
438 |
|
439 QAudioDeviceInfo QAudioDeviceInfo::defaultOutputDevice() |
|
440 { |
|
441 return QAudioDeviceFactory::defaultOutputDevice(); |
|
442 } |
|
443 |
|
444 /*! |
|
445 Returns a list of audio devices that support \a mode. |
|
446 */ |
|
447 |
|
448 QList<QAudioDeviceInfo> QAudioDeviceInfo::availableDevices(QAudio::Mode mode) |
|
449 { |
|
450 return QAudioDeviceFactory::availableDevices(mode); |
|
451 } |
|
452 |
|
453 |
|
454 /*! |
|
455 \internal |
|
456 */ |
|
457 |
|
458 QAudioDeviceInfo::QAudioDeviceInfo(const QString &realm, const QByteArray &handle, QAudio::Mode mode): |
|
459 d(new QAudioDeviceInfoPrivate(realm, handle, mode)) |
|
460 { |
|
461 } |
|
462 |
|
463 /*! |
|
464 \internal |
|
465 */ |
|
466 |
|
467 QString QAudioDeviceInfo::realm() const |
|
468 { |
|
469 return d->realm; |
|
470 } |
|
471 |
|
472 /*! |
|
473 \internal |
|
474 */ |
|
475 |
|
476 QByteArray QAudioDeviceInfo::handle() const |
|
477 { |
|
478 return d->handle; |
|
479 } |
|
480 |
|
481 |
|
482 /*! |
|
483 \internal |
|
484 */ |
|
485 |
|
486 QAudio::Mode QAudioDeviceInfo::mode() const |
|
487 { |
|
488 return d->mode; |
|
489 } |
|
490 |
|
491 QT_END_NAMESPACE |
|
492 |