1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** 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 are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qsound.h"
|
---|
43 |
|
---|
44 | #ifndef QT_NO_SOUND
|
---|
45 |
|
---|
46 | #include "qlist.h"
|
---|
47 | #include <private/qobject_p.h>
|
---|
48 | #include "qsound_p.h"
|
---|
49 |
|
---|
50 | QT_BEGIN_NAMESPACE
|
---|
51 |
|
---|
52 | static QList<QAuServer*> *servers=0;
|
---|
53 |
|
---|
54 | QAuServer::QAuServer(QObject* parent)
|
---|
55 | : QObject(parent)
|
---|
56 | {
|
---|
57 | if (!servers)
|
---|
58 | servers = new QList<QAuServer*>;
|
---|
59 | servers->prepend(this);
|
---|
60 | }
|
---|
61 |
|
---|
62 | QAuServer::~QAuServer()
|
---|
63 | {
|
---|
64 | servers->removeAll(this);
|
---|
65 | if (servers->count() == 0) {
|
---|
66 | delete servers;
|
---|
67 | servers = 0;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | void QAuServer::play(const QString& filename)
|
---|
72 | {
|
---|
73 | QSound s(filename);
|
---|
74 | play(&s);
|
---|
75 | }
|
---|
76 |
|
---|
77 | extern QAuServer* qt_new_audio_server();
|
---|
78 |
|
---|
79 | static QAuServer& server()
|
---|
80 | {
|
---|
81 | if (!servers) qt_new_audio_server();
|
---|
82 | return *servers->first();
|
---|
83 | }
|
---|
84 |
|
---|
85 | class QSoundPrivate : public QObjectPrivate
|
---|
86 | {
|
---|
87 | public:
|
---|
88 | QSoundPrivate(const QString& fname)
|
---|
89 | : filename(fname), bucket(0), looprem(0), looptotal(1)
|
---|
90 | {
|
---|
91 | }
|
---|
92 |
|
---|
93 | ~QSoundPrivate()
|
---|
94 | {
|
---|
95 | delete bucket;
|
---|
96 | }
|
---|
97 |
|
---|
98 | QString filename;
|
---|
99 | QAuBucket* bucket;
|
---|
100 | int looprem;
|
---|
101 | int looptotal;
|
---|
102 | };
|
---|
103 |
|
---|
104 | /*!
|
---|
105 | \class QSound
|
---|
106 | \brief The QSound class provides access to the platform audio facilities.
|
---|
107 |
|
---|
108 | \ingroup multimedia
|
---|
109 | \mainclass
|
---|
110 |
|
---|
111 | Qt provides the most commonly required audio operation in GUI
|
---|
112 | applications: asynchronously playing a sound file. This is most
|
---|
113 | easily accomplished using the static play() function:
|
---|
114 |
|
---|
115 | \snippet doc/src/snippets/code/src_gui_kernel_qsound.cpp 0
|
---|
116 |
|
---|
117 | Alternatively, create a QSound object from the sound file first
|
---|
118 | and then call the play() slot:
|
---|
119 |
|
---|
120 | \snippet doc/src/snippets/code/src_gui_kernel_qsound.cpp 1
|
---|
121 |
|
---|
122 | Once created a QSound object can be queried for its fileName() and
|
---|
123 | total number of loops() (i.e. the number of times the sound will
|
---|
124 | play). The number of repetitions can be altered using the
|
---|
125 | setLoops() function. While playing the sound, the loopsRemaining()
|
---|
126 | function returns the remaining number of repetitions. Use the
|
---|
127 | isFinished() function to determine whether the sound has finished
|
---|
128 | playing.
|
---|
129 |
|
---|
130 | Sounds played using a QSound object may use more memory than the
|
---|
131 | static play() function, but it may also play more immediately
|
---|
132 | (depending on the underlying platform audio facilities). Use the
|
---|
133 | static isAvailable() function to determine whether sound
|
---|
134 | facilities exist on the platform. Which facilities that are
|
---|
135 | actually used varies:
|
---|
136 |
|
---|
137 | \table
|
---|
138 | \header \o Platform \o Audio Facility
|
---|
139 | \row
|
---|
140 | \o Microsoft Windows
|
---|
141 | \o The underlying multimedia system is used; only WAVE format sound files
|
---|
142 | are supported.
|
---|
143 | \row
|
---|
144 | \o X11
|
---|
145 | \o The \l{ftp://ftp.x.org/contrib/audio/nas/}{Network Audio System}
|
---|
146 | is used if available, otherwise all operations work silently. NAS
|
---|
147 | supports WAVE and AU files.
|
---|
148 | \row
|
---|
149 | \o Mac OS X
|
---|
150 | \o NSSound is used. All formats that NSSound supports, including QuickTime formats,
|
---|
151 | are supported by Qt for Mac OS X.
|
---|
152 | \row
|
---|
153 | \o Qt for Embedded Linux
|
---|
154 | \o A built-in mixing sound server is used, accessing \c /dev/dsp
|
---|
155 | directly. Only the WAVE format is supported.
|
---|
156 | \endtable
|
---|
157 |
|
---|
158 | Note that QSound does not support \l{resources.html}{resources}.
|
---|
159 | This might be fixed in a future Qt version.
|
---|
160 | */
|
---|
161 |
|
---|
162 | /*!
|
---|
163 | Plays the sound stored in the file specified by the given \a filename.
|
---|
164 |
|
---|
165 | \sa stop(), loopsRemaining(), isFinished()
|
---|
166 | */
|
---|
167 | void QSound::play(const QString& filename)
|
---|
168 | {
|
---|
169 | server().play(filename);
|
---|
170 | }
|
---|
171 |
|
---|
172 | /*!
|
---|
173 | Constructs a QSound object from the file specified by the given \a
|
---|
174 | filename and with the given \a parent.
|
---|
175 |
|
---|
176 | This may use more memory than the static play() function, but it
|
---|
177 | may also play more immediately (depending on the underlying
|
---|
178 | platform audio facilities).
|
---|
179 |
|
---|
180 | \sa play()
|
---|
181 | */
|
---|
182 | QSound::QSound(const QString& filename, QObject* parent)
|
---|
183 | : QObject(*new QSoundPrivate(filename), parent)
|
---|
184 | {
|
---|
185 | server().init(this);
|
---|
186 | }
|
---|
187 |
|
---|
188 | #ifdef QT3_SUPPORT
|
---|
189 | /*!
|
---|
190 | \obsolete
|
---|
191 |
|
---|
192 | Constructs a QSound object from the file specified by the given \a
|
---|
193 | filename and with the given \a parent and \a name. Use the
|
---|
194 | QSound() construcor and QObject::setObjectName() instead.
|
---|
195 |
|
---|
196 | \oldcode
|
---|
197 | QSound *mySound = new QSound(filename, parent, name);
|
---|
198 | \newcode
|
---|
199 | QSounc *mySound = new QSound(filename, parent);
|
---|
200 | mySound->setObjectName(name);
|
---|
201 | \endcode
|
---|
202 | */
|
---|
203 | QSound::QSound(const QString& filename, QObject* parent, const char* name)
|
---|
204 | : QObject(*new QSoundPrivate(filename), parent)
|
---|
205 | {
|
---|
206 | setObjectName(QString::fromAscii(name));
|
---|
207 | server().init(this);
|
---|
208 | }
|
---|
209 | #endif
|
---|
210 |
|
---|
211 | /*!
|
---|
212 | Destroys this sound object. If the sound is not finished playing,
|
---|
213 | the stop() function is called before the sound object is
|
---|
214 | destructed.
|
---|
215 |
|
---|
216 | \sa stop(), isFinished()
|
---|
217 | */
|
---|
218 | QSound::~QSound()
|
---|
219 | {
|
---|
220 | if (!isFinished())
|
---|
221 | stop();
|
---|
222 | }
|
---|
223 |
|
---|
224 | /*!
|
---|
225 | Returns true if the sound has finished playing; otherwise returns false.
|
---|
226 |
|
---|
227 | \warning On Windows this function always returns true for unlooped sounds.
|
---|
228 | */
|
---|
229 | bool QSound::isFinished() const
|
---|
230 | {
|
---|
231 | Q_D(const QSound);
|
---|
232 | return d->looprem == 0;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /*!
|
---|
236 | \overload
|
---|
237 |
|
---|
238 | Starts playing the sound specified by this QSound object.
|
---|
239 |
|
---|
240 | The function returns immediately. Depending on the platform audio
|
---|
241 | facilities, other sounds may stop or be mixed with the new
|
---|
242 | sound. The sound can be played again at any time, possibly mixing
|
---|
243 | or replacing previous plays of the sound.
|
---|
244 |
|
---|
245 | \sa fileName()
|
---|
246 | */
|
---|
247 | void QSound::play()
|
---|
248 | {
|
---|
249 | Q_D(QSound);
|
---|
250 | d->looprem = d->looptotal;
|
---|
251 | server().play(this);
|
---|
252 | }
|
---|
253 |
|
---|
254 | /*!
|
---|
255 | Returns the number of times the sound will play.
|
---|
256 |
|
---|
257 | \sa loopsRemaining(), setLoops()
|
---|
258 | */
|
---|
259 | int QSound::loops() const
|
---|
260 | {
|
---|
261 | Q_D(const QSound);
|
---|
262 | return d->looptotal;
|
---|
263 | }
|
---|
264 |
|
---|
265 | /*!
|
---|
266 | Returns the remaining number of times the sound will loop (this
|
---|
267 | value decreases each time the sound is played).
|
---|
268 |
|
---|
269 | \sa loops(), isFinished()
|
---|
270 | */
|
---|
271 | int QSound::loopsRemaining() const
|
---|
272 | {
|
---|
273 | Q_D(const QSound);
|
---|
274 | return d->looprem;
|
---|
275 | }
|
---|
276 |
|
---|
277 | /*!
|
---|
278 | \fn void QSound::setLoops(int number)
|
---|
279 |
|
---|
280 | Sets the sound to repeat the given \a number of times when it is
|
---|
281 | played.
|
---|
282 |
|
---|
283 | Note that passing the value -1 will cause the sound to loop
|
---|
284 | indefinitely.
|
---|
285 |
|
---|
286 | \sa loops()
|
---|
287 | */
|
---|
288 | void QSound::setLoops(int n)
|
---|
289 | {
|
---|
290 | Q_D(QSound);
|
---|
291 | d->looptotal = n;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /*!
|
---|
295 | Returns the filename associated with this QSound object.
|
---|
296 |
|
---|
297 | \sa QSound()
|
---|
298 | */
|
---|
299 | QString QSound::fileName() const
|
---|
300 | {
|
---|
301 | Q_D(const QSound);
|
---|
302 | return d->filename;
|
---|
303 | }
|
---|
304 |
|
---|
305 | /*!
|
---|
306 | Stops the sound playing.
|
---|
307 |
|
---|
308 | Note that on Windows the current loop will finish if a sound is
|
---|
309 | played in a loop.
|
---|
310 |
|
---|
311 | \sa play()
|
---|
312 | */
|
---|
313 | void QSound::stop()
|
---|
314 | {
|
---|
315 | Q_D(QSound);
|
---|
316 | server().stop(this);
|
---|
317 | d->looprem = 0;
|
---|
318 | }
|
---|
319 |
|
---|
320 |
|
---|
321 | /*!
|
---|
322 | Returns true if sound facilities exist on the platform; otherwise
|
---|
323 | returns false.
|
---|
324 |
|
---|
325 | If no sound is available, all QSound operations work silently and
|
---|
326 | quickly. An application may choose either to notify the user if
|
---|
327 | sound is crucial to the application or to operate silently without
|
---|
328 | bothering the user.
|
---|
329 |
|
---|
330 | Note: On Windows this always returns true because some sound card
|
---|
331 | drivers do not implement a way to find out whether it is available
|
---|
332 | or not.
|
---|
333 | */
|
---|
334 | bool QSound::isAvailable()
|
---|
335 | {
|
---|
336 | return server().okay();
|
---|
337 | }
|
---|
338 |
|
---|
339 | /*!
|
---|
340 | Sets the internal bucket record of sound \a s to \a b, deleting
|
---|
341 | any previous setting.
|
---|
342 | */
|
---|
343 | void QAuServer::setBucket(QSound* s, QAuBucket* b)
|
---|
344 | {
|
---|
345 | delete s->d_func()->bucket;
|
---|
346 | s->d_func()->bucket = b;
|
---|
347 | }
|
---|
348 |
|
---|
349 | /*!
|
---|
350 | Returns the internal bucket record of sound \a s.
|
---|
351 | */
|
---|
352 | QAuBucket* QAuServer::bucket(QSound* s)
|
---|
353 | {
|
---|
354 | return s->d_func()->bucket;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /*!
|
---|
358 | Decrements the QSound::loopRemaining() value for sound \a s,
|
---|
359 | returning the result.
|
---|
360 | */
|
---|
361 | int QAuServer::decLoop(QSound* s)
|
---|
362 | {
|
---|
363 | if (s->d_func()->looprem > 0)
|
---|
364 | --s->d_func()->looprem;
|
---|
365 | return s->d_func()->looprem;
|
---|
366 | }
|
---|
367 |
|
---|
368 | /*!
|
---|
369 | Initializes the sound. The default implementation does nothing.
|
---|
370 | */
|
---|
371 | void QAuServer::init(QSound*)
|
---|
372 | {
|
---|
373 | }
|
---|
374 |
|
---|
375 | QAuBucket::~QAuBucket()
|
---|
376 | {
|
---|
377 | }
|
---|
378 | /*!
|
---|
379 | \fn bool QSound::available()
|
---|
380 |
|
---|
381 | Use the isAvailable() function instead.
|
---|
382 | */
|
---|
383 |
|
---|
384 | QT_END_NAMESPACE
|
---|
385 |
|
---|
386 | #endif // QT_NO_SOUND
|
---|