WSJT-X/Detector.cpp

96 lines
2.9 KiB
C++
Raw Normal View History

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 19:09:13 -04:00
#include "Detector.hpp"
#include <QDateTime>
#include <QtAlgorithms>
#include <QDebug>
#include "commons.h"
Detector::Detector (unsigned frameRate, unsigned periodLengthInSeconds, unsigned bytesPerSignal, QObject * parent)
: QIODevice (parent)
, m_frameRate (frameRate)
, m_period (periodLengthInSeconds)
, m_bytesPerSignal (bytesPerSignal)
, m_monitoring (false)
, m_starting (false)
{
clear ();
}
bool Detector::reset ()
{
clear ();
return QIODevice::reset ();
}
void Detector::clear ()
{
// set index to roughly where we are in time (1ms resolution)
// qint64 now (QDateTime::currentMSecsSinceEpoch ());
// unsigned msInPeriod ((now % 86400000LL) % (m_period * 1000));
// jt9com_.kin = qMin ((msInPeriod * m_frameRate) / 1000, static_cast<unsigned> (sizeof (jt9com_.d2) / sizeof (jt9com_.d2[0])));
jt9com_.kin = 0;
// fill buffer with zeros
qFill (jt9com_.d2, jt9com_.d2 + sizeof (jt9com_.d2) / sizeof (jt9com_.d2[0]), 0);
}
qint64 Detector::writeData (char const * data, qint64 maxSize)
{
Q_ASSERT (!(maxSize % static_cast<qint64> (sizeof (frame_t)))); // no torn frames
Q_ASSERT (!(reinterpret_cast<size_t> (data) % __alignof__ (frame_t))); // data is aligned as frame_t would be
frame_t const * frames (reinterpret_cast<frame_t const *> (data));
qint64 framesAcceptable (sizeof (jt9com_.d2) / sizeof (jt9com_.d2[0]) - jt9com_.kin);
qint64 framesAccepted (qMin (static_cast<qint64> (maxSize / sizeof (jt9com_.d2[0])), framesAcceptable));
if (framesAccepted < static_cast<qint64> (maxSize / sizeof (jt9com_.d2[0])))
{
qDebug () << "dropped " << maxSize / sizeof (jt9com_.d2[0]) - framesAccepted << " frames of data on the floor!";
}
qCopy (frames, frames + framesAccepted, &jt9com_.d2[jt9com_.kin]);
unsigned lastSignalIndex (jt9com_.kin * sizeof (jt9com_.d2[0]) / m_bytesPerSignal);
jt9com_.kin += framesAccepted;
unsigned currentSignalIndex (jt9com_.kin * sizeof (jt9com_.d2[0]) / m_bytesPerSignal);
if (currentSignalIndex != lastSignalIndex && m_monitoring)
{
Q_EMIT bytesWritten (currentSignalIndex * m_bytesPerSignal);
}
if (!secondInPeriod ())
{
if (!m_starting)
{
// next samples will be in new period so wrap around to
// start of buffer
//
// we don't bother calling reset () since we expect to fill
// the whole buffer and don't need to waste cycles zeroing
jt9com_.kin = 0;
m_starting = true;
}
}
else if (m_starting)
{
m_starting = false;
}
return maxSize; // we drop any data past the end of
// the buffer on the floor until the
// next period starts
}
unsigned Detector::secondInPeriod () const
{
// we take the time of the data as the following assuming no latency
// delivering it to us (not true but close enough for us)
qint64 now (QDateTime::currentMSecsSinceEpoch ());
unsigned secondInToday ((now % 86400000LL) / 1000);
return secondInToday % m_period;
}