2012-05-22 13:09:48 -04:00
|
|
|
#include "soundin.h"
|
2013-07-31 20:49:58 -04:00
|
|
|
|
2013-08-05 09:57:55 -04:00
|
|
|
#include <QAudioDeviceInfo>
|
|
|
|
#include <QAudioFormat>
|
|
|
|
#include <QAudioInput>
|
2013-07-31 20:49:58 -04:00
|
|
|
#include <QDebug>
|
2012-05-22 13:09:48 -04:00
|
|
|
|
2013-08-05 09:57:55 -04:00
|
|
|
bool SoundInput::audioError () const
|
2012-05-22 13:09:48 -04:00
|
|
|
{
|
2013-08-05 09:57:55 -04:00
|
|
|
bool result (true);
|
2012-05-22 13:09:48 -04:00
|
|
|
|
2013-08-05 09:57:55 -04:00
|
|
|
Q_ASSERT_X (m_stream, "SoundInput", "programming error");
|
|
|
|
if (m_stream)
|
|
|
|
{
|
|
|
|
switch (m_stream->error ())
|
|
|
|
{
|
|
|
|
case QAudio::OpenError:
|
|
|
|
Q_EMIT error (tr ("An error opening the audio input device has occurred."));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QAudio::IOError:
|
|
|
|
Q_EMIT error (tr ("An error occurred during read from the audio input device."));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QAudio::UnderrunError:
|
|
|
|
Q_EMIT error (tr ("Audio data not being fed to the audio input device fast enough."));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QAudio::FatalError:
|
|
|
|
Q_EMIT error (tr ("Non-recoverable error, audio input device not usable at this time."));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QAudio::NoError:
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
2013-07-31 20:49:58 -04:00
|
|
|
}
|
2013-08-05 09:57:55 -04:00
|
|
|
return result;
|
2013-07-29 20:51:42 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:57:55 -04:00
|
|
|
bool SoundInput::start(QAudioDeviceInfo const& device, int framesPerBuffer, QIODevice * sink)
|
2012-05-22 13:09:48 -04:00
|
|
|
{
|
2013-07-29 20:51:42 -04:00
|
|
|
stop();
|
2012-05-22 13:09:48 -04:00
|
|
|
|
2013-08-05 09:57:55 -04:00
|
|
|
QAudioFormat format (device.preferredFormat());
|
|
|
|
format.setChannelCount (1);
|
|
|
|
format.setCodec ("audio/pcm");
|
|
|
|
format.setSampleRate (12000);
|
|
|
|
format.setSampleType (QAudioFormat::SignedInt);
|
|
|
|
format.setSampleSize (16);
|
2012-05-22 13:09:48 -04:00
|
|
|
|
2013-08-05 09:57:55 -04:00
|
|
|
if (!format.isValid ())
|
2013-07-29 20:51:42 -04:00
|
|
|
{
|
2013-08-05 09:57:55 -04:00
|
|
|
Q_EMIT error (tr ("Requested input audio format is not valid."));
|
|
|
|
return false;
|
2013-07-29 20:51:42 -04:00
|
|
|
}
|
2012-05-22 13:09:48 -04:00
|
|
|
|
2013-08-05 09:57:55 -04:00
|
|
|
// this function lies!
|
|
|
|
// if (!device.isFormatSupported (format))
|
|
|
|
// {
|
|
|
|
// Q_EMIT error (tr ("Requested input audio format is not supported on device."));
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
|
|
|
|
m_stream.reset (new QAudioInput (device, format, this));
|
|
|
|
if (audioError ())
|
2013-07-29 20:51:42 -04:00
|
|
|
{
|
2013-08-05 09:57:55 -04:00
|
|
|
return false;
|
2013-07-29 20:51:42 -04:00
|
|
|
}
|
2012-05-22 13:09:48 -04:00
|
|
|
|
2013-08-05 09:57:55 -04:00
|
|
|
connect (m_stream.data(), &QAudioInput::stateChanged, this, &SoundInput::handleStateChanged);
|
2013-07-30 16:36:14 -04:00
|
|
|
|
2013-08-05 09:57:55 -04:00
|
|
|
m_stream->setBufferSize (m_stream->format ().bytesForFrames (framesPerBuffer));
|
2013-07-31 20:49:58 -04:00
|
|
|
|
2013-08-05 09:57:55 -04:00
|
|
|
m_stream->start (sink);
|
2013-07-30 16:36:14 -04:00
|
|
|
|
2013-08-05 09:57:55 -04:00
|
|
|
qDebug () << "audio input buffer size = " << m_stream->bufferSize () << " bytes\n";
|
2013-07-30 16:36:14 -04:00
|
|
|
|
2013-08-05 09:57:55 -04:00
|
|
|
return audioError () ? false : true;
|
2013-07-30 16:36:14 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:57:55 -04:00
|
|
|
void SoundInput::handleStateChanged (QAudio::State newState) const
|
2013-07-30 16:36:14 -04:00
|
|
|
{
|
2013-08-05 09:57:55 -04:00
|
|
|
switch (newState)
|
|
|
|
{
|
|
|
|
case QAudio::IdleState:
|
|
|
|
qDebug () << "SoundInput idle\n";
|
|
|
|
Q_EMIT status (tr ("Idle"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QAudio::ActiveState:
|
|
|
|
qDebug () << "SoundInput active\n";
|
|
|
|
Q_EMIT status (tr ("Receiving"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QAudio::SuspendedState:
|
|
|
|
qDebug () << "SoundInput suspended\n";
|
|
|
|
Q_EMIT status (tr ("Suspended"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QAudio::StoppedState:
|
|
|
|
if (audioError ())
|
|
|
|
{
|
|
|
|
qDebug () << "SoundInput error\n";
|
|
|
|
Q_EMIT status (tr ("Error"));
|
2013-07-30 16:36:14 -04:00
|
|
|
}
|
2013-08-05 09:57:55 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug () << "SoundInput stopped\n";
|
|
|
|
Q_EMIT status (tr ("Stopped"));
|
2013-07-30 16:36:14 -04:00
|
|
|
}
|
2013-08-05 09:57:55 -04:00
|
|
|
break;
|
|
|
|
}
|
2013-07-30 16:36:14 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:57:55 -04:00
|
|
|
void SoundInput::stop()
|
2013-07-30 16:36:14 -04:00
|
|
|
{
|
2013-08-05 09:57:55 -04:00
|
|
|
if (m_stream)
|
|
|
|
{
|
|
|
|
m_stream->stop ();
|
|
|
|
}
|
|
|
|
m_stream.reset ();
|
2013-07-30 16:36:14 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:57:55 -04:00
|
|
|
SoundInput::~SoundInput ()
|
2013-07-30 16:36:14 -04:00
|
|
|
{
|
|
|
|
}
|