WSJT-X/soundout.cpp
Bill Somerville b6559fae7c Added audio channel support.
Audio input can be mono, left of stereo pair or, right of stereo
pair. Audio output can be mono, left of stereo pair, right of stereo
pair or, both of stereo pair (the same output goes to both channels in
both mode). Settings are remembered between sessions.

Stream channel suport is implemented mainly in the new AudioDevice
class which is now the base class of Modulator and Detector.

Audio channels are selected on the configuration screen. Only
supported channel configurations per device can be selected.

Audio output volume (actually attenuation) is now possible from the
GUI. I have added a slider control to the main window; I don't
necessarily propose this as a final release location for the widget as
I understand that changes to the main screen are sensitive. This
location is just a starting suggestion for a trial. The volume
(attenuation) setting is remembered between sessions and is not device
dependent. This addresses all issues of volume setting on *nix
versions since there is no need to use pavucontrol to set audio
levels. The volume (attenuation) action is logarithmic.

Shaped CW keying has been implemented in Modulator although it is
currently disabled as I am not 100% happy wth the implementation. If
you want to try it define the C++ preprocessor macro WSJT_SOFT_KEYING
in your build.

The Modulator instance has been moved to the same thread as the
SoundOutput instance as it should have been since the output callback
already operates in that thread. Cross thread slots are now correctly
called in a thread safe way as a result.

A number of files where in the SVN repository with DOS line endings
which I have removed. SVN users on Windows need set the config for
native line endings so that DOS line endings are automatically
stripped on checkin.

The DevSetup class now holds it's UI o the heap to reduce imapact on
build dependencies.

The application settings are now passed to objects from the main.cpp
file. Management of settings are moved to the responsible classes (top
level windows). This has involved a few settings moving groups so
users will see some settings reverting to default values on the first
run of an update.

Persistance of top level windows geometry and position is now handled
in the recommened manner (constructor for load, closeEvent for store
in modal windows and, hideEvent for store in modeless dialogs).

The MainWindow class now holds its children as members rather than
global variables.

The LogQSO class now hides its implementation and takes responsibility
for its own settings and widows rendering parameters. A new settings
file group is implemented to persist the LogQSO class settings.

The WideGraph class now hides its implementation and manages its own
settings and window rendering parameters.

  --This line, and those below, will be ignored--

M    Modulator.cpp
M    rigclass.cpp
M    widegraph.cpp
M    signalmeter.cpp
M    soundin.cpp
M    soundout.cpp
M    mainwindow.h
M    main.cpp
M    meterwidget.h
M    devsetup.cpp
M    mainwindow.ui
M    Detector.cpp
M    logqso.h
M    rigclass.h
M    mainwindow.cpp
M    meterwidget.cpp
M    soundin.h
M    devsetup.ui
M    wsjtx.pro
M    devsetup.h
M    logqso.cpp
M    Modulator.hpp
M    psk_reporter.cpp
M    killbyname.cpp
M    Detector.hpp
M    signalmeter.h
M    widegraph.h
M    psk_reporter.h
M    soundout.h
M    PSKReporter.h
M    lib/afc65b.f90
M    lib/gran.c
M    lib/usleep.c
M    lib/afc9.f90
M    lib/wrapkarn.c
A    AudioDevice.hpp


git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@3542 ab8295b8-cf94-4d9e-aec4-7959e3be5d79
2013-08-10 15:29:55 +00:00

197 lines
4.4 KiB
C++

#include "soundout.h"
#include <QDateTime>
#include <QAudioDeviceInfo>
#include <QAudioOutput>
#include <qmath.h>
#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, unsigned channels)
{
Q_ASSERT (0 < channels && channels < 3);
if (!m_stream || device != m_currentDevice || channels != static_cast<unsigned> (m_stream->format ().channelCount ()))
{
QAudioFormat format (device.preferredFormat ());
format.setChannelCount (channels);
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 ();
m_stream->setVolume (m_volume);
connect (m_stream.data(), &QAudioOutput::stateChanged, this, &SoundOutput::handleStateChanged);
m_currentDevice = device;
}
//
// This buffer size is critical since for proper sound streaming. If
// it is too short; high activity levels on the machine 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.
//
// 2 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 ();
}
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 ();
}
}
qreal SoundOutput::attenuation () const
{
return -(10. * qLn (m_volume) / qLn (10.));
}
void SoundOutput::setAttenuation (qreal a)
{
Q_ASSERT (0. <= a && a <= 99.);
m_volume = qPow (10., -a / 10.);
qDebug () << "SoundOut: attn = " << a << ", vol = " << m_volume;
if (m_stream)
{
m_stream->setVolume (m_volume);
}
}
void SoundOutput::resetAttenuation ()
{
m_volume = 1.;
if (m_stream)
{
m_stream->setVolume (m_volume);
}
}
void SoundOutput::stopStream ()
{
if (m_stream)
{
m_stream->stop ();
audioError ();
}
}
void SoundOutput::handleStateChanged (QAudio::State newState)
{
switch (newState)
{
case QAudio::IdleState:
Q_EMIT status (tr ("Idle"));
m_active = false;
break;
case QAudio::ActiveState:
m_active = true;
Q_EMIT status (tr ("Sending"));
break;
case QAudio::SuspendedState:
m_active = true;
Q_EMIT status (tr ("Suspended"));
break;
case QAudio::StoppedState:
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 ();
}
}