1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtMultimedia module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
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 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qaudiodevicefactory_p.h"
|
---|
43 | #include <QtMultimedia/qaudioengine.h>
|
---|
44 | #include <QtMultimedia/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 QtMultimedia
|
---|
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 | \snippet doc/src/snippets/audio/main.cpp 1
|
---|
118 | \dots 8
|
---|
119 | \snippet doc/src/snippets/audio/main.cpp 2
|
---|
120 |
|
---|
121 | A QAudioDeviceInfo is used by Qt to construct
|
---|
122 | classes that communicate with the device--such as
|
---|
123 | QAudioInput, and QAudioOutput. The static
|
---|
124 | functions defaultInputDevice(), defaultOutputDevice(), and
|
---|
125 | availableDevices() let you get a list of all available
|
---|
126 | devices. Devices are fetch according to the value of mode
|
---|
127 | this is specified by the QAudio::Mode enum.
|
---|
128 | The QAudioDeviceInfo returned are only valid for the QAudio::Mode.
|
---|
129 |
|
---|
130 | For instance:
|
---|
131 |
|
---|
132 | \code
|
---|
133 | foreach(const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput))
|
---|
134 | qDebug() << "Device name: " << deviceInfo.deviceName();
|
---|
135 | \endcode
|
---|
136 |
|
---|
137 | In this code sample, we loop through all devices that are able to output
|
---|
138 | sound, i.e., play an audio stream in a supported format. For each device we
|
---|
139 | find, we simply print the deviceName().
|
---|
140 |
|
---|
141 | \sa QAudioOutput, QAudioInput
|
---|
142 | */
|
---|
143 |
|
---|
144 | /*!
|
---|
145 | Constructs an empty QAudioDeviceInfo object.
|
---|
146 | */
|
---|
147 |
|
---|
148 | QAudioDeviceInfo::QAudioDeviceInfo():
|
---|
149 | d(new QAudioDeviceInfoPrivate)
|
---|
150 | {
|
---|
151 | }
|
---|
152 |
|
---|
153 | /*!
|
---|
154 | Constructs a copy of \a other.
|
---|
155 | */
|
---|
156 |
|
---|
157 | QAudioDeviceInfo::QAudioDeviceInfo(const QAudioDeviceInfo& other):
|
---|
158 | d(other.d)
|
---|
159 | {
|
---|
160 | }
|
---|
161 |
|
---|
162 | /*!
|
---|
163 | Destroy this audio device info.
|
---|
164 | */
|
---|
165 |
|
---|
166 | QAudioDeviceInfo::~QAudioDeviceInfo()
|
---|
167 | {
|
---|
168 | }
|
---|
169 |
|
---|
170 | /*!
|
---|
171 | Sets the QAudioDeviceInfo object to be equal to \a other.
|
---|
172 | */
|
---|
173 |
|
---|
174 | QAudioDeviceInfo& QAudioDeviceInfo::operator=(const QAudioDeviceInfo &other)
|
---|
175 | {
|
---|
176 | d = other.d;
|
---|
177 | return *this;
|
---|
178 | }
|
---|
179 |
|
---|
180 | /*!
|
---|
181 | Returns whether this QAudioDeviceInfo object holds a device definition.
|
---|
182 | */
|
---|
183 |
|
---|
184 | bool QAudioDeviceInfo::isNull() const
|
---|
185 | {
|
---|
186 | return d->info == 0;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /*!
|
---|
190 | Returns human readable name of audio device.
|
---|
191 |
|
---|
192 | Device names vary depending on platform/audio plugin being used.
|
---|
193 |
|
---|
194 | They are a unique string identifiers for the audio device.
|
---|
195 |
|
---|
196 | eg. default, Intel, U0x46d0x9a4
|
---|
197 | */
|
---|
198 |
|
---|
199 | QString QAudioDeviceInfo::deviceName() const
|
---|
200 | {
|
---|
201 | return isNull() ? QString() : d->info->deviceName();
|
---|
202 | }
|
---|
203 |
|
---|
204 | /*!
|
---|
205 | Returns true if \a settings are supported by the audio device of this QAudioDeviceInfo.
|
---|
206 | */
|
---|
207 |
|
---|
208 | bool QAudioDeviceInfo::isFormatSupported(const QAudioFormat &settings) const
|
---|
209 | {
|
---|
210 | return isNull() ? false : d->info->isFormatSupported(settings);
|
---|
211 | }
|
---|
212 |
|
---|
213 | /*!
|
---|
214 | Returns QAudioFormat of default settings.
|
---|
215 |
|
---|
216 | These settings are provided by the platform/audio plugin being used.
|
---|
217 |
|
---|
218 | They also are dependent on the QAudio::Mode being used.
|
---|
219 |
|
---|
220 | A typical audio system would provide something like:
|
---|
221 | \list
|
---|
222 | \o Input settings: 8000Hz mono 8 bit.
|
---|
223 | \o Output settings: 44100Hz stereo 16 bit little endian.
|
---|
224 | \endlist
|
---|
225 | */
|
---|
226 |
|
---|
227 | QAudioFormat QAudioDeviceInfo::preferredFormat() const
|
---|
228 | {
|
---|
229 | return isNull() ? QAudioFormat() : d->info->preferredFormat();
|
---|
230 | }
|
---|
231 |
|
---|
232 | /*!
|
---|
233 | Returns closest QAudioFormat to \a settings that system audio supports.
|
---|
234 |
|
---|
235 | These settings are provided by the platform/audio plugin being used.
|
---|
236 |
|
---|
237 | They also are dependent on the QAudio::Mode being used.
|
---|
238 | */
|
---|
239 |
|
---|
240 | QAudioFormat QAudioDeviceInfo::nearestFormat(const QAudioFormat &settings) const
|
---|
241 | {
|
---|
242 | if (isFormatSupported(settings))
|
---|
243 | return settings;
|
---|
244 |
|
---|
245 | QAudioFormat nearest = settings;
|
---|
246 |
|
---|
247 | nearest.setCodec(QLatin1String("audio/pcm"));
|
---|
248 |
|
---|
249 | if (nearest.sampleType() == QAudioFormat::Unknown) {
|
---|
250 | QAudioFormat preferred = preferredFormat();
|
---|
251 | nearest.setSampleType(preferred.sampleType());
|
---|
252 | }
|
---|
253 |
|
---|
254 | QMap<int,int> testFrequencies;
|
---|
255 | QList<int> frequenciesAvailable = supportedFrequencies();
|
---|
256 | QMap<int,int> testSampleSizes;
|
---|
257 | QList<int> sampleSizesAvailable = supportedSampleSizes();
|
---|
258 |
|
---|
259 | // Get sorted sampleSizes (equal to and ascending values only)
|
---|
260 | if (sampleSizesAvailable.contains(settings.sampleSize()))
|
---|
261 | testSampleSizes.insert(0,settings.sampleSize());
|
---|
262 | sampleSizesAvailable.removeAll(settings.sampleSize());
|
---|
263 | foreach (int size, sampleSizesAvailable) {
|
---|
264 | int larger = (size > settings.sampleSize()) ? size : settings.sampleSize();
|
---|
265 | int smaller = (size > settings.sampleSize()) ? settings.sampleSize() : size;
|
---|
|
---|