src/multimedia/audio/qaudiooutput.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    71     start() with a QIODevice. QAudioOutput will then fetch the data it
    71     start() with a QIODevice. QAudioOutput will then fetch the data it
    72     needs from the io device. So playing back an audio file is as
    72     needs from the io device. So playing back an audio file is as
    73     simple as:
    73     simple as:
    74 
    74 
    75     \code
    75     \code
    76       QFile inputFile;
    76       QFile inputFile;     // class member.
       
    77       QAudioOutput* audio; // class member.
       
    78     \endcode
       
    79 
       
    80     \code
    77       inputFile.setFileName("/tmp/test.raw");
    81       inputFile.setFileName("/tmp/test.raw");
    78       inputFile.open(QIODevice::ReadOnly);
    82       inputFile.open(QIODevice::ReadOnly);
    79 
    83 
    80       QAudioFormat format;
    84       QAudioFormat format;
    81       // Set up the format, eg.
    85       // Set up the format, eg.
    84       format.setSampleSize(8);
    88       format.setSampleSize(8);
    85       format.setCodec("audio/pcm");
    89       format.setCodec("audio/pcm");
    86       format.setByteOrder(QAudioFormat::LittleEndian);
    90       format.setByteOrder(QAudioFormat::LittleEndian);
    87       format.setSampleType(QAudioFormat::UnSignedInt);
    91       format.setSampleType(QAudioFormat::UnSignedInt);
    88 
    92 
    89       QAudioOutput *audio = new QAudioOutput(format, this);
    93       QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
       
    94       if (!info.isFormatSupported(format)) {
       
    95           qWarning()<<"raw audio format not supported by backend, cannot play audio.";
       
    96           return;
       
    97       }
       
    98 
       
    99       audio = new QAudioOutput(format, this);
    90       connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(finishedPlaying(QAudio::State)));
   100       connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(finishedPlaying(QAudio::State)));
    91       audio->start(inputFile);
   101       audio->start(inputFile);
    92 
   102 
    93     \endcode
   103     \endcode
    94 
   104 
   102       void finishedPlaying(QAudio::State state)
   112       void finishedPlaying(QAudio::State state)
   103       {
   113       {
   104         if(state == QAudio::IdleState) {
   114         if(state == QAudio::IdleState) {
   105           audio->stop();
   115           audio->stop();
   106           inputFile.close();
   116           inputFile.close();
       
   117           delete audio;
   107         }
   118         }
   108       }
   119       }
   109     \endcode
   120     \endcode
   110 
   121 
   111     At any given time, the QAudioOutput will be in one of four states:
   122     At any given time, the QAudioOutput will be in one of four states:
   123     relative to the position in the stream, i.e., if the QAudioOutput
   134     relative to the position in the stream, i.e., if the QAudioOutput
   124     is in the SuspendedState or the IdleState, the notify() signal is
   135     is in the SuspendedState or the IdleState, the notify() signal is
   125     not emitted. A typical use-case would be to update a
   136     not emitted. A typical use-case would be to update a
   126     \l{QSlider}{slider} that allows seeking in the stream.
   137     \l{QSlider}{slider} that allows seeking in the stream.
   127     If you want the time since playback started regardless of which
   138     If you want the time since playback started regardless of which
   128     states the audio output has been in, clock() is the function for you.
   139     states the audio output has been in, elapsedUSecs() is the function for you.
   129 
   140 
   130     If an error occurs, you can fetch the \l{QAudio::Error}{error
   141     If an error occurs, you can fetch the \l{QAudio::Error}{error
   131     type} with the error() function. Please see the QAudio::Error enum
   142     type} with the error() function. Please see the QAudio::Error enum
   132     for a description of the possible errors that are reported.  When
   143     for a description of the possible errors that are reported.  When
   133     an error is encountered, the state changes to QAudio::StopState.
   144     an error is encountered, the state changes to QAudio::StoppedState.
   134     You can check for errors by connecting to the stateChanged()
   145     You can check for errors by connecting to the stateChanged()
   135     signal:
   146     signal:
   136 
   147 
   137     \snippet doc/src/snippets/audio/main.cpp 3
   148     \snippet doc/src/snippets/audio/main.cpp 3
   138 
   149 
   186     return d->format();
   197     return d->format();
   187 }
   198 }
   188 
   199 
   189 /*!
   200 /*!
   190     Uses the \a device as the QIODevice to transfer data.
   201     Uses the \a device as the QIODevice to transfer data.
   191     If \a device is null then the class creates an internal QIODevice.
       
   192     Returns a pointer to the QIODevice being used to handle the data
       
   193     transfer. This QIODevice can be used to write() audio data
       
   194     directly.
       
   195     Passing a QIODevice allows the data to be transfered without any extra code.
   202     Passing a QIODevice allows the data to be transfered without any extra code.
   196     All that is required is to open the QIODevice.
   203     All that is required is to open the QIODevice.
   197 
   204 
   198     \sa QIODevice
   205     \sa QIODevice
   199 */
   206 */
   200 
   207 
   201 QIODevice* QAudioOutput::start(QIODevice* device)
   208 void QAudioOutput::start(QIODevice* device)
   202 {
   209 {
   203     /*
   210     /*
   204     PULL MODE (valid QIODevice)
   211     -If currently not StoppedState, stop.
   205     -If currently not StopState, stop.
       
   206     -If previous start was push mode, delete internal QIODevice.
   212     -If previous start was push mode, delete internal QIODevice.
   207     -open audio output.
   213     -open audio output.
   208     -If ok, NoError and ActiveState, else OpenError and StopState
   214     -If ok, NoError and ActiveState, else OpenError and StoppedState
   209     -emit stateChanged()
   215     -emit stateChanged()
   210     -return device
   216     */
   211 
   217     d->start(device);
   212     PUSH MODE (device = 0)
   218 }
   213     -If currently not StopState, stop.
   219 
       
   220 /*!
       
   221     Returns a pointer to the QIODevice being used to handle the data
       
   222     transfer. This QIODevice can be used to write() audio data directly.
       
   223 
       
   224     \sa QIODevice
       
   225 */
       
   226 
       
   227 QIODevice* QAudioOutput::start()
       
   228 {
       
   229     /*
       
   230     -If currently not StoppedState, stop.
   214     -If no internal QIODevice, create one.
   231     -If no internal QIODevice, create one.
   215     -open audio output.
   232     -open audio output.
   216     -If ok, NoError and IdleState, else OpenError and StopState
   233     -If ok, NoError and IdleState, else OpenError and StoppedState
   217     -emit stateChanged()
   234     -emit stateChanged()
   218     -return internal QIODevice
   235     -return internal QIODevice
   219     */
   236     */
   220     return d->start(device);
   237     return d->start(0);
   221 }
   238 }
   222 
   239 
   223 /*!
   240 /*!
   224     Stops the audio output.
   241     Stops the audio output.
   225 */
   242 */
   226 
   243 
   227 void QAudioOutput::stop()
   244 void QAudioOutput::stop()
   228 {
   245 {
   229     /*
   246     /*
   230     -If StopState, return
   247     -If StoppedState, return
   231     -set to StopState
   248     -set to StoppedState
   232     -detach from audio device
   249     -detach from audio device
   233     -emit stateChanged()
   250     -emit stateChanged()
   234     */
   251     */
   235     d->stop();
   252     d->stop();
   236 }
   253 }
   255 void QAudioOutput::suspend()
   272 void QAudioOutput::suspend()
   256 {
   273 {
   257     /*
   274     /*
   258     -If not ActiveState|IdleState, return
   275     -If not ActiveState|IdleState, return
   259     -stop processing audio, saving all buffered audio data
   276     -stop processing audio, saving all buffered audio data
   260     -set NoError and SuspendState
   277     -set NoError and SuspendedState
   261     -emit stateChanged()
   278     -emit stateChanged()
   262     */
   279     */
   263     d->suspend();
   280     d->suspend();
   264 }
   281 }
   265 
   282 
   268 */
   285 */
   269 
   286 
   270 void QAudioOutput::resume()
   287 void QAudioOutput::resume()
   271 {
   288 {
   272     /*
   289     /*
   273     -If SuspendState, return
   290     -If SuspendedState, return
   274     -resume audio
   291     -resume audio
   275     -(PULL MODE): set ActiveState, NoError
   292     -(PULL MODE): set ActiveState, NoError
   276     -(PUSH MODE): set IdleState, NoError
   293     -(PUSH MODE): set IdleState, NoError
   277     -kick start audio if needed
   294     -kick start audio if needed
   278     -emit stateChanged()
   295     -emit stateChanged()
   356 /*!
   373 /*!
   357     Returns the amount of audio data processed since start()
   374     Returns the amount of audio data processed since start()
   358     was called in microseconds.
   375     was called in microseconds.
   359 */
   376 */
   360 
   377 
   361 qint64 QAudioOutput::totalTime() const
   378 qint64 QAudioOutput::processedUSecs() const
   362 {
   379 {
   363     return d->totalTime();
   380     return d->processedUSecs();
   364 }
   381 }
   365 
   382 
   366 /*!
   383 /*!
   367     Returns the microseconds since start() was called, including time in Idle and
   384     Returns the microseconds since start() was called, including time in Idle and
   368     Suspend states.
   385     Suspend states.
   369 */
   386 */
   370 
   387 
   371 qint64 QAudioOutput::clock() const
   388 qint64 QAudioOutput::elapsedUSecs() const
   372 {
   389 {
   373     return d->clock();
   390     return d->elapsedUSecs();
   374 }
   391 }
   375 
   392 
   376 /*!
   393 /*!
   377     Returns the error state.
   394     Returns the error state.
   378 */
   395 */