| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2010 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 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 qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 |  | 
|---|
| 43 | #include <QtMultimedia/qaudio.h> | 
|---|
| 44 | #include <QtMultimedia/qaudiodeviceinfo.h> | 
|---|
| 45 | #include <QtMultimedia/qaudioengine.h> | 
|---|
| 46 | #include <QtMultimedia/qaudioinput.h> | 
|---|
| 47 |  | 
|---|
| 48 | #include "qaudiodevicefactory_p.h" | 
|---|
| 49 |  | 
|---|
| 50 | QT_BEGIN_NAMESPACE | 
|---|
| 51 |  | 
|---|
| 52 | /*! | 
|---|
| 53 | \class QAudioInput | 
|---|
| 54 | \brief The QAudioInput class provides an interface for receiving audio data from an audio input device. | 
|---|
| 55 |  | 
|---|
| 56 | \inmodule QtMultimedia | 
|---|
| 57 | \ingroup  multimedia | 
|---|
| 58 | \since 4.6 | 
|---|
| 59 |  | 
|---|
| 60 | You can construct an audio input with the system's | 
|---|
| 61 | \l{QAudioDeviceInfo::defaultInputDevice()}{default audio input | 
|---|
| 62 | device}. It is also possible to create QAudioInput with a | 
|---|
| 63 | specific QAudioDeviceInfo. When you create the audio input, you | 
|---|
| 64 | should also send in the QAudioFormat to be used for the recording | 
|---|
| 65 | (see the QAudioFormat class description for details). | 
|---|
| 66 |  | 
|---|
| 67 | To record to a file: | 
|---|
| 68 |  | 
|---|
| 69 | QAudioInput lets you record audio with an audio input device. The | 
|---|
| 70 | default constructor of this class will use the systems default | 
|---|
| 71 | audio device, but you can also specify a QAudioDeviceInfo for a | 
|---|
| 72 | specific device. You also need to pass in the QAudioFormat in | 
|---|
| 73 | which you wish to record. | 
|---|
| 74 |  | 
|---|
| 75 | Starting up the QAudioInput is simply a matter of calling start() | 
|---|
| 76 | with a QIODevice opened for writing. For instance, to record to a | 
|---|
| 77 | file, you can: | 
|---|
| 78 |  | 
|---|
| 79 | \code | 
|---|
| 80 | QFile outputFile;   // class member. | 
|---|
| 81 | QAudioInput* audio; // class member. | 
|---|
| 82 | \endcode | 
|---|
| 83 |  | 
|---|
| 84 | \code | 
|---|
| 85 | { | 
|---|
| 86 | outputFile.setFileName("/tmp/test.raw"); | 
|---|
| 87 | outputFile.open( QIODevice::WriteOnly | QIODevice::Truncate ); | 
|---|
| 88 |  | 
|---|
| 89 | QAudioFormat format; | 
|---|
| 90 | // set up the format you want, eg. | 
|---|
| 91 | format.setFrequency(8000); | 
|---|
| 92 | format.setChannels(1); | 
|---|
| 93 | format.setSampleSize(8); | 
|---|
| 94 | format.setCodec("audio/pcm"); | 
|---|
| 95 | format.setByteOrder(QAudioFormat::LittleEndian); | 
|---|
| 96 | format.setSampleType(QAudioFormat::UnSignedInt); | 
|---|
| 97 |  | 
|---|
| 98 | if (QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice()); | 
|---|
| 99 | if (!info.isFormatSupported(format)) { | 
|---|
| 100 | qWarning()<<"default format not supported try to use nearest"; | 
|---|
| 101 | format = info.nearestFormat(format); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | audio = new QAudioInput(format, this); | 
|---|
| 105 | QTimer::singleShot(3000, this, SLOT(stopRecording())); | 
|---|
| 106 | audio->start(&outputFile); | 
|---|
| 107 | // Records audio for 3000ms | 
|---|
| 108 | } | 
|---|
| 109 | \endcode | 
|---|
| 110 |  | 
|---|
| 111 | This will start recording if the format specified is supported by | 
|---|
| 112 | the input device (you can check this with | 
|---|
| 113 | QAudioDeviceInfo::isFormatSupported(). In case there are any | 
|---|
| 114 | snags, use the error() function to check what went wrong. We stop | 
|---|
| 115 | recording in the \c stopRecording() slot. | 
|---|
| 116 |  | 
|---|
| 117 | \code | 
|---|
| 118 | void stopRecording() | 
|---|
| 119 | { | 
|---|
| 120 | audio->stop(); | 
|---|
| 121 | outputFile->close(); | 
|---|
| 122 | delete audio; | 
|---|
| 123 | } | 
|---|
| 124 | \endcode | 
|---|
| 125 |  | 
|---|
| 126 | At any point in time, QAudioInput will be in one of four states: | 
|---|
| 127 | active, suspended, stopped, or idle. These states are specified by | 
|---|
| 128 | the QAudio::State enum. You can request a state change directly through | 
|---|
| 129 | suspend(), resume(), stop(), reset(), and start(). The current | 
|---|
| 130 | state is reported by state(). QAudioOutput will also signal you | 
|---|
| 131 | when the state changes (stateChanged()). | 
|---|
| 132 |  | 
|---|
| 133 | QAudioInput provides several ways of measuring the time that has | 
|---|
| 134 | passed since the start() of the recording. The \c processedUSecs() | 
|---|
| 135 | function returns the length of the stream in microseconds written, | 
|---|
| 136 | i.e., it leaves out the times the audio input was suspended or idle. | 
|---|
| 137 | The elapsedUSecs() function returns the time elapsed since start() was called regardless of | 
|---|
| 138 | which states the QAudioInput has been in. | 
|---|
| 139 |  | 
|---|
| 140 | If an error should occur, you can fetch its reason with error(). | 
|---|
| 141 | The possible error reasons are described by the QAudio::Error | 
|---|
| 142 | enum. The QAudioInput will enter the \l{QAudio::}{StoppedState} when | 
|---|
| 143 | an error is encountered.  Connect to the stateChanged() signal to | 
|---|
| 144 | handle the error: | 
|---|
| 145 |  | 
|---|
| 146 | \snippet doc/src/snippets/audio/main.cpp 0 | 
|---|
| 147 |  | 
|---|
| 148 | \sa QAudioOutput, QAudioDeviceInfo | 
|---|
| 149 | */ | 
|---|
| 150 |  | 
|---|
| 151 | /*! | 
|---|
| 152 | Construct a new audio input and attach it to \a parent. | 
|---|
| 153 | The default audio input device is used with the output | 
|---|
| 154 | \a format parameters. | 
|---|
| 155 | */ | 
|---|
| 156 |  | 
|---|
| 157 | QAudioInput::QAudioInput(const QAudioFormat &format, QObject *parent): | 
|---|
| 158 | QObject(parent) | 
|---|
| 159 | { | 
|---|
| 160 | d = QAudioDeviceFactory::createDefaultInputDevice(format); | 
|---|
| 161 | connect(d, SIGNAL(notify()), SIGNAL(notify())); | 
|---|
| 162 | connect(d, SIGNAL(stateChanged(QAudio::State)), SIGNAL(stateChanged(QAudio::State))); | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | /*! | 
|---|
| 166 | Construct a new audio input and attach it to \a parent. | 
|---|
| 167 | The device referenced by \a audioDevice is used with the input | 
|---|
| 168 | \a format parameters. | 
|---|
| 169 | */ | 
|---|
| 170 |  | 
|---|
| 171 | QAudioInput::QAudioInput(const QAudioDeviceInfo &audioDevice, const QAudioFormat &format, QObject *parent): | 
|---|
| 172 | QObject(parent) | 
|---|
| 173 | { | 
|---|
| 174 | d = QAudioDeviceFactory::createInputDevice(audioDevice, format); | 
|---|
| 175 | connect(d, SIGNAL(notify()), SIGNAL(notify())); | 
|---|
| 176 | connect(d, SIGNAL(stateChanged(QAudio::State)), SIGNAL(stateChanged(QAudio::State))); | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | /*! | 
|---|
| 180 | Destroy this audio input. | 
|---|
| 181 | */ | 
|---|
| 182 |  | 
|---|
| 183 | QAudioInput::~QAudioInput() | 
|---|
| 184 | { | 
|---|
| 185 | delete d; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | /*! | 
|---|
| 189 | Uses the \a device as the QIODevice to transfer data. | 
|---|
| 190 | Passing a QIODevice allows the data to be transfered without any extra code. | 
|---|
| 191 | All that is required is to open the QIODevice. | 
|---|
| 192 |  | 
|---|
| 193 | \sa QIODevice | 
|---|
| 194 | */ | 
|---|
| 195 |  | 
|---|
| 196 | void QAudioInput::start(QIODevice* device) | 
|---|
| 197 | { | 
|---|
| 198 | /* | 
|---|
| 199 | -If currently not StoppedState, stop | 
|---|
| 200 | -If previous start was push mode, delete internal QIODevice. | 
|---|
| 201 | -open audio input. | 
|---|
| 202 | If ok, NoError and ActiveState, else OpenError and StoppedState. | 
|---|
| 203 | -emit stateChanged() | 
|---|
| 204 | */ | 
|---|
| 205 | d->start(device); | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | /*! | 
|---|
| 209 | Returns a pointer to the QIODevice being used to handle the data | 
|---|
| 210 | transfer. This QIODevice can be used to read() audio data | 
|---|
| 211 | directly. | 
|---|
| 212 |  | 
|---|
| 213 | \sa QIODevice | 
|---|
| 214 | */ | 
|---|
| 215 |  | 
|---|
| 216 | QIODevice* QAudioInput::start() | 
|---|
| 217 | { | 
|---|
| 218 | /* | 
|---|
| 219 | -If currently not StoppedState, stop | 
|---|
| 220 | -If no internal QIODevice, create one. | 
|---|
| 221 | -open audio input. | 
|---|
| 222 | -If ok, NoError and IdleState, else OpenError and StoppedState | 
|---|
| 223 | -emit stateChanged() | 
|---|
| 224 | -return internal QIODevice | 
|---|
| 225 | */ | 
|---|
| 226 | return d->start(0); | 
|---|
| 227 | } | 
|---|
| 228 |  | 
|---|
| 229 | /*! | 
|---|
| 230 | Returns the QAudioFormat being used. | 
|---|
| 231 | */ | 
|---|
| 232 |  | 
|---|
| 233 | QAudioFormat QAudioInput::format() const | 
|---|
| 234 | { | 
|---|
| 235 | return d->format(); | 
|---|
| 236 | } | 
|---|
| 237 |  | 
|---|
| 238 | /*! | 
|---|
| 239 | Stops the audio input. | 
|---|
| 240 | */ | 
|---|
| 241 |  | 
|---|
| 242 | void QAudioInput::stop() | 
|---|
| 243 | { | 
|---|
| 244 | /* | 
|---|
| 245 | -If StoppedState, return | 
|---|
| 246 | -set to StoppedState | 
|---|
| 247 | -detach from audio device | 
|---|
| 248 | -emit stateChanged() | 
|---|
| 249 | */ | 
|---|
| 250 | d->stop(); | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 | /*! | 
|---|
| 254 | Drops all audio data in the buffers, resets buffers to zero. | 
|---|
| 255 | */ | 
|---|
| 256 |  | 
|---|
| 257 | void QAudioInput::reset() | 
|---|
| 258 | { | 
|---|
| 259 | /* | 
|---|
| 260 | -drop all buffered audio, set buffers to zero. | 
|---|
| 261 | -call stop() | 
|---|
| 262 | */ | 
|---|
| 263 | d->reset(); | 
|---|
| 264 | } | 
|---|
| 265 |  | 
|---|
| 266 | /*! | 
|---|
| 267 | Stops processing audio data, preserving buffered audio data. | 
|---|
| 268 | */ | 
|---|
| 269 |  | 
|---|
| 270 | void QAudioInput::suspend() | 
|---|
| 271 | { | 
|---|
| 272 | /* | 
|---|
| 273 | -If not ActiveState|IdleState, return | 
|---|
| 274 | -stop processing audio, saving all buffered audio data | 
|---|
| 275 | -set NoError and SuspendedState | 
|---|
| 276 | -emit stateChanged() | 
|---|
| 277 | */ | 
|---|
| 278 | d->suspend(); | 
|---|
| 279 | } | 
|---|
| 280 |  | 
|---|
| 281 | /*! | 
|---|
| 282 | Resumes processing audio data after a suspend(). | 
|---|
| 283 | */ | 
|---|
| 284 |  | 
|---|
| 285 | void QAudioInput::resume() | 
|---|
| 286 | { | 
|---|
| 287 | /* | 
|---|
| 288 | -If SuspendedState, return | 
|---|
| 289 | -resume audio | 
|---|
| 290 | -(PULL MODE): set ActiveState, NoError | 
|---|
| 291 | -(PUSH MODE): set IdleState, NoError | 
|---|
| 292 | -kick start audio if needed | 
|---|
| 293 | -emit stateChanged() | 
|---|
| 294 | */ | 
|---|
| 295 | d->resume(); | 
|---|
| 296 | } | 
|---|
| 297 |  | 
|---|
| 298 | /*! | 
|---|
| 299 | Sets the audio buffer size to \a value milliseconds. | 
|---|
| 300 |  | 
|---|
| 301 | Note: This function can be called anytime before start(), calls to this | 
|---|
| 302 | are ignored after start(). It should not be assumed that the buffer size | 
|---|
| 303 | set is the actual buffer size used, calling bufferSize() anytime after start() | 
|---|
| 304 | will return the actual buffer size being used. | 
|---|
| 305 |  | 
|---|
| 306 | */ | 
|---|
| 307 |  | 
|---|
| 308 | void QAudioInput::setBufferSize(int value) | 
|---|
| 309 | { | 
|---|
| 310 | d->setBufferSize(value); | 
|---|
| 311 | } | 
|---|
| 312 |  | 
|---|
| 313 | /*! | 
|---|
| 314 | Returns the audio buffer size in milliseconds. | 
|---|
| 315 |  | 
|---|
| 316 | If called before start(), returns platform default value. | 
|---|
| 317 | If called before start() but setBufferSize() was called prior, returns value set by setBufferSize(). | 
|---|
| 318 | If called after start(), returns the actual buffer size being used. This may not be what was set previously | 
|---|
| 319 | by setBufferSize(). | 
|---|
| 320 |  | 
|---|
| 321 | */ | 
|---|
| 322 |  | 
|---|
| 323 | int QAudioInput::bufferSize() const | 
|---|
| 324 | { | 
|---|
| 325 | return d->bufferSize(); | 
|---|
| 326 | } | 
|---|
| 327 |  | 
|---|
| 328 | /*! | 
|---|
| 329 | Returns the amount of audio data available to read in bytes. | 
|---|
| 330 | */ | 
|---|
| 331 |  | 
|---|
| 332 | int QAudioInput::bytesReady() const | 
|---|
| 333 | { | 
|---|
| 334 | /* | 
|---|
| 335 | -If not ActiveState|IdleState, return 0 | 
|---|
| 336 | -return amount of audio data available to read | 
|---|
| 337 | */ | 
|---|
| 338 | return d->bytesReady(); | 
|---|
| 339 | } | 
|---|
| 340 |  | 
|---|
| 341 | /*! | 
|---|
| 342 | Returns the period size in bytes. | 
|---|
| 343 |  | 
|---|
| 344 | Note: This is the recommended read size in bytes. | 
|---|
| 345 | */ | 
|---|
| 346 |  | 
|---|
| 347 | int QAudioInput::periodSize() const | 
|---|
| 348 | { | 
|---|
| 349 | return d->periodSize(); | 
|---|
| 350 | } | 
|---|
| 351 |  | 
|---|
| 352 | /*! | 
|---|
| 353 | Sets the interval for notify() signal to be emitted. | 
|---|
| 354 | This is based on the \a ms of audio data processed | 
|---|
| 355 | not on actual real-time. The resolution of the timer is platform specific. | 
|---|
| 356 | */ | 
|---|
| 357 |  | 
|---|
| 358 | void QAudioInput::setNotifyInterval(int ms) | 
|---|
| 359 | { | 
|---|
| 360 | d->setNotifyInterval(ms); | 
|---|
| 361 | } | 
|---|
| 362 |  | 
|---|
| 363 | /*! | 
|---|
| 364 | Returns the notify interval in milliseconds. | 
|---|
| 365 | */ | 
|---|
| 366 |  | 
|---|
| 367 | int QAudioInput::notifyInterval() const | 
|---|
| 368 | { | 
|---|
| 369 | return d->notifyInterval(); | 
|---|
| 370 | } | 
|---|
| 371 |  | 
|---|
| 372 | /*! | 
|---|
| 373 | Returns the amount of audio data processed since start() | 
|---|
| 374 | was called in microseconds. | 
|---|
| 375 | */ | 
|---|
| 376 |  | 
|---|
| 377 | qint64 QAudioInput::processedUSecs() const | 
|---|
| 378 | { | 
|---|
| 379 | return d->processedUSecs(); | 
|---|
| 380 | } | 
|---|
| 381 |  | 
|---|
| 382 | /*! | 
|---|
| 383 | Returns the microseconds since start() was called, including time in Idle and | 
|---|
| 384 | Suspend states. | 
|---|
| 385 | */ | 
|---|
| 386 |  | 
|---|
| 387 | qint64 QAudioInput::elapsedUSecs() const | 
|---|
| 388 | { | 
|---|
| 389 | return d->elapsedUSecs(); | 
|---|
| 390 | } | 
|---|
| 391 |  | 
|---|
| 392 | /*! | 
|---|
| 393 | Returns the error state. | 
|---|
| 394 | */ | 
|---|
| 395 |  | 
|---|
| 396 | QAudio::Error QAudioInput::error() const | 
|---|
| 397 | { | 
|---|
| 398 | return d->error(); | 
|---|
| 399 | } | 
|---|
| 400 |  | 
|---|
| 401 | /*! | 
|---|
| 402 | Returns the state of audio processing. | 
|---|
| 403 | */ | 
|---|
| 404 |  | 
|---|
| 405 | QAudio::State QAudioInput::state() const | 
|---|
| 406 | { | 
|---|
| 407 | return d->state(); | 
|---|
| 408 | } | 
|---|
| 409 |  | 
|---|
| 410 | /*! | 
|---|
| 411 | \fn QAudioInput::stateChanged(QAudio::State state) | 
|---|
| 412 | This signal is emitted when the device \a state has changed. | 
|---|
| 413 | */ | 
|---|
| 414 |  | 
|---|
| 415 | /*! | 
|---|
| 416 | \fn QAudioInput::notify() | 
|---|
| 417 | This signal is emitted when x ms of audio data has been processed | 
|---|
| 418 | the interval set by setNotifyInterval(x). | 
|---|
| 419 | */ | 
|---|
| 420 |  | 
|---|
| 421 | QT_END_NAMESPACE | 
|---|
| 422 |  | 
|---|