CopperSpice API  1.9.1
QAudioInput Class Reference

The QAudioInput class provides an interface for receiving audio data from an audio input device. More...

Inheritance diagram for QAudioInput:
QObject

Public Signals

void notify ()
 
void stateChanged (QAudio::State state)
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 

Public Methods

 QAudioInput (const QAudioDeviceInfo &audioDeviceInfo, const QAudioFormat &format=QAudioFormat (), QObject *parent=nullptr)
 
 QAudioInput (const QAudioFormat &format=QAudioFormat (), QObject *parent=nullptr)
 
 ~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 bytes)
 
void setNotifyInterval (int milliSeconds)
 
void setVolume (qreal volume)
 
QIODevicestart ()
 
void start (QIODevice *device)
 
QAudio::State state () const
 
void stop ()
 
void suspend ()
 
qreal volume () const
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 
- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 
- Properties inherited from QObject
 objectName
 

Detailed Description

The QAudioInput class provides an interface for receiving audio data from an audio input device.

An audio input can be constructed with the system's default audio input device. It is also possible to create a QAudioInput with a specific QAudioDeviceInfo. When creating the audio input the QAudioFormat should be specified. Starting up the QAudioInput is simply a matter of calling start() with a QIODevice opened for writing.

The following code shows how to record to a file. This will start recording if the format specified is supported by the input device (you can check this with QAudioDeviceInfo::isFormatSupported(). In case there are any issues use the error() function to check what went wrong.

QFile outputFile; // class member
QAudioInput *audioInput; // class member
void startRecording() {
outputFile.setFileName("/tmp/test.raw");
outputFile.open( QIODevice::WriteOnly | QIODevice::Truncate );
// 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);
if (! info.isFormatSupported(format)) {
qWarning() << "default format not supported try to use nearest";
format = info.nearestFormat(format);
}
audioInput = new QAudioInput(format, this);
QTimer::singleShot(3000, this, SLOT(stopRecording()));
audioInput->start(&outputFile);
// Records audio for 3000ms
}

The code to stop recording is placed in the stopRecording() 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 the QAudio::State enum. You can request a state change directly through suspend(), resume(), stop(), reset(), and start(). The current state is reported by state(). QAudioOutput will also signal you when the state changes (stateChanged()).

QAudioInput provides several ways of measuring the time that has passed since the start() of the recording. The processedUSecs() function returns the length of the stream in microseconds written, i.e., it leaves out the times the audio input was suspended or idle. The elapsedUSecs() function returns the time elapsed since start() was called regardless of which states the QAudioInput has been in.

If an error should occur, you can fetch its reason with error(). The possible error reasons are described by the QAudio::Error> enum. The QAudioInput will enter the StoppedState when an error is encountered. Connect to the stateChanged() signal to handle the error:

void stateChanged(QAudio::State newState) {
switch(newState) {
case QAudio::StoppedState:
if (audioInput->error() != QAudio::NoError) {
// Perform error handling
} else {
}
break;
}
}
See also
QAudioOutput, QAudioDeviceInfo

Constructor & Destructor Documentation

QAudioInput::QAudioInput ( const QAudioFormat format = QAudioFormat(),
QObject parent = nullptr 
)
explicit

Construct a new audio input and attach it to parent. The default audio input device is used with the output format parameters.

QAudioInput::QAudioInput ( const QAudioDeviceInfo audioDeviceInfo,
const QAudioFormat format = QAudioFormat(),
QObject parent = nullptr 
)
explicit

Construct a new audio input and attach it to parent. The device referenced by audioDeviceInfo is used with the input format parameters.

QAudioInput::~QAudioInput ( )

Destroy this audio input.

Method Documentation

int QAudioInput::bufferSize ( ) const

Returns the audio buffer size in milliseconds.

If called before start(), returns platform default value. If called before start() but setBufferSize() was called prior, returns value set by setBufferSize(). If called after start(), returns the actual buffer size being used. This may not be what was set previously by setBufferSize().

See also
setBufferSize()
int QAudioInput::bytesReady ( ) const

Returns the amount of audio data available to read in bytes.

The returned value is only valid while in QAudio::ActiveState or QAudio::IdleState state, otherwise returns zero.

qint64 QAudioInput::elapsedUSecs ( ) const

Returns the microseconds since start() was called, including time in Idle and Suspend states.

QAudio::Error QAudioInput::error ( ) const

Returns the error state.

QAudioFormat QAudioInput::format ( ) const

Returns the QAudioFormat being used.

void QAudioInput::notify ( )
signal

This signal is emitted when x ms of audio data has been processed the interval set by setNotifyInterval(x).

int QAudioInput::notifyInterval ( ) const

Returns the notify interval in milliseconds.

See also
setNotifyInterval()
int QAudioInput::periodSize ( ) const

Returns the period size in bytes.

qint64 QAudioInput::processedUSecs ( ) const

Returns the amount of audio data processed since start() was called in microseconds.

void QAudioInput::reset ( )

Drops all audio data in the buffers, resets buffers to zero.

void QAudioInput::resume ( )

Resumes processing audio data after a suspend().

Sets error() to QAudio::NoError. Sets state() to QAudio::ActiveState if you previously called start(QIODevice*). Sets state() to QAudio::IdleState if you previously called start(). emits stateChanged() signal.

void QAudioInput::setBufferSize ( int  bytes)

Sets the audio buffer size to bytes.

The method can be called anytime before start() is called. After start() has been called, this method has no effect. The actual buffer size may be rounded up. Calling bufferSize() after start() has been called will return the calculated buffer.

See also
bufferSize()
void QAudioInput::setNotifyInterval ( int  milliSeconds)

Sets the interval for notify() signal to be emitted. This is based on the milliSeconds of audio data processed not on clock time. The minimum resolution of the timer is platform specific and values should be checked with notifyInterval() to confirm actual value being used.

See also
notifyInterval()
void QAudioInput::setVolume ( qreal  volume)

Sets the value of the property to volume.

QIODevice * QAudioInput::start ( )

Returns a pointer to a new QIODevice that will be used to handle the data transfer. This QIODevice can be used to read() audio data directly. You will typically connect to the readyRead() 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 the state() is set to QAudio::IdleState, error() is set to QAudio::NoError and the stateChanged() signal is emitted.

If a problem occurs during this process the error() is set to QAudio::OpenError, the state() is set to QAudio::StoppedState, and the stateChanged signal is emitted.

See also
QIODevice
void QAudioInput::start ( QIODevice device)

Uses the device as the QIODevice to transfer data. Passing a QIODevice allows the data to be transferred without any extra code. All that is required is to open the QIODevice. QAudioInput does not take ownership of device.

The QAudioInput will write to the device when new data is available. You can subclass QIODevice and reimplement writeData() if you wish to access the data. If you simply want to save data to a file, you can pass a QFile to this function.

If able to successfully get audio data from the systems audio device the state() is set to either QAudio::ActiveState or QAudio::IdleState, error() is set to QAudio::NoError and the stateChanged() signal is emitted.

If a problem occurs during this process the error() is set to QAudio::OpenError, the state() is set to QAudio::StoppedState, and the stateChanged signal is emitted.

See also
QIODevice
QAudio::State QAudioInput::state ( ) const

Returns the state of audio processing.

void QAudioInput::stateChanged ( QAudio::State  state)
signal

This signal is emitted when the device state has changed.

void QAudioInput::stop ( )

Stops the audio input, detaching from the system resource. Sets error() to QAudio::NoError, state() to QAudio::StoppedState and then emits the stateChanged() signal.

void QAudioInput::suspend ( )

Stops processing audio data, preserving buffered audio data. Sets error() to QAudio::NoError, state() to QAudio::SuspendedState, and emits the stateChanged() signal.

qreal QAudioInput::volume ( ) const

Returns the value of the property.