You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

220 lines
8.3 KiB

///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Dan Chokola, KD2HSG //
// Based on the packet modulator plugin: //
// Copyright (C) 2020-2023 Jon Beniston, M7RCE <jon@beniston.com> //
// Copyright (C) 2020-2022 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
// //
// 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>
#include "dsp/upchannelizer.h"
#include "dsp/dspcommands.h"
#include "chirpmodemmodbaseband.h"
MESSAGE_CLASS_DEFINITION(ChirpModemModBaseband::MsgConfigureChirpModemModBaseband, Message)
ChirpModemModBaseband::ChirpModemModBaseband()
{
m_sampleFifo.resize(SampleSourceFifo::getSizePolicy(48000));
m_channelizer = new UpChannelizer(&m_source);
m_generator.setMaxThreadCount(1);
qDebug("ChirpModemModBaseband::ChirpModemModBaseband");
QObject::connect(&m_sampleFifo, &SampleSourceFifo::dataRead, this, &ChirpModemModBaseband::handleData, Qt::QueuedConnection);
QObject::connect(&m_inputMessageQueue, &MessageQueue::messageEnqueued, this, &ChirpModemModBaseband::handleInputMessages);
}
ChirpModemModBaseband::~ChirpModemModBaseband()
{
// A generation in progress posts to m_inputMessageQueue: let it finish first
m_generator.clear();
m_generator.waitForDone();
m_inputMessageQueue.clear();
delete m_channelizer;
}
void ChirpModemModBaseband::reset()
{
QMutexLocker mutexLocker(&m_mutex);
m_generator.clear(); // not yet started; one running is dropped by its epoch
m_sampleFifo.reset();
m_source.reset();
}
void ChirpModemModBaseband::pull(const SampleVector::iterator& begin, unsigned int nbSamples)
{
unsigned int part1Begin, part1End, part2Begin, part2End;
m_sampleFifo.read(nbSamples, part1Begin, part1End, part2Begin, part2End);
SampleVector& data = m_sampleFifo.getData();
if (part1Begin != part1End) {
std::copy(data.begin() + part1Begin, data.begin() + part1End, begin);
}
unsigned int shift = part1End - part1Begin;
if (part2Begin != part2End) {
std::copy(data.begin() + part2Begin, data.begin() + part2End, begin + shift);
}
}
void ChirpModemModBaseband::handleData()
{
QMutexLocker mutexLocker(&m_mutex);
SampleVector& data = m_sampleFifo.getData();
unsigned int ipart1begin, ipart1end, ipart2begin, ipart2end;
qreal rmsLevel, peakLevel;
int numSamples;
unsigned int remainder = m_sampleFifo.remainder();
// Stops when a message is queued, so requests and settings take effect between blocks
while ((remainder > 0) && (m_inputMessageQueue.size() == 0))
{
m_sampleFifo.write(remainder, ipart1begin, ipart1end, ipart2begin, ipart2end);
if (ipart1begin != ipart1end) {
processFifo(data, ipart1begin, ipart1end);
}
if (ipart2begin != ipart2end) {
processFifo(data, ipart2begin, ipart2end);
}
remainder = m_sampleFifo.remainder();
}
m_source.getLevels(rmsLevel, peakLevel, numSamples);
emit levelChanged(rmsLevel, peakLevel, numSamples);
}
void ChirpModemModBaseband::processFifo(SampleVector& data, unsigned int iBegin, unsigned int iEnd)
{
m_channelizer->prefetch(iEnd - iBegin);
m_channelizer->pull(data.begin() + iBegin, iEnd - iBegin);
}
void ChirpModemModBaseband::handleInputMessages()
{
Message *message;
while ((message = m_inputMessageQueue.pop()))
{
if (!handleMessage(*message)) {
qDebug("%s: unhandled message: %s", Q_FUNC_INFO, message->getIdentifier());
}
delete message;
}
}
bool ChirpModemModBaseband::handleMessage(const Message& cmd)
{
if (MsgConfigureChirpModemModBaseband::match(cmd))
{
QMutexLocker mutexLocker(&m_mutex);
const auto& cfg = static_cast<const MsgConfigureChirpModemModBaseband&>(cmd);
qDebug() << "ChirpModemModBaseband::handleMessage: MsgConfigureChirpModemModBaseband";
applySettings(cfg.getSettingsKeys(), cfg.getSettings(), cfg.getForce());
return true;
}
else if (ChirpModemModMessages::MsgTx::match(cmd))
{
QMutexLocker mutexLocker(&m_mutex);
generate(m_source.makeJob(static_cast<const ChirpModemModMessages::MsgTx&>(cmd).getRequest()));
return true;
}
else if (ChirpModemModGen::MsgGenerated::match(cmd))
{
QMutexLocker mutexLocker(&m_mutex);
auto transmission = static_cast<const ChirpModemModGen::MsgGenerated&>(cmd).getTransmission();
if (!m_source.addGenerated(transmission))
{
// Made for a channel rate or offset that has since changed
ChirpModemModGen::Job job = transmission->m_job;
job.m_channelSampleRate = m_channelizer->getChannelSampleRate();
job.m_channelFrequencyOffset = m_channelizer->getChannelFrequencyOffset();
job.m_regenerated = true;
generate(job);
}
return true;
}
else if (DSPSignalNotification::match(cmd))
{
QMutexLocker mutexLocker(&m_mutex);
const auto& notif = static_cast<const DSPSignalNotification&>(cmd);
qDebug() << "ChirpModemModBaseband::handleMessage: DSPSignalNotification: basebandSampleRate:" << notif.getSampleRate();
m_sampleFifo.resize(SampleSourceFifo::getSizePolicy(notif.getSampleRate()));
m_channelizer->setBasebandSampleRate(notif.getSampleRate());
for (const auto& job : m_source.applyChannelSettings(m_channelizer->getChannelSampleRate(), m_channelizer->getChannelFrequencyOffset())) {
generate(job);
}
return true;
}
else
{
return false;
}
}
void ChirpModemModBaseband::applySettings(const QStringList& settingsKeys, const ChirpModemModSettings& settings, bool force)
{
if (force ||
(settingsKeys.contains("inputFrequencyOffset") && (settings.m_inputFrequencyOffset != m_settings.m_inputFrequencyOffset)) ||
(settingsKeys.contains("bandwidth") && (settings.m_bandwidth != m_settings.m_bandwidth)))
{
m_channelizer->setChannelization(settings.requestedChannelSampleRate(), settings.m_inputFrequencyOffset);
for (const auto& job : m_source.applyChannelSettings(m_channelizer->getChannelSampleRate(), m_channelizer->getChannelFrequencyOffset())) {
generate(job);
}
}
m_source.applySettings(settingsKeys, settings, force);
if (force) {
m_settings = settings;
} else {
m_settings.applySettings(settingsKeys, settings);
}
}
// On the generator thread. The result comes back to the DSP thread as a message.
void ChirpModemModBaseband::generate(const ChirpModemModGen::Job& job)
{
m_source.generationStarted();
MessageQueue *queue = &m_inputMessageQueue;
m_generator.start([job, queue]() {
auto transmission = std::make_shared<ChirpModemModGen::Transmission>();
transmission->m_job = job;
ChirpModemModGen::generate(*transmission);
queue->push(ChirpModemModGen::MsgGenerated::create(transmission));
});
}
int ChirpModemModBaseband::getChannelSampleRate() const
{
return m_channelizer->getChannelSampleRate();
}
int ChirpModemModBaseband::getChannelFrequencyOffset() const
{
return m_channelizer->getChannelFrequencyOffset();
}