WSJT-X/soundout.cpp
Bill Somerville ef586e4b58 Qt 5 Audio replaces PortAudio.
Currently only Qt5 or above is known to work with this code. It may be
possible to backport it to Qt4 if required.

Audio output goes back to a separate thread to try and minimize
stutters in streaming on Windows particularly.

A crash on Linux due to mishandling of stereo audio output has been
fixed and both left and right channels are now correctly synthesised
with identical contents.

Rigs are enumerated directly from hamlib API rather than running a sub
process reading output of rigctl -l. This was initially done to get
rid of some GUI thread blocking in the configuration dialog, but is
generally a better way of doing it anyway.

Some refactoring in MainWindow to accomodate the audio streaming,
modulation and detecting classes.

Exit handling for application refactored to use signals rather than
brute force event loop exit. This was required to get correct thread
shutdown semantics.

The GUI update timer is now stopped during application shutdown which
is necessary to stop crashes when shutting down gracefully with
signals and window close() calls.

There is an outstanding issue with Linux audio streams; the QAudio
Input/Output classes create a new stream name each time a stream is
started. This doesn't play well with PulseAudio utilities such as
pavucontrol to set stream volume as settings are lost every tx
period. I have tried to keep a single stream for all output but there
are problems restarting it that haven't been resolved yet.

The QtCreator project file has been rearranged a little because it
passes all the object files to the linker rather than using an archive
library. Since the GNU linker is single pass; the object files need to
be in a logical order with definitions appearing afer references to
them. This was required to avoid a linking error.

The lib/Makefile.linux has been enhanced to use the fortran compiler
to locate the correct version of the Fortran library to use. This is
necessary on the latest Linux distros because the unversioned symlink
to compiler support libraries is no longer provided. This only an
issue with mixed programming language links where the linker driver
for one language has to link support libraraies for another language.


git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@3532 ab8295b8-cf94-4d9e-aec4-7959e3be5d79
2013-08-07 23:09:13 +00:00

179 lines
4.1 KiB
C++

#include "soundout.h"
#include <QDateTime>
#include <QAudioDeviceInfo>
#include <QAudioOutput>
#include <QDebug>
#if defined (WIN32)
# define MS_BUFFERED 1000
#else
# define MS_BUFFERED 2000
#endif
bool SoundOutput::audioError () const
{
bool result (true);
Q_ASSERT_X (m_stream, "SoundOutput", "programming error");
if (m_stream)
{
switch (m_stream->error ())
{
case QAudio::OpenError:
Q_EMIT error (tr ("An error opening the audio output device has occurred."));
break;
case QAudio::IOError:
Q_EMIT error (tr ("An error occurred during write to the audio output device."));
break;
case QAudio::UnderrunError:
Q_EMIT error (tr ("Audio data not being fed to the audio output device fast enough."));
break;
case QAudio::FatalError:
Q_EMIT error (tr ("Non-recoverable error, audio output device not usable at this time."));
break;
case QAudio::NoError:
result = false;
break;
}
}
return result;
}
SoundOutput::SoundOutput (QIODevice * source)
: m_source (source)
, m_active (false)
, m_currentDevice (QAudioDeviceInfo::defaultOutputDevice ())
{
Q_ASSERT (source);
}
void SoundOutput::startStream (QAudioDeviceInfo const& device)
{
if (!m_stream || device != m_currentDevice)
{
QAudioFormat format (device.preferredFormat ());
#ifdef UNIX
format.setChannelCount (2);
#else
format.setChannelCount (1);
#endif
format.setCodec ("audio/pcm");
format.setSampleRate (48000);
format.setSampleType (QAudioFormat::SignedInt);
format.setSampleSize (16);
if (!format.isValid ())
{
Q_EMIT error (tr ("Requested output audio format is not valid."));
}
if (!device.isFormatSupported (format))
{
Q_EMIT error (tr ("Requested output audio format is not supported on device."));
}
m_stream.reset (new QAudioOutput (device, format, this));
audioError ();
connect (m_stream.data(), &QAudioOutput::stateChanged, this, &SoundOutput::handleStateChanged);
m_currentDevice = device;
}
//
// This buffer size is critical since we are running in the GUI
// thread. If it is too short; high activity levels on the GUI can
// starve the audio buffer. On the other hand the Windows
// implementation seems to take the length of the buffer in time to
// stop the audio stream even if reset() is used.
//
// 1 seconds seems a reasonable compromise except for Windows
// where things are probably broken.
//
// we have to set this before every start on the stream because the
// Windows implementation seems to forget the buffer size after a
// stop.
m_stream->setBufferSize (m_stream->format ().bytesForDuration (MS_BUFFERED * 1000));
m_stream->start (m_source);
audioError ();
qDebug () << "audio output buffer size = " << m_stream->bufferSize () << " bytes";
}
void SoundOutput::suspend ()
{
if (m_stream && QAudio::ActiveState == m_stream->state ())
{
m_stream->suspend ();
audioError ();
}
}
void SoundOutput::resume ()
{
if (m_stream && QAudio::SuspendedState == m_stream->state ())
{
m_stream->resume ();
audioError ();
}
}
void SoundOutput::stopStream ()
{
if (m_stream)
{
m_stream->stop ();
audioError ();
}
}
void SoundOutput::handleStateChanged (QAudio::State newState)
{
switch (newState)
{
case QAudio::IdleState:
qDebug () << "SoundOutput: entered Idle state";
Q_EMIT status (tr ("Idle"));
m_active = false;
break;
case QAudio::ActiveState:
qDebug () << "SoundOutput: entered Active state";
m_active = true;
Q_EMIT status (tr ("Sending"));
break;
case QAudio::SuspendedState:
qDebug () << "SoundOutput: entered Suspended state";
m_active = true;
Q_EMIT status (tr ("Suspended"));
break;
case QAudio::StoppedState:
qDebug () << "SoundOutput: entered Stopped state";
m_active = false;
if (audioError ())
{
Q_EMIT status (tr ("Error"));
}
else
{
Q_EMIT status (tr ("Stopped"));
}
break;
}
}
SoundOutput::~SoundOutput ()
{
if (m_stream)
{
m_stream->stop ();
}
}