2013-08-07 19:09:13 -04:00
|
|
|
#include "Detector.hpp"
|
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QtAlgorithms>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#include "commons.h"
|
|
|
|
|
2013-09-27 09:57:48 -04:00
|
|
|
extern "C" {
|
|
|
|
void fil4_(qint16*, qint32*, qint16*, qint32*);
|
|
|
|
}
|
|
|
|
|
2013-08-10 11:29:55 -04:00
|
|
|
Detector::Detector (unsigned frameRate, unsigned periodLengthInSeconds, unsigned framesPerSignal, QObject * parent)
|
|
|
|
: AudioDevice (parent)
|
2013-08-07 19:09:13 -04:00
|
|
|
, m_frameRate (frameRate)
|
|
|
|
, m_period (periodLengthInSeconds)
|
2013-08-10 11:29:55 -04:00
|
|
|
, m_framesPerSignal (framesPerSignal)
|
2013-08-07 19:09:13 -04:00
|
|
|
, 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;
|
|
|
|
|
2013-09-27 20:58:36 -04:00
|
|
|
// fill buffer with zeros (G4WJS commented out because it might cause decoder hangs)
|
2013-09-27 11:07:28 -04:00
|
|
|
// qFill (jt9com_.d2, jt9com_.d2 + sizeof (jt9com_.d2) / sizeof (jt9com_.d2[0]), 0);
|
2013-08-07 19:09:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
qint64 Detector::writeData (char const * data, qint64 maxSize)
|
|
|
|
{
|
2013-09-27 20:58:36 -04:00
|
|
|
bool overrun (false);
|
|
|
|
int excess (0);
|
2013-08-10 21:30:05 -04:00
|
|
|
if (m_monitoring)
|
|
|
|
{
|
|
|
|
Q_ASSERT (!(maxSize % static_cast<qint64> (bytesPerFrame ()))); // no torn frames
|
2013-08-07 19:09:13 -04:00
|
|
|
|
2013-08-10 21:30:05 -04:00
|
|
|
qint64 framesAcceptable (sizeof (jt9com_.d2) / sizeof (jt9com_.d2[0]) - jt9com_.kin);
|
|
|
|
qint64 framesAccepted (qMin (static_cast<qint64> (maxSize / bytesPerFrame ()), framesAcceptable));
|
2013-09-27 20:58:36 -04:00
|
|
|
|
|
|
|
overrun = framesAccepted < static_cast<qint64> (maxSize / bytesPerFrame ());
|
|
|
|
if (overrun)
|
2013-08-10 21:30:05 -04:00
|
|
|
{
|
|
|
|
qDebug () << "dropped " << maxSize / sizeof (jt9com_.d2[0]) - framesAccepted << " frames of data on the floor!";
|
|
|
|
}
|
2013-08-07 19:09:13 -04:00
|
|
|
|
2013-09-27 20:58:36 -04:00
|
|
|
Q_ASSERT (2 == bytesPerFrame ()); // only mono until fil4 can do stereo
|
|
|
|
excess = framesAccepted % 4;
|
2013-09-27 09:57:48 -04:00
|
|
|
qint32 m_n1,m_n2;
|
2013-09-27 20:58:36 -04:00
|
|
|
m_n1 = framesAccepted - excess;
|
2013-09-27 09:57:48 -04:00
|
|
|
fil4_((qint16 *)data, &m_n1, m_translate, &m_n2);
|
|
|
|
store ((char const *) m_translate, m_n2, &jt9com_.d2[jt9com_.kin]);
|
2013-08-07 19:09:13 -04:00
|
|
|
|
2013-08-10 21:30:05 -04:00
|
|
|
unsigned lastSignalIndex (jt9com_.kin / m_framesPerSignal);
|
2013-09-27 09:57:48 -04:00
|
|
|
jt9com_.kin += m_n2;
|
2013-08-10 21:30:05 -04:00
|
|
|
unsigned currentSignalIndex (jt9com_.kin / m_framesPerSignal);
|
2013-08-07 19:09:13 -04:00
|
|
|
|
2013-08-10 21:30:05 -04:00
|
|
|
if (currentSignalIndex != lastSignalIndex && m_monitoring)
|
|
|
|
{
|
|
|
|
Q_EMIT framesWritten (currentSignalIndex * m_framesPerSignal);
|
|
|
|
}
|
2013-08-07 19:09:13 -04:00
|
|
|
|
2013-08-10 21:30:05 -04:00
|
|
|
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)
|
2013-08-07 19:09:13 -04:00
|
|
|
{
|
2013-08-10 21:30:05 -04:00
|
|
|
m_starting = false;
|
2013-08-07 19:09:13 -04:00
|
|
|
}
|
|
|
|
}
|
2013-08-10 21:30:05 -04:00
|
|
|
else
|
2013-08-07 19:09:13 -04:00
|
|
|
{
|
2013-08-10 21:30:05 -04:00
|
|
|
jt9com_.kin = 0;
|
2013-08-07 19:09:13 -04:00
|
|
|
}
|
|
|
|
|
2013-09-27 20:58:36 -04:00
|
|
|
return maxSize - (overrun ? 0 : excess * bytesPerFrame ());
|
|
|
|
// we drop any data past the end of the buffer on
|
|
|
|
// the floor until the next period starts
|
2013-08-07 19:09:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|