1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-10 18:43:28 -05:00

AMMod: revised thread processing

This commit is contained in:
f4exb 2024-08-17 22:33:55 +02:00 committed by Edouard Griffiths
parent 2219fcd809
commit 2b26f15463
6 changed files with 113 additions and 46 deletions

View File

@ -56,18 +56,13 @@ const char* const AMMod::m_channelId ="AMMod";
AMMod::AMMod(DeviceAPI *deviceAPI) : AMMod::AMMod(DeviceAPI *deviceAPI) :
ChannelAPI(m_channelIdURI, ChannelAPI::StreamSingleSource), ChannelAPI(m_channelIdURI, ChannelAPI::StreamSingleSource),
m_deviceAPI(deviceAPI), m_deviceAPI(deviceAPI),
m_running(false),
m_fileSize(0), m_fileSize(0),
m_recordLength(0), m_recordLength(0),
m_sampleRate(48000) m_sampleRate(48000),
m_levelMeter(nullptr)
{ {
setObjectName(m_channelId); setObjectName(m_channelId);
m_thread = new QThread(this);
m_basebandSource = new AMModBaseband();
m_basebandSource->setInputFileStream(&m_ifstream);
m_basebandSource->setChannel(this);
m_basebandSource->moveToThread(m_thread);
applySettings(m_settings, true); applySettings(m_settings, true);
m_deviceAPI->addChannelSource(this); m_deviceAPI->addChannelSource(this);
@ -93,8 +88,8 @@ AMMod::~AMMod()
delete m_networkManager; delete m_networkManager;
m_deviceAPI->removeChannelSourceAPI(this); m_deviceAPI->removeChannelSourceAPI(this);
m_deviceAPI->removeChannelSource(this); m_deviceAPI->removeChannelSource(this);
delete m_basebandSource;
delete m_thread; stop();
} }
void AMMod::setDeviceAPI(DeviceAPI *deviceAPI) void AMMod::setDeviceAPI(DeviceAPI *deviceAPI)
@ -116,22 +111,62 @@ uint32_t AMMod::getNumberOfDeviceStreams() const
void AMMod::start() void AMMod::start()
{ {
if (m_running) {
return;
}
qDebug("AMMod::start"); qDebug("AMMod::start");
m_thread = new QThread(this);
m_basebandSource = new AMModBaseband();
m_basebandSource->setInputFileStream(&m_ifstream);
m_basebandSource->setChannel(this);
m_basebandSource->reset(); m_basebandSource->reset();
m_basebandSource->setCWKeyer(&m_cwKeyer);
m_basebandSource->moveToThread(m_thread);
QObject::connect(
m_thread,
&QThread::finished,
m_basebandSource,
&QObject::deleteLater
);
QObject::connect(
m_thread,
&QThread::finished,
m_thread,
&QThread::deleteLater
);
m_thread->start(); m_thread->start();
AMModBaseband::MsgConfigureAMModBaseband *msg = AMModBaseband::MsgConfigureAMModBaseband::create(m_settings, true);
m_basebandSource->getInputMessageQueue()->push(msg);
if (m_levelMeter) {
connect(m_basebandSource, SIGNAL(levelChanged(qreal, qreal, int)), m_levelMeter, SLOT(levelChanged(qreal, qreal, int)));
}
m_running = true;
} }
void AMMod::stop() void AMMod::stop()
{ {
if (!m_running) {
return;
}
qDebug("AMMod::stop"); qDebug("AMMod::stop");
m_running = false;
m_thread->exit(); m_thread->exit();
m_thread->wait(); m_thread->wait();
} }
void AMMod::pull(SampleVector::iterator& begin, unsigned int nbSamples) void AMMod::pull(SampleVector::iterator& begin, unsigned int nbSamples)
{ {
if (m_running) {
m_basebandSource->pull(begin, nbSamples); m_basebandSource->pull(begin, nbSamples);
} }
}
void AMMod::setCenterFrequency(qint64 frequency) void AMMod::setCenterFrequency(qint64 frequency)
{ {
@ -203,11 +238,13 @@ bool AMMod::handleMessage(const Message& cmd)
} }
else if (DSPSignalNotification::match(cmd)) else if (DSPSignalNotification::match(cmd))
{ {
qDebug() << "AMMod::handleMessage: DSPSignalNotification";
// Forward to the source // Forward to the source
DSPSignalNotification& notif = (DSPSignalNotification&) cmd; DSPSignalNotification& notif = (DSPSignalNotification&) cmd;
DSPSignalNotification* rep = new DSPSignalNotification(notif); // make a copy
qDebug() << "AMMod::handleMessage: DSPSignalNotification"; if (m_running) {
m_basebandSource->getInputMessageQueue()->push(rep); m_basebandSource->getInputMessageQueue()->push(new DSPSignalNotification(notif));
}
// Forward to GUI if any // Forward to GUI if any
if (getMessageQueueToGUI()) { if (getMessageQueueToGUI()) {
getMessageQueueToGUI()->push(new DSPSignalNotification(notif)); getMessageQueueToGUI()->push(new DSPSignalNotification(notif));
@ -338,8 +375,11 @@ void AMMod::applySettings(const AMModSettings& settings, bool force)
reverseAPIKeys.append("streamIndex"); reverseAPIKeys.append("streamIndex");
} }
if (m_running)
{
AMModBaseband::MsgConfigureAMModBaseband *msg = AMModBaseband::MsgConfigureAMModBaseband::create(settings, force); AMModBaseband::MsgConfigureAMModBaseband *msg = AMModBaseband::MsgConfigureAMModBaseband::create(settings, force);
m_basebandSource->getInputMessageQueue()->push(msg); m_basebandSource->getInputMessageQueue()->push(msg);
}
if (settings.m_useReverseAPI) if (settings.m_useReverseAPI)
{ {
@ -412,7 +452,7 @@ int AMMod::webapiSettingsGet(
webapiFormatChannelSettings(response, m_settings); webapiFormatChannelSettings(response, m_settings);
SWGSDRangel::SWGCWKeyerSettings *apiCwKeyerSettings = response.getAmModSettings()->getCwKeyer(); SWGSDRangel::SWGCWKeyerSettings *apiCwKeyerSettings = response.getAmModSettings()->getCwKeyer();
const CWKeyerSettings& cwKeyerSettings = m_basebandSource->getCWKeyer().getSettings(); const CWKeyerSettings& cwKeyerSettings = getCWKeyer()->getSettings();
CWKeyer::webapiFormatChannelSettings(apiCwKeyerSettings, cwKeyerSettings); CWKeyer::webapiFormatChannelSettings(apiCwKeyerSettings, cwKeyerSettings);
return 200; return 200;
@ -440,11 +480,11 @@ int AMMod::webapiSettingsPutPatch(
if (channelSettingsKeys.contains("cwKeyer")) if (channelSettingsKeys.contains("cwKeyer"))
{ {
SWGSDRangel::SWGCWKeyerSettings *apiCwKeyerSettings = response.getAmModSettings()->getCwKeyer(); SWGSDRangel::SWGCWKeyerSettings *apiCwKeyerSettings = response.getAmModSettings()->getCwKeyer();
CWKeyerSettings cwKeyerSettings = m_basebandSource->getCWKeyer().getSettings(); CWKeyerSettings cwKeyerSettings = getCWKeyer()->getSettings();
CWKeyer::webapiSettingsPutPatch(channelSettingsKeys, cwKeyerSettings, apiCwKeyerSettings); CWKeyer::webapiSettingsPutPatch(channelSettingsKeys, cwKeyerSettings, apiCwKeyerSettings);
CWKeyer::MsgConfigureCWKeyer *msgCwKeyer = CWKeyer::MsgConfigureCWKeyer::create(cwKeyerSettings, force); CWKeyer::MsgConfigureCWKeyer *msgCwKeyer = CWKeyer::MsgConfigureCWKeyer::create(cwKeyerSettings, force);
m_basebandSource->getCWKeyer().getInputMessageQueue()->push(msgCwKeyer); getCWKeyer()->getInputMessageQueue()->push(msgCwKeyer);
if (m_guiMessageQueue) // forward to GUI if any if (m_guiMessageQueue) // forward to GUI if any
{ {
@ -615,9 +655,13 @@ void AMMod::webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings& respons
void AMMod::webapiFormatChannelReport(SWGSDRangel::SWGChannelReport& response) void AMMod::webapiFormatChannelReport(SWGSDRangel::SWGChannelReport& response)
{ {
response.getAmModReport()->setChannelPowerDb(CalcDb::dbPower(getMagSq())); response.getAmModReport()->setChannelPowerDb(CalcDb::dbPower(getMagSq()));
if (m_running)
{
response.getAmModReport()->setAudioSampleRate(m_basebandSource->getAudioSampleRate()); response.getAmModReport()->setAudioSampleRate(m_basebandSource->getAudioSampleRate());
response.getAmModReport()->setChannelSampleRate(m_basebandSource->getChannelSampleRate()); response.getAmModReport()->setChannelSampleRate(m_basebandSource->getChannelSampleRate());
} }
}
void AMMod::webapiReverseSendSettings(QList<QString>& channelSettingsKeys, const AMModSettings& settings, bool force) void AMMod::webapiReverseSendSettings(QList<QString>& channelSettingsKeys, const AMModSettings& settings, bool force)
{ {
@ -654,7 +698,7 @@ void AMMod::webapiReverseSendCWSettings(const CWKeyerSettings& cwKeyerSettings)
swgAMModSettings->setCwKeyer(new SWGSDRangel::SWGCWKeyerSettings()); swgAMModSettings->setCwKeyer(new SWGSDRangel::SWGCWKeyerSettings());
SWGSDRangel::SWGCWKeyerSettings *apiCwKeyerSettings = swgAMModSettings->getCwKeyer(); SWGSDRangel::SWGCWKeyerSettings *apiCwKeyerSettings = swgAMModSettings->getCwKeyer();
m_basebandSource->getCWKeyer().webapiFormatChannelSettings(apiCwKeyerSettings, cwKeyerSettings); getCWKeyer()->webapiFormatChannelSettings(apiCwKeyerSettings, cwKeyerSettings);
QString channelSettingsURL = QString("http://%1:%2/sdrangel/deviceset/%3/channel/%4/settings") QString channelSettingsURL = QString("http://%1:%2/sdrangel/deviceset/%3/channel/%4/settings")
.arg(m_settings.m_reverseAPIAddress) .arg(m_settings.m_reverseAPIAddress)
@ -756,10 +800,10 @@ void AMMod::webapiFormatChannelSettings(
if (force) if (force)
{ {
const CWKeyerSettings& cwKeyerSettings = m_basebandSource->getCWKeyer().getSettings(); const CWKeyerSettings& cwKeyerSettings = getCWKeyer()->getSettings();
swgAMModSettings->setCwKeyer(new SWGSDRangel::SWGCWKeyerSettings()); swgAMModSettings->setCwKeyer(new SWGSDRangel::SWGCWKeyerSettings());
SWGSDRangel::SWGCWKeyerSettings *apiCwKeyerSettings = swgAMModSettings->getCwKeyer(); SWGSDRangel::SWGCWKeyerSettings *apiCwKeyerSettings = swgAMModSettings->getCwKeyer();
m_basebandSource->getCWKeyer().webapiFormatChannelSettings(apiCwKeyerSettings, cwKeyerSettings); getCWKeyer()->webapiFormatChannelSettings(apiCwKeyerSettings, cwKeyerSettings);
} }
if (settings.m_channelMarker && (channelSettingsKeys.contains("channelMarker") || force)) if (settings.m_channelMarker && (channelSettingsKeys.contains("channelMarker") || force))
@ -800,25 +844,32 @@ void AMMod::networkManagerFinished(QNetworkReply *reply)
double AMMod::getMagSq() const double AMMod::getMagSq() const
{ {
if (m_running) {
return m_basebandSource->getMagSq(); return m_basebandSource->getMagSq();
} }
return 0;
}
CWKeyer *AMMod::getCWKeyer() CWKeyer *AMMod::getCWKeyer()
{ {
return &m_basebandSource->getCWKeyer(); return &m_cwKeyer;
}
void AMMod::setLevelMeter(QObject *levelMeter)
{
connect(m_basebandSource, SIGNAL(levelChanged(qreal, qreal, int)), levelMeter, SLOT(levelChanged(qreal, qreal, int)));
} }
int AMMod::getAudioSampleRate() const int AMMod::getAudioSampleRate() const
{ {
if (m_running) {
return m_basebandSource->getAudioSampleRate(); return m_basebandSource->getAudioSampleRate();
} }
return 0;
}
int AMMod::getFeedbackAudioSampleRate() const int AMMod::getFeedbackAudioSampleRate() const
{ {
if (m_running) {
return m_basebandSource->getFeedbackAudioSampleRate(); return m_basebandSource->getFeedbackAudioSampleRate();
} }
return 0;
}

View File

@ -28,6 +28,7 @@
#include <QNetworkRequest> #include <QNetworkRequest>
#include "dsp/basebandsamplesource.h" #include "dsp/basebandsamplesource.h"
#include "dsp/cwkeyer.h"
#include "channel/channelapi.h" #include "channel/channelapi.h"
#include "util/message.h" #include "util/message.h"
@ -235,7 +236,7 @@ public:
uint32_t getNumberOfDeviceStreams() const; uint32_t getNumberOfDeviceStreams() const;
double getMagSq() const; double getMagSq() const;
CWKeyer *getCWKeyer(); CWKeyer *getCWKeyer();
void setLevelMeter(QObject *levelMeter); void setLevelMeter(QObject *levelMeter) { m_levelMeter = levelMeter; }
int getAudioSampleRate() const; int getAudioSampleRate() const;
int getFeedbackAudioSampleRate() const; int getFeedbackAudioSampleRate() const;
@ -250,6 +251,7 @@ private:
DeviceAPI* m_deviceAPI; DeviceAPI* m_deviceAPI;
QThread *m_thread; QThread *m_thread;
bool m_running;
AMModBaseband* m_basebandSource; AMModBaseband* m_basebandSource;
AMModSettings m_settings; AMModSettings m_settings;
@ -264,6 +266,8 @@ private:
QNetworkAccessManager *m_networkManager; QNetworkAccessManager *m_networkManager;
QNetworkRequest m_networkRequest; QNetworkRequest m_networkRequest;
CWKeyer m_cwKeyer;
QObject *m_levelMeter;
virtual bool handleMessage(const Message& cmd); virtual bool handleMessage(const Message& cmd);
void applySettings(const AMModSettings& settings, bool force = false); void applySettings(const AMModSettings& settings, bool force = false);

View File

@ -21,6 +21,7 @@
#include "dsp/upchannelizer.h" #include "dsp/upchannelizer.h"
#include "dsp/dspengine.h" #include "dsp/dspengine.h"
#include "dsp/dspcommands.h" #include "dsp/dspcommands.h"
#include "dsp/cwkeyer.h"
#include "ammodbaseband.h" #include "ammodbaseband.h"
@ -171,8 +172,8 @@ bool AMModBaseband::handleMessage(const Message& cmd)
qDebug() << "AMModBaseband::handleMessage: MsgConfigureCWKeyer"; qDebug() << "AMModBaseband::handleMessage: MsgConfigureCWKeyer";
const CWKeyer::MsgConfigureCWKeyer& cfg = (CWKeyer::MsgConfigureCWKeyer&) cmd; const CWKeyer::MsgConfigureCWKeyer& cfg = (CWKeyer::MsgConfigureCWKeyer&) cmd;
CWKeyer::MsgConfigureCWKeyer *notif = new CWKeyer::MsgConfigureCWKeyer(cfg); CWKeyer::MsgConfigureCWKeyer *notif = new CWKeyer::MsgConfigureCWKeyer(cfg);
CWKeyer& cwKeyer = m_source.getCWKeyer(); CWKeyer *cwKeyer = m_source.getCWKeyer();
cwKeyer.getInputMessageQueue()->push(notif); cwKeyer->getInputMessageQueue()->push(notif);
return true; return true;
} }

View File

@ -63,7 +63,7 @@ public:
void reset(); void reset();
void pull(const SampleVector::iterator& begin, unsigned int nbSamples); void pull(const SampleVector::iterator& begin, unsigned int nbSamples);
MessageQueue *getInputMessageQueue() { return &m_inputMessageQueue; } //!< Get the queue for asynchronous inbound communication MessageQueue *getInputMessageQueue() { return &m_inputMessageQueue; } //!< Get the queue for asynchronous inbound communication
CWKeyer& getCWKeyer() { return m_source.getCWKeyer(); } void setCWKeyer(CWKeyer *cwKeyer) { m_source.setCWKeyer(cwKeyer); }
double getMagSq() const { return m_source.getMagSq(); } double getMagSq() const { return m_source.getMagSq(); }
int getAudioSampleRate() const { return m_source.getAudioSampleRate(); } int getAudioSampleRate() const { return m_source.getAudioSampleRate(); }
int getFeedbackAudioSampleRate() const { return m_source.getFeedbackAudioSampleRate(); } int getFeedbackAudioSampleRate() const { return m_source.getFeedbackAudioSampleRate(); }

View File

@ -19,6 +19,7 @@
#include <QDebug> #include <QDebug>
#include "dsp/datafifo.h" #include "dsp/datafifo.h"
#include "dsp/cwkeyer.h"
#include "util/messagequeue.h" #include "util/messagequeue.h"
#include "maincore.h" #include "maincore.h"
@ -35,7 +36,8 @@ AMModSource::AMModSource() :
m_levelCalcCount(0), m_levelCalcCount(0),
m_peakLevel(0.0f), m_peakLevel(0.0f),
m_levelSum(0.0f), m_levelSum(0.0f),
m_ifstream(nullptr) m_ifstream(nullptr),
m_cwKeyer(nullptr)
{ {
m_audioFifo.setLabel("AMModSource.m_audioFifo"); m_audioFifo.setLabel("AMModSource.m_audioFifo");
m_feedbackAudioFifo.setLabel("AMModSource.m_feedbackAudioFifo"); m_feedbackAudioFifo.setLabel("AMModSource.m_feedbackAudioFifo");
@ -218,14 +220,18 @@ void AMModSource::pullAF(Real& sample)
case AMModSettings::AMModInputCWTone: case AMModSettings::AMModInputCWTone:
Real fadeFactor; Real fadeFactor;
if (m_cwKeyer.getSample()) if (!m_cwKeyer) {
break;
}
if (m_cwKeyer->getSample())
{ {
m_cwKeyer.getCWSmoother().getFadeSample(true, fadeFactor); m_cwKeyer->getCWSmoother().getFadeSample(true, fadeFactor);
sample = m_toneNco.next() * fadeFactor; sample = m_toneNco.next() * fadeFactor;
} }
else else
{ {
if (m_cwKeyer.getCWSmoother().getFadeSample(false, fadeFactor)) if (m_cwKeyer->getCWSmoother().getFadeSample(false, fadeFactor))
{ {
sample = m_toneNco.next() * fadeFactor; sample = m_toneNco.next() * fadeFactor;
} }
@ -320,8 +326,12 @@ void AMModSource::applyAudioSampleRate(int sampleRate)
m_interpolatorDistance = (Real) sampleRate / (Real) m_channelSampleRate; m_interpolatorDistance = (Real) sampleRate / (Real) m_channelSampleRate;
m_interpolator.create(48, sampleRate, m_settings.m_rfBandwidth / 2.2, 3.0); m_interpolator.create(48, sampleRate, m_settings.m_rfBandwidth / 2.2, 3.0);
m_toneNco.setFreq(m_settings.m_toneFrequency, sampleRate); m_toneNco.setFreq(m_settings.m_toneFrequency, sampleRate);
m_cwKeyer.setSampleRate(sampleRate);
m_cwKeyer.reset(); if (m_cwKeyer)
{
m_cwKeyer->setSampleRate(sampleRate);
m_cwKeyer->reset();
}
QList<ObjectPipe*> pipes; QList<ObjectPipe*> pipes;
MainCore::instance()->getMessagePipes().getMessagePipes(m_channel, "reportdemod", pipes); MainCore::instance()->getMessagePipes().getMessagePipes(m_channel, "reportdemod", pipes);

View File

@ -31,12 +31,12 @@
#include "dsp/ncof.h" #include "dsp/ncof.h"
#include "dsp/interpolator.h" #include "dsp/interpolator.h"
#include "util/movingaverage.h" #include "util/movingaverage.h"
#include "dsp/cwkeyer.h"
#include "audio/audiofifo.h" #include "audio/audiofifo.h"
#include "ammodsettings.h" #include "ammodsettings.h"
class ChannelAPI; class ChannelAPI;
class CWKeyer;
class AMModSource : public QObject, public ChannelSampleSource class AMModSource : public QObject, public ChannelSampleSource
{ {
@ -57,7 +57,8 @@ public:
void applyFeedbackAudioSampleRate(int sampleRate); void applyFeedbackAudioSampleRate(int sampleRate);
int getAudioSampleRate() const { return m_audioSampleRate; } int getAudioSampleRate() const { return m_audioSampleRate; }
int getFeedbackAudioSampleRate() const { return m_feedbackAudioSampleRate; } int getFeedbackAudioSampleRate() const { return m_feedbackAudioSampleRate; }
CWKeyer& getCWKeyer() { return m_cwKeyer; } void setCWKeyer(CWKeyer *cwKeyer) { m_cwKeyer = cwKeyer; }
CWKeyer* getCWKeyer() { return m_cwKeyer; }
double getMagSq() const { return m_magsq; } double getMagSq() const { return m_magsq; }
void getLevels(qreal& rmsLevel, qreal& peakLevel, int& numSamples) const void getLevels(qreal& rmsLevel, qreal& peakLevel, int& numSamples) const
{ {
@ -112,7 +113,7 @@ private:
Real m_levelSum; Real m_levelSum;
std::ifstream *m_ifstream; std::ifstream *m_ifstream;
CWKeyer m_cwKeyer; CWKeyer *m_cwKeyer;
QRecursiveMutex m_mutex; QRecursiveMutex m_mutex;