1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-26 17:58:43 -05:00
sdrangel/plugins/channelrx/demodft8/ft8demodbaseband.cpp

301 lines
10 KiB
C++
Raw Normal View History

2023-01-14 19:22:57 -05:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2019-2023 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
// Copyright (C) 2022 Jiří Pinkava <jiri.pinkava@rossum.ai> //
2023-01-14 19:22:57 -05:00
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation as version 3 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <QDebug>
2023-01-18 17:00:59 -05:00
#include <QThread>
2023-01-14 19:22:57 -05:00
#include "dsp/dspengine.h"
#include "dsp/dspcommands.h"
#include "dsp/spectrumvis.h"
2023-01-18 17:00:59 -05:00
#include "maincore.h"
2023-01-14 19:22:57 -05:00
2023-01-18 17:00:59 -05:00
#include "ft8demodworker.h"
2023-01-14 19:22:57 -05:00
#include "ft8demodbaseband.h"
MESSAGE_CLASS_DEFINITION(FT8DemodBaseband::MsgConfigureFT8DemodBaseband, Message)
FT8DemodBaseband::FT8DemodBaseband() :
m_channelizer(&m_sink),
m_messageQueueToGUI(nullptr),
2023-01-20 00:42:11 -05:00
m_spectrumVis(nullptr),
m_deviceCenterFrequency(0)
2023-01-14 19:22:57 -05:00
{
2023-01-18 17:00:59 -05:00
qDebug("FT8DemodBaseband::FT8DemodBaseband");
2023-01-14 19:22:57 -05:00
m_sampleFifo.setSize(SampleSinkFifo::getSizePolicy(48000));
2023-01-18 17:00:59 -05:00
m_ft8WorkerBuffer = new int16_t[FT8DemodSettings::m_ft8SampleRate*15];
m_workerThread = new QThread();
m_ft8DemodWorker = new FT8DemodWorker();
m_ft8DemodWorker->moveToThread(m_workerThread);
QObject::connect(
m_workerThread,
&QThread::finished,
m_ft8DemodWorker,
&QObject::deleteLater
);
QObject::connect(
m_workerThread,
&QThread::finished,
m_ft8DemodWorker,
&QThread::deleteLater
);
QObject::connect(
this,
&FT8DemodBaseband::bufferReady,
m_ft8DemodWorker,
2023-01-19 17:51:43 -05:00
&FT8DemodWorker::processBuffer,
Qt::QueuedConnection
2023-01-18 17:00:59 -05:00
);
m_workerThread->start();
2023-01-14 19:22:57 -05:00
QObject::connect(
&m_sampleFifo,
&SampleSinkFifo::dataReady,
this,
&FT8DemodBaseband::handleData,
Qt::QueuedConnection
);
m_channelSampleRate = 0;
2023-01-18 17:00:59 -05:00
m_sink.setFT8Buffer(&m_ft8Buffer);
2023-01-14 19:22:57 -05:00
connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()));
2023-01-18 17:00:59 -05:00
connect(&MainCore::instance()->getMasterTimer(), SIGNAL(timeout()), this, SLOT(tick()));
2023-01-14 19:22:57 -05:00
}
FT8DemodBaseband::~FT8DemodBaseband()
{
2023-01-18 17:00:59 -05:00
m_workerThread->exit();
m_workerThread->wait();
delete[] m_ft8WorkerBuffer;
2023-01-14 19:22:57 -05:00
}
void FT8DemodBaseband::reset()
{
QMutexLocker mutexLocker(&m_mutex);
m_sampleFifo.reset();
m_channelSampleRate = 0;
}
2023-01-20 00:42:11 -05:00
void FT8DemodBaseband::setMessageQueueToGUI(MessageQueue *messageQueue)
{
m_messageQueueToGUI = messageQueue;
m_ft8DemodWorker->setReportingMessageQueue(m_messageQueueToGUI);
}
2023-01-14 19:22:57 -05:00
void FT8DemodBaseband::setChannel(ChannelAPI *channel)
{
m_sink.setChannel(channel);
m_ft8DemodWorker->setChannel(channel);
2023-01-14 19:22:57 -05:00
}
void FT8DemodBaseband::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end)
{
m_sampleFifo.write(begin, end);
}
void FT8DemodBaseband::handleData()
{
QMutexLocker mutexLocker(&m_mutex);
while ((m_sampleFifo.fill() > 0) && (m_inputMessageQueue.size() == 0))
{
SampleVector::iterator part1begin;
SampleVector::iterator part1end;
SampleVector::iterator part2begin;
SampleVector::iterator part2end;
std::size_t count = m_sampleFifo.readBegin(m_sampleFifo.fill(), &part1begin, &part1end, &part2begin, &part2end);
// first part of FIFO data
if (part1begin != part1end) {
m_channelizer.feed(part1begin, part1end);
}
// second part of FIFO data (used when block wraps around)
if(part2begin != part2end) {
m_channelizer.feed(part2begin, part2end);
}
m_sampleFifo.readCommit((unsigned int) count);
}
2023-01-16 15:51:26 -05:00
qreal rmsLevel, peakLevel;
int numSamples;
m_sink.getLevels(rmsLevel, peakLevel, numSamples);
emit levelChanged(rmsLevel, peakLevel, numSamples);
2023-01-14 19:22:57 -05:00
}
void FT8DemodBaseband::handleInputMessages()
{
Message* message;
while ((message = m_inputMessageQueue.pop()) != nullptr)
{
if (handleMessage(*message)) {
delete message;
}
}
}
bool FT8DemodBaseband::handleMessage(const Message& cmd)
{
if (MsgConfigureFT8DemodBaseband::match(cmd))
{
QMutexLocker mutexLocker(&m_mutex);
MsgConfigureFT8DemodBaseband& cfg = (MsgConfigureFT8DemodBaseband&) cmd;
qDebug() << "FT8DemodBaseband::handleMessage: MsgConfigureFT8DemodBaseband";
applySettings(cfg.getSettings(), cfg.getForce());
return true;
}
else if (DSPSignalNotification::match(cmd))
{
QMutexLocker mutexLocker(&m_mutex);
DSPSignalNotification& notif = (DSPSignalNotification&) cmd;
qDebug() << "FT8DemodBaseband::handleMessage: DSPSignalNotification: basebandSampleRate: " << notif.getSampleRate();
m_sampleFifo.setSize(SampleSinkFifo::getSizePolicy(notif.getSampleRate()));
m_channelizer.setBasebandSampleRate(notif.getSampleRate());
m_sink.applyChannelSettings(m_channelizer.getChannelSampleRate(), m_channelizer.getChannelFrequencyOffset());
if (m_channelSampleRate != m_channelizer.getChannelSampleRate())
{
2023-01-18 17:00:59 -05:00
m_sink.applyFT8SampleRate(); // reapply when channel sample rate changes
2023-01-14 19:22:57 -05:00
m_channelSampleRate = m_channelizer.getChannelSampleRate();
}
2023-01-20 00:42:11 -05:00
if (notif.getCenterFrequency() != m_deviceCenterFrequency)
{
m_ft8DemodWorker->invalidateSequence();
m_deviceCenterFrequency = notif.getCenterFrequency();
m_ft8DemodWorker->setBaseFrequency(m_deviceCenterFrequency + m_settings.m_inputFrequencyOffset);
2023-01-20 00:42:11 -05:00
}
2023-01-14 19:22:57 -05:00
return true;
}
else
{
return false;
}
}
void FT8DemodBaseband::applySettings(const FT8DemodSettings& settings, bool force)
{
if ((settings.m_inputFrequencyOffset != m_settings.m_inputFrequencyOffset) || force)
{
2023-01-20 00:42:11 -05:00
m_ft8DemodWorker->invalidateSequence();
m_ft8DemodWorker->setBaseFrequency(m_deviceCenterFrequency + settings.m_inputFrequencyOffset);
2023-01-18 17:00:59 -05:00
m_channelizer.setChannelization(FT8DemodSettings::m_ft8SampleRate, settings.m_inputFrequencyOffset);
2023-01-14 19:22:57 -05:00
m_sink.applyChannelSettings(m_channelizer.getChannelSampleRate(), m_channelizer.getChannelFrequencyOffset());
if (m_channelSampleRate != m_channelizer.getChannelSampleRate())
{
2023-01-18 17:00:59 -05:00
m_sink.applyFT8SampleRate(); // reapply when channel sample rate changes
2023-01-14 19:22:57 -05:00
m_channelSampleRate = m_channelizer.getChannelSampleRate();
}
}
if ((settings.m_filterBank[settings.m_filterIndex].m_spanLog2 != m_settings.m_filterBank[settings.m_filterIndex].m_spanLog2) || force)
{
if (m_spectrumVis)
{
2023-01-18 17:00:59 -05:00
DSPSignalNotification *msg = new DSPSignalNotification(FT8DemodSettings::m_ft8SampleRate/(1<<settings.m_filterBank[settings.m_filterIndex].m_spanLog2), 0);
m_spectrumVis->getInputMessageQueue()->push(msg);
2023-01-14 19:22:57 -05:00
}
}
2023-01-19 17:51:43 -05:00
if ((m_settings.m_filterBank[m_settings.m_filterIndex].m_lowCutoff != settings.m_filterBank[settings.m_filterIndex].m_lowCutoff) || force) {
m_ft8DemodWorker->setLowFrequency(settings.m_filterBank[settings.m_filterIndex].m_lowCutoff);
}
if ((m_settings.m_filterBank[m_settings.m_filterIndex].m_rfBandwidth != settings.m_filterBank[settings.m_filterIndex].m_rfBandwidth) || force) {
m_ft8DemodWorker->setHighFrequency(settings.m_filterBank[settings.m_filterIndex].m_rfBandwidth);
}
if ((settings.m_recordWav != m_settings.m_recordWav) || force) {
m_ft8DemodWorker->setRecordSamples(settings.m_recordWav);
}
if ((settings.m_logMessages != m_settings.m_logMessages) || force) {
m_ft8DemodWorker->setLogMessages(settings.m_logMessages);
}
if ((settings.m_nbDecoderThreads != m_settings.m_nbDecoderThreads) || force) {
m_ft8DemodWorker->setNbDecoderThreads(settings.m_nbDecoderThreads);
}
if ((settings.m_decoderTimeBudget != m_settings.m_decoderTimeBudget) || force) {
m_ft8DemodWorker->setDecoderTimeBudget(settings.m_decoderTimeBudget);
}
if ((settings.m_useOSD != m_settings.m_useOSD) || force) {
m_ft8DemodWorker->setUseOSD(settings.m_useOSD);
}
if ((settings.m_osdDepth != m_settings.m_osdDepth) || force) {
m_ft8DemodWorker->setOSDDepth(settings.m_osdDepth);
}
if ((settings.m_osdLDPCThreshold != m_settings.m_osdLDPCThreshold) || force) {
m_ft8DemodWorker->setOSDLDPCThreshold(settings.m_osdLDPCThreshold);
}
if ((settings.m_verifyOSD != m_settings.m_verifyOSD) || force) {
m_ft8DemodWorker->setVerifyOSD(settings.m_verifyOSD);
}
2023-01-14 19:22:57 -05:00
m_sink.applySettings(settings, force);
m_settings = settings;
}
int FT8DemodBaseband::getChannelSampleRate() const
{
return m_channelizer.getChannelSampleRate();
}
void FT8DemodBaseband::setBasebandSampleRate(int sampleRate)
{
m_channelizer.setBasebandSampleRate(sampleRate);
m_sink.applyChannelSettings(m_channelizer.getChannelSampleRate(), m_channelizer.getChannelFrequencyOffset());
}
2023-01-18 17:00:59 -05:00
void FT8DemodBaseband::tick()
{
QDateTime nowUTC = QDateTime::currentDateTimeUtc();
if (nowUTC.time().second() % 15 < 14)
{
if (m_tickCount++ == 0)
{
QDateTime periodTs = nowUTC.addSecs(-15);
// qDebug("FT8DemodBaseband::tick: %s", qPrintable(nowUTC.toString("yyyy-MM-dd HH:mm:ss")));
2023-01-18 17:00:59 -05:00
m_ft8Buffer.getCurrentBuffer(m_ft8WorkerBuffer);
emit bufferReady(m_ft8WorkerBuffer, periodTs);
periodTs = nowUTC;
}
}
else
{
m_tickCount = 0;
}
}