
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQAudioInput class provides an interface for receiving audio data from an audio input device.More...
| Header: | #include <QAudioInput> |
| Since: | Qt 4.6 |
| Inherits: | QObject |
| QAudioInput(const QAudioFormat & format = QAudioFormat(), QObject * parent = 0) | |
| QAudioInput(const QAudioDeviceInfo & audioDevice, const QAudioFormat & format = QAudioFormat(), QObject * parent = 0) | |
| ~QAudioInput() | |
| int | bufferSize() const |
| int | bytesReady() const |
| qint64 | elapsedUSecs() const |
| QAudio::Error | error() const |
| QAudioFormat | format() const |
| int | notifyInterval() const |
| int | periodSize() const |
| qint64 | processedUSecs() const |
| void | reset() |
| void | resume() |
| void | setBufferSize(int value) |
| void | setNotifyInterval(int ms) |
| void | start(QIODevice * device) |
| QIODevice * | start() |
| QAudio::State | state() const |
| void | stop() |
| void | suspend() |
| void | notify() |
| void | stateChanged(QAudio::State state) |
TheQAudioInput class provides an interface for receiving audio data from an audio input device.
You can construct an audio input with the system'sdefault audio input device. It is also possible to createQAudioInput with a specificQAudioDeviceInfo. When you create the audio input, you should also send in theQAudioFormat to be used for the recording (see theQAudioFormat class description for details).
To record to a file:
QAudioInput lets you record audio with an audio input device. The default constructor of this class will use the systems default audio device, but you can also specify aQAudioDeviceInfo for a specific device. You also need to pass in theQAudioFormat in which you wish to record.
Starting up theQAudioInput is simply a matter of callingstart() with aQIODevice opened for writing. For instance, to record to a file, you can:
QFile outputFile;// class member.QAudioInput*audioInput;// class member. ...void startRecording() { outputFile.setFileName("/tmp/test.raw"); outputFile.open(QIODevice::WriteOnly|QIODevice::Truncate );QAudioFormat format;// set up the format you want, eg. format.setFrequency(8000); format.setChannels(1); format.setSampleSize(8); format.setCodec("audio/pcm"); format.setByteOrder(QAudioFormat::LittleEndian); format.setSampleType(QAudioFormat::UnSignedInt);QAudioDeviceInfo info=QAudioDeviceInfo::defaultInputDevice();if (!info.isFormatSupported(format)) {qWarning()<<"default format not supported try to use nearest"; format= info.nearestFormat(format); } audioInput=newQAudioInput(format,this);QTimer::singleShot(3000,this, SLOT(stopRecording())); audioInput->start(&outputFile);// Records audio for 3000ms }
This will start recording if the format specified is supported by the input device (you can check this withQAudioDeviceInfo::isFormatSupported(). In case there are any snags, use theerror() function to check what went wrong. We stop recording in thestopRecording() slot.
void stopRecording() { audioInput->stop(); outputFile.close();delete audioInput; }
At any point in time,QAudioInput will be in one of four states: active, suspended, stopped, or idle. These states are specified by theQAudio::State enum. You can request a state change directly throughsuspend(),resume(),stop(),reset(), andstart(). The current state is reported bystate().QAudioOutput will also signal you when the state changes (stateChanged()).
QAudioInput provides several ways of measuring the time that has passed since thestart() of the recording. TheprocessedUSecs() function returns the length of the stream in microseconds written, i.e., it leaves out the times the audio input was suspended or idle. TheelapsedUSecs() function returns the time elapsed sincestart() was called regardless of which states theQAudioInput has been in.
If an error should occur, you can fetch its reason witherror(). The possible error reasons are described by theQAudio::Error enum. TheQAudioInput will enter theStoppedState when an error is encountered. Connect to thestateChanged() signal to handle the error:
void stateChanged(QAudio::State newState) {switch(newState) {caseQAudio::StoppedState:if (audioInput->error()!=QAudio::NoError) {// Perform error handling }else { }break;
On Symbian, processes which use this class must have theUserEnvironment platform security capability. If the client process lacks this capability, calls to either overload ofstart() will fail. This failure is indicated by theQAudioInput object setting itserror() value toQAudio::OpenError and then emitting astateChanged(QAudio::StoppedState) signal.
Platform security capabilities are added via theTARGET.CAPABILITY qmake variable.
See alsoQAudioOutput andQAudioDeviceInfo.
Construct a new audio input and attach it toparent. The default audio input device is used with the outputformat parameters.
Construct a new audio input and attach it toparent. The device referenced byaudioDevice is used with the inputformat parameters.
Destroy this audio input.
Returns the audio buffer size in milliseconds.
If called beforestart(), returns platform default value. If called beforestart() butsetBufferSize() was called prior, returns value set bysetBufferSize(). If called afterstart(), returns the actual buffer size being used. This may not be what was set previously bysetBufferSize().
See alsosetBufferSize().
Returns the amount of audio data available to read in bytes.
NOTE: returned value is only valid while inQAudio::ActiveState orQAudio::IdleState state, otherwise returns zero.
Returns the microseconds sincestart() was called, including time in Idle and Suspend states.
Returns the error state.
Returns theQAudioFormat being used.
[signal]void QAudioInput::notify()This signal is emitted when x ms of audio data has been processed the interval set bysetNotifyInterval(x).
Returns the notify interval in milliseconds.
See alsosetNotifyInterval().
Returns the period size in bytes.
Note: This is the recommended read size in bytes.
Returns the amount of audio data processed sincestart() was called in microseconds.
Drops all audio data in the buffers, resets buffers to zero.
Resumes processing audio data after asuspend().
Setserror() toQAudio::NoError. Setsstate() toQAudio::ActiveState if you previously called start(QIODevice*). Setsstate() toQAudio::IdleState if you previously calledstart(). emitsstateChanged() signal.
Sets the audio buffer size tovalue milliseconds.
Note: This function can be called anytime beforestart(), calls to this are ignored afterstart(). It should not be assumed that the buffer size set is the actual buffer size used, callingbufferSize() anytime afterstart() will return the actual buffer size being used.
See alsobufferSize().
Sets the interval fornotify() signal to be emitted. This is based on thems of audio data processed not on actual real-time. The minimum resolution of the timer is platform specific and values should be checked withnotifyInterval() to confirm actual value being used.
See alsonotifyInterval().
Uses thedevice as theQIODevice to transfer data. Passing aQIODevice allows the data to be transferred without any extra code. All that is required is to open theQIODevice.QAudioInput does not take ownership ofdevice.
TheQAudioInput will write to the device when new data is available. You can subclassQIODevice and reimplementwriteData() if you wish to access the data. If you simply want to save data to a file, you can pass aQFile to this function.
If able to successfully get audio data from the systems audio device thestate() is set to eitherQAudio::ActiveState orQAudio::IdleState,error() is set toQAudio::NoError and thestateChanged() signal is emitted.
If a problem occurs during this process theerror() is set toQAudio::OpenError,state() is set toQAudio::StoppedState andstateChanged() signal is emitted.
QAudioInput#Symbian Platform Security Requirements
See alsoQIODevice.
Returns a pointer to a newQIODevice that will be used to handle the data transfer. ThisQIODevice can be used toread() audio data directly. You will typically connect to thereadyRead() signal, and read from the device in the slot you connect to.QAudioInput keeps ownership of the device.
If able to access the systems audio device thestate() is set toQAudio::IdleState,error() is set toQAudio::NoError and thestateChanged() signal is emitted.
If a problem occurs during this process theerror() is set toQAudio::OpenError,state() is set toQAudio::StoppedState andstateChanged() signal is emitted.
QAudioInput#Symbian Platform Security Requirements
See alsoQIODevice.
Returns the state of audio processing.
[signal]void QAudioInput::stateChanged(QAudio::State state)This signal is emitted when the devicestate has changed.
Stops the audio input, detaching from the system resource.
Setserror() toQAudio::NoError,state() toQAudio::StoppedState and emitstateChanged() signal.
Stops processing audio data, preserving buffered audio data.
Setserror() toQAudio::NoError,state() toQAudio::SuspendedState and emitstateChanged() signal.
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.