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-10-04 15:00:29 -04:00
|
|
|
void SoundInput::start(QAudioDeviceInfo const& device, unsigned channels, int framesPerBuffer, QIODevice * sink, unsigned downSampleFactor)
|
2012-05-22 13:09:48 -04:00
|
|
|
{
|
2013-08-10 11:29:55 -04:00
|
|
|
Q_ASSERT (0 < channels && channels < 3);
|
|
|
|
Q_ASSERT (sink);
|
|
|
|
|
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());
|
2013-08-10 11:29:55 -04:00
|
|
|
format.setChannelCount (channels);
|
2013-08-05 09:57:55 -04:00
|
|
|
format.setCodec ("audio/pcm");
|
2013-10-04 15:00:29 -04:00
|
|
|
format.setSampleRate (12000 * downSampleFactor);
|
2013-08-05 09:57:55 -04:00
|
|
|
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."));
|
2013-08-17 15:21:14 -04:00
|
|
|
return;
|
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."));
|
2013-08-17 15:21:14 -04:00
|
|
|
// return;
|
2013-08-05 09:57:55 -04:00
|
|
|
// }
|
|
|
|
|
|
|
|
m_stream.reset (new QAudioInput (device, format, this));
|
|
|
|
if (audioError ())
|
2013-07-29 20:51:42 -04:00
|
|
|
{
|
2013-08-17 15:21:14 -04:00
|
|
|
return;
|
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);
|
|
|
|
m_stream->setBufferSize (m_stream->format ().bytesForFrames (framesPerBuffer));
|
|
|
|
m_stream->start (sink);
|
2013-08-17 15:21:14 -04:00
|
|
|
audioError ();
|
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:
|
|
|
|
Q_EMIT status (tr ("Idle"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QAudio::ActiveState:
|
|
|
|
Q_EMIT status (tr ("Receiving"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QAudio::SuspendedState:
|
|
|
|
Q_EMIT status (tr ("Suspended"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QAudio::StoppedState:
|
|
|
|
if (audioError ())
|
|
|
|
{
|
|
|
|
Q_EMIT status (tr ("Error"));
|
2013-07-30 16:36:14 -04:00
|
|
|
}
|
2013-08-05 09:57:55 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|