[31] | 1 | /****************************************************************************
|
---|
[45] | 2 | ** $Id:qsound.cpp 35 2005-12-11 15:03:18Z dmik $
|
---|
[31] | 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) :
|
---|
[45] | 82 | filename(fname), server(0), bucket(0), looprem(0), looptotal(1)
|
---|
[31] | 83 | {
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | ~QSoundData()
|
---|
| 87 | {
|
---|
| 88 | delete bucket;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | QString filename;
|
---|
[45] | 92 | QAuServer* server;
|
---|
[31] | 93 | QAuBucket* bucket;
|
---|
| 94 | int looprem;
|
---|
| 95 | int looptotal;
|
---|
| 96 | };
|
---|
| 97 |
|
---|
| 98 | /*!
|
---|
| 99 | \class QSound qsound.h
|
---|
| 100 | \brief The QSound class provides access to the platform audio facilities.
|
---|
| 101 |
|
---|
| 102 | \ingroup multimedia
|
---|
| 103 | \mainclass
|
---|
| 104 |
|
---|
| 105 | Qt provides the most commonly required audio operation in GUI
|
---|
| 106 | applications: asynchronously playing a sound file. This is most
|
---|
| 107 | easily accomplished with a single call:
|
---|
| 108 | \code
|
---|
| 109 | QSound::play("mysounds/bells.wav");
|
---|
| 110 | \endcode
|
---|
| 111 |
|
---|
| 112 | A second API is provided in which a QSound object is created from
|
---|
| 113 | a sound file and is played later:
|
---|
| 114 | \code
|
---|
| 115 | QSound bells("mysounds/bells.wav");
|
---|
| 116 |
|
---|
| 117 | bells.play();
|
---|
| 118 | \endcode
|
---|
| 119 |
|
---|
| 120 | Sounds played using the second model may use more memory but play
|
---|
| 121 | more immediately than sounds played using the first model,
|
---|
| 122 | depending on the underlying platform audio facilities.
|
---|
| 123 |
|
---|
| 124 | On Microsoft Windows the underlying multimedia system is used;
|
---|
| 125 | only WAVE format sound files are supported.
|
---|
| 126 |
|
---|
[45] | 127 | On IBM OS/2 the underlying MMPM system is used;
|
---|
| 128 | all MMPM audio formats are supported.
|
---|
| 129 |
|
---|
[31] | 130 | On X11 the \link ftp://ftp.x.org/contrib/audio/nas/ Network Audio
|
---|
| 131 | System\endlink is used if available, otherwise all operations work
|
---|
| 132 | silently. NAS supports WAVE and AU files.
|
---|
| 133 |
|
---|
| 134 | On Macintosh, ironically, we use QT (\link
|
---|
| 135 | http://quicktime.apple.com QuickTime\endlink) for sound, this
|
---|
| 136 | means all QuickTime formats are supported by Qt/Mac.
|
---|
| 137 |
|
---|
| 138 | On Qt/Embedded, a built-in mixing sound server is used, which
|
---|
| 139 | accesses \c /dev/dsp directly. Only the WAVE format is supported.
|
---|
| 140 |
|
---|
| 141 | The availability of sound can be tested with
|
---|
| 142 | QSound::isAvailable().
|
---|
| 143 | */
|
---|
| 144 |
|
---|
| 145 | /*!
|
---|
| 146 | \fn static bool QSound::available()
|
---|
| 147 |
|
---|
| 148 | Returns TRUE if sound support is available; otherwise returns FALSE.
|
---|
| 149 | */
|
---|
| 150 |
|
---|
| 151 | /*!
|
---|
| 152 | Plays the sound in a file called \a filename.
|
---|
| 153 | */
|
---|
| 154 | void QSound::play(const QString& filename)
|
---|
| 155 | {
|
---|
| 156 | server().play(filename);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | /*!
|
---|
| 160 | Constructs a QSound that can quickly play the sound in a file
|
---|
| 161 | named \a filename.
|
---|
| 162 |
|
---|
| 163 | This may use more memory than the static \c play function.
|
---|
| 164 |
|
---|
| 165 | The \a parent and \a name arguments (default 0) are passed on to
|
---|
| 166 | the QObject constructor.
|
---|
| 167 | */
|
---|
| 168 | QSound::QSound(const QString& filename, QObject* parent, const char* name) :
|
---|
| 169 | QObject(parent,name),
|
---|
| 170 | d(new QSoundData(filename))
|
---|
| 171 | {
|
---|
[45] | 172 | d->server = &server();
|
---|
| 173 | d->server->init(this);
|
---|
[31] | 174 | }
|
---|
| 175 |
|
---|
| 176 | /*!
|
---|
| 177 | Destroys the sound object.
|
---|
| 178 | */
|
---|
| 179 | QSound::~QSound()
|
---|
| 180 | {
|
---|
| 181 | if ( !isFinished() )
|
---|
| 182 | stop();
|
---|
| 183 | delete d;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /*!
|
---|
| 187 | Returns TRUE if the sound has finished playing; otherwise returns FALSE.
|
---|
| 188 | */
|
---|
| 189 | bool QSound::isFinished() const
|
---|
| 190 | {
|
---|
| 191 | return d->looprem == 0;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /*!
|
---|
| 195 | \overload
|
---|
| 196 |
|
---|
| 197 | Starts the sound playing. The function returns immediately.
|
---|
| 198 | Depending on the platform audio facilities, other sounds may stop
|
---|
| 199 | or may be mixed with the new sound.
|
---|
| 200 |
|
---|
| 201 | The sound can be played again at any time, possibly mixing or
|
---|
| 202 | replacing previous plays of the sound.
|
---|
| 203 | */
|
---|
| 204 | void QSound::play()
|
---|
| 205 | {
|
---|
| 206 | d->looprem = d->looptotal;
|
---|
| 207 | server().play(this);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | /*!
|
---|
| 211 | Returns the number of times the sound will play.
|
---|
| 212 | */
|
---|
| 213 | int QSound::loops() const
|
---|
| 214 | {
|
---|
| 215 | return d->looptotal;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | /*!
|
---|
| 219 | Returns the number of times the sound will loop. This value
|
---|
| 220 | decreases each time the sound loops.
|
---|
| 221 | */
|
---|
| 222 | int QSound::loopsRemaining() const
|
---|
| 223 | {
|
---|
| 224 | return d->looprem;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | /*!
|
---|
| 228 | Sets the sound to repeat \a l times when it is played. Passing the
|
---|
| 229 | value -1 will cause the sound to loop indefinitely.
|
---|
| 230 |
|
---|
| 231 | \sa loops()
|
---|
| 232 | */
|
---|
| 233 | void QSound::setLoops(int l)
|
---|
| 234 | {
|
---|
| 235 | d->looptotal = l;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | /*!
|
---|
| 239 | Returns the filename associated with the sound.
|
---|
| 240 | */
|
---|
| 241 | QString QSound::fileName() const
|
---|
| 242 | {
|
---|
| 243 | return d->filename;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | /*!
|
---|
| 247 | Stops the sound playing.
|
---|
| 248 |
|
---|
| 249 | On Windows the current loop will finish if a sound is played
|
---|
| 250 | in a loop.
|
---|
| 251 |
|
---|
| 252 | \sa play()
|
---|
| 253 | */
|
---|
| 254 | void QSound::stop()
|
---|
| 255 | {
|
---|
| 256 | server().stop(this);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 |
|
---|
| 260 | /*!
|
---|
| 261 | Returns TRUE if sound facilities exist on the platform; otherwise
|
---|
| 262 | returns FALSE. An application may choose either to notify the user
|
---|
| 263 | if sound is crucial to the application or to operate silently
|
---|
| 264 | without bothering the user.
|
---|
| 265 |
|
---|
| 266 | If no sound is available, all QSound operations work silently and
|
---|
| 267 | quickly.
|
---|
| 268 | */
|
---|
| 269 | bool QSound::isAvailable()
|
---|
| 270 | {
|
---|
| 271 | return server().okay();
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | /*!
|
---|
[45] | 275 | Returns true if sound \a s was initialized using this server instance
|
---|
| 276 | and false otherwise.
|
---|
| 277 | */
|
---|
| 278 | bool QAuServer::isRelevant(QSound* s)
|
---|
| 279 | {
|
---|
| 280 | return s->d->server == this;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | /*!
|
---|
[31] | 284 | Sets the internal bucket record of sound \a s to \a b, deleting
|
---|
| 285 | any previous setting.
|
---|
| 286 | */
|
---|
| 287 | void QAuServer::setBucket(QSound* s, QAuBucket* b)
|
---|
| 288 | {
|
---|
| 289 | delete s->d->bucket;
|
---|
| 290 | s->d->bucket = b;
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | /*!
|
---|
| 294 | Returns the internal bucket record of sound \a s.
|
---|
| 295 | */
|
---|
| 296 | QAuBucket* QAuServer::bucket(QSound* s)
|
---|
| 297 | {
|
---|
| 298 | return s->d->bucket;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | /*!
|
---|
| 302 | Decrements the QSound::loopRemaining() value for sound \a s,
|
---|
| 303 | returning the result.
|
---|
| 304 | */
|
---|
| 305 | int QAuServer::decLoop(QSound* s)
|
---|
| 306 | {
|
---|
| 307 | if ( s->d->looprem > 0 )
|
---|
| 308 | --s->d->looprem;
|
---|
| 309 | return s->d->looprem;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | /*!
|
---|
| 313 | Initializes the sound. The default implementation does nothing.
|
---|
| 314 | */
|
---|
| 315 | void QAuServer::init(QSound*)
|
---|
| 316 | {
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | QAuBucket::~QAuBucket()
|
---|
| 320 | {
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | #endif // QT_NO_SOUND
|
---|