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

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

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

  • Property svn:eol-style set to native
File size: 12.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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 transferred 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 \l{QAudioInput#Symbian Platform Security Requirements}
215
216 \sa QIODevice
217*/
218
219void QAudioInput::start(QIODevice* device)
220{
221 d->start(device);
222}
223
224/*!
225 Returns a pointer to the QIODevice being used to handle the data
226 transfer. This QIODevice can be used to read() audio data
227 directly.
228
229 If able to access the systems audio device the state() is set to
230 QAudio::IdleState, error() is set to QAudio::NoError
231 and the stateChanged() signal is emitted.
232
233 If a problem occurs during this process the error() is set to QAudio::OpenError,
234 state() is set to QAudio::StoppedState and stateChanged() signal is emitted.
235
236 \l{QAudioInput#Symbian Platform Security Requirements}
237
238 \sa QIODevice
239*/
240
241QIODevice* QAudioInput::start()
242{
243 return d->start(0);
244}
245
246/*!
247 Returns the QAudioFormat being used.
248*/
249
250QAudioFormat QAudioInput::format() const
251{
252 return d->format();
253}
254
255/*!
256 Stops the audio input, detaching from the system resource.
257
258 Sets error() to QAudio::NoError, state() to QAudio::StoppedState and
259 emit stateChanged() signal.
260*/
261
262void QAudioInput::stop()
263{
264 d->stop();
265}
266
267/*!
268 Drops all audio data in the buffers, resets buffers to zero.
269*/
270
271void QAudioInput::reset()
272{
273 d->reset();
274}
275
276/*!
277 Stops processing audio data, preserving buffered audio data.
278
279 Sets error() to QAudio::NoError, state() to QAudio::SuspendedState and
280 emit stateChanged() signal.
281*/
282
283void QAudioInput::suspend()
284{
285 d->suspend();
286}
287
288/*!
289 Resumes processing audio data after a suspend().
290
291 Sets error() to QAudio::NoError.
292 Sets state() to QAudio::ActiveState if you previously called start(QIODevice*).
293 Sets state() to QAudio::IdleState if you previously called start().
294 emits stateChanged() signal.
295*/
296
297void QAudioInput::resume()
298{
299 d->resume();
300}
301
302/*!
303 Sets the audio buffer size to \a value milliseconds.
304
305 Note: This function can be called anytime before start(), calls to this
306 are ignored after start(). It should not be assumed that the buffer size
307 set is the actual buffer size used, calling bufferSize() anytime after start()
308 will return the actual buffer size being used.
309
310*/
311
312void QAudioInput::setBufferSize(int value)
313{
314 d->setBufferSize(value);
315}
316
317/*!
318 Returns the audio buffer size in milliseconds.
319
320 If called before start(), returns platform default value.
321 If called before start() but setBufferSize() was called prior, returns value set by setBufferSize().
322 If called after start(), returns the actual buffer size being used. This may not be what was set previously
323 by setBufferSize().
324
325*/
326
327int QAudioInput::bufferSize() const
328{
329 return d->bufferSize();
330}
331
332/*!
333 Returns the amount of audio data available to read in bytes.
334
335 NOTE: returned value is only valid while in QAudio::ActiveState or QAudio::IdleState
336 state, otherwise returns zero.
337*/
338
339int QAudioInput::bytesReady() const
340{
341 /*
342 -If not ActiveState|IdleState, return 0
343 -return amount of audio data available to read
344 */
345 return d->bytesReady();
346}
347
348/*!
349 Returns the period size in bytes.
350
351 Note: This is the recommended read size in bytes.
352*/
353
354int QAudioInput::periodSize() const
355{
356 return d->periodSize();
357}
358
359/*!
360 Sets the interval for notify() signal to be emitted.
361 This is based on the \a ms of audio data processed
362 not on actual real-time.
363 The minimum resolution of the timer is platform specific and values
364 should be checked with notifyInterval() to confirm actual value
365 being used.
366*/
367
368void QAudioInput::setNotifyInterval(int ms)
369{
370 d->setNotifyInterval(ms);
371}
372
373/*!
374 Returns the notify interval in milliseconds.
375*/
376
377int QAudioInput::notifyInterval() const
378{
379 return d->notifyInterval();
380}
381
382/*!
383 Returns the amount of audio data processed since start()
384 was called in microseconds.
385*/
386
387qint64 QAudioInput::processedUSecs() const
388{
389 return d->processedUSecs();
390}
391
392/*!
393 Returns the microseconds since start() was called, including time in Idle and
394 Suspend states.
395*/
396
397qint64 QAudioInput::elapsedUSecs() const
398{
399 return d->elapsedUSecs();
400}
401
402/*!
403 Returns the error state.
404*/
405
406QAudio::Error QAudioInput::error() const
407{
408 return d->error();
409}
410
411/*!
412 Returns the state of audio processing.
413*/
414
415QAudio::State QAudioInput::state() const
416{
417 return d->state();
418}
419
420/*!
421 \fn QAudioInput::stateChanged(QAudio::State state)
422 This signal is emitted when the device \a state has changed.
423*/
424
425/*!
426 \fn QAudioInput::notify()
427 This signal is emitted when x ms of audio data has been processed
428 the interval set by setNotifyInterval(x).
429*/
430
431QT_END_NAMESPACE
432
Note: See TracBrowser for help on using the repository browser.