diff options
Diffstat (limited to 'Detector/Detector.cpp')
| -rw-r--r-- | Detector/Detector.cpp | 166 |
1 files changed, 125 insertions, 41 deletions
diff --git a/Detector/Detector.cpp b/Detector/Detector.cpp index bb26a4b3..f75cb9a9 100644 --- a/Detector/Detector.cpp +++ b/Detector/Detector.cpp @@ -1,17 +1,23 @@ #include "Detector.hpp" +#include <algorithm> #include <QDateTime> #include <QtAlgorithms> #include <QDebug> -#include <math.h> +#include <QMutexLocker> +#include <QVector> +#include <QThread> +#include <cmath> +#include <cstddef> #include "commons.h" +#include "DecDataMutex.hpp" #include "moc_Detector.cpp" extern "C" { - void fil4_(qint16*, qint32*, qint16*, qint32*); + void fil4_state_(qint16*, qint32*, qint16*, qint32*, float*); } -extern dec_data_t dec_data; +#include "ReceiveAudio.hpp" Detector::Detector (unsigned frameRate, double periodLengthInSeconds, unsigned downSampleFactor, QObject * parent) @@ -33,6 +39,60 @@ void Detector::setBlockSize (unsigned n) m_samplesPerFFT = n; } +void Detector::setTRPeriod (double period) +{ + if (QThread::currentThread () != thread ()) + { + QMetaObject::invokeMethod (this, [this, period] { setTRPeriod (period); }, + Qt::QueuedConnection); + return; + } + if (m_period != period) + { + m_period = period; + m_last_period_offset_ms = -1; + clear (); + } +} + +void Detector::setStreamDescriptor (AudioStreamDescriptor descriptor) +{ + m_stream_clock.setDescriptor (descriptor); + m_last_period_offset_ms = -1; + clear (); +} + +void Detector::flushBufferedFrames (qint64 frameLimit) +{ + qint64 framesWritten {0}; + ReceiveAudio audio; + { + QMutexLocker lock {&dec_data_mutex ()}; + auto& producer = m_receiveAudioProducer.data (); + if (dec_data_input_blocked ()) return; + if (m_downSampleFactor > 1 && m_bufferPos + && m_receiveAudioProducer.frames () < frameLimit) + { + auto const blockFrames = m_samplesPerFFT * m_downSampleFactor; + std::fill (m_buffer.data () + m_bufferPos, + m_buffer.data () + blockFrames, 0); + qint32 framesToProcess = blockFrames; + qint32 framesAfterDownSample = m_samplesPerFFT; + fil4_state_ (m_buffer.data (), &framesToProcess, + &producer.d2[m_receiveAudioProducer.frames ()], + &framesAfterDownSample, m_downsampleState.data ()); + m_receiveAudioProducer.setFrames (std::min<qint64> ( + frameLimit, m_receiveAudioProducer.frames () + framesAfterDownSample)); + } + framesWritten = std::min<qint64> (frameLimit, m_receiveAudioProducer.frames ()); + if (framesWritten <= m_receiveAudioProducer.capturedEnd ()) return; + audio = m_receiveAudioProducer.capture (framesWritten, m_period); + m_bufferPos = 0; + } + Q_EMIT this->framesWritten (framesWritten); + Q_EMIT audioBlock (audio); +} + bool Detector::reset () { clear (); @@ -43,40 +103,55 @@ bool Detector::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)); - // dec_data.params.kin = qMin ((msInPeriod * m_frameRate) / 1000, static_cast<unsigned> (sizeof (dec_data.d2) / sizeof (dec_data.d2[0]))); - dec_data.params.kin = 0; + QMutexLocker lock {&dec_data_mutex ()}; m_bufferPos = 0; + if (dec_data_input_blocked ()) return; + + resetPeriodBuffer (); +} - // fill buffer with zeros (G4WJS commented out because it might cause decoder hangs) - // qFill (dec_data.d2, dec_data.d2 + sizeof (dec_data.d2) / sizeof (dec_data.d2[0]), 0); +void Detector::resetPeriodBuffer () +{ + m_receiveAudioProducer.reset (m_period); + m_bufferPos = 0; } qint64 Detector::writeData (char const * data, qint64 maxSize) { - static unsigned mstr0=999999; - qint64 ms0 = QDateTime::currentMSecsSinceEpoch() % 86400000; - unsigned mstr = ms0 % int(1000.0*m_period); // ms into the nominal Tx start time - if(mstr < mstr0) { //When mstr has wrapped around to 0, restart the buffer - dec_data.params.kin = 0; - m_bufferPos = 0; - } - mstr0=mstr; - - // no torn frames - Q_ASSERT (!(maxSize % static_cast<qint64> (bytesPerFrame ()))); - // these are in terms of input frames (not down sampled) - size_t framesAcceptable ((sizeof (dec_data.d2) / - sizeof (dec_data.d2[0]) - dec_data.params.kin) * m_downSampleFactor); - size_t framesAccepted (qMin (static_cast<size_t> (maxSize / - bytesPerFrame ()), framesAcceptable)); - - if (framesAccepted < static_cast<size_t> (maxSize / bytesPerFrame ())) { - qDebug () << "dropped " << maxSize / bytesPerFrame () - framesAccepted - << " frames of data on the floor!" - << dec_data.params.kin << mstr; + auto const bytes_per_frame = static_cast<qint64> (bytesPerFrame ()); + Q_ASSERT (!(maxSize % bytes_per_frame)); + qint64 const frames_received = maxSize / bytes_per_frame; + QVector<qint64> frame_counts; + QVector<ReceiveAudio> audio; + qint64 const now_ms = m_stream_clock.timestamp ( + QDateTime::currentMSecsSinceEpoch ()); + m_stream_clock.advance (frames_received); + qint64 const period_ms = static_cast<qint64> (1000.0 * m_period); + qint64 const day_ms = now_ms % 86400000; + qint64 const mstr = day_ms % period_ms; // ms into the nominal Tx start time + + if (dec_data_input_blocked ()) return maxSize; + + { + QMutexLocker lock {&dec_data_mutex ()}; + auto& producer = m_receiveAudioProducer.data (); + if (dec_data_input_blocked ()) return maxSize; + if(m_last_period_offset_ms >= 0 && mstr < m_last_period_offset_ms) { + resetPeriodBuffer (); + } + m_last_period_offset_ms = mstr; + + // these are in terms of input frames (not down sampled) + size_t framesAcceptable ((sizeof producer.d2 / + sizeof producer.d2[0] - m_receiveAudioProducer.frames ()) * m_downSampleFactor); + size_t framesAccepted (qMin (static_cast<size_t> (frames_received), framesAcceptable)); + + if (framesAccepted < static_cast<size_t> (frames_received)) { + auto const frames_dropped = frames_received + - static_cast<qint64> (framesAccepted); + qDebug () << "dropped " << frames_dropped + << " frames of data on the floor!" + << m_receiveAudioProducer.frames () << mstr; } for (unsigned remaining = framesAccepted; remaining; ) { @@ -91,33 +166,42 @@ qint64 Detector::writeData (char const * data, qint64 maxSize) if(m_bufferPos==m_samplesPerFFT*m_downSampleFactor) { qint32 framesToProcess (m_samplesPerFFT * m_downSampleFactor); qint32 framesAfterDownSample (m_samplesPerFFT); - if(m_downSampleFactor > 1 && dec_data.params.kin>=0 && - dec_data.params.kin < (NTMAX*12000 - framesAfterDownSample)) { - fil4_(&m_buffer[0], &framesToProcess, &dec_data.d2[dec_data.params.kin], - &framesAfterDownSample); - dec_data.params.kin += framesAfterDownSample; + if(m_downSampleFactor > 1 && m_receiveAudioProducer.frames ()>=0 && + m_receiveAudioProducer.frames () < (NTMAX*12000 - framesAfterDownSample)) { + fil4_state_(&m_buffer[0], &framesToProcess, + &producer.d2[m_receiveAudioProducer.frames ()], + &framesAfterDownSample, m_downsampleState.data ()); + m_receiveAudioProducer.setFrames (m_receiveAudioProducer.frames () + framesAfterDownSample); } else { // qDebug() << "framesToProcess = " << framesToProcess; - // qDebug() << "dec_data.params.kin = " << dec_data.params.kin; + // qDebug() << "receive audio frames = " << m_receiveAudioProducer.frames (); // qDebug() << "secondInPeriod = " << secondInPeriod(); // qDebug() << "framesAfterDownSample" << framesAfterDownSample; } - Q_EMIT framesWritten (dec_data.params.kin); + frame_counts << m_receiveAudioProducer.frames (); + audio << m_receiveAudioProducer.capture (m_receiveAudioProducer.frames (), m_period); m_bufferPos = 0; } } else { store (&data[(framesAccepted - remaining) * bytesPerFrame ()], - numFramesProcessed, &dec_data.d2[dec_data.params.kin]); + numFramesProcessed, &producer.d2[m_receiveAudioProducer.frames ()]); m_bufferPos += numFramesProcessed; - dec_data.params.kin += numFramesProcessed; + m_receiveAudioProducer.setFrames (m_receiveAudioProducer.frames () + numFramesProcessed); if (m_bufferPos == static_cast<unsigned> (m_samplesPerFFT)) { - Q_EMIT framesWritten (dec_data.params.kin); + frame_counts << m_receiveAudioProducer.frames (); + audio << m_receiveAudioProducer.capture (m_receiveAudioProducer.frames (), m_period); m_bufferPos = 0; } } remaining -= numFramesProcessed; } + } + + for (auto frames : frame_counts) { + Q_EMIT framesWritten (frames); + } + for (auto const& block : audio) Q_EMIT audioBlock (block); // we drop any data past the end of the buffer on the floor until // the next period starts |
