| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 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 QtGui 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 qt-info@nokia.com. | 
|---|
| 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), server(0), bucket(0), looprem(0), looptotal(1) | 
|---|
| 90 | { | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | ~QSoundPrivate() | 
|---|
| 94 | { | 
|---|
| 95 | delete bucket; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | QString filename; | 
|---|
| 99 | QAuServer* server; | 
|---|
| 100 | QAuBucket* bucket; | 
|---|
| 101 | int looprem; | 
|---|
| 102 | int looptotal; | 
|---|
| 103 | }; | 
|---|
| 104 |  | 
|---|
| 105 | /*! | 
|---|
| 106 | \class QSound | 
|---|
| 107 | \brief The QSound class provides access to the platform audio facilities. | 
|---|
| 108 |  | 
|---|
| 109 | \ingroup multimedia | 
|---|
| 110 |  | 
|---|
| 111 |  | 
|---|
| 112 | Qt provides the most commonly required audio operation in GUI | 
|---|
| 113 | applications: asynchronously playing a sound file. This is most | 
|---|
| 114 | easily accomplished using the static play() function: | 
|---|
| 115 |  | 
|---|
| 116 | \snippet doc/src/snippets/code/src_gui_kernel_qsound.cpp 0 | 
|---|
| 117 |  | 
|---|
| 118 | Alternatively, create a QSound object from the sound file first | 
|---|
| 119 | and then call the play() slot: | 
|---|
| 120 |  | 
|---|
| 121 | \snippet doc/src/snippets/code/src_gui_kernel_qsound.cpp 1 | 
|---|
| 122 |  | 
|---|
| 123 | Once created a QSound object can be queried for its fileName() and | 
|---|
| 124 | total number of loops() (i.e. the number of times the sound will | 
|---|
| 125 | play). The number of repetitions can be altered using the | 
|---|
| 126 | setLoops() function. While playing the sound, the loopsRemaining() | 
|---|
| 127 | function returns the remaining number of repetitions. Use the | 
|---|
| 128 | isFinished() function to determine whether the sound has finished | 
|---|
| 129 | playing. | 
|---|
| 130 |  | 
|---|
| 131 | Sounds played using a QSound object may use more memory than the | 
|---|
| 132 | static play() function, but it may also play more immediately | 
|---|
| 133 | (depending on the underlying platform audio facilities). Use the | 
|---|
| 134 | static isAvailable() function to determine whether sound | 
|---|
| 135 | facilities exist on the platform. Which facilities that are | 
|---|
| 136 | actually used varies: | 
|---|
| 137 |  | 
|---|
| 138 | \table | 
|---|
| 139 | \header \o Platform \o Audio Facility | 
|---|
| 140 | \row | 
|---|
| 141 | \o Microsoft Windows | 
|---|
| 142 | \o The underlying multimedia system is used; only WAVE format sound files | 
|---|
| 143 | are supported. | 
|---|
| 144 | \row | 
|---|
| 145 | \o X11 | 
|---|
| 146 | \o The \l{ftp://ftp.x.org/contrib/audio/nas/}{Network Audio System} | 
|---|
| 147 | is used if available, otherwise all operations work silently. NAS | 
|---|
| 148 | supports WAVE and AU files. | 
|---|
| 149 | \row | 
|---|
| 150 | \o Mac OS X | 
|---|
| 151 | \o NSSound is used. All formats that NSSound supports, including QuickTime formats, | 
|---|
| 152 | are supported by Qt for Mac OS X. | 
|---|
| 153 | \row | 
|---|
| 154 | \o Qt for Embedded Linux | 
|---|
| 155 | \o A built-in mixing sound server is used, accessing \c /dev/dsp | 
|---|
| 156 | directly. Only the WAVE format is supported. | 
|---|
| 157 | \row | 
|---|
| 158 | \o Symbian | 
|---|
| 159 | \o CMdaAudioPlayerUtility is used. All formats that Symbian OS or devices support | 
|---|
| 160 | are supported also by Qt. | 
|---|
| 161 | \endtable | 
|---|
| 162 |  | 
|---|
| 163 | Note that QSound does not support \l{resources.html}{resources}. | 
|---|
| 164 | This might be fixed in a future Qt version. | 
|---|
| 165 | */ | 
|---|
| 166 |  | 
|---|
| 167 | /*! | 
|---|
| 168 | Plays the sound stored in the file specified by the given \a filename. | 
|---|
| 169 |  | 
|---|
| 170 | \sa stop(), loopsRemaining(), isFinished() | 
|---|
| 171 | */ | 
|---|
| 172 | void QSound::play(const QString& filename) | 
|---|
| 173 | { | 
|---|
| 174 | server().play(filename); | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | /*! | 
|---|
| 178 | Constructs a QSound object from the file specified by the given \a | 
|---|
| 179 | filename and with the given \a parent. | 
|---|
| 180 |  | 
|---|
| 181 | This may use more memory than the static play() function, but it | 
|---|
| 182 | may also play more immediately (depending on the underlying | 
|---|
| 183 | platform audio facilities). | 
|---|
| 184 |  | 
|---|
| 185 | \sa play() | 
|---|
| 186 | */ | 
|---|
| 187 | QSound::QSound(const QString& filename, QObject* parent) | 
|---|
| 188 | : QObject(*new QSoundPrivate(filename), parent) | 
|---|
| 189 | { | 
|---|
| 190 | d_func()->server = &server(); | 
|---|
| 191 | d_func()->server->init(this); | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | #ifdef QT3_SUPPORT | 
|---|
| 195 | /*! | 
|---|
| 196 | \obsolete | 
|---|
| 197 |  | 
|---|
| 198 | Constructs a QSound object from the file specified by the given \a | 
|---|
| 199 | filename and with the given \a parent and \a name. Use the | 
|---|
| 200 | QSound() construcor and QObject::setObjectName() instead. | 
|---|
| 201 |  | 
|---|
| 202 | \oldcode | 
|---|
| 203 | QSound *mySound = new QSound(filename, parent, name); | 
|---|
| 204 | \newcode | 
|---|
| 205 | QSounc *mySound = new QSound(filename, parent); | 
|---|
| 206 | mySound->setObjectName(name); | 
|---|
| 207 | \endcode | 
|---|
| 208 | */ | 
|---|
| 209 | QSound::QSound(const QString& filename, QObject* parent, const char* name) | 
|---|
| 210 | : QObject(*new QSoundPrivate(filename), parent) | 
|---|
| 211 | { | 
|---|
| 212 | setObjectName(QString::fromAscii(name)); | 
|---|
| 213 | server().init(this); | 
|---|
| 214 | } | 
|---|
| 215 | #endif | 
|---|
| 216 |  | 
|---|
| 217 | /*! | 
|---|
| 218 | Destroys this sound object. If the sound is not finished playing, | 
|---|
| 219 | the stop() function is called before the sound object is | 
|---|
| 220 | destructed. | 
|---|
| 221 |  | 
|---|
| 222 | \sa stop(), isFinished() | 
|---|
| 223 | */ | 
|---|
| 224 | QSound::~QSound() | 
|---|
| 225 | { | 
|---|
| 226 | if (!isFinished()) | 
|---|
| 227 | stop(); | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | /*! | 
|---|
| 231 | Returns true if the sound has finished playing; otherwise returns false. | 
|---|
| 232 |  | 
|---|
| 233 | \warning On Windows this function always returns true for unlooped sounds. | 
|---|
| 234 | */ | 
|---|
| 235 | bool QSound::isFinished() const | 
|---|
| 236 | { | 
|---|
| 237 | Q_D(const QSound); | 
|---|
| 238 | return d->looprem == 0; | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | /*! | 
|---|
| 242 | \overload | 
|---|
| 243 |  | 
|---|
| 244 | Starts playing the sound specified by this QSound object. | 
|---|
| 245 |  | 
|---|
| 246 | The function returns immediately.  Depending on the platform audio | 
|---|
| 247 | facilities, other sounds may stop or be mixed with the new | 
|---|
| 248 | sound. The sound can be played again at any time, possibly mixing | 
|---|
| 249 | or replacing previous plays of the sound. | 
|---|
| 250 |  | 
|---|
| 251 | \sa fileName() | 
|---|
| 252 | */ | 
|---|
| 253 | void QSound::play() | 
|---|
| 254 | { | 
|---|
| 255 | Q_D(QSound); | 
|---|
| 256 | d->looprem = d->looptotal; | 
|---|
| 257 | server().play(this); | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 | /*! | 
|---|
| 261 | Returns the number of times the sound will play. | 
|---|
| 262 |  | 
|---|
| 263 | \sa loopsRemaining(), setLoops() | 
|---|
| 264 | */ | 
|---|
| 265 | int QSound::loops() const | 
|---|
| 266 | { | 
|---|
| 267 | Q_D(const QSound); | 
|---|
| 268 | return d->looptotal; | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 | /*! | 
|---|
| 272 | Returns the remaining number of times the sound will loop (this | 
|---|
| 273 | value decreases each time the sound is played). | 
|---|
| 274 |  | 
|---|
| 275 | \sa loops(), isFinished() | 
|---|
| 276 | */ | 
|---|
| 277 | int QSound::loopsRemaining() const | 
|---|
| 278 | { | 
|---|
| 279 | Q_D(const QSound); | 
|---|
| 280 | return d->looprem; | 
|---|
| 281 | } | 
|---|
| 282 |  | 
|---|
| 283 | /*! | 
|---|
| 284 | \fn void QSound::setLoops(int number) | 
|---|
| 285 |  | 
|---|
| 286 | Sets the sound to repeat the given \a number of times when it is | 
|---|
| 287 | played. | 
|---|
| 288 |  | 
|---|
| 289 | Note that passing the value -1 will cause the sound to loop | 
|---|
| 290 | indefinitely. | 
|---|
| 291 |  | 
|---|
| 292 | \sa loops() | 
|---|
| 293 | */ | 
|---|
| 294 | void QSound::setLoops(int n) | 
|---|
| 295 | { | 
|---|
| 296 | Q_D(QSound); | 
|---|
| 297 | d->looptotal = n; | 
|---|
| 298 | } | 
|---|
| 299 |  | 
|---|
| 300 | /*! | 
|---|
| 301 | Returns the filename associated with this QSound object. | 
|---|
| 302 |  | 
|---|
| 303 | \sa QSound() | 
|---|
| 304 | */ | 
|---|
| 305 | QString QSound::fileName() const | 
|---|
| 306 | { | 
|---|
| 307 | Q_D(const QSound); | 
|---|
| 308 | return d->filename; | 
|---|
| 309 | } | 
|---|
| 310 |  | 
|---|
| 311 | /*! | 
|---|
| 312 | Stops the sound playing. | 
|---|
| 313 |  | 
|---|
| 314 | Note that on Windows the current loop will finish if a sound is | 
|---|
| 315 | played in a loop. | 
|---|
| 316 |  | 
|---|
| 317 | \sa play() | 
|---|
| 318 | */ | 
|---|
| 319 | void QSound::stop() | 
|---|
| 320 | { | 
|---|
| 321 | Q_D(QSound); | 
|---|
| 322 | server().stop(this); | 
|---|
| 323 | d->looprem = 0; | 
|---|
| 324 | } | 
|---|
| 325 |  | 
|---|
| 326 |  | 
|---|
| 327 | /*! | 
|---|
| 328 | Returns true if sound facilities exist on the platform; otherwise | 
|---|
| 329 | returns false. | 
|---|
| 330 |  | 
|---|
| 331 | If no sound is available, all QSound operations work silently and | 
|---|
| 332 | quickly. An application may choose either to notify the user if | 
|---|
| 333 | sound is crucial to the application or to operate silently without | 
|---|
| 334 | bothering the user. | 
|---|
| 335 |  | 
|---|
| 336 | Note: On Windows this always returns true because some sound card | 
|---|
| 337 | drivers do not implement a way to find out whether it is available | 
|---|
| 338 | or not. | 
|---|
| 339 | */ | 
|---|
| 340 | bool QSound::isAvailable() | 
|---|
| 341 | { | 
|---|
| 342 | return server().okay(); | 
|---|
| 343 | } | 
|---|
| 344 |  | 
|---|
| 345 | /*! | 
|---|
| 346 | Returns true if sound \a s was initialized using this server instance | 
|---|
| 347 | and false otherwise. | 
|---|
| 348 | */ | 
|---|
| 349 | bool QAuServer::isRelevant(QSound* s) | 
|---|
| 350 | { | 
|---|
| 351 | return s->d_func()->server == this; | 
|---|
| 352 | } | 
|---|
| 353 |  | 
|---|
| 354 | /*! | 
|---|
| 355 | Sets the internal bucket record of sound \a s to \a b, deleting | 
|---|
| 356 | any previous setting. | 
|---|
| 357 | */ | 
|---|
| 358 | void QAuServer::setBucket(QSound* s, QAuBucket* b) | 
|---|
| 359 | { | 
|---|
| 360 | delete s->d_func()->bucket; | 
|---|
| 361 | s->d_func()->bucket = b; | 
|---|
| 362 | } | 
|---|
| 363 |  | 
|---|
| 364 | /*! | 
|---|
| 365 | Returns the internal bucket record of sound \a s. | 
|---|
| 366 | */ | 
|---|
| 367 | QAuBucket* QAuServer::bucket(QSound* s) | 
|---|
| 368 | { | 
|---|
| 369 | return s->d_func()->bucket; | 
|---|
| 370 | } | 
|---|
| 371 |  | 
|---|
| 372 | /*! | 
|---|
| 373 | Decrements the QSound::loopRemaining() value for sound \a s, | 
|---|
| 374 | returning the result. | 
|---|
| 375 | */ | 
|---|
| 376 | int QAuServer::decLoop(QSound* s) | 
|---|
| 377 | { | 
|---|
| 378 | if (s->d_func()->looprem > 0) | 
|---|
| 379 | --s->d_func()->looprem; | 
|---|
| 380 | return s->d_func()->looprem; | 
|---|
| 381 | } | 
|---|
| 382 |  | 
|---|
| 383 | /*! | 
|---|
| 384 | Initializes the sound. The default implementation does nothing. | 
|---|
| 385 | */ | 
|---|
| 386 | void QAuServer::init(QSound*) | 
|---|
| 387 | { | 
|---|
| 388 | } | 
|---|
| 389 |  | 
|---|
| 390 | QAuBucket::~QAuBucket() | 
|---|
| 391 | { | 
|---|
| 392 | } | 
|---|
| 393 | /*! | 
|---|
| 394 | \fn bool QSound::available() | 
|---|
| 395 |  | 
|---|
| 396 | Use the isAvailable() function instead. | 
|---|
| 397 | */ | 
|---|
| 398 |  | 
|---|
| 399 | QT_END_NAMESPACE | 
|---|
| 400 |  | 
|---|
| 401 | #endif // QT_NO_SOUND | 
|---|