| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: qsound.cpp 35 2005-12-11 15:03:18Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Implementation of QSound class and QAuServer internal class
|
|---|
| 5 | **
|
|---|
| 6 | ** Created : 000117
|
|---|
| 7 | **
|
|---|
| 8 | ** Copyright (C) 1999-2000 Trolltech AS. All rights reserved.
|
|---|
| 9 | **
|
|---|
| 10 | ** This file is part of the kernel module of the Qt GUI Toolkit.
|
|---|
| 11 | **
|
|---|
| 12 | ** This file may be distributed under the terms of the Q Public License
|
|---|
| 13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
|---|
| 14 | ** LICENSE.QPL included in the packaging of this file.
|
|---|
| 15 | **
|
|---|
| 16 | ** This file may be distributed and/or modified under the terms of the
|
|---|
| 17 | ** GNU General Public License version 2 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 19 | ** packaging of this file.
|
|---|
| 20 | **
|
|---|
| 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
|---|
| 22 | ** licenses may use this file in accordance with the Qt Commercial License
|
|---|
| 23 | ** Agreement provided with the Software.
|
|---|
| 24 | **
|
|---|
| 25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|---|
| 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 27 | **
|
|---|
| 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
|---|
| 29 | ** information about Qt Commercial License Agreements.
|
|---|
| 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
|---|
| 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
|---|
| 32 | **
|
|---|
| 33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
|---|
| 34 | ** not clear to you.
|
|---|
| 35 | **
|
|---|
| 36 | **********************************************************************/
|
|---|
| 37 |
|
|---|
| 38 | #include "qsound.h"
|
|---|
| 39 |
|
|---|
| 40 | #ifndef QT_NO_SOUND
|
|---|
| 41 |
|
|---|
| 42 | #include "qptrlist.h"
|
|---|
| 43 |
|
|---|
| 44 | static QPtrList<QAuServer> *servers=0;
|
|---|
| 45 |
|
|---|
| 46 | QAuServer::QAuServer(QObject* parent, const char* name) :
|
|---|
| 47 | QObject(parent,name)
|
|---|
| 48 | {
|
|---|
| 49 | if ( !servers ) {
|
|---|
| 50 | servers = new QPtrList<QAuServer>;
|
|---|
| 51 | // ### add cleanup
|
|---|
| 52 | }
|
|---|
| 53 | servers->prepend(this);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | QAuServer::~QAuServer()
|
|---|
| 57 | {
|
|---|
| 58 | servers->remove(this);
|
|---|
| 59 | if ( servers->count() == 0 ) {
|
|---|
| 60 | delete servers;
|
|---|
| 61 | servers = 0;
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | void QAuServer::play(const QString& filename)
|
|---|
| 66 | {
|
|---|
| 67 | QSound s(filename);
|
|---|
| 68 | play(&s);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | extern QAuServer* qt_new_audio_server();
|
|---|
| 72 |
|
|---|
| 73 | static QAuServer& server()
|
|---|
| 74 | {
|
|---|
| 75 | if (!servers) qt_new_audio_server();
|
|---|
| 76 | return *servers->first();
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | class QSoundData {
|
|---|
| 80 | public:
|
|---|
| 81 | QSoundData(const QString& fname) :
|
|---|
| 82 | filename(fname), bucket(0), looprem(0), looptotal(1)
|
|---|
| 83 | {
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | ~QSoundData()
|
|---|
| 87 | {
|
|---|
| 88 | delete bucket;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | QString filename;
|
|---|
| 92 | QAuBucket* bucket;
|
|---|
| 93 | int looprem;
|
|---|
| 94 | int looptotal;
|
|---|
| 95 | };
|
|---|
| 96 |
|
|---|
| 97 | /*!
|
|---|
| 98 | \class QSound qsound.h
|
|---|
| 99 | \brief The QSound class provides access to the platform audio facilities.
|
|---|
| 100 |
|
|---|
| 101 | \ingroup multimedia
|
|---|
| 102 | \mainclass
|
|---|
| 103 |
|
|---|
| 104 | Qt provides the most commonly required audio operation in GUI
|
|---|
| 105 | applications: asynchronously playing a sound file. This is most
|
|---|
| 106 | easily accomplished with a single call:
|
|---|
| 107 | \code
|
|---|
| 108 | QSound::play("mysounds/bells.wav");
|
|---|
| 109 | \endcode
|
|---|
| 110 |
|
|---|
| 111 | A second API is provided in which a QSound object is created from
|
|---|
| 112 | a sound file and is played later:
|
|---|
| 113 | \code
|
|---|
| 114 | QSound bells("mysounds/bells.wav");
|
|---|
| 115 |
|
|---|
| 116 | bells.play();
|
|---|
| 117 | \endcode
|
|---|
| 118 |
|
|---|
| 119 | Sounds played using the second model may use more memory but play
|
|---|
| 120 | more immediately than sounds played using the first model,
|
|---|
| 121 | depending on the underlying platform audio facilities.
|
|---|
| 122 |
|
|---|
| 123 | On Microsoft Windows the underlying multimedia system is used;
|
|---|
| 124 | only WAVE format sound files are supported.
|
|---|
| 125 |
|
|---|
| 126 | On X11 the \link ftp://ftp.x.org/contrib/audio/nas/ Network Audio
|
|---|
| 127 | System\endlink is used if available, otherwise all operations work
|
|---|
| 128 | silently. NAS supports WAVE and AU files.
|
|---|
| 129 |
|
|---|
| 130 | On Macintosh, ironically, we use QT (\link
|
|---|
| 131 | http://quicktime.apple.com QuickTime\endlink) for sound, this
|
|---|
| 132 | means all QuickTime formats are supported by Qt/Mac.
|
|---|
| 133 |
|
|---|
| 134 | On Qt/Embedded, a built-in mixing sound server is used, which
|
|---|
| 135 | accesses \c /dev/dsp directly. Only the WAVE format is supported.
|
|---|
| 136 |
|
|---|
| 137 | The availability of sound can be tested with
|
|---|
| 138 | QSound::isAvailable().
|
|---|
| 139 | */
|
|---|
| 140 |
|
|---|
| 141 | /*!
|
|---|
| 142 | \fn static bool QSound::available()
|
|---|
| 143 |
|
|---|
| 144 | Returns TRUE if sound support is available; otherwise returns FALSE.
|
|---|
| 145 | */
|
|---|
| 146 |
|
|---|
| 147 | /*!
|
|---|
| 148 | Plays the sound in a file called \a filename.
|
|---|
| 149 | */
|
|---|
| 150 | void QSound::play(const QString& filename)
|
|---|
| 151 | {
|
|---|
| 152 | server().play(filename);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /*!
|
|---|
| 156 | Constructs a QSound that can quickly play the sound in a file
|
|---|
| 157 | named \a filename.
|
|---|
| 158 |
|
|---|
| 159 | This may use more memory than the static \c play function.
|
|---|
| 160 |
|
|---|
| 161 | The \a parent and \a name arguments (default 0) are passed on to
|
|---|
| 162 | the QObject constructor.
|
|---|
| 163 | */
|
|---|
| 164 | QSound::QSound(const QString& filename, QObject* parent, const char* name) :
|
|---|
| 165 | QObject(parent,name),
|
|---|
| 166 | d(new QSoundData(filename))
|
|---|
| 167 | {
|
|---|
| 168 | server().init(this);
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /*!
|
|---|
| 172 | Destroys the sound object.
|
|---|
| 173 | */
|
|---|
| 174 | QSound::~QSound()
|
|---|
| 175 | {
|
|---|
| 176 | if ( !isFinished() )
|
|---|
| 177 | stop();
|
|---|
| 178 | delete d;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /*!
|
|---|
| 182 | Returns TRUE if the sound has finished playing; otherwise returns FALSE.
|
|---|
| 183 | */
|
|---|
| 184 | bool QSound::isFinished() const
|
|---|
| 185 | {
|
|---|
| 186 | return d->looprem == 0;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | /*!
|
|---|
| 190 | \overload
|
|---|
| 191 |
|
|---|
| 192 | Starts the sound playing. The function returns immediately.
|
|---|
| 193 | Depending on the platform audio facilities, other sounds may stop
|
|---|
| 194 | or may be mixed with the new sound.
|
|---|
| 195 |
|
|---|
| 196 | The sound can be played again at any time, possibly mixing or
|
|---|
| 197 | replacing previous plays of the sound.
|
|---|
| 198 | */
|
|---|
| 199 | void QSound::play()
|
|---|
| 200 | {
|
|---|
| 201 | d->looprem = d->looptotal;
|
|---|
| 202 | server().play(this);
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /*!
|
|---|
| 206 | Returns the number of times the sound will play.
|
|---|
| 207 | */
|
|---|
| 208 | int QSound::loops() const
|
|---|
| 209 | {
|
|---|
| 210 | return d->looptotal;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | /*!
|
|---|
| 214 | Returns the number of times the sound will loop. This value
|
|---|
| 215 | decreases each time the sound loops.
|
|---|
| 216 | */
|
|---|
| 217 | int QSound::loopsRemaining() const
|
|---|
| 218 | {
|
|---|
| 219 | return d->looprem;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | /*!
|
|---|
| 223 | Sets the sound to repeat \a l times when it is played. Passing the
|
|---|
| 224 | value -1 will cause the sound to loop indefinitely.
|
|---|
| 225 |
|
|---|
| 226 | \sa loops()
|
|---|
| 227 | */
|
|---|
| 228 | void QSound::setLoops(int l)
|
|---|
| 229 | {
|
|---|
| 230 | d->looptotal = l;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | /*!
|
|---|
| 234 | Returns the filename associated with the sound.
|
|---|
| 235 | */
|
|---|
| 236 | QString QSound::fileName() const
|
|---|
| 237 | {
|
|---|
| 238 | return d->filename;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | /*!
|
|---|
| 242 | Stops the sound playing.
|
|---|
| 243 |
|
|---|
| 244 | On Windows the current loop will finish if a sound is played
|
|---|
| 245 | in a loop.
|
|---|
| 246 |
|
|---|
| 247 | \sa play()
|
|---|
| 248 | */
|
|---|
| 249 | void QSound::stop()
|
|---|
| 250 | {
|
|---|
| 251 | server().stop(this);
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 | /*!
|
|---|
| 256 | Returns TRUE if sound facilities exist on the platform; otherwise
|
|---|
| 257 | returns FALSE. An application may choose either to notify the user
|
|---|
| 258 | if sound is crucial to the application or to operate silently
|
|---|
| 259 | without bothering the user.
|
|---|
| 260 |
|
|---|
| 261 | If no sound is available, all QSound operations work silently and
|
|---|
| 262 | quickly.
|
|---|
| 263 | */
|
|---|
| 264 | bool QSound::isAvailable()
|
|---|
| 265 | {
|
|---|
| 266 | return server().okay();
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | /*!
|
|---|
| 270 | Sets the internal bucket record of sound \a s to \a b, deleting
|
|---|
| 271 | any previous setting.
|
|---|
| 272 | */
|
|---|
| 273 | void QAuServer::setBucket(QSound* s, QAuBucket* b)
|
|---|
| 274 | {
|
|---|
| 275 | delete s->d->bucket;
|
|---|
| 276 | s->d->bucket = b;
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | /*!
|
|---|
| 280 | Returns the internal bucket record of sound \a s.
|
|---|
| 281 | */
|
|---|
| 282 | QAuBucket* QAuServer::bucket(QSound* s)
|
|---|
| 283 | {
|
|---|
| 284 | return s->d->bucket;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | /*!
|
|---|
| 288 | Decrements the QSound::loopRemaining() value for sound \a s,
|
|---|
| 289 | returning the result.
|
|---|
| 290 | */
|
|---|
| 291 | int QAuServer::decLoop(QSound* s)
|
|---|
| 292 | {
|
|---|
| 293 | if ( s->d->looprem > 0 )
|
|---|
| 294 | --s->d->looprem;
|
|---|
| 295 | return s->d->looprem;
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | /*!
|
|---|
| 299 | Initializes the sound. The default implementation does nothing.
|
|---|
| 300 | */
|
|---|
| 301 | void QAuServer::init(QSound*)
|
|---|
| 302 | {
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | QAuBucket::~QAuBucket()
|
|---|
| 306 | {
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | #endif // QT_NO_SOUND
|
|---|