source: trunk/src/multimedia/audio/qaudioinput.cpp@ 769

Last change on this file since 769 was 769, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.3 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 12.9 KB
Line 
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
50QT_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 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 \section1 Symbian Platform Security Requirements
151
152 On Symbian, processes which use this class must have the
153 \c UserEnvironment platform security capability. If the client
154 process lacks this capability, calls to either overload of start()
155 will fail.
156 This failure is indicated by the QAudioInput object setting
157 its error() value to \l{QAudio::OpenError} and then emitting a
158 \l{stateChanged()}{stateChanged}(\l{QAudio::StoppedState}) signal.
159
160 Platform security capabilities are added via the
161 \l{qmake-variable-reference.html#target-capability}{TARGET.CAPABILITY}
162 qmake variable.
163*/
164
165/*!
166 Construct a new audio input and attach it to \a parent.
167 The default audio input device is used with the output
168 \a format parameters.
169*/
170
171QAudioInput::QAudioInput(const QAudioFormat &format, QObject *parent):
172 QObject(parent)
173{
174 d = QAudioDeviceFactory::createDefaultInputDevice(format);
175 connect(d, SIGNAL(notify()), SIGNAL(notify()));
176 connect(d, SIGNAL(stateChanged(QAudio::State)), SIGNAL(stateChanged(QAudio::State)));
177}
178
179/*!
180 Construct a new audio input and attach it to \a parent.
181 The device referenced by \a audioDevice is used with the input
182 \a format parameters.
183*/
184
185QAudioInput::QAudioInput(const QAudioDeviceInfo &audioDevice, const QAudioFormat &format, QObject *parent):
186 QObject(parent)
187{
188 d = QAudioDeviceFactory::createInputDevice(audioDevice, format);
189 connect(d, SIGNAL(notify()), SIGNAL(notify()));
190 connect(d, SIGNAL(stateChanged(QAudio::State)), SIGNAL(stateChanged(QAudio::State)));
191}
192
193/*!
194 Destroy this audio input.
195*/
196
197QAudioInput::~QAudioInput()
198{
199 delete d;
200}
201
202/*!
203 Uses the \a device as the QIODevice to transfer data.
204 Passing a QIODevice allows the data to be transfered without any extra code.
205 All that is required is to open the QIODevice.
206
207 If able to successfully get audio data from the systems audio device the
208 state() is set to either QAudio::ActiveState or QAudio::IdleState,
209 error() is set to QAudio::NoError and the stateChanged() signal is emitted.
210
211 If a problem occurs during this process the error() is set to QAudio::OpenError,
212 state() is set to QAudio::StoppedState and stateChanged() signal is emitted.
213
214 In either case, the stateChanged() signal may be emitted either synchronously
215 during execution of the start() function or asynchronously after start() has
216 returned to the caller.
217
218 \sa {Symbian Platform Security Requirements}
219
220 \sa QIODevice
221*/
222
223void QAudioInput::start(QIODevice* device)
224{
225 d->start(device);
226}
227
228/*!
229 Returns a pointer to the QIODevice being used to handle the data
230 transfer. This QIODevice can be used to read() audio data
231 directly.
232
233 If able to access the systems audio device the state() is set to
234 QAudio::IdleState, error() is set to QAudio::NoError
235 and the stateChanged() signal is emitted.
236
237 If a problem occurs during this process the error() is set to QAudio::OpenError,
238 state() is set to QAudio::StoppedState and stateChanged() signal is emitted.
239
240 In either case, the stateChanged() signal may be emitted either synchronously
241 during execution of the start() function or asynchronously after start() has
242 returned to the caller.
243
244 \sa {Symbian Platform Security Requirements}
245
246 \sa QIODevice
247*/
248
249QIODevice* QAudioInput::start()
250{
251 return d->start(0);
252}
253
254/*!
255 Returns the QAudioFormat being used.
256*/
257
258QAudioFormat QAudioInput::format() const
259{
260 return d->format();
261}
262
263/*!
264 Stops the audio input, detaching from the system resource.
265
266 Sets error() to QAudio::NoError, state() to QAudio::StoppedState and
267 emit stateChanged() signal.
268*/
269
270void QAudioInput::stop()
271{
272 d->stop();
273}
274
275/*!
276 Drops all audio data in the buffers, resets buffers to zero.
277*/
278
279void QAudioInput::reset()
280{
281 d->reset();
282}
283
284/*!
285 Stops processing audio data, preserving buffered audio data.
286
287 Sets error() to QAudio::NoError, state() to QAudio::SuspendedState and
288 emit stateChanged() signal.
289
290 Note: signal will always be emitted during execution of the resume() function.
291*/
292
293void QAudioInput::suspend()
294{
295 d->suspend();
296}
297
298/*!
299 Resumes processing audio data after a suspend().
300
301 Sets error() to QAudio::NoError.
302 Sets state() to QAudio::ActiveState if you previously called start(QIODevice*).
303 Sets state() to QAudio::IdleState if you previously called start().
304 emits stateChanged() signal.
305*/
306
307void QAudioInput::resume()
308{
309 d->resume();
310}
311
312/*!
313 Sets the audio buffer size to \a value bytes.
314
315 Note: This function can be called anytime before start(), calls to this
316 are ignored after start(). It should not be assumed that the buffer size
317 set is the actual buffer size used, calling bufferSize() anytime after start()
318 will return the actual buffer size being used.
319
320*/
321
322void QAudioInput::setBufferSize(int value)
323{
324 d->setBufferSize(value);
325}
326
327/*!
328 Returns the audio buffer size in bytes.
329
330 If called before start(), returns platform default value.
331 If called before start() but setBufferSize() was called prior, returns value set by setBufferSize().
332 If called after start(), returns the actual buffer size being used. This may not be what was set previously
333 by setBufferSize().
334
335*/
336
337int QAudioInput::bufferSize() const
338{
339 return d->bufferSize();
340}
341
342/*!
343 Returns the amount of audio data available to read in bytes.
344
345 NOTE: returned value is only valid while in QAudio::ActiveState or QAudio::IdleState
346 state, otherwise returns zero.
347*/
348
349int QAudioInput::bytesReady() const
350{
351 /*
352 -If not ActiveState|IdleState, return 0
353 -return amount of audio data available to read
354 */
355 return d->bytesReady();
356}
357
358/*!
359 Returns the period size in bytes.
360
361 Note: This is the recommended read size in bytes.
362*/
363
364int QAudioInput::periodSize() const
365{
366 return d->periodSize();
367}
368
369/*!
370 Sets the interval for notify() signal to be emitted.
371 This is based on the \a ms of audio data processed
372 not on actual real-time.
373 The minimum resolution of the timer is platform specific and values
374 should be checked with notifyInterval() to confirm actual value
375 being used.
376*/
377
378void QAudioInput::setNotifyInterval(int ms)
379{
380 d->setNotifyInterval(ms);
381}
382
383/*!
384 Returns the notify interval in milliseconds.
385*/
386
387int QAudioInput::notifyInterval() const
388{
389 return d->notifyInterval();
390}
391
392/*!
393 Returns the amount of audio data processed since start()
394 was called in microseconds.
395*/
396
397qint64 QAudioInput::processedUSecs() const
398{
399 return d->processedUSecs();
400}
401
402/*!
403 Returns the microseconds since start() was called, including time in Idle and
404 Suspend states.
405*/
406
407qint64 QAudioInput::elapsedUSecs() const
408{
409 return d->elapsedUSecs();
410}
411
412/*!
413 Returns the error state.
414*/
415
416QAudio::Error QAudioInput::error() const
417{
418 return d->error();
419}
420
421/*!
422 Returns the state of audio processing.
423*/
424
425QAudio::State QAudioInput::state() const
426{
427 return d->state();
428}
429
430/*!
431 \fn QAudioInput::stateChanged(QAudio::State state)
432 This signal is emitted when the device \a state has changed.
433*/
434
435/*!
436 \fn QAudioInput::notify()
437 This signal is emitted when x ms of audio data has been processed
438 the interval set by setNotifyInterval(x).
439*/
440
441QT_END_NAMESPACE
442
Note: See TracBrowser for help on using the repository browser.