1
/****************************************************************************
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
7
** This file is part of the QtMultimedia module of the Qt Toolkit.
9
** $QT_BEGIN_LICENSE:LGPL$
10
** No Commercial Usage
11
** This file contains pre-release code and may not be distributed.
12
** You may use this file in accordance with the terms and conditions
13
** contained in the Technology Preview License Agreement accompanying
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.
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.
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
40
****************************************************************************/
43
#include <QtMultimedia/qaudio.h>
44
#include <QtMultimedia/qaudiodeviceinfo.h>
45
#include <QtMultimedia/qaudioengine.h>
46
#include <QtMultimedia/qaudiooutput.h>
48
#include "qaudiodevicefactory_p.h"
55
\brief The QAudioOutput class provides an interface for sending audio data to an audio output device.
57
\inmodule QtMultimedia
61
You can construct an audio output with the system's
62
\l{QAudioDeviceInfo::defaultOutputDevice()}{default audio output
63
device}. It is also possible to create QAudioOutput with a
64
specific QAudioDeviceInfo. When you create the audio output, you
65
should also send in the QAudioFormat to be used for the playback
66
(see the QAudioFormat class description for details).
70
Starting to play an audio stream is simply a matter of calling
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
77
inputFile.setFileName("/tmp/test.raw");
78
inputFile.open(QIODevice::ReadOnly);
81
// Set up the format, eg.
82
format.setFrequency(8000);
83
format.setChannels(1);
84
format.setSampleSize(8);
85
format.setCodec("audio/pcm");
86
format.setByteOrder(QAudioFormat::LittleEndian);
87
format.setSampleType(QAudioFormat::UnSignedInt);
89
QAudioOutput *audio = new QAudioOutput(format, this);
90
connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(finishedPlaying(QAudio::State)));
91
audio->start(inputFile);
95
The file will start playing assuming that the audio system and
96
output device support it. If you run out of luck, check what's
97
up with the error() function.
99
After the file has finished playing, we need to stop the device:
102
void finishedPlaying(QAudio::State state)
104
if(state == QAudio::IdleState) {
111
At any given time, the QAudioOutput will be in one of four states:
112
active, suspended, stopped, or idle. These states are described
113
by the QAudio::State enum.
114
State changes are reported through the stateChanged() signal. You
115
can use this signal to, for instance, update the GUI of the
116
application; the mundane example here being changing the state of
117
a \c { play/pause } button. You request a state change directly
118
with suspend(), stop(), reset(), resume(), and start().
120
While the stream is playing, you can set a notify interval in
121
milliseconds with setNotifyInterval(). This interval specifies the
122
time between two emissions of the notify() signal. This is
123
relative to the position in the stream, i.e., if the QAudioOutput
124
is in the SuspendedState or the IdleState, the notify() signal is
125
not emitted. A typical use-case would be to update a
126
\l{QSlider}{slider} that allows seeking in the stream.
127
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.
130
If an error occurs, you can fetch the \l{QAudio::Error}{error
131
type} with the error() function. Please see the QAudio::Error enum
132
for a description of the possible errors that are reported. When
133
an error is encountered, the state changes to QAudio::StopState.
134
You can check for errors by connecting to the stateChanged()
137
\snippet doc/src/snippets/audio/main.cpp 3
139
\sa QAudioInput, QAudioDeviceInfo
143
Construct a new audio output and attach it to \a parent.
144
The default audio output device is used with the output
145
\a format parameters.
148
QAudioOutput::QAudioOutput(const QAudioFormat &format, QObject *parent):
151
d = QAudioDeviceFactory::createDefaultOutputDevice(format);
152
connect(d, SIGNAL(notify()), SIGNAL(notify()));
153
connect(d, SIGNAL(stateChanged(QAudio::State)), SIGNAL(stateChanged(QAudio::State)));
157
Construct a new audio output and attach it to \a parent.
158
The device referenced by \a audioDevice is used with the output
159
\a format parameters.
162
QAudioOutput::QAudioOutput(const QAudioDeviceInfo &audioDevice, const QAudioFormat &format, QObject *parent):
165
d = QAudioDeviceFactory::createOutputDevice(audioDevice, format);
166
connect(d, SIGNAL(notify()), SIGNAL(notify()));
167
connect(d, SIGNAL(stateChanged(QAudio::State)), SIGNAL(stateChanged(QAudio::State)));
171
Destroys this audio output.
174
QAudioOutput::~QAudioOutput()
180
Returns the QAudioFormat being used.
184
QAudioFormat QAudioOutput::format() const
190
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
195
Passing a QIODevice allows the data to be transfered without any extra code.
196
All that is required is to open the QIODevice.
201
QIODevice* QAudioOutput::start(QIODevice* device)
204
PULL MODE (valid QIODevice)
205
-If currently not StopState, stop.
206
-If previous start was push mode, delete internal QIODevice.
208
-If ok, NoError and ActiveState, else OpenError and StopState
212
PUSH MODE (device = 0)
213
-If currently not StopState, stop.
214
-If no internal QIODevice, create one.
216
-If ok, NoError and IdleState, else OpenError and StopState
218
-return internal QIODevice
220
return d->start(device);
224
Stops the audio output.
227
void QAudioOutput::stop()
230
-If StopState, return
232
-detach from audio device
239
Drops all audio data in the buffers, resets buffers to zero.
242
void QAudioOutput::reset()
245
-drop all buffered audio, set buffers to zero.
252
Stops processing audio data, preserving buffered audio data.
255
void QAudioOutput::suspend()
258
-If not ActiveState|IdleState, return
259
-stop processing audio, saving all buffered audio data
260
-set NoError and SuspendState
267
Resumes processing audio data after a suspend().
270
void QAudioOutput::resume()
273
-If SuspendState, return
275
-(PULL MODE): set ActiveState, NoError
276
-(PUSH MODE): set IdleState, NoError
277
-kick start audio if needed
284
Returns the free space available in bytes in the audio buffer.
287
int QAudioOutput::bytesFree() const
290
-If not ActiveState|IdleState, return 0
291
-return space available in audio buffer in bytes
293
return d->bytesFree();
297
Returns the period size in bytes.
299
Note: This is the recommended write size in bytes.
302
int QAudioOutput::periodSize() const
304
return d->periodSize();
308
Sets the audio buffer size to \a value in bytes.
310
Note: This function can be called anytime before start(), calls to this
311
are ignored after start(). It should not be assumed that the buffer size
312
set is the actual buffer size used, calling bufferSize() anytime after start()
313
will return the actual buffer size being used.
316
void QAudioOutput::setBufferSize(int value)
318
d->setBufferSize(value);
322
Returns the audio buffer size in bytes.
324
If called before start(), returns platform default value.
325
If called before start() but setBufferSize() was called prior, returns value set by setBufferSize().
326
If called after start(), returns the actual buffer size being used. This may not be what was set previously
331
int QAudioOutput::bufferSize() const
333
return d->bufferSize();
337
Sets the interval for notify() signal to be emitted.
338
This is based on the \a ms of audio data processed
339
not on actual real-time. The resolution of the timer is platform specific.
342
void QAudioOutput::setNotifyInterval(int ms)
344
d->setNotifyInterval(ms);
348
Returns the notify interval in milliseconds.
351
int QAudioOutput::notifyInterval() const
353
return d->notifyInterval();
357
Returns the amount of audio data processed since start()
358
was called in microseconds.
361
qint64 QAudioOutput::totalTime() const
363
return d->totalTime();
367
Returns the microseconds since start() was called, including time in Idle and
371
qint64 QAudioOutput::clock() const
377
Returns the error state.
380
QAudio::Error QAudioOutput::error() const
386
Returns the state of audio processing.
389
QAudio::State QAudioOutput::state() const
395
\fn QAudioOutput::stateChanged(QAudio::State state)
396
This signal is emitted when the device \a state has changed.
397
This is the current state of the audio output.
401
\fn QAudioOutput::notify()
402
This signal is emitted when x ms of audio data has been processed
403
the interval set by setNotifyInterval(x).